yatex

view yatexenv.el @ 56:a9653fbd1c1c

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