yatex

view yatexhie.el @ 270:1b4e0acd0106

Include newpage.rb.
author HIROSE Yuuji <yuuji@gentei.org>
date Thu, 10 May 2012 11:10:13 +0900
parents 0734be649cb8
children 5921f28ef77c
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX hierarchy browser.
3 ;;; yatexhie.el
4 ;;; (c)1995-2012 by HIROSE Yuuji [yuuji@yatex.org]
5 ;;; Last modified Mon Jan 9 20:19:06 2012 on firestorm
6 ;;; $Id$
8 ;; ----- Customizable variables -----
9 (defvar YaTeX-hierarchy-ignore-heading-regexp
10 "\\$[A-Z][a-z]+: .* \\$\\|-\\*- .* -\\*-"
11 "*Regexp of lines to ignore as files' headline.")
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)extsame P)revsame u)p K)illbuf RET)select"
19 (if (and YaTeX-emacs-19 window-system) " Mouse2)select" "")
20 " ?)help"))
21 (defvar YaTeX-hierarchy-saved-wc nil "Saved window configuration.")
23 ;; ----- Functions for parsing hierarchy -----
25 (defun YaTeX-all-included-files (&optional file)
26 "Return all included files from FILE as a list.
27 If FILE is nil, use current buffer."
28 (save-excursion
29 (let ((include-regex (concat YaTeX-ec-regexp
30 "\\(\\(input\\)\\|" ;match#2
31 "\\(include\\)\\)\\b")) ;match#3
32 list file (cb (current-buffer)))
33 (if file (set-buffer (YaTeX-switch-to-buffer file t)))
34 (goto-char (point-min))
35 (while (YaTeX-re-search-active-forward
36 include-regex YaTeX-comment-prefix nil t)
37 (cond
38 ((match-beginning 2) ;\input, {} is optional, 1 argument
39 (skip-chars-forward " {")
40 (setq file (buffer-substring
41 (point)
42 (progn
43 (skip-chars-forward
44 (concat "^ \t\n\r" YaTeX-ec-regexp "{}"))
45 (point)))))
46 ((and (match-beginning 3) (looking-at "{"))
47 (skip-chars-forward "{")
48 (setq file (buffer-substring
49 (point)
50 (progn
51 (forward-char -1) (forward-list 1) (1- (point)))))))
52 (or (string-match YaTeX-default-TeX-extensions file)
53 (setq file (concat file ".tex")))
54 (setq list (cons file list)))
55 (set-buffer cb)
56 (nreverse list))))
58 (defun YaTeX-document-hierarchy (&optional file basedir)
59 "Return the document hierarchy beginning from FILE as a list.
60 If FILE is nil, beginning with current buffer's file."
61 (setq file (or file buffer-file-name))
62 (and YaTeX-search-file-from-top-directory
63 (not (file-exists-p file))
64 (string-match "^[^/].*/" file)
65 (setq file (expand-file-name file basedir)))
66 (message "Parsing [%s]..." (file-name-nondirectory file))
67 (prog1
68 (save-excursion
69 (if (or (file-exists-p file) (null file))
70 (progn
71 (if file
72 (let ((parent buffer-file-name))
73 (YaTeX-switch-to-buffer file t) ;set buffer to file
74 (or YaTeX-parent-file
75 (YaTeX-get-builtin "!")
76 (setq YaTeX-parent-file parent))))
77 (cons (buffer-file-name (current-buffer))
78 (mapcar '(lambda (f) ;return value
79 (YaTeX-document-hierarchy f basedir))
80 (YaTeX-all-included-files))))))
81 (message "Parsing [%s]...done" (file-name-nondirectory file))))
83 ;; ----- Functions for displaying hierarchy -----
85 (defun YaTeX-hierarchy-get-file-heading (file)
86 "Get a FILE's heading."
87 (save-excursion
88 (set-buffer (find-file-noselect file))
89 (save-excursion
90 (let (p)
91 (goto-char (point-min))
92 (cond
93 ((re-search-forward
94 (concat YaTeX-ec-regexp YaTeX-sectioning-regexp) nil t)
95 (search-forward "{")
96 (forward-char -1)
97 (setq p (condition-case nil
98 (progn (forward-list 1) (1- (point)))
99 (error (point-end-of-line))))
100 (goto-char (1+ (match-beginning 0)))
101 (skip-chars-forward " \t\n")
102 (buffer-substring (point) (min (point-end-of-line) p)))
103 ((catch 'found
104 (while (re-search-forward "^ *%\\([^#]\\)" nil t)
105 (or (re-search-forward
106 YaTeX-hierarchy-ignore-heading-regexp
107 (point-end-of-line) t)
108 (throw 'found t))))
109 (beginning-of-line)
110 (search-forward "%")
111 (skip-chars-forward "% \t")
112 (buffer-substring (point) (point-end-of-line)))
113 (t ""))))))
115 (defun YaTeX-display-a-hierachy (hier level)
116 "Put a HIER of document hierarchy.
117 LEVEL is including depth."
118 (message "Formatting hierarchy buffer...")
119 (let ((lastatomcol 0) list i p)
120 (cond
121 ((listp hier)
122 (setq list hier)
123 (while list
124 (YaTeX-display-a-hierachy (car list) (1+ level))
125 (setq list (cdr list))))
126 ((stringp hier) ;is an atom
127 (insert " ")
128 (setq i level)
129 (while (> i 2)
130 (insert "| ")
131 (setq i (1- i)))
132 (if (> level 1) (insert "+---"))
133 (setq p (point))
134 (insert (or (buffer-name (get-file-buffer hier))
135 (file-name-nondirectory hier)))
136 (if (and window-system YaTeX-emacs-19)
137 (put-text-property p (point) 'mouse-face 'underline))
138 (insert " ")
139 (indent-to-column (1- (/ (window-width) 2)))
140 (insert "% " (YaTeX-hierarchy-get-file-heading hier))
141 (insert "\n"))))
142 (message "Formatting hierarchy buffer..."))
144 (defun YaTeX-display-hierarchy (file &optional use-default)
145 "Display document hierarchy that is beginning from FILE."
146 (interactive "P")
147 (setq YaTeX-hierarchy-saved-wc
148 (list (current-window-configuration)
149 (and (featurep 'windows)
150 (boundp 'win:current-config)
151 win:current-config)))
152 (let*((b-in (YaTeX-get-builtin "!"))
153 default)
154 ;;む・踉擦鵙髟阡鮫このへんの仕様どう瘢雹したらいいか良く分からん...
155 (if default (setq default (expand-file-name default)))
156 (YaTeX-visit-main t) ;move to parent file
157 (setq default buffer-file-name)
158 (setq file
159 (or (if use-default default file)
160 (read-file-name
161 (format
162 "Main .tex file%s: "
163 (if default
164 (format "(default %s)"(file-name-nondirectory default))
165 ""))
166 "" default 1))))
167 (setq file (expand-file-name file))
168 (setq YaTeX-hierarchy-current-main file)
169 (let ((dbuf "*document hierarchy*")
170 (topdir default-directory))
171 (YaTeX-showup-buffer dbuf nil t)
172 (set-buffer (get-buffer dbuf))
173 (setq truncate-lines t)
174 (let ((buffer-read-only nil))
175 (erase-buffer)
176 (YaTeX-display-a-hierachy (YaTeX-document-hierarchy file topdir) 0))
177 (goto-char (point-min))
178 (YaTeX-hierarchy-next 0)
179 (set-buffer-modified-p nil)
180 (YaTeX-hierarchy-mode)
181 ))
183 (defun YaTeX-display-hierarchy-directly ()
184 "Same as YaTeX-display-hierarchy. Call from mouse."
185 (interactive)
186 (YaTeX-display-hierarchy nil t))
188 (defun YaTeX-hierarchy-mode ()
189 "Major mode to browse and select document hierarchy.
191 \\[YaTeX-hierarchy-next] next line
192 \\[YaTeX-hierarchy-prev] previous line
193 \\[YaTeX-hierarchy-forward] move forward in the same level
194 \\[YaTeX-hierarchy-backward] move backward in the same level
195 \\[YaTeX-hierarchy-up-document] move to parent file
196 \\[delete-other-windows] delete other windows
197 \\[other-window] other window
198 \\[shrink-window] shrink window
199 \\[enlarge-window] enlarge window
200 \\[YaTeX-hierarchy-show] show file contents in the next window
201 \\[YaTeX-hierarchy-scroll-up] scroll up file contents buffer
202 \\[YaTeX-hierarchy-scroll-down] scroll down file contents buffer
203 \\[YaTeX-hierarchy-top] show the top of file contents
204 \\[YaTeX-hierarchy-bottom] show the bottom of file contents
205 \\[YaTeX-hierarchy-lastpos] return to the previous position
206 \\[YaTeX-hierarchy-select] select file
207 \\[YaTeX-hierarchy-mouse-select] select
208 "
209 (setq major-mode 'YaTeX-hierarchy-mode
210 mode-name "YaTeX hier")
211 (use-local-map YaTeX-hierarchy-mode-map)
212 (setq buffer-read-only t)
213 (message YaTeX-hierarchy-buffer-message))
215 ;; ----- Subfunctions for interactive functions -----
216 (defun YaTeX-hierarchy-get-current-file-buffer ()
217 "Return the buffer associated with current line's file."
218 (let ((file (buffer-substring
219 (point)
220 (save-excursion
221 (skip-chars-forward "^ \t" (point-end-of-line)) (point))))
222 (hilit-auto-highlight) buffer)
223 (set-buffer (find-file-noselect YaTeX-hierarchy-current-main))
224 (if (get-buffer file) ;buffer is active
225 (setq buffer (get-buffer file)) ;may contain `<2>'
226 (if (string-match "<[2-9]>$" file)
227 (setq file (substring file 0 -3)))
228 (save-excursion
229 (setq buffer (YaTeX-switch-to-buffer file t)))))) ; open it!
231 ;; ----- Interactive functions -----
232 (defun YaTeX-hierarchy-next (arg &optional quiet)
233 "Move to next line's file in YaTeX document hierarchy buffer."
234 (interactive "p")
235 (forward-line arg)
236 (skip-chars-forward "- +\\|")
237 (if (and (/= arg 0) (not quiet))
238 (YaTeX-hierarchy-select t))
239 (message YaTeX-hierarchy-buffer-message))
241 (defun YaTeX-hierarchy-prev (arg)
242 "Move to previous line's file in YaTeX document hierarchy buffer."
243 (interactive "p")
244 (YaTeX-hierarchy-next (- arg)))
246 (defun YaTeX-hierarchy-next-line (arg)
247 (interactive "p")
248 (YaTeX-hierarchy-next arg t))
250 (defun YaTeX-hierarchy-prev-line (arg)
251 (interactive "p")
252 (YaTeX-hierarchy-next (- arg) t))
254 (defun YaTeX-hierarchy-forward (arg)
255 "Move to forward file in same hierarchy level."
256 (interactive "p")
257 (YaTeX-hierarchy-next 0)
258 (let ((p (point))(column (current-column)) (i (if (> arg 0) arg (- arg))))
259 (if (= column 0) (error "Not on file line."))
260 (while (> i 0)
261 (if (catch 'found
262 (while (and (not (eobp)) (not (bobp)))
263 (forward-line (if (> arg 0) 1 -1))
264 (move-to-column column)
265 (if (looking-at "[- +\\|]") nil
266 (YaTeX-hierarchy-next 0)
267 (if (= (current-column) column) (throw 'found t)))
268 (beginning-of-line)))
269 nil
270 (goto-char p)
271 (error "No same level file."))
272 (setq i (1- i)))))
274 (defun YaTeX-hierarchy-backward (arg)
275 "Move to backward file in same hierarchy level."
276 (interactive "p")
277 (YaTeX-hierarchy-forward (- arg)))
279 (defun YaTeX-hierarchy-up-document ()
280 "Up level, that is, move to parent file position."
281 (interactive)
282 (YaTeX-hierarchy-next 0) ;adjust column
283 (let ((p (point)) (line (count-lines (point-min) (point))) column)
284 (if (or (<= line 1) (< (current-column) 6))
285 (message "No more parent")
286 (backward-char 1)
287 (or (= (char-after (point)) ?-) (error "Unexpected hierarchy buffer"))
288 (setq column (current-column))
289 (while (and (> line 1) (looking-at "[- +\\|]"))
290 (forward-line -1)
291 (move-to-column column))
292 (YaTeX-hierarchy-next 0)
293 (push-mark p t)
294 (message "Mark set to last position"))))
296 (defun YaTeX-hierarchy-kill-buffer (arg)
297 "Kill buffer associated with current line's file."
298 (interactive "p")
299 (YaTeX-hierarchy-next 0) ;move to file name column
300 (if (bolp) (error "Not on file name line"))
301 (let ((file (buffer-substring
302 (point)
303 (progn (skip-chars-forward "^ \t") (point)))))
304 (YaTeX-hierarchy-next arg)
305 (cond
306 ((get-buffer file)
307 (kill-buffer (get-buffer file))
308 (message "Buffer [%s] was killed" file))
309 (t (message "Buffer [%s] is not active." file)))))
311 (defun YaTeX-hierarchy-select (arg)
312 "Select current line's file in YaTeX document hierarchy buffer.
313 If ARG is non-nil, show the buffer in the next window."
314 (interactive "P")
315 (beginning-of-line)
316 (skip-chars-forward "- +\\|")
317 (or (eolp)
318 (let ((buffer (YaTeX-hierarchy-get-current-file-buffer)))
319 (if buffer ;if file was found
320 (if arg
321 (YaTeX-showup-buffer buffer nil)
322 (if (and YaTeX-emacs-19 window-system
323 (get-buffer-window buffer t))
324 (goto-buffer-window buffer) ;select currently displaying
325 (YaTeX-switch-to-buffer-other-window buffer)))))))
327 (defun YaTeX-hierarchy-show ()
328 "Show current line's file in the next window."
329 (interactive)
330 (YaTeX-hierarchy-select t))
332 (defun YaTeX-hierarchy-mouse-select (event)
333 (interactive "e")
334 (mouse-set-point event)
335 (YaTeX-hierarchy-select nil))
337 (defun YaTeX-hierarchy-quit ()
338 "Quit from YaTeX-hierarchy buffer and restore window configuration."
339 (interactive)
340 (if (or (not (featurep 'windows))
341 (car YaTeX-hierarchy-saved-wc)
342 (and (= (car (cdr YaTeX-hierarchy-saved-wc)) win:current-config)))
343 (set-window-configuration (car YaTeX-hierarchy-saved-wc))
344 (bury-buffer nil)))
346 (defun YaTeX-hierarchy-scroll-up (arg &optional action)
347 "Scroll up file contents of YaTeX-hierarchy."
348 (interactive "P")
349 (YaTeX-hierarchy-next 0 t)
350 (let*((bufname (buffer-substring
351 (point)
352 (save-excursion (skip-chars-forward "^ \t") (point))))
353 (buf (get-buffer bufname))
354 (cw (selected-window)))
355 (cond
356 ((and buf (get-buffer-window buf))
357 (select-window (get-buffer-window buf)))
358 ((and buf (YaTeX-showup-buffer buf nil t)) t)
359 (t (YaTeX-hierarchy-select nil)))
360 (unwind-protect
361 (cond
362 ((eq action 'down) (scroll-down arg))
363 ((eq action 'top) (beginning-of-buffer))
364 ((eq action 'bottom) (end-of-buffer))
365 ((eq action 'last) (exchange-point-and-mark))
366 (t (scroll-up arg)))
367 (select-window cw))))
369 (defun YaTeX-hierarchy-scroll-down (arg)
370 "Scroll down file contents of YaTeX-hierarchy."
371 (interactive "P")
372 (YaTeX-hierarchy-scroll-up arg 'down))
374 (defun YaTeX-hierarchy-top ()
375 "Show the top of YaTeX-hierarchy inspection buffer's."
376 (interactive)
377 (YaTeX-hierarchy-scroll-up nil 'top)
378 )
380 (defun YaTeX-hierarchy-bottom ()
381 "Show the top of YaTeX-hierarchy inspection buffer's."
382 (interactive)
383 (YaTeX-hierarchy-scroll-up nil 'bottom)
384 )
386 (defun YaTeX-hierarchy-lastpos ()
387 "Go to last position in YaTeX-hierarchy buffer."
388 (interactive)
389 (YaTeX-hierarchy-scroll-up nil 'last)
390 )
392 ;; ----- Setting up keymap -----
393 (defvar YaTeX-hierarchy-mode-map nil "Keymap used in YaTeX-hierarchy-mode.")
394 (if YaTeX-hierarchy-mode-map nil
395 (setq YaTeX-hierarchy-mode-map (make-sparse-keymap))
396 (define-key YaTeX-hierarchy-mode-map "n" 'YaTeX-hierarchy-next)
397 (define-key YaTeX-hierarchy-mode-map "p" 'YaTeX-hierarchy-prev)
398 (define-key YaTeX-hierarchy-mode-map "j" 'YaTeX-hierarchy-next-line)
399 (define-key YaTeX-hierarchy-mode-map "k" 'YaTeX-hierarchy-prev-line)
400 (substitute-all-key-definition
401 'next-line 'YaTeX-hierarchy-next-line YaTeX-hierarchy-mode-map)
402 (substitute-all-key-definition
403 'previous-line 'YaTeX-hierarchy-prev-line YaTeX-hierarchy-mode-map)
404 (define-key YaTeX-hierarchy-mode-map "N" 'YaTeX-hierarchy-forward)
405 (define-key YaTeX-hierarchy-mode-map "P" 'YaTeX-hierarchy-backward)
406 (define-key YaTeX-hierarchy-mode-map "u" 'YaTeX-hierarchy-up-document)
407 (define-key YaTeX-hierarchy-mode-map "K" 'YaTeX-hierarchy-kill-buffer)
408 (define-key YaTeX-hierarchy-mode-map "1" 'delete-other-windows)
409 (define-key YaTeX-hierarchy-mode-map "o" 'other-window)
410 (define-key YaTeX-hierarchy-mode-map "-" 'shrink-window)
411 (define-key YaTeX-hierarchy-mode-map "+" 'enlarge-window)
412 (define-key YaTeX-hierarchy-mode-map "." 'YaTeX-hierarchy-show)
413 (define-key YaTeX-hierarchy-mode-map " " 'YaTeX-hierarchy-scroll-up)
414 (define-key YaTeX-hierarchy-mode-map "b" 'YaTeX-hierarchy-scroll-down)
415 (define-key YaTeX-hierarchy-mode-map "\C-?" 'YaTeX-hierarchy-scroll-down)
416 (define-key YaTeX-hierarchy-mode-map "\C-m" 'YaTeX-hierarchy-select)
417 (define-key YaTeX-hierarchy-mode-map "<" 'YaTeX-hierarchy-top)
418 (define-key YaTeX-hierarchy-mode-map ">" 'YaTeX-hierarchy-bottom)
419 (define-key YaTeX-hierarchy-mode-map "'" 'YaTeX-hierarchy-lastpos)
420 (define-key YaTeX-hierarchy-mode-map "g" 'YaTeX-hierarchy-select)
421 (define-key YaTeX-hierarchy-mode-map "q" 'YaTeX-hierarchy-quit)
422 (define-key YaTeX-hierarchy-mode-map "?" 'describe-mode)
423 (if (and YaTeX-emacs-19 window-system)
424 (define-key YaTeX-hierarchy-mode-map
425 [mouse-2] 'YaTeX-hierarchy-mouse-select))
426 )
428 (provide 'yatexhie)
429 ;;end of yatexhie.el