yatex

diff yatexenv.el @ 84:73cba5ddd111

Converted from RCS of yatex
author yuuji
date Sun, 27 Sep 2009 13:04:14 +0000
parents 0734be649cb8
children 699f3c6c8b2c
line diff
     1.1 --- a/yatexenv.el	Thu Dec 25 04:10:32 2003 +0000
     1.2 +++ b/yatexenv.el	Sun Sep 27 13:04:14 2009 +0000
     1.3 @@ -1,8 +1,8 @@
     1.4  ;;; -*- Emacs-Lisp -*-
     1.5  ;;; YaTeX environment-specific functions.
     1.6  ;;; yatexenv.el
     1.7 -;;; (c) 1994-2003 by HIROSE Yuuji.[yuuji@yatex.org]
     1.8 -;;; Last modified Fri Jun 27 12:09:30 2003 on firestorm
     1.9 +;;; (c) 1994-2006 by HIROSE Yuuji.[yuuji@yatex.org]
    1.10 +;;; Last modified Sat Jun 24 08:14:11 2006 on firestorm
    1.11  ;;; $Id$
    1.12  
    1.13  ;;;
    1.14 @@ -10,8 +10,8 @@
    1.15  ;;;
    1.16  
    1.17  ;; Showing the matching column of tabular environment.
    1.18 -(defun YaTeX-array-what-column ()
    1.19 -  "Show matching columne title of array environment.
    1.20 +(defun YaTeX-array-what-column-internal ()
    1.21 +  "Return the cons of matching column and its title of array environment.
    1.22  When calling from a program, make sure to be in array/tabular environment."
    1.23    (let ((p (point)) beg eot bor (nlptn "\\\\\\\\") (andptn "[^\\]&")
    1.24  	(n 0) j
    1.25 @@ -54,18 +54,23 @@
    1.26  	    (error "This column exceeds the limit."))
    1.27  	(setq j (1- j)))
    1.28        (skip-chars-forward "\\s ")
    1.29 -      (message
    1.30 -       "This is the column(#%d) of: %s" n
    1.31 -       (buffer-substring
    1.32 -	(point)
    1.33 -	(progn
    1.34 -	  (re-search-forward (concat andptn "\\|" nlptn) eot)
    1.35 -	  (goto-char (match-beginning 0))
    1.36 -	  (if (looking-at andptn)
    1.37 -	      (forward-char 1))
    1.38 -	  (skip-chars-backward "\\s ")
    1.39 -	  (point))))))
    1.40 -)
    1.41 +      (list n
    1.42 +	    (buffer-substring
    1.43 +	     (point)
    1.44 +	     (progn
    1.45 +	       (re-search-forward (concat andptn "\\|" nlptn) eot)
    1.46 +	       (goto-char (match-beginning 0))
    1.47 +	       (if (looking-at andptn)
    1.48 +		   (forward-char 1))
    1.49 +	       (skip-chars-backward "\\s ")
    1.50 +	       (point)))))))
    1.51 +
    1.52 +(defun YaTeX-array-what-column ()
    1.53 +  "Show matching column title of array environment.
    1.54 +When calling from a program, make sure to be in array/tabular environment."
    1.55 +  (apply 'message
    1.56 +	 "This is the column(#%d) of: %s"
    1.57 +	 (YaTeX-array-what-column-internal)))
    1.58  
    1.59  ;;;###autoload
    1.60  (defun YaTeX-what-column ()
    1.61 @@ -74,28 +79,12 @@
    1.62    (cond
    1.63     ((YaTeX-quick-in-environment-p '("tabular" "tabular*" "array" "array*"))
    1.64      (YaTeX-array-what-column))
    1.65 -   (t (message "Not in array/tabular environment.")))
    1.66 -)
    1.67 +   (t (message "Not in array/tabular environment."))))
    1.68  
    1.69 -(defun YaTeX-tabular-parse-format (&optional tabular*)
    1.70 -  "Parse `tabular' format.
    1.71 -Return the list of (No.ofCols PointEndofFormat)"
    1.72 -  (let ((p (point)) elt boform eoform (cols 0))
    1.73 -    (save-excursion
    1.74 -      (if (null (YaTeX-beginning-of-environment t))
    1.75 -	  (error "Beginning of tabular not found."))
    1.76 -      (skip-chars-forward "^{")
    1.77 -      (forward-list 1)
    1.78 -      (if tabular*
    1.79 -	  (progn (skip-chars-forward "^{")
    1.80 -		 (forward-list 1)))
    1.81 -      (skip-chars-forward "^{" p)
    1.82 -      (if (/= (following-char) ?\{) (error "Tabular format not found."))
    1.83 -      (setq boform (1+ (point))
    1.84 -	    eoform (progn (forward-list 1) (1- (point))))
    1.85 -      (if (> eoform p) (error "Non-terminated tabular format."))
    1.86 -      (goto-char boform)
    1.87 -      (while (< (point) eoform)
    1.88 +(defun YaTeX-tabular-parse-format-count-cols (beg end)
    1.89 +  (goto-char beg)
    1.90 +  (let (elt (cols 0))
    1.91 +    (while (< (point) end)
    1.92  	(setq elt (following-char))
    1.93  	(cond
    1.94  	 ((string-match (char-to-string elt) "clr") ;normal indicators.
    1.95 @@ -105,16 +94,60 @@
    1.96  	  (forward-char 1))
    1.97  	 ((string-match (char-to-string elt) "p@") ;p or @ expression
    1.98  	  (setq cols (+ (if (eq elt ?p) 1 0) cols))
    1.99 -	  (skip-chars-forward "^{" p)
   1.100 +	  ;;(skip-chars-forward "^{" p)
   1.101 +	  (skip-chars-forward "^{" end)
   1.102  	  (forward-list 1))
   1.103 +	 ((equal elt ?*)		;*{N}{EXP} -> Repeat EXP N times
   1.104 +	  (skip-chars-forward "^{" end)
   1.105 +	  (setq cols (* (string-to-int
   1.106 +			 (buffer-substring
   1.107 +			  (1+ (point))
   1.108 +			  (progn (forward-list 1) (1- (point)))))
   1.109 +			(YaTeX-tabular-parse-format-count-cols
   1.110 +			 (progn (skip-chars-forward "^{" end) (1+ (point)))
   1.111 +			 (progn (forward-list 1) (1- (point)))))))
   1.112  	 (t (forward-char 1))		;unknown char
   1.113  	 ))
   1.114 -      (list cols (1+ eoform))))
   1.115 -)
   1.116 +    cols))
   1.117 +
   1.118 +(defun YaTeX-tabular-parse-format (&optional type)
   1.119 +  "Parse `tabular' format.
   1.120 +Return the list of (No.ofCols PointEndofFormat)"
   1.121 +  (let ((p (point)) boform eoform (cols 0))
   1.122 +    (save-excursion
   1.123 +      (if (null (YaTeX-beginning-of-environment t))
   1.124 +	  (error "Beginning of tabular not found."))
   1.125 +      (skip-chars-forward "^{")
   1.126 +      (forward-list 1)
   1.127 +      (cond
   1.128 +       ((eq type 'tabular*)
   1.129 +	(skip-chars-forward "^{")
   1.130 +	(forward-list 1)))
   1.131 +      (skip-chars-forward "^{" p)
   1.132 +      (if (/= (following-char) ?\{) (error "Tabular format not found."))
   1.133 +      (setq boform (1+ (point))
   1.134 +	    eoform (progn (forward-list 1) (1- (point))))
   1.135 +      (if (> eoform p) (error "Non-terminated tabular format."))
   1.136 +      (goto-char boform)
   1.137 +      (setq cols
   1.138 +	    (cond
   1.139 +	     ((eq type 'alignat)
   1.140 +	      (max
   1.141 +	       1
   1.142 +	       (1-
   1.143 +		(* 2
   1.144 +		   (string-to-int
   1.145 +		    (buffer-substring
   1.146 +		     (point)
   1.147 +		     (progn (up-list -1) (forward-list 1) (1- (point)))))))))
   1.148 +	     (t
   1.149 +	      (YaTeX-tabular-parse-format-count-cols (point) eoform))))
   1.150 +      (list cols (1+ eoform)))))
   1.151 +
   1.152  ;; Insert &
   1.153 -(defun YaTeX-intelligent-newline-tabular (&optional tabular*)
   1.154 +(defun YaTeX-intelligent-newline-tabular (&optional type)
   1.155    "Parse current tabular format and insert that many `&'s."
   1.156 -  (let*((p (point)) (format (YaTeX-tabular-parse-format tabular*))
   1.157 +  (let*((p (point)) (format (YaTeX-tabular-parse-format type))
   1.158  	(cols (car format)) (beg (car (cdr format)))
   1.159  	space hline)
   1.160      (cond
   1.161 @@ -132,16 +165,43 @@
   1.162      (insert "\\\\")
   1.163      (if hline (insert " \\hline"))
   1.164      (goto-char p)
   1.165 -    (YaTeX-indent-line)
   1.166 -)
   1.167 -)
   1.168 +    (YaTeX-indent-line)))
   1.169  
   1.170  (defun YaTeX-intelligent-newline-tabular* ()
   1.171    "Parse current tabular* format and insert that many `&'s."
   1.172 -  (YaTeX-intelligent-newline-tabular t)
   1.173 -)
   1.174 +  (YaTeX-intelligent-newline-tabular 'tabular*))
   1.175  
   1.176  (fset 'YaTeX-intelligent-newline-array 'YaTeX-intelligent-newline-tabular)
   1.177 +(fset 'YaTeX-intelligent-newline-supertabular 'YaTeX-intelligent-newline-tabular)
   1.178 +
   1.179 +(defun YaTeX-intelligent-newline-alignat ()
   1.180 +  (YaTeX-intelligent-newline-tabular 'alignat))
   1.181 +(fset 'YaTeX-intelligent-newline-alignat* 'YaTeX-intelligent-newline-alignat)
   1.182 +
   1.183 +(defun YaTeX-intelligent-newline-align ()
   1.184 +  "Intelligent newline function for align.
   1.185 +Count the number of & in the first align line and insert that many &s."
   1.186 +  (let*((p (point)) (cols 0))
   1.187 +    (save-excursion
   1.188 +      (YaTeX-beginning-of-environment)
   1.189 +      (catch 'done
   1.190 +	(while (YaTeX-re-search-active-forward
   1.191 +		"\\(&\\)\\|\\(\\\\\\\\\\)" YaTeX-comment-prefix p t)
   1.192 +	  (if (match-beginning 1) (setq cols (1+ cols)) (throw 'done t)))))
   1.193 +    (save-excursion
   1.194 +      (if (= cols 0)
   1.195 +	  (insert "&")
   1.196 +	(while (>= (setq cols (1- cols)) 0)
   1.197 +	  (insert "& "))))
   1.198 +    (YaTeX-indent-line)))
   1.199 +
   1.200 +(mapcar
   1.201 + '(lambda (s)
   1.202 +    (fset (intern (concat  "YaTeX-intelligent-newline-"
   1.203 +			   (symbol-name s)))
   1.204 +	  'YaTeX-intelligent-newline-align))
   1.205 + '(align* flalign  flalign* matrix pmatrix bmatrix Bmatrix vmatrix Vmatrix
   1.206 +   cases))
   1.207  
   1.208  ;;;
   1.209  ;; Functions for tabbing environment
   1.210 @@ -214,8 +274,7 @@
   1.211      (newline)
   1.212      (undo-boundary)
   1.213      (if (and env func (fboundp func))
   1.214 -	(funcall func)))
   1.215 -)
   1.216 +	(funcall func))))
   1.217  
   1.218  ;;;
   1.219  ;; Environment-specific line indenting functions