yatex

annotate yatexsec.el @ 582:f4c2dca86202

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