yatex

view yatexsec.el @ 270:1b4e0acd0106

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