yatex

view yatexhie.el @ 573:9e07513fc2ef

Do not escape URL when it has triple `%XX's
author HIROSE Yuuji <yuuji@gentei.org>
date Wed, 11 Sep 2019 09:27:10 +0900
parents d69fd7b1ac4d
children
line source
1 ;;; yatexhie.el --- YaTeX hierarchy browser
2 ;;;
3 ;;; (c)1995-2013 by HIROSE Yuuji [yuuji@yatex.org]
4 ;;; Last modified Sun Dec 21 14:05:20 2014 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.")
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 (function ;return value
79 (lambda (f)
80 (YaTeX-document-hierarchy f basedir)))
81 (YaTeX-all-included-files))))))
82 (message "Parsing [%s]...done" (file-name-nondirectory file))))
84 ;; ----- Functions for displaying hierarchy -----
86 (defun YaTeX-hierarchy-get-file-heading (file)
87 "Get a FILE's heading."
88 (save-excursion
89 (set-buffer (find-file-noselect file))
90 (save-excursion
91 (let (p)
92 (goto-char (point-min))
93 (cond
94 ((re-search-forward
95 (concat YaTeX-ec-regexp YaTeX-sectioning-regexp) nil t)
96 (search-forward "{")
97 (forward-char -1)
98 (setq p (condition-case nil
99 (progn (forward-list 1) (1- (point)))
100 (error (point-end-of-line))))
101 (goto-char (1+ (match-beginning 0)))
102 (skip-chars-forward " \t\n")
103 (buffer-substring (point) (min (point-end-of-line) p)))
104 ((catch 'found
105 (while (re-search-forward "^ *%\\([^#]\\)" nil t)
106 (or (re-search-forward
107 YaTeX-hierarchy-ignore-heading-regexp
108 (point-end-of-line) t)
109 (throw 'found t))))
110 (beginning-of-line)
111 (search-forward "%")
112 (skip-chars-forward "% \t")
113 (buffer-substring (point) (point-end-of-line)))
114 (t ""))))))
116 (defun YaTeX-display-a-hierachy (hier level)
117 "Put a HIER of document hierarchy.
118 LEVEL is including depth."
119 (message "Formatting hierarchy buffer...")
120 (let ((lastatomcol 0) list i p)
121 (cond
122 ((listp hier)
123 (setq list hier)
124 (while list
125 (YaTeX-display-a-hierachy (car list) (1+ level))
126 (setq list (cdr list))))
127 ((stringp hier) ;is an atom
128 (insert " ")
129 (setq i level)
130 (while (> i 2)
131 (insert "| ")
132 (setq i (1- i)))
133 (if (> level 1) (insert "+---"))
134 (setq p (point))
135 (insert (or (buffer-name (get-file-buffer hier))
136 (file-name-nondirectory hier)))
137 (if (and window-system YaTeX-emacs-19)
138 (put-text-property p (point) 'mouse-face 'underline))
139 (insert " ")
140 (indent-to-column (1- (/ (window-width) 2)))
141 (insert "% " (YaTeX-hierarchy-get-file-heading hier))
142 (insert "\n"))))
143 (message "Formatting hierarchy buffer..."))
145 (defun YaTeX-display-hierarchy (file &optional use-default)
146 "Display document hierarchy that is beginning from FILE."
147 (interactive "P")
148 (setq YaTeX-hierarchy-saved-wc
149 (list (current-window-configuration)
150 (and (featurep 'windows)
151 (boundp 'win:current-config)
152 win:current-config)))
153 (let*((b-in (YaTeX-get-builtin "!"))
154 default)
155 ;;む・踉擦鵙髟阡鮫このへんの仕様どう瘢雹したらいいか良く分からん...
156 (if default (setq default (expand-file-name default)))
157 (YaTeX-visit-main t) ;move to parent file
158 (setq default buffer-file-name)
159 (setq file
160 (or (if use-default default file)
161 (read-file-name
162 (format
163 "Main .tex file%s: "
164 (if default
165 (format "(default %s)"(file-name-nondirectory default))
166 ""))
167 "" default 1))))
168 (setq file (expand-file-name file))
169 (setq YaTeX-hierarchy-current-main file)
170 (let ((dbuf "*document hierarchy*")
171 (topdir default-directory))
172 (YaTeX-showup-buffer dbuf nil t)
173 (set-buffer (get-buffer dbuf))
174 (setq truncate-lines t)
175 (let ((buffer-read-only nil))
176 (erase-buffer)
177 (YaTeX-display-a-hierachy (YaTeX-document-hierarchy file topdir) 0))
178 (goto-char (point-min))
179 (YaTeX-hierarchy-next 0)
180 (set-buffer-modified-p nil)
181 (YaTeX-hierarchy-mode)
182 ))
184 (defun YaTeX-display-hierarchy-directly ()
185 "Same as YaTeX-display-hierarchy. Call from mouse."
186 (interactive)
187 (YaTeX-display-hierarchy nil t))
189 (defun YaTeX-hierarchy-mode ()
190 "Major mode to browse and select document hierarchy.
192 \\[YaTeX-hierarchy-next] next line
193 \\[YaTeX-hierarchy-prev] previous line
194 \\[YaTeX-hierarchy-forward] move forward in the same level
195 \\[YaTeX-hierarchy-backward] move backward in the same level
196 \\[YaTeX-hierarchy-up-document] move to parent file
197 \\[delete-other-windows] delete other windows
198 \\[other-window] other window
199 \\[shrink-window] shrink window
200 \\[enlarge-window] enlarge window
201 \\[YaTeX-hierarchy-show] show file contents in the next window
202 \\[YaTeX-hierarchy-scroll-up] scroll up file contents buffer
203 \\[YaTeX-hierarchy-scroll-down] scroll down file contents buffer
204 \\[YaTeX-hierarchy-top] show the top of file contents
205 \\[YaTeX-hierarchy-bottom] show the bottom of file contents
206 \\[YaTeX-hierarchy-lastpos] return to the previous position
207 \\[YaTeX-hierarchy-select] select file
208 \\[YaTeX-hierarchy-mouse-select] select
209 "
210 (setq major-mode 'YaTeX-hierarchy-mode
211 mode-name "YaTeX hier")
212 (use-local-map YaTeX-hierarchy-mode-map)
213 (setq buffer-read-only t)
214 (message YaTeX-hierarchy-buffer-message))
216 ;; ----- Subfunctions for interactive functions -----
217 (defun YaTeX-hierarchy-get-current-file-buffer ()
218 "Return the buffer associated with current line's file."
219 (let ((file (buffer-substring
220 (point)
221 (save-excursion
222 (skip-chars-forward "^ \t" (point-end-of-line)) (point))))
223 (hilit-auto-highlight) buffer)
224 (set-buffer (find-file-noselect YaTeX-hierarchy-current-main))
225 (if (get-buffer file) ;buffer is active
226 (setq buffer (get-buffer file)) ;may contain `<2>'
227 (if (string-match "<[2-9]>$" file)
228 (setq file (substring file 0 -3)))
229 (save-excursion
230 (setq buffer (YaTeX-switch-to-buffer file t)))))) ; open it!
232 ;; ----- Interactive functions -----
233 (defun YaTeX-hierarchy-next (arg &optional quiet)
234 "Move to next line's file in YaTeX document hierarchy buffer."
235 (interactive "p")
236 (forward-line arg)
237 (skip-chars-forward "- +\\|")
238 (if (and (/= arg 0) (not quiet))
239 (YaTeX-hierarchy-select t))
240 (message YaTeX-hierarchy-buffer-message))
242 (defun YaTeX-hierarchy-prev (arg)
243 "Move to previous line's file in YaTeX document hierarchy buffer."
244 (interactive "p")
245 (YaTeX-hierarchy-next (- arg)))
247 (defun YaTeX-hierarchy-next-line (arg)
248 (interactive "p")
249 (YaTeX-hierarchy-next arg t))
251 (defun YaTeX-hierarchy-prev-line (arg)
252 (interactive "p")
253 (YaTeX-hierarchy-next (- arg) t))
255 (defun YaTeX-hierarchy-forward (arg)
256 "Move to forward file in same hierarchy level."
257 (interactive "p")
258 (YaTeX-hierarchy-next 0)
259 (let ((p (point))(column (current-column)) (i (if (> arg 0) arg (- arg))))
260 (if (= column 0) (error "Not on file line."))
261 (while (> i 0)
262 (if (catch 'found
263 (while (and (not (eobp)) (not (bobp)))
264 (forward-line (if (> arg 0) 1 -1))
265 (move-to-column column)
266 (if (looking-at "[- +\\|]") nil
267 (YaTeX-hierarchy-next 0)
268 (if (= (current-column) column) (throw 'found t)))
269 (beginning-of-line)))
270 nil
271 (goto-char p)
272 (error "No same level file."))
273 (setq i (1- i)))))
275 (defun YaTeX-hierarchy-backward (arg)
276 "Move to backward file in same hierarchy level."
277 (interactive "p")
278 (YaTeX-hierarchy-forward (- arg)))
280 (defun YaTeX-hierarchy-up-document ()
281 "Up level, that is, move to parent file position."
282 (interactive)
283 (YaTeX-hierarchy-next 0) ;adjust column
284 (let ((p (point)) (line (count-lines (point-min) (point))) column)
285 (if (or (<= line 1) (< (current-column) 6))
286 (message "No more parent")
287 (backward-char 1)
288 (or (= (char-after (point)) ?-) (error "Unexpected hierarchy buffer"))
289 (setq column (current-column))
290 (while (and (> line 1) (looking-at "[- +\\|]"))
291 (forward-line -1)
292 (move-to-column column))
293 (YaTeX-hierarchy-next 0)
294 (push-mark p t)
295 (message "Mark set to last position"))))
297 (defun YaTeX-hierarchy-kill-buffer (arg)
298 "Kill buffer associated with current line's file."
299 (interactive "p")
300 (YaTeX-hierarchy-next 0) ;move to file name column
301 (if (bolp) (error "Not on file name line"))
302 (let ((file (buffer-substring
303 (point)
304 (progn (skip-chars-forward "^ \t") (point)))))
305 (YaTeX-hierarchy-next arg)
306 (cond
307 ((get-buffer file)
308 (kill-buffer (get-buffer file))
309 (message "Buffer [%s] was killed" file))
310 (t (message "Buffer [%s] is not active." file)))))
312 (defun YaTeX-hierarchy-select (arg)
313 "Select current line's file in YaTeX document hierarchy buffer.
314 If ARG is non-nil, show the buffer in the next window."
315 (interactive "P")
316 (beginning-of-line)
317 (skip-chars-forward "- +\\|")
318 (or (eolp)
319 (let ((buffer (YaTeX-hierarchy-get-current-file-buffer)))
320 (if buffer ;if file was found
321 (if arg
322 (YaTeX-showup-buffer buffer nil)
323 (if (and YaTeX-emacs-19 window-system
324 (get-buffer-window buffer t))
325 (goto-buffer-window buffer) ;select currently displaying
326 (YaTeX-switch-to-buffer-other-window buffer)))))))
328 (defun YaTeX-hierarchy-show ()
329 "Show current line's file in the next window."
330 (interactive)
331 (YaTeX-hierarchy-select t))
333 (defun YaTeX-hierarchy-mouse-select (event)
334 (interactive "e")
335 (mouse-set-point event)
336 (YaTeX-hierarchy-select nil))
338 (defun YaTeX-hierarchy-quit ()
339 "Quit from YaTeX-hierarchy buffer and restore window configuration."
340 (interactive)
341 (if (or (not (featurep 'windows))
342 (car YaTeX-hierarchy-saved-wc)
343 (and (= (car (cdr YaTeX-hierarchy-saved-wc)) win:current-config)))
344 (set-window-configuration (car YaTeX-hierarchy-saved-wc))
345 (bury-buffer nil)))
347 (defun YaTeX-hierarchy-scroll-up (arg &optional action)
348 "Scroll up file contents of YaTeX-hierarchy."
349 (interactive "P")
350 (YaTeX-hierarchy-next 0 t)
351 (let*((bufname (buffer-substring
352 (point)
353 (save-excursion (skip-chars-forward "^ \t") (point))))
354 (buf (get-buffer bufname))
355 (cw (selected-window)))
356 (cond
357 ((and buf (get-buffer-window buf))
358 (select-window (get-buffer-window buf)))
359 ((and buf (YaTeX-showup-buffer buf nil t)) t)
360 (t (YaTeX-hierarchy-select nil)))
361 (unwind-protect
362 (cond
363 ((eq action 'down) (scroll-down arg))
364 ((eq action 'top) (beginning-of-buffer))
365 ((eq action 'bottom) (end-of-buffer))
366 ((eq action 'last) (exchange-point-and-mark))
367 (t (scroll-up arg)))
368 (select-window cw))))
370 (defun YaTeX-hierarchy-scroll-down (arg)
371 "Scroll down file contents of YaTeX-hierarchy."
372 (interactive "P")
373 (YaTeX-hierarchy-scroll-up arg 'down))
375 (defun YaTeX-hierarchy-top ()
376 "Show the top of YaTeX-hierarchy inspection buffer's."
377 (interactive)
378 (YaTeX-hierarchy-scroll-up nil 'top)
379 )
381 (defun YaTeX-hierarchy-bottom ()
382 "Show the top of YaTeX-hierarchy inspection buffer's."
383 (interactive)
384 (YaTeX-hierarchy-scroll-up nil 'bottom)
385 )
387 (defun YaTeX-hierarchy-lastpos ()
388 "Go to last position in YaTeX-hierarchy buffer."
389 (interactive)
390 (YaTeX-hierarchy-scroll-up nil 'last)
391 )
393 ;; ----- Setting up keymap -----
394 (defvar YaTeX-hierarchy-mode-map nil "Keymap used in YaTeX-hierarchy-mode.")
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