yatex

view yatexenv.el @ 562:43508ed8bcc8

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