yatex

view yatexhie.el @ 626:5f6ffe70a6ca

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