yatex

view yatexenv.el @ 79:0734be649cb8

Do not care file-coding-system when YaTeX-kanji-code is nil. New completion yatexpkg.el is introduced.
author yuuji
date Thu, 25 Dec 2003 04:10:32 +0000
parents 1b172d26b55e
children 9b4354af748c
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX environment-specific functions.
3 ;;; yatexenv.el
4 ;;; (c) 1994-2003 by HIROSE Yuuji.[yuuji@yatex.org]
5 ;;; Last modified Fri Jun 27 12:09:30 2003 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 ()
14 "Show matching columne 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 (message
58 "This is the column(#%d) of: %s" n
59 (buffer-substring
60 (point)
61 (progn
62 (re-search-forward (concat andptn "\\|" nlptn) eot)
63 (goto-char (match-beginning 0))
64 (if (looking-at andptn)
65 (forward-char 1))
66 (skip-chars-backward "\\s ")
67 (point))))))
68 )
70 ;;;###autoload
71 (defun YaTeX-what-column ()
72 "Show which kind of column the current position is belonging to."
73 (interactive)
74 (cond
75 ((YaTeX-quick-in-environment-p '("tabular" "tabular*" "array" "array*"))
76 (YaTeX-array-what-column))
77 (t (message "Not in array/tabular environment.")))
78 )
80 (defun YaTeX-tabular-parse-format (&optional tabular*)
81 "Parse `tabular' format.
82 Return the list of (No.ofCols PointEndofFormat)"
83 (let ((p (point)) elt boform eoform (cols 0))
84 (save-excursion
85 (if (null (YaTeX-beginning-of-environment t))
86 (error "Beginning of tabular not found."))
87 (skip-chars-forward "^{")
88 (forward-list 1)
89 (if tabular*
90 (progn (skip-chars-forward "^{")
91 (forward-list 1)))
92 (skip-chars-forward "^{" p)
93 (if (/= (following-char) ?\{) (error "Tabular format not found."))
94 (setq boform (1+ (point))
95 eoform (progn (forward-list 1) (1- (point))))
96 (if (> eoform p) (error "Non-terminated tabular format."))
97 (goto-char boform)
98 (while (< (point) eoform)
99 (setq elt (following-char))
100 (cond
101 ((string-match (char-to-string elt) "clr") ;normal indicators.
102 (setq cols (1+ cols))
103 (forward-char 1))
104 ((equal elt ?|) ;vertical
105 (forward-char 1))
106 ((string-match (char-to-string elt) "p@") ;p or @ expression
107 (setq cols (+ (if (eq elt ?p) 1 0) cols))
108 (skip-chars-forward "^{" p)
109 (forward-list 1))
110 (t (forward-char 1)) ;unknown char
111 ))
112 (list cols (1+ eoform))))
113 )
114 ;; Insert &
115 (defun YaTeX-intelligent-newline-tabular (&optional tabular*)
116 "Parse current tabular format and insert that many `&'s."
117 (let*((p (point)) (format (YaTeX-tabular-parse-format tabular*))
118 (cols (car format)) (beg (car (cdr format)))
119 space hline)
120 (cond
121 ((search-backward "&" beg t)
122 (goto-char p)
123 (setq hline (search-backward "\\hline" beg t))
124 (setq space (if (search-backward "\t&" beg t) "\t" " "))
125 (goto-char p))
126 (t ;;(insert "\\hline\n")
127 (setq space " ")))
128 (goto-char p)
129 (while (> (1- cols) 0)
130 (insert "&" space)
131 (setq cols (1- cols)))
132 (insert "\\\\")
133 (if hline (insert " \\hline"))
134 (goto-char p)
135 (YaTeX-indent-line)
136 )
137 )
139 (defun YaTeX-intelligent-newline-tabular* ()
140 "Parse current tabular* format and insert that many `&'s."
141 (YaTeX-intelligent-newline-tabular t)
142 )
144 (fset 'YaTeX-intelligent-newline-array 'YaTeX-intelligent-newline-tabular)
146 ;;;
147 ;; Functions for tabbing environment
148 ;;;
149 (defun YaTeX-intelligent-newline-tabbing ()
150 "Check the number of \\= in the first line and insert that many \\>."
151 (let ((p (point)) begenv tabcount)
152 (save-excursion
153 (YaTeX-beginning-of-environment)
154 (setq begenv (point-end-of-line))
155 (if (YaTeX-search-active-forward "\\\\" YaTeX-comment-prefix p t)
156 (progn
157 (setq tabcount 0)
158 (while (> (point) begenv)
159 (if (search-backward "\\=" begenv 1)
160 (setq tabcount (1+ tabcount)))))))
161 (YaTeX-indent-line)
162 (if tabcount
163 (progn
164 (save-excursion
165 (while (> tabcount 0)
166 (insert "\\>\t")
167 (setq tabcount (1- tabcount))))
168 (forward-char 2))
169 (insert "\\= \\\\")
170 (forward-char -5)))
171 )
173 ;;;
174 ;; Functions for itemize/enumerate/list environments
175 ;;;
177 (defun YaTeX-intelligent-newline-itemize ()
178 "Insert '\\item '."
179 (insert "\\item ")
180 (YaTeX-indent-line)
181 )
182 (fset 'YaTeX-intelligent-newline-enumerate 'YaTeX-intelligent-newline-itemize)
183 (fset 'YaTeX-intelligent-newline-list 'YaTeX-intelligent-newline-itemize)
185 (defun YaTeX-intelligent-newline-description ()
186 (insert "\\item[] ")
187 (forward-char -2)
188 (YaTeX-indent-line)
189 )
191 (defun YaTeX-intelligent-newline-thebibliography ()
192 "Insert '\\bibitem '."
193 (YaTeX-indent-line)
194 (YaTeX-make-section nil nil nil "bibitem")
195 (YaTeX-indent-line)
196 )
198 ;;;
199 ;; Intelligent newline
200 ;;;
201 ;;;###autoload
202 (defun YaTeX-intelligent-newline (arg)
203 "Insert newline and environment-specific entry.
204 `\\item' for some itemizing environment,
205 `\\> \\> \\' for tabbing environemnt,
206 `& & \\ \hline' for tabular environment."
207 (interactive "P")
208 (let*(env func)
209 (end-of-line)
210 (setq env (YaTeX-inner-environment))
211 (if arg (setq env (YaTeX-read-environment "For what environment? ")))
212 (setq func (intern-soft (concat "YaTeX-intelligent-newline-" env)))
213 (end-of-line)
214 (newline)
215 (undo-boundary)
216 (if (and env func (fboundp func))
217 (funcall func)))
218 )
220 ;;;
221 ;; Environment-specific line indenting functions
222 ;;;
223 ;;;###autoload
224 (defun YaTeX-indent-line-equation ()
225 "Indent a line in equation family."
226 (let ((p (point)) (l-r 0) right-p peol depth (mp YaTeX-environment-indent))
227 (if (save-excursion
228 (beginning-of-line)
229 (skip-chars-forward " \t")
230 (looking-at "\\\\right\\b"))
231 (progn (YaTeX-reindent
232 (save-excursion (YaTeX-goto-corresponding-leftright)
233 (- (current-column) 0))))
234 (save-excursion
235 (forward-line -1)
236 (while (and (not (bobp)) (YaTeX-on-comment-p))
237 (forward-line -1))
238 ;;(beginning-of-line) ;must be unnecessary
239 (skip-chars-forward " \t")
240 (if (eolp) (error "Math-environment can't have a null line!!"))
241 (setq depth (current-column)
242 peol (point-end-of-line))
243 (while (re-search-forward
244 "\\\\\\(\\(left\\)\\|\\(right\\)\\)\\b" peol t)
245 (setq l-r (+ l-r (if (match-beginning 2) 1 -1))))
246 (cond
247 ((progn (beginning-of-line)
248 (re-search-forward "\\\\\\\\\\s *$" (point-end-of-line) t))
249 ;;If previous line has `\\', this indentation is always normal.
250 (setq depth (+ (YaTeX-current-indentation) mp)))
251 ((> l-r 0)
252 (beginning-of-line)
253 (search-forward "\\left" peol nil l-r)
254 (goto-char (1+ (match-beginning 0)))
255 (setq depth (current-column)))
256 ((< l-r 0)
257 (goto-char (match-beginning 0)) ;should be \right
258 (YaTeX-goto-corresponding-leftright)
259 (beginning-of-line)
260 (skip-chars-forward " \t")
261 ;(setq depth (+ (current-column) mp)) ;+mp is good?
262 (setq depth (current-column)))
263 (t ;if \left - \right = 0
264 (cond
265 ((re-search-forward "\\\\\\\\\\s *$" peol t)
266 (setq depth (+ (YaTeX-current-indentation) mp)))
267 ((re-search-forward "\\\\end{" peol t)
268 nil) ;same indentation as previous line's
269 ((re-search-forward "\\\\begin{" peol t)
270 (setq depth (+ depth mp)))
271 (t
272 (or (bobp) (forward-line -1))
273 (cond
274 ((re-search-forward
275 "\\\\\\\\\\s *$\\|\\\\begin{" (point-end-of-line) t)
276 (setq depth (+ depth mp)))
277 )))))
278 (goto-char p))
279 (YaTeX-reindent depth))))
281 ;;;###autoload
282 (defun YaTeX-goto-corresponding-leftright ()
283 "Go to corresponding \left or \right."
284 (let ((YaTeX-struct-begin "\\left%1")
285 (YaTeX-struct-end "\\right%1")
286 (YaTeX-struct-name-regexp "[][(){}\\.|]")
287 (in-leftright-p t))
288 (YaTeX-goto-corresponding-environment t)))
290 ;;;
291 ;; Functions for formatting region being enclosed with environment
292 ;;;
293 ; These functions must take two argument; region-beginning, region-end.
295 (defun YaTeX-enclose-equation (beg end)
296 (goto-char beg)
297 (save-restriction
298 (let (m0 bsl)
299 (narrow-to-region beg end)
300 (while (YaTeX-re-search-active-forward
301 "\\(\\$\\)" YaTeX-comment-prefix nil t)
302 (goto-char (setq m0 (match-beginning 0)))
303 (setq bsl 0)
304 (if (and (not (bobp)) (= (char-after (1- (point))) ?\\ ))
305 (while (progn (forward-char -1) (= (char-after (point)) ?\\ ))
306 (setq bsl (1+ bsl))))
307 (goto-char m0)
308 (if (= 0 (% bsl 2))
309 (delete-char 1)
310 (forward-char 1))))))
312 (fset 'YaTeX-enclose-eqnarray 'YaTeX-enclose-equation)
313 (fset 'YaTeX-enclose-eqnarray* 'YaTeX-enclose-equation)
315 (defun YaTeX-enclose-verbatim (beg end)) ;do nothing when enclose verbatim
316 (fset 'YaTeX-enclose-verbatim* 'YaTeX-enclose-verbatim)
318 (provide 'yatexenv)