yatex

view yatexenv.el @ 306:9cfc505ba6aa

Utilize M-Ret in `longtable' environment
author HIROSE Yuuji <yuuji@gentei.org>
date Wed, 20 Nov 2013 14:15:42 +0900
parents 8d3156073892
children 2a72779d9c50
line source
1 ;;; yatexenv.el --- YaTeX environment-specific functions
2 ;;; (c) 1994-2013 by HIROSE Yuuji.[yuuji@yatex.org]
3 ;;; Last modified Wed Nov 20 14:14:39 2013 on firestorm
4 ;;; $Id$
6 ;;; Code:
7 ;;;
8 ;; Functions for tabular environment
9 ;;;
11 ;; Showing the matching column of tabular environment.
12 (defun YaTeX-array-what-column-internal ()
13 "Return the cons of matching column and its title of array environment.
14 When calling from a program, make sure to be in array/tabular environment."
15 (let ((p (point)) beg eot bor (nlptn "\\\\\\\\") (andptn "[^\\]&")
16 (n 0) j
17 (firsterr "This line might be the first row."))
18 (save-excursion
19 (YaTeX-beginning-of-environment)
20 (search-forward "{" p) (up-list 1)
21 (search-forward "{" p) (up-list 1)
22 ;;(re-search-forward andptn p)
23 (while (progn (search-forward "&" p)
24 (equal (char-after (1- (match-beginning 0))) ?\\ )))
25 (setq beg (1- (point))) ;beg is the point of the first &
26 (or (re-search-forward nlptn p t)
27 (error firsterr))
28 (setq eot (point)) ;eot is the point of the first \\
29 (goto-char p)
30 (or (re-search-backward nlptn beg t)
31 (error firsterr))
32 (setq bor (point)) ;bor is the beginning of this row.
33 (while (< (1- (point)) p)
34 (if (equal (following-char) ?&)
35 (forward-char 1)
36 (re-search-forward andptn nil 1))
37 (setq n (1+ n))) ;Check current column number
38 (goto-char p)
39 (cond ;Start searching \multicolumn{N}
40 ((> n 1)
41 (re-search-backward andptn) ;Sure to find!
42 (while (re-search-backward "\\\\multicolumn{\\([0-9]+\\)}" bor t)
43 (setq n (+ n (string-to-int
44 (buffer-substring (match-beginning 1)
45 (match-end 1)))
46 -1)))))
47 (message "%s" n)
48 (goto-char (1- beg))
49 (beginning-of-line)
50 (setq j n)
51 (while (> j 1)
52 (or (re-search-forward andptn p nil)
53 (error "This column exceeds the limit."))
54 (setq j (1- j)))
55 (skip-chars-forward "\\s ")
56 (list n
57 (buffer-substring
58 (point)
59 (progn
60 (re-search-forward (concat andptn "\\|" nlptn) eot)
61 (goto-char (match-beginning 0))
62 (if (looking-at andptn)
63 (forward-char 1))
64 (skip-chars-backward "\\s ")
65 (point)))))))
67 (defun YaTeX-array-what-column ()
68 "Show matching column title of array environment.
69 When calling from a program, make sure to be in array/tabular environment."
70 (apply 'message
71 "This is the column(#%d) of: %s"
72 (YaTeX-array-what-column-internal)))
74 ;;;###autoload
75 (defun YaTeX-what-column ()
76 "Show which kind of column the current position is belonging to."
77 (interactive)
78 (cond
79 ((YaTeX-quick-in-environment-p '("tabular" "tabular*" "array" "array*"))
80 (YaTeX-array-what-column))
81 (t (message "Not in array/tabular environment."))))
83 (defun YaTeX-tabular-parse-format-count-cols (beg end)
84 (goto-char beg)
85 (let (elt (cols 0))
86 (while (< (point) end)
87 (setq elt (following-char))
88 (cond
89 ((string-match (char-to-string elt) "clr") ;normal indicators.
90 (setq cols (1+ cols))
91 (forward-char 1))
92 ((equal elt ?|) ;vertical
93 (forward-char 1))
94 ((string-match (char-to-string elt) "p@") ;p or @ expression
95 (setq cols (+ (if (eq elt ?p) 1 0) cols))
96 ;;(skip-chars-forward "^{" p)
97 (skip-chars-forward "^{" end)
98 (forward-list 1))
99 ((equal elt ?*) ;*{N}{EXP} -> Repeat EXP N times
100 (skip-chars-forward "^{" end)
101 (setq cols (* (string-to-int
102 (buffer-substring
103 (1+ (point))
104 (progn (forward-list 1) (1- (point)))))
105 (YaTeX-tabular-parse-format-count-cols
106 (progn (skip-chars-forward "^{" end) (1+ (point)))
107 (progn (forward-list 1) (1- (point)))))))
108 (t (forward-char 1)) ;unknown char
109 ))
110 cols))
112 (defun YaTeX-tabular-parse-format (&optional type)
113 "Parse `tabular' format.
114 Return the list of (No.ofCols PointEndofFormat)"
115 (let ((p (point)) boform eoform (cols 0))
116 (save-excursion
117 (if (null (YaTeX-beginning-of-environment t))
118 (error "Beginning of tabular not found."))
119 (skip-chars-forward "^{")
120 (forward-list 1)
121 (cond
122 ((eq type 'tabular*)
123 (skip-chars-forward "^{")
124 (forward-list 1)))
125 (skip-chars-forward "^{" p)
126 (if (/= (following-char) ?\{) (error "Tabular format not found."))
127 (setq boform (1+ (point))
128 eoform (progn (forward-list 1) (1- (point))))
129 (if (> eoform p) (error "Non-terminated tabular format."))
130 (goto-char boform)
131 (setq cols
132 (cond
133 ((eq type 'alignat)
134 (max
135 1
136 (* 2 (string-to-int
137 (buffer-substring
138 (point)
139 (progn (up-list -1) (forward-list 1) (1- (point))))))))
140 (t
141 (YaTeX-tabular-parse-format-count-cols (point) eoform))))
142 (list cols (1+ eoform)))))
144 ;; Insert &
145 (defun YaTeX-intelligent-newline-tabular (&optional type)
146 "Parse current tabular format and insert that many `&'s."
147 (let*((p (point)) (format (YaTeX-tabular-parse-format type))
148 (cols (car format)) (beg (car (cdr format)))
149 space hline)
150 (cond
151 ((search-backward "&" beg t)
152 (goto-char p)
153 (setq hline (search-backward "\\hline" beg t))
154 (setq space (if (search-backward "\t&" beg t) "\t" " "))
155 (goto-char p))
156 (t ;;(insert "\\hline\n")
157 (setq space " ")))
158 (goto-char p)
159 (while (> (1- cols) 0)
160 (insert "&" space)
161 (setq cols (1- cols)))
162 (insert "\\\\")
163 (if hline (insert " \\hline"))
164 (goto-char p)
165 (YaTeX-indent-line)))
167 (defun YaTeX-intelligent-newline-tabular* ()
168 "Parse current tabular* format and insert that many `&'s."
169 (YaTeX-intelligent-newline-tabular 'tabular*))
171 (fset 'YaTeX-intelligent-newline-array 'YaTeX-intelligent-newline-tabular)
172 (fset 'YaTeX-intelligent-newline-supertabular 'YaTeX-intelligent-newline-tabular)
173 (fset 'YaTeX-intelligent-newline-longtable '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 (< (count-lines
263 (or (get 'YaTeX-inner-environment 'point)
264 (max 1 (- (point) 17))) ;"\begin{document}\n" == 17
265 (point))
266 2)
267 nil
268 (if (save-excursion (re-search-backward "\\\\par\\>" nil t))
269 (progn
270 (YaTeX-indent-line)
271 (insert "\\par")))
272 (newline))
273 (YaTeX-indent-line))
275 ;;;
276 ;; Intelligent newline
277 ;;;
278 ;;;###autoload
279 (defun YaTeX-intelligent-newline (arg)
280 "Insert newline and environment-specific entry.
281 `\\item' for some itemizing environment,
282 `\\> \\> \\' for tabbing environemnt,
283 `& & \\ \hline' for tabular environment."
284 (interactive "P")
285 (let*(env func)
286 (end-of-line)
287 (setq env (YaTeX-inner-environment))
288 (if arg (setq env (YaTeX-read-environment "For what environment? ")))
289 (setq func (intern-soft (concat "YaTeX-intelligent-newline-" env)))
290 (end-of-line)
291 (newline)
292 (undo-boundary)
293 (if (and env func (fboundp func))
294 (funcall func))))
296 ;;;
297 ;; Environment-specific line indenting functions
298 ;;;
299 ;;;###autoload
300 (defun YaTeX-indent-line-equation ()
301 "Indent a line in equation family."
302 (let ((p (point)) (l-r 0) right-p peol depth (mp YaTeX-environment-indent))
303 (if (save-excursion
304 (beginning-of-line)
305 (skip-chars-forward " \t")
306 (looking-at "\\\\right\\b"))
307 (progn (YaTeX-reindent
308 (save-excursion (YaTeX-goto-corresponding-leftright)
309 (- (current-column) 0))))
310 (save-excursion
311 (forward-line -1)
312 (while (and (not (bobp)) (YaTeX-on-comment-p))
313 (forward-line -1))
314 ;;(beginning-of-line) ;must be unnecessary
315 (skip-chars-forward " \t")
316 (if (eolp) (error "Math-environment can't have a null line!!"))
317 (setq depth (current-column)
318 peol (point-end-of-line))
319 (while (re-search-forward
320 "\\\\\\(\\(left\\)\\|\\(right\\)\\)\\b" peol t)
321 (setq l-r (+ l-r (if (match-beginning 2) 1 -1))))
322 (cond
323 ((progn (beginning-of-line)
324 (re-search-forward "\\\\\\\\\\s *$" (point-end-of-line) t))
325 ;;If previous line has `\\', this indentation is always normal.
326 (setq depth (+ (YaTeX-current-indentation) mp)))
327 ((> l-r 0)
328 (beginning-of-line)
329 (search-forward "\\left" peol nil l-r)
330 (goto-char (1+ (match-beginning 0)))
331 (setq depth (current-column)))
332 ((< l-r 0)
333 (goto-char (match-beginning 0)) ;should be \right
334 (YaTeX-goto-corresponding-leftright)
335 (beginning-of-line)
336 (skip-chars-forward " \t")
337 ;(setq depth (+ (current-column) mp)) ;+mp is good?
338 (setq depth (current-column)))
339 (t ;if \left - \right = 0
340 (cond
341 ((re-search-forward "\\\\\\\\\\s *$" peol t)
342 (setq depth (+ (YaTeX-current-indentation) mp)))
343 ((re-search-forward "\\\\end{" peol t)
344 nil) ;same indentation as previous line's
345 ((re-search-forward "\\\\begin{" peol t)
346 (setq depth (+ depth mp)))
347 (t
348 (or (bobp) (forward-line -1))
349 (cond
350 ((re-search-forward
351 "\\\\\\\\\\s *$\\|\\\\begin{" (point-end-of-line) t)
352 (setq depth (+ depth mp)))
353 )))))
354 (goto-char p))
355 (YaTeX-reindent depth))))
357 ;;;###autoload
358 (defun YaTeX-goto-corresponding-leftright ()
359 "Go to corresponding \left or \right."
360 (let ((YaTeX-struct-begin "\\left%1")
361 (YaTeX-struct-end "\\right%1")
362 (YaTeX-struct-name-regexp "[][(){}\\.|]")
363 (in-leftright-p t))
364 (YaTeX-goto-corresponding-environment t)))
366 ;;;
367 ;; Functions for formatting region being enclosed with environment
368 ;;;
369 ; These functions must take two argument; region-beginning, region-end.
371 (defun YaTeX-enclose-equation (beg end)
372 (goto-char beg)
373 (save-restriction
374 (let (m0 bsl)
375 (narrow-to-region beg end)
376 (while (YaTeX-re-search-active-forward
377 "\\(\\$\\)" YaTeX-comment-prefix nil t)
378 (goto-char (setq m0 (match-beginning 0)))
379 (setq bsl 0)
380 (if (and (not (bobp)) (= (char-after (1- (point))) ?\\ ))
381 (while (progn (forward-char -1) (= (char-after (point)) ?\\ ))
382 (setq bsl (1+ bsl))))
383 (goto-char m0)
384 (if (= 0 (% bsl 2))
385 (delete-char 1)
386 (forward-char 1))))))
388 (fset 'YaTeX-enclose-eqnarray 'YaTeX-enclose-equation)
389 (fset 'YaTeX-enclose-eqnarray* 'YaTeX-enclose-equation)
391 (defun YaTeX-enclose-verbatim (beg end)) ;do nothing when enclose verbatim
392 (fset 'YaTeX-enclose-verbatim* 'YaTeX-enclose-verbatim)
394 (provide 'yatexenv)