yatex

view yatexenv.el @ 353:2a72779d9c50

Rewrite lambda notations to suppress annoying warnings from emacs24
author HIROSE Yuuji <yuuji@gentei.org>
date Sun, 21 Dec 2014 14:20:01 +0900
parents 9cfc505ba6aa
children dbef6cf84f98
line source
1 ;;; yatexenv.el --- YaTeX environment-specific functions
2 ;;; (c) 1994-2013 by HIROSE Yuuji.[yuuji@yatex.org]
3 ;;; Last modified Sun Dec 21 13:58:31 2014 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 (function
202 (lambda (s)
203 (fset (intern (concat "YaTeX-intelligent-newline-"
204 (symbol-name s)))
205 'YaTeX-intelligent-newline-align)))
206 '(align* flalign flalign* matrix pmatrix bmatrix Bmatrix vmatrix Vmatrix
207 cases eqnarray eqnarray* alignat alignat*))
209 ;;;
210 ;; Functions for tabbing environment
211 ;;;
212 (defun YaTeX-intelligent-newline-tabbing ()
213 "Check the number of \\= in the first line and insert that many \\>."
214 (let ((p (point)) begenv tabcount)
215 (save-excursion
216 (YaTeX-beginning-of-environment)
217 (setq begenv (point-end-of-line))
218 (if (YaTeX-search-active-forward "\\\\" YaTeX-comment-prefix p t)
219 (progn
220 (setq tabcount 0)
221 (while (> (point) begenv)
222 (if (search-backward "\\=" begenv 1)
223 (setq tabcount (1+ tabcount)))))))
224 (YaTeX-indent-line)
225 (if tabcount
226 (progn
227 (save-excursion
228 (while (> tabcount 0)
229 (insert "\\>\t")
230 (setq tabcount (1- tabcount))))
231 (forward-char 2))
232 (insert "\\= \\\\")
233 (forward-char -5))))
235 ;;;
236 ;; Functions for itemize/enumerate/list environments
237 ;;;
239 (defun YaTeX-intelligent-newline-itemize ()
240 "Insert '\\item '."
241 (insert "\\item ")
242 (YaTeX-indent-line))
244 (fset 'YaTeX-intelligent-newline-enumerate 'YaTeX-intelligent-newline-itemize)
245 (fset 'YaTeX-intelligent-newline-list 'YaTeX-intelligent-newline-itemize)
247 (defun YaTeX-intelligent-newline-description ()
248 (insert "\\item[] ")
249 (forward-char -2)
250 (YaTeX-indent-line))
252 (defun YaTeX-intelligent-newline-thebibliography ()
253 "Insert '\\bibitem '."
254 (YaTeX-indent-line)
255 (YaTeX-make-section nil nil nil "bibitem")
256 (YaTeX-indent-line))
258 ;;;
259 ;; For document environment
260 ;;;
261 (defun YaTeX-intelligent-newline-document ()
262 "New paragraph by null line or `\\par'."
263 (if (< (count-lines
264 (or (get 'YaTeX-inner-environment 'point)
265 (max 1 (- (point) 17))) ;"\begin{document}\n" == 17
266 (point))
267 2)
268 nil
269 (if (save-excursion (re-search-backward "\\\\par\\>" nil t))
270 (progn
271 (YaTeX-indent-line)
272 (insert "\\par")))
273 (newline))
274 (YaTeX-indent-line))
276 ;;;
277 ;; Intelligent newline
278 ;;;
279 ;;;###autoload
280 (defun YaTeX-intelligent-newline (arg)
281 "Insert newline and environment-specific entry.
282 `\\item' for some itemizing environment,
283 `\\> \\> \\' for tabbing environemnt,
284 `& & \\ \hline' for tabular environment."
285 (interactive "P")
286 (let*(env func)
287 (end-of-line)
288 (setq env (YaTeX-inner-environment))
289 (if arg (setq env (YaTeX-read-environment "For what environment? ")))
290 (setq func (intern-soft (concat "YaTeX-intelligent-newline-" env)))
291 (end-of-line)
292 (newline)
293 (undo-boundary)
294 (if (and env func (fboundp func))
295 (funcall func))))
297 ;;;
298 ;; Environment-specific line indenting functions
299 ;;;
300 ;;;###autoload
301 (defun YaTeX-indent-line-equation ()
302 "Indent a line in equation family."
303 (let ((p (point)) (l-r 0) right-p peol depth (mp YaTeX-environment-indent))
304 (if (save-excursion
305 (beginning-of-line)
306 (skip-chars-forward " \t")
307 (looking-at "\\\\right\\b"))
308 (progn (YaTeX-reindent
309 (save-excursion (YaTeX-goto-corresponding-leftright)
310 (- (current-column) 0))))
311 (save-excursion
312 (forward-line -1)
313 (while (and (not (bobp)) (YaTeX-on-comment-p))
314 (forward-line -1))
315 ;;(beginning-of-line) ;must be unnecessary
316 (skip-chars-forward " \t")
317 (if (eolp) (error "Math-environment can't have a null line!!"))
318 (setq depth (current-column)
319 peol (point-end-of-line))
320 (while (re-search-forward
321 "\\\\\\(\\(left\\)\\|\\(right\\)\\)\\b" peol t)
322 (setq l-r (+ l-r (if (match-beginning 2) 1 -1))))
323 (cond
324 ((progn (beginning-of-line)
325 (re-search-forward "\\\\\\\\\\s *$" (point-end-of-line) t))
326 ;;If previous line has `\\', this indentation is always normal.
327 (setq depth (+ (YaTeX-current-indentation) mp)))
328 ((> l-r 0)
329 (beginning-of-line)
330 (search-forward "\\left" peol nil l-r)
331 (goto-char (1+ (match-beginning 0)))
332 (setq depth (current-column)))
333 ((< l-r 0)
334 (goto-char (match-beginning 0)) ;should be \right
335 (YaTeX-goto-corresponding-leftright)
336 (beginning-of-line)
337 (skip-chars-forward " \t")
338 ;(setq depth (+ (current-column) mp)) ;+mp is good?
339 (setq depth (current-column)))
340 (t ;if \left - \right = 0
341 (cond
342 ((re-search-forward "\\\\\\\\\\s *$" peol t)
343 (setq depth (+ (YaTeX-current-indentation) mp)))
344 ((re-search-forward "\\\\end{" peol t)
345 nil) ;same indentation as previous line's
346 ((re-search-forward "\\\\begin{" peol t)
347 (setq depth (+ depth mp)))
348 (t
349 (or (bobp) (forward-line -1))
350 (cond
351 ((re-search-forward
352 "\\\\\\\\\\s *$\\|\\\\begin{" (point-end-of-line) t)
353 (setq depth (+ depth mp)))
354 )))))
355 (goto-char p))
356 (YaTeX-reindent depth))))
358 ;;;###autoload
359 (defun YaTeX-goto-corresponding-leftright ()
360 "Go to corresponding \left or \right."
361 (let ((YaTeX-struct-begin "\\left%1")
362 (YaTeX-struct-end "\\right%1")
363 (YaTeX-struct-name-regexp "[][(){}\\.|]")
364 (in-leftright-p t))
365 (YaTeX-goto-corresponding-environment t)))
367 ;;;
368 ;; Functions for formatting region being enclosed with environment
369 ;;;
370 ; These functions must take two argument; region-beginning, region-end.
372 (defun YaTeX-enclose-equation (beg end)
373 (goto-char beg)
374 (save-restriction
375 (let (m0 bsl)
376 (narrow-to-region beg end)
377 (while (YaTeX-re-search-active-forward
378 "\\(\\$\\)" YaTeX-comment-prefix nil t)
379 (goto-char (setq m0 (match-beginning 0)))
380 (setq bsl 0)
381 (if (and (not (bobp)) (= (char-after (1- (point))) ?\\ ))
382 (while (progn (forward-char -1) (= (char-after (point)) ?\\ ))
383 (setq bsl (1+ bsl))))
384 (goto-char m0)
385 (if (= 0 (% bsl 2))
386 (delete-char 1)
387 (forward-char 1))))))
389 (fset 'YaTeX-enclose-eqnarray 'YaTeX-enclose-equation)
390 (fset 'YaTeX-enclose-eqnarray* 'YaTeX-enclose-equation)
392 (defun YaTeX-enclose-verbatim (beg end)) ;do nothing when enclose verbatim
393 (fset 'YaTeX-enclose-verbatim* 'YaTeX-enclose-verbatim)
395 (provide 'yatexenv)