yatex

view yatexsec.el @ 60:9e08ed569d80

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