yatex

view yatexsec.el @ 84:73cba5ddd111

Converted from RCS of yatex
author yuuji
date Sun, 27 Sep 2009 13:04:14 +0000
parents 1b172d26b55e
children 2f9069006bdb
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX sectioning browser.
3 ;;; yatexsec.el
4 ;;; (c) 1994,1998,1999,2003 by HIROSE Yuuji [yuuji@yatex.org]
5 ;;; Last modified Fri Jun 27 12:10:34 2003 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 ((p (point)) ;save-excursion is NG because
119 ptn ln) ;this function should switch buffer
120 (beginning-of-line)
121 (if (re-search-forward YaTeX-sectioning-regexp)
122 (progn
123 (save-restriction
124 (narrow-to-region (point-beginning-of-line) (point-end-of-line))
125 (setq ptn (buffer-substring
126 (1- (match-beginning 0))
127 (progn (skip-chars-forward "^}") (1+ (point))))
128 ln (buffer-substring
129 (progn (search-forward "line:") (match-end 0))
130 (progn (skip-chars-forward "0-9") (point)))))
131 (goto-char p)
132 (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent nil t)
133 (or
134 (and ln (string< "" ln)
135 (progn
136 (goto-line (max 1 (1- (string-to-int ln))))
137 (and
138 (search-forward ptn nil t)
139 (goto-char (match-beginning 0)))))
140 (progn
141 (goto-char (point-max))
142 (search-backward ptn)))
143 (if keep (goto-buffer-window YaTeX-sectioning-buffer))
144 (current-buffer))
145 nil)))
147 (defun YaTeX-sectioning-buffer-jump (&optional keep)
148 "Goto corresponding sectioning unit with current line in the next window.
149 If optional argument KEEP is non-nil, only shows the line."
150 (interactive)
151 (if (and YaTeX-sectioning-buffer-parent
152 (get-buffer YaTeX-sectioning-buffer-parent))
153 (YaTeX-sectioning-buffer-jump-internal keep)
154 (message "No line number expression."))
155 )
157 (defun YaTeX-sectioning-buffer-show ()
158 "Show corresponding sectioning unit with current line."
159 (interactive)
160 (YaTeX-sectioning-buffer-jump-internal t)
161 )
163 (defun YaTeX-sectioning-hide-under (n)
164 "Hide sectioning commands under level N."
165 (let ((cw (selected-window)))
166 (YaTeX-showup-buffer YaTeX-sectioning-buffer nil t)
167 (if (>= n YaTeX-sectioning-max-level)
168 (progn
169 (set-selective-display nil)
170 (message "Show all."))
171 (set-selective-display (1+ n))
172 (if (rassq n YaTeX-sectioning-level)
173 (message "Hide lower than %s" (car (rassq n YaTeX-sectioning-level)))
174 (message "")))
175 (if (numberp selective-display)
176 (setq mode-name (format "level %d" (1- selective-display)))
177 (setq mode-name (format "all")))
178 (select-window cw))
179 )
180 (defun YaTeX-sectioning-hide ()
181 "Call YaTeX-sectioning-hide-under with argument according to pressed key."
182 (interactive)
183 (YaTeX-sectioning-hide-under (- last-command-char ?0)))
185 (defun YaTeX-sectioning-help ()
186 "Show help of sectioning."
187 (interactive)
188 (let ((cw (selected-window)) sb (hb (get-buffer-create "*Help*")))
189 (unwind-protect
190 (progn
191 (other-window 1)
192 (setq sb (current-buffer))
193 (switch-to-buffer hb)
194 (erase-buffer)
195 (insert "===== View sectioning =====
196 C-p Up sectioning level. 0 Show only \\part,
197 C-n Down sectioning level. 1 and \\chapter,
198 C-v Scroll up *Sectioning line* buffer. 2 and \\section,
199 M-v Scroll down *Sectioning line* buffer. 3 and \\subsection,
200 C-z Scroll up by 1 line. 4 and \\subsubsection,
201 C-w Scroll down by 1 line. 5 and \\paragraph.
202 SPC Complete word. 6 Show all.
203 TAB Complete word.
204 C-l Recenter recent line.
205 RET Select.
206 ==== End of HELP =====
207 ")
208 (set-buffer-modified-p nil)
209 (goto-char (point-min))
210 (momentary-string-display "" (point-min)))
211 (bury-buffer hb)
212 (switch-to-buffer sb)
213 (select-window cw)))
214 )
216 (defun YaTeX-sectioning-up (n)
217 "Up section level.
218 Refers the YaTeX-read-section-in-minibuffer's local variable minibuffer-start."
219 (interactive "p")
220 (if (eq (selected-window) (minibuffer-window))
221 (let*((command (YaTeX-minibuffer-string))
222 (aster (and (string< "" command)
223 (equal (substring command -1) "*")))
224 (command (if aster (substring command 0 -1) command))
225 (alist YaTeX-sectioning-level)
226 (level 0))
227 (or (assoc command alist) (error "No such sectioning command."))
228 (while (not (string= (car (nth level alist)) command))
229 (setq level (1+ level))) ;I want to use `member'....
230 (setq level (- level n))
231 (if (or (< level 0) (>= level (length alist)))
232 (ding)
233 (YaTeX-minibuffer-erase)
234 (insert (concat (car (nth level alist)) (if aster "*" ""))))))
235 )
237 (defun YaTeX-sectioning-down (n)
238 "Down section level."
239 (interactive "p")
240 (YaTeX-sectioning-up (- n))
241 )
243 (defun YaTeX-sectioning-scroll-up (n)
244 (interactive "P")
245 (let ((section-buffer YaTeX-sectioning-buffer)
246 (cw (selected-window)))
247 (YaTeX-showup-buffer section-buffer nil t)
248 (unwind-protect
249 (scroll-up (or n (- (window-height) 2)))
250 (select-window cw)))
251 )
253 (defun YaTeX-sectioning-scroll-down (n)
254 (interactive "P")
255 (let ((section-buffer YaTeX-sectioning-buffer)
256 (cw (selected-window)))
257 (YaTeX-showup-buffer section-buffer nil t)
258 (unwind-protect
259 (scroll-down (or n (- (window-height) 2)))
260 (select-window cw)))
261 )
263 (defun YaTeX-sectioning-recenter (arg)
264 "Recenter `<<--' line"
265 (interactive "P")
266 (let ((cw (selected-window)))
267 (unwind-protect
268 (progn
269 (YaTeX-showup-buffer YaTeX-sectioning-buffer nil t)
270 (or (search-forward "<<--" nil t)
271 (search-backward "<<--" nil))
272 (recenter (or arg (/ (window-height) 2))))
273 (select-window cw)))
274 )
276 (defvar YaTeX-sectioning-minibuffer " *sectioning*"
277 "Miniuffer used for sectioning")
278 ;;;###autoload
279 (defun YaTeX-read-section-in-minibuffer (prompt table &optional default delim)
280 (interactive)
281 (let ((minibuffer-completion-table table))
282 (read-from-minibuffer
283 prompt default YaTeX-sectioning-minibuffer-map))
284 )
286 (defun YaTeX-get-sectioning-level ()
287 "Get section-level on the cursor."
288 (cdr-safe (assoc (buffer-substring
289 (point)
290 (progn (skip-chars-forward "a-z") (point)))
291 YaTeX-sectioning-level))
292 )
294 (defvar YaTeX-sectioning-buffer "*Sectioning lines*")
295 (defvar YaTeX-sectioning-indent 1)
296 (defun YaTeX-collect-sections ()
297 "Collect all the lines which contains sectioning command."
298 (let ((cw (selected-window)) level indent begp (prevp 1) (prevl 1)
299 (pattern (concat YaTeX-ec-regexp
300 "\\(" YaTeX-sectioning-regexp "\\)\\*?{"))
301 (cb (current-buffer)))
302 (save-excursion
303 (set-buffer (get-buffer-create YaTeX-sectioning-buffer))
304 (setq buffer-read-only nil)
305 (erase-buffer)
306 (set-buffer cb)
307 (YaTeX-showup-buffer YaTeX-sectioning-buffer) ;show buffer
308 (goto-char (point-min))
309 (let ((standard-output (get-buffer YaTeX-sectioning-buffer)))
310 (while (re-search-forward pattern nil t)
311 (goto-char (1+ (match-beginning 0)))
312 (setq level (YaTeX-get-sectioning-level)
313 begp (match-beginning 0))
314 ;;(beginning-of-line)
315 ;;(skip-chars-forward " \t")
316 (setq indent (format "%%%ds" (* level YaTeX-sectioning-indent)))
317 (princ (format indent ""))
318 (if (YaTeX-on-comment-p) (princ "%"))
319 (princ (buffer-substring begp (progn (forward-list 1) (point))))
320 (setq prevl (+ prevl (count-lines prevp (point)) -1)
321 prevp (point))
322 (princ (format " (line:%d)" prevl))
323 (princ "\n")))
324 (set-buffer YaTeX-sectioning-buffer)
325 (make-local-variable 'YaTeX-sectioning-buffer-parent)
326 (YaTeX-sectioning-mode)
327 (use-local-map YaTeX-sectioning-buffer-map)
328 (setq YaTeX-sectioning-buffer-parent cb)
329 (if (numberp selective-display)
330 (setq mode-name (format "level %d" (1- selective-display))))
331 YaTeX-sectioning-buffer))
332 )
334 (defvar YaTeX-pending-undo nil)
335 (defun YaTeX-section-overview ()
336 "Show section overview. Return the nearest sectioning command."
337 (interactive)
338 (let ((cw (selected-window)) (ln (count-lines (point-min) (point)))
339 (pattern "(line:\\([0-9]+\\))")
340 secbuf (command ""))
341 (save-excursion
342 (setq secbuf (YaTeX-collect-sections))
343 (YaTeX-showup-buffer secbuf nil t)
344 (set-buffer secbuf)
345 (goto-char (point-max))
346 (while (re-search-backward pattern nil t)
347 (if (< ln (string-to-int (YaTeX-match-string 1))) nil
348 (beginning-of-line)
349 (search-forward YaTeX-ec)
350 (looking-at YaTeX-TeX-token-regexp)
351 (setq command (YaTeX-match-string 0))
352 (end-of-line)
353 (insert " <<--")
354 (setq pattern (concat "HackyRegexp" "ForFailure"))))
355 (set-buffer-modified-p nil)
356 (setq buffer-read-only t buffer-undo-list nil)
357 (make-local-variable 'YaTeX-pending-undo)
358 (forward-line 1)
359 (if (eobp) (recenter -1) (recenter -3))
360 (select-window cw)
361 command))
362 )
364 ;;;###autoload
365 (defun YaTeX-make-section-with-overview ()
366 "Input sectining command with previous overview."
367 (interactive)
368 (insert
369 YaTeX-ec
370 (YaTeX-read-section-in-minibuffer
371 "Sectioning(Up=C-p, Down=C-n, Help=?): "
372 YaTeX-sectioning-level (YaTeX-section-overview))
373 "{}")
374 (forward-char -1)
375 )
377 (defun YaTeX-shifted-section (sc n)
378 "Get SC's N-shifted sectioning command."
379 (let (lv)
380 (setq lv (- (cdr (assoc sc YaTeX-sectioning-level)) n)
381 lv (max (min YaTeX-sectioning-max-level lv) 0))
382 (car (nth lv YaTeX-sectioning-level)))
383 )
385 (defun YaTeX-shift-section-up (n)
386 "Shift sectioning command down by level N."
387 (interactive "p")
388 (let ((cb (current-buffer)) sc nsc lv)
389 (if (and YaTeX-sectioning-buffer-parent
390 (get-buffer YaTeX-sectioning-buffer-parent)
391 (save-excursion
392 (beginning-of-line)
393 (skip-chars-forward "^\\\\" (point-end-of-line))
394 (YaTeX-on-section-command-p YaTeX-sectioning-regexp)))
395 (save-excursion
396 (or (buffer-name (get-buffer YaTeX-sectioning-buffer-parent))
397 (error "This buffer is obsolete."))
398 (setq nsc (YaTeX-shifted-section (YaTeX-match-string 1) n))
399 (YaTeX-sectioning-buffer-jump-internal)
400 (undo-boundary)
401 (goto-char (match-beginning 0))
402 (skip-chars-forward "\\\\")
403 (delete-region
404 (point) (progn (skip-chars-forward "^*{") (point)))
405 (insert nsc)
406 (undo-boundary)
407 ;; Return to *Sectioning Lines* buffer
408 (select-window (get-buffer-window cb))
409 (beginning-of-line)
410 (let (buffer-read-only)
411 (delete-region
412 (point) (progn (skip-chars-forward " \t") (point)))
413 (indent-to-column (* (cdr (assoc nsc YaTeX-sectioning-level))
414 YaTeX-sectioning-indent))
415 (skip-chars-forward "^\\\\")
416 (delete-region
417 (1+ (point)) (progn (skip-chars-forward "^*{") (point)))
418 (insert nsc)
419 (undo-boundary))
420 (set-buffer-modified-p nil)
421 (setq YaTeX-pending-undo pending-undo-list)
422 )))
423 )
424 (defun YaTeX-shift-section-down (n)
425 "Shift sectioning command down by level N."
426 (interactive "p")
427 (YaTeX-shift-section-up (- n))
428 )
429 (defun YaTeX-shift-section-undo (arg)
430 "Undo YaTeX-shift-section-up/down."
431 (interactive "p")
432 (and YaTeX-sectioning-buffer-parent
433 (get-buffer YaTeX-sectioning-buffer-parent)
434 (equal (current-buffer) (get-buffer YaTeX-sectioning-buffer))
435 (let ((cb (current-buffer))
436 (lc (if (eq last-command 'YaTeX-shift-section-undo) 'undo t)))
437 (let ((pending-undo-list YaTeX-pending-undo)
438 buffer-read-only (last-command lc))
439 (undo arg)
440 (setq YaTeX-pending-undo pending-undo-list))
441 (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent)
442 (goto-buffer-window YaTeX-sectioning-buffer-parent)
443 (undo-boundary)
444 (let ((last-command lc)
445 (pending-undo-list
446 (if (eq lc 'undo) YaTeX-pending-undo pending-undo-list)))
447 (undo arg)
448 (setq YaTeX-pending-undo pending-undo-list))
449 (goto-buffer-window cb)
450 (setq this-command 'YaTeX-shift-section-undo)))
451 )
452 (defun YaTeX-sync-section-buffer ()
453 "Synchronize *Sectioning Lines* buffer with parent buffer."
454 (interactive)
455 (if (and YaTeX-sectioning-buffer-parent
456 (get-buffer YaTeX-sectioning-buffer-parent))
457 (let ((cb (current-buffer)) (p (point)))
458 (set-buffer (get-buffer YaTeX-sectioning-buffer-parent))
459 (YaTeX-section-overview)
460 (switch-to-buffer cb)
461 (goto-char p)))
462 )
463 (defun YaTeX-shift-section-up-region (beg end n)
464 "Shift sectioning commands in region down by level N."
465 (interactive "r\np")
466 (or YaTeX-sectioning-buffer-parent
467 (get-buffer YaTeX-sectioning-buffer-parent)
468 (error "Can't find corresponding LaTeX buffer"))
469 (save-excursion
470 (goto-char beg)
471 (let ((cb (current-buffer)) nsc from to repllist (e (make-marker)))
472 (set-marker e end)
473 (while (progn (skip-chars-forward "^\\\\") (< (point) e))
474 (YaTeX-on-section-command-p YaTeX-sectioning-regexp)
475 (setq from (YaTeX-match-string 0)
476 nsc (YaTeX-shifted-section (YaTeX-match-string 1) n))
477 (goto-char (match-beginning 0))
478 (let (buffer-read-only)
479 ;(delete-region (point) (progn (beginning-of-line) (point)))
480 (delete-region (progn (beginning-of-line) (point))
481 (progn (skip-chars-forward " \t") (point)))
482 (indent-to-column (cdr (assoc nsc YaTeX-sectioning-level)))
483 (delete-region
484 (progn (skip-chars-forward "%\\\\") (point))
485 (progn (skip-chars-forward "^*{") (point)))
486 (insert nsc))
487 (YaTeX-on-section-command-p YaTeX-sectioning-regexp)
488 (setq to (YaTeX-match-string 0)
489 repllist (cons (cons from to) repllist))
490 (forward-line 1))
491 (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent)
492 (goto-buffer-window YaTeX-sectioning-buffer-parent)
493 (save-excursion
494 (goto-char (point-max))
495 (undo-boundary)
496 (while repllist
497 (if (search-backward (car (car repllist)) nil t)
498 (progn
499 (goto-char (match-beginning 0)) ;confirm
500 (delete-region (point) (match-end 0))
501 (insert (cdr (car repllist)))
502 (goto-char (match-beginning 0))))
503 (setq repllist (cdr repllist))))
504 (goto-buffer-window cb)))
505 )
506 (defun YaTeX-shift-section-down-region (beg end n)
507 "Shift sectioning commands in region down by level N."
508 (interactive "r\np")
509 (YaTeX-shift-section-up-region beg end (- n))
510 )
511 (defun YaTeX-sectioning-buffer-next-line (n)
512 "Move to next line in *Sectioning Lines* buffer."
513 (interactive "p")
514 (forward-line n)
515 (skip-chars-forward " \t%")
516 )
517 (defun YaTeX-sectioning-buffer-prev-line (n)
518 "Move to previous line in *Sectioning Lines* buffer."
519 (interactive "p")
520 (YaTeX-sectioning-buffer-next-line (- n))
521 )
522 (provide 'yatexsec)