yatex

annotate yatexsec.el @ 64:36a48185b95a

Changes are listed in yatex.new. Major one is supporing GNU Emacs20 and XEmacs.
author yuuji
date Tue, 16 Dec 1997 13:28:38 +0000
parents b9f753846b6b
children 0eb6997bee16
rev   line source
yuuji@23 1 ;;; -*- Emacs-Lisp -*-
yuuji@23 2 ;;; YaTeX sectioning browser.
yuuji@23 3 ;;; yatexsec.el
yuuji@23 4 ;;; (c ) 1994 by HIROSE Yuuji [yuuji@ae.keio.ac.jp]
yuuji@61 5 ;;; Last modified Mon Feb 17 11:26:44 1997 on supra
yuuji@23 6 ;;; $Id$
yuuji@23 7
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@23 58 '(lambda () (interactive) (YaTeX-sectioning-scroll-down 1)))
yuuji@49 59 (define-key YaTeX-sectioning-minibuffer-map "\C-z"
yuuji@23 60 '(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@58 118 (let (ptn (p (point))) ;save-excursion is NG because
yuuji@58 119 (beginning-of-line) ;this function should switch buffer
yuuji@58 120 (if (re-search-forward YaTeX-sectioning-regexp)
yuuji@58 121 (progn (setq ptn (buffer-substring
yuuji@58 122 (1- (match-beginning 0))
yuuji@58 123 (progn (skip-chars-forward "^}") (1+ (point)))))
yuuji@58 124 (goto-char p)
yuuji@58 125 (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent nil t)
yuuji@58 126 (goto-char (point-max))
yuuji@58 127 (search-backward ptn)
yuuji@58 128 (if keep (goto-buffer-window YaTeX-sectioning-buffer))
yuuji@58 129 (current-buffer))
yuuji@58 130 nil))
yuuji@58 131 )
yuuji@58 132 (defun YaTeX-sectioning-buffer-jump (&optional keep)
yuuji@58 133 "Goto corresponding sectioning unit with current line in the next window.
yuuji@58 134 If optional argument KEEP is non-nil, only shows the line."
yuuji@23 135 (interactive)
yuuji@23 136 (if (and YaTeX-sectioning-buffer-parent
yuuji@23 137 (get-buffer YaTeX-sectioning-buffer-parent))
yuuji@58 138 (YaTeX-sectioning-buffer-jump-internal keep)
yuuji@58 139 (message "No line number expression."))
yuuji@58 140 )
yuuji@58 141
yuuji@58 142 (defun YaTeX-sectioning-buffer-show ()
yuuji@58 143 "Show corresponding sectioning unit with current line."
yuuji@58 144 (interactive)
yuuji@58 145 (YaTeX-sectioning-buffer-jump-internal t)
yuuji@23 146 )
yuuji@23 147
yuuji@23 148 (defun YaTeX-sectioning-hide-under (n)
yuuji@23 149 "Hide sectioning commands under level N."
yuuji@23 150 (let ((cw (selected-window)))
yuuji@23 151 (YaTeX-showup-buffer YaTeX-sectioning-buffer nil t)
yuuji@49 152 (if (>= n YaTeX-sectioning-max-level)
yuuji@23 153 (progn
yuuji@23 154 (set-selective-display nil)
yuuji@23 155 (message "Show all."))
yuuji@23 156 (set-selective-display (1+ n))
yuuji@49 157 (if (rassq n YaTeX-sectioning-level)
yuuji@49 158 (message "Hide lower than %s" (car (rassq n YaTeX-sectioning-level)))
yuuji@23 159 (message "")))
yuuji@23 160 (if (numberp selective-display)
yuuji@23 161 (setq mode-name (format "level %d" (1- selective-display)))
yuuji@23 162 (setq mode-name (format "all")))
yuuji@23 163 (select-window cw))
yuuji@23 164 )
yuuji@23 165 (defun YaTeX-sectioning-hide ()
yuuji@23 166 "Call YaTeX-sectioning-hide-under with argument according to pressed key."
yuuji@23 167 (interactive)
yuuji@23 168 (YaTeX-sectioning-hide-under (- last-command-char ?0)))
yuuji@23 169
yuuji@23 170 (defun YaTeX-sectioning-help ()
yuuji@23 171 "Show help of sectioning."
yuuji@23 172 (interactive)
yuuji@23 173 (let ((cw (selected-window)) sb (hb (get-buffer-create "*Help*")))
yuuji@23 174 (unwind-protect
yuuji@23 175 (progn
yuuji@23 176 (other-window 1)
yuuji@23 177 (setq sb (current-buffer))
yuuji@23 178 (switch-to-buffer hb)
yuuji@23 179 (erase-buffer)
yuuji@23 180 (insert "===== View sectioning =====
yuuji@23 181 C-p Up sectioning level. 0 Show only \\part,
yuuji@23 182 C-n Down sectioning level. 1 and \\chapter,
yuuji@23 183 C-v Scroll up *Sectioning line* buffer. 2 and \\section,
yuuji@23 184 M-v Scroll down *Sectioning line* buffer. 3 and \\subsection,
yuuji@49 185 C-z Scroll up by 1 line. 4 and \\subsubsection,
yuuji@49 186 C-w Scroll down by 1 line. 5 and \\paragraph.
yuuji@49 187 SPC Complete word. 6 Show all.
yuuji@49 188 TAB Complete word.
yuuji@49 189 C-l Recenter recent line.
yuuji@23 190 RET Select.
yuuji@23 191 ==== End of HELP =====
yuuji@23 192 ")
yuuji@23 193 (set-buffer-modified-p nil)
yuuji@23 194 (goto-char (point-min))
yuuji@23 195 (momentary-string-display "" (point-min)))
yuuji@23 196 (bury-buffer hb)
yuuji@23 197 (switch-to-buffer sb)
yuuji@23 198 (select-window cw)))
yuuji@23 199 )
yuuji@23 200
yuuji@23 201 (defun YaTeX-sectioning-up (n)
yuuji@23 202 "Up section level.
yuuji@23 203 Refers the YaTeX-read-section-in-minibuffer's local variable minibuffer-start."
yuuji@23 204 (interactive "p")
yuuji@23 205 (if (eq (selected-window) (minibuffer-window))
yuuji@23 206 (let*((command (buffer-string))
yuuji@53 207 (aster (and (string< "" command)
yuuji@53 208 (equal (substring command -1) "*")))
yuuji@49 209 (command (if aster (substring command 0 -1) command))
yuuji@23 210 (alist YaTeX-sectioning-level)
yuuji@49 211 (level 0))
yuuji@49 212 (or (assoc command alist) (error "No such sectioning command."))
yuuji@49 213 (while (not (string= (car (nth level alist)) command))
yuuji@49 214 (setq level (1+ level))) ;I want to use `member'....
yuuji@23 215 (setq level (- level n))
yuuji@23 216 (if (or (< level 0) (>= level (length alist)))
yuuji@23 217 (ding)
yuuji@23 218 (erase-buffer)
yuuji@49 219 (insert (concat (car (nth level alist)) (if aster "*" ""))))))
yuuji@23 220 )
yuuji@23 221
yuuji@23 222 (defun YaTeX-sectioning-down (n)
yuuji@23 223 "Down section level."
yuuji@23 224 (interactive "p")
yuuji@23 225 (YaTeX-sectioning-up (- n))
yuuji@23 226 )
yuuji@23 227
yuuji@23 228 (defun YaTeX-sectioning-scroll-up (n)
yuuji@23 229 (interactive "P")
yuuji@23 230 (let ((section-buffer YaTeX-sectioning-buffer)
yuuji@23 231 (cw (selected-window)))
yuuji@23 232 (YaTeX-showup-buffer section-buffer nil t)
yuuji@23 233 (unwind-protect
yuuji@23 234 (scroll-up (or n (- (window-height) 2)))
yuuji@23 235 (select-window cw)))
yuuji@23 236 )
yuuji@23 237
yuuji@23 238 (defun YaTeX-sectioning-scroll-down (n)
yuuji@23 239 (interactive "P")
yuuji@23 240 (let ((section-buffer YaTeX-sectioning-buffer)
yuuji@23 241 (cw (selected-window)))
yuuji@23 242 (YaTeX-showup-buffer section-buffer nil t)
yuuji@23 243 (unwind-protect
yuuji@23 244 (scroll-down (or n (- (window-height) 2)))
yuuji@23 245 (select-window cw)))
yuuji@23 246 )
yuuji@23 247
yuuji@23 248 (defun YaTeX-sectioning-recenter (arg)
yuuji@23 249 "Recenter `<<--' line"
yuuji@23 250 (interactive "P")
yuuji@23 251 (let ((cw (selected-window)))
yuuji@23 252 (unwind-protect
yuuji@23 253 (progn
yuuji@23 254 (YaTeX-showup-buffer YaTeX-sectioning-buffer nil t)
yuuji@23 255 (or (search-forward "<<--" nil t)
yuuji@49 256 (search-backward "<<--" nil))
yuuji@23 257 (recenter (or arg (/ (window-height) 2))))
yuuji@23 258 (select-window cw)))
yuuji@23 259 )
yuuji@23 260
yuuji@23 261 (defvar YaTeX-sectioning-minibuffer " *sectioning*"
yuuji@23 262 "Miniuffer used for sectioning")
yuuji@49 263 ;;;###autoload
yuuji@23 264 (defun YaTeX-read-section-in-minibuffer (prompt table &optional default delim)
yuuji@23 265 (interactive)
yuuji@23 266 (let ((minibuffer-completion-table table))
yuuji@23 267 (read-from-minibuffer
yuuji@49 268 prompt default YaTeX-sectioning-minibuffer-map))
yuuji@23 269 )
yuuji@23 270
yuuji@23 271 (defun YaTeX-get-sectioning-level ()
yuuji@23 272 "Get section-level on the cursor."
yuuji@23 273 (cdr-safe (assoc (buffer-substring
yuuji@23 274 (point)
yuuji@23 275 (progn (skip-chars-forward "a-z") (point)))
yuuji@23 276 YaTeX-sectioning-level))
yuuji@23 277 )
yuuji@23 278
yuuji@23 279 (defvar YaTeX-sectioning-buffer "*Sectioning lines*")
yuuji@64 280 (defvar YaTeX-sectioning-indent 1)
yuuji@23 281 (defun YaTeX-colloect-sections ()
yuuji@23 282 "Collect all the lines which contains sectioning command."
yuuji@23 283 (let ((cw (selected-window)) level indent begp (prevp 1) (prevl 1)
yuuji@23 284 (pattern (concat YaTeX-ec-regexp
yuuji@23 285 "\\(" YaTeX-sectioning-regexp "\\)\\*?{"))
yuuji@23 286 (cb (current-buffer)))
yuuji@23 287 (save-excursion
yuuji@58 288 (set-buffer (get-buffer-create YaTeX-sectioning-buffer))
yuuji@58 289 (setq buffer-read-only nil)
yuuji@58 290 (set-buffer cb)
yuuji@23 291 (YaTeX-showup-buffer YaTeX-sectioning-buffer) ;show buffer
yuuji@23 292 (goto-char (point-min))
yuuji@23 293 (with-output-to-temp-buffer YaTeX-sectioning-buffer
yuuji@23 294 (while (re-search-forward pattern nil t)
yuuji@23 295 (goto-char (1+ (match-beginning 0)))
yuuji@23 296 (setq level (YaTeX-get-sectioning-level)
yuuji@23 297 begp (match-beginning 0))
yuuji@23 298 ;;(beginning-of-line)
yuuji@23 299 ;;(skip-chars-forward " \t")
yuuji@64 300 (setq indent (format "%%%ds" (* level YaTeX-sectioning-indent)))
yuuji@23 301 (princ (format indent ""))
yuuji@23 302 (if (YaTeX-on-comment-p) (princ "%"))
yuuji@23 303 (princ (buffer-substring begp (progn (forward-list 1) (point))))
yuuji@23 304 (setq prevl (+ prevl (count-lines prevp (point)) -1)
yuuji@23 305 prevp (point))
yuuji@23 306 (princ (format " (line:%d)" prevl))
yuuji@23 307 (princ "\n")))
yuuji@23 308 (set-buffer YaTeX-sectioning-buffer)
yuuji@23 309 (make-local-variable 'YaTeX-sectioning-buffer-parent)
yuuji@58 310 (YaTeX-sectioning-mode)
yuuji@23 311 (use-local-map YaTeX-sectioning-buffer-map)
yuuji@23 312 (setq YaTeX-sectioning-buffer-parent cb)
yuuji@23 313 (if (numberp selective-display)
yuuji@23 314 (setq mode-name (format "level %d" (1- selective-display))))
yuuji@23 315 YaTeX-sectioning-buffer))
yuuji@23 316 )
yuuji@23 317
yuuji@58 318 (defvar YaTeX-pending-undo nil)
yuuji@23 319 (defun YaTeX-section-overview ()
yuuji@23 320 "Show section overview. Return the nearest sectioning command."
yuuji@23 321 (interactive)
yuuji@23 322 (let ((cw (selected-window)) (ln (count-lines (point-min) (point)))
yuuji@23 323 (pattern "(line:\\([0-9]+\\))")
yuuji@58 324 secbuf (command ""))
yuuji@23 325 (save-excursion
yuuji@23 326 (setq secbuf (YaTeX-colloect-sections))
yuuji@23 327 (YaTeX-showup-buffer secbuf nil t)
yuuji@58 328 (set-buffer secbuf)
yuuji@23 329 (goto-char (point-max))
yuuji@23 330 (while (re-search-backward pattern nil t)
yuuji@23 331 (if (< ln (string-to-int (YaTeX-match-string 1))) nil
yuuji@23 332 (beginning-of-line)
yuuji@23 333 (search-forward YaTeX-ec)
yuuji@23 334 (looking-at YaTeX-TeX-token-regexp)
yuuji@23 335 (setq command (YaTeX-match-string 0))
yuuji@23 336 (end-of-line)
yuuji@23 337 (insert " <<--")
yuuji@23 338 (setq pattern (concat "HackyRegexp" "ForFailure"))))
yuuji@23 339 (set-buffer-modified-p nil)
yuuji@58 340 (setq buffer-read-only t buffer-undo-list nil)
yuuji@58 341 (make-local-variable 'YaTeX-pending-undo)
yuuji@23 342 (forward-line 1)
yuuji@23 343 (if (eobp) (recenter -1) (recenter -3))
yuuji@23 344 (select-window cw)
yuuji@23 345 command))
yuuji@23 346 )
yuuji@23 347
yuuji@49 348 ;;;###autoload
yuuji@23 349 (defun YaTeX-make-section-with-overview ()
yuuji@23 350 "Input sectining command with previous overview."
yuuji@23 351 (interactive)
yuuji@23 352 (insert
yuuji@23 353 YaTeX-ec
yuuji@23 354 (YaTeX-read-section-in-minibuffer
yuuji@23 355 "Sectioning(Up=C-p, Down=C-n, Help=?): "
yuuji@23 356 YaTeX-sectioning-level (YaTeX-section-overview))
yuuji@23 357 "{}")
yuuji@23 358 (forward-char -1)
yuuji@23 359 )
yuuji@23 360
yuuji@58 361 (defun YaTeX-shifted-section (sc n)
yuuji@58 362 "Get SC's N-shifted sectioning command."
yuuji@58 363 (let (lv)
yuuji@58 364 (setq lv (- (cdr (assoc sc YaTeX-sectioning-level)) n)
yuuji@58 365 lv (max (min YaTeX-sectioning-max-level lv) 0))
yuuji@58 366 (car (nth lv YaTeX-sectioning-level)))
yuuji@58 367 )
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@58 372 (let ((cb (current-buffer)) sc 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@58 376 (or (= (char-after (point)) ?\\ )
yuuji@58 377 (skip-chars-backward "^\\\\" (point-beginning-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@58 406 )))
yuuji@58 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@58 411 (YaTeX-shift-section-up (- n))
yuuji@58 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@58 434 (setq this-command 'YaTeX-shift-section-undo)))
yuuji@58 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@58 445 (goto-char p)))
yuuji@58 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@58 463 (delete-region (point) (progn (beginning-of-line) (point)))
yuuji@58 464 (indent-to-column (cdr (assoc nsc YaTeX-sectioning-level)))
yuuji@58 465 (delete-region
yuuji@58 466 (1+ (point)) (progn (skip-chars-forward "^*{") (point)))
yuuji@58 467 (insert nsc))
yuuji@58 468 (YaTeX-on-section-command-p YaTeX-sectioning-regexp)
yuuji@58 469 (setq to (YaTeX-match-string 0)
yuuji@58 470 repllist (cons (cons from to) repllist))
yuuji@58 471 (forward-line 1))
yuuji@58 472 (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent)
yuuji@58 473 (goto-buffer-window YaTeX-sectioning-buffer-parent)
yuuji@58 474 (save-excursion
yuuji@58 475 (goto-char (point-max))
yuuji@58 476 (undo-boundary)
yuuji@58 477 (while repllist
yuuji@58 478 (if (search-backward (car (car repllist)) nil t)
yuuji@58 479 (progn
yuuji@58 480 (goto-char (match-beginning 0)) ;confirm
yuuji@58 481 (delete-region (point) (match-end 0))
yuuji@58 482 (insert (cdr (car repllist)))
yuuji@58 483 (goto-char (match-beginning 0))))
yuuji@58 484 (setq repllist (cdr repllist))))
yuuji@58 485 (goto-buffer-window cb)))
yuuji@58 486 )
yuuji@58 487 (defun YaTeX-shift-section-down-region (beg end n)
yuuji@58 488 "Shift sectioning commands in region down by level N."
yuuji@58 489 (interactive "r\np")
yuuji@58 490 (YaTeX-shift-section-up-region beg end (- n))
yuuji@58 491 )
yuuji@58 492 (defun YaTeX-sectioning-buffer-next-line (n)
yuuji@58 493 "Move to next line in *Sectioning Lines* buffer."
yuuji@58 494 (interactive "p")
yuuji@58 495 (forward-line n)
yuuji@58 496 (skip-chars-forward " \t%")
yuuji@58 497 )
yuuji@58 498 (defun YaTeX-sectioning-buffer-prev-line (n)
yuuji@58 499 "Move to previous line in *Sectioning Lines* buffer."
yuuji@58 500 (interactive "p")
yuuji@58 501 (YaTeX-sectioning-buffer-next-line (- n))
yuuji@58 502 )
yuuji@23 503 (provide 'yatexsec)