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