yatex

annotate yatexsec.el @ 627:f294195d0d3f

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