yuuji@287: ;;; yatexsec.el --- YaTeX sectioning browser yuuji@287: ;;; yuuji@451: ;;; (c) 1994-2017 by HIROSE Yuuji [yuuji@yatex.org] yuuji@532: ;;; Last modified Wed May 30 13:29:50 2018 on firestorm yuuji@366: ;;; $Id$ yuuji@23: yuuji@287: ;;; Code: yuuji@23: (defvar YaTeX-sectioning-level yuuji@49: '(("part" . 0) yuuji@49: ("chapter" . 1) yuuji@49: ("section" . 2) yuuji@49: ("subsection" . 3) yuuji@49: ("subsubsection" . 4) yuuji@49: ("paragraph" . 5) yuuji@49: ("subparagraph" . 6)) yuuji@49: "*Alist of LaTeX's sectioning command and its level. yuuji@49: This value must be written in numerically ascending order and consecutive. yuuji@49: Needn't define the level of `*' commands such as `section*'.") yuuji@49: yuuji@49: (defvar YaTeX-sectioning-max-level yuuji@49: (cdr (nth (1- (length YaTeX-sectioning-level)) YaTeX-sectioning-level)) yuuji@49: "*The heighest(numerically) level of sectioning command. yuuji@49: This must be the heighest number in YaTeX-sectioning-level.") yuuji@23: yuuji@23: (defun YaTeX-sectioning-map-hide (map) yuuji@23: (let ((ch ?0)) yuuji@23: (while (<= ch ?9) yuuji@23: (define-key map (char-to-string ch) 'YaTeX-sectioning-hide) yuuji@23: (setq ch (1+ ch)))) yuuji@23: ) yuuji@23: yuuji@49: (defvar YaTeX-sectioning-minibuffer-map nil yuuji@23: "Key map used in minibuffer for sectioning.") yuuji@49: (if YaTeX-sectioning-minibuffer-map nil yuuji@49: (setq YaTeX-sectioning-minibuffer-map yuuji@23: (copy-keymap minibuffer-local-completion-map)) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map "\C-p" yuuji@23: 'YaTeX-sectioning-up) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map "\C-e" yuuji@23: 'YaTeX-sectioning-up) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map "\C-i" yuuji@23: 'YaTeX-minibuffer-complete) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map " " yuuji@23: 'YaTeX-minibuffer-complete) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map "\C-n" yuuji@23: 'YaTeX-sectioning-down) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map "\C-x" yuuji@23: 'YaTeX-sectioning-down) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map "\C-v" yuuji@23: 'YaTeX-sectioning-scroll-up) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map "\C-c" yuuji@23: 'YaTeX-sectioning-scroll-up) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map "\M-v" yuuji@23: 'YaTeX-sectioning-scroll-down) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map "\C-r" yuuji@23: 'YaTeX-sectioning-scroll-down) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map "\C-w" yuuji@353: (function (lambda () (interactive) (YaTeX-sectioning-scroll-down 1)))) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map "\C-z" yuuji@353: (function (lambda () (interactive) (YaTeX-sectioning-scroll-up 1)))) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map "\C-l" yuuji@23: 'YaTeX-sectioning-recenter) yuuji@49: (define-key YaTeX-sectioning-minibuffer-map "?" yuuji@23: 'YaTeX-sectioning-help) yuuji@49: (YaTeX-sectioning-map-hide YaTeX-sectioning-minibuffer-map) yuuji@23: ) yuuji@23: yuuji@23: (defvar YaTeX-sectioning-buffer-map nil yuuji@23: "Key map used in YaTeX-sectioning-buffer.") yuuji@23: (if YaTeX-sectioning-buffer-map nil yuuji@23: (setq YaTeX-sectioning-buffer-map (make-sparse-keymap)) yuuji@58: (define-key YaTeX-sectioning-buffer-map " " 'YaTeX-sectioning-buffer-jump) yuuji@58: (define-key YaTeX-sectioning-buffer-map "." 'YaTeX-sectioning-buffer-show) yuuji@23: (define-key YaTeX-sectioning-buffer-map (concat YaTeX-prefix "\C-c") yuuji@23: 'YaTeX-sectioning-buffer-jump) yuuji@58: (define-key YaTeX-sectioning-buffer-map "u" 'YaTeX-shift-section-up) yuuji@58: (define-key YaTeX-sectioning-buffer-map "d" 'YaTeX-shift-section-down) yuuji@58: (define-key YaTeX-sectioning-buffer-map "U" 'YaTeX-shift-section-up-region) yuuji@58: (define-key YaTeX-sectioning-buffer-map "D" 'YaTeX-shift-section-down-region) yuuji@58: (define-key YaTeX-sectioning-buffer-map "s" 'YaTeX-sync-section-buffer) yuuji@58: (define-key YaTeX-sectioning-buffer-map "n" yuuji@58: 'YaTeX-sectioning-buffer-next-line) yuuji@58: (define-key YaTeX-sectioning-buffer-map "p" yuuji@58: 'YaTeX-sectioning-buffer-prev-line) yuuji@58: (define-key YaTeX-sectioning-buffer-map "h" 'describe-mode) yuuji@58: (define-key YaTeX-sectioning-buffer-map "o" 'other-window) yuuji@58: (define-key YaTeX-sectioning-buffer-map "-" 'shrink-window) yuuji@58: (define-key YaTeX-sectioning-buffer-map "+" 'enlarge-window) yuuji@61: (define-key YaTeX-sectioning-buffer-map "q" 'delete-window) yuuji@58: (define-key YaTeX-sectioning-buffer-map "\C-_" 'YaTeX-shift-section-undo) yuuji@58: (and YaTeX-emacs-19 (boundp 'window-system) (eq window-system 'x) yuuji@58: (define-key YaTeX-sectioning-buffer-map [?\C-/] yuuji@58: 'YaTeX-shift-section-undo)) yuuji@23: (YaTeX-sectioning-map-hide YaTeX-sectioning-buffer-map) yuuji@23: ) yuuji@23: yuuji@58: (defun YaTeX-sectioning-mode () yuuji@58: "Mode for browsing document's sectioning structure. yuuji@58: \\[YaTeX-shift-section-up] Shift up a sectioning command yuuji@58: \\[YaTeX-shift-section-down] Shift down a sectioning command yuuji@58: \\[YaTeX-shift-section-up-region] Shift up sectioning commands in region yuuji@58: \\[YaTeX-shift-section-down-region] Shift down sectioning commands in region yuuji@58: \\[YaTeX-shift-section-undo] Undo changes of shifting yuuji@58: \\[YaTeX-sync-section-buffer] Synchronize sectioning buffer with source yuuji@58: \\[YaTeX-sectioning-buffer-next-line] Next line yuuji@58: \\[YaTeX-sectioning-buffer-prev-line] Previous line yuuji@58: \\[YaTeX-sectioning-buffer-jump] Previous line yuuji@58: \\[YaTeX-sectioning-buffer-show] Show curresponding source line yuuji@58: " yuuji@58: (interactive) yuuji@58: (setq major-mode 'YaTeX-sectioning-mode yuuji@58: mode-name "sectioning") yuuji@58: (use-local-map YaTeX-sectioning-buffer-map) yuuji@58: ) yuuji@58: yuuji@23: (defvar YaTeX-sectioning-buffer-parent nil) yuuji@58: (defun YaTeX-sectioning-buffer-jump-internal (&optional keep) yuuji@77: (let ((p (point)) ;save-excursion is NG because yuuji@77: ptn ln) ;this function should switch buffer yuuji@77: (beginning-of-line) yuuji@58: (if (re-search-forward YaTeX-sectioning-regexp) yuuji@77: (progn yuuji@77: (save-restriction yuuji@77: (narrow-to-region (point-beginning-of-line) (point-end-of-line)) yuuji@77: (setq ptn (buffer-substring yuuji@77: (1- (match-beginning 0)) yuuji@77: (progn (skip-chars-forward "^}") (1+ (point)))) yuuji@77: ln (buffer-substring yuuji@77: (progn (search-forward "line:") (match-end 0)) yuuji@77: (progn (skip-chars-forward "0-9") (point))))) yuuji@77: (goto-char p) yuuji@77: (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent nil t) yuuji@77: (or yuuji@77: (and ln (string< "" ln) yuuji@77: (progn yuuji@178: (goto-char (point-min)) yuuji@451: (forward-line (max 0 (- (YaTeX-str2int ln) 2))) yuuji@77: (and yuuji@77: (search-forward ptn nil t) yuuji@77: (goto-char (match-beginning 0))))) yuuji@77: (progn yuuji@77: (goto-char (point-max)) yuuji@77: (search-backward ptn))) yuuji@77: (if keep (goto-buffer-window YaTeX-sectioning-buffer)) yuuji@77: (current-buffer)) yuuji@77: nil))) yuuji@77: yuuji@58: (defun YaTeX-sectioning-buffer-jump (&optional keep) yuuji@58: "Goto corresponding sectioning unit with current line in the next window. yuuji@58: If optional argument KEEP is non-nil, only shows the line." yuuji@23: (interactive) yuuji@23: (if (and YaTeX-sectioning-buffer-parent yuuji@23: (get-buffer YaTeX-sectioning-buffer-parent)) yuuji@58: (YaTeX-sectioning-buffer-jump-internal keep) yuuji@58: (message "No line number expression.")) yuuji@58: ) yuuji@58: yuuji@58: (defun YaTeX-sectioning-buffer-show () yuuji@58: "Show corresponding sectioning unit with current line." yuuji@58: (interactive) yuuji@58: (YaTeX-sectioning-buffer-jump-internal t) yuuji@23: ) yuuji@23: yuuji@23: (defun YaTeX-sectioning-hide-under (n) yuuji@23: "Hide sectioning commands under level N." yuuji@23: (let ((cw (selected-window))) yuuji@23: (YaTeX-showup-buffer YaTeX-sectioning-buffer nil t) yuuji@49: (if (>= n YaTeX-sectioning-max-level) yuuji@23: (progn yuuji@23: (set-selective-display nil) yuuji@23: (message "Show all.")) yuuji@23: (set-selective-display (1+ n)) yuuji@49: (if (rassq n YaTeX-sectioning-level) yuuji@49: (message "Hide lower than %s" (car (rassq n YaTeX-sectioning-level))) yuuji@23: (message ""))) yuuji@23: (if (numberp selective-display) yuuji@23: (setq mode-name (format "level %d" (1- selective-display))) yuuji@23: (setq mode-name (format "all"))) yuuji@23: (select-window cw)) yuuji@23: ) yuuji@23: (defun YaTeX-sectioning-hide () yuuji@23: "Call YaTeX-sectioning-hide-under with argument according to pressed key." yuuji@23: (interactive) yuuji@290: (YaTeX-sectioning-hide-under (- (YaTeX-last-key) ?0))) yuuji@23: yuuji@23: (defun YaTeX-sectioning-help () yuuji@23: "Show help of sectioning." yuuji@23: (interactive) yuuji@23: (let ((cw (selected-window)) sb (hb (get-buffer-create "*Help*"))) yuuji@23: (unwind-protect yuuji@23: (progn yuuji@23: (other-window 1) yuuji@23: (setq sb (current-buffer)) yuuji@23: (switch-to-buffer hb) yuuji@532: (setq buffer-read-only nil) ;; Emacs26 yuuji@23: (erase-buffer) yuuji@23: (insert "===== View sectioning ===== yuuji@23: C-p Up sectioning level. 0 Show only \\part, yuuji@23: C-n Down sectioning level. 1 and \\chapter, yuuji@23: C-v Scroll up *Sectioning line* buffer. 2 and \\section, yuuji@23: M-v Scroll down *Sectioning line* buffer. 3 and \\subsection, yuuji@49: C-z Scroll up by 1 line. 4 and \\subsubsection, yuuji@49: C-w Scroll down by 1 line. 5 and \\paragraph. yuuji@49: SPC Complete word. 6 Show all. yuuji@49: TAB Complete word. yuuji@49: C-l Recenter recent line. yuuji@23: RET Select. yuuji@23: ==== End of HELP ===== yuuji@23: ") yuuji@23: (set-buffer-modified-p nil) yuuji@23: (goto-char (point-min)) yuuji@23: (momentary-string-display "" (point-min))) yuuji@23: (bury-buffer hb) yuuji@23: (switch-to-buffer sb) yuuji@23: (select-window cw))) yuuji@23: ) yuuji@23: yuuji@23: (defun YaTeX-sectioning-up (n) yuuji@23: "Up section level. yuuji@23: Refers the YaTeX-read-section-in-minibuffer's local variable minibuffer-start." yuuji@23: (interactive "p") yuuji@23: (if (eq (selected-window) (minibuffer-window)) yuuji@72: (let*((command (YaTeX-minibuffer-string)) yuuji@53: (aster (and (string< "" command) yuuji@53: (equal (substring command -1) "*"))) yuuji@49: (command (if aster (substring command 0 -1) command)) yuuji@23: (alist YaTeX-sectioning-level) yuuji@49: (level 0)) yuuji@49: (or (assoc command alist) (error "No such sectioning command.")) yuuji@49: (while (not (string= (car (nth level alist)) command)) yuuji@49: (setq level (1+ level))) ;I want to use `member'.... yuuji@23: (setq level (- level n)) yuuji@23: (if (or (< level 0) (>= level (length alist))) yuuji@23: (ding) yuuji@72: (YaTeX-minibuffer-erase) yuuji@49: (insert (concat (car (nth level alist)) (if aster "*" "")))))) yuuji@23: ) yuuji@23: yuuji@23: (defun YaTeX-sectioning-down (n) yuuji@23: "Down section level." yuuji@23: (interactive "p") yuuji@23: (YaTeX-sectioning-up (- n)) yuuji@23: ) yuuji@23: yuuji@23: (defun YaTeX-sectioning-scroll-up (n) yuuji@23: (interactive "P") yuuji@23: (let ((section-buffer YaTeX-sectioning-buffer) yuuji@23: (cw (selected-window))) yuuji@23: (YaTeX-showup-buffer section-buffer nil t) yuuji@23: (unwind-protect yuuji@23: (scroll-up (or n (- (window-height) 2))) yuuji@23: (select-window cw))) yuuji@23: ) yuuji@23: yuuji@23: (defun YaTeX-sectioning-scroll-down (n) yuuji@23: (interactive "P") yuuji@23: (let ((section-buffer YaTeX-sectioning-buffer) yuuji@23: (cw (selected-window))) yuuji@23: (YaTeX-showup-buffer section-buffer nil t) yuuji@23: (unwind-protect yuuji@23: (scroll-down (or n (- (window-height) 2))) yuuji@23: (select-window cw))) yuuji@23: ) yuuji@23: yuuji@23: (defun YaTeX-sectioning-recenter (arg) yuuji@23: "Recenter `<<--' line" yuuji@23: (interactive "P") yuuji@23: (let ((cw (selected-window))) yuuji@23: (unwind-protect yuuji@23: (progn yuuji@23: (YaTeX-showup-buffer YaTeX-sectioning-buffer nil t) yuuji@23: (or (search-forward "<<--" nil t) yuuji@49: (search-backward "<<--" nil)) yuuji@23: (recenter (or arg (/ (window-height) 2)))) yuuji@23: (select-window cw))) yuuji@23: ) yuuji@23: yuuji@23: (defvar YaTeX-sectioning-minibuffer " *sectioning*" yuuji@23: "Miniuffer used for sectioning") yuuji@49: ;;;###autoload yuuji@23: (defun YaTeX-read-section-in-minibuffer (prompt table &optional default delim) yuuji@23: (interactive) yuuji@23: (let ((minibuffer-completion-table table)) yuuji@23: (read-from-minibuffer yuuji@49: prompt default YaTeX-sectioning-minibuffer-map)) yuuji@23: ) yuuji@23: yuuji@23: (defun YaTeX-get-sectioning-level () yuuji@23: "Get section-level on the cursor." yuuji@23: (cdr-safe (assoc (buffer-substring yuuji@23: (point) yuuji@23: (progn (skip-chars-forward "a-z") (point))) yuuji@23: YaTeX-sectioning-level)) yuuji@23: ) yuuji@23: yuuji@23: (defvar YaTeX-sectioning-buffer "*Sectioning lines*") yuuji@64: (defvar YaTeX-sectioning-indent 1) yuuji@72: (defun YaTeX-collect-sections () yuuji@23: "Collect all the lines which contains sectioning command." yuuji@23: (let ((cw (selected-window)) level indent begp (prevp 1) (prevl 1) yuuji@23: (pattern (concat YaTeX-ec-regexp yuuji@23: "\\(" YaTeX-sectioning-regexp "\\)\\*?{")) yuuji@23: (cb (current-buffer))) yuuji@23: (save-excursion yuuji@58: (set-buffer (get-buffer-create YaTeX-sectioning-buffer)) yuuji@58: (setq buffer-read-only nil) yuuji@70: (erase-buffer) yuuji@58: (set-buffer cb) yuuji@23: (YaTeX-showup-buffer YaTeX-sectioning-buffer) ;show buffer yuuji@23: (goto-char (point-min)) yuuji@68: (let ((standard-output (get-buffer YaTeX-sectioning-buffer))) yuuji@23: (while (re-search-forward pattern nil t) yuuji@23: (goto-char (1+ (match-beginning 0))) yuuji@23: (setq level (YaTeX-get-sectioning-level) yuuji@23: begp (match-beginning 0)) yuuji@23: ;;(beginning-of-line) yuuji@23: ;;(skip-chars-forward " \t") yuuji@64: (setq indent (format "%%%ds" (* level YaTeX-sectioning-indent))) yuuji@23: (princ (format indent "")) yuuji@23: (if (YaTeX-on-comment-p) (princ "%")) yuuji@23: (princ (buffer-substring begp (progn (forward-list 1) (point)))) yuuji@23: (setq prevl (+ prevl (count-lines prevp (point)) -1) yuuji@23: prevp (point)) yuuji@23: (princ (format " (line:%d)" prevl)) yuuji@23: (princ "\n"))) yuuji@23: (set-buffer YaTeX-sectioning-buffer) yuuji@23: (make-local-variable 'YaTeX-sectioning-buffer-parent) yuuji@58: (YaTeX-sectioning-mode) yuuji@23: (use-local-map YaTeX-sectioning-buffer-map) yuuji@23: (setq YaTeX-sectioning-buffer-parent cb) yuuji@23: (if (numberp selective-display) yuuji@23: (setq mode-name (format "level %d" (1- selective-display)))) yuuji@23: YaTeX-sectioning-buffer)) yuuji@23: ) yuuji@23: yuuji@58: (defvar YaTeX-pending-undo nil) yuuji@23: (defun YaTeX-section-overview () yuuji@23: "Show section overview. Return the nearest sectioning command." yuuji@23: (interactive) yuuji@23: (let ((cw (selected-window)) (ln (count-lines (point-min) (point))) yuuji@23: (pattern "(line:\\([0-9]+\\))") yuuji@58: secbuf (command "")) yuuji@23: (save-excursion yuuji@72: (setq secbuf (YaTeX-collect-sections)) yuuji@23: (YaTeX-showup-buffer secbuf nil t) yuuji@58: (set-buffer secbuf) yuuji@23: (goto-char (point-max)) yuuji@23: (while (re-search-backward pattern nil t) yuuji@451: (if (< ln (YaTeX-str2int (YaTeX-match-string 1))) nil yuuji@23: (beginning-of-line) yuuji@23: (search-forward YaTeX-ec) yuuji@23: (looking-at YaTeX-TeX-token-regexp) yuuji@23: (setq command (YaTeX-match-string 0)) yuuji@23: (end-of-line) yuuji@23: (insert " <<--") yuuji@23: (setq pattern (concat "HackyRegexp" "ForFailure")))) yuuji@23: (set-buffer-modified-p nil) yuuji@58: (setq buffer-read-only t buffer-undo-list nil) yuuji@58: (make-local-variable 'YaTeX-pending-undo) yuuji@23: (forward-line 1) yuuji@23: (if (eobp) (recenter -1) (recenter -3)) yuuji@23: (select-window cw) yuuji@23: command)) yuuji@23: ) yuuji@23: yuuji@49: ;;;###autoload yuuji@23: (defun YaTeX-make-section-with-overview () yuuji@23: "Input sectining command with previous overview." yuuji@23: (interactive) yuuji@23: (insert yuuji@23: YaTeX-ec yuuji@23: (YaTeX-read-section-in-minibuffer yuuji@23: "Sectioning(Up=C-p, Down=C-n, Help=?): " yuuji@23: YaTeX-sectioning-level (YaTeX-section-overview)) yuuji@23: "{}") yuuji@23: (forward-char -1) yuuji@23: ) yuuji@23: yuuji@58: (defun YaTeX-shifted-section (sc n) yuuji@58: "Get SC's N-shifted sectioning command." yuuji@58: (let (lv) yuuji@58: (setq lv (- (cdr (assoc sc YaTeX-sectioning-level)) n) yuuji@58: lv (max (min YaTeX-sectioning-max-level lv) 0)) yuuji@58: (car (nth lv YaTeX-sectioning-level))) yuuji@58: ) yuuji@58: yuuji@58: (defun YaTeX-shift-section-up (n) yuuji@58: "Shift sectioning command down by level N." yuuji@58: (interactive "p") yuuji@58: (let ((cb (current-buffer)) sc nsc lv) yuuji@58: (if (and YaTeX-sectioning-buffer-parent yuuji@58: (get-buffer YaTeX-sectioning-buffer-parent) yuuji@58: (save-excursion yuuji@68: (beginning-of-line) yuuji@68: (skip-chars-forward "^\\\\" (point-end-of-line)) yuuji@58: (YaTeX-on-section-command-p YaTeX-sectioning-regexp))) yuuji@58: (save-excursion yuuji@58: (or (buffer-name (get-buffer YaTeX-sectioning-buffer-parent)) yuuji@58: (error "This buffer is obsolete.")) yuuji@58: (setq nsc (YaTeX-shifted-section (YaTeX-match-string 1) n)) yuuji@58: (YaTeX-sectioning-buffer-jump-internal) yuuji@58: (undo-boundary) yuuji@58: (goto-char (match-beginning 0)) yuuji@58: (skip-chars-forward "\\\\") yuuji@58: (delete-region yuuji@58: (point) (progn (skip-chars-forward "^*{") (point))) yuuji@58: (insert nsc) yuuji@58: (undo-boundary) yuuji@58: ;; Return to *Sectioning Lines* buffer yuuji@58: (select-window (get-buffer-window cb)) yuuji@58: (beginning-of-line) yuuji@58: (let (buffer-read-only) yuuji@58: (delete-region yuuji@58: (point) (progn (skip-chars-forward " \t") (point))) yuuji@64: (indent-to-column (* (cdr (assoc nsc YaTeX-sectioning-level)) yuuji@64: YaTeX-sectioning-indent)) yuuji@58: (skip-chars-forward "^\\\\") yuuji@58: (delete-region yuuji@58: (1+ (point)) (progn (skip-chars-forward "^*{") (point))) yuuji@58: (insert nsc) yuuji@58: (undo-boundary)) yuuji@58: (set-buffer-modified-p nil) yuuji@58: (setq YaTeX-pending-undo pending-undo-list) yuuji@58: ))) yuuji@58: ) yuuji@58: (defun YaTeX-shift-section-down (n) yuuji@58: "Shift sectioning command down by level N." yuuji@58: (interactive "p") yuuji@58: (YaTeX-shift-section-up (- n)) yuuji@58: ) yuuji@58: (defun YaTeX-shift-section-undo (arg) yuuji@58: "Undo YaTeX-shift-section-up/down." yuuji@58: (interactive "p") yuuji@58: (and YaTeX-sectioning-buffer-parent yuuji@58: (get-buffer YaTeX-sectioning-buffer-parent) yuuji@58: (equal (current-buffer) (get-buffer YaTeX-sectioning-buffer)) yuuji@58: (let ((cb (current-buffer)) yuuji@58: (lc (if (eq last-command 'YaTeX-shift-section-undo) 'undo t))) yuuji@58: (let ((pending-undo-list YaTeX-pending-undo) yuuji@58: buffer-read-only (last-command lc)) yuuji@58: (undo arg) yuuji@58: (setq YaTeX-pending-undo pending-undo-list)) yuuji@58: (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent) yuuji@58: (goto-buffer-window YaTeX-sectioning-buffer-parent) yuuji@58: (undo-boundary) yuuji@58: (let ((last-command lc) yuuji@58: (pending-undo-list yuuji@58: (if (eq lc 'undo) YaTeX-pending-undo pending-undo-list))) yuuji@58: (undo arg) yuuji@58: (setq YaTeX-pending-undo pending-undo-list)) yuuji@58: (goto-buffer-window cb) yuuji@58: (setq this-command 'YaTeX-shift-section-undo))) yuuji@58: ) yuuji@58: (defun YaTeX-sync-section-buffer () yuuji@58: "Synchronize *Sectioning Lines* buffer with parent buffer." yuuji@58: (interactive) yuuji@58: (if (and YaTeX-sectioning-buffer-parent yuuji@58: (get-buffer YaTeX-sectioning-buffer-parent)) yuuji@58: (let ((cb (current-buffer)) (p (point))) yuuji@58: (set-buffer (get-buffer YaTeX-sectioning-buffer-parent)) yuuji@58: (YaTeX-section-overview) yuuji@58: (switch-to-buffer cb) yuuji@58: (goto-char p))) yuuji@58: ) yuuji@58: (defun YaTeX-shift-section-up-region (beg end n) yuuji@58: "Shift sectioning commands in region down by level N." yuuji@58: (interactive "r\np") yuuji@58: (or YaTeX-sectioning-buffer-parent yuuji@58: (get-buffer YaTeX-sectioning-buffer-parent) yuuji@58: (error "Can't find corresponding LaTeX buffer")) yuuji@58: (save-excursion yuuji@58: (goto-char beg) yuuji@58: (let ((cb (current-buffer)) nsc from to repllist (e (make-marker))) yuuji@58: (set-marker e end) yuuji@58: (while (progn (skip-chars-forward "^\\\\") (< (point) e)) yuuji@58: (YaTeX-on-section-command-p YaTeX-sectioning-regexp) yuuji@58: (setq from (YaTeX-match-string 0) yuuji@58: nsc (YaTeX-shifted-section (YaTeX-match-string 1) n)) yuuji@58: (goto-char (match-beginning 0)) yuuji@58: (let (buffer-read-only) yuuji@68: ;(delete-region (point) (progn (beginning-of-line) (point))) yuuji@68: (delete-region (progn (beginning-of-line) (point)) yuuji@68: (progn (skip-chars-forward " \t") (point))) yuuji@58: (indent-to-column (cdr (assoc nsc YaTeX-sectioning-level))) yuuji@58: (delete-region yuuji@68: (progn (skip-chars-forward "%\\\\") (point)) yuuji@68: (progn (skip-chars-forward "^*{") (point))) yuuji@58: (insert nsc)) yuuji@58: (YaTeX-on-section-command-p YaTeX-sectioning-regexp) yuuji@58: (setq to (YaTeX-match-string 0) yuuji@58: repllist (cons (cons from to) repllist)) yuuji@58: (forward-line 1)) yuuji@58: (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent) yuuji@58: (goto-buffer-window YaTeX-sectioning-buffer-parent) yuuji@58: (save-excursion yuuji@58: (goto-char (point-max)) yuuji@58: (undo-boundary) yuuji@58: (while repllist yuuji@58: (if (search-backward (car (car repllist)) nil t) yuuji@58: (progn yuuji@58: (goto-char (match-beginning 0)) ;confirm yuuji@58: (delete-region (point) (match-end 0)) yuuji@58: (insert (cdr (car repllist))) yuuji@58: (goto-char (match-beginning 0)))) yuuji@58: (setq repllist (cdr repllist)))) yuuji@58: (goto-buffer-window cb))) yuuji@58: ) yuuji@58: (defun YaTeX-shift-section-down-region (beg end n) yuuji@58: "Shift sectioning commands in region down by level N." yuuji@58: (interactive "r\np") yuuji@58: (YaTeX-shift-section-up-region beg end (- n)) yuuji@58: ) yuuji@58: (defun YaTeX-sectioning-buffer-next-line (n) yuuji@58: "Move to next line in *Sectioning Lines* buffer." yuuji@58: (interactive "p") yuuji@58: (forward-line n) yuuji@58: (skip-chars-forward " \t%") yuuji@58: ) yuuji@58: (defun YaTeX-sectioning-buffer-prev-line (n) yuuji@58: "Move to previous line in *Sectioning Lines* buffer." yuuji@58: (interactive "p") yuuji@58: (YaTeX-sectioning-buffer-next-line (- n)) yuuji@58: ) yuuji@23: (provide 'yatexsec)