yuuji@23: ;;; -*- Emacs-Lisp -*- yuuji@23: ;;; YaTeX environment-specific functions. yuuji@23: ;;; yatexenv.el yuuji@23: ;;; (c ) 1994 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp] yuuji@49: ;;; Last modified Thu Nov 24 04:33:18 1994 on 98fa yuuji@23: ;;; $Id$ yuuji@23: yuuji@23: ;;; yuuji@23: ;; Functions for tabular environment yuuji@23: ;;; yuuji@23: yuuji@23: ;; Showing the matching column of tabular environment. yuuji@23: (defun YaTeX-array-what-column () yuuji@23: "Show matching columne title of array environment. yuuji@23: When calling from a program, make sure to be in array/tabular environment." yuuji@49: (let ((p (point)) beg eot bor (nlptn "\\\\\\\\") (andptn "[^\\]&") yuuji@49: (n 0) j yuuji@23: (firsterr "This line might be the first row.")) yuuji@23: (save-excursion yuuji@23: (YaTeX-beginning-of-environment) yuuji@23: (search-forward "{" p) (up-list 1) yuuji@23: (search-forward "{" p) (up-list 1) yuuji@23: ;;(re-search-forward andptn p) yuuji@23: (while (progn (search-forward "&" p) yuuji@23: (equal (char-after (1- (match-beginning 0))) ?\\ ))) yuuji@23: (setq beg (1- (point))) ;beg is the point of the first & yuuji@23: (or (re-search-forward nlptn p t) yuuji@23: (error firsterr)) yuuji@23: (setq eot (point)) ;eot is the point of the first \\ yuuji@23: (goto-char p) yuuji@23: (or (re-search-backward nlptn beg t) yuuji@23: (error firsterr)) yuuji@23: (setq bor (point)) ;bor is the beginning of this row. yuuji@23: (while (< (1- (point)) p) yuuji@23: (if (equal (following-char) ?&) yuuji@23: (forward-char 1) yuuji@23: (re-search-forward andptn nil 1)) yuuji@23: (setq n (1+ n))) ;Check current column number yuuji@23: (goto-char p) yuuji@23: (cond ;Start searching \multicolumn{N} yuuji@23: ((> n 1) yuuji@23: (re-search-backward andptn) ;Sure to find! yuuji@23: (while (re-search-backward "\\\\multicolumn{\\([0-9]+\\)}" bor t) yuuji@23: (setq n (+ n (string-to-int yuuji@23: (buffer-substring (match-beginning 1) yuuji@23: (match-end 1))) yuuji@23: -1))))) yuuji@23: (message "%s" n) yuuji@23: (goto-char (1- beg)) yuuji@49: (setq j n) yuuji@49: (while (> j 1) yuuji@49: (or (re-search-forward andptn p nil) yuuji@49: (error "This column exceeds the limit.")) yuuji@49: (setq j (1- j))) yuuji@49: (skip-chars-forward "\\s ") yuuji@49: (message yuuji@49: "This is the column(#%d) of: %s" n yuuji@49: (buffer-substring yuuji@49: (point) yuuji@49: (progn yuuji@49: (re-search-forward (concat andptn "\\|" nlptn) eot) yuuji@49: (goto-char (match-beginning 0)) yuuji@49: (if (looking-at andptn) yuuji@49: (forward-char 1)) yuuji@49: (skip-chars-backward "\\s ") yuuji@49: (point)))))) yuuji@23: ) yuuji@23: yuuji@23: ;;;###autoload yuuji@23: (defun YaTeX-what-column () yuuji@23: "Show which kind of column the current position is belonging to." yuuji@23: (interactive) yuuji@23: (cond yuuji@23: ((YaTeX-quick-in-environment-p '("tabular" "tabular*" "array" "array*")) yuuji@23: (YaTeX-array-what-column)) yuuji@23: (t (message "Not in array/tabuar environment."))) yuuji@23: ) yuuji@23: yuuji@31: (defun YaTeX-tabular-parse-format (&optional tabular*) yuuji@31: "Parse `tabular' format. yuuji@31: Return the list of (No.ofCols PointEndofFormat)" yuuji@31: (let ((p (point)) elt boform eoform (cols 0)) yuuji@31: (save-excursion yuuji@31: (if (null (YaTeX-beginning-of-environment t)) yuuji@31: (error "Beginning of tabular not found.")) yuuji@31: (skip-chars-forward "^{") yuuji@31: (forward-list 1) yuuji@31: (if tabular* yuuji@31: (progn (skip-chars-forward "^{") yuuji@31: (forward-list 1))) yuuji@31: (skip-chars-forward "^{" p) yuuji@31: (if (/= (following-char) ?\{) (error "Tabular format not found.")) yuuji@31: (setq boform (1+ (point)) yuuji@31: eoform (progn (forward-list 1) (1- (point)))) yuuji@31: (if (> eoform p) (error "Non-terminated tabular format.")) yuuji@31: (goto-char boform) yuuji@31: (while (< (point) eoform) yuuji@31: (setq elt (following-char)) yuuji@31: (cond yuuji@31: ((string-match (char-to-string elt) "clr") ;normal indicators. yuuji@31: (setq cols (1+ cols)) yuuji@31: (forward-char 1)) yuuji@31: ((equal elt ?|) ;vertical yuuji@31: (forward-char 1)) yuuji@31: ((string-match (char-to-string elt) "p@") ;p or @ expression yuuji@31: (setq cols (+ (if (eq elt ?p) 1 0) cols)) yuuji@31: (skip-chars-forward "^{" p) yuuji@31: (forward-list 1)))) yuuji@31: (list cols (1+ eoform)))) yuuji@31: ) yuuji@31: ;; Insert & yuuji@31: (defun YaTeX-intelligent-newline-tabular (&optional tabular*) yuuji@31: "Parse current tabular format and insert that many `&'s." yuuji@31: (let*((p (point)) (format (YaTeX-tabular-parse-format tabular*)) yuuji@31: (cols (car format)) (beg (car (cdr format))) yuuji@31: space hline) yuuji@31: (setq hline (search-backward "\\hline" beg t)) yuuji@31: (goto-char p) yuuji@31: (setq space (if (search-backward "\t&" beg t) "\t" " ")) yuuji@31: (goto-char p) yuuji@31: (YaTeX-indent-line) yuuji@31: (setq p (point)) yuuji@31: (while (> (1- cols) 0) yuuji@31: (insert "&" space) yuuji@31: (setq cols (1- cols))) yuuji@31: (insert "\\\\") yuuji@31: (if hline (insert " \\hline")) yuuji@31: (goto-char p)) yuuji@31: ) yuuji@31: yuuji@31: (defun YaTeX-intelligent-newline-tabular* () yuuji@31: "Parse current tabular* format and insert that many `&'s." yuuji@31: (YaTeX-intelligent-newline-tabular t) yuuji@31: ) yuuji@31: yuuji@31: (fset 'YaTeX-intelligent-newline-array 'YaTeX-intelligent-newline-tabular) yuuji@31: yuuji@23: ;;; yuuji@23: ;; Functions for tabbing environment yuuji@23: ;;; yuuji@23: (defun YaTeX-intelligent-newline-tabbing () yuuji@23: "Check the number of \\= in the first line and insert that many \\>." yuuji@23: (let ((p (point)) begenv tabcount) yuuji@23: (save-excursion yuuji@23: (YaTeX-beginning-of-environment) yuuji@23: (setq begenv (point-end-of-line)) yuuji@23: (if (YaTeX-search-active-forward "\\\\" YaTeX-comment-prefix p t) yuuji@23: (progn yuuji@23: (setq tabcount 0) yuuji@23: (while (> (point) begenv) yuuji@23: (if (search-backward "\\=" begenv 1) yuuji@23: (setq tabcount (1+ tabcount))))))) yuuji@23: (YaTeX-indent-line) yuuji@23: (if tabcount yuuji@23: (progn yuuji@23: (save-excursion yuuji@23: (while (> tabcount 0) yuuji@23: (insert "\\>\t") yuuji@23: (setq tabcount (1- tabcount)))) yuuji@23: (forward-char 2)) yuuji@23: (insert "\\="))) yuuji@23: ) yuuji@23: yuuji@23: ;;; yuuji@23: ;; Functions for itemize/enumerate/list environments yuuji@23: ;;; yuuji@23: yuuji@23: (defun YaTeX-indent-for-item () yuuji@23: (let (col (p (point)) begenv) yuuji@23: (save-excursion yuuji@23: (YaTeX-beginning-of-environment t) yuuji@23: (setq begenv (point-end-of-line)) yuuji@23: (goto-char p) yuuji@23: (if (YaTeX-search-active-backward "\\item" YaTeX-comment-prefix begenv t) yuuji@23: (setq col (current-column)))) yuuji@23: (if col (indent-to col) (YaTeX-indent-line))) yuuji@23: ) yuuji@23: yuuji@23: (defvar YaTeX-item-for-insert "\\item ") yuuji@23: (defun YaTeX-intelligent-newline-itemize () yuuji@23: "Insert '\\item '." yuuji@23: (YaTeX-indent-for-item) yuuji@23: (insert YaTeX-item-for-insert) yuuji@23: ) yuuji@23: (fset 'YaTeX-intelligent-newline-enumerate 'YaTeX-intelligent-newline-itemize) yuuji@23: (fset 'YaTeX-intelligent-newline-list 'YaTeX-intelligent-newline-itemize) yuuji@23: yuuji@23: (defun YaTeX-intelligent-newline-description () yuuji@23: (YaTeX-indent-for-item) yuuji@23: (insert "\\item[] ") yuuji@23: (forward-char -2) yuuji@23: ) yuuji@23: yuuji@23: yuuji@23: ;;; yuuji@23: ;; Intelligent newline yuuji@23: ;;; yuuji@23: ;;;###autoload yuuji@23: (defun YaTeX-intelligent-newline (arg) yuuji@23: "Insert newline and environment-specific entry. yuuji@23: `\\item' for some itemizing environment, yuuji@23: `\\> \\> \\' for tabbing environemnt, yuuji@23: `& & \\ \hline' for tabular environment." yuuji@23: (interactive "P") yuuji@23: (let*((env (YaTeX-inner-environment)) yuuji@23: func) yuuji@23: (if arg (setq env (YaTeX-read-environment "For what environment? "))) yuuji@23: (setq func (intern-soft (concat "YaTeX-intelligent-newline-" env))) yuuji@23: (end-of-line) yuuji@23: (newline) yuuji@23: (if (and env func (fboundp func)) yuuji@23: (funcall func))) yuuji@23: )