yatex

view yatexsec.el @ 72:0aaebd07dad0

Support font-lock on XEmacs-21, Emacs-20, Emacs-21. Support Emacs-21.
author yuuji
date Mon, 25 Dec 2000 10:19:28 +0000
parents 44e3a5e1e883
children 1b172d26b55e
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX sectioning browser.
3 ;;; yatexsec.el
4 ;;; (c ) 1994,1998,1999 by HIROSE Yuuji [yuuji@yatex.org]
5 ;;; Last modified Mon Dec 25 19:18:36 2000 on firestorm
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 (YaTeX-minibuffer-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 (YaTeX-minibuffer-erase)
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 (defvar YaTeX-sectioning-indent 1)
281 (defun YaTeX-collect-sections ()
282 "Collect all the lines which contains sectioning command."
283 (let ((cw (selected-window)) level indent begp (prevp 1) (prevl 1)
284 (pattern (concat YaTeX-ec-regexp
285 "\\(" YaTeX-sectioning-regexp "\\)\\*?{"))
286 (cb (current-buffer)))
287 (save-excursion
288 (set-buffer (get-buffer-create YaTeX-sectioning-buffer))
289 (setq buffer-read-only nil)
290 (erase-buffer)
291 (set-buffer cb)
292 (YaTeX-showup-buffer YaTeX-sectioning-buffer) ;show buffer
293 (goto-char (point-min))
294 (let ((standard-output (get-buffer YaTeX-sectioning-buffer)))
295 (while (re-search-forward pattern nil t)
296 (goto-char (1+ (match-beginning 0)))
297 (setq level (YaTeX-get-sectioning-level)
298 begp (match-beginning 0))
299 ;;(beginning-of-line)
300 ;;(skip-chars-forward " \t")
301 (setq indent (format "%%%ds" (* level YaTeX-sectioning-indent)))
302 (princ (format indent ""))
303 (if (YaTeX-on-comment-p) (princ "%"))
304 (princ (buffer-substring begp (progn (forward-list 1) (point))))
305 (setq prevl (+ prevl (count-lines prevp (point)) -1)
306 prevp (point))
307 (princ (format " (line:%d)" prevl))
308 (princ "\n")))
309 (set-buffer YaTeX-sectioning-buffer)
310 (make-local-variable 'YaTeX-sectioning-buffer-parent)
311 (YaTeX-sectioning-mode)
312 (use-local-map YaTeX-sectioning-buffer-map)
313 (setq YaTeX-sectioning-buffer-parent cb)
314 (if (numberp selective-display)
315 (setq mode-name (format "level %d" (1- selective-display))))
316 YaTeX-sectioning-buffer))
317 )
319 (defvar YaTeX-pending-undo nil)
320 (defun YaTeX-section-overview ()
321 "Show section overview. Return the nearest sectioning command."
322 (interactive)
323 (let ((cw (selected-window)) (ln (count-lines (point-min) (point)))
324 (pattern "(line:\\([0-9]+\\))")
325 secbuf (command ""))
326 (save-excursion
327 (setq secbuf (YaTeX-collect-sections))
328 (YaTeX-showup-buffer secbuf nil t)
329 (set-buffer secbuf)
330 (goto-char (point-max))
331 (while (re-search-backward pattern nil t)
332 (if (< ln (string-to-int (YaTeX-match-string 1))) nil
333 (beginning-of-line)
334 (search-forward YaTeX-ec)
335 (looking-at YaTeX-TeX-token-regexp)
336 (setq command (YaTeX-match-string 0))
337 (end-of-line)
338 (insert " <<--")
339 (setq pattern (concat "HackyRegexp" "ForFailure"))))
340 (set-buffer-modified-p nil)
341 (setq buffer-read-only t buffer-undo-list nil)
342 (make-local-variable 'YaTeX-pending-undo)
343 (forward-line 1)
344 (if (eobp) (recenter -1) (recenter -3))
345 (select-window cw)
346 command))
347 )
349 ;;;###autoload
350 (defun YaTeX-make-section-with-overview ()
351 "Input sectining command with previous overview."
352 (interactive)
353 (insert
354 YaTeX-ec
355 (YaTeX-read-section-in-minibuffer
356 "Sectioning(Up=C-p, Down=C-n, Help=?): "
357 YaTeX-sectioning-level (YaTeX-section-overview))
358 "{}")
359 (forward-char -1)
360 )
362 (defun YaTeX-shifted-section (sc n)
363 "Get SC's N-shifted sectioning command."
364 (let (lv)
365 (setq lv (- (cdr (assoc sc YaTeX-sectioning-level)) n)
366 lv (max (min YaTeX-sectioning-max-level lv) 0))
367 (car (nth lv YaTeX-sectioning-level)))
368 )
370 (defun YaTeX-shift-section-up (n)
371 "Shift sectioning command down by level N."
372 (interactive "p")
373 (let ((cb (current-buffer)) sc nsc lv)
374 (if (and YaTeX-sectioning-buffer-parent
375 (get-buffer YaTeX-sectioning-buffer-parent)
376 (save-excursion
377 (beginning-of-line)
378 (skip-chars-forward "^\\\\" (point-end-of-line))
379 (YaTeX-on-section-command-p YaTeX-sectioning-regexp)))
380 (save-excursion
381 (or (buffer-name (get-buffer YaTeX-sectioning-buffer-parent))
382 (error "This buffer is obsolete."))
383 (setq nsc (YaTeX-shifted-section (YaTeX-match-string 1) n))
384 (YaTeX-sectioning-buffer-jump-internal)
385 (undo-boundary)
386 (goto-char (match-beginning 0))
387 (skip-chars-forward "\\\\")
388 (delete-region
389 (point) (progn (skip-chars-forward "^*{") (point)))
390 (insert nsc)
391 (undo-boundary)
392 ;; Return to *Sectioning Lines* buffer
393 (select-window (get-buffer-window cb))
394 (beginning-of-line)
395 (let (buffer-read-only)
396 (delete-region
397 (point) (progn (skip-chars-forward " \t") (point)))
398 (indent-to-column (* (cdr (assoc nsc YaTeX-sectioning-level))
399 YaTeX-sectioning-indent))
400 (skip-chars-forward "^\\\\")
401 (delete-region
402 (1+ (point)) (progn (skip-chars-forward "^*{") (point)))
403 (insert nsc)
404 (undo-boundary))
405 (set-buffer-modified-p nil)
406 (setq YaTeX-pending-undo pending-undo-list)
407 )))
408 )
409 (defun YaTeX-shift-section-down (n)
410 "Shift sectioning command down by level N."
411 (interactive "p")
412 (YaTeX-shift-section-up (- n))
413 )
414 (defun YaTeX-shift-section-undo (arg)
415 "Undo YaTeX-shift-section-up/down."
416 (interactive "p")
417 (and YaTeX-sectioning-buffer-parent
418 (get-buffer YaTeX-sectioning-buffer-parent)
419 (equal (current-buffer) (get-buffer YaTeX-sectioning-buffer))
420 (let ((cb (current-buffer))
421 (lc (if (eq last-command 'YaTeX-shift-section-undo) 'undo t)))
422 (let ((pending-undo-list YaTeX-pending-undo)
423 buffer-read-only (last-command lc))
424 (undo arg)
425 (setq YaTeX-pending-undo pending-undo-list))
426 (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent)
427 (goto-buffer-window YaTeX-sectioning-buffer-parent)
428 (undo-boundary)
429 (let ((last-command lc)
430 (pending-undo-list
431 (if (eq lc 'undo) YaTeX-pending-undo pending-undo-list)))
432 (undo arg)
433 (setq YaTeX-pending-undo pending-undo-list))
434 (goto-buffer-window cb)
435 (setq this-command 'YaTeX-shift-section-undo)))
436 )
437 (defun YaTeX-sync-section-buffer ()
438 "Synchronize *Sectioning Lines* buffer with parent buffer."
439 (interactive)
440 (if (and YaTeX-sectioning-buffer-parent
441 (get-buffer YaTeX-sectioning-buffer-parent))
442 (let ((cb (current-buffer)) (p (point)))
443 (set-buffer (get-buffer YaTeX-sectioning-buffer-parent))
444 (YaTeX-section-overview)
445 (switch-to-buffer cb)
446 (goto-char p)))
447 )
448 (defun YaTeX-shift-section-up-region (beg end n)
449 "Shift sectioning commands in region down by level N."
450 (interactive "r\np")
451 (or YaTeX-sectioning-buffer-parent
452 (get-buffer YaTeX-sectioning-buffer-parent)
453 (error "Can't find corresponding LaTeX buffer"))
454 (save-excursion
455 (goto-char beg)
456 (let ((cb (current-buffer)) nsc from to repllist (e (make-marker)))
457 (set-marker e end)
458 (while (progn (skip-chars-forward "^\\\\") (< (point) e))
459 (YaTeX-on-section-command-p YaTeX-sectioning-regexp)
460 (setq from (YaTeX-match-string 0)
461 nsc (YaTeX-shifted-section (YaTeX-match-string 1) n))
462 (goto-char (match-beginning 0))
463 (let (buffer-read-only)
464 ;(delete-region (point) (progn (beginning-of-line) (point)))
465 (delete-region (progn (beginning-of-line) (point))
466 (progn (skip-chars-forward " \t") (point)))
467 (indent-to-column (cdr (assoc nsc YaTeX-sectioning-level)))
468 (delete-region
469 (progn (skip-chars-forward "%\\\\") (point))
470 (progn (skip-chars-forward "^*{") (point)))
471 (insert nsc))
472 (YaTeX-on-section-command-p YaTeX-sectioning-regexp)
473 (setq to (YaTeX-match-string 0)
474 repllist (cons (cons from to) repllist))
475 (forward-line 1))
476 (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent)
477 (goto-buffer-window YaTeX-sectioning-buffer-parent)
478 (save-excursion
479 (goto-char (point-max))
480 (undo-boundary)
481 (while repllist
482 (if (search-backward (car (car repllist)) nil t)
483 (progn
484 (goto-char (match-beginning 0)) ;confirm
485 (delete-region (point) (match-end 0))
486 (insert (cdr (car repllist)))
487 (goto-char (match-beginning 0))))
488 (setq repllist (cdr repllist))))
489 (goto-buffer-window cb)))
490 )
491 (defun YaTeX-shift-section-down-region (beg end n)
492 "Shift sectioning commands in region down by level N."
493 (interactive "r\np")
494 (YaTeX-shift-section-up-region beg end (- n))
495 )
496 (defun YaTeX-sectioning-buffer-next-line (n)
497 "Move to next line in *Sectioning Lines* buffer."
498 (interactive "p")
499 (forward-line n)
500 (skip-chars-forward " \t%")
501 )
502 (defun YaTeX-sectioning-buffer-prev-line (n)
503 "Move to previous line in *Sectioning Lines* buffer."
504 (interactive "p")
505 (YaTeX-sectioning-buffer-next-line (- n))
506 )
507 (provide 'yatexsec)