rev |
line source |
yuuji@23
|
1 ;;; -*- Emacs-Lisp -*-
|
yuuji@23
|
2 ;;; YaTeX sectioning browser.
|
yuuji@23
|
3 ;;; yatexsec.el
|
yuuji@79
|
4 ;;; (c) 1994,1998,1999,2003 by HIROSE Yuuji [yuuji@yatex.org]
|
yuuji@79
|
5 ;;; Last modified Fri Jun 27 12:10:34 2003 on firestorm
|
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@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@77
|
136 (goto-line (max 1 (1- (string-to-int ln))))
|
yuuji@77
|
137 (and
|
yuuji@77
|
138 (search-forward ptn nil t)
|
yuuji@77
|
139 (goto-char (match-beginning 0)))))
|
yuuji@77
|
140 (progn
|
yuuji@77
|
141 (goto-char (point-max))
|
yuuji@77
|
142 (search-backward ptn)))
|
yuuji@77
|
143 (if keep (goto-buffer-window YaTeX-sectioning-buffer))
|
yuuji@77
|
144 (current-buffer))
|
yuuji@77
|
145 nil)))
|
yuuji@77
|
146
|
yuuji@58
|
147 (defun YaTeX-sectioning-buffer-jump (&optional keep)
|
yuuji@58
|
148 "Goto corresponding sectioning unit with current line in the next window.
|
yuuji@58
|
149 If optional argument KEEP is non-nil, only shows the line."
|
yuuji@23
|
150 (interactive)
|
yuuji@23
|
151 (if (and YaTeX-sectioning-buffer-parent
|
yuuji@23
|
152 (get-buffer YaTeX-sectioning-buffer-parent))
|
yuuji@58
|
153 (YaTeX-sectioning-buffer-jump-internal keep)
|
yuuji@58
|
154 (message "No line number expression."))
|
yuuji@58
|
155 )
|
yuuji@58
|
156
|
yuuji@58
|
157 (defun YaTeX-sectioning-buffer-show ()
|
yuuji@58
|
158 "Show corresponding sectioning unit with current line."
|
yuuji@58
|
159 (interactive)
|
yuuji@58
|
160 (YaTeX-sectioning-buffer-jump-internal t)
|
yuuji@23
|
161 )
|
yuuji@23
|
162
|
yuuji@23
|
163 (defun YaTeX-sectioning-hide-under (n)
|
yuuji@23
|
164 "Hide sectioning commands under level N."
|
yuuji@23
|
165 (let ((cw (selected-window)))
|
yuuji@23
|
166 (YaTeX-showup-buffer YaTeX-sectioning-buffer nil t)
|
yuuji@49
|
167 (if (>= n YaTeX-sectioning-max-level)
|
yuuji@23
|
168 (progn
|
yuuji@23
|
169 (set-selective-display nil)
|
yuuji@23
|
170 (message "Show all."))
|
yuuji@23
|
171 (set-selective-display (1+ n))
|
yuuji@49
|
172 (if (rassq n YaTeX-sectioning-level)
|
yuuji@49
|
173 (message "Hide lower than %s" (car (rassq n YaTeX-sectioning-level)))
|
yuuji@23
|
174 (message "")))
|
yuuji@23
|
175 (if (numberp selective-display)
|
yuuji@23
|
176 (setq mode-name (format "level %d" (1- selective-display)))
|
yuuji@23
|
177 (setq mode-name (format "all")))
|
yuuji@23
|
178 (select-window cw))
|
yuuji@23
|
179 )
|
yuuji@23
|
180 (defun YaTeX-sectioning-hide ()
|
yuuji@23
|
181 "Call YaTeX-sectioning-hide-under with argument according to pressed key."
|
yuuji@23
|
182 (interactive)
|
yuuji@23
|
183 (YaTeX-sectioning-hide-under (- last-command-char ?0)))
|
yuuji@23
|
184
|
yuuji@23
|
185 (defun YaTeX-sectioning-help ()
|
yuuji@23
|
186 "Show help of sectioning."
|
yuuji@23
|
187 (interactive)
|
yuuji@23
|
188 (let ((cw (selected-window)) sb (hb (get-buffer-create "*Help*")))
|
yuuji@23
|
189 (unwind-protect
|
yuuji@23
|
190 (progn
|
yuuji@23
|
191 (other-window 1)
|
yuuji@23
|
192 (setq sb (current-buffer))
|
yuuji@23
|
193 (switch-to-buffer hb)
|
yuuji@23
|
194 (erase-buffer)
|
yuuji@23
|
195 (insert "===== View sectioning =====
|
yuuji@23
|
196 C-p Up sectioning level. 0 Show only \\part,
|
yuuji@23
|
197 C-n Down sectioning level. 1 and \\chapter,
|
yuuji@23
|
198 C-v Scroll up *Sectioning line* buffer. 2 and \\section,
|
yuuji@23
|
199 M-v Scroll down *Sectioning line* buffer. 3 and \\subsection,
|
yuuji@49
|
200 C-z Scroll up by 1 line. 4 and \\subsubsection,
|
yuuji@49
|
201 C-w Scroll down by 1 line. 5 and \\paragraph.
|
yuuji@49
|
202 SPC Complete word. 6 Show all.
|
yuuji@49
|
203 TAB Complete word.
|
yuuji@49
|
204 C-l Recenter recent line.
|
yuuji@23
|
205 RET Select.
|
yuuji@23
|
206 ==== End of HELP =====
|
yuuji@23
|
207 ")
|
yuuji@23
|
208 (set-buffer-modified-p nil)
|
yuuji@23
|
209 (goto-char (point-min))
|
yuuji@23
|
210 (momentary-string-display "" (point-min)))
|
yuuji@23
|
211 (bury-buffer hb)
|
yuuji@23
|
212 (switch-to-buffer sb)
|
yuuji@23
|
213 (select-window cw)))
|
yuuji@23
|
214 )
|
yuuji@23
|
215
|
yuuji@23
|
216 (defun YaTeX-sectioning-up (n)
|
yuuji@23
|
217 "Up section level.
|
yuuji@23
|
218 Refers the YaTeX-read-section-in-minibuffer's local variable minibuffer-start."
|
yuuji@23
|
219 (interactive "p")
|
yuuji@23
|
220 (if (eq (selected-window) (minibuffer-window))
|
yuuji@72
|
221 (let*((command (YaTeX-minibuffer-string))
|
yuuji@53
|
222 (aster (and (string< "" command)
|
yuuji@53
|
223 (equal (substring command -1) "*")))
|
yuuji@49
|
224 (command (if aster (substring command 0 -1) command))
|
yuuji@23
|
225 (alist YaTeX-sectioning-level)
|
yuuji@49
|
226 (level 0))
|
yuuji@49
|
227 (or (assoc command alist) (error "No such sectioning command."))
|
yuuji@49
|
228 (while (not (string= (car (nth level alist)) command))
|
yuuji@49
|
229 (setq level (1+ level))) ;I want to use `member'....
|
yuuji@23
|
230 (setq level (- level n))
|
yuuji@23
|
231 (if (or (< level 0) (>= level (length alist)))
|
yuuji@23
|
232 (ding)
|
yuuji@72
|
233 (YaTeX-minibuffer-erase)
|
yuuji@49
|
234 (insert (concat (car (nth level alist)) (if aster "*" ""))))))
|
yuuji@23
|
235 )
|
yuuji@23
|
236
|
yuuji@23
|
237 (defun YaTeX-sectioning-down (n)
|
yuuji@23
|
238 "Down section level."
|
yuuji@23
|
239 (interactive "p")
|
yuuji@23
|
240 (YaTeX-sectioning-up (- n))
|
yuuji@23
|
241 )
|
yuuji@23
|
242
|
yuuji@23
|
243 (defun YaTeX-sectioning-scroll-up (n)
|
yuuji@23
|
244 (interactive "P")
|
yuuji@23
|
245 (let ((section-buffer YaTeX-sectioning-buffer)
|
yuuji@23
|
246 (cw (selected-window)))
|
yuuji@23
|
247 (YaTeX-showup-buffer section-buffer nil t)
|
yuuji@23
|
248 (unwind-protect
|
yuuji@23
|
249 (scroll-up (or n (- (window-height) 2)))
|
yuuji@23
|
250 (select-window cw)))
|
yuuji@23
|
251 )
|
yuuji@23
|
252
|
yuuji@23
|
253 (defun YaTeX-sectioning-scroll-down (n)
|
yuuji@23
|
254 (interactive "P")
|
yuuji@23
|
255 (let ((section-buffer YaTeX-sectioning-buffer)
|
yuuji@23
|
256 (cw (selected-window)))
|
yuuji@23
|
257 (YaTeX-showup-buffer section-buffer nil t)
|
yuuji@23
|
258 (unwind-protect
|
yuuji@23
|
259 (scroll-down (or n (- (window-height) 2)))
|
yuuji@23
|
260 (select-window cw)))
|
yuuji@23
|
261 )
|
yuuji@23
|
262
|
yuuji@23
|
263 (defun YaTeX-sectioning-recenter (arg)
|
yuuji@23
|
264 "Recenter `<<--' line"
|
yuuji@23
|
265 (interactive "P")
|
yuuji@23
|
266 (let ((cw (selected-window)))
|
yuuji@23
|
267 (unwind-protect
|
yuuji@23
|
268 (progn
|
yuuji@23
|
269 (YaTeX-showup-buffer YaTeX-sectioning-buffer nil t)
|
yuuji@23
|
270 (or (search-forward "<<--" nil t)
|
yuuji@49
|
271 (search-backward "<<--" nil))
|
yuuji@23
|
272 (recenter (or arg (/ (window-height) 2))))
|
yuuji@23
|
273 (select-window cw)))
|
yuuji@23
|
274 )
|
yuuji@23
|
275
|
yuuji@23
|
276 (defvar YaTeX-sectioning-minibuffer " *sectioning*"
|
yuuji@23
|
277 "Miniuffer used for sectioning")
|
yuuji@49
|
278 ;;;###autoload
|
yuuji@23
|
279 (defun YaTeX-read-section-in-minibuffer (prompt table &optional default delim)
|
yuuji@23
|
280 (interactive)
|
yuuji@23
|
281 (let ((minibuffer-completion-table table))
|
yuuji@23
|
282 (read-from-minibuffer
|
yuuji@49
|
283 prompt default YaTeX-sectioning-minibuffer-map))
|
yuuji@23
|
284 )
|
yuuji@23
|
285
|
yuuji@23
|
286 (defun YaTeX-get-sectioning-level ()
|
yuuji@23
|
287 "Get section-level on the cursor."
|
yuuji@23
|
288 (cdr-safe (assoc (buffer-substring
|
yuuji@23
|
289 (point)
|
yuuji@23
|
290 (progn (skip-chars-forward "a-z") (point)))
|
yuuji@23
|
291 YaTeX-sectioning-level))
|
yuuji@23
|
292 )
|
yuuji@23
|
293
|
yuuji@23
|
294 (defvar YaTeX-sectioning-buffer "*Sectioning lines*")
|
yuuji@64
|
295 (defvar YaTeX-sectioning-indent 1)
|
yuuji@72
|
296 (defun YaTeX-collect-sections ()
|
yuuji@23
|
297 "Collect all the lines which contains sectioning command."
|
yuuji@23
|
298 (let ((cw (selected-window)) level indent begp (prevp 1) (prevl 1)
|
yuuji@23
|
299 (pattern (concat YaTeX-ec-regexp
|
yuuji@23
|
300 "\\(" YaTeX-sectioning-regexp "\\)\\*?{"))
|
yuuji@23
|
301 (cb (current-buffer)))
|
yuuji@23
|
302 (save-excursion
|
yuuji@58
|
303 (set-buffer (get-buffer-create YaTeX-sectioning-buffer))
|
yuuji@58
|
304 (setq buffer-read-only nil)
|
yuuji@70
|
305 (erase-buffer)
|
yuuji@58
|
306 (set-buffer cb)
|
yuuji@23
|
307 (YaTeX-showup-buffer YaTeX-sectioning-buffer) ;show buffer
|
yuuji@23
|
308 (goto-char (point-min))
|
yuuji@68
|
309 (let ((standard-output (get-buffer YaTeX-sectioning-buffer)))
|
yuuji@23
|
310 (while (re-search-forward pattern nil t)
|
yuuji@23
|
311 (goto-char (1+ (match-beginning 0)))
|
yuuji@23
|
312 (setq level (YaTeX-get-sectioning-level)
|
yuuji@23
|
313 begp (match-beginning 0))
|
yuuji@23
|
314 ;;(beginning-of-line)
|
yuuji@23
|
315 ;;(skip-chars-forward " \t")
|
yuuji@64
|
316 (setq indent (format "%%%ds" (* level YaTeX-sectioning-indent)))
|
yuuji@23
|
317 (princ (format indent ""))
|
yuuji@23
|
318 (if (YaTeX-on-comment-p) (princ "%"))
|
yuuji@23
|
319 (princ (buffer-substring begp (progn (forward-list 1) (point))))
|
yuuji@23
|
320 (setq prevl (+ prevl (count-lines prevp (point)) -1)
|
yuuji@23
|
321 prevp (point))
|
yuuji@23
|
322 (princ (format " (line:%d)" prevl))
|
yuuji@23
|
323 (princ "\n")))
|
yuuji@23
|
324 (set-buffer YaTeX-sectioning-buffer)
|
yuuji@23
|
325 (make-local-variable 'YaTeX-sectioning-buffer-parent)
|
yuuji@58
|
326 (YaTeX-sectioning-mode)
|
yuuji@23
|
327 (use-local-map YaTeX-sectioning-buffer-map)
|
yuuji@23
|
328 (setq YaTeX-sectioning-buffer-parent cb)
|
yuuji@23
|
329 (if (numberp selective-display)
|
yuuji@23
|
330 (setq mode-name (format "level %d" (1- selective-display))))
|
yuuji@23
|
331 YaTeX-sectioning-buffer))
|
yuuji@23
|
332 )
|
yuuji@23
|
333
|
yuuji@58
|
334 (defvar YaTeX-pending-undo nil)
|
yuuji@23
|
335 (defun YaTeX-section-overview ()
|
yuuji@23
|
336 "Show section overview. Return the nearest sectioning command."
|
yuuji@23
|
337 (interactive)
|
yuuji@23
|
338 (let ((cw (selected-window)) (ln (count-lines (point-min) (point)))
|
yuuji@23
|
339 (pattern "(line:\\([0-9]+\\))")
|
yuuji@58
|
340 secbuf (command ""))
|
yuuji@23
|
341 (save-excursion
|
yuuji@72
|
342 (setq secbuf (YaTeX-collect-sections))
|
yuuji@23
|
343 (YaTeX-showup-buffer secbuf nil t)
|
yuuji@58
|
344 (set-buffer secbuf)
|
yuuji@23
|
345 (goto-char (point-max))
|
yuuji@23
|
346 (while (re-search-backward pattern nil t)
|
yuuji@23
|
347 (if (< ln (string-to-int (YaTeX-match-string 1))) nil
|
yuuji@23
|
348 (beginning-of-line)
|
yuuji@23
|
349 (search-forward YaTeX-ec)
|
yuuji@23
|
350 (looking-at YaTeX-TeX-token-regexp)
|
yuuji@23
|
351 (setq command (YaTeX-match-string 0))
|
yuuji@23
|
352 (end-of-line)
|
yuuji@23
|
353 (insert " <<--")
|
yuuji@23
|
354 (setq pattern (concat "HackyRegexp" "ForFailure"))))
|
yuuji@23
|
355 (set-buffer-modified-p nil)
|
yuuji@58
|
356 (setq buffer-read-only t buffer-undo-list nil)
|
yuuji@58
|
357 (make-local-variable 'YaTeX-pending-undo)
|
yuuji@23
|
358 (forward-line 1)
|
yuuji@23
|
359 (if (eobp) (recenter -1) (recenter -3))
|
yuuji@23
|
360 (select-window cw)
|
yuuji@23
|
361 command))
|
yuuji@23
|
362 )
|
yuuji@23
|
363
|
yuuji@49
|
364 ;;;###autoload
|
yuuji@23
|
365 (defun YaTeX-make-section-with-overview ()
|
yuuji@23
|
366 "Input sectining command with previous overview."
|
yuuji@23
|
367 (interactive)
|
yuuji@23
|
368 (insert
|
yuuji@23
|
369 YaTeX-ec
|
yuuji@23
|
370 (YaTeX-read-section-in-minibuffer
|
yuuji@23
|
371 "Sectioning(Up=C-p, Down=C-n, Help=?): "
|
yuuji@23
|
372 YaTeX-sectioning-level (YaTeX-section-overview))
|
yuuji@23
|
373 "{}")
|
yuuji@23
|
374 (forward-char -1)
|
yuuji@23
|
375 )
|
yuuji@23
|
376
|
yuuji@58
|
377 (defun YaTeX-shifted-section (sc n)
|
yuuji@58
|
378 "Get SC's N-shifted sectioning command."
|
yuuji@58
|
379 (let (lv)
|
yuuji@58
|
380 (setq lv (- (cdr (assoc sc YaTeX-sectioning-level)) n)
|
yuuji@58
|
381 lv (max (min YaTeX-sectioning-max-level lv) 0))
|
yuuji@58
|
382 (car (nth lv YaTeX-sectioning-level)))
|
yuuji@58
|
383 )
|
yuuji@58
|
384
|
yuuji@58
|
385 (defun YaTeX-shift-section-up (n)
|
yuuji@58
|
386 "Shift sectioning command down by level N."
|
yuuji@58
|
387 (interactive "p")
|
yuuji@58
|
388 (let ((cb (current-buffer)) sc nsc lv)
|
yuuji@58
|
389 (if (and YaTeX-sectioning-buffer-parent
|
yuuji@58
|
390 (get-buffer YaTeX-sectioning-buffer-parent)
|
yuuji@58
|
391 (save-excursion
|
yuuji@68
|
392 (beginning-of-line)
|
yuuji@68
|
393 (skip-chars-forward "^\\\\" (point-end-of-line))
|
yuuji@58
|
394 (YaTeX-on-section-command-p YaTeX-sectioning-regexp)))
|
yuuji@58
|
395 (save-excursion
|
yuuji@58
|
396 (or (buffer-name (get-buffer YaTeX-sectioning-buffer-parent))
|
yuuji@58
|
397 (error "This buffer is obsolete."))
|
yuuji@58
|
398 (setq nsc (YaTeX-shifted-section (YaTeX-match-string 1) n))
|
yuuji@58
|
399 (YaTeX-sectioning-buffer-jump-internal)
|
yuuji@58
|
400 (undo-boundary)
|
yuuji@58
|
401 (goto-char (match-beginning 0))
|
yuuji@58
|
402 (skip-chars-forward "\\\\")
|
yuuji@58
|
403 (delete-region
|
yuuji@58
|
404 (point) (progn (skip-chars-forward "^*{") (point)))
|
yuuji@58
|
405 (insert nsc)
|
yuuji@58
|
406 (undo-boundary)
|
yuuji@58
|
407 ;; Return to *Sectioning Lines* buffer
|
yuuji@58
|
408 (select-window (get-buffer-window cb))
|
yuuji@58
|
409 (beginning-of-line)
|
yuuji@58
|
410 (let (buffer-read-only)
|
yuuji@58
|
411 (delete-region
|
yuuji@58
|
412 (point) (progn (skip-chars-forward " \t") (point)))
|
yuuji@64
|
413 (indent-to-column (* (cdr (assoc nsc YaTeX-sectioning-level))
|
yuuji@64
|
414 YaTeX-sectioning-indent))
|
yuuji@58
|
415 (skip-chars-forward "^\\\\")
|
yuuji@58
|
416 (delete-region
|
yuuji@58
|
417 (1+ (point)) (progn (skip-chars-forward "^*{") (point)))
|
yuuji@58
|
418 (insert nsc)
|
yuuji@58
|
419 (undo-boundary))
|
yuuji@58
|
420 (set-buffer-modified-p nil)
|
yuuji@58
|
421 (setq YaTeX-pending-undo pending-undo-list)
|
yuuji@58
|
422 )))
|
yuuji@58
|
423 )
|
yuuji@58
|
424 (defun YaTeX-shift-section-down (n)
|
yuuji@58
|
425 "Shift sectioning command down by level N."
|
yuuji@58
|
426 (interactive "p")
|
yuuji@58
|
427 (YaTeX-shift-section-up (- n))
|
yuuji@58
|
428 )
|
yuuji@58
|
429 (defun YaTeX-shift-section-undo (arg)
|
yuuji@58
|
430 "Undo YaTeX-shift-section-up/down."
|
yuuji@58
|
431 (interactive "p")
|
yuuji@58
|
432 (and YaTeX-sectioning-buffer-parent
|
yuuji@58
|
433 (get-buffer YaTeX-sectioning-buffer-parent)
|
yuuji@58
|
434 (equal (current-buffer) (get-buffer YaTeX-sectioning-buffer))
|
yuuji@58
|
435 (let ((cb (current-buffer))
|
yuuji@58
|
436 (lc (if (eq last-command 'YaTeX-shift-section-undo) 'undo t)))
|
yuuji@58
|
437 (let ((pending-undo-list YaTeX-pending-undo)
|
yuuji@58
|
438 buffer-read-only (last-command lc))
|
yuuji@58
|
439 (undo arg)
|
yuuji@58
|
440 (setq YaTeX-pending-undo pending-undo-list))
|
yuuji@58
|
441 (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent)
|
yuuji@58
|
442 (goto-buffer-window YaTeX-sectioning-buffer-parent)
|
yuuji@58
|
443 (undo-boundary)
|
yuuji@58
|
444 (let ((last-command lc)
|
yuuji@58
|
445 (pending-undo-list
|
yuuji@58
|
446 (if (eq lc 'undo) YaTeX-pending-undo pending-undo-list)))
|
yuuji@58
|
447 (undo arg)
|
yuuji@58
|
448 (setq YaTeX-pending-undo pending-undo-list))
|
yuuji@58
|
449 (goto-buffer-window cb)
|
yuuji@58
|
450 (setq this-command 'YaTeX-shift-section-undo)))
|
yuuji@58
|
451 )
|
yuuji@58
|
452 (defun YaTeX-sync-section-buffer ()
|
yuuji@58
|
453 "Synchronize *Sectioning Lines* buffer with parent buffer."
|
yuuji@58
|
454 (interactive)
|
yuuji@58
|
455 (if (and YaTeX-sectioning-buffer-parent
|
yuuji@58
|
456 (get-buffer YaTeX-sectioning-buffer-parent))
|
yuuji@58
|
457 (let ((cb (current-buffer)) (p (point)))
|
yuuji@58
|
458 (set-buffer (get-buffer YaTeX-sectioning-buffer-parent))
|
yuuji@58
|
459 (YaTeX-section-overview)
|
yuuji@58
|
460 (switch-to-buffer cb)
|
yuuji@58
|
461 (goto-char p)))
|
yuuji@58
|
462 )
|
yuuji@58
|
463 (defun YaTeX-shift-section-up-region (beg end n)
|
yuuji@58
|
464 "Shift sectioning commands in region down by level N."
|
yuuji@58
|
465 (interactive "r\np")
|
yuuji@58
|
466 (or YaTeX-sectioning-buffer-parent
|
yuuji@58
|
467 (get-buffer YaTeX-sectioning-buffer-parent)
|
yuuji@58
|
468 (error "Can't find corresponding LaTeX buffer"))
|
yuuji@58
|
469 (save-excursion
|
yuuji@58
|
470 (goto-char beg)
|
yuuji@58
|
471 (let ((cb (current-buffer)) nsc from to repllist (e (make-marker)))
|
yuuji@58
|
472 (set-marker e end)
|
yuuji@58
|
473 (while (progn (skip-chars-forward "^\\\\") (< (point) e))
|
yuuji@58
|
474 (YaTeX-on-section-command-p YaTeX-sectioning-regexp)
|
yuuji@58
|
475 (setq from (YaTeX-match-string 0)
|
yuuji@58
|
476 nsc (YaTeX-shifted-section (YaTeX-match-string 1) n))
|
yuuji@58
|
477 (goto-char (match-beginning 0))
|
yuuji@58
|
478 (let (buffer-read-only)
|
yuuji@68
|
479 ;(delete-region (point) (progn (beginning-of-line) (point)))
|
yuuji@68
|
480 (delete-region (progn (beginning-of-line) (point))
|
yuuji@68
|
481 (progn (skip-chars-forward " \t") (point)))
|
yuuji@58
|
482 (indent-to-column (cdr (assoc nsc YaTeX-sectioning-level)))
|
yuuji@58
|
483 (delete-region
|
yuuji@68
|
484 (progn (skip-chars-forward "%\\\\") (point))
|
yuuji@68
|
485 (progn (skip-chars-forward "^*{") (point)))
|
yuuji@58
|
486 (insert nsc))
|
yuuji@58
|
487 (YaTeX-on-section-command-p YaTeX-sectioning-regexp)
|
yuuji@58
|
488 (setq to (YaTeX-match-string 0)
|
yuuji@58
|
489 repllist (cons (cons from to) repllist))
|
yuuji@58
|
490 (forward-line 1))
|
yuuji@58
|
491 (YaTeX-showup-buffer YaTeX-sectioning-buffer-parent)
|
yuuji@58
|
492 (goto-buffer-window YaTeX-sectioning-buffer-parent)
|
yuuji@58
|
493 (save-excursion
|
yuuji@58
|
494 (goto-char (point-max))
|
yuuji@58
|
495 (undo-boundary)
|
yuuji@58
|
496 (while repllist
|
yuuji@58
|
497 (if (search-backward (car (car repllist)) nil t)
|
yuuji@58
|
498 (progn
|
yuuji@58
|
499 (goto-char (match-beginning 0)) ;confirm
|
yuuji@58
|
500 (delete-region (point) (match-end 0))
|
yuuji@58
|
501 (insert (cdr (car repllist)))
|
yuuji@58
|
502 (goto-char (match-beginning 0))))
|
yuuji@58
|
503 (setq repllist (cdr repllist))))
|
yuuji@58
|
504 (goto-buffer-window cb)))
|
yuuji@58
|
505 )
|
yuuji@58
|
506 (defun YaTeX-shift-section-down-region (beg end n)
|
yuuji@58
|
507 "Shift sectioning commands in region down by level N."
|
yuuji@58
|
508 (interactive "r\np")
|
yuuji@58
|
509 (YaTeX-shift-section-up-region beg end (- n))
|
yuuji@58
|
510 )
|
yuuji@58
|
511 (defun YaTeX-sectioning-buffer-next-line (n)
|
yuuji@58
|
512 "Move to next line in *Sectioning Lines* buffer."
|
yuuji@58
|
513 (interactive "p")
|
yuuji@58
|
514 (forward-line n)
|
yuuji@58
|
515 (skip-chars-forward " \t%")
|
yuuji@58
|
516 )
|
yuuji@58
|
517 (defun YaTeX-sectioning-buffer-prev-line (n)
|
yuuji@58
|
518 "Move to previous line in *Sectioning Lines* buffer."
|
yuuji@58
|
519 (interactive "p")
|
yuuji@58
|
520 (YaTeX-sectioning-buffer-next-line (- n))
|
yuuji@58
|
521 )
|
yuuji@23
|
522 (provide 'yatexsec)
|