yatex

view yatexhie.el @ 52:5d94deabb9f9

Set YaTeX-indent-line to 'indent-line-function. Revise fill features.
author yuuji
date Sun, 22 Jan 1995 14:20:46 +0000
parents
children 5f4b18da14b3
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX hierarchy browser.
3 ;;; yatexhie.el
4 ;;; (c )1995 by HIROSE Yuuji [yuuji@ae.keio.ac.jp]
5 ;;; Last modified Sun Jan 22 23:15:25 1995 on landcruiser
6 ;;; $Id$
8 ;; ----- Customizable variables -----
9 (defvar YaTeX-hierarchy-inspect-mode t
10 "*Non-nil inspects the contents of file of cursor position.")
13 ;; ----- General variables -----
14 (defvar YaTeX-default-TeX-extensions "\\.\\(tex\\|sty\\)")
15 (defvar YaTeX-hierarchy-current-main nil)
16 (defvar YaTeX-hierarchy-buffer-message
17 (concat
18 "n)ext p)rev N)extsamelev P)revsamelev u)p K)ill-buffer RET)select"
19 (if (and YaTeX-emacs-19 window-system) " Mouse2)select" "")))
20 (defvar YaTeX-hierarchy-saved-wc nil "Saved window configuration.")
22 ;; ----- Functions for parsing hierarchy -----
24 (defun YaTeX-all-included-files (&optional file)
25 "Return all included files from FILE as a list.
26 If FILE is nil, use current buffer."
27 (save-excursion
28 (let ((include-regex (concat YaTeX-ec-regexp
29 "\\(\\(input\\)\\|" ;match#2
30 "\\(include\\)\\)\\b")) ;match#3
31 list file (cb (current-buffer)))
32 (if file (set-buffer (YaTeX-switch-to-buffer file t)))
33 (goto-char (point-min))
34 (while (YaTeX-re-search-active-forward
35 include-regex YaTeX-comment-prefix nil t)
36 (cond
37 ((match-beginning 2) ;\input, {} is optional, 1 argument
38 (skip-chars-forward " {")
39 (setq file (buffer-substring
40 (point)
41 (progn
42 (skip-chars-forward
43 (concat "^ \t\n\r" YaTeX-ec-regexp "{}"))
44 (point)))))
45 ((match-beginning 3)
46 (skip-chars-forward "{")
47 (setq file (buffer-substring
48 (point)
49 (progn
50 (forward-char -1) (forward-list 1) (1- (point)))))))
51 (or (string-match YaTeX-default-TeX-extensions file)
52 (setq file (concat file ".tex")))
53 (setq list (cons file list)))
54 (set-buffer cb)
55 (nreverse list))))
57 (defun YaTeX-document-hierarchy (&optional file)
58 "Return the document hierarchy beginning from FILE as a list.
59 If FILE is nil, beginning with current buffer's file."
60 (setq file (or file buffer-file-name))
61 (message "Parsing [%s]..." (file-name-nondirectory file))
62 (prog1
63 (save-excursion
64 (if (or (file-exists-p file) (null file))
65 (progn
66 (if file
67 (let ((parent buffer-file-name))
68 (YaTeX-switch-to-buffer file t) ;set buffer to file
69 (or YaTeX-parent-file
70 (YaTeX-get-builtin "!")
71 (setq YaTeX-parent-file parent))))
72 (cons (buffer-file-name (current-buffer))
73 (mapcar 'YaTeX-document-hierarchy ;return value
74 (YaTeX-all-included-files))))))
75 (message "Parsing [%s]...done" (file-name-nondirectory file))))
77 ;; ----- Functions for displaying hierarchy -----
79 (defun YaTeX-hierarchy-get-file-heading (file)
80 "Get a FILE's heading."
81 (save-excursion
82 (set-buffer (find-file-noselect file))
83 (save-excursion
84 (goto-char (point-min))
85 (cond
86 ((and
87 (YaTeX-re-search-active-forward
88 (concat YaTeX-ec-regexp YaTeX-sectioning-regexp)
89 YaTeX-comment-prefix nil t)
90 (re-search-forward "{\\([^}]+\\)}" nil t))
91 (goto-char (match-beginning 1))
92 (skip-chars-forward " \t\n")
93 (buffer-substring (point)
94 (min (point-end-of-line)
95 (match-end 1))))
96 ((re-search-forward "^ *%\\([^#]\\)" nil t)
97 (goto-char (match-beginning 1))
98 (skip-chars-forward " \t")
99 (buffer-substring (point) (point-end-of-line)))
100 (t "")))))
102 (defun YaTeX-display-a-hierachy (hier level)
103 "Put a HIER of document hierarchy.
104 LEVEL is including depth."
105 (message "Formatting hierarchy buffer...")
106 (let ((lastatomcol 0) list i p)
107 (cond
108 ((listp hier)
109 (setq list hier)
110 (while list
111 (YaTeX-display-a-hierachy (car list) (1+ level))
112 (setq list (cdr list))))
113 ((stringp hier) ;is an atom
114 (insert " ")
115 (setq i level)
116 (while (> i 2)
117 (insert "| ")
118 (setq i (1- i)))
119 (if (> level 1) (insert "+---"))
120 (setq p (point))
121 (insert (or (buffer-name (get-file-buffer hier))
122 (file-name-nondirectory hier)))
123 (if (and window-system YaTeX-emacs-19)
124 (put-text-property p (point) 'mouse-face 'underline))
125 (insert " ")
126 (indent-to-column (1- (/ (window-width) 2)))
127 (insert "% " (YaTeX-hierarchy-get-file-heading hier))
128 (insert "\n"))))
129 (message "Formatting hierarchy buffer..."))
131 (defun YaTeX-display-hierarchy (file &optional use-default)
132 "Display document hierarchy that is beginning from FILE."
133 (interactive "P")
134 (setq YaTeX-hierarchy-saved-wc
135 (list (current-window-configuration)
136 (and (featurep 'windows)
137 (boundp 'win:current-config)
138 win:current-config)))
139 (let*((b-in (YaTeX-get-builtin "!"))
140 (default (or YaTeX-parent-file
141 (and b-in (YaTeX-guess-parent b-in))
142 buffer-file-name)))
143 ;;む・踉擦鵙髟阡鮫このへんの仕様どう瘢雹したらいいか良く分からん...
144 (if default (setq default (expand-file-name default)))
145 (YaTeX-visit-main t) ;move to parent file
146 (setq file
147 (or (if use-default default file)
148 (read-file-name
149 (format
150 "Main .tex file%s: "
151 (if default
152 (format "(default %s)"(file-name-nondirectory default))
153 ""))
154 "" default 1))))
155 (setq file (expand-file-name file))
156 (setq YaTeX-hierarchy-current-main file)
157 (let ((dbuf "*document hierarchy*"))
158 (YaTeX-showup-buffer dbuf nil t)
159 (set-buffer (get-buffer dbuf))
160 (setq truncate-lines t)
161 (let ((buffer-read-only nil))
162 (erase-buffer)
163 (YaTeX-display-a-hierachy (YaTeX-document-hierarchy file) 0))
164 (goto-char (point-min))
165 (YaTeX-hierarchy-next 0)
166 (set-buffer-modified-p nil)
167 (YaTeX-hierarchy-mode)
168 ))
170 (defun YaTeX-display-hierarchy-directly ()
171 "Same as YaTeX-display-hierarchy. Call from mouse."
172 (interactive)
173 (YaTeX-display-hierarchy nil t))
175 (defun YaTeX-hierarchy-mode ()
176 "Major mode to browse and select document hierarchy.
178 \\[YaTeX-hierarchy-next] next line
179 \\[YaTeX-hierarchy-prev] previous line
180 \\[YaTeX-hierarchy-forward] move forward in same level
181 \\[YaTeX-hierarchy-backward] move backward in same level
182 \\[YaTeX-hierarchy-up-document] move to parent file
183 \\[delete-other-windows] delete other windows
184 \\[other-window] other window
185 \\[YaTeX-hierarchy-show] show buffer contents in the next window
186 \\[YaTeX-hierarchy-select] select file
187 \\[YaTeX-hierarchy-mouse-select] select
188 "
189 (setq major-mode 'YaTeX-hierarchy-mode
190 mode-name "YaTeX hier")
191 (use-local-map YaTeX-hierarchy-mode-map)
192 (setq buffer-read-only t)
193 (message YaTeX-hierarchy-buffer-message))
195 ;; ----- Subfunctions for interactive functions -----
196 (defun YaTeX-hierarchy-get-current-file-buffer ()
197 "Return the buffer associated with current line's file."
198 (let ((file (buffer-substring
199 (point)
200 (save-excursion
201 (skip-chars-forward "^ \t" (point-end-of-line)) (point))))
202 (hilit-auto-highlight) buffer)
203 (set-buffer (find-file-noselect YaTeX-hierarchy-current-main))
204 (if (get-buffer file) ;buffer is active
205 (setq buffer (get-buffer file)) ;may contain `<2>'
206 (if (string-match "<[2-9]>$" file)
207 (setq file (substring file 0 -3)))
208 (save-excursion
209 (setq buffer (YaTeX-switch-to-buffer file t)))))) ; open it!
211 ;; ----- Interactive functions -----
212 (defun YaTeX-hierarchy-next (arg &optional quiet)
213 "Move to next line's file in YaTeX document hierarchy buffer."
214 (interactive "p")
215 (forward-line arg)
216 (skip-chars-forward "- +\\|")
217 (if (and (/= arg 0) YaTeX-hierarchy-inspect-mode (not quiet))
218 (YaTeX-hierarchy-select t))
219 (message YaTeX-hierarchy-buffer-message))
221 (defun YaTeX-hierarchy-prev (arg)
222 "Move to previous line's file in YaTeX document hierarchy buffer."
223 (interactive "p")
224 (YaTeX-hierarchy-next (- arg)))
226 (defun YaTeX-hierarchy-next-line (arg)
227 (interactive "p")
228 (YaTeX-hierarchy-next arg t))
230 (defun YaTeX-hierarchy-prev-line (arg)
231 (interactive "p")
232 (YaTeX-hierarchy-next (- arg) t))
234 (defun YaTeX-hierarchy-forward (arg)
235 "Move to forward file in same hierarchy level."
236 (interactive "p")
237 (YaTeX-hierarchy-next 0)
238 (let ((p (point))(column (current-column)) (i (if (> arg 0) arg (- arg))))
239 (if (= column 0) (error "Not on file line."))
240 (while (> i 0)
241 (if (catch 'found
242 (while (and (not (eobp)) (not (bobp)))
243 (forward-line (if (> arg 0) 1 -1))
244 (move-to-column column)
245 (if (looking-at "[- +\\|]") nil
246 (YaTeX-hierarchy-next 0)
247 (if (= (current-column) column) (throw 'found t)))
248 (beginning-of-line)))
249 nil
250 (goto-char p)
251 (error "No same level file."))
252 (setq i (1- i)))))
254 (defun YaTeX-hierarchy-backward (arg)
255 "Move to backward file in same hierarchy level."
256 (interactive "p")
257 (YaTeX-hierarchy-forward (- arg)))
259 (defun YaTeX-hierarchy-up-document ()
260 "Up level, that is, move to parent file position."
261 (interactive)
262 (YaTeX-hierarchy-next 0) ;adjust column
263 (let ((p (point)) (line (count-lines (point-min) (point))) column)
264 (if (or (<= line 1) (< (current-column) 6))
265 (message "No more parent")
266 (backward-char 1)
267 (or (= (char-after (point)) ?-) (error "Unexpected hierarchy buffer"))
268 (setq column (current-column))
269 (while (and (> line 1) (looking-at "[- +\\|]"))
270 (forward-line -1)
271 (move-to-column column))
272 (YaTeX-hierarchy-next 0)
273 (push-mark p t)
274 (message "Mark set to last position"))))
276 (defun YaTeX-hierarchy-kill-buffer (arg)
277 "Kill buffer associated with current line's file."
278 (interactive "p")
279 (YaTeX-hierarchy-next 0) ;move to file name column
280 (if (bolp) (error "Not on file name line"))
281 (let ((file (buffer-substring (point) (point-end-of-line))))
282 (YaTeX-hierarchy-next arg)
283 (cond
284 ((get-buffer file)
285 (kill-buffer (get-buffer file))
286 (message "Buffer [%s] was killed" file))
287 (t (message "Buffer [%s] is not active." file)))))
289 (defun YaTeX-hierarchy-select (arg)
290 "Select current line's file in YaTeX document hierarchy buffer.
291 If ARG is non-nil, show the buffer in the next window."
292 (interactive "P")
293 (beginning-of-line)
294 (skip-chars-forward "- +\\|")
295 (or (eolp)
296 (let ((buffer (YaTeX-hierarchy-get-current-file-buffer)))
297 (if buffer ;if file was found
298 (if arg
299 (YaTeX-showup-buffer buffer nil)
300 (if (and YaTeX-emacs-19 window-system
301 (get-buffer-window buffer t))
302 (goto-buffer-window buffer) ;select currently displaying
303 (YaTeX-switch-to-buffer-other-window buffer)))))))
305 (defun YaTeX-hierarchy-show ()
306 "Show current line's file in the next window."
307 (interactive)
308 (YaTeX-hierarchy-select t))
310 (defun YaTeX-hierarchy-mouse-select (event)
311 (interactive "e")
312 (mouse-set-point event)
313 (YaTeX-hierarchy-select nil))
315 (defun YaTeX-hierarchy-quit ()
316 "Quit from YaTeX-hierarchy buffer and restore window configuration."
317 (interactive)
318 (if (or (not (featurep 'windows))
319 (car YaTeX-hierarchy-saved-wc)
320 (and (= (car (cdr YaTeX-hierarchy-saved-wc)) win:current-config)))
321 (set-window-configuration (car YaTeX-hierarchy-saved-wc))
322 (bury-buffer nil)))
324 (defun YaTeX-hierarchy-toggle-inspection (arg)
325 "Toggle inspection mode of YaTeX-hierarchy buffer."
326 (interactive "P")
327 (setq YaTeX-hierarchy-inspect-mode
328 (or arg (not YaTeX-hierarchy-inspect-mode)))
329 (message "YaTeX hierarchy inspection mode %s"
330 (if YaTeX-hierarchy-inspect-mode "ON" "OFF")))
332 ;; ----- Setting up keymap -----
333 (defvar YaTeX-hierarchy-mode-map nil "Keymap used in YaTeX-hierarchy-mode.")
334 (if YaTeX-hierarchy-mode-map nil
335 (setq YaTeX-hierarchy-mode-map (make-sparse-keymap))
336 (define-key YaTeX-hierarchy-mode-map "n" 'YaTeX-hierarchy-next)
337 (define-key YaTeX-hierarchy-mode-map "p" 'YaTeX-hierarchy-prev)
338 (define-key YaTeX-hierarchy-mode-map "j" 'YaTeX-hierarchy-next-line)
339 (define-key YaTeX-hierarchy-mode-map "k" 'YaTeX-hierarchy-prev-line)
340 (substitute-all-key-definition
341 'next-line 'YaTeX-hierarchy-next-line YaTeX-hierarchy-mode-map)
342 (substitute-all-key-definition
343 'previous-line 'YaTeX-hierarchy-prev-line YaTeX-hierarchy-mode-map)
344 (define-key YaTeX-hierarchy-mode-map "N" 'YaTeX-hierarchy-forward)
345 (define-key YaTeX-hierarchy-mode-map "P" 'YaTeX-hierarchy-backward)
346 (define-key YaTeX-hierarchy-mode-map "u" 'YaTeX-hierarchy-up-document)
347 (define-key YaTeX-hierarchy-mode-map "K" 'YaTeX-hierarchy-kill-buffer)
348 (define-key YaTeX-hierarchy-mode-map "1" 'delete-other-windows)
349 (define-key YaTeX-hierarchy-mode-map "o" 'other-window)
350 (define-key YaTeX-hierarchy-mode-map "." 'YaTeX-hierarchy-show)
351 (define-key YaTeX-hierarchy-mode-map "\C-m" 'YaTeX-hierarchy-select)
352 (define-key YaTeX-hierarchy-mode-map ";" 'YaTeX-hierarchy-toggle-inspection)
353 (define-key YaTeX-hierarchy-mode-map "q" 'YaTeX-hierarchy-quit)
354 (define-key YaTeX-hierarchy-mode-map "?" 'describe-mode)
355 (if (and YaTeX-emacs-19 window-system)
356 (define-key YaTeX-hierarchy-mode-map
357 [mouse-2] 'YaTeX-hierarchy-mouse-select))
358 )
360 (provide 'yatexhie)
361 ;;end of yatexhie.el