yatex

view yatexenv.el @ 250:4f73b796ec20

Fix intelligent-newline for alignat(*).
author yuuji@gentei.org
date Sat, 11 Feb 2012 12:08:55 +0900
parents 5ab3322d0f03
children ceca2d094d6f
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX environment-specific functions.
3 ;;; yatexenv.el
4 ;;; (c) 1994-2006 by HIROSE Yuuji.[yuuji@yatex.org]
5 ;;; Last modified Sat Feb 11 10:35:22 2012 on firestorm
6 ;;; $Id$
8 ;;;
9 ;; Functions for tabular environment
10 ;;;
12 ;; Showing the matching column of tabular environment.
13 (defun YaTeX-array-what-column-internal ()
14 "Return the cons of matching column and its title of array environment.
15 When calling from a program, make sure to be in array/tabular environment."
16 (let ((p (point)) beg eot bor (nlptn "\\\\\\\\") (andptn "[^\\]&")
17 (n 0) j
18 (firsterr "This line might be the first row."))
19 (save-excursion
20 (YaTeX-beginning-of-environment)
21 (search-forward "{" p) (up-list 1)
22 (search-forward "{" p) (up-list 1)
23 ;;(re-search-forward andptn p)
24 (while (progn (search-forward "&" p)
25 (equal (char-after (1- (match-beginning 0))) ?\\ )))
26 (setq beg (1- (point))) ;beg is the point of the first &
27 (or (re-search-forward nlptn p t)
28 (error firsterr))
29 (setq eot (point)) ;eot is the point of the first \\
30 (goto-char p)
31 (or (re-search-backward nlptn beg t)
32 (error firsterr))
33 (setq bor (point)) ;bor is the beginning of this row.
34 (while (< (1- (point)) p)
35 (if (equal (following-char) ?&)
36 (forward-char 1)
37 (re-search-forward andptn nil 1))
38 (setq n (1+ n))) ;Check current column number
39 (goto-char p)
40 (cond ;Start searching \multicolumn{N}
41 ((> n 1)
42 (re-search-backward andptn) ;Sure to find!
43 (while (re-search-backward "\\\\multicolumn{\\([0-9]+\\)}" bor t)
44 (setq n (+ n (string-to-int
45 (buffer-substring (match-beginning 1)
46 (match-end 1)))
47 -1)))))
48 (message "%s" n)
49 (goto-char (1- beg))
50 (beginning-of-line)
51 (setq j n)
52 (while (> j 1)
53 (or (re-search-forward andptn p nil)
54 (error "This column exceeds the limit."))
55 (setq j (1- j)))
56 (skip-chars-forward "\\s ")
57 (list n
58 (buffer-substring
59 (point)
60 (progn
61 (re-search-forward (concat andptn "\\|" nlptn) eot)
62 (goto-char (match-beginning 0))
63 (if (looking-at andptn)
64 (forward-char 1))
65 (skip-chars-backward "\\s ")
66 (point)))))))
68 (defun YaTeX-array-what-column ()
69 "Show matching column title of array environment.
70 When calling from a program, make sure to be in array/tabular environment."
71 (apply 'message
72 "This is the column(#%d) of: %s"
73 (YaTeX-array-what-column-internal)))
75 ;;;###autoload
76 (defun YaTeX-what-column ()
77 "Show which kind of column the current position is belonging to."
78 (interactive)
79 (cond
80 ((YaTeX-quick-in-environment-p '("tabular" "tabular*" "array" "array*"))
81 (YaTeX-array-what-column))
82 (t (message "Not in array/tabular environment."))))
84 (defun YaTeX-tabular-parse-format-count-cols (beg end)
85 (goto-char beg)
86 (let (elt (cols 0))
87 (while (< (point) end)
88 (setq elt (following-char))
89 (cond
90 ((string-match (char-to-string elt) "clr") ;normal indicators.
91 (setq cols (1+ cols))
92 (forward-char 1))
93 ((equal elt ?|) ;vertical
94 (forward-char 1))
95 ((string-match (char-to-string elt) "p@") ;p or @ expression
96 (setq cols (+ (if (eq elt ?p) 1 0) cols))
97 ;;(skip-chars-forward "^{" p)
98 (skip-chars-forward "^{" end)
99 (forward-list 1))
100 ((equal elt ?*) ;*{N}{EXP} -> Repeat EXP N times
101 (skip-chars-forward "^{" end)
102 (setq cols (* (string-to-int
103 (buffer-substring
104 (1+ (point))
105 (progn (forward-list 1) (1- (point)))))
106 (YaTeX-tabular-parse-format-count-cols
107 (progn (skip-chars-forward "^{" end) (1+ (point)))
108 (progn (forward-list 1) (1- (point)))))))
109 (t (forward-char 1)) ;unknown char
110 ))
111 cols))
113 (defun YaTeX-tabular-parse-format (&optional type)
114 "Parse `tabular' format.
115 Return the list of (No.ofCols PointEndofFormat)"
116 (let ((p (point)) boform eoform (cols 0))
117 (save-excursion
118 (if (null (YaTeX-beginning-of-environment t))
119 (error "Beginning of tabular not found."))
120 (skip-chars-forward "^{")
121 (forward-list 1)
122 (cond
123 ((eq type 'tabular*)
124 (skip-chars-forward "^{")
125 (forward-list 1)))
126 (skip-chars-forward "^{" p)
127 (if (/= (following-char) ?\{) (error "Tabular format not found."))
128 (setq boform (1+ (point))
129 eoform (progn (forward-list 1) (1- (point))))
130 (if (> eoform p) (error "Non-terminated tabular format."))
131 (goto-char boform)
132 (setq cols
133 (cond
134 ((eq type 'alignat)
135 (max
136 1
137 (* 2 (string-to-int
138 (buffer-substring
139 (point)
140 (progn (up-list -1) (forward-list 1) (1- (point))))))))
141 (t
142 (YaTeX-tabular-parse-format-count-cols (point) eoform))))
143 (list cols (1+ eoform)))))
145 ;; Insert &
146 (defun YaTeX-intelligent-newline-tabular (&optional type)
147 "Parse current tabular format and insert that many `&'s."
148 (let*((p (point)) (format (YaTeX-tabular-parse-format type))
149 (cols (car format)) (beg (car (cdr format)))
150 space hline)
151 (cond
152 ((search-backward "&" beg t)
153 (goto-char p)
154 (setq hline (search-backward "\\hline" beg t))
155 (setq space (if (search-backward "\t&" beg t) "\t" " "))
156 (goto-char p))
157 (t ;;(insert "\\hline\n")
158 (setq space " ")))
159 (goto-char p)
160 (while (> (1- cols) 0)
161 (insert "&" space)
162 (setq cols (1- cols)))
163 (insert "\\\\")
164 (if hline (insert " \\hline"))
165 (goto-char p)
166 (YaTeX-indent-line)))
168 (defun YaTeX-intelligent-newline-tabular* ()
169 "Parse current tabular* format and insert that many `&'s."
170 (YaTeX-intelligent-newline-tabular 'tabular*))
172 (fset 'YaTeX-intelligent-newline-array 'YaTeX-intelligent-newline-tabular)
173 (fset 'YaTeX-intelligent-newline-supertabular 'YaTeX-intelligent-newline-tabular)
175 (defun YaTeX-intelligent-newline-align ()
176 "Intelligent newline function for align.
177 Count the number of & in the first align line and insert that many &s."
178 (let*((p (point)) (amps 0))
179 (if (string-match "alignat" env)
180 (setq amps (1- (car (YaTeX-tabular-parse-format 'alignat))))
181 (save-excursion
182 (YaTeX-beginning-of-environment)
183 (catch 'done
184 (while (YaTeX-re-search-active-forward
185 "\\(&\\)\\|\\(\\\\\\\\\\)" YaTeX-comment-prefix p t)
186 (if (match-beginning 1) (setq amps (1+ amps)) (throw 'done t))))))
187 (save-excursion
188 (forward-line -1)
189 (skip-chars-forward " \t")
190 (or (prog1 (looking-at "\\\\begin{") (end-of-line))
191 (save-excursion
192 (skip-chars-backward " \t")
193 (and (= (preceding-char) ?\\) (= (char-after (- (point) 2)) ?\\)))
194 (insert "\\\\")))
195 (save-excursion
196 (while (>= (setq amps (1- amps)) 0)
197 (insert "& ")))
198 (YaTeX-indent-line)))
200 (mapcar
201 '(lambda (s)
202 (fset (intern (concat "YaTeX-intelligent-newline-"
203 (symbol-name s)))
204 'YaTeX-intelligent-newline-align))
205 '(align* flalign flalign* matrix pmatrix bmatrix Bmatrix vmatrix Vmatrix
206 cases eqnarray eqnarray* alignat alignat*))
208 ;;;
209 ;; Functions for tabbing environment
210 ;;;
211 (defun YaTeX-intelligent-newline-tabbing ()
212 "Check the number of \\= in the first line and insert that many \\>."
213 (let ((p (point)) begenv tabcount)
214 (save-excursion
215 (YaTeX-beginning-of-environment)
216 (setq begenv (point-end-of-line))
217 (if (YaTeX-search-active-forward "\\\\" YaTeX-comment-prefix p t)
218 (progn
219 (setq tabcount 0)
220 (while (> (point) begenv)
221 (if (search-backward "\\=" begenv 1)
222 (setq tabcount (1+ tabcount)))))))
223 (YaTeX-indent-line)
224 (if tabcount
225 (progn
226 (save-excursion
227 (while (> tabcount 0)
228 (insert "\\>\t")
229 (setq tabcount (1- tabcount))))
230 (forward-char 2))
231 (insert "\\= \\\\")
232 (forward-char -5))))
234 ;;;
235 ;; Functions for itemize/enumerate/list environments
236 ;;;
238 (defun YaTeX-intelligent-newline-itemize ()
239 "Insert '\\item '."
240 (insert "\\item ")
241 (YaTeX-indent-line))
243 (fset 'YaTeX-intelligent-newline-enumerate 'YaTeX-intelligent-newline-itemize)
244 (fset 'YaTeX-intelligent-newline-list 'YaTeX-intelligent-newline-itemize)
246 (defun YaTeX-intelligent-newline-description ()
247 (insert "\\item[] ")
248 (forward-char -2)
249 (YaTeX-indent-line))
251 (defun YaTeX-intelligent-newline-thebibliography ()
252 "Insert '\\bibitem '."
253 (YaTeX-indent-line)
254 (YaTeX-make-section nil nil nil "bibitem")
255 (YaTeX-indent-line))
257 ;;;
258 ;; For document environment
259 ;;;
260 (defun YaTeX-intelligent-newline-document ()
261 "New paragraph by null line or `\\par'."
262 (if (save-excursion (re-search-backward "\\\\par\\>" nil t))
263 (progn
264 (YaTeX-indent-line)
265 (insert "\\par")))
266 (newline)
267 (YaTeX-indent-line))
269 ;;;
270 ;; Intelligent newline
271 ;;;
272 ;;;###autoload
273 (defun YaTeX-intelligent-newline (arg)
274 "Insert newline and environment-specific entry.
275 `\\item' for some itemizing environment,
276 `\\> \\> \\' for tabbing environemnt,
277 `& & \\ \hline' for tabular environment."
278 (interactive "P")
279 (let*(env func)
280 (end-of-line)
281 (setq env (YaTeX-inner-environment))
282 (if arg (setq env (YaTeX-read-environment "For what environment? ")))
283 (setq func (intern-soft (concat "YaTeX-intelligent-newline-" env)))
284 (end-of-line)
285 (newline)
286 (undo-boundary)
287 (if (and env func (fboundp func))
288 (funcall func))))
290 ;;;
291 ;; Environment-specific line indenting functions
292 ;;;
293 ;;;###autoload
294 (defun YaTeX-indent-line-equation ()
295 "Indent a line in equation family."
296 (let ((p (point)) (l-r 0) right-p peol depth (mp YaTeX-environment-indent))
297 (if (save-excursion
298 (beginning-of-line)
299 (skip-chars-forward " \t")
300 (looking-at "\\\\right\\b"))
301 (progn (YaTeX-reindent
302 (save-excursion (YaTeX-goto-corresponding-leftright)
303 (- (current-column) 0))))
304 (save-excursion
305 (forward-line -1)
306 (while (and (not (bobp)) (YaTeX-on-comment-p))
307 (forward-line -1))
308 ;;(beginning-of-line) ;must be unnecessary
309 (skip-chars-forward " \t")
310 (if (eolp) (error "Math-environment can't have a null line!!"))
311 (setq depth (current-column)
312 peol (point-end-of-line))
313 (while (re-search-forward
314 "\\\\\\(\\(left\\)\\|\\(right\\)\\)\\b" peol t)
315 (setq l-r (+ l-r (if (match-beginning 2) 1 -1))))
316 (cond
317 ((progn (beginning-of-line)
318 (re-search-forward "\\\\\\\\\\s *$" (point-end-of-line) t))
319 ;;If previous line has `\\', this indentation is always normal.
320 (setq depth (+ (YaTeX-current-indentation) mp)))
321 ((> l-r 0)
322 (beginning-of-line)
323 (search-forward "\\left" peol nil l-r)
324 (goto-char (1+ (match-beginning 0)))
325 (setq depth (current-column)))
326 ((< l-r 0)
327 (goto-char (match-beginning 0)) ;should be \right
328 (YaTeX-goto-corresponding-leftright)
329 (beginning-of-line)
330 (skip-chars-forward " \t")
331 ;(setq depth (+ (current-column) mp)) ;+mp is good?
332 (setq depth (current-column)))
333 (t ;if \left - \right = 0
334 (cond
335 ((re-search-forward "\\\\\\\\\\s *$" peol t)
336 (setq depth (+ (YaTeX-current-indentation) mp)))
337 ((re-search-forward "\\\\end{" peol t)
338 nil) ;same indentation as previous line's
339 ((re-search-forward "\\\\begin{" peol t)
340 (setq depth (+ depth mp)))
341 (t
342 (or (bobp) (forward-line -1))
343 (cond
344 ((re-search-forward
345 "\\\\\\\\\\s *$\\|\\\\begin{" (point-end-of-line) t)
346 (setq depth (+ depth mp)))
347 )))))
348 (goto-char p))
349 (YaTeX-reindent depth))))
351 ;;;###autoload
352 (defun YaTeX-goto-corresponding-leftright ()
353 "Go to corresponding \left or \right."
354 (let ((YaTeX-struct-begin "\\left%1")
355 (YaTeX-struct-end "\\right%1")
356 (YaTeX-struct-name-regexp "[][(){}\\.|]")
357 (in-leftright-p t))
358 (YaTeX-goto-corresponding-environment t)))
360 ;;;
361 ;; Functions for formatting region being enclosed with environment
362 ;;;
363 ; These functions must take two argument; region-beginning, region-end.
365 (defun YaTeX-enclose-equation (beg end)
366 (goto-char beg)
367 (save-restriction
368 (let (m0 bsl)
369 (narrow-to-region beg end)
370 (while (YaTeX-re-search-active-forward
371 "\\(\\$\\)" YaTeX-comment-prefix nil t)
372 (goto-char (setq m0 (match-beginning 0)))
373 (setq bsl 0)
374 (if (and (not (bobp)) (= (char-after (1- (point))) ?\\ ))
375 (while (progn (forward-char -1) (= (char-after (point)) ?\\ ))
376 (setq bsl (1+ bsl))))
377 (goto-char m0)
378 (if (= 0 (% bsl 2))
379 (delete-char 1)
380 (forward-char 1))))))
382 (fset 'YaTeX-enclose-eqnarray 'YaTeX-enclose-equation)
383 (fset 'YaTeX-enclose-eqnarray* 'YaTeX-enclose-equation)
385 (defun YaTeX-enclose-verbatim (beg end)) ;do nothing when enclose verbatim
386 (fset 'YaTeX-enclose-verbatim* 'YaTeX-enclose-verbatim)
388 (provide 'yatexenv)