yatex

view yatexsec.el @ 626:5f6ffe70a6ca

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