yatex

view yatexsec.el @ 63:6ad4a2399731

pcs39334@asciinet.or.jp removed
author yuuji
date Sat, 13 Dec 1997 12:41:43 +0000
parents 3a7c0c2bf16d
children 36a48185b95a
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX sectioning browser.
3 ;;; yatexsec.el
4 ;;; (c ) 1994 by HIROSE Yuuji [yuuji@ae.keio.ac.jp]
5 ;;; Last modified Mon Feb 17 11:26:44 1997 on supra
6 ;;; $Id$
8 (defvar YaTeX-sectioning-level
9 '(("part" . 0)
10 ("chapter" . 1)
11 ("section" . 2)
12 ("subsection" . 3)
13 ("subsubsection" . 4)
14 ("paragraph" . 5)
15 ("subparagraph" . 6))
16 "*Alist of LaTeX's sectioning command and its level.
17 This value must be written in numerically ascending order and consecutive.
18 Needn't define the level of `*' commands such as `section*'.")
20 (defvar YaTeX-sectioning-max-level
21 (cdr (nth (1- (length YaTeX-sectioning-level)) YaTeX-sectioning-level))
22 "*The heighest(numerically) level of sectioning command.
23 This must be the heighest number in YaTeX-sectioning-level.")
25 (defun YaTeX-sectioning-map-hide (map)
26 (let ((ch ?0))
27 (while (<= ch ?9)
28 (define-key map (char-to-string ch) 'YaTeX-sectioning-hide)
29 (setq ch (1+ ch))))
30 )
32 (defvar YaTeX-sectioning-minibuffer-map nil
33 "Key map used in minibuffer for sectioning.")
34 (if YaTeX-sectioning-minibuffer-map nil
35 (setq YaTeX-sectioning-minibuffer-map
36 (copy-keymap minibuffer-local-completion-map))
37 (define-key YaTeX-sectioning-minibuffer-map "\C-p"
38 'YaTeX-sectioning-up)
39 (define-key YaTeX-sectioning-minibuffer-map "\C-e"
40 'YaTeX-sectioning-up)
41 (define-key YaTeX-sectioning-minibuffer-map "\C-i"
42 'YaTeX-minibuffer-complete)
43 (define-key YaTeX-sectioning-minibuffer-map " "
44 'YaTeX-minibuffer-complete)
45 (define-key YaTeX-sectioning-minibuffer-map "\C-n"
46 'YaTeX-sectioning-down)
47 (define-key YaTeX-sectioning-minibuffer-map "\C-x"
48 'YaTeX-sectioning-down)
49 (define-key YaTeX-sectioning-minibuffer-map "\C-v"
50 'YaTeX-sectioning-scroll-up)
51 (define-key YaTeX-sectioning-minibuffer-map "\C-c"
52 'YaTeX-sectioning-scroll-up)
53 (define-key YaTeX-sectioning-minibuffer-map "\M-v"
54 'YaTeX-sectioning-scroll-down)
55 (define-key YaTeX-sectioning-minibuffer-map "\C-r"
56 'YaTeX-sectioning-scroll-down)
57 (define-key YaTeX-sectioning-minibuffer-map "\C-w"
58 '(lambda () (interactive) (YaTeX-sectioning-scroll-down 1)))
59 (define-key YaTeX-sectioning-minibuffer-map "\C-z"
60 '(lambda () (interactive) (YaTeX-sectioning-scroll-up 1)))
61 (define-key YaTeX-sectioning-minibuffer-map "\C-l"
62 'YaTeX-sectioning-recenter)
63 (define-key YaTeX-sectioning-minibuffer-map "?"
64 'YaTeX-sectioning-help)
65 (YaTeX-sectioning-map-hide YaTeX-sectioning-minibuffer-map)
66 )
68 (defvar YaTeX-sectioning-buffer-map nil
69 "Key map used in YaTeX-sectioning-buffer.")
70 (if YaTeX-sectioning-buffer-map nil
71 (setq YaTeX-sectioning-buffer-map (make-sparse-keymap))
72 (define-key YaTeX-sectioning-buffer-map " " 'YaTeX-sectioning-buffer-jump)
73 (define-key YaTeX-sectioning-buffer-map "." 'YaTeX-sectioning-buffer-show)
74 (define-key YaTeX-sectioning-buffer-map (concat YaTeX-prefix "\C-c")
75 'YaTeX-sectioning-buffer-jump)
76 (define-key YaTeX-sectioning-buffer-map "u" 'YaTeX-shift-section-up)
77 (define-key YaTeX-sectioning-buffer-map "d" 'YaTeX-shift-section-down)
78 (define-key YaTeX-sectioning-buffer-map "U" 'YaTeX-shift-section-up-region)
79 (define-key YaTeX-sectioning-buffer-map "D" 'YaTeX-shift-section-down-region)
80 (define-key YaTeX-sectioning-buffer-map "s" 'YaTeX-sync-section-buffer)
81 (define-key YaTeX-sectioning-buffer-map "n"
82 'YaTeX-sectioning-buffer-next-line)
83 (define-key YaTeX-sectioning-buffer-map "p"
84 'YaTeX-sectioning-buffer-prev-line)
85 (define-key YaTeX-sectioning-buffer-map "h" 'describe-mode)
86 (define-key YaTeX-sectioning-buffer-map "o" 'other-window)
87 (define-key YaTeX-sectioning-buffer-map "-" 'shrink-window)
88 (define-key YaTeX-sectioning-buffer-map "+" 'enlarge-window)
89 (define-key YaTeX-sectioning-buffer-map "q" 'delete-window)
90 (define-key YaTeX-sectioning-buffer-map "\C-_" 'YaTeX-shift-section-undo)
91 (and YaTeX-emacs-19 (boundp 'window-system) (eq window-system 'x)
92 (define-key YaTeX-sectioning-buffer-map [?\C-/]
93 'YaTeX-shift-section-undo))
94 (YaTeX-sectioning-map-hide YaTeX-sectioning-buffer-map)
95 )
97 (defun YaTeX-sectioning-mode ()
98 "Mode for browsing document's sectioning structure.
99 \\[YaTeX-shift-section-up] Shift up a sectioning command
100 \\[YaTeX-shift-section-down] Shift down a sectioning command
101 \\[YaTeX-shift-section-up-region] Shift up sectioning commands in region
102 \\[YaTeX-shift-section-down-region] Shift down sectioning commands in region
103 \\[YaTeX-shift-section-undo] Undo changes of shifting
104 \\[YaTeX-sync-section-buffer] Synchronize sectioning buffer with source
105 \\[YaTeX-sectioning-buffer-next-line] Next line
106 \\[YaTeX-sectioning-buffer-prev-line] Previous line
107 \\[YaTeX-sectioning-buffer-jump] Previous line
108 \\[YaTeX-sectioning-buffer-show] Show curresponding source line
109 "
110 (interactive)
111 (setq major-mode 'YaTeX-sectioning-mode
112 mode-name "sectioning")
113 (use-local-map YaTeX-sectioning-buffer-map)
114 )
116 (defvar YaTeX-sectioning-buffer-parent nil)
117 (defun YaTeX-sectioning-buffer-jump-internal (&optional keep)
118 (let (ptn (p (point))) ;save-excursion is NG because
119 (beginning-of-line) ;this function should switch buffer
120 (if (re-search-forward YaTeX-sectioning-regexp)
121 (progn (setq ptn (buffer-substring
122 (1- (match-beginning 0))
123 (progn (skip-chars-forward "^}") (1+ (point)))))
124 (goto-char p)
125 (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent nil t)
126 (goto-char (point-max))
127 (search-backward ptn)
128 (if keep (goto-buffer-window YaTeX-sectioning-buffer))
129 (current-buffer))
130 nil))
131 )
132 (defun YaTeX-sectioning-buffer-jump (&optional keep)
133 "Goto corresponding sectioning unit with current line in the next window.
134 If optional argument KEEP is non-nil, only shows the line."
135 (interactive)
136 (if (and YaTeX-sectioning-buffer-parent
137 (get-buffer YaTeX-sectioning-buffer-parent))
138 (YaTeX-sectioning-buffer-jump-internal keep)
139 (message "No line number expression."))
140 )
142 (defun YaTeX-sectioning-buffer-show ()
143 "Show corresponding sectioning unit with current line."
144 (interactive)
145 (YaTeX-sectioning-buffer-jump-internal t)
146 )
148 (defun YaTeX-sectioning-hide-under (n)
149 "Hide sectioning commands under level N."
150 (let ((cw (selected-window)))
151 (YaTeX-showup-buffer YaTeX-sectioning-buffer nil t)
152 (if (>= n YaTeX-sectioning-max-level)
153 (progn
154 (set-selective-display nil)
155 (message "Show all."))
156 (set-selective-display (1+ n))
157 (if (rassq n YaTeX-sectioning-level)
158 (message "Hide lower than %s" (car (rassq n YaTeX-sectioning-level)))
159 (message "")))
160 (if (numberp selective-display)
161 (setq mode-name (format "level %d" (1- selective-display)))
162 (setq mode-name (format "all")))
163 (select-window cw))
164 )
165 (defun YaTeX-sectioning-hide ()
166 "Call YaTeX-sectioning-hide-under with argument according to pressed key."
167 (interactive)
168 (YaTeX-sectioning-hide-under (- last-command-char ?0)))
170 (defun YaTeX-sectioning-help ()
171 "Show help of sectioning."
172 (interactive)
173 (let ((cw (selected-window)) sb (hb (get-buffer-create "*Help*")))
174 (unwind-protect
175 (progn
176 (other-window 1)
177 (setq sb (current-buffer))
178 (switch-to-buffer hb)
179 (erase-buffer)
180 (insert "===== View sectioning =====
181 C-p Up sectioning level. 0 Show only \\part,
182 C-n Down sectioning level. 1 and \\chapter,
183 C-v Scroll up *Sectioning line* buffer. 2 and \\section,
184 M-v Scroll down *Sectioning line* buffer. 3 and \\subsection,
185 C-z Scroll up by 1 line. 4 and \\subsubsection,
186 C-w Scroll down by 1 line. 5 and \\paragraph.
187 SPC Complete word. 6 Show all.
188 TAB Complete word.
189 C-l Recenter recent line.
190 RET Select.
191 ==== End of HELP =====
192 ")
193 (set-buffer-modified-p nil)
194 (goto-char (point-min))
195 (momentary-string-display "" (point-min)))
196 (bury-buffer hb)
197 (switch-to-buffer sb)
198 (select-window cw)))
199 )
201 (defun YaTeX-sectioning-up (n)
202 "Up section level.
203 Refers the YaTeX-read-section-in-minibuffer's local variable minibuffer-start."
204 (interactive "p")
205 (if (eq (selected-window) (minibuffer-window))
206 (let*((command (buffer-string))
207 (aster (and (string< "" command)
208 (equal (substring command -1) "*")))
209 (command (if aster (substring command 0 -1) command))
210 (alist YaTeX-sectioning-level)
211 (level 0))
212 (or (assoc command alist) (error "No such sectioning command."))
213 (while (not (string= (car (nth level alist)) command))
214 (setq level (1+ level))) ;I want to use `member'....
215 (setq level (- level n))
216 (if (or (< level 0) (>= level (length alist)))
217 (ding)
218 (erase-buffer)
219 (insert (concat (car (nth level alist)) (if aster "*" ""))))))
220 )
222 (defun YaTeX-sectioning-down (n)
223 "Down section level."
224 (interactive "p")
225 (YaTeX-sectioning-up (- n))
226 )
228 (defun YaTeX-sectioning-scroll-up (n)
229 (interactive "P")
230 (let ((section-buffer YaTeX-sectioning-buffer)
231 (cw (selected-window)))
232 (YaTeX-showup-buffer section-buffer nil t)
233 (unwind-protect
234 (scroll-up (or n (- (window-height) 2)))
235 (select-window cw)))
236 )
238 (defun YaTeX-sectioning-scroll-down (n)
239 (interactive "P")
240 (let ((section-buffer YaTeX-sectioning-buffer)
241 (cw (selected-window)))
242 (YaTeX-showup-buffer section-buffer nil t)
243 (unwind-protect
244 (scroll-down (or n (- (window-height) 2)))
245 (select-window cw)))
246 )
248 (defun YaTeX-sectioning-recenter (arg)
249 "Recenter `<<--' line"
250 (interactive "P")
251 (let ((cw (selected-window)))
252 (unwind-protect
253 (progn
254 (YaTeX-showup-buffer YaTeX-sectioning-buffer nil t)
255 (or (search-forward "<<--" nil t)
256 (search-backward "<<--" nil))
257 (recenter (or arg (/ (window-height) 2))))
258 (select-window cw)))
259 )
261 (defvar YaTeX-sectioning-minibuffer " *sectioning*"
262 "Miniuffer used for sectioning")
263 ;;;###autoload
264 (defun YaTeX-read-section-in-minibuffer (prompt table &optional default delim)
265 (interactive)
266 (let ((minibuffer-completion-table table))
267 (read-from-minibuffer
268 prompt default YaTeX-sectioning-minibuffer-map))
269 )
271 (defun YaTeX-get-sectioning-level ()
272 "Get section-level on the cursor."
273 (cdr-safe (assoc (buffer-substring
274 (point)
275 (progn (skip-chars-forward "a-z") (point)))
276 YaTeX-sectioning-level))
277 )
279 (defvar YaTeX-sectioning-buffer "*Sectioning lines*")
280 (defun YaTeX-colloect-sections ()
281 "Collect all the lines which contains sectioning command."
282 (let ((cw (selected-window)) level indent begp (prevp 1) (prevl 1)
283 (pattern (concat YaTeX-ec-regexp
284 "\\(" YaTeX-sectioning-regexp "\\)\\*?{"))
285 (cb (current-buffer)))
286 (save-excursion
287 (set-buffer (get-buffer-create YaTeX-sectioning-buffer))
288 (setq buffer-read-only nil)
289 (set-buffer cb)
290 (YaTeX-showup-buffer YaTeX-sectioning-buffer) ;show buffer
291 (goto-char (point-min))
292 (with-output-to-temp-buffer YaTeX-sectioning-buffer
293 (while (re-search-forward pattern nil t)
294 (goto-char (1+ (match-beginning 0)))
295 (setq level (YaTeX-get-sectioning-level)
296 begp (match-beginning 0))
297 ;;(beginning-of-line)
298 ;;(skip-chars-forward " \t")
299 (setq indent (format "%%%ds" level))
300 (princ (format indent ""))
301 (if (YaTeX-on-comment-p) (princ "%"))
302 (princ (buffer-substring begp (progn (forward-list 1) (point))))
303 (setq prevl (+ prevl (count-lines prevp (point)) -1)
304 prevp (point))
305 (princ (format " (line:%d)" prevl))
306 (princ "\n")))
307 (set-buffer YaTeX-sectioning-buffer)
308 (make-local-variable 'YaTeX-sectioning-buffer-parent)
309 (YaTeX-sectioning-mode)
310 (use-local-map YaTeX-sectioning-buffer-map)
311 (setq YaTeX-sectioning-buffer-parent cb)
312 (if (numberp selective-display)
313 (setq mode-name (format "level %d" (1- selective-display))))
314 YaTeX-sectioning-buffer))
315 )
317 (defvar YaTeX-pending-undo nil)
318 (defun YaTeX-section-overview ()
319 "Show section overview. Return the nearest sectioning command."
320 (interactive)
321 (let ((cw (selected-window)) (ln (count-lines (point-min) (point)))
322 (pattern "(line:\\([0-9]+\\))")
323 secbuf (command ""))
324 (save-excursion
325 (setq secbuf (YaTeX-colloect-sections))
326 (YaTeX-showup-buffer secbuf nil t)
327 (set-buffer secbuf)
328 (goto-char (point-max))
329 (while (re-search-backward pattern nil t)
330 (if (< ln (string-to-int (YaTeX-match-string 1))) nil
331 (beginning-of-line)
332 (search-forward YaTeX-ec)
333 (looking-at YaTeX-TeX-token-regexp)
334 (setq command (YaTeX-match-string 0))
335 (end-of-line)
336 (insert " <<--")
337 (setq pattern (concat "HackyRegexp" "ForFailure"))))
338 (set-buffer-modified-p nil)
339 (setq buffer-read-only t buffer-undo-list nil)
340 (make-local-variable 'YaTeX-pending-undo)
341 (forward-line 1)
342 (if (eobp) (recenter -1) (recenter -3))
343 (select-window cw)
344 command))
345 )
347 ;;;###autoload
348 (defun YaTeX-make-section-with-overview ()
349 "Input sectining command with previous overview."
350 (interactive)
351 (insert
352 YaTeX-ec
353 (YaTeX-read-section-in-minibuffer
354 "Sectioning(Up=C-p, Down=C-n, Help=?): "
355 YaTeX-sectioning-level (YaTeX-section-overview))
356 "{}")
357 (forward-char -1)
358 )
360 (defun YaTeX-shifted-section (sc n)
361 "Get SC's N-shifted sectioning command."
362 (let (lv)
363 (setq lv (- (cdr (assoc sc YaTeX-sectioning-level)) n)
364 lv (max (min YaTeX-sectioning-max-level lv) 0))
365 (car (nth lv YaTeX-sectioning-level)))
366 )
368 (defun YaTeX-shift-section-up (n)
369 "Shift sectioning command down by level N."
370 (interactive "p")
371 (let ((cb (current-buffer)) sc nsc lv)
372 (if (and YaTeX-sectioning-buffer-parent
373 (get-buffer YaTeX-sectioning-buffer-parent)
374 (save-excursion
375 (or (= (char-after (point)) ?\\ )
376 (skip-chars-backward "^\\\\" (point-beginning-of-line)))
377 (YaTeX-on-section-command-p YaTeX-sectioning-regexp)))
378 (save-excursion
379 (or (buffer-name (get-buffer YaTeX-sectioning-buffer-parent))
380 (error "This buffer is obsolete."))
381 (setq nsc (YaTeX-shifted-section (YaTeX-match-string 1) n))
382 (YaTeX-sectioning-buffer-jump-internal)
383 (undo-boundary)
384 (goto-char (match-beginning 0))
385 (skip-chars-forward "\\\\")
386 (delete-region
387 (point) (progn (skip-chars-forward "^*{") (point)))
388 (insert nsc)
389 (undo-boundary)
390 ;; Return to *Sectioning Lines* buffer
391 (select-window (get-buffer-window cb))
392 (beginning-of-line)
393 (let (buffer-read-only)
394 (delete-region
395 (point) (progn (skip-chars-forward " \t") (point)))
396 (indent-to-column (cdr (assoc nsc YaTeX-sectioning-level)))
397 (skip-chars-forward "^\\\\")
398 (delete-region
399 (1+ (point)) (progn (skip-chars-forward "^*{") (point)))
400 (insert nsc)
401 (undo-boundary))
402 (set-buffer-modified-p nil)
403 (setq YaTeX-pending-undo pending-undo-list)
404 )))
405 )
406 (defun YaTeX-shift-section-down (n)
407 "Shift sectioning command down by level N."
408 (interactive "p")
409 (YaTeX-shift-section-up (- n))
410 )
411 (defun YaTeX-shift-section-undo (arg)
412 "Undo YaTeX-shift-section-up/down."
413 (interactive "p")
414 (and YaTeX-sectioning-buffer-parent
415 (get-buffer YaTeX-sectioning-buffer-parent)
416 (equal (current-buffer) (get-buffer YaTeX-sectioning-buffer))
417 (let ((cb (current-buffer))
418 (lc (if (eq last-command 'YaTeX-shift-section-undo) 'undo t)))
419 (let ((pending-undo-list YaTeX-pending-undo)
420 buffer-read-only (last-command lc))
421 (undo arg)
422 (setq YaTeX-pending-undo pending-undo-list))
423 (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent)
424 (goto-buffer-window YaTeX-sectioning-buffer-parent)
425 (undo-boundary)
426 (let ((last-command lc)
427 (pending-undo-list
428 (if (eq lc 'undo) YaTeX-pending-undo pending-undo-list)))
429 (undo arg)
430 (setq YaTeX-pending-undo pending-undo-list))
431 (goto-buffer-window cb)
432 (setq this-command 'YaTeX-shift-section-undo)))
433 )
434 (defun YaTeX-sync-section-buffer ()
435 "Synchronize *Sectioning Lines* buffer with parent buffer."
436 (interactive)
437 (if (and YaTeX-sectioning-buffer-parent
438 (get-buffer YaTeX-sectioning-buffer-parent))
439 (let ((cb (current-buffer)) (p (point)))
440 (set-buffer (get-buffer YaTeX-sectioning-buffer-parent))
441 (YaTeX-section-overview)
442 (switch-to-buffer cb)
443 (goto-char p)))
444 )
445 (defun YaTeX-shift-section-up-region (beg end n)
446 "Shift sectioning commands in region down by level N."
447 (interactive "r\np")
448 (or YaTeX-sectioning-buffer-parent
449 (get-buffer YaTeX-sectioning-buffer-parent)
450 (error "Can't find corresponding LaTeX buffer"))
451 (save-excursion
452 (goto-char beg)
453 (let ((cb (current-buffer)) nsc from to repllist (e (make-marker)))
454 (set-marker e end)
455 (while (progn (skip-chars-forward "^\\\\") (< (point) e))
456 (YaTeX-on-section-command-p YaTeX-sectioning-regexp)
457 (setq from (YaTeX-match-string 0)
458 nsc (YaTeX-shifted-section (YaTeX-match-string 1) n))
459 (goto-char (match-beginning 0))
460 (let (buffer-read-only)
461 (delete-region (point) (progn (beginning-of-line) (point)))
462 (indent-to-column (cdr (assoc nsc YaTeX-sectioning-level)))
463 (delete-region
464 (1+ (point)) (progn (skip-chars-forward "^*{") (point)))
465 (insert nsc))
466 (YaTeX-on-section-command-p YaTeX-sectioning-regexp)
467 (setq to (YaTeX-match-string 0)
468 repllist (cons (cons from to) repllist))
469 (forward-line 1))
470 (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent)
471 (goto-buffer-window YaTeX-sectioning-buffer-parent)
472 (save-excursion
473 (goto-char (point-max))
474 (undo-boundary)
475 (while repllist
476 (if (search-backward (car (car repllist)) nil t)
477 (progn
478 (goto-char (match-beginning 0)) ;confirm
479 (delete-region (point) (match-end 0))
480 (insert (cdr (car repllist)))
481 (goto-char (match-beginning 0))))
482 (setq repllist (cdr repllist))))
483 (goto-buffer-window cb)))
484 )
485 (defun YaTeX-shift-section-down-region (beg end n)
486 "Shift sectioning commands in region down by level N."
487 (interactive "r\np")
488 (YaTeX-shift-section-up-region beg end (- n))
489 )
490 (defun YaTeX-sectioning-buffer-next-line (n)
491 "Move to next line in *Sectioning Lines* buffer."
492 (interactive "p")
493 (forward-line n)
494 (skip-chars-forward " \t%")
495 )
496 (defun YaTeX-sectioning-buffer-prev-line (n)
497 "Move to previous line in *Sectioning Lines* buffer."
498 (interactive "p")
499 (YaTeX-sectioning-buffer-next-line (- n))
500 )
501 (provide 'yatexsec)