yatex

view yatexenv.el @ 58:3a7c0c2bf16d

Official support for AMS-LaTeX, HTML, xdvi -remote, Netscape
author yuuji
date Thu, 01 Feb 1996 18:55:47 +0000
parents a9653fbd1c1c
children 48ac97a6b6ce
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 Fri Feb 2 03:47:14 1996 on supra
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 (cond
120 ((search-backward "&" beg t)
121 (goto-char p)
122 (setq hline (search-backward "\\hline" beg t))
123 (setq space (if (search-backward "\t&" beg t) "\t" " "))
124 (goto-char p))
125 (t ;;(insert "\\hline\n")
126 (setq space " ")))
127 (goto-char p)
128 (while (> (1- cols) 0)
129 (insert "&" space)
130 (setq cols (1- cols)))
131 (insert "\\\\")
132 (if hline (insert " \\hline"))
133 (goto-char p)
134 (YaTeX-indent-line)
135 )
136 )
138 (defun YaTeX-intelligent-newline-tabular* ()
139 "Parse current tabular* format and insert that many `&'s."
140 (YaTeX-intelligent-newline-tabular t)
141 )
143 (fset 'YaTeX-intelligent-newline-array 'YaTeX-intelligent-newline-tabular)
145 ;;;
146 ;; Functions for tabbing environment
147 ;;;
148 (defun YaTeX-intelligent-newline-tabbing ()
149 "Check the number of \\= in the first line and insert that many \\>."
150 (let ((p (point)) begenv tabcount)
151 (save-excursion
152 (YaTeX-beginning-of-environment)
153 (setq begenv (point-end-of-line))
154 (if (YaTeX-search-active-forward "\\\\" YaTeX-comment-prefix p t)
155 (progn
156 (setq tabcount 0)
157 (while (> (point) begenv)
158 (if (search-backward "\\=" begenv 1)
159 (setq tabcount (1+ tabcount)))))))
160 (YaTeX-indent-line)
161 (if tabcount
162 (progn
163 (save-excursion
164 (while (> tabcount 0)
165 (insert "\\>\t")
166 (setq tabcount (1- tabcount))))
167 (forward-char 2))
168 (insert "\\= \\\\")
169 (forward-char -5)))
170 )
172 ;;;
173 ;; Functions for itemize/enumerate/list environments
174 ;;;
176 (defun YaTeX-intelligent-newline-itemize ()
177 "Insert '\\item '."
178 (insert "\\item ")
179 (YaTeX-indent-line)
180 )
181 (fset 'YaTeX-intelligent-newline-enumerate 'YaTeX-intelligent-newline-itemize)
182 (fset 'YaTeX-intelligent-newline-list 'YaTeX-intelligent-newline-itemize)
184 (defun YaTeX-intelligent-newline-description ()
185 (insert "\\item[] ")
186 (forward-char -2)
187 (YaTeX-indent-line)
188 )
191 ;;;
192 ;; Intelligent newline
193 ;;;
194 ;;;###autoload
195 (defun YaTeX-intelligent-newline (arg)
196 "Insert newline and environment-specific entry.
197 `\\item' for some itemizing environment,
198 `\\> \\> \\' for tabbing environemnt,
199 `& & \\ \hline' for tabular environment."
200 (interactive "P")
201 (let*((env (YaTeX-inner-environment))
202 func)
203 (if arg (setq env (YaTeX-read-environment "For what environment? ")))
204 (setq func (intern-soft (concat "YaTeX-intelligent-newline-" env)))
205 (end-of-line)
206 (newline)
207 (undo-boundary)
208 (if (and env func (fboundp func))
209 (funcall func)))
210 )
212 ;;;
213 ;; Environment-specific line indenting functions
214 ;;;
215 ;;;###autoload
216 (defun YaTeX-indent-line-equation ()
217 "Indent a line in equation family."
218 (let ((p (point)) (l-r 0) right-p peol depth (mp YaTeX-environment-indent))
219 (if (save-excursion
220 (beginning-of-line)
221 (skip-chars-forward " \t")
222 (looking-at "\\\\right\\b"))
223 (progn (YaTeX-reindent
224 (save-excursion (YaTeX-goto-corresponding-leftright)
225 (current-column))))
226 (save-excursion
227 (forward-line -1)
228 (while (and (not (bobp)) (YaTeX-on-comment-p))
229 (forward-line -1))
230 ;;(beginning-of-line) ;must be unnecessary
231 (skip-chars-forward " \t")
232 (if (eolp) (error "Math-environment can't have a null line!!"))
233 (setq depth (current-column)
234 peol (point-end-of-line))
235 (while (re-search-forward
236 "\\\\\\(\\(left\\)\\|\\(right\\)\\)\\b" peol t)
237 (setq l-r (+ l-r (if (match-beginning 2) 1 -1))))
238 (cond
239 ((progn (beginning-of-line)
240 (re-search-forward "\\\\\\\\\\s *$" (point-end-of-line) t))
241 ;;If previous line has `\\', this indentation is always normal.
242 (setq depth (+ (YaTeX-current-indentation) mp)))
243 ((> l-r 0)
244 (beginning-of-line)
245 (search-forward "\\left" peol)
246 (goto-char (1+ (match-beginning 0)))
247 (setq depth (current-column)))
248 ((< l-r 0)
249 (goto-char (match-beginning 0)) ;should be \right
250 (YaTeX-goto-corresponding-leftright)
251 (beginning-of-line)
252 (skip-chars-forward " \t")
253 (setq depth (+ (current-column) mp))) ;+mp is good?
254 (t ;if \left - \right = 0
255 (cond
256 ((re-search-forward "\\\\\\\\\\s *$" peol t)
257 (setq depth (+ (YaTeX-current-indentation) mp)))
258 ((re-search-forward "\\\\end{" peol t)
259 nil) ;same indentation as previous line's
260 ((re-search-forward "\\\\begin{" peol t)
261 (setq depth (+ depth mp)))
262 (t
263 (or (bobp) (forward-line -1))
264 (cond
265 ((re-search-forward
266 "\\\\\\\\\\s *$\\|\\\\begin{" (point-end-of-line) t)
267 (setq depth (+ depth mp)))
268 )))))
269 (goto-char p))
270 (YaTeX-reindent depth))))
272 ;;;###autoload
273 (defun YaTeX-goto-corresponding-leftright ()
274 "Go to corresponding \left or \right.
275 Note that this function assumes the corresponding \left\right
276 is on another line."
277 (let ((YaTeX-struct-begin "\\left%1")
278 (YaTeX-struct-end "\\right%1")
279 (YaTeX-struct-name-regexp "[][(){}.|]"))
280 (YaTeX-goto-corresponding-environment t)))
282 ;;;
283 ;; Functions for formatting region being enclosed with environment
284 ;;;
285 ; These functions must take two argument; region-beginning, region-end.
287 (defun YaTeX-enclose-equation (beg end)
288 (goto-char beg)
289 (save-restriction
290 (let (m0 bsl)
291 (narrow-to-region beg end)
292 (while (YaTeX-re-search-active-forward
293 "\\(\\$\\)" YaTeX-comment-prefix nil t)
294 (goto-char (setq m0 (match-beginning 0)))
295 (setq bsl 0)
296 (if (and (not (bobp)) (= (char-after (1- (point))) ?\\ ))
297 (while (progn (forward-char -1) (= (char-after (point)) ?\\ ))
298 (setq bsl (1+ bsl))))
299 (goto-char m0)
300 (if (= 0 (% bsl 2))
301 (delete-char 1)
302 (forward-char 1))))))
304 (fset 'YaTeX-enclose-eqnarray 'YaTeX-enclose-equation)
305 (fset 'YaTeX-enclose-eqnarray* 'YaTeX-enclose-equation)
307 (defun YaTeX-enclose-verbatim (beg end)) ;do nothing when enclose verbatim
308 (fset 'YaTeX-enclose-verbatim* 'YaTeX-enclose-verbatim)
310 (provide 'yatexenv)