23
|
1 ;;; -*- Emacs-Lisp -*-
|
|
2 ;;; YaTeX environment-specific functions.
|
|
3 ;;; yatexenv.el
|
|
4 ;;; (c ) 1994 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
|
31
|
5 ;;; Last modified Mon Jul 11 02:01:18 1994 on 98fa
|
23
|
6 ;;; $Id$
|
|
7
|
|
8 ;;;
|
|
9 ;; Functions for tabular environment
|
|
10 ;;;
|
|
11
|
|
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 "[^\\]&") (n 0)
|
|
17 (firsterr "This line might be the first row."))
|
|
18 (save-excursion
|
|
19 (YaTeX-beginning-of-environment)
|
|
20 (search-forward "{" p) (up-list 1)
|
|
21 (search-forward "{" p) (up-list 1)
|
|
22 ;;(re-search-forward andptn p)
|
|
23 (while (progn (search-forward "&" p)
|
|
24 (equal (char-after (1- (match-beginning 0))) ?\\ )))
|
|
25 (setq beg (1- (point))) ;beg is the point of the first &
|
|
26 (or (re-search-forward nlptn p t)
|
|
27 (error firsterr))
|
|
28 (setq eot (point)) ;eot is the point of the first \\
|
|
29 (goto-char p)
|
|
30 (or (re-search-backward nlptn beg t)
|
|
31 (error firsterr))
|
|
32 (setq bor (point)) ;bor is the beginning of this row.
|
|
33 (while (< (1- (point)) p)
|
|
34 (if (equal (following-char) ?&)
|
|
35 (forward-char 1)
|
|
36 (re-search-forward andptn nil 1))
|
|
37 (setq n (1+ n))) ;Check current column number
|
|
38 (goto-char p)
|
|
39 (cond ;Start searching \multicolumn{N}
|
|
40 ((> n 1)
|
|
41 (re-search-backward andptn) ;Sure to find!
|
|
42 (while (re-search-backward "\\\\multicolumn{\\([0-9]+\\)}" bor t)
|
|
43 (setq n (+ n (string-to-int
|
|
44 (buffer-substring (match-beginning 1)
|
|
45 (match-end 1)))
|
|
46 -1)))))
|
|
47 (message "%s" n)
|
|
48 (goto-char (1- beg))
|
|
49 (cond
|
|
50 ((= n 1) (message "Here is the FIRST column!"))
|
|
51 (t (while (> n 1)
|
|
52 (or (re-search-forward andptn p nil)
|
|
53 (error "This column exceeds the limit."))
|
|
54 (setq n (1- n)))
|
|
55 (skip-chars-forward "\\s ")
|
|
56 (message
|
|
57 "Here is the column of: %s"
|
|
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 )
|
|
68
|
|
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 )
|
|
78
|
31
|
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 (list cols (1+ eoform))))
|
|
110 )
|
|
111 ;; Insert &
|
|
112 (defun YaTeX-intelligent-newline-tabular (&optional tabular*)
|
|
113 "Parse current tabular format and insert that many `&'s."
|
|
114 (let*((p (point)) (format (YaTeX-tabular-parse-format tabular*))
|
|
115 (cols (car format)) (beg (car (cdr format)))
|
|
116 space hline)
|
|
117 (setq hline (search-backward "\\hline" beg t))
|
|
118 (goto-char p)
|
|
119 (setq space (if (search-backward "\t&" beg t) "\t" " "))
|
|
120 (goto-char p)
|
|
121 (YaTeX-indent-line)
|
|
122 (setq p (point))
|
|
123 (while (> (1- cols) 0)
|
|
124 (insert "&" space)
|
|
125 (setq cols (1- cols)))
|
|
126 (insert "\\\\")
|
|
127 (if hline (insert " \\hline"))
|
|
128 (goto-char p))
|
|
129 )
|
|
130
|
|
131 (defun YaTeX-intelligent-newline-tabular* ()
|
|
132 "Parse current tabular* format and insert that many `&'s."
|
|
133 (YaTeX-intelligent-newline-tabular t)
|
|
134 )
|
|
135
|
|
136 (fset 'YaTeX-intelligent-newline-array 'YaTeX-intelligent-newline-tabular)
|
|
137
|
23
|
138 ;;;
|
|
139 ;; Functions for tabbing environment
|
|
140 ;;;
|
|
141 (defun YaTeX-intelligent-newline-tabbing ()
|
|
142 "Check the number of \\= in the first line and insert that many \\>."
|
|
143 (let ((p (point)) begenv tabcount)
|
|
144 (save-excursion
|
|
145 (YaTeX-beginning-of-environment)
|
|
146 (setq begenv (point-end-of-line))
|
|
147 (if (YaTeX-search-active-forward "\\\\" YaTeX-comment-prefix p t)
|
|
148 (progn
|
|
149 (setq tabcount 0)
|
|
150 (while (> (point) begenv)
|
|
151 (if (search-backward "\\=" begenv 1)
|
|
152 (setq tabcount (1+ tabcount)))))))
|
|
153 (YaTeX-indent-line)
|
|
154 (if tabcount
|
|
155 (progn
|
|
156 (save-excursion
|
|
157 (while (> tabcount 0)
|
|
158 (insert "\\>\t")
|
|
159 (setq tabcount (1- tabcount))))
|
|
160 (forward-char 2))
|
|
161 (insert "\\=")))
|
|
162 )
|
|
163
|
|
164 ;;;
|
|
165 ;; Functions for itemize/enumerate/list environments
|
|
166 ;;;
|
|
167
|
|
168 (defun YaTeX-indent-for-item ()
|
|
169 (let (col (p (point)) begenv)
|
|
170 (save-excursion
|
|
171 (YaTeX-beginning-of-environment t)
|
|
172 (setq begenv (point-end-of-line))
|
|
173 (goto-char p)
|
|
174 (if (YaTeX-search-active-backward "\\item" YaTeX-comment-prefix begenv t)
|
|
175 (setq col (current-column))))
|
|
176 (if col (indent-to col) (YaTeX-indent-line)))
|
|
177 )
|
|
178
|
|
179 (defvar YaTeX-item-for-insert "\\item ")
|
|
180 (defun YaTeX-intelligent-newline-itemize ()
|
|
181 "Insert '\\item '."
|
|
182 (YaTeX-indent-for-item)
|
|
183 (insert YaTeX-item-for-insert)
|
|
184 )
|
|
185 (fset 'YaTeX-intelligent-newline-enumerate 'YaTeX-intelligent-newline-itemize)
|
|
186 (fset 'YaTeX-intelligent-newline-list 'YaTeX-intelligent-newline-itemize)
|
|
187
|
|
188 (defun YaTeX-intelligent-newline-description ()
|
|
189 (YaTeX-indent-for-item)
|
|
190 (insert "\\item[] ")
|
|
191 (forward-char -2)
|
|
192 )
|
|
193
|
|
194
|
|
195 ;;;
|
|
196 ;; Intelligent newline
|
|
197 ;;;
|
|
198 ;;;###autoload
|
|
199 (defun YaTeX-intelligent-newline (arg)
|
|
200 "Insert newline and environment-specific entry.
|
|
201 `\\item' for some itemizing environment,
|
|
202 `\\> \\> \\' for tabbing environemnt,
|
|
203 `& & \\ \hline' for tabular environment."
|
|
204 (interactive "P")
|
|
205 (let*((env (YaTeX-inner-environment))
|
|
206 func)
|
|
207 (if arg (setq env (YaTeX-read-environment "For what environment? ")))
|
|
208 (setq func (intern-soft (concat "YaTeX-intelligent-newline-" env)))
|
|
209 (end-of-line)
|
|
210 (newline)
|
|
211 (if (and env func (fboundp func))
|
|
212 (funcall func)))
|
|
213 )
|