yatex

view yatexenv.el @ 60:9e08ed569d80

yahtml: change keystroke of calling browser to [prefix] t p yahtml: don't count <a>tag contents as column in <pre> yahtml: support [prefix] c against src="", align="". and many many more
author yuuji
date Fri, 24 Jan 1997 09:04:06 +0000
parents 48ac97a6b6ce
children b9f753846b6b
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX environment-specific functions.
3 ;;; yatexenv.el
4 ;;; (c ) 1994-1997 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
5 ;;; Last modified Fri Jan 24 18:00:38 1997 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 (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 )
192 ;;;
193 ;; Intelligent newline
194 ;;;
195 ;;;###autoload
196 (defun YaTeX-intelligent-newline (arg)
197 "Insert newline and environment-specific entry.
198 `\\item' for some itemizing environment,
199 `\\> \\> \\' for tabbing environemnt,
200 `& & \\ \hline' for tabular environment."
201 (interactive "P")
202 (let*((env (YaTeX-inner-environment))
203 func)
204 (if arg (setq env (YaTeX-read-environment "For what environment? ")))
205 (setq func (intern-soft (concat "YaTeX-intelligent-newline-" env)))
206 (end-of-line)
207 (newline)
208 (undo-boundary)
209 (if (and env func (fboundp func))
210 (funcall func)))
211 )
213 ;;;
214 ;; Environment-specific line indenting functions
215 ;;;
216 ;;;###autoload
217 (defun YaTeX-indent-line-equation ()
218 "Indent a line in equation family."
219 (let ((p (point)) (l-r 0) right-p peol depth (mp YaTeX-environment-indent))
220 (if (save-excursion
221 (beginning-of-line)
222 (skip-chars-forward " \t")
223 (looking-at "\\\\right\\b"))
224 (progn (YaTeX-reindent
225 (save-excursion (YaTeX-goto-corresponding-leftright)
226 (current-column))))
227 (save-excursion
228 (forward-line -1)
229 (while (and (not (bobp)) (YaTeX-on-comment-p))
230 (forward-line -1))
231 ;;(beginning-of-line) ;must be unnecessary
232 (skip-chars-forward " \t")
233 (if (eolp) (error "Math-environment can't have a null line!!"))
234 (setq depth (current-column)
235 peol (point-end-of-line))
236 (while (re-search-forward
237 "\\\\\\(\\(left\\)\\|\\(right\\)\\)\\b" peol t)
238 (setq l-r (+ l-r (if (match-beginning 2) 1 -1))))
239 (cond
240 ((progn (beginning-of-line)
241 (re-search-forward "\\\\\\\\\\s *$" (point-end-of-line) t))
242 ;;If previous line has `\\', this indentation is always normal.
243 (setq depth (+ (YaTeX-current-indentation) mp)))
244 ((> l-r 0)
245 (beginning-of-line)
246 (search-forward "\\left" peol)
247 (goto-char (1+ (match-beginning 0)))
248 (setq depth (current-column)))
249 ((< l-r 0)
250 (goto-char (match-beginning 0)) ;should be \right
251 (YaTeX-goto-corresponding-leftright)
252 (beginning-of-line)
253 (skip-chars-forward " \t")
254 (setq depth (+ (current-column) mp))) ;+mp is good?
255 (t ;if \left - \right = 0
256 (cond
257 ((re-search-forward "\\\\\\\\\\s *$" peol t)
258 (setq depth (+ (YaTeX-current-indentation) mp)))
259 ((re-search-forward "\\\\end{" peol t)
260 nil) ;same indentation as previous line's
261 ((re-search-forward "\\\\begin{" peol t)
262 (setq depth (+ depth mp)))
263 (t
264 (or (bobp) (forward-line -1))
265 (cond
266 ((re-search-forward
267 "\\\\\\\\\\s *$\\|\\\\begin{" (point-end-of-line) t)
268 (setq depth (+ depth mp)))
269 )))))
270 (goto-char p))
271 (YaTeX-reindent depth))))
273 ;;;###autoload
274 (defun YaTeX-goto-corresponding-leftright ()
275 "Go to corresponding \left or \right.
276 Note that this function assumes the corresponding \left\right
277 is on another line."
278 (let ((YaTeX-struct-begin "\\left%1")
279 (YaTeX-struct-end "\\right%1")
280 (YaTeX-struct-name-regexp "[][(){}.|]"))
281 (YaTeX-goto-corresponding-environment t)))
283 ;;;
284 ;; Functions for formatting region being enclosed with environment
285 ;;;
286 ; These functions must take two argument; region-beginning, region-end.
288 (defun YaTeX-enclose-equation (beg end)
289 (goto-char beg)
290 (save-restriction
291 (let (m0 bsl)
292 (narrow-to-region beg end)
293 (while (YaTeX-re-search-active-forward
294 "\\(\\$\\)" YaTeX-comment-prefix nil t)
295 (goto-char (setq m0 (match-beginning 0)))
296 (setq bsl 0)
297 (if (and (not (bobp)) (= (char-after (1- (point))) ?\\ ))
298 (while (progn (forward-char -1) (= (char-after (point)) ?\\ ))
299 (setq bsl (1+ bsl))))
300 (goto-char m0)
301 (if (= 0 (% bsl 2))
302 (delete-char 1)
303 (forward-char 1))))))
305 (fset 'YaTeX-enclose-eqnarray 'YaTeX-enclose-equation)
306 (fset 'YaTeX-enclose-eqnarray* 'YaTeX-enclose-equation)
308 (defun YaTeX-enclose-verbatim (beg end)) ;do nothing when enclose verbatim
309 (fset 'YaTeX-enclose-verbatim* 'YaTeX-enclose-verbatim)
311 (provide 'yatexenv)