yatex

annotate yatex.el @ 70:44e3a5e1e883

Fix makefile for Windows Brush up label completion \cite completion Support much more about LaTeX2e --- [yahtml] English Info (By Jun Ohya) Automatic pixel size detection for <img src="..."> Aware global-class of css definition & for char-entity reference
author yuuji
date Sun, 09 Apr 2000 03:37:47 +0000
parents 807c1e7e68b7
children 0aaebd07dad0
rev   line source
yuuji@0 1 ;;; -*- Emacs-Lisp -*-
yuuji@69 2 ;;; Yet Another tex-mode for emacs - //野鳥//
yuuji@70 3 ;;; yatex.el rev. 1.68
yuuji@70 4 ;;; (c )1991-2000 by HIROSE Yuuji.[yuuji@yatex.org]
yuuji@70 5 ;;; Last modified Sun Apr 9 12:35:15 2000 on firestorm
yuuji@11 6 ;;; $Id$
yuuji@69 7 ;;; The latest version of this software is always available at;
yuuji@69 8 ;;; http://www.yatex.org/
yuuji@0 9
yuuji@13 10 ;; This program is free software; you can redistribute it and/or modify
yuuji@13 11 ;; it under the terms of the GNU General Public License as published by
yuuji@13 12 ;; the Free Software Foundation; either version 2, or (at your option)
yuuji@13 13 ;; any later version.
yuuji@5 14
yuuji@13 15 ;; This program is distributed in the hope that it will be useful,
yuuji@13 16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
yuuji@13 17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
yuuji@13 18 ;; GNU General Public License for more details.
yuuji@13 19
yuuji@13 20 ;; You should have received a copy of the GNU General Public License
yuuji@68 21 ;; along with this program; see the file COPYING. If not, write to the
yuuji@68 22 ;; Software Foundation Inc., 59 Temple Place - Suite 330, Boston, MA
yuuji@68 23 ;; 02111-1307, USA.
yuuji@5 24
yuuji@3 25 (require 'comment)
yuuji@64 26 (require 'yatexlib)
yuuji@70 27 (defconst YaTeX-revision-number "1.6*"
yuuji@69 28 "Revision number of running yatex.el")
yuuji@69 29
yuuji@0 30 ;---------- Local variables ----------
yuuji@22 31 (defvar YaTeX-prefix "\C-c"
yuuji@59 32 "*Prefix key to call YaTeX functions.
yuuji@69 33 You can select favorite prefix key by setq in your ~/.emacs.")
yuuji@69 34
yuuji@11 35 (defvar YaTeX-environment-indent 1
yuuji@69 36 "*Indentation depth at column width in LaTeX environments.")
yuuji@69 37
yuuji@64 38 (defvar YaTeX-fill-prefix nil
yuuji@5 39 "*fill-prefix used for auto-fill-mode.
yuuji@69 40 The default value is nil.")
yuuji@69 41
yuuji@5 42 (defvar YaTeX-fill-column 72
yuuji@69 43 "*fill-column used for auto-fill-mode.")
yuuji@69 44
yuuji@4 45 (defvar YaTeX-comment-prefix "%"
yuuji@69 46 "TeX comment prefix.")
yuuji@69 47
yuuji@5 48 (defvar YaTeX-current-position-register ?3
yuuji@22 49 "*Position register to keep where the last completion was done.
yuuji@22 50 All of YaTeX completing input store the current position into
yuuji@5 51 the register YaTeX-current-position-register. So every time you
yuuji@52 52 make a trip to any other part of text other than you are writing, you can
yuuji@13 53 return to the editing paragraph by calling register-to-point with argument
yuuji@69 54 YaTeX-current-position-register.")
yuuji@69 55
yuuji@18 56 ;;(defvar YaTeX-tmp-dic-unit 'main-file
yuuji@18 57 ;; "*Default switching unit of temporary dictionary.
yuuji@18 58 ;;There are two switching unit:
yuuji@18 59 ;;'main-file : switch tmp-dic according to main-file directory.
yuuji@18 60 ;;'directory : switch tmp-dic dir by dir."
yuuji@18 61 ;;)
yuuji@7 62 (defvar tex-command (if YaTeX-japan "jlatex" "latex")
yuuji@69 63 "*Default command for typesetting LaTeX text.")
yuuji@69 64
yuuji@7 65 (defvar bibtex-command (if YaTeX-japan "jbibtex" "bibtex")
yuuji@69 66 "*Default command of BibTeX.")
yuuji@69 67
yuuji@0 68 (defvar dvi2-command ;previewer command for your site
yuuji@52 69 (if YaTeX-dos "dviout -wait=0"
yuuji@58 70 "xdvi -geo +0+0 -s 4")
yuuji@5 71 "*Default previewer command including its option.
yuuji@69 72 This default value is for X window system.")
yuuji@69 73
yuuji@13 74 (defvar makeindex-command (if YaTeX-dos "makeind" "makeindex")
yuuji@69 75 "*Default makeindex command.")
yuuji@69 76
yuuji@5 77 (defvar dviprint-command-format
yuuji@13 78 (if YaTeX-dos "dviprt %s %f%t"
yuuji@5 79 "dvi2ps %f %t %s | lpr")
yuuji@22 80 "*Command line string to print out current file.
yuuji@22 81 Format string %s will be replaced by the filename. Do not forget to
yuuji@22 82 specify the `from usage' and `to usage' with their option by format string
yuuji@22 83 %f and %t.
yuuji@69 84 See also documentation of dviprint-from-format and dviprint-to-format.")
yuuji@69 85
yuuji@5 86 (defvar dviprint-from-format
yuuji@13 87 (if YaTeX-dos "%b-" "-f %b")
yuuji@69 88 "*`From' page format of dvi filter. %b will turn to beginning page number.")
yuuji@69 89
yuuji@5 90 (defvar dviprint-to-format
yuuji@13 91 (if YaTeX-dos "%e" "-t %e")
yuuji@69 92 "*`To' page format of dvi filter. %e will turn to end page number.")
yuuji@69 93
yuuji@5 94 (defvar YaTeX-default-document-style
yuuji@5 95 (concat (if YaTeX-japan "j") "article")
yuuji@69 96 "*Default LaTeX Documentstyle for YaTeX-typeset-region.")
yuuji@69 97
yuuji@5 98 (defvar YaTeX-need-nonstop nil
yuuji@69 99 "*T for adding `\\nonstopmode{}' to text before invoking latex command.")
yuuji@69 100
yuuji@0 101 (defvar latex-warning-regexp "line.* [0-9]*"
yuuji@69 102 "*Regular expression of line number of warning message by latex command.")
yuuji@69 103
yuuji@0 104 (defvar latex-error-regexp "l\\.[1-9][0-9]*"
yuuji@22 105 "*Regular expression of line number of latex error.
yuuji@22 106 Perhaps your latex command stops at this error message with line number of
yuuji@69 107 LaTeX source text.")
yuuji@69 108
yuuji@1 109 (defvar latex-dos-emergency-message
yuuji@3 110 "Emergency stop" ;<- for Micro tex, ASCII-pTeX 1.6
yuuji@22 111 "Message pattern of emergency stop of typesetting.
yuuji@22 112 Because Demacs (GNU Emacs on DOS) cannot have concurrent process, the
yuuji@0 113 latex command which is stopping on a LaTeX error, is terminated by Demacs.
yuuji@13 114 Many latex command on DOS display some messages when it is terminated by
yuuji@13 115 other process, user or OS. Define to this variable a message string of your
yuuji@13 116 latex command on DOS shown at abnormal termination.
yuuji@0 117 Remember Demacs's call-process function is not oriented for interactive
yuuji@69 118 process.")
yuuji@69 119
yuuji@5 120 (defvar latex-message-kanji-code 2
yuuji@5 121 "*Kanji coding system latex command types out.
yuuji@69 122 1 = Shift JIS, 2 = JIS, 3 = EUC.")
yuuji@69 123
yuuji@64 124 (defvar NTT-jTeX nil
yuuji@64 125 "*T for using NTT-jTeX for latex command.
yuuji@64 126 More precisely, setting t to this variables inhibits inter-word break on
yuuji@64 127 typeset document by line-break of source text. That is, YaTeX automatically
yuuji@64 128 put % after each line at filling.
yuuji@64 129 改行+インデントによって、タイプセット後の字間が空いてしまうのを抑制する場合に
yuuji@64 130 tにする(古いNTT-jTeXで顕著に現れる)。具体的には、fillするときに各行の終わりに
yuuji@69 131 %を付加する。")
yuuji@69 132
yuuji@70 133 (defvar YaTeX-use-LaTeX2e t "*Use LaTeX2e or not. Nil meas latex 2.09")
yuuji@70 134
yuuji@70 135
yuuji@64 136 (defvar YaTeX-item-regexp
yuuji@64 137 (concat (regexp-quote "\\") "\\(sub\\|bib\\)*item")
yuuji@69 138 "*Regular expression of item command.")
yuuji@69 139
yuuji@7 140 (defvar YaTeX-sectioning-regexp
yuuji@69 141 "\\(part\\|chapter\\*?\\|\\(sub\\)*\\(section\\|paragraph\\)\\)\\(\\*\\|\\b\\)"
yuuji@69 142 "*LaTeX sectioning commands regexp.")
yuuji@69 143
yuuji@58 144 (defvar YaTeX-paragraph-start
yuuji@59 145 (concat "^[ \t]*%\\|^[ \t]*$\\|\\'\\|^\C-l\\|\\\\\\\\$\\|^[ \t]*\\\\\\("
yuuji@13 146 YaTeX-sectioning-regexp ;sectioning commands
yuuji@7 147 "\\|[A-z]*item\\|begin{\\|end{" ;special declaration
yuuji@70 148 "\\|\\[\\|\\]"
yuuji@59 149 "\\|newpage\\b\\|vspace\\b"
yuuji@7 150 "\\)")
yuuji@58 151 "*Paragraph starting regexp of common LaTeX source. Use this value
yuuji@69 152 for YaTeX-uncomment-paragraph.")
yuuji@69 153
yuuji@58 154 (defvar YaTeX-paragraph-separate
yuuji@58 155 (concat "^[ \t]*%\\|^[ \t]*$\\|^\C-l\\|\\\\\\\\$\\|^[ \t]*\\\\\\("
yuuji@58 156 YaTeX-sectioning-regexp ;sectioning commands
yuuji@58 157 "\\|begin{\\|end{" ;special declaration
yuuji@68 158 "\\|\\[\\|\\]"
yuuji@59 159 "\\|newpage\\b\\|vspace\\b"
yuuji@58 160 "\\)")
yuuji@7 161 "*Paragraph delimiter regexp of common LaTeX source. Use this value
yuuji@69 162 for YaTeX-uncomment-paragraph.")
yuuji@69 163
yuuji@49 164 (defvar YaTeX-verbatim-environments
yuuji@49 165 '("verbatim" "verbatim*")
yuuji@49 166 "*Assume these environments of this variable disable LaTeX commands.")
yuuji@51 167 (defvar YaTeX-verb-regexp "verb\\*?\\|path"
yuuji@51 168 "*Regexp of verb family. Do not contain preceding \\\\ nor \\(\\).")
yuuji@49 169 (defvar YaTeX-fill-inhibit-environments
yuuji@49 170 (append '("tabular" "tabular*" "array" "picture" "eqnarray" "eqnarray*"
yuuji@69 171 "equation" "equation*" "math" "displaymath")
yuuji@49 172 YaTeX-verbatim-environments)
yuuji@13 173 "*In these environments, YaTeX inhibits fill-paragraph from formatting.
yuuji@69 174 Define those environments as a form of list.")
yuuji@69 175
yuuji@52 176 (defvar YaTeX-itemizing-env-regexp
yuuji@64 177 "itemize\\|enumerate\\|description\\|list\\|thebibliography"
yuuji@53 178 "*Regexp of itemizing environments")
yuuji@53 179 (defvar YaTeX-equation-env-regexp
yuuji@54 180 "array\\*?\\|equation\\*?"
yuuji@53 181 "*Regexp of environments for equations")
yuuji@57 182 (defvar YaTeX-array-env-regexp
yuuji@58 183 (concat
yuuji@58 184 "array\\*?\\|eqnarray\\*?\\|tabbing\\|tabular\\*?\\|" ;LaTeX
yuuji@69 185 "matrix\\|pmatrix\\|bmatrix\\|vmatrix\\|Vmatrix\\|" ;AMS-LaTeX
yuuji@58 186 "align\\*?\\|split\\*?\\|aligned\\*?\\|alignat\\*?\\|" ;AMS-LaTeX
yuuji@68 187 "[bpvV]?matrix\\|smallmatrix\\|cases\\|" ;AMS-LaTeX
yuuji@58 188 "xalignat\\*?\\|xxalignat\\*?") ;AMS-LaTeX
yuuji@57 189 "*Regexp of environments where `&' becomes field delimiter.")
yuuji@11 190 (defvar YaTeX-uncomment-once t
yuuji@22 191 "*T for removing all continuous commenting character(%).
yuuji@69 192 Nil for removing only one commenting character at the beginning-of-line.")
yuuji@69 193
yuuji@22 194 (defvar YaTeX-close-paren-always t
yuuji@69 195 "*Close parenthesis always when YaTeX-modify-mode is nil.")
yuuji@69 196
yuuji@22 197 (defvar YaTeX-greek-by-maketitle-completion nil
yuuji@69 198 "*T for greek letters completion by maketitle-type completion.")
yuuji@69 199
yuuji@22 200 (defvar YaTeX-auto-math-mode t
yuuji@22 201 "*T for changing YaTeX-math mode automatically.")
yuuji@58 202 (defvar YaTeX-use-AMS-LaTeX nil
yuuji@69 203 "*T for using AMS-LaTeX")
yuuji@69 204
yuuji@22 205 (defvar yatex-mode-hook nil
yuuji@69 206 "*List of functions to be called at the end of yatex-mode initializations.")
yuuji@69 207
yuuji@70 208 (defvar YaTeX-search-file-from-top-directory t
yuuji@70 209 "*Non-nil means to search input-files from the directory where main file
yuuji@70 210 exists.")
yuuji@70 211
yuuji@22 212 ;;-- Math mode values --
yuuji@22 213
yuuji@22 214 (defvar YaTeX-math-key-list-default
yuuji@22 215 '((";" . YaTeX-math-sign-alist)
yuuji@52 216 (":" . YaTeX-greek-key-alist))
yuuji@69 217 "Default key sequence to invoke math-mode's image completion.")
yuuji@69 218
yuuji@22 219 (defvar YaTeX-math-key-list-private nil
yuuji@69 220 "*User defined alist, math-mode-prefix vs completion alist.")
yuuji@69 221
yuuji@22 222 (defvar YaTeX-math-key-list
yuuji@22 223 (append YaTeX-math-key-list-private YaTeX-math-key-list-default)
yuuji@69 224 "Key sequence to invoke math-mode's image completion.")
yuuji@69 225
yuuji@54 226 (defvar YaTeX-skip-default-reader nil
yuuji@69 227 "Non-nil skips default argument reader of section-type completion.")
yuuji@69 228
yuuji@54 229 (defvar YaTeX-simple-messages nil
yuuji@69 230 "Non-nil makes minibuffer messages simpler.")
yuuji@69 231
yuuji@64 232 (defvar YaTeX-template-file "~/work/template.tex"
yuuji@69 233 "*Template TeX source file. This will be inserted to empty file.")
yuuji@69 234
yuuji@57 235 (defvar YaTeX-addin-prefix "YaTeX:")
yuuji@0 236 ;------------ Completion table ------------
yuuji@0 237 ; Set tex-section-like command possible completion
yuuji@22 238 (defvar section-table
yuuji@70 239 (append
yuuji@70 240 '(("part") ("chapter") ("chapter*") ("section") ("section*")
yuuji@70 241 ("subsection") ("subsection*")
yuuji@70 242 ("subsubsection") ("paragraph") ("subparagraph")
yuuji@70 243 ("author") ("thanks") ("documentstyle") ("pagestyle")
yuuji@70 244 ("title") ("underline") ("label") ("makebox")
yuuji@70 245 ("footnote") ("footnotetext") ("index")
yuuji@70 246 ("hspace*") ("vspace*") ("bibliography") ("bibitem") ("cite")
yuuji@70 247 ("input") ("include") ("includeonly") ("mbox") ("hbox") ("caption")
yuuji@70 248 ("newlength") ("setlength" 2) ("addtolength" 2) ("settowidth" 2)
yuuji@70 249 ("setcounter" 2) ("addtocounter" 2) ("stepcounter" 2)
yuuji@70 250 ("newcommand" 2) ("renewcommand" 2)
yuuji@70 251 ("setcounter" 2) ("newenvironment" 3) ("newtheorem" 2)
yuuji@70 252 ("cline") ("framebox") ("savebox" 2) ("sbox" 2) ("newsavebox") ("usebox")
yuuji@70 253 ("date") ("put") ("ref") ("pageref")
yuuji@70 254 ("multicolumn" 3) ("shortstack")
yuuji@70 255 ;; for mathmode accent
yuuji@70 256 ("tilde") ("hat") ("check") ("bar") ("dot") ("ddot") ("vec")
yuuji@70 257 ("widetilde") ("widehat") ("overline") ("overrightarrow")
yuuji@70 258 ;; section types in mathmode
yuuji@70 259 ("frac" 2) ("sqrt") ("mathrm") ("mathbf") ("mathit")
yuuji@70 260 )
yuuji@70 261 (if YaTeX-use-LaTeX2e
yuuji@70 262 '(("documentclass") ("usepackage")
yuuji@70 263 ("textbf") ("textgt") ("textit") ("textmc") ("textmd") ("textnormal")
yuuji@70 264 ("textrm") ("textsc") ("textsf") ("textsl") ("texttt") ("textup")
yuuji@70 265 ("mathbf") ("mathcal") ("mathit") ("mathnormal") ("mathrm")
yuuji@70 266 ("mathsf") ("mathtt")
yuuji@70 267 ("scalebox" 1) ;is faking of argument position
yuuji@70 268 ("rotatebox" 2) ("resizebox" 2) ("reflectbox")
yuuji@70 269 ("colorbox" 2) ("fcolorbox" 3) ("textcolor" 2) ("color")
yuuji@70 270 ("includegraphics") ("includegraphics*")
yuuji@70 271 )))
yuuji@69 272 "Default completion table for section-type completion.")
yuuji@69 273
yuuji@0 274 (defvar user-section-table nil)
yuuji@6 275 (defvar tmp-section-table nil)
yuuji@0 276
yuuji@0 277 ; Set tex-environment possible completion
yuuji@22 278 (defvar env-table
yuuji@22 279 '(("quote") ("quotation") ("center") ("verse") ("document")
yuuji@22 280 ("verbatim") ("itemize") ("enumerate") ("description")
yuuji@53 281 ("list") ("tabular") ("tabular*") ("table") ("tabbing") ("titlepage")
yuuji@70 282 ("sloppypar") ("picture") ("displaymath")
yuuji@22 283 ("eqnarray") ("figure") ("equation") ("abstract") ("array")
yuuji@22 284 ("thebibliography") ("theindex") ("flushleft") ("flushright")
yuuji@22 285 ("minipage")
yuuji@22 286 )
yuuji@69 287 "Default completion table for begin-type completion.")
yuuji@69 288
yuuji@0 289 (defvar user-env-table nil)
yuuji@6 290 (defvar tmp-env-table nil)
yuuji@0 291
yuuji@13 292 ; Set {\Large }-like completion
yuuji@22 293 (defvar fontsize-table
yuuji@22 294 '(("rm") ("em") ("bf") ("boldmath") ("it") ("sl") ("sf") ("sc") ("tt")
yuuji@22 295 ("dg") ("dm")
yuuji@22 296 ("tiny") ("scriptsize") ("footnotesize") ("small")("normalsize")
yuuji@22 297 ("large") ("Large") ("LARGE") ("huge") ("Huge")
yuuji@70 298 ("rmfamily") ("sffamily") ("ttfamily")
yuuji@70 299 ("mdseries") ("bfseries") ("upshape")
yuuji@70 300 ("itshape") ("slshape") ("scshape")
yuuji@22 301 )
yuuji@69 302 "Default completion table for large-type completion.")
yuuji@69 303
yuuji@70 304 (defvar LaTeX2e-fontstyle-alist
yuuji@70 305 '(("rm" . "rmfamily")
yuuji@70 306 ("sf" . "sffamily")
yuuji@70 307 ("tt" . "ttfamily")
yuuji@70 308 ("md" . "mdseries")
yuuji@70 309 ("bf" . "bfseries")
yuuji@70 310 ("up" . "upshape")
yuuji@70 311 ("it" . "itshape")
yuuji@70 312 ("sl" . "slshape")
yuuji@70 313 ("sc" . "scshape")))
yuuji@70 314
yuuji@0 315 (defvar user-fontsize-table nil)
yuuji@6 316 (defvar tmp-fontsize-table nil)
yuuji@0 317
yuuji@22 318 (defvar singlecmd-table
yuuji@51 319 (append
yuuji@68 320 '(("maketitle") ("makeindex") ("sloppy") ("protect")
yuuji@70 321 ("LaTeX") ("TeX") ("item") ("item[]") ("appendix") ("hline") ("kill")
yuuji@54 322 ;;("rightarrow") ("Rightarrow") ("leftarrow") ("Leftarrow")
yuuji@70 323 ("pagebreak") ("nopagebreak") ("tableofcontents")
yuuji@68 324 ("newpage") ("clearpage") ("cleardoublepage")
yuuji@51 325 ("footnotemark") ("verb") ("verb*")
yuuji@59 326 ("linebreak") ("pagebreak") ("noindent") ("indent")
yuuji@70 327 ("left") ("right") ("dots") ("smallskip") ("medskip") ("bigskip")
yuuji@51 328 )
yuuji@51 329 (if YaTeX-greek-by-maketitle-completion
yuuji@51 330 '(("alpha") ("beta") ("gamma") ("delta") ("epsilon")
yuuji@51 331 ("varepsilon") ("zeta") ("eta") ("theta")("vartheta")
yuuji@51 332 ("iota") ("kappa") ("lambda") ("mu") ("nu") ("xi") ("pi")
yuuji@51 333 ("varpi") ("rho") ("varrho") ("sigma") ("varsigma") ("tau")
yuuji@51 334 ("upsilon") ("phi") ("varphi") ("chi") ("psi") ("omega")
yuuji@51 335 ("Gamma") ("Delta") ("Theta") ("Lambda")("Xi") ("Pi")
yuuji@51 336 ("Sigma") ("Upsilon") ("Phi") ("Psi") ("Omega"))))
yuuji@69 337 "Default completion table for maketitle-type completion.")
yuuji@69 338
yuuji@0 339 (defvar user-singlecmd-table nil)
yuuji@6 340 (defvar tmp-singlecmd-table nil)
yuuji@0 341
yuuji@0 342 ;---------- Key mode map ----------
yuuji@0 343 ;;;
yuuji@0 344 ;; Create new key map: YaTeX-mode-map
yuuji@0 345 ;; Do not change this section.
yuuji@0 346 ;;;
yuuji@0 347 (defvar YaTeX-mode-map nil
yuuji@69 348 "Keymap used in YaTeX mode")
yuuji@69 349
yuuji@4 350 (defvar YaTeX-prefix-map nil
yuuji@69 351 "Keymap used when YaTeX-prefix key pushed")
yuuji@69 352
yuuji@54 353 (defvar YaTeX-user-extensional-map (make-sparse-keymap)
yuuji@54 354 "*Keymap used for the user's customization")
yuuji@7 355 (defvar YaTeX-current-completion-type nil
yuuji@69 356 "Has current completion type. This may be used in YaTeX addin functions.")
yuuji@69 357
yuuji@22 358 (defvar YaTeX-modify-mode nil
yuuji@51 359 "*Current editing mode.
yuuji@51 360 When non-nil, each opening parentheses only opens,
yuuji@69 361 nil enters both open/close parentheses when opening parentheses key pressed.")
yuuji@69 362
yuuji@22 363 (defvar YaTeX-math-mode nil
yuuji@69 364 "Holds whether current mode is math-mode.")
yuuji@0 365 ;;;
yuuji@0 366 ;; Define key table
yuuji@0 367 ;;;
yuuji@0 368 (if YaTeX-mode-map
yuuji@0 369 nil
yuuji@0 370 (setq YaTeX-mode-map (make-sparse-keymap))
yuuji@4 371 (setq YaTeX-prefix-map (make-sparse-keymap))
yuuji@4 372 (define-key YaTeX-mode-map "\"" 'YaTeX-insert-quote)
yuuji@5 373 (define-key YaTeX-mode-map "{" 'YaTeX-insert-braces)
yuuji@22 374 (define-key YaTeX-mode-map "(" 'YaTeX-insert-parens)
yuuji@7 375 (define-key YaTeX-mode-map "$" 'YaTeX-insert-dollar)
yuuji@69 376 (define-key YaTeX-mode-map "|" 'YaTeX-insert-bar)
yuuji@57 377 (define-key YaTeX-mode-map "&" 'YaTeX-insert-amper)
yuuji@22 378 (define-key YaTeX-mode-map "[" 'YaTeX-insert-brackets)
yuuji@4 379 (define-key YaTeX-mode-map YaTeX-prefix YaTeX-prefix-map)
yuuji@22 380 (define-key YaTeX-mode-map "\M-\C-@" 'YaTeX-mark-environment)
yuuji@22 381 (define-key YaTeX-mode-map "\M-\C-a" 'YaTeX-beginning-of-environment)
yuuji@22 382 (define-key YaTeX-mode-map "\M-\C-e" 'YaTeX-end-of-environment)
yuuji@22 383 (define-key YaTeX-mode-map "\M-\C-m" 'YaTeX-intelligent-newline)
yuuji@32 384 (define-key YaTeX-mode-map "\C-i" 'YaTeX-indent-line)
yuuji@13 385 (YaTeX-define-key "%" 'YaTeX-%-menu)
yuuji@4 386 (YaTeX-define-key "t" 'YaTeX-typeset-menu)
yuuji@11 387 (YaTeX-define-key "w" 'YaTeX-switch-mode-menu)
yuuji@5 388 (YaTeX-define-key "'" 'YaTeX-prev-error)
yuuji@5 389 (YaTeX-define-key "^" 'YaTeX-visit-main)
yuuji@5 390 (YaTeX-define-key "4^" 'YaTeX-visit-main-other-window)
yuuji@51 391 (YaTeX-define-key "4g" 'YaTeX-goto-corresponding-*-other-window)
yuuji@53 392 (YaTeX-define-key "44" 'YaTeX-switch-to-window)
yuuji@53 393 (and YaTeX-emacs-19 window-system
yuuji@53 394 (progn
yuuji@53 395 (YaTeX-define-key "5^" 'YaTeX-visit-main-other-frame)
yuuji@53 396 (YaTeX-define-key "5g" 'YaTeX-goto-corresponding-*-other-frame)
yuuji@53 397 (YaTeX-define-key "55" 'YaTeX-switch-to-window)))
yuuji@5 398 (YaTeX-define-key " " 'YaTeX-do-completion)
yuuji@4 399 (YaTeX-define-key "v" 'YaTeX-version)
yuuji@0 400
yuuji@5 401 (YaTeX-define-key "}" 'YaTeX-insert-braces-region)
yuuji@5 402 (YaTeX-define-key "]" 'YaTeX-insert-brackets-region)
yuuji@11 403 (YaTeX-define-key ")" 'YaTeX-insert-parens-region)
yuuji@11 404 (YaTeX-define-key "$" 'YaTeX-insert-dollars-region)
yuuji@5 405 (YaTeX-define-key "i" 'YaTeX-fill-item)
yuuji@4 406 (YaTeX-define-key
yuuji@7 407 "\\" '(lambda () (interactive) (insert "$\\backslash$")))
yuuji@22 408 (if YaTeX-no-begend-shortcut
yuuji@22 409 (progn
yuuji@22 410 (YaTeX-define-key "B" 'YaTeX-make-begin-end-region)
yuuji@22 411 (YaTeX-define-key "b" 'YaTeX-make-begin-end))
yuuji@22 412 (YaTeX-define-begend-key "bc" "center")
yuuji@22 413 (YaTeX-define-begend-key "bd" "document")
yuuji@22 414 (YaTeX-define-begend-key "bD" "description")
yuuji@22 415 (YaTeX-define-begend-key "be" "enumerate")
yuuji@22 416 (YaTeX-define-begend-key "bE" "equation")
yuuji@22 417 (YaTeX-define-begend-key "bi" "itemize")
yuuji@22 418 (YaTeX-define-begend-key "bl" "flushleft")
yuuji@22 419 (YaTeX-define-begend-key "bm" "minipage")
yuuji@22 420 (YaTeX-define-begend-key "bt" "tabbing")
yuuji@22 421 (YaTeX-define-begend-key "bT" "tabular")
yuuji@22 422 (YaTeX-define-begend-key "b\^t" "table")
yuuji@22 423 (YaTeX-define-begend-key "bp" "picture")
yuuji@22 424 (YaTeX-define-begend-key "bq" "quote")
yuuji@22 425 (YaTeX-define-begend-key "bQ" "quotation")
yuuji@22 426 (YaTeX-define-begend-key "br" "flushright")
yuuji@22 427 (YaTeX-define-begend-key "bv" "verbatim")
yuuji@22 428 (YaTeX-define-begend-key "bV" "verse")
yuuji@22 429 (YaTeX-define-key "B " 'YaTeX-make-begin-end-region)
yuuji@22 430 (YaTeX-define-key "b " 'YaTeX-make-begin-end))
yuuji@4 431 (YaTeX-define-key "e" 'YaTeX-end-environment)
yuuji@14 432 (YaTeX-define-key "S" 'YaTeX-make-section-region)
yuuji@4 433 (YaTeX-define-key "s" 'YaTeX-make-section)
yuuji@4 434 (YaTeX-define-key "L" 'YaTeX-make-fontsize-region)
yuuji@4 435 (YaTeX-define-key "l" 'YaTeX-make-fontsize)
yuuji@4 436 (YaTeX-define-key "m" 'YaTeX-make-singlecmd)
yuuji@22 437 (YaTeX-define-key "." 'YaTeX-comment-paragraph)
yuuji@22 438 (YaTeX-define-key "," 'YaTeX-uncomment-paragraph)
yuuji@22 439 (YaTeX-define-key ">" 'YaTeX-comment-region)
yuuji@22 440 (YaTeX-define-key "<" 'YaTeX-uncomment-region)
yuuji@5 441 (YaTeX-define-key "g" 'YaTeX-goto-corresponding-*)
yuuji@5 442 (YaTeX-define-key "k" 'YaTeX-kill-*)
yuuji@5 443 (YaTeX-define-key "c" 'YaTeX-change-*)
yuuji@5 444 (YaTeX-define-key "a" 'YaTeX-make-accent)
yuuji@16 445 (YaTeX-define-key "?" 'YaTeX-help)
yuuji@22 446 (YaTeX-define-key "/" 'YaTeX-apropos)
yuuji@22 447 (YaTeX-define-key "&" 'YaTeX-what-column)
yuuji@52 448 (YaTeX-define-key "d" 'YaTeX-display-hierarchy)
yuuji@54 449 (YaTeX-define-key "x" YaTeX-user-extensional-map)
yuuji@5 450 (YaTeX-define-key "n"
yuuji@7 451 '(lambda () (interactive) (insert "\\\\")))
yuuji@13 452 (if YaTeX-dos
yuuji@7 453 (define-key YaTeX-prefix-map "\C-r"
yuuji@0 454 '(lambda () (interactive)
yuuji@3 455 (set-screen-height YaTeX-saved-screen-height) (recenter))))
yuuji@22 456 (mapcar
yuuji@22 457 (function
yuuji@22 458 (lambda (key)
yuuji@22 459 (define-key YaTeX-mode-map (car key) 'YaTeX-math-insert-sequence)))
yuuji@69 460 YaTeX-math-key-list))
yuuji@0 461
yuuji@49 462 (defvar YaTeX-section-completion-map nil
yuuji@22 463 "*Key map used at YaTeX completion in the minibuffer.")
yuuji@49 464 (if YaTeX-section-completion-map nil
yuuji@49 465 (setq YaTeX-section-completion-map
yuuji@22 466 (copy-keymap (or (and (boundp 'gmhist-completion-map)
yuuji@22 467 gmhist-completion-map)
yuuji@22 468 minibuffer-local-completion-map)))
yuuji@49 469 (define-key YaTeX-section-completion-map
yuuji@22 470 " " 'YaTeX-minibuffer-complete)
yuuji@49 471 (define-key YaTeX-section-completion-map
yuuji@22 472 "\C-i" 'YaTeX-minibuffer-complete)
yuuji@49 473 (define-key YaTeX-section-completion-map
yuuji@22 474 "\C-v" 'YaTeX-read-section-with-overview))
yuuji@22 475
yuuji@14 476 (defvar YaTeX-recursive-map nil
yuuji@22 477 "*Key map used at YaTeX reading arguments in the minibuffer.")
yuuji@14 478 (if YaTeX-recursive-map nil
yuuji@14 479 (setq YaTeX-recursive-map (copy-keymap global-map))
yuuji@14 480 (define-key YaTeX-recursive-map YaTeX-prefix YaTeX-prefix-map))
yuuji@14 481
yuuji@0 482 ;---------- Define other variable ----------
yuuji@22 483 (defvar env-name "document" "*Initial tex-environment completion")
yuuji@22 484 (defvar section-name "documentstyle" "*Initial tex-section completion")
yuuji@22 485 (defvar fontsize-name "large" "*Initial fontsize completion")
yuuji@22 486 (defvar single-command "maketitle" "*Initial LaTeX single command")
yuuji@13 487 (defvar YaTeX-kanji-code (if YaTeX-dos 1 2)
yuuji@69 488 "*File kanji code used by Japanese TeX.")
yuuji@69 489
yuuji@22 490 (defvar YaTeX-coding-system nil "File coding system used by Japanese TeX.")
yuuji@5 491 (cond
yuuji@64 492 (YaTeX-emacs-20
yuuji@64 493 (setq YaTeX-coding-system
yuuji@64 494 (cdr (assoc YaTeX-kanji-code YaTeX-kanji-code-alist))))
yuuji@5 495 ((boundp 'MULE)
yuuji@22 496 (setq YaTeX-coding-system
yuuji@64 497 (symbol-value (cdr (assoc YaTeX-kanji-code YaTeX-kanji-code-alist)))))
yuuji@5 498 ((boundp 'NEMACS)
yuuji@64 499 (setq YaTeX-latex-message-code latex-message-kanji-code)))
yuuji@64 500
yuuji@64 501 (defvar YaTeX-mode-syntax-table nil
yuuji@64 502 "*Syntax table for yatex-mode")
yuuji@64 503
yuuji@64 504 (if YaTeX-mode-syntax-table nil
yuuji@64 505 (setq YaTeX-mode-syntax-table (make-syntax-table (standard-syntax-table)))
yuuji@64 506 (modify-syntax-entry ?\n " " YaTeX-mode-syntax-table)
yuuji@68 507 (modify-syntax-entry ?\{ "(}" YaTeX-mode-syntax-table)
yuuji@70 508 (modify-syntax-entry ?\} "){" YaTeX-mode-syntax-table)
yuuji@70 509 (modify-syntax-entry ?\t " " YaTeX-mode-syntax-table)
yuuji@70 510 (modify-syntax-entry ?\f ">" YaTeX-mode-syntax-table)
yuuji@70 511 (modify-syntax-entry ?\n ">" YaTeX-mode-syntax-table)
yuuji@70 512 (modify-syntax-entry ?$ "$$" YaTeX-mode-syntax-table)
yuuji@70 513 (modify-syntax-entry ?% "<" YaTeX-mode-syntax-table)
yuuji@70 514 (modify-syntax-entry ?\\ "/" YaTeX-mode-syntax-table)
yuuji@70 515 (modify-syntax-entry ?~ " " YaTeX-mode-syntax-table))
yuuji@52 516
yuuji@14 517 ;---------- Provide YaTeX-mode ----------
yuuji@0 518 ;;;
yuuji@0 519 ;; Major mode definition
yuuji@0 520 ;;;
yuuji@0 521 (defun yatex-mode ()
yuuji@7 522 " Yet Another LaTeX mode: Major mode for editing input files of LaTeX.
yuuji@13 523 -You can invoke processes concerning LaTeX typesetting by
yuuji@7 524 \\[YaTeX-typeset-menu]
yuuji@22 525 -Complete LaTeX environment form of `\\begin{env} ... \\end{env}' by
yuuji@7 526 \\[YaTeX-make-begin-end]
yuuji@7 527 -Enclose region into some environment by
yuuji@7 528 \\[universal-argument] \\[YaTeX-make-begin-end]
yuuji@7 529 -Complete LaTeX command which takes argument like `\\section{}' by
yuuji@7 530 \\[YaTeX-make-section]
yuuji@7 531 -Put LaTeX command which takes no arguments like `\\maketitle' by
yuuji@7 532 \\[YaTeX-make-singlecmd]
yuuji@7 533 -Complete font or character size descriptor like `{\\large }' by
yuuji@7 534 \\[YaTeX-make-fontsize]
yuuji@22 535 -Enclose region into those descriptors above by
yuuji@7 536 \\[universal-argument] \\[YaTeX-make-fontsize]
yuuji@54 537 -Enter European accent notations by
yuuji@7 538 \\[YaTeX-make-accent]
yuuji@13 539 -Toggle various modes of YaTeX by
yuuji@13 540 \\[YaTeX-switch-mode-menu]
yuuji@22 541 -Change environt name (on the begin/end line) by
yuuji@22 542 \\[YaTeX-change-*]
yuuji@22 543 -Kill LaTeX command/environment sequences by
yuuji@22 544 \\[YaTeX-kill-*]
yuuji@22 545 -Kill LaTeX command/environment with its contents
yuuji@22 546 \\[universal-argument] \\[YaTeX-kill-*]
yuuji@22 547 -Go to corresponding object (begin/end, file, labels) by
yuuji@54 548 \\[YaTeX-goto-corresponding-*] or
yuuji@54 549 \\[YaTeX-goto-corresponding-*-other-window] (in other window)
yuuji@54 550 \\[YaTeX-goto-corresponding-*-other-frame] (in other frame)
yuuji@22 551 -Go to main LaTeX source text by
yuuji@54 552 \\[YaTeX-visit-main] or
yuuji@54 553 \\[YaTeX-visit-main-other-window] (in other window)
yuuji@54 554 \\[YaTeX-visit-main-other-frame] (in other frame)
yuuji@22 555 -Comment out or uncomment region by
yuuji@22 556 \\[YaTeX-comment-region] or \\[YaTeX-uncomment-region]
yuuji@22 557 -Comment out or uncomment paragraph by
yuuji@22 558 \\[YaTeX-comment-paragraph] or \\[YaTeX-uncomment-paragraph]
yuuji@22 559 -Make an \\item entry hang-indented by
yuuji@22 560 \\[YaTeX-fill-item]
yuuji@22 561 -Enclose the region with parentheses by
yuuji@22 562 \\[YaTeX-insert-parens-region]
yuuji@22 563 \\[YaTeX-insert-braces-region]
yuuji@22 564 \\[YaTeX-insert-brackets-region]
yuuji@22 565 \\[YaTeX-insert-dollars-region]
yuuji@22 566 -Look up the corresponding column header of tabular environment by
yuuji@22 567 \\[YaTeX-what-column]
yuuji@54 568 -Enter a newline and an entry suitable for environment by
yuuji@54 569 \\[YaTeX-intelligent-newline]
yuuji@54 570 -View the structure of file inclusion by
yuuji@54 571 \\[YaTeX-display-hierarchy]
yuuji@22 572 -Refer the online help of popular LaTeX commands by
yuuji@54 573 \\[YaTeX-help] (help)
yuuji@54 574 \\[YaTeX-apropos] (apropos)
yuuji@13 575 -Edit `%# notation' by
yuuji@13 576 \\[YaTeX-%-menu]
yuuji@7 577
yuuji@7 578 Those are enough for fastening your editing of LaTeX source. But further
yuuji@22 579 more features are available and they are documented in the manual.
yuuji@22 580 "
yuuji@0 581 (interactive)
yuuji@0 582 (kill-all-local-variables)
yuuji@7 583 (setq major-mode 'yatex-mode)
yuuji@55 584 (setq mode-name (if YaTeX-japan "やてふ" "YaTeX"))
yuuji@18 585 (mapcar 'make-local-variable
yuuji@52 586 '(dvi2-command fill-column fill-prefix
yuuji@18 587 tmp-env-table tmp-section-table tmp-fontsize-table
yuuji@52 588 tmp-singlecmd-table paragraph-start paragraph-separate
yuuji@52 589 YaTeX-math-mode indent-line-function
yuuji@58 590 comment-start comment-start-skip
yuuji@22 591 ))
yuuji@14 592 (cond ((boundp 'MULE)
yuuji@5 593 (set-file-coding-system YaTeX-coding-system))
yuuji@69 594 ((and YaTeX-emacs-20 (boundp 'buffer-file-coding-system))
yuuji@69 595 (setq buffer-file-coding-system YaTeX-coding-system))
yuuji@64 596 ((featurep 'mule)
yuuji@64 597 (set-file-coding-system YaTeX-coding-system))
yuuji@69 598 ((boundp 'NEMACS)
yuuji@5 599 (make-local-variable 'kanji-fileio-code)
yuuji@5 600 (setq kanji-fileio-code YaTeX-kanji-code)))
yuuji@5 601 (setq fill-column YaTeX-fill-column
yuuji@7 602 fill-prefix YaTeX-fill-prefix
yuuji@58 603 paragraph-start YaTeX-paragraph-start
yuuji@58 604 paragraph-separate YaTeX-paragraph-separate
yuuji@52 605 indent-line-function 'YaTeX-indent-line
yuuji@58 606 comment-start YaTeX-comment-prefix
yuuji@60 607 comment-end ""
yuuji@70 608 comment-start-skip "[^\\\\]%+[ \t]*"
yuuji@22 609 )
yuuji@0 610 (use-local-map YaTeX-mode-map)
yuuji@64 611 (set-syntax-table YaTeX-mode-syntax-table)
yuuji@13 612 (if YaTeX-dos (setq YaTeX-saved-screen-height (screen-height)))
yuuji@6 613 (YaTeX-read-user-completion-table)
yuuji@54 614 (and (fboundp 'YaTeX-19-collect-macros) (YaTeX-19-collect-macros))
yuuji@70 615 (makunbound 'inenv)
yuuji@60 616 (turn-on-auto-fill) ;1.63
yuuji@64 617 (and (= 0 (buffer-size)) (file-exists-p YaTeX-template-file)
yuuji@64 618 (y-or-n-p (format "Insert %s?" YaTeX-template-file))
yuuji@64 619 (insert-file-contents (expand-file-name YaTeX-template-file)))
yuuji@69 620 (run-hooks 'text-mode-hook 'yatex-mode-hook))
yuuji@5 621
yuuji@0 622 ;---------- Define YaTeX-mode functions ----------
yuuji@13 623 (defvar YaTeX-ec "\\" "Escape character of current mark-up language.")
yuuji@22 624 (defvar YaTeX-ec-regexp (regexp-quote YaTeX-ec))
yuuji@13 625 (defvar YaTeX-struct-begin
yuuji@13 626 (concat YaTeX-ec "begin{%1}%2")
yuuji@64 627 "Keyword format of begin-environment.")
yuuji@64 628 (defvar YaTeX-struct-end
yuuji@64 629 (concat YaTeX-ec "end{%1}")
yuuji@64 630 "Keyword format of end-environment.")
yuuji@13 631 (defvar YaTeX-struct-name-regexp "[^}]+"
yuuji@13 632 "Environment name regexp.")
yuuji@22 633 (defvar YaTeX-TeX-token-regexp
yuuji@55 634 (cond (YaTeX-japan "[A-Za-z*あ-ん亜-龠]+")
yuuji@22 635 (t "[A-Za-z*]+"))
yuuji@22 636 "Regexp of characters which can be a member of TeX command's name.")
yuuji@32 637 (defvar YaTeX-command-token-regexp YaTeX-TeX-token-regexp
yuuji@51 638 "Regexp of characters which can be a member of current mark up language's command name.")
yuuji@49 639
yuuji@13 640 ;;(defvar YaTeX-struct-section
yuuji@13 641 ;; (concat YaTeX-ec "%1{%2}")
yuuji@13 642 ;; "Keyword to make section.")
yuuji@13 643
yuuji@0 644 ;;;
yuuji@22 645 ;; autoload section
yuuji@22 646 ;;;
yuuji@22 647
yuuji@22 648 ;;autoload from yatexprc.el
yuuji@22 649 (autoload 'YaTeX-visit-main "yatexprc" "Visit main LaTeX file." t)
yuuji@22 650 (autoload 'YaTeX-visit-main-other-window "yatexprc"
yuuji@52 651 "Visit main other window." t)
yuuji@53 652 (autoload 'YaTeX-main-file-p "yatexprc" "Check if the file is main." t)
yuuji@51 653 (autoload 'YaTeX-get-builtin "yatexprc" "Get %# built-in." t)
yuuji@59 654 (autoload 'YaTeX-system "yatexprc" "Call system command" t)
yuuji@60 655 (autoload 'YaTeX-save-buffers "yatexprc" "Save buffers of same major mode" t)
yuuji@22 656
yuuji@22 657 ;;autoload from yatexmth.el
yuuji@22 658 (autoload 'YaTeX-math-insert-sequence "yatexmth" "Image input." t)
yuuji@22 659 (autoload 'YaTeX-in-math-mode-p "yatexmth" "Check if in math-env." t)
yuuji@22 660 (autoload 'YaTeX-toggle-math-mode "yatexmth" "YaTeX math-mode interfaces." t)
yuuji@52 661 (autoload 'YaTeX-math-member-p "yatexmth" "Check if a word is math command." t)
yuuji@69 662 (autoload 'YaTeX-insert-amsparens-region "yatexmth" "AMS parens region" t)
yuuji@69 663 (autoload 'YaTeX-insert-amsbraces-region "yatexmth" "AMS braces region" t)
yuuji@69 664 (autoload 'YaTeX-insert-amsbrackets-region "yatexmth" "AMS brackets region" t)
yuuji@69 665 (autoload 'YaTeX-on-parenthesis-p "yatexmth" "Check if on math-parens" t)
yuuji@69 666 (autoload 'YaTeX-goto-open-paren "yatexmth" "Goto opening paren" t)
yuuji@69 667 (autoload 'YaTeX-change-parentheses "yatexmth" "Change corresponding parens" t)
yuuji@22 668
yuuji@22 669 ;;autoload from yatexhlp.el
yuuji@22 670 (autoload 'YaTeX-help "yatexhlp" "YaTeX helper with LaTeX commands." t)
yuuji@22 671 (autoload 'YaTeX-apropos "yatexhlp" "Apropos for (La)TeX commands." t)
yuuji@22 672
yuuji@22 673 ;;autoload from yatexgen.el
yuuji@22 674 (autoload 'YaTeX-generate "yatexgen" "YaTeX add-in function generator." t)
yuuji@22 675 (autoload 'YaTeX-generate-simple "yatexgen" "YaTeX add-in support." t)
yuuji@22 676
yuuji@22 677 ;;autoload from yatexsec.el
yuuji@69 678 (autoload 'YaTeX-section-overview "yatexsec" "YaTeX sectioning(view)" t)
yuuji@22 679 (autoload 'YaTeX-read-section-in-minibuffer "yatexsec" "YaTeX sectioning" t)
yuuji@22 680 (autoload 'YaTeX-make-section-with-overview "yatexsec" "YaTeX sectioning" t)
yuuji@22 681
yuuji@22 682 ;;autoload from yatexenv.el
yuuji@22 683 (autoload 'YaTeX-what-column "yatexenv" "YaTeX env. specific funcs" t)
yuuji@22 684 (autoload 'YaTeX-intelligent-newline "yatexenv" "YaTeX env. specific funcs" t)
yuuji@53 685 (autoload 'YaTeX-indent-line-equation "yatexenv" "Indent equation lines." t)
yuuji@53 686 (autoload 'YaTeX-goto-corresponding-leftright "yatexenv" "\left\right jumps" t)
yuuji@22 687
yuuji@52 688 ;;autoload from yatexhie.el
yuuji@52 689 (autoload 'YaTeX-display-hierarchy "yatexhie"
yuuji@52 690 "YaTeX document hierarchy browser" t)
yuuji@61 691 (autoload 'YaTeX-display-hierarchy-directly "yatexhie"
yuuji@61 692 "Same as YaTeX-display-hierarchy. Call from mouse." t)
yuuji@52 693
yuuji@60 694
yuuji@22 695 ;;;
yuuji@0 696 ;; YaTeX-mode functions
yuuji@0 697 ;;;
yuuji@22 698 (defun YaTeX-insert-begin-end (env region-mode)
yuuji@45 699 "Insert \\begin{mode-name} and \\end{mode-name}.
yuuji@22 700 This works also for other defined begin/end tokens to define the structure."
yuuji@7 701 (setq YaTeX-current-completion-type 'begin)
yuuji@58 702 (let*((ccol (current-column)) beg beg2 exchange
yuuji@22 703 (arg region-mode) ;for old compatibility
yuuji@58 704 (indent-column (+ ccol YaTeX-environment-indent))(i 1) func)
yuuji@22 705 (if (and region-mode (> (point) (mark)))
yuuji@16 706 (progn (exchange-point-and-mark)
yuuji@16 707 (setq exchange t
yuuji@16 708 ccol (current-column)
yuuji@16 709 indent-column (+ ccol YaTeX-environment-indent))))
yuuji@16 710 ;;VER2 (insert "\\begin{" env "}" (YaTeX-addin env))
yuuji@16 711 (setq beg (point))
yuuji@16 712 (YaTeX-insert-struc 'begin env)
yuuji@58 713 (setq beg2 (point))
yuuji@16 714 (insert "\n")
yuuji@16 715 (indent-to indent-column)
yuuji@16 716 (save-excursion
yuuji@16 717 ;;indent optional argument of \begin{env}, if any
yuuji@16 718 (while (> (point-beginning-of-line) beg)
yuuji@16 719 (skip-chars-forward "\\s " (point-end-of-line))
yuuji@16 720 (indent-to indent-column)
yuuji@16 721 (forward-line -1)))
yuuji@58 722 (require 'yatexenv)
yuuji@22 723 (if region-mode
yuuji@16 724 ;;if region-mode, indent all text in the region
yuuji@7 725 (save-excursion
yuuji@53 726 (if (fboundp (intern-soft (concat "YaTeX-enclose-" env)))
yuuji@53 727 (funcall (intern-soft (concat "YaTeX-enclose-" env))
yuuji@53 728 (point) (mark))
yuuji@53 729 (while (< (progn (forward-line 1) (point)) (mark))
yuuji@53 730 (if (eolp) nil
yuuji@53 731 (skip-chars-forward " \t\n")
yuuji@58 732 (indent-to indent-column))))))
yuuji@22 733 (if region-mode (exchange-point-and-mark))
yuuji@16 734 (indent-to ccol)
yuuji@16 735 ;;VER2 (insert "\\end{" env "}\n")
yuuji@16 736 (YaTeX-insert-struc 'end env)
yuuji@58 737 (YaTeX-reindent ccol)
yuuji@22 738 (if region-mode
yuuji@16 739 (progn
yuuji@54 740 (insert "\n")
yuuji@16 741 (or exchange (exchange-point-and-mark)))
yuuji@58 742 (goto-char beg2)
yuuji@64 743 (YaTeX-intelligent-newline nil)
yuuji@64 744 (YaTeX-indent-line))
yuuji@5 745 (if YaTeX-current-position-register
yuuji@69 746 (point-to-register YaTeX-current-position-register))))
yuuji@0 747
yuuji@0 748 (defun YaTeX-make-begin-end (arg)
yuuji@0 749 "Make LaTeX environment command of \\begin{env.} ... \\end{env.}
yuuji@0 750 by completing read.
yuuji@0 751 If you invoke this command with universal argument,
yuuji@5 752 \(key binding for universal-argument is \\[universal-argument]\)
yuuji@0 753 you can put REGION into that environment between \\begin and \\end."
yuuji@0 754 (interactive "P")
yuuji@0 755 (let*
yuuji@0 756 ((mode (if arg " region" ""))
yuuji@0 757 (env
yuuji@5 758 (YaTeX-read-environment
yuuji@5 759 (format "Begin environment%s(default %s): " mode env-name))))
yuuji@0 760 (if (string= env "")
yuuji@0 761 (setq env env-name))
yuuji@0 762 (setq env-name env)
yuuji@7 763 (YaTeX-update-table
yuuji@7 764 (list env-name) 'env-table 'user-env-table 'tmp-env-table)
yuuji@69 765 (YaTeX-insert-begin-end env-name arg)))
yuuji@0 766
yuuji@0 767 (defun YaTeX-make-begin-end-region ()
yuuji@0 768 "Call YaTeX-make-begin-end with ARG to specify region mode."
yuuji@0 769 (interactive)
yuuji@69 770 (YaTeX-make-begin-end t))
yuuji@0 771
yuuji@46 772 (defun YaTeX-make-section (arg &optional beg end cmd)
yuuji@0 773 "Make LaTeX \\section{} type command with completing read.
yuuji@13 774 With numeric ARG, you can specify the number of arguments of
yuuji@0 775 LaTeX command.
yuuji@0 776 For example, if you want to produce LaTeX command
yuuji@0 777
yuuji@0 778 \\addtolength{\\topmargin}{8mm}
yuuji@0 779
yuuji@5 780 which has two arguments. You can produce that sequence by typing...
yuuji@0 781 ESC 2 C-c s add SPC RET \\topm SPC RET 8mm RET
yuuji@0 782 \(by default\)
yuuji@13 783 Then yatex will automatically complete `addtolength' with two arguments
yuuji@6 784 next time.
yuuji@14 785 You can complete symbol at LaTeX command and the 1st argument.
yuuji@14 786
yuuji@14 787 If the optional 2nd and 3rd argument BEG END are specified, enclose
yuuji@46 788 the region from BEG to END into the first argument of the LaTeX sequence.
yuuji@46 789 Optional 4th arg CMD is LaTeX command name, for non-interactive use."
yuuji@6 790 (interactive "P")
yuuji@7 791 (setq YaTeX-current-completion-type 'section)
yuuji@68 792 (if (equal arg '(4)) (setq beg (region-beginning) end (region-end)))
yuuji@14 793 (unwind-protect
yuuji@14 794 (let*
yuuji@22 795 ((source-window (selected-window))
yuuji@22 796 (section
yuuji@46 797 (or cmd
yuuji@46 798 (YaTeX-read-section
yuuji@54 799 (if YaTeX-simple-messages
yuuji@54 800 (format "Section-type (default %s): " section-name)
yuuji@54 801 (if (> (minibuffer-depth) 0)
yuuji@54 802 (format "%s???{} (default %s)%s: " YaTeX-ec section-name
yuuji@54 803 (format "[level:%d]" (minibuffer-depth)))
yuuji@68 804 (format "(C-v for view-section) %s???{%s} (default %s): "
yuuji@68 805 YaTeX-ec (if beg "region" "") section-name)))
yuuji@46 806 nil)))
yuuji@14 807 (section (if (string= section "") section-name section))
yuuji@14 808 (numarg ;; The number of section-type command's argument
yuuji@68 809 (or (and (numberp arg) arg)
yuuji@51 810 (nth 1 (YaTeX-lookup-table section 'section))
yuuji@14 811 1))
yuuji@14 812 (arg-reader (intern-soft (concat "YaTeX::" section)))
yuuji@14 813 (addin-args (and arg-reader (fboundp arg-reader)))
yuuji@14 814 (title "")
yuuji@54 815 (j 1)
yuuji@14 816 (enable-recursive-minibuffers t));;let
yuuji@14 817 (setq section-name section)
yuuji@14 818 (if beg
yuuji@14 819 (let ((e (make-marker)))
yuuji@14 820 (goto-char end)
yuuji@14 821 (insert "}")
yuuji@14 822 (set-marker e (point))
yuuji@14 823 (goto-char beg)
yuuji@14 824 (insert YaTeX-ec section-name "{")
yuuji@68 825 (goto-char e)
yuuji@68 826 (set-marker e nil))
yuuji@14 827 (use-global-map YaTeX-recursive-map)
yuuji@54 828 (if (= numarg 0) (YaTeX-make-singlecmd section-name)
yuuji@64 829 (progn (insert YaTeX-ec section-name)
yuuji@64 830 (insert (YaTeX-addin section-name))))
yuuji@54 831 (while (<= j numarg)
yuuji@54 832 (insert
yuuji@54 833 "{"
yuuji@54 834 (setq title
yuuji@54 835 (cond
yuuji@54 836 (addin-args (funcall arg-reader j))
yuuji@54 837 (YaTeX-skip-default-reader "")
yuuji@54 838 (t
yuuji@54 839 (read-string (format "Argument %d of %s: " j section)))))
yuuji@54 840 "}")
yuuji@54 841 (setq j (1+ j))))
yuuji@14 842 (YaTeX-update-table
yuuji@14 843 (if (/= numarg 1) (list section numarg)
yuuji@14 844 (list section))
yuuji@14 845 'section-table 'user-section-table 'tmp-section-table)
yuuji@14 846 (if YaTeX-current-position-register
yuuji@14 847 (point-to-register YaTeX-current-position-register))
yuuji@14 848 (if (string= (buffer-substring (- (point) 2) (point)) "{}")
yuuji@54 849 (forward-char -1))
yuuji@54 850 (while (string= (buffer-substring (- (point) 3) (1- (point))) "{}")
yuuji@54 851 (forward-char -2)))
yuuji@69 852 (if (<= (minibuffer-depth) 0) (use-global-map global-map))))
yuuji@0 853
yuuji@14 854 (defun YaTeX-make-section-region (args beg end)
yuuji@14 855 "Call YaTeX-make-section with arguments to specify region mode."
yuuji@14 856 (interactive "P\nr")
yuuji@69 857 (YaTeX-make-section args beg end))
yuuji@0 858
yuuji@54 859 (defun YaTeX-make-fontsize (arg &optional fontsize)
yuuji@0 860 "Make completion like {\\large ...} or {\\slant ...} in minibuffer.
yuuji@0 861 If you invoke this command with universal argument, you can put region
yuuji@0 862 into {\\xxx } braces.
yuuji@5 863 \(key binding for universal-argument is \\[universal-argument]\)"
yuuji@0 864 (interactive "P")
yuuji@49 865 (YaTeX-sync-local-table 'tmp-fontsize-table)
yuuji@0 866 (let* ((mode (if arg "region" ""))
yuuji@0 867 (fontsize
yuuji@54 868 (or fontsize
yuuji@54 869 (YaTeX-read-fontsize
yuuji@54 870 (if YaTeX-simple-messages
yuuji@54 871 (format "Font or size (default %s): " fontsize-name)
yuuji@54 872 (format "{\\??? %s} (default %s)%s: " mode fontsize-name
yuuji@54 873 (if (> (minibuffer-depth) 0)
yuuji@54 874 (format "[level:%d]" (minibuffer-depth)) "")))
yuuji@54 875 nil nil))))
yuuji@0 876 (if (string= fontsize "")
yuuji@0 877 (setq fontsize fontsize-name))
yuuji@68 878 (setq YaTeX-current-completion-type 'large)
yuuji@0 879 (setq fontsize-name fontsize)
yuuji@6 880 (YaTeX-update-table
yuuji@6 881 (list fontsize-name)
yuuji@6 882 'fontsize-table 'user-fontsize-table 'tmp-fontsize-table)
yuuji@70 883 (and YaTeX-use-LaTeX2e
yuuji@70 884 (YaTeX-latex2e-p)
yuuji@70 885 (setq fontsize (cdr (assoc fontsize-name LaTeX2e-fontstyle-alist)))
yuuji@70 886 (setq fontsize-name fontsize))
yuuji@0 887 (if arg
yuuji@0 888 (save-excursion
yuuji@0 889 (if (> (point) (mark)) (exchange-point-and-mark))
yuuji@0 890 (insert "{\\" fontsize-name " ")
yuuji@0 891 (exchange-point-and-mark)
yuuji@0 892 (insert "}"))
yuuji@68 893 (insert "{\\" fontsize-name " ")
yuuji@5 894 (if YaTeX-current-position-register
yuuji@5 895 (point-to-register YaTeX-current-position-register))
yuuji@68 896 (save-excursion
yuuji@69 897 (insert (YaTeX-addin fontsize-name) "}")))))
yuuji@0 898
yuuji@0 899 (defun YaTeX-make-fontsize-region ()
yuuji@5 900 "Call function:YaTeX-make-fontsize with ARG to specify region mode."
yuuji@0 901 (interactive)
yuuji@69 902 (YaTeX-make-fontsize t))
yuuji@0 903
yuuji@52 904 (defvar YaTeX-singlecmd-suffix "" "*Suffix for maketitle-type commands.")
yuuji@51 905 (defvar YaTeX-read-singlecmd-history nil "Holds maketitle-type history.")
yuuji@51 906 (put 'YaTeX-read-singlecmd-history 'no-default t)
yuuji@0 907 (defun YaTeX-make-singlecmd (single)
yuuji@0 908 (interactive
yuuji@49 909 (list (YaTeX-cplread-with-learning
yuuji@54 910 (if YaTeX-simple-messages
yuuji@54 911 (format "maketitle-type (default %s): " single-command)
yuuji@54 912 (format "%s??? (default %s)%s: " YaTeX-ec single-command
yuuji@54 913 (if (> (minibuffer-depth) 0)
yuuji@54 914 (format "[level:%d]" (minibuffer-depth)) "")))
yuuji@49 915 'singlecmd-table 'user-singlecmd-table 'tmp-singlecmd-table
yuuji@51 916 nil nil nil 'YaTeX-read-singlecmd-history)))
yuuji@0 917 (if (string= single "")
yuuji@0 918 (setq single single-command))
yuuji@0 919 (setq single-command single)
yuuji@7 920 (setq YaTeX-current-completion-type 'maketitle)
yuuji@54 921 (let ((dollar (and (not (YaTeX-in-math-mode-p))
yuuji@54 922 (YaTeX-math-member-p single-command)))
yuuji@54 923 p q)
yuuji@54 924 (if dollar (insert "$"))
yuuji@52 925 (insert YaTeX-ec single-command)
yuuji@52 926 (setq p (point))
yuuji@52 927 (insert (YaTeX-addin single) YaTeX-singlecmd-suffix)
yuuji@54 928 (if dollar (insert "$"))
yuuji@52 929 (setq q (point))
yuuji@52 930 (goto-char p)
yuuji@52 931 (forward-char -2)
yuuji@52 932 (if (looking-at "\\[\\]") (forward-char 1) (goto-char q)))
yuuji@5 933 (if YaTeX-current-position-register
yuuji@69 934 (point-to-register YaTeX-current-position-register)))
yuuji@0 935
yuuji@0 936 (defvar YaTeX-completion-begin-regexp "[{\\]"
yuuji@51 937 "Regular expression of limit where LaTeX command's completion begins.")
yuuji@0 938
yuuji@0 939 (defun YaTeX-do-completion ()
yuuji@0 940 "Try completion on LaTeX command preceding point."
yuuji@0 941 (interactive)
yuuji@0 942 (if
yuuji@0 943 (or (eq (preceding-char) ? )
yuuji@0 944 (eq (preceding-char) ?\t)
yuuji@0 945 (eq (preceding-char) ?\n)
yuuji@0 946 (bobp))
yuuji@0 947 (message "Nothing to complete.") ;Do not complete
yuuji@0 948 (let* ((end (point))
yuuji@6 949 (limit (point-beginning-of-line))
yuuji@0 950 (completion-begin
yuuji@6 951 (progn (re-search-backward "[ \t\n]" limit 1) (point)))
yuuji@0 952 (begin (progn
yuuji@0 953 (goto-char end)
yuuji@0 954 (if (re-search-backward YaTeX-completion-begin-regexp
yuuji@0 955 completion-begin t)
yuuji@0 956 (1+ (point))
yuuji@0 957 nil))))
yuuji@0 958 (goto-char end)
yuuji@0 959 (cond
yuuji@0 960 ((null begin)
yuuji@13 961 (message "I think it is not a LaTeX sequence."))
yuuji@0 962 (t
yuuji@49 963 (mapcar 'YaTeX-sync-local-table
yuuji@18 964 '(tmp-section-table tmp-env-table tmp-singlecmd-table))
yuuji@6 965 (let*((pattern (buffer-substring begin end))
yuuji@6 966 (all-table
yuuji@6 967 (append
yuuji@6 968 section-table user-section-table tmp-section-table
yuuji@6 969 env-table user-env-table tmp-env-table
yuuji@6 970 singlecmd-table user-singlecmd-table tmp-singlecmd-table))
yuuji@6 971 ;; First,
yuuji@6 972 ;; search completion without backslash.
yuuji@6 973 (completion (try-completion pattern all-table)))
yuuji@0 974 (if
yuuji@0 975 (eq completion nil)
yuuji@0 976 ;; Next,
yuuji@0 977 ;; search completion with backslash
yuuji@0 978 (setq completion
yuuji@0 979 (try-completion (buffer-substring (1- begin) end)
yuuji@0 980 all-table nil)
yuuji@0 981 begin (1- begin)))
yuuji@0 982 (cond
yuuji@0 983 ((null completion)
yuuji@0 984 (message (concat "Can't find completion for '" pattern "'"))
yuuji@0 985 (ding))
yuuji@0 986 ((eq completion t) (message "Sole completion."))
yuuji@0 987 ((not (string= completion pattern))
yuuji@0 988 (kill-region begin end)
yuuji@0 989 (insert completion)
yuuji@0 990 )
yuuji@0 991 (t
yuuji@0 992 (message "Making completion list...")
yuuji@0 993 (with-output-to-temp-buffer "*Help*"
yuuji@0 994 (display-completion-list
yuuji@69 995 (all-completions pattern all-table)))))))))))
yuuji@0 996
yuuji@12 997 (defun YaTeX-toggle-modify-mode (&optional arg)
yuuji@12 998 (interactive "P")
yuuji@12 999 (or (memq 'YaTeX-modify-mode mode-line-format)
yuuji@12 1000 (setq mode-line-format
yuuji@12 1001 (append (list "" 'YaTeX-modify-mode) mode-line-format)))
yuuji@11 1002 (if (or arg (null YaTeX-modify-mode))
yuuji@11 1003 (progn
yuuji@12 1004 (setq YaTeX-modify-mode "*m*")
yuuji@11 1005 (message "Modify mode"))
yuuji@11 1006 (setq YaTeX-modify-mode nil)
yuuji@11 1007 (message "Cancel modify mode."))
yuuji@69 1008 (set-buffer-modified-p (buffer-modified-p))) ;redraw mode-line
yuuji@11 1009
yuuji@46 1010 (defun YaTeX-switch-mode-menu (arg &optional char)
yuuji@11 1011 (interactive "P")
yuuji@22 1012 (message "Toggle: (M)odify-mode ma(T)h-mode")
yuuji@46 1013 (let ((c (or char (read-char))))
yuuji@11 1014 (cond
yuuji@11 1015 ((= c ?m) (YaTeX-toggle-modify-mode arg))
yuuji@22 1016 ((or (= c ?$) (= c ?t))
yuuji@22 1017 (if YaTeX-auto-math-mode
yuuji@22 1018 (message "Makes no sense in YaTeX-auto-math-mode.")
yuuji@69 1019 (YaTeX-toggle-math-mode arg))))))
yuuji@11 1020
yuuji@0 1021 (defun YaTeX-insert-quote ()
yuuji@0 1022 (interactive)
yuuji@0 1023 (insert
yuuji@0 1024 (cond
yuuji@49 1025 ((YaTeX-literal-p) ?\")
yuuji@0 1026 ((= (preceding-char) ?\\ ) ?\")
yuuji@57 1027 ;((= (preceding-char) ?\( ) ?\")
yuuji@22 1028 ((or (= (preceding-char) 32)
yuuji@22 1029 (= (preceding-char) 9)
yuuji@22 1030 (= (preceding-char) ?\n)
yuuji@22 1031 (bobp)
yuuji@22 1032 (string-match
yuuji@57 1033 (regexp-quote (char-to-string (preceding-char)))
yuuji@57 1034 "、。,.?!「」『』【】()"))
yuuji@22 1035 "``")
yuuji@69 1036 (t "''"))))
yuuji@22 1037
yuuji@22 1038 (defun YaTeX-closable-p ()
yuuji@22 1039 (and (not YaTeX-modify-mode)
yuuji@54 1040 (not (eq YaTeX-close-paren-always 'never))
yuuji@22 1041 (or YaTeX-close-paren-always (eolp))
yuuji@22 1042 (not (input-pending-p))
yuuji@49 1043 (not (YaTeX-literal-p)))
yuuji@22 1044 ;;(or YaTeX-modify-mode
yuuji@22 1045 ;; (and (not YaTeX-close-paren-always) (not (eolp)))
yuuji@22 1046 ;; (input-pending-p)
yuuji@22 1047 ;; (YaTeX-quick-in-environment-p "verbatim"))
yuuji@69 1048 )
yuuji@0 1049
yuuji@5 1050 (defun YaTeX-insert-braces-region (beg end &optional open close)
yuuji@0 1051 (interactive "r")
yuuji@0 1052 (save-excursion
yuuji@0 1053 (goto-char end)
yuuji@5 1054 (insert (or close "}"))
yuuji@0 1055 (goto-char beg)
yuuji@69 1056 (insert (or open "{"))))
yuuji@0 1057
yuuji@49 1058 (defun YaTeX-insert-braces (arg &optional open close)
yuuji@49 1059 (interactive "p")
yuuji@22 1060 (let (env)
yuuji@22 1061 (cond
yuuji@51 1062 ((YaTeX-jmode) (YaTeX-self-insert arg))
yuuji@49 1063 ((not (YaTeX-closable-p)) (YaTeX-self-insert arg))
yuuji@53 1064 ((save-excursion
yuuji@59 1065 (and (> (- (point) (point-min)) 6)
yuuji@59 1066 (condition-case () (forward-char -6) (error nil)))
yuuji@53 1067 (looking-at "\\\\left\\\\"))
yuuji@53 1068 (insert "{\\right\\}")
yuuji@53 1069 (forward-char -8))
yuuji@69 1070 ((save-excursion ;from matsu@math.s.chiba-u.ac.jp
yuuji@69 1071 (and (> (- (point) (point-min)) 6) (forward-char -6))
yuuji@69 1072 (looking-at "\\\\[bB]igl\\\\"))
yuuji@69 1073 (insert
yuuji@69 1074 (concat
yuuji@69 1075 "{" (buffer-substring (match-beginning 0) (- (match-end 0) 2)) "r\\}"))
yuuji@69 1076 (forward-char -7))
yuuji@69 1077 ((save-excursion
yuuji@69 1078 (and (> (- (point) (point-min)) 7)
yuuji@69 1079 (condition-case () (forward-char -7) (error nil)))
yuuji@69 1080 (looking-at "\\\\[bB]iggl\\\\"))
yuuji@69 1081 (insert
yuuji@69 1082 (concat
yuuji@69 1083 "{" (buffer-substring (match-beginning 0) (- (match-end 0) 2)) "r\\}"))
yuuji@69 1084 (forward-char -8))
yuuji@69 1085 ((= (preceding-char) ?\\ )
yuuji@69 1086 (insert "{\\}")
yuuji@69 1087 (forward-char -2)) ;matsu's hack ends here
yuuji@48 1088 ((and (> (point) (+ (point-min) 4))
yuuji@48 1089 (save-excursion (backward-char 4) (looking-at "\\\\end"))
yuuji@49 1090 (not (YaTeX-literal-p))
yuuji@22 1091 (setq env (YaTeX-inner-environment)))
yuuji@22 1092 (momentary-string-display
yuuji@22 1093 (concat
yuuji@48 1094 "{"
yuuji@22 1095 (cond
yuuji@22 1096 (YaTeX-japan
yuuji@55 1097 (format "今度からはちゃんと %s b を使いましょう" YaTeX-prefix))
yuuji@58 1098 (t (format "You don't understand Zen of `%s b':p" YaTeX-prefix)))
yuuji@22 1099 "}")
yuuji@22 1100 (point))
yuuji@47 1101 (insert (or open "{") env (or close "}")))
yuuji@22 1102 (t
yuuji@47 1103 (insert (or open "{") (or close "}"))
yuuji@60 1104 (forward-char -1)
yuuji@60 1105 (if (and (eq (char-after (point)) ?\})
yuuji@60 1106 (eq (char-after (- (point) 2)) ?\\ ))
yuuji@60 1107 (progn (insert "\\") (forward-char -1)))
yuuji@69 1108 ))))
yuuji@22 1109
yuuji@22 1110 (defun YaTeX-jmode ()
yuuji@22 1111 (or (and (boundp 'canna:*japanese-mode*) canna:*japanese-mode*)
yuuji@69 1112 (and (boundp 'egg:*mode-on*) egg:*mode-on* egg:*input-mode*)))
yuuji@52 1113
yuuji@59 1114 (defun YaTeX-jmode-off ()
yuuji@59 1115 (cond
yuuji@59 1116 ((and (boundp 'canna:*japanese-mode*) canna:*japanese-mode*)
yuuji@59 1117 (canna-toggle-japanese-mode))
yuuji@59 1118 ((and (boundp 'egg:*mode-on*) egg:*mode-on* egg:*input-mode*)
yuuji@59 1119 (egg:toggle-egg-mode-on-off))
yuuji@60 1120 ((and (fboundp 'skk-mode) (boundp 'skk-mode) skk-mode)
yuuji@69 1121 (cond
yuuji@69 1122 ((fboundp 'skk-latin-mode) (skk-latin-mode t))
yuuji@69 1123 ((fboundp 'skk-mode-off) (skk-mode-off))
yuuji@69 1124 (t (j-mode-off))))
yuuji@69 1125 ((and (fboundp 'fep-force-off) (fep-force-off)))))
yuuji@59 1126
yuuji@22 1127 (defun YaTeX-self-insert (arg)
yuuji@52 1128 (call-interactively (global-key-binding (char-to-string last-command-char))))
yuuji@52 1129
yuuji@22 1130 (defun YaTeX-insert-brackets (arg)
yuuji@22 1131 "Insert Kagi-kakko or \\ [ \\] pair or simply \[."
yuuji@22 1132 (interactive "p")
yuuji@22 1133 (let ((col (1- (current-column))))
yuuji@22 1134 (cond
yuuji@22 1135 ((YaTeX-jmode) (YaTeX-self-insert arg))
yuuji@22 1136 ((not (YaTeX-closable-p))
yuuji@22 1137 (YaTeX-self-insert arg))
yuuji@53 1138 ((save-excursion
yuuji@53 1139 (and (> (- (point) (point-min)) 5) (forward-char -5))
yuuji@53 1140 (looking-at "\\\\left"))
yuuji@53 1141 (insert "[\\right]")
yuuji@53 1142 (forward-char -7))
yuuji@69 1143 ((save-excursion ;from matsu@math.s.chiba-u.ac.jp
yuuji@69 1144 (and (> (- (point) (point-min)) 5) (forward-char -5))
yuuji@69 1145 (looking-at "\\\\[bB]igl"))
yuuji@69 1146 (insert
yuuji@69 1147 (concat
yuuji@69 1148 "[" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r]"))
yuuji@69 1149 (forward-char -6))
yuuji@69 1150 ((save-excursion
yuuji@69 1151 (and (> (- (point) (point-min)) 6) (forward-char -6))
yuuji@69 1152 (looking-at "\\\\[bB]iggl"))
yuuji@69 1153 (insert
yuuji@69 1154 (concat
yuuji@69 1155 "[" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r]"))
yuuji@69 1156 (forward-char -7)) ;matsu's hack ends here
yuuji@36 1157 ((and (= (preceding-char) ?\\ )
yuuji@36 1158 (/= (char-after (- (point) 2)) ?\\ )
yuuji@36 1159 (not (YaTeX-in-math-mode-p)))
yuuji@22 1160 (insert last-command-char "\n")
yuuji@22 1161 (indent-to (max 0 col))
yuuji@22 1162 (insert "\\]")
yuuji@22 1163 (beginning-of-line)
yuuji@22 1164 (open-line 1)
yuuji@53 1165 (delete-region (point) (progn (beginning-of-line) (point)))
yuuji@22 1166 (indent-to (+ YaTeX-environment-indent (max 0 col)))
yuuji@53 1167 (or YaTeX-auto-math-mode YaTeX-math-mode (YaTeX-toggle-math-mode 1)))
yuuji@22 1168 ((YaTeX-closable-p)
yuuji@22 1169 (insert "[]")
yuuji@22 1170 (backward-char 1))
yuuji@69 1171 (t (YaTeX-self-insert arg)))))
yuuji@0 1172
yuuji@5 1173 (defun YaTeX-insert-brackets-region (beg end)
yuuji@5 1174 (interactive "r")
yuuji@69 1175 (YaTeX-insert-braces-region beg end "[" "]"))
yuuji@5 1176
yuuji@22 1177 (defun YaTeX-insert-parens (arg)
yuuji@22 1178 "Insert parenthesis pair."
yuuji@22 1179 (interactive "p")
yuuji@22 1180 (cond
yuuji@22 1181 ((YaTeX-jmode) (YaTeX-self-insert arg))
yuuji@22 1182 ((not (YaTeX-closable-p)) (YaTeX-self-insert arg))
yuuji@53 1183 ((save-excursion
yuuji@53 1184 (and (> (- (point) (point-min)) 5) (forward-char -5))
yuuji@53 1185 (looking-at "\\\\left"))
yuuji@53 1186 (insert "(\\right)")
yuuji@53 1187 (forward-char -7))
yuuji@69 1188 ((save-excursion ;from matsu@math.s.chiba-u.ac.jp
yuuji@69 1189 (and (> (- (point) (point-min)) 5) (forward-char -5))
yuuji@69 1190 (looking-at "\\\\[bB]igl"))
yuuji@69 1191 (insert
yuuji@69 1192 (concat
yuuji@69 1193 "(" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r)"))
yuuji@69 1194 (forward-char -6))
yuuji@69 1195 ((save-excursion
yuuji@69 1196 (and (> (- (point) (point-min)) 6) (forward-char -6))
yuuji@69 1197 (looking-at "\\\\[bB]iggl"))
yuuji@69 1198 (insert
yuuji@69 1199 (concat
yuuji@69 1200 "(" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r)"))
yuuji@69 1201 (forward-char -7))
yuuji@69 1202 ((= (preceding-char) ?\\ ) ;matsu's hack ends here
yuuji@22 1203 (insert "(\\)")
yuuji@22 1204 (backward-char 2))
yuuji@22 1205 ((YaTeX-closable-p)
yuuji@22 1206 (insert "()")
yuuji@22 1207 (backward-char 1))
yuuji@69 1208 (t (YaTeX-self-insert arg))))
yuuji@22 1209
yuuji@11 1210 (defun YaTeX-insert-parens-region (beg end)
yuuji@11 1211 (interactive "r")
yuuji@69 1212 (YaTeX-insert-braces-region beg end "(" ")"))
yuuji@69 1213
yuuji@69 1214 (defun YaTeX-insert-bar (arg)
yuuji@69 1215 "Insert bar pair."
yuuji@69 1216 (interactive "p")
yuuji@69 1217 (cond
yuuji@69 1218 ((YaTeX-jmode) (YaTeX-self-insert arg))
yuuji@69 1219 ((not (YaTeX-closable-p)) (YaTeX-self-insert arg))
yuuji@69 1220 ((save-excursion
yuuji@69 1221 (and (> (- (point) (point-min)) 5) (forward-char -5))
yuuji@69 1222 (looking-at "\\\\left"))
yuuji@69 1223 (insert "|\\right|")
yuuji@69 1224 (forward-char -7))
yuuji@69 1225 ((save-excursion ;from matsu@math.s.chiba-u.ac.jp
yuuji@69 1226 (and (> (- (point) (point-min)) 5) (forward-char -5))
yuuji@69 1227 (looking-at "\\\\[bB]igl"))
yuuji@69 1228 (insert
yuuji@69 1229 (concat
yuuji@69 1230 "|" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r|"))
yuuji@69 1231 (forward-char -6))
yuuji@69 1232 ((save-excursion
yuuji@69 1233 (and (> (- (point) (point-min)) 6) (forward-char -6))
yuuji@69 1234 (looking-at "\\\\[bB]iggl"))
yuuji@69 1235 (insert
yuuji@69 1236 (concat
yuuji@69 1237 "|" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r|"))
yuuji@69 1238 (forward-char -7))
yuuji@69 1239 ((save-excursion ; added by Jin <MAF01011@nifty.ne.jp>
yuuji@69 1240 (and (> (- (point) (point-min)) 6) (forward-char -6))
yuuji@69 1241 (looking-at "\\\\left\\\\"))
yuuji@69 1242 (insert "|\\right\\|")
yuuji@69 1243 (forward-char -8))
yuuji@69 1244 ((save-excursion
yuuji@69 1245 (and (> (- (point) (point-min)) 6) (forward-char -6))
yuuji@69 1246 (looking-at "\\\\[bB]igl\\\\"))
yuuji@69 1247 (insert
yuuji@69 1248 (concat
yuuji@69 1249 "|" (buffer-substring (match-beginning 0) (- (match-end 0) 2)) "r\\|"))
yuuji@69 1250 (forward-char -7))
yuuji@69 1251 ((save-excursion
yuuji@69 1252 (and (> (- (point) (point-min)) 7) (forward-char -7))
yuuji@69 1253 (looking-at "\\\\[bB]iggl\\\\"))
yuuji@69 1254 (insert
yuuji@69 1255 (concat
yuuji@69 1256 "|" (buffer-substring (match-beginning 0) (- (match-end 0) 2)) "r\\|"))
yuuji@69 1257 (forward-char -8)) ; added by Jin up to here.
yuuji@69 1258 ((= (preceding-char) ?\\ )
yuuji@69 1259 (insert "|\\|")
yuuji@69 1260 (backward-char 2))
yuuji@69 1261 ; ((and (YaTeX-closable-p)
yuuji@69 1262 ; (/= (preceding-char) ?|)
yuuji@69 1263 ; (/= (following-char) ?|))
yuuji@69 1264 ; (insert "||")
yuuji@69 1265 ; (backward-char 1))
yuuji@69 1266 (t (YaTeX-self-insert arg))))
yuuji@11 1267
yuuji@0 1268 (defun YaTeX-insert-dollar ()
yuuji@0 1269 (interactive)
yuuji@22 1270 (if (or (not (YaTeX-closable-p))
yuuji@57 1271 (= (preceding-char) 92)
yuuji@58 1272 (and (YaTeX-in-math-mode-p)
yuuji@58 1273 (or (/= (preceding-char) ?$) (/= (following-char) ?$))))
yuuji@7 1274 (insert "$")
yuuji@7 1275 (insert "$$")
yuuji@22 1276 (forward-char -1)
yuuji@59 1277 (YaTeX-jmode-off)
yuuji@69 1278 (or YaTeX-auto-math-mode YaTeX-math-mode (YaTeX-toggle-math-mode 1))))
yuuji@0 1279
yuuji@11 1280 (defun YaTeX-insert-dollars-region (beg end)
yuuji@11 1281 (interactive "r")
yuuji@69 1282 (YaTeX-insert-braces-region beg end "$" "$"))
yuuji@11 1283
yuuji@57 1284 (defun YaTeX-insert-amper ()
yuuji@57 1285 (interactive)
yuuji@57 1286 (if (or (string-match YaTeX-array-env-regexp
yuuji@57 1287 (or (YaTeX-inner-environment t) "document"))
yuuji@57 1288 (= (preceding-char) 92)
yuuji@57 1289 (YaTeX-literal-p))
yuuji@57 1290 (insert "&")
yuuji@69 1291 (insert "\\&")))
yuuji@57 1292
yuuji@0 1293 (defun YaTeX-version ()
yuuji@0 1294 "Return string of the version of running YaTeX."
yuuji@0 1295 (interactive)
yuuji@0 1296 (message
yuuji@5 1297 (concat "Yet Another tex-mode "
yuuji@55 1298 (if YaTeX-japan "「野鳥」" "`Wild Bird'")
yuuji@7 1299 " Revision "
yuuji@69 1300 YaTeX-revision-number)))
yuuji@0 1301
yuuji@46 1302 (defun YaTeX-typeset-menu (arg &optional char)
yuuji@46 1303 "Typeset, preview, visit error and miscellaneous convenient menu.
yuuji@46 1304 Optional second argument CHAR is for non-interactive call from menu."
yuuji@5 1305 (interactive "P")
yuuji@7 1306 (message
yuuji@58 1307 (concat "J)latex R)egion B)ibtex mk(I)ndex "
yuuji@68 1308 (if (fboundp 'start-process) "K)ill-latex ")
yuuji@58 1309 "P)review "
yuuji@58 1310 (and (boundp 'window-system) window-system "S)earch ")
yuuji@58 1311 "V)iewerr L)pr"))
yuuji@46 1312 (let ((sw (selected-window)) (c (or char (read-char))))
yuuji@32 1313 (require 'yatexprc) ;for Nemacs's bug
yuuji@32 1314 (select-window sw)
yuuji@4 1315 (cond
yuuji@5 1316 ((= c ?j) (YaTeX-typeset-buffer))
yuuji@5 1317 ((= c ?r) (YaTeX-typeset-region))
yuuji@11 1318 ((= c ?b) (YaTeX-call-command-on-file
yuuji@11 1319 bibtex-command "*YaTeX-bibtex*"))
yuuji@13 1320 ((= c ?i) (YaTeX-call-command-on-file
yuuji@11 1321 makeindex-command "*YaTeX-makeindex*"))
yuuji@7 1322 ((= c ?k) (YaTeX-kill-typeset-process YaTeX-typeset-process))
yuuji@4 1323 ((= c ?p) (call-interactively 'YaTeX-preview))
yuuji@7 1324 ((= c ?q) (YaTeX-system "lpq" "*Printer queue*"))
yuuji@4 1325 ((= c ?v) (YaTeX-view-error))
yuuji@5 1326 ((= c ?l) (YaTeX-lpr arg))
yuuji@13 1327 ((= c ?m) (YaTeX-switch-mode-menu arg))
yuuji@58 1328 ((= c ?b) (YaTeX-insert-string "\\"))
yuuji@69 1329 ((= c ?s) (YaTeX-xdvi-remote-search arg)))))
yuuji@4 1330
yuuji@46 1331 (defun YaTeX-%-menu (&optional beg end char)
yuuji@13 1332 "Operate %# notation."
yuuji@16 1333 ;;Do not use interactive"r" for the functions which require no mark
yuuji@16 1334 (interactive)
yuuji@13 1335 (message "!)Edit-%%#! B)EGIN-END-region L)Edit-%%#LPR")
yuuji@46 1336 (let ((c (or char (read-char))) (string "") key
yuuji@13 1337 (b (make-marker)) (e (make-marker)))
yuuji@13 1338 (save-excursion
yuuji@13 1339 (cond
yuuji@13 1340 ((or (= c ?!) (= c ?l)) ;Edit `%#!'
yuuji@13 1341 (goto-char (point-min))
yuuji@13 1342 (setq key (cond ((= c ?!) "%#!")
yuuji@13 1343 ((= c ?l) "%#LPR")))
yuuji@13 1344 (if (re-search-forward key nil t)
yuuji@13 1345 (progn
yuuji@13 1346 (setq string (buffer-substring (point) (point-end-of-line)))
yuuji@53 1347 (delete-region (point) (progn (end-of-line) (point))))
yuuji@13 1348 (open-line 1)
yuuji@53 1349 (delete-region (point) (progn (beginning-of-line)(point)));for 19 :-<
yuuji@13 1350 (insert key))
yuuji@13 1351 (unwind-protect
yuuji@13 1352 (setq string (read-string (concat key ": ") string))
yuuji@13 1353 (insert string)))
yuuji@13 1354
yuuji@13 1355 ((= c ?b) ;%#BEGIN %#END region
yuuji@16 1356 (or end (setq beg (min (point) (mark)) end (max (point) (mark))))
yuuji@13 1357 (set-marker b beg)
yuuji@13 1358 (set-marker e end)
yuuji@13 1359 (goto-char (point-min))
yuuji@13 1360 (while (re-search-forward "^%#\\(BEGIN\\)\\|\\(END\\)$" nil t)
yuuji@53 1361 (beginning-of-line)
yuuji@53 1362 (delete-region (point) (progn (forward-line 1) (point))))
yuuji@68 1363 (goto-char b)
yuuji@13 1364 (open-line 1)
yuuji@53 1365 (delete-region (point) (progn (beginning-of-line)(point)));for 19 :-<
yuuji@13 1366 (insert "%#BEGIN")
yuuji@68 1367 (goto-char e)
yuuji@68 1368 (insert "%#END\n")
yuuji@68 1369 (set-marker b nil)
yuuji@68 1370 (set-marker e nil))
yuuji@69 1371 ))))
yuuji@13 1372
yuuji@51 1373 (defun YaTeX-goto-corresponding-label (reverse &optional otherwin)
yuuji@48 1374 "Jump to corresponding \\label{} and \\ref{} or \\cite and \\bibitem.
yuuji@48 1375 The default search direction depends on the command at the cursor position.
yuuji@48 1376 When the cursor is on \\ref(\\cite), YaTeX will try to search the
yuuji@48 1377 corresponding \\label(\\bibitem) backward,
yuuji@48 1378 and if it fails search forward again. And when the cursor is
yuuji@48 1379 on \\label(\\bibitem), YaTeX will search the corresponding \\ref(\\cite)
yuuji@48 1380 forward at first and secondary backward.
yuuji@48 1381 Argument REVERSE non-nil makes the default
yuuji@48 1382 direction rule reverse. Since Search string is automatically set in
yuuji@13 1383 search-last-string, you can repeat search the same label/ref by typing
yuuji@51 1384 \\[isearch-forward] or \\[isearch-backward].
yuuji@51 1385 If optional second argument OTHERWIN is non-nil, move to other window."
yuuji@13 1386
yuuji@51 1387 (let ((scmd "") label direc string blist (p (point)) (cb (current-buffer))
yuuji@68 1388 (refcommands "label\\|\\(page\\)?ref\\|cite\\|bibitem")
yuuji@48 1389 (func (function (lambda (string sfunc)
yuuji@48 1390 (or
yuuji@48 1391 (funcall sfunc string nil t)
yuuji@68 1392 (funcall (if (eq sfunc 're-search-forward)
yuuji@68 1393 're-search-backward 're-search-forward)
yuuji@48 1394 string nil t))))))
yuuji@7 1395 (cond
yuuji@48 1396 ((YaTeX-on-section-command-p refcommands)
yuuji@48 1397 (setq scmd (cdr (assoc (YaTeX-match-string 1)
yuuji@68 1398 '(("label" . "\\(page\\)?ref") ("ref" . "label")
yuuji@68 1399 ("pageref" . "label")
yuuji@48 1400 ("cite" . "bibitem") ("bibitem" . "cite")))))
yuuji@7 1401 (goto-char (match-end 0))
yuuji@7 1402 (let ((label (buffer-substring
yuuji@7 1403 (1- (point)) (progn (backward-list 1) (1+ (point))))))
yuuji@68 1404 ;(setq string (concat "\\" scmd "{" label "}"))
yuuji@68 1405 (setq string (concat "\\\\" scmd "{" (regexp-quote label) "}"))
yuuji@48 1406 (setq direc (if (string-match "ref\\|cite" scmd)
yuuji@68 1407 're-search-forward 're-search-backward))
yuuji@48 1408 (if YaTeX-current-position-register
yuuji@48 1409 (point-to-register YaTeX-current-position-register))
yuuji@68 1410 (if reverse (setq direc (if (eq direc 're-search-forward)
yuuji@68 1411 're-search-backward 're-search-forward)))
yuuji@48 1412 (if (funcall func string direc) ;label/ref found!
yuuji@13 1413 (progn
yuuji@51 1414 (if otherwin
yuuji@51 1415 (progn
yuuji@51 1416 (goto-char p)
yuuji@51 1417 (if (one-window-p)
yuuji@51 1418 (split-window-calculate-height
yuuji@51 1419 YaTeX-default-pop-window-height))
yuuji@51 1420 (select-window (get-lru-window))
yuuji@51 1421 (switch-to-buffer cb)))
yuuji@13 1422 (goto-char (match-beginning 0))
yuuji@51 1423 (push-mark p))
yuuji@51 1424 ;;if label/ref not found, search through all yatex buffers.
yuuji@51 1425 (goto-char p) ;resume position of current buffer
yuuji@48 1426 (setq blist (YaTeX-yatex-buffer-list))
yuuji@48 1427 (catch 'found
yuuji@48 1428 (while blist
yuuji@48 1429 (set-buffer (car blist))
yuuji@48 1430 (if (YaTeX-on-section-command-p refcommands)
yuuji@48 1431 (goto-char (match-beginning 0)))
yuuji@48 1432 (if (funcall func string direc)
yuuji@48 1433 (progn
yuuji@51 1434 (cond
yuuji@51 1435 (otherwin
yuuji@51 1436 (set-buffer cb)
yuuji@51 1437 (goto-char p)
yuuji@51 1438 (if (one-window-p)
yuuji@51 1439 (split-window-calculate-height
yuuji@51 1440 YaTeX-default-pop-window-height))
yuuji@51 1441 (select-window (get-lru-window))
yuuji@51 1442 (switch-to-buffer (car blist)))
yuuji@51 1443 ((or (get-buffer-window (car blist))
yuuji@51 1444 (and YaTeX-emacs-19
yuuji@51 1445 (get-buffer-window (car blist) t)))
yuuji@51 1446 (goto-buffer-window (car blist)))
yuuji@51 1447 (t
yuuji@49 1448 (switch-to-buffer (car blist))
yuuji@49 1449 (message
yuuji@49 1450 (format "Type %s %c to return to original position."
yuuji@49 1451 (key-description
yuuji@49 1452 (car
yuuji@49 1453 (or (where-is-internal 'register-to-point)
yuuji@49 1454 (where-is-internal 'jump-to-register))))
yuuji@51 1455 YaTeX-current-position-register))))
yuuji@48 1456 (goto-char (match-beginning 0))
yuuji@48 1457 (throw 'found t)))
yuuji@48 1458 (setq blist (cdr blist)))))
yuuji@48 1459 )
yuuji@48 1460 (if YaTeX-emacs-19
yuuji@68 1461 (setq regexp-search-ring
yuuji@68 1462 (cons string (delete string regexp-search-ring)))
yuuji@68 1463 (setq search-last-regexp string)))
yuuji@69 1464 (t nil))))
yuuji@5 1465
yuuji@58 1466 (defun YaTeX-goto-corresponding-environment (&optional allow-mismatch noerr)
yuuji@53 1467 "Go to corresponding begin/end enclosure.
yuuji@58 1468 Optional argument ALLOW-MISMATCH allows mismatch open/clese. Use this
yuuji@58 1469 for \left(, \right).
yuuji@58 1470 Optional third argument NOERR causes no error for unballanced environment."
yuuji@3 1471 (interactive)
yuuji@5 1472 (if (not (YaTeX-on-begin-end-p)) nil
yuuji@53 1473 (let ((p (match-end 0)) b0 b1 env (nest 0) regexp re-s (op (point))
yuuji@7 1474 (m0 (match-beginning 0)) ;whole matching
yuuji@7 1475 (m1 (match-beginning 1)) ;environment in \begin{}
yuuji@70 1476 (m2 (match-beginning 2)) ;environment in \end{}
yuuji@70 1477 (m3 (match-beginning 3))) ;environment in \[ \] \( \)
yuuji@7 1478 ;(setq env (regexp-quote (buffer-substring p (match-beginning 0))))
yuuji@5 1479 (if (cond
yuuji@7 1480 (m1 ;if begin{xxx}
yuuji@53 1481 (setq env
yuuji@53 1482 (if allow-mismatch YaTeX-struct-name-regexp
yuuji@53 1483 (regexp-quote (buffer-substring m1 (match-end 1)))))
yuuji@13 1484 ; (setq regexp (concat "\\(\\\\end{" env "}\\)\\|"
yuuji@13 1485 ; "\\(\\\\begin{" env "}\\)"))
yuuji@13 1486 (setq regexp
yuuji@13 1487 (concat
yuuji@13 1488 "\\("
yuuji@53 1489 (YaTeX-replace-format-args
yuuji@53 1490 (regexp-quote YaTeX-struct-end) env "" "")
yuuji@13 1491 "\\)\\|\\("
yuuji@53 1492 (YaTeX-replace-format-args
yuuji@53 1493 (regexp-quote YaTeX-struct-begin) env "" "")
yuuji@13 1494 "\\)"))
yuuji@46 1495 (setq re-s 're-search-forward))
yuuji@7 1496 (m2 ;if end{xxx}
yuuji@53 1497 (setq env
yuuji@53 1498 (if allow-mismatch YaTeX-struct-name-regexp
yuuji@53 1499 (regexp-quote (buffer-substring m2 (match-end 2)))))
yuuji@13 1500 ; (setq regexp (concat "\\(\\\\begin{" env "}\\)\\|"
yuuji@13 1501 ; "\\(\\\\end{" env "}\\)"))
yuuji@13 1502 (setq regexp
yuuji@13 1503 (concat
yuuji@13 1504 "\\("
yuuji@53 1505 (YaTeX-replace-format-args
yuuji@53 1506 (regexp-quote YaTeX-struct-begin) env "" "")
yuuji@13 1507 "\\)\\|\\("
yuuji@53 1508 (YaTeX-replace-format-args
yuuji@53 1509 (regexp-quote YaTeX-struct-end) env "" "")
yuuji@13 1510 "\\)"))
yuuji@46 1511 (setq re-s 're-search-backward))
yuuji@70 1512 (m3 ;math environment
yuuji@70 1513 (setq env (char-after (1+ m3))
yuuji@70 1514 regexp (format "\\(%s%s\\)\\|\\(%s%s\\)"
yuuji@70 1515 YaTeX-ec-regexp
yuuji@70 1516 (regexp-quote
yuuji@70 1517 (cdr (assq env '((?( . ")") (?) . "(")
yuuji@70 1518 (?[ . "]") (?] . "[")))))
yuuji@70 1519 YaTeX-ec-regexp
yuuji@70 1520 (regexp-quote (char-to-string env)))
yuuji@70 1521 re-s (if (memq env '(?\( ?\[))
yuuji@70 1522 're-search-forward
yuuji@70 1523 're-search-backward)))
yuuji@58 1524 (t (if noerr nil (error "Corresponding environment not found."))))
yuuji@7 1525 (progn
yuuji@7 1526 (while (and (>= nest 0) (funcall re-s regexp nil t))
yuuji@7 1527 (setq b0 (match-beginning 0) b1 (match-beginning 1))
yuuji@7 1528 (if (or (equal b0 m0)
yuuji@49 1529 (YaTeX-literal-p b0))
yuuji@7 1530 nil
yuuji@7 1531 (setq nest (if (equal b0 b1)
yuuji@7 1532 (1- nest) (1+ nest)))))
yuuji@58 1533 (if (< nest 0)
yuuji@58 1534 (goto-char (match-beginning 0)) ;found.
yuuji@7 1535 (goto-char op)
yuuji@58 1536 (funcall
yuuji@58 1537 (if noerr 'message 'error)
yuuji@58 1538 "Corresponding environment `%s' not found." env)
yuuji@58 1539 (sit-for 1)
yuuji@69 1540 nil))))))
yuuji@3 1541
yuuji@60 1542 (defun YaTeX-goto-corresponding-file (&optional other)
yuuji@60 1543 "Visit or switch buffer of corresponding file,
yuuji@60 1544 looking at \\input or \\include or \includeonly on current line."
yuuji@60 1545 (if (not (YaTeX-on-includes-p)) nil
yuuji@70 1546 (let ((parent buffer-file-name) input-file b)
yuuji@60 1547 (save-excursion
yuuji@60 1548 (if (search-forward "{" (point-end-of-line) t)
yuuji@60 1549 nil
yuuji@60 1550 (skip-chars-backward "^,{"))
yuuji@60 1551 (setq input-file
yuuji@60 1552 (buffer-substring
yuuji@60 1553 (point) (progn (skip-chars-forward "^ ,}") (point))))
yuuji@60 1554 (if (not (string-match "\\.\\(tex\\|sty\\)$" input-file))
yuuji@60 1555 (setq input-file (concat input-file ".tex"))))
yuuji@60 1556 (cond
yuuji@60 1557 (other (YaTeX-switch-to-buffer-other-window input-file))
yuuji@70 1558 ((setq b (YaTeX-get-file-buffer input-file))
yuuji@70 1559 (goto-buffer-window b))
yuuji@60 1560 (t (YaTeX-switch-to-buffer input-file)))
yuuji@60 1561 (or (YaTeX-get-builtin "!")
yuuji@60 1562 YaTeX-parent-file
yuuji@69 1563 (setq YaTeX-parent-file parent)))))
yuuji@5 1564
yuuji@5 1565 (defun YaTeX-goto-corresponding-BEGIN-END ()
yuuji@5 1566 (if (not (YaTeX-on-BEGIN-END-p)) nil
yuuji@5 1567 (if (cond
yuuji@5 1568 ((equal (match-beginning 0) (match-beginning 1)) ;if on %#BEGIN
yuuji@5 1569 (not (search-forward "%#END" nil t)))
yuuji@5 1570 (t ; if on %#END
yuuji@5 1571 (not (search-backward "%#BEGIN" nil t))))
yuuji@7 1572 (error "Corresponding %%#BEGIN/END not found."))
yuuji@5 1573 (beginning-of-line)
yuuji@69 1574 t))
yuuji@5 1575
yuuji@59 1576 (defvar YaTeX-processed-file-regexp-alist nil
yuuji@59 1577 "Alist of regexp of processed file regexp vs. its file name part;
yuuji@59 1578 For example, if you include image file with `\\epsfile{file=FILE}' where
yuuji@59 1579 `FILE' is processed file. You might want to view FILE with other previewer
yuuji@59 1580 such as ghostview, or want to preview its source which was drawn with
yuuji@59 1581 other drawing tool, tgif for example. Then you should set entire regexp
yuuji@59 1582 of including expression and enclose its file name part with \\\\( and \\\\).
yuuji@59 1583
yuuji@59 1584 Ex. (\"\\\\\\\\epsfile{[^}]*file=\\\\([^,} ]+\\\\)\\\\(\\\\.e?ps\\\\)?[^}]*}\" 1)
yuuji@59 1585
yuuji@59 1586 Where the first group surrounded by \\\\( and \\\\) is the file name part
yuuji@59 1587 of expression. So you should set 1 to second element. And the first
yuuji@59 1588 matching group is sent to (image) processor defined by the variable
yuuji@59 1589 YaTeX-file-processor-alist. See also the documentation of
yuuji@59 1590 YaTeX-file-processor-alist.
yuuji@59 1591
yuuji@59 1592 ↑じゃ良くわかんないすね。例えば tgif hoge.obj して hoge.eps を
yuuji@59 1593 \\epsfile{file=hoge.eps} でインクルードしているとしよう。その行で
yuuji@59 1594 \[prefix\] g を押した時に tgif を起動して欲しかったら、まず上のような
yuuji@59 1595 正規表現を設定する。\\\\(と\\\\)で囲んだところがファイル名になるように
yuuji@59 1596 注意する。でファイル名部分が何番目の\\\\(\\\\)になるかをリストの2番目に書く。
yuuji@59 1597 すると、その部分が変数 YaTeX-file-processor-alist で定義された
yuuji@59 1598 処理プログラムに渡される。というわけ。
yuuji@59 1599 ん〜やっぱりむずかしいね。分からない時は隣の Lisper に聞くか、
yuuji@59 1600 fj野鳥の会で聞こう!
yuuji@59 1601 ")
yuuji@59 1602
yuuji@59 1603 (defvar YaTeX-processed-file-regexp-alist-default
yuuji@64 1604 '(("\\\\epsfile\\(\\[[^]]+\\]\\)?{[^},]*file=\\(\\([^,} ]*/\\)?[^,}. ]+\\)\\(\\.e?ps\\)?[^}]*}" 2)
yuuji@60 1605 ("\\\\epsfig{[^},]*fi\\(le\\|gure\\)=\\(\\([^,} ]*/\\)?[^,}. ]+\\)\\(\\.e?ps\\)?[^}]*}" 2)
yuuji@60 1606 ("\\\\postscriptbox{[^}]*}{[^}]*}{\\(\\([^,} ]*/\\)?[^}. ]+\\)\\(\\.e?ps\\)?}" 1)
yuuji@64 1607 ("\\\\\\(epsfbox\\|includegraphics\\|epsfig\\)\\*?{\\(\\([^,} ]*/\\)?[^}. ]+\\)\\(\\.e?ps\\)?}" 2) ;\epsfbox{hoge.ps} or \includegraphics{hoge.eps}
yuuji@60 1608 ("\\\\\\(psbox\\)\\(\\[[^]]+\\]\\)?{\\(\\([^,} ]*/\\)?[^} ]+\\)\\(\\.e?ps\\)}" 3) ;\psbox[options...]{hoge.eps} (97/1/11)
yuuji@68 1609 ("\\\\input{\\([^} ]+\\)\\(\\.tps\\)}" 1) ;tgif2tex (1998/9/16)
yuuji@59 1610 )
yuuji@69 1611 "See the documentation of YaTeX-processed-file-regexp-alist.")
yuuji@59 1612
yuuji@59 1613 (defvar YaTeX-file-processor-alist nil
yuuji@59 1614 "*Alist of files' processor vs. its extension;
yuuji@69 1615 See also the documentation of YaTeX-processed-file-regexp-alist.")
yuuji@69 1616
yuuji@59 1617 (defvar YaTeX-file-processor-alist-default
yuuji@59 1618 '(("tgif" . ".obj")
yuuji@59 1619 ("ghostview" . ".ps")
yuuji@59 1620 ("ghostview" . ".eps")
yuuji@59 1621 (t . ".tex")
yuuji@59 1622 (t . ".sty")
yuuji@59 1623 (t . ""))
yuuji@69 1624 "See the documentation of YaTeX-file-processor-alist.")
yuuji@59 1625
yuuji@59 1626 (defun YaTeX-goto-corresponding-file-processor (&optional other)
yuuji@59 1627 "Execute corresponding file processor."
yuuji@59 1628 (save-excursion
yuuji@59 1629 (or (looking-at YaTeX-ec-regexp)
yuuji@59 1630 (skip-chars-backward (concat "^" YaTeX-ec) (point-beginning-of-line)))
yuuji@59 1631 (let ((list (append YaTeX-processed-file-regexp-alist
yuuji@59 1632 YaTeX-processed-file-regexp-alist-default))
yuuji@59 1633 (p (point)) flist file
yuuji@70 1634 (peol (point-end-of-line))
yuuji@70 1635 (basedir
yuuji@70 1636 (if YaTeX-search-file-from-top-directory
yuuji@70 1637 (save-excursion (YaTeX-visit-main t) default-directory)
yuuji@70 1638 ".")))
yuuji@59 1639 (setq flist (catch 'found
yuuji@70 1640 (while list
yuuji@70 1641 (goto-char p)
yuuji@70 1642 (if (re-search-forward (car (car list)) peol t)
yuuji@70 1643 (progn
yuuji@70 1644 (setq file (YaTeX-match-string
yuuji@70 1645 (car (cdr (car list)))))
yuuji@70 1646 (throw 'found (cdr (car list)))))
yuuji@70 1647 (setq list (cdr list)))))
yuuji@59 1648 (if flist ;if pattern and file name found
yuuji@59 1649 (let*((plist (append YaTeX-file-processor-alist
yuuji@59 1650 YaTeX-file-processor-alist-default))
yuuji@59 1651 (plist0 plist)
yuuji@59 1652 ext cmd src buf (alt (car (cdr flist))))
yuuji@59 1653 (if (and (re-search-forward
yuuji@59 1654 (concat YaTeX-comment-prefix "\\s *\\(.*\\)$") peol t)
yuuji@59 1655 (assoc (setq cmd (YaTeX-match-string 1))
yuuji@59 1656 YaTeX-file-processor-alist))
yuuji@59 1657 (setq src ;if processor is specified
yuuji@59 1658 (concat file
yuuji@59 1659 (cdr (assoc cmd YaTeX-file-processor-alist))))
yuuji@59 1660 (while plist ;if processor is not specified
yuuji@59 1661 (setq ext (cdr (car plist)))
yuuji@60 1662 (if (and (string< "" (concat file ext))
yuuji@70 1663 (file-exists-p
yuuji@70 1664 (expand-file-name (concat file ext) basedir)))
yuuji@70 1665 (setq cmd (car (car plist))
yuuji@70 1666 src (concat file ext) plist nil))
yuuji@59 1667 (setq plist (cdr plist)))
yuuji@59 1668 (if (and (null src) alt YaTeX-create-file-prefix-g)
yuuji@59 1669 (setq cmd alt
yuuji@59 1670 src (concat file (cdr (assoc alt plist0))))))
yuuji@70 1671 (if src ;if processor and src file found
yuuji@70 1672 (let ((default-directory basedir))
yuuji@59 1673 (cond
yuuji@70 1674 ((stringp cmd)
yuuji@70 1675 (let ((buf (concat "* " cmd " " src " *")))
yuuji@70 1676 (YaTeX-system (concat cmd " " src) buf)
yuuji@70 1677 t))
yuuji@70 1678 ((eq t cmd)
yuuji@70 1679 (let ((parent buffer-file-name))
yuuji@70 1680 (funcall
yuuji@70 1681 (cond
yuuji@70 1682 (other 'YaTeX-switch-to-buffer-other-window)
yuuji@70 1683 ((get-file-buffer src) 'goto-buffer-window)
yuuji@70 1684 (t 'YaTeX-switch-to-buffer))
yuuji@70 1685 src)
yuuji@70 1686 (or (YaTeX-get-builtin "!")
yuuji@70 1687 YaTeX-parent-file
yuuji@70 1688 (setq YaTeX-parent-file parent))
yuuji@70 1689 t))
yuuji@70 1690 ((symbolp cmd)
yuuji@70 1691 (cond
yuuji@70 1692 ((symbol-function cmd)
yuuji@70 1693 (funcall cmd src other)))
yuuji@70 1694 t)))))))))
yuuji@59 1695
yuuji@14 1696 (defun YaTeX-on-section-command-p (command)
yuuji@53 1697 "Check if point is on the LaTeX command: COMMAND(regexp).
yuuji@49 1698 Return nil if point is not on it. Otherwise return the
yuuji@49 1699 number of argument position.
yuuji@48 1700 Section command name is stored in match-data #1."
yuuji@53 1701 (let ((p (point)) md (parg 0) (argc 1) word (grouping 0) (i 0))
yuuji@53 1702 (while (setq i (string-match "\\\\(" command i))
yuuji@53 1703 (setq grouping (1+ grouping) i (+ i 2)))
yuuji@7 1704 (save-excursion
yuuji@49 1705 (if (looking-at YaTeX-ec-regexp) nil
yuuji@49 1706 (catch 'found ;caught value has no meaning
yuuji@49 1707 (while t
yuuji@49 1708 (if (bobp) (throw 'found nil))
yuuji@49 1709 (cond
yuuji@49 1710 ((looking-at YaTeX-ec-regexp) (throw 'found t))
yuuji@49 1711 ((looking-at "[[{]") nil)
yuuji@53 1712 ((looking-at "[]}]")(condition-case nil (up-list -1) (error nil)))
yuuji@49 1713 (t (skip-chars-backward " \t\r\n")))
yuuji@49 1714 (skip-chars-backward (concat "^ \t\r\n{}[]" YaTeX-ec-regexp))
yuuji@51 1715 (or (bobp) (forward-char -1)))))
yuuji@51 1716 (if (and
yuuji@51 1717 (looking-at (concat YaTeX-ec-regexp "\\(" command "\\)"
yuuji@51 1718 "\\(\\(\\[[^]]+\\]\\)*\\)" ;optional arg
yuuji@60 1719 ;"[ \t\n\r]*{[^}]+}")) ;arg braces
yuuji@60 1720 "[ \t\n\r]*{[^}]*}")) ;arg braces
yuuji@51 1721 (not (YaTeX-lookup-table
yuuji@51 1722 (setq word (YaTeX-match-string 1)) 'singlecmd)))
yuuji@46 1723 (progn
yuuji@49 1724 (setq md (match-data))
yuuji@49 1725 (skip-chars-forward "^{")
yuuji@49 1726 (if (<= (point) p) (setq parg (1+ parg)))
yuuji@49 1727 (setq argc
yuuji@51 1728 (or (car (cdr (YaTeX-lookup-table word 'section)))
yuuji@49 1729 argc))
yuuji@53 1730 (while (and (>= (setq argc (1- argc)) 0)
yuuji@53 1731 (progn (skip-chars-forward " \t\n\r")
yuuji@53 1732 (looking-at "{")))
yuuji@49 1733 (forward-list 1)
yuuji@53 1734 (if (<= (point) p) (setq parg (1+ parg))))
yuuji@49 1735 (store-match-data md)
yuuji@53 1736 (setq i (+ 2 grouping))
yuuji@53 1737 (if (and (match-beginning i)
yuuji@53 1738 (>= p (match-beginning i)) (< p (match-end i)))
yuuji@49 1739 -1 ;return -1 if point is on optional arg
yuuji@49 1740 (if (< p (point)) parg))
yuuji@69 1741 )))))
yuuji@5 1742
yuuji@52 1743 (defun YaTeX-on-maketitle-p ()
yuuji@52 1744 "Check if point is on maketitle type commands.
yuuji@52 1745 Call this function after YaTeX-on-section-command-p."
yuuji@52 1746 (let ((p (point)))
yuuji@52 1747 (save-excursion
yuuji@52 1748 (or (= (char-after (point)) ?\\ )
yuuji@52 1749 (progn
yuuji@52 1750 (skip-chars-backward
yuuji@52 1751 (concat "^" YaTeX-ec-regexp) (point-beginning-of-line))
yuuji@52 1752 (or (bobp) (bolp) (backward-char 1))))
yuuji@52 1753 (and (looking-at (concat YaTeX-ec-regexp YaTeX-TeX-token-regexp))
yuuji@52 1754 (<= (match-beginning 0) p)
yuuji@52 1755 (> (match-end 0) p)))))
yuuji@52 1756
yuuji@5 1757 (defun YaTeX-on-begin-end-p ()
yuuji@5 1758 (save-excursion
yuuji@5 1759 (beginning-of-line)
yuuji@5 1760 (re-search-forward
yuuji@13 1761 ;;"\\\\begin{\\([^}]+\\)}\\|\\\\end{\\([^}]+\\)}"
yuuji@13 1762 (concat
yuuji@13 1763 (YaTeX-replace-format-args
yuuji@13 1764 (regexp-quote YaTeX-struct-begin)
yuuji@13 1765 (concat "\\(" YaTeX-struct-name-regexp "\\)") "" "" "")
yuuji@13 1766 "\\|"
yuuji@13 1767 (YaTeX-replace-format-args
yuuji@13 1768 (regexp-quote YaTeX-struct-end)
yuuji@70 1769 (concat "\\(" YaTeX-struct-name-regexp "\\)") "" "" "")
yuuji@70 1770 "\\|\\("
yuuji@70 1771 YaTeX-ec-regexp ;;"[][()]\\)"
yuuji@70 1772 "[][]\\)"
yuuji@70 1773 )
yuuji@69 1774 (point-end-of-line) t)))
yuuji@6 1775
yuuji@5 1776 (defun YaTeX-on-includes-p ()
yuuji@5 1777 (save-excursion
yuuji@5 1778 (beginning-of-line)
yuuji@5 1779 (re-search-forward "\\(\\(include.*\\)\\|\\(input\\)\\){.*}"
yuuji@69 1780 (point-end-of-line) t)))
yuuji@6 1781
yuuji@7 1782 (defun YaTeX-on-comment-p (&optional sw)
yuuji@22 1783 "Return t if current line is commented out.
yuuji@22 1784 Optional argument SW t to treat all `%' lines as comment,
yuuji@7 1785 even if on `%#' notation."
yuuji@7 1786 (save-excursion
yuuji@7 1787 (beginning-of-line)
yuuji@22 1788 (skip-chars-forward "\\s ")
yuuji@69 1789 (looking-at (if sw "%" "%[^#]"))))
yuuji@7 1790
yuuji@5 1791 (defun YaTeX-on-BEGIN-END-p ()
yuuji@5 1792 (save-excursion
yuuji@7 1793 (let ((case-fold-search nil))
yuuji@5 1794 (beginning-of-line)
yuuji@69 1795 (re-search-forward
yuuji@69 1796 "\\(%#BEGIN\\)\\|\\(%#END\\)" (point-end-of-line) t))))
yuuji@6 1797
yuuji@7 1798 (defun YaTeX-goto-corresponding-* (arg)
yuuji@5 1799 "Parse current line and call suitable function."
yuuji@7 1800 (interactive "P")
yuuji@5 1801 (cond
yuuji@7 1802 ((YaTeX-goto-corresponding-label arg))
yuuji@5 1803 ((YaTeX-goto-corresponding-environment))
yuuji@61 1804 ((YaTeX-goto-corresponding-file-processor arg))
yuuji@60 1805 ((YaTeX-goto-corresponding-file arg))
yuuji@5 1806 ((YaTeX-goto-corresponding-BEGIN-END))
yuuji@53 1807 ((and (string-match
yuuji@53 1808 YaTeX-equation-env-regexp ;to delay loading
yuuji@53 1809 (or (YaTeX-inner-environment t) "document"))
yuuji@53 1810 (YaTeX-goto-corresponding-leftright)))
yuuji@69 1811 (t (message "I don't know where to go."))))
yuuji@5 1812
yuuji@51 1813 (defun YaTeX-goto-corresponding-*-other-window (arg)
yuuji@51 1814 "Parse current line and call suitable function."
yuuji@51 1815 (interactive "P")
yuuji@51 1816 (cond
yuuji@51 1817 ((YaTeX-goto-corresponding-label arg t))
yuuji@51 1818 ;;((YaTeX-goto-corresponding-environment))
yuuji@51 1819 ((YaTeX-goto-corresponding-file t))
yuuji@51 1820 ;;((YaTeX-goto-corresponding-BEGIN-END))
yuuji@69 1821 (t (message "I don't know where to go."))))
yuuji@51 1822
yuuji@5 1823 (defun YaTeX-comment-region (alt-prefix)
yuuji@22 1824 "Comment out region by '%'.
yuuji@22 1825 If you call this function on the 'begin{}' or 'end{}' line,
yuuji@22 1826 it comments out whole environment"
yuuji@5 1827 (interactive "P")
yuuji@4 1828 (if (not (YaTeX-on-begin-end-p))
yuuji@49 1829 (comment-out-region
yuuji@5 1830 (if alt-prefix
yuuji@5 1831 (read-string "Insert prefix: ")
yuuji@5 1832 YaTeX-comment-prefix))
yuuji@69 1833 (YaTeX-comment-uncomment-env 'comment-out-region)))
yuuji@4 1834
yuuji@5 1835 (defun YaTeX-uncomment-region (alt-prefix)
yuuji@4 1836 "Uncomment out region by '%'."
yuuji@5 1837 (interactive "P")
yuuji@4 1838 (if (not (YaTeX-on-begin-end-p))
yuuji@5 1839 (uncomment-region
yuuji@5 1840 (if alt-prefix (read-string "Remove prefix: ")
yuuji@11 1841 YaTeX-comment-prefix)
yuuji@49 1842 (region-beginning) (region-end) YaTeX-uncomment-once)
yuuji@69 1843 (YaTeX-comment-uncomment-env 'uncomment-region)))
yuuji@4 1844
yuuji@4 1845 (defun YaTeX-comment-uncomment-env (func)
yuuji@4 1846 "Comment or uncomment out one LaTeX environment switching function by FUNC."
yuuji@49 1847 (let (beg (p (point)))
yuuji@49 1848 (save-excursion
yuuji@49 1849 (beginning-of-line)
yuuji@49 1850 (setq beg (point))
yuuji@49 1851 (YaTeX-goto-corresponding-environment)
yuuji@58 1852 (beginning-of-line)
yuuji@49 1853 (if (> p (point)) (setq beg (1+ beg)) (forward-char 1))
yuuji@49 1854 (funcall func YaTeX-comment-prefix beg (point) YaTeX-uncomment-once)))
yuuji@45 1855 (message "%sommented out current environment."
yuuji@69 1856 (if (eq func 'comment-out-region) "C" "Un-c")))
yuuji@4 1857
yuuji@3 1858 (defun YaTeX-comment-paragraph ()
yuuji@3 1859 "Comment out current paragraph."
yuuji@3 1860 (interactive)
yuuji@3 1861 (save-excursion
yuuji@7 1862 (cond
yuuji@7 1863 ((YaTeX-on-begin-end-p)
yuuji@7 1864 (beginning-of-line)
yuuji@7 1865 (insert YaTeX-comment-prefix)
yuuji@7 1866 (YaTeX-goto-corresponding-environment)
yuuji@7 1867 (beginning-of-line)
yuuji@7 1868 (insert YaTeX-comment-prefix))
yuuji@7 1869 ((YaTeX-on-comment-p)
yuuji@7 1870 (message "Already commented out."))
yuuji@7 1871 (t
yuuji@4 1872 (mark-paragraph)
yuuji@7 1873 (if (looking-at paragraph-separate) (forward-line 1))
yuuji@69 1874 (comment-out-region "%")))))
yuuji@3 1875
yuuji@3 1876 (defun YaTeX-uncomment-paragraph ()
yuuji@3 1877 "Uncomment current paragraph."
yuuji@3 1878 (interactive)
yuuji@3 1879 (save-excursion
yuuji@4 1880 (if (YaTeX-on-begin-end-p)
yuuji@68 1881 (let ((p (point-marker)))
yuuji@22 1882 (YaTeX-goto-corresponding-environment)
yuuji@11 1883 (YaTeX-remove-prefix YaTeX-comment-prefix YaTeX-uncomment-once)
yuuji@68 1884 (goto-char p)
yuuji@68 1885 (YaTeX-remove-prefix YaTeX-comment-prefix YaTeX-uncomment-once)
yuuji@68 1886 (set-marker p nil))
yuuji@7 1887 (if (YaTeX-on-comment-p)
yuuji@7 1888 (let*((fill-prefix "")
yuuji@7 1889 ;;append `^%' to head of paragraph delimiter.
yuuji@7 1890 (paragraph-start
yuuji@7 1891 (concat
yuuji@61 1892 "^$\\|^%\\(" YaTeX-paragraph-separate "\\)"))
yuuji@49 1893 (paragraph-separate paragraph-start))
yuuji@7 1894 (mark-paragraph)
yuuji@7 1895 (if (not (bobp)) (forward-line 1))
yuuji@49 1896 (uncomment-region "%" nil nil YaTeX-uncomment-once))
yuuji@69 1897 (message "This line is not a comment line.")))))
yuuji@4 1898
yuuji@4 1899 (defun YaTeX-remove-prefix (prefix &optional once)
yuuji@7 1900 "Remove prefix on current line as far as prefix detected. But
yuuji@4 1901 optional argument ONCE makes deletion once."
yuuji@4 1902 (interactive "sPrefix:")
yuuji@4 1903 (beginning-of-line)
yuuji@4 1904 (while (re-search-forward (concat "^" prefix) (point-end-of-line) t)
yuuji@4 1905 (replace-match "")
yuuji@69 1906 (if once (end-of-line))))
yuuji@3 1907
yuuji@22 1908 (defun YaTeX-kill-some-pairs (predicate gofunc kill-contents)
yuuji@22 1909 "Kill some matching pair.
yuuji@64 1910 This function assumes that pairs occupy whole of each line where they resid."
yuuji@7 1911 (if (not (funcall predicate)) nil
yuuji@64 1912 (let ((b1 (match-beginning 0)) (e1 (match-end 0))
yuuji@64 1913 b2 e2)
yuuji@7 1914 (save-excursion
yuuji@7 1915 (funcall gofunc)
yuuji@64 1916 (funcall predicate) ;get match data
yuuji@64 1917 (if (< (point) e1) ;if currently on begin-line
yuuji@64 1918 (progn
yuuji@64 1919 (setq b2 b1 e2 e1
yuuji@64 1920 b1 (match-beginning 0) e1 (match-end 0))
yuuji@64 1921 (goto-char e2)) ;goto end-line's end
yuuji@64 1922 (setq b2 (match-beginning 0)
yuuji@64 1923 e2 (match-end 0))
yuuji@64 1924 (goto-char e2)) ;now e2 has surely end-line's end
yuuji@64 1925 (skip-chars-forward " \t")
yuuji@64 1926 (and (eolp)
yuuji@64 1927 (not (eobp))
yuuji@64 1928 (setq e2 (1+ (point))))
yuuji@64 1929 (if (not kill-contents)
yuuji@64 1930 (kill-region
yuuji@64 1931 (progn
yuuji@64 1932 (goto-char b2)
yuuji@68 1933 (skip-chars-backward " \t%")
yuuji@64 1934 (if (bolp) (point) b2))
yuuji@64 1935 e2))
yuuji@64 1936 (goto-char b1)
yuuji@68 1937 (skip-chars-backward " \t%")
yuuji@64 1938 (if (not kill-contents)
yuuji@64 1939 (progn
yuuji@64 1940 (kill-append
yuuji@64 1941 (buffer-substring
yuuji@64 1942 (setq b1 (if (bolp) (point) b1))
yuuji@64 1943 (setq e1
yuuji@64 1944 (progn
yuuji@64 1945 (goto-char e1)
yuuji@64 1946 (while (looking-at "{\\| \t")
yuuji@64 1947 (forward-list 1))
yuuji@64 1948 (skip-chars-forward " \t")
yuuji@64 1949 (if (and (eolp) (not (eobp)))
yuuji@64 1950 (1+ (point))
yuuji@64 1951 (point)))))
yuuji@64 1952 t)
yuuji@64 1953 (delete-region b1 e1))
yuuji@64 1954 (kill-region
yuuji@64 1955 (if (bolp) (point) b1)
yuuji@64 1956 e2)))
yuuji@69 1957 t)))
yuuji@5 1958
yuuji@22 1959 (defun YaTeX-kill-section-command (point kill-all)
yuuji@22 1960 "Kill section-type command at POINT leaving its argument.
yuuji@22 1961 Non-nil for the second argument kill its argument too."
yuuji@22 1962 (let (beg (end (make-marker)))
yuuji@14 1963 (save-excursion
yuuji@22 1964 (goto-char point)
yuuji@22 1965 (or (looking-at YaTeX-ec-regexp)
yuuji@22 1966 (progn
yuuji@22 1967 (skip-chars-backward (concat "^" YaTeX-ec-regexp))
yuuji@22 1968 (forward-char -1)))
yuuji@22 1969 (setq beg (point))
yuuji@22 1970 (skip-chars-forward "^{")
yuuji@22 1971 (forward-list 1)
yuuji@22 1972 (set-marker end (point))
yuuji@68 1973 (if kill-all (kill-region beg end)
yuuji@22 1974 (goto-char beg)
yuuji@68 1975 (kill-region
yuuji@22 1976 (point) (progn (skip-chars-forward "^{" end) (1+ (point))))
yuuji@22 1977 (goto-char end)
yuuji@68 1978 (set-marker end nil)
yuuji@68 1979 (kill-append (buffer-substring (point) (1- (point))) nil)
yuuji@69 1980 (delete-backward-char 1)))))
yuuji@14 1981
yuuji@22 1982 (defun YaTeX-kill-paren (kill-contents)
yuuji@22 1983 "Kill parentheses leaving its contents.
yuuji@22 1984 But kill its contents if the argument KILL-CONTENTS is non-nil."
yuuji@14 1985 (save-excursion
yuuji@14 1986 (let (p)
yuuji@14 1987 (if (looking-at "\\s(\\|\\(\\s)\\)")
yuuji@14 1988 (progn
yuuji@14 1989 (if (match-beginning 1)
yuuji@14 1990 (up-list -1))
yuuji@14 1991 (setq p (point))
yuuji@14 1992 (forward-list 1)
yuuji@22 1993 (if kill-contents (delete-region p (point))
yuuji@22 1994 (backward-delete-char 1)
yuuji@22 1995 (goto-char p)
yuuji@22 1996 (if (looking-at
yuuji@22 1997 (concat "{" YaTeX-ec-regexp
yuuji@32 1998 YaTeX-command-token-regexp "+"
yuuji@22 1999 "\\s +"))
yuuji@22 2000 (delete-region
yuuji@22 2001 (point)
yuuji@22 2002 (progn (re-search-forward "\\s +" nil t) (point)))
yuuji@22 2003 (delete-char 1)))
yuuji@69 2004 t)))))
yuuji@14 2005
yuuji@22 2006 (defvar YaTeX-read-environment-history nil "Holds history of environments.")
yuuji@22 2007 (put 'YaTeX-read-environment-history 'no-default t)
yuuji@22 2008 (defun YaTeX-read-environment (prompt &optional predicate must-match initial)
yuuji@22 2009 "Read a LaTeX environment name with completion."
yuuji@49 2010 (YaTeX-sync-local-table 'tmp-env-table)
yuuji@51 2011 (completing-read-with-history
yuuji@51 2012 prompt
yuuji@51 2013 (append tmp-env-table user-env-table env-table)
yuuji@51 2014 predicate must-match initial
yuuji@69 2015 'YaTeX-read-environment-history))
yuuji@22 2016
yuuji@22 2017 (defvar YaTeX-read-section-history nil "Holds history of section-types.")
yuuji@22 2018 (put 'YaTeX-read-section-history 'no-default t)
yuuji@22 2019 (defun YaTeX-read-section (prompt &optional predicate initial)
yuuji@22 2020 "Read a LaTeX section-type command with completion."
yuuji@49 2021 (YaTeX-sync-local-table 'tmp-section-table)
yuuji@51 2022 (let ((minibuffer-completion-table
yuuji@22 2023 (append tmp-section-table user-section-table section-table)))
yuuji@51 2024 (read-from-minibuffer-with-history
yuuji@51 2025 prompt initial YaTeX-section-completion-map nil
yuuji@69 2026 'YaTeX-read-section-history)))
yuuji@22 2027
yuuji@22 2028 (defun YaTeX-read-section-with-overview ()
yuuji@22 2029 "Read sectioning command with overview.
yuuji@22 2030 This function refers a local variable `source-window' in YaTeX-make-section"
yuuji@22 2031 (interactive)
yuuji@52 2032 (require 'yatexsec) ;some case needs this
yuuji@22 2033 (if (> (minibuffer-depth) 1)
yuuji@22 2034 (error "Too many minibuffer levels for overview."))
yuuji@64 2035 (let ((sw (selected-window))
yuuji@64 2036 (minibuffer-max-depth nil) ; for XEmacs20
yuuji@64 2037 (enable-recursive-minibuffers t) sect)
yuuji@22 2038 (unwind-protect
yuuji@22 2039 (progn
yuuji@22 2040 (select-window source-window)
yuuji@22 2041 (setq sect (YaTeX-read-section-in-minibuffer
yuuji@22 2042 "Sectioning(Up=C-p, Down=C-n, Help=?): "
yuuji@22 2043 YaTeX-sectioning-level (YaTeX-section-overview))))
yuuji@22 2044 (select-window sw))
yuuji@22 2045 (if (eq (selected-window) (minibuffer-window))
yuuji@22 2046 (erase-buffer))
yuuji@22 2047 (insert sect)
yuuji@69 2048 (exit-minibuffer)))
yuuji@22 2049
yuuji@51 2050 (defvar YaTeX-read-fontsize-history nil "Holds history of font designator.")
yuuji@51 2051 (put 'YaTeX-read-fontsize-history 'no-default t)
yuuji@22 2052 (defun YaTeX-read-fontsize (prompt &optional predicate must-match initial)
yuuji@22 2053 "Read a LaTeX font changing command with completion."
yuuji@49 2054 (YaTeX-sync-local-table 'tmp-fontsize-table)
yuuji@51 2055 (completing-read-with-history
yuuji@22 2056 prompt (append tmp-fontsize-table user-fontsize-table fontsize-table)
yuuji@69 2057 predicate must-match initial 'YaTeX-read-fontsize-history))
yuuji@5 2058
yuuji@5 2059 (defun YaTeX-change-environment ()
yuuji@5 2060 "Change the name of environment."
yuuji@5 2061 (interactive)
yuuji@5 2062 (if (not (YaTeX-on-begin-end-p)) nil
yuuji@5 2063 (save-excursion
yuuji@7 2064 (let (p env (m1 (match-beginning 1)) (m2 (match-beginning 2)))
yuuji@7 2065 (setq env (if m1 (buffer-substring m1 (match-end 1))
yuuji@7 2066 (buffer-substring m2 (match-end 2))))
yuuji@11 2067 (goto-char (match-beginning 0))
yuuji@5 2068 (set-mark-command nil)
yuuji@5 2069 (YaTeX-goto-corresponding-environment)
yuuji@5 2070 (setq newenv (YaTeX-read-environment
yuuji@5 2071 (format "Change environment `%s' to: " env)))
yuuji@13 2072 (cond
yuuji@13 2073 ((string= newenv "") (message "Change environment cancelled."))
yuuji@13 2074 ((string= newenv env) (message "No need to change."))
yuuji@13 2075 (t
yuuji@5 2076 (search-forward (concat "{" env) (point-end-of-line) t)
yuuji@68 2077 (replace-match (concat "{" newenv) t)
yuuji@5 2078 (exchange-point-and-mark)
yuuji@5 2079 (search-forward (concat "{" env) (point-end-of-line) t)
yuuji@68 2080 (replace-match (concat "{" newenv) t)))
yuuji@69 2081 t))))
yuuji@5 2082
yuuji@22 2083 (defun YaTeX-change-section ()
yuuji@22 2084 "Change section-type command."
yuuji@5 2085 (interactive)
yuuji@49 2086 (let*((where (YaTeX-on-section-command-p YaTeX-command-token-regexp))
yuuji@70 2087 (p (point)) (cmd (YaTeX-match-string 1))
yuuji@70 2088 (beg (make-marker)) (end (make-marker)) old new)
yuuji@49 2089 (if (null where) nil
yuuji@70 2090 (unwind-protect
yuuji@70 2091 (progn
yuuji@70 2092 (cond
yuuji@70 2093 ((equal where 0);;if point is on section command
yuuji@70 2094 (set-marker beg (match-beginning 1))
yuuji@70 2095 (set-marker end (match-end 1))
yuuji@70 2096 (goto-char beg) ;beginning of the command
yuuji@70 2097 (setq new (YaTeX-read-section
yuuji@70 2098 (format "Change `%s' to: " cmd) nil)))
yuuji@49 2099
yuuji@70 2100 ((= where -1);;if point is on a optional parameter
yuuji@70 2101 (set-marker beg (match-beginning 2))
yuuji@70 2102 (skip-chars-forward "^{")
yuuji@70 2103 (set-marker end (point))
yuuji@70 2104 (goto-char p)
yuuji@70 2105 (setq new
yuuji@70 2106 (if (fboundp (intern-soft (concat YaTeX-addin-prefix cmd)))
yuuji@70 2107 (YaTeX-addin cmd)
yuuji@70 2108 (concat "["
yuuji@70 2109 (read-string (format "Change `%s' to: "
yuuji@70 2110 (buffer-substring
yuuji@70 2111 (1+ beg) (1- end))))
yuuji@70 2112 "]"))))
yuuji@49 2113
yuuji@70 2114 ((> where 0);;if point is in arguments' braces
yuuji@70 2115 (or (looking-at "{")
yuuji@70 2116 (progn (skip-chars-backward "^{") (forward-char -1)))
yuuji@70 2117 (set-marker beg (1+ (point)))
yuuji@70 2118 (forward-list 1)
yuuji@70 2119 (forward-char -1)
yuuji@70 2120 (set-marker end (point))
yuuji@70 2121 (setq old (buffer-substring beg end))
yuuji@70 2122 (goto-char p)
yuuji@70 2123 (if (> (length old) 40)
yuuji@70 2124 (setq old (concat (substring old 0 12) "..."
yuuji@70 2125 (substring old -12))))
yuuji@70 2126 (setq new
yuuji@70 2127 (if (intern-soft (concat "YaTeX::" cmd))
yuuji@70 2128 (funcall (intern-soft (concat "YaTeX::" cmd)) where)
yuuji@70 2129 (read-string (format "Change `%s' to: " old)))))
yuuji@70 2130 ) ;cond
yuuji@70 2131 (delete-region beg end)
yuuji@70 2132 (goto-char beg)
yuuji@70 2133 (insert-before-markers new))
yuuji@70 2134 (set-marker beg nil)
yuuji@70 2135 (set-marker end nil))
yuuji@49 2136 ;;(goto-char (marker-position p))
yuuji@69 2137 new)))
yuuji@49 2138
yuuji@49 2139 (defun YaTeX-change-fontsize ()
yuuji@49 2140 "Change large-type command."
yuuji@49 2141 (let ((lt (append tmp-fontsize-table user-fontsize-table fontsize-table))
yuuji@49 2142 (p (point)) large old new beg end)
yuuji@49 2143 ;;(and (looking-at "}") (up-list -1))
yuuji@49 2144 ;;(and (looking-at "{") (forward-char 1))
yuuji@49 2145 ;;Is above convenient?
yuuji@49 2146 (save-excursion
yuuji@49 2147 (or (looking-at YaTeX-ec-regexp)
yuuji@49 2148 (progn
yuuji@49 2149 (skip-chars-backward (concat "^" YaTeX-ec-regexp))
yuuji@49 2150 (forward-char -1)))
yuuji@49 2151 (cond
yuuji@49 2152 ((and
yuuji@49 2153 (looking-at
yuuji@49 2154 (concat YaTeX-ec-regexp "\\(" YaTeX-TeX-token-regexp "\\)"))
yuuji@49 2155 (< p (match-end 0))
yuuji@49 2156 (assoc (setq old (YaTeX-match-string 1)) lt))
yuuji@49 2157 (goto-char p)
yuuji@49 2158 (setq beg (match-beginning 1) end (match-end 1) ;save match position
yuuji@49 2159 new (completing-read
yuuji@49 2160 (format "Change font/size `%s' to : " old) lt))
yuuji@22 2161 (delete-region beg end)
yuuji@49 2162 (goto-char beg)
yuuji@22 2163 (insert-before-markers new)
yuuji@49 2164 new)
yuuji@49 2165 (t nil)
yuuji@69 2166 ))))
yuuji@22 2167
yuuji@52 2168 (defun YaTeX-change-math-image ()
yuuji@52 2169 "Change with image completion."
yuuji@52 2170 (let (maketitle memberp beg end)
yuuji@52 2171 (if (and (YaTeX-on-maketitle-p)
yuuji@52 2172 (progn
yuuji@52 2173 (setq maketitle (substring (YaTeX-match-string 0) 1))
yuuji@52 2174 (setq memberp (YaTeX-math-member-p maketitle))))
yuuji@52 2175 (let ((last-command-char (string-to-char (car memberp))))
yuuji@52 2176 (setq beg (match-beginning 0) end (match-end 0))
yuuji@52 2177 (delete-region beg end)
yuuji@52 2178 (YaTeX-math-insert-sequence t (cdr memberp))))))
yuuji@52 2179
yuuji@22 2180 (defun YaTeX-kill-* (&optional arg)
yuuji@22 2181 "Parse current line and call suitable function.
yuuji@22 2182 Non-nil for ARG kills its contents too."
yuuji@22 2183 (interactive "P")
yuuji@5 2184 (cond
yuuji@5 2185 ((YaTeX-kill-some-pairs 'YaTeX-on-begin-end-p
yuuji@22 2186 'YaTeX-goto-corresponding-environment arg))
yuuji@5 2187 ((YaTeX-kill-some-pairs 'YaTeX-on-BEGIN-END-p
yuuji@22 2188 'YaTeX-goto-corresponding-BEGIN-END arg))
yuuji@32 2189 ((YaTeX-on-section-command-p YaTeX-command-token-regexp);on any command
yuuji@22 2190 (YaTeX-kill-section-command (match-beginning 0) arg))
yuuji@22 2191 ((YaTeX-kill-paren arg))
yuuji@69 2192 (t (message "I don't know what to kill."))))
yuuji@5 2193
yuuji@5 2194 (defun YaTeX-change-* ()
yuuji@5 2195 "Parse current line and call suitable function."
yuuji@5 2196 (interactive)
yuuji@5 2197 (cond
yuuji@5 2198 ((YaTeX-change-environment))
yuuji@22 2199 ((YaTeX-change-section))
yuuji@49 2200 ((YaTeX-change-fontsize))
yuuji@52 2201 ((YaTeX-change-math-image))
yuuji@69 2202 ((YaTeX-change-parentheses))
yuuji@69 2203 (t (message "I don't know what to change."))))
yuuji@5 2204
yuuji@7 2205 ;;;
yuuji@48 2206 ;Check availability of add-in functions
yuuji@7 2207 ;;;
yuuji@7 2208 (cond
yuuji@48 2209 ((featurep 'yatexadd) nil) ;Already provided.
yuuji@48 2210 ((progn (load "yatexadd" t) (featurep 'yatexadd)) nil)
yuuji@7 2211 (t (message "YaTeX add-in functions not supplied.")))
yuuji@7 2212
yuuji@5 2213 (defun YaTeX-addin (name)
yuuji@5 2214 "Check availability of addin function and call it if exists."
yuuji@7 2215 (if (and (not (get 'YaTeX-generate 'disabled))
yuuji@57 2216 (intern-soft (concat YaTeX-addin-prefix name))
yuuji@57 2217 (fboundp (intern-soft (concat YaTeX-addin-prefix name))))
yuuji@57 2218 (let ((s (funcall (intern (concat YaTeX-addin-prefix name)))))
yuuji@7 2219 (if (stringp s) s ""))
yuuji@69 2220 "")) ;Add in function is not bound.
yuuji@69 2221
yuuji@7 2222
yuuji@22 2223 (defun YaTeX-on-item-p (&optional point)
yuuji@22 2224 "Return t if POINT (default is (point)) is on \\item."
yuuji@22 2225 (let ((p (or point (point))))
yuuji@22 2226 (save-excursion
yuuji@22 2227 (goto-char p)
yuuji@22 2228 (end-of-line)
yuuji@22 2229 (setq p (point))
yuuji@22 2230 (re-search-backward YaTeX-paragraph-delimiter nil t)
yuuji@69 2231 (re-search-forward YaTeX-item-regexp p t))))
yuuji@22 2232
yuuji@49 2233 (defun YaTeX-in-verb-p (&optional point)
yuuji@49 2234 "Check if POINT is in verb or verb*. Default of POINT is (point)."
yuuji@49 2235 (setq point (or point (point)))
yuuji@7 2236 (save-excursion
yuuji@70 2237 (let ((md (match-data)))
yuuji@70 2238 (goto-char point)
yuuji@70 2239 (unwind-protect
yuuji@70 2240 (if (not (re-search-backward
yuuji@70 2241 (concat YaTeX-ec-regexp
yuuji@70 2242 "\\(" YaTeX-verb-regexp "\\)"
yuuji@70 2243 "\\([^-A-Za-z_*]\\)")
yuuji@70 2244 (point-beginning-of-line) t))
yuuji@70 2245 nil
yuuji@70 2246 (goto-char (match-end 2))
yuuji@70 2247 (skip-chars-forward
yuuji@70 2248 (concat "^" (buffer-substring (match-beginning 2) (match-end 2))))
yuuji@70 2249 (and (< (match-beginning 2) point) (< (1- point) (point))))
yuuji@70 2250 (store-match-data md)))))
yuuji@49 2251
yuuji@49 2252 (defun YaTeX-literal-p (&optional point)
yuuji@49 2253 "Check if POINT is in verb or verb* or verbatime environment family.
yuuji@49 2254 Default of POINT is (point)."
yuuji@70 2255 (let ((md (match-data)))
yuuji@70 2256 (unwind-protect
yuuji@70 2257 (cond
yuuji@70 2258 ((equal YaTeX-ec "\\") ;maybe LaTeX
yuuji@70 2259 (save-excursion
yuuji@70 2260 (and point (goto-char point))
yuuji@70 2261 (or (YaTeX-in-verb-p (point))
yuuji@70 2262 (and (not (looking-at "\\\\end{verb"))
yuuji@70 2263 (YaTeX-quick-in-environment-p
yuuji@70 2264 YaTeX-verbatim-environments))))))
yuuji@70 2265 (store-match-data md))))
yuuji@5 2266
yuuji@5 2267 (defun YaTeX-in-environment-p (env)
yuuji@7 2268 "Return if current LaTeX environment is ENV.
yuuji@7 2269 ENV is given in the form of environment's name or its list."
yuuji@22 2270 (let ((md (match-data)) (nest 0) p envrx)
yuuji@5 2271 (cond
yuuji@22 2272 ((atom env)
yuuji@22 2273 (setq envrx
yuuji@22 2274 (concat "\\("
yuuji@22 2275 (regexp-quote
yuuji@22 2276 (YaTeX-replace-format-args
yuuji@22 2277 YaTeX-struct-begin env "" ""))
yuuji@22 2278 "\\)\\|\\("
yuuji@22 2279 (regexp-quote
yuuji@22 2280 (YaTeX-replace-format-args
yuuji@22 2281 YaTeX-struct-end env "" ""))
yuuji@22 2282 "\\)"))
yuuji@22 2283 (save-excursion
yuuji@22 2284 (setq p (catch 'open
yuuji@22 2285 (while (YaTeX-re-search-active-backward
yuuji@22 2286 envrx YaTeX-comment-prefix nil t)
yuuji@22 2287 (if (match-beginning 2)
yuuji@22 2288 (setq nest (1+ nest))
yuuji@22 2289 (setq nest (1- nest)))
yuuji@22 2290 (if (< nest 0) (throw 'open t)))))))
yuuji@5 2291 ((listp env)
yuuji@49 2292 (setq p
yuuji@49 2293 (or (YaTeX-in-environment-p (car env))
yuuji@49 2294 (and (cdr env) (YaTeX-in-environment-p (cdr env)))))))
yuuji@7 2295 (store-match-data md)
yuuji@49 2296 p;(or p (YaTeX-in-verb-p (match-beginning 0)))
yuuji@69 2297 ))
yuuji@5 2298
yuuji@5 2299 (defun YaTeX-quick-in-environment-p (env)
yuuji@7 2300 "Check quickly but unsure if current environment is ENV.
yuuji@22 2301 ENV is given in the form of environment's name or its list.
yuuji@49 2302 This function returns correct result only if ENV is NOT nested."
yuuji@7 2303 (save-excursion
yuuji@49 2304 (let ((md (match-data)) (p (point)) rc clfound)
yuuji@7 2305 (cond
yuuji@7 2306 ((listp env)
yuuji@49 2307 (or (YaTeX-quick-in-environment-p (car env))
yuuji@49 2308 (and (cdr env) (YaTeX-quick-in-environment-p (cdr env)))))
yuuji@7 2309 (t
yuuji@22 2310 (if (YaTeX-search-active-backward
yuuji@22 2311 (YaTeX-replace-format-args YaTeX-struct-begin env "" "")
yuuji@22 2312 YaTeX-comment-prefix nil t)
yuuji@49 2313 (setq rc (not (YaTeX-search-active-forward
yuuji@49 2314 (YaTeX-replace-format-args
yuuji@49 2315 YaTeX-struct-end env)
yuuji@49 2316 YaTeX-comment-prefix p t nil))))
yuuji@7 2317 (store-match-data md)
yuuji@69 2318 rc)))))
yuuji@5 2319
yuuji@22 2320 ;; Filling \item
yuuji@46 2321 (defun YaTeX-remove-trailing-comment (start end)
yuuji@46 2322 "Remove trailing comment from START to end."
yuuji@46 2323 (save-excursion
yuuji@60 2324 (let ((trcom (concat YaTeX-comment-prefix "$")))
yuuji@46 2325 (goto-char start)
yuuji@46 2326 (while (re-search-forward trcom end t)
yuuji@54 2327 (if (/= (char-after (1- (match-beginning 0))) ?\\ )
yuuji@69 2328 (replace-match "\\1"))))))
yuuji@5 2329
yuuji@58 2330 (defun YaTeX-get-item-info (&optional recent thisenv)
yuuji@51 2331 "Return the list of the beginning of \\item and column of its item.
yuuji@53 2332 If it seems to be outside of itemizing environment, just return nil.
yuuji@58 2333 Non-nil for optional argument RECENT refers recent \\item.
yuuji@58 2334 Optional second argument THISENV omits calling YaTeX-inner-environment."
yuuji@22 2335 (save-excursion
yuuji@58 2336 (let* ((p (point)) env e0 c
yuuji@58 2337 (bndry (and (setq env (or thisenv (YaTeX-inner-environment t)))
yuuji@53 2338 (get 'YaTeX-inner-environment 'point)
yuuji@53 2339 )))
yuuji@22 2340 (end-of-line)
yuuji@53 2341 (if (if recent
yuuji@53 2342 (YaTeX-re-search-active-backward
yuuji@53 2343 YaTeX-item-regexp YaTeX-comment-prefix bndry t)
yuuji@53 2344 (goto-char bndry)
yuuji@53 2345 (YaTeX-re-search-active-forward
yuuji@53 2346 YaTeX-item-regexp YaTeX-comment-prefix p t))
yuuji@51 2347 (progn
yuuji@53 2348 (goto-char (match-end 0))
yuuji@53 2349 ;(setq c (current-column))
yuuji@58 2350 (if (string-match "desc" env)
yuuji@58 2351 (setq c 6)
yuuji@58 2352 (if (equal (following-char) ?\[) (forward-list 1))
yuuji@58 2353 (setq c 0))
yuuji@51 2354 (skip-chars-forward " \t" (point-end-of-line))
yuuji@69 2355 (list (point-beginning-of-line) (+ c (current-column))))))))
yuuji@22 2356
yuuji@5 2357 (defun YaTeX-fill-item ()
yuuji@5 2358 "Fill item in itemize environment."
yuuji@5 2359 (interactive)
yuuji@5 2360 (save-excursion
yuuji@51 2361 (let* ((p (point))
yuuji@51 2362 (item-term (concat
yuuji@58 2363 "\\(^[ \t]*$\\)\\|" YaTeX-item-regexp "\\|\\("
yuuji@53 2364 YaTeX-ec-regexp "\\(begin\\|end\\)\\)"))
yuuji@51 2365 ;;This value depends on LaTeX.
yuuji@51 2366 fill-prefix start col
yuuji@53 2367 (info (YaTeX-get-item-info t)))
yuuji@55 2368 (if (null info) nil ;not on \item, do nothing
yuuji@22 2369 (setq start (car info)
yuuji@22 2370 col (car (cdr info)))
yuuji@55 2371 (save-excursion
yuuji@55 2372 (if (re-search-backward "^\\s *$" start t)
yuuji@55 2373 ;;if separated from \item line, isolate this block
yuuji@55 2374 (progn
yuuji@55 2375 (setq start (1+ (match-end 0)))
yuuji@55 2376 (goto-char start)
yuuji@55 2377 (skip-chars-forward " \t")
yuuji@55 2378 (delete-region (point) start) ;is this your favor???
yuuji@55 2379 (indent-to col))))
yuuji@46 2380 (beginning-of-line)
yuuji@46 2381 (if (<= (save-excursion
yuuji@46 2382 (re-search-forward
yuuji@58 2383 (concat "\\\\end{\\|\\\\begin{\\|^[ \t]*$") nil t)
yuuji@46 2384 (match-beginning 0))
yuuji@46 2385 p)
yuuji@51 2386 (progn (message "Not on itemize.") nil)
yuuji@51 2387 (end-of-line)
yuuji@51 2388 (newline)
yuuji@51 2389 (indent-to col)
yuuji@51 2390 (setq fill-prefix
yuuji@51 2391 (buffer-substring (point-beginning-of-line)(point)))
yuuji@51 2392 (beginning-of-line)
yuuji@53 2393 (delete-region (point) (progn (forward-line 1) (point)))
yuuji@51 2394 (re-search-forward item-term nil 1)
yuuji@51 2395 (YaTeX-remove-trailing-comment start (point))
yuuji@51 2396 (beginning-of-line)
yuuji@51 2397 (push-mark (point) t)
yuuji@51 2398 (goto-char start)
yuuji@51 2399 (forward-line 1)
yuuji@51 2400 (while (< (point) (mark))
yuuji@51 2401 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
yuuji@51 2402 (forward-line 1))
yuuji@51 2403 (fill-region-as-paragraph start (mark))
yuuji@51 2404 (if NTT-jTeX
yuuji@51 2405 (while (progn(forward-line -1)(end-of-line) (> (point) start))
yuuji@51 2406 (insert ?%)))
yuuji@69 2407 (pop-mark))))))
yuuji@5 2408
yuuji@51 2409 (defun YaTeX-fill-paragraph (arg)
yuuji@51 2410 "YaTeX adjustment function for fill-paragraph.
yuuji@51 2411 *Protect \\verb from unexpected broken up."
yuuji@51 2412 (interactive "P")
yuuji@51 2413 (cond
yuuji@51 2414 ((not (eq major-mode 'yatex-mode)) (fill-paragraph arg))
yuuji@51 2415 ((YaTeX-quick-in-environment-p YaTeX-fill-inhibit-environments) nil)
yuuji@68 2416 ((YaTeX-in-math-mode-p) nil)
yuuji@51 2417 (t
yuuji@51 2418 (save-excursion
yuuji@51 2419 (let ((verbrex (concat YaTeX-ec-regexp
yuuji@51 2420 "\\(" YaTeX-verb-regexp "\\)" ;match#1
yuuji@51 2421 "\\(.\\).*\\(\\2\\)")) ;match #2 and #3
yuuji@58 2422 (p (point)) ii end poslist spacelist (fill-prefix fill-prefix))
yuuji@52 2423 (cond
yuuji@52 2424 ((save-excursion (beginning-of-line) ;if point is on the first
yuuji@52 2425 (setq end (point)) ;non-whitespace char
yuuji@52 2426 (skip-chars-forward " \t")
yuuji@52 2427 (equal (point) p))
yuuji@52 2428 (setq fill-prefix (buffer-substring p end)))
yuuji@53 2429 ((and ;;(not YaTeX-emacs-19)
yuuji@52 2430 (string-match YaTeX-itemizing-env-regexp
yuuji@53 2431 (or (YaTeX-inner-environment t) "document"))
yuuji@58 2432 (setq ii (YaTeX-get-item-info)))
yuuji@58 2433 (save-excursion
yuuji@58 2434 (beginning-of-line)
yuuji@58 2435 (indent-to-column (car (cdr ii)))
yuuji@58 2436 (setq fill-prefix
yuuji@58 2437 (buffer-substring (point) (point-beginning-of-line)))
yuuji@58 2438 (delete-region (point) (progn (beginning-of-line) (point))))))
yuuji@51 2439 (mark-paragraph)
yuuji@51 2440 (save-restriction
yuuji@51 2441 (narrow-to-region (region-beginning) (region-end))
yuuji@52 2442 (YaTeX-remove-trailing-comment (point-min) (point-max))
yuuji@58 2443 (goto-char (point-min))
yuuji@51 2444 (while (YaTeX-re-search-active-forward
yuuji@58 2445 verbrex YaTeX-comment-prefix (point-max) t)
yuuji@51 2446 (setq end (match-beginning 3))
yuuji@51 2447 (goto-char (match-beginning 2))
yuuji@51 2448 (while (re-search-forward "\\s " end t)
yuuji@51 2449 (setq poslist (cons (make-marker) poslist)
yuuji@51 2450 spacelist (cons (preceding-char) spacelist))
yuuji@51 2451 (replace-match "_")
yuuji@51 2452 (set-marker (car poslist) (match-beginning 0))))
yuuji@58 2453 ;;(fill-paragraph arg)
yuuji@58 2454 (fill-region-as-paragraph (point-min) (point-max) arg)
yuuji@51 2455 (while spacelist
yuuji@68 2456 (goto-char (car poslist))
yuuji@68 2457 (set-marker (car poslist) nil)
yuuji@51 2458 (delete-char 1)
yuuji@51 2459 (insert (car spacelist))
yuuji@52 2460 (setq spacelist (cdr spacelist) poslist (cdr poslist)))
yuuji@52 2461 (goto-char (point-min))
yuuji@52 2462 (forward-word 1)
yuuji@52 2463 (beginning-of-line)
yuuji@59 2464 (while (re-search-forward "\\\\\\(\\(page\\)?ref\\|cite\\){" nil t)
yuuji@59 2465 (if (< (point-end-of-line)
yuuji@59 2466 (save-excursion (forward-char -1) (forward-list 1) (point)))
yuuji@59 2467 (progn (end-of-line) (insert YaTeX-comment-prefix))))
yuuji@59 2468 (goto-char (point-min))
yuuji@52 2469 (if (and NTT-jTeX (looking-at "[ \t]"))
yuuji@52 2470 (progn
yuuji@52 2471 (goto-char (point-min))
yuuji@52 2472 (while (not (eobp))
yuuji@52 2473 (end-of-line)
yuuji@52 2474 (or (bolp)
yuuji@52 2475 (save-excursion
yuuji@52 2476 (backward-word 1)
yuuji@52 2477 (looking-at "\\sw+")) ;is not japanese string
yuuji@59 2478 (progn (setq p (point)) (insert YaTeX-comment-prefix)))
yuuji@52 2479 (forward-line 1))
yuuji@52 2480 (goto-char p)
yuuji@61 2481 (if (looking-at "%") (delete-char 1)) ;remove last inserted `%'
yuuji@69 2482 ))))))))
yuuji@51 2483
yuuji@52 2484 (if (fboundp 'YaTeX-saved-indent-new-comment-line) nil
yuuji@52 2485 (fset 'YaTeX-saved-indent-new-comment-line
yuuji@52 2486 (symbol-function 'indent-new-comment-line))
yuuji@52 2487 (fset 'indent-new-comment-line 'YaTeX-indent-new-comment-line))
yuuji@52 2488
yuuji@58 2489 (defun YaTeX-indent-new-comment-line (&optional soft)
yuuji@58 2490 "Tuned `indent-new-comment-line' function for yatex.
yuuji@58 2491 See the documentation of `YaTeX-saved-indent-new-comment-line'."
yuuji@69 2492 (interactive)
yuuji@52 2493 (cond
yuuji@60 2494 ((or (not (memq major-mode '(yatex-mode yahtml-mode)))
yuuji@53 2495 (string-match
yuuji@53 2496 "document"
yuuji@53 2497 (or (and (boundp 'inenv) inenv)
yuuji@53 2498 (or (YaTeX-inner-environment t) "document"))))
yuuji@58 2499 (apply 'YaTeX-saved-indent-new-comment-line (if soft (list soft))))
yuuji@60 2500 ; ((and (eq major-mode 'yahtml-mode)
yuuji@60 2501 ; (string-match
yuuji@60 2502 ; "^[Pp][Rr][Ee]" (yahtml-inner-environment-but "^[Aa]\\b" t)))
yuuji@60 2503 ; (yahtml-indent-new-commnet-line))
yuuji@61 2504 ((and (eq major-mode 'yatex-mode) ;1997/2/4
yuuji@61 2505 (YaTeX-in-math-mode-p)) nil) ;1996/12/30
yuuji@58 2506 (t (let (fill-prefix)
yuuji@69 2507 (apply 'YaTeX-saved-indent-new-comment-line (if soft (list soft)))))))
yuuji@51 2508
yuuji@5 2509 (defun YaTeX-fill-* ()
yuuji@5 2510 "Fill paragraph according to its condition."
yuuji@5 2511 (interactive)
yuuji@5 2512 (cond
yuuji@5 2513 ((YaTeX-fill-item))
yuuji@69 2514 ))
yuuji@5 2515
yuuji@22 2516 ;; Accent completion
yuuji@5 2517 (defun YaTeX-read-accent-char (x)
yuuji@5 2518 "Read char in accent braces."
yuuji@5 2519 (let ((c (read-char)))
yuuji@5 2520 (concat
yuuji@5 2521 (if (and (or (= c ?i) (= c ?j))
yuuji@5 2522 (not (string-match (regexp-quote x) "cdb")))
yuuji@5 2523 "\\" "")
yuuji@69 2524 (char-to-string c))))
yuuji@5 2525
yuuji@5 2526 (defun YaTeX-make-accent ()
yuuji@5 2527 "Make accent usage."
yuuji@5 2528 (interactive)
yuuji@5 2529 (message "1:` 2:' 3:^ 4:\" 5:~ 6:= 7:. u v H t c d b")
yuuji@5 2530 (let ((c (read-char))(case-fold-search nil))
yuuji@5 2531 (setq c (cond ((and (> c ?0) (< c ?8))
yuuji@5 2532 (substring "`'^\"~=." (1- (- c ?0)) (- c ?0)))
yuuji@5 2533 ((= c ?h) "H")
yuuji@5 2534 (t (char-to-string c))))
yuuji@5 2535 (if (not (string-match c "`'^\"~=.uvHtcdb")) nil
yuuji@5 2536 (insert "\\" c "{}")
yuuji@5 2537 (backward-char 1)
yuuji@5 2538 (insert (YaTeX-read-accent-char c))
yuuji@5 2539 (if (string= c "t") (insert (YaTeX-read-accent-char c)))
yuuji@69 2540 (forward-char 1))))
yuuji@5 2541
yuuji@22 2542 ;; Indentation
yuuji@22 2543 (defun YaTeX-current-indentation ()
yuuji@22 2544 "Return the indentation of current environment."
yuuji@22 2545 (save-excursion
yuuji@53 2546 ;;(beginning-of-line)
yuuji@53 2547 (if (YaTeX-beginning-of-environment t)
yuuji@53 2548 (goto-char (get 'YaTeX-inner-environment 'point))
yuuji@53 2549 (forward-line -1)
yuuji@53 2550 (beginning-of-line)
yuuji@53 2551 (skip-chars-forward " \t"))
yuuji@69 2552 (current-column)))
yuuji@22 2553
yuuji@53 2554 (defun YaTeX-previous-line-indentation ()
yuuji@53 2555 (save-excursion
yuuji@53 2556 (forward-line -1)
yuuji@53 2557 (skip-chars-forward " \t")
yuuji@53 2558 (current-column)))
yuuji@53 2559
yuuji@22 2560 (defun YaTeX-indent-line ()
yuuji@22 2561 "Indent corrent line referrin current environment."
yuuji@22 2562 (interactive)
yuuji@48 2563 (let ((indent-relative
yuuji@48 2564 (function
yuuji@51 2565 (lambda (&optional additional)
yuuji@48 2566 (YaTeX-reindent
yuuji@51 2567 (+ (YaTeX-current-indentation)
yuuji@51 2568 (or additional 0)
yuuji@51 2569 YaTeX-environment-indent)))))
yuuji@68 2570 sect depth iteminfo (p (point)) pp (peol (point-end-of-line))
yuuji@52 2571 ;;inenv below is sometimes defined in YaTeX-indent-new-comment-line
yuuji@52 2572 (inenv (or (and (boundp 'inenv) inenv) (YaTeX-inner-environment t))))
yuuji@52 2573 ;;(if NTT-jTeX ;;Do you need this section?
yuuji@52 2574 ;; (save-excursion
yuuji@52 2575 ;; (end-of-line)
yuuji@52 2576 ;; (let ((p (point)))
yuuji@52 2577 ;; (forward-line -1)
yuuji@52 2578 ;; (end-of-line)
yuuji@52 2579 ;; (or (= p (point))
yuuji@52 2580 ;; (progn (backward-char (length YaTeX-comment-prefix))
yuuji@52 2581 ;; (not (looking-at (regexp-quote YaTeX-comment-prefix))))
yuuji@52 2582 ;; (progn
yuuji@52 2583 ;; (skip-chars-backward YaTeX-comment-prefix)
yuuji@52 2584 ;; (kill-line))))))
yuuji@52 2585 (or inenv (setq inenv "document")) ;is the default environment
yuuji@48 2586 (cond
yuuji@70 2587 ((and (YaTeX-on-begin-end-p)
yuuji@70 2588 (or
yuuji@70 2589 (match-beginning 2) ;if \end
yuuji@70 2590 (and (match-beginning 3) ;if \) \]
yuuji@70 2591 (= (char-syntax (char-after (1+ (match-beginning 3)))) ?\)))))
yuuji@70 2592 (YaTeX-reindent
yuuji@70 2593 (save-excursion
yuuji@70 2594 (YaTeX-goto-corresponding-environment)
yuuji@70 2595 (current-column))))
yuuji@53 2596 ((string-match YaTeX-equation-env-regexp inenv)
yuuji@68 2597 (YaTeX-indent-line-equation)) ;autoload-ed from yatexenv
yuuji@52 2598 (;(YaTeX-in-environment-p '("itemize" "enumerate" "description" "list"))
yuuji@52 2599 (string-match YaTeX-itemizing-env-regexp inenv)
yuuji@52 2600 ;;(YaTeX-on-item-p) ??
yuuji@53 2601 ;;(setq iteminfo (YaTeX-get-item-info t))
yuuji@53 2602 (if (save-excursion
yuuji@51 2603 (beginning-of-line)
yuuji@53 2604 (re-search-forward YaTeX-item-regexp peol t))
yuuji@68 2605 (progn
yuuji@68 2606 (save-excursion
yuuji@68 2607 (goto-char (1+ (match-beginning 0)))
yuuji@68 2608 (setq depth
yuuji@68 2609 (* YaTeX-environment-indent
yuuji@68 2610 (cond
yuuji@68 2611 ((looking-at "subsubsub") 3)
yuuji@68 2612 ((looking-at "subsub") 2)
yuuji@68 2613 ((looking-at "sub") 1)
yuuji@68 2614 (t 0)))))
yuuji@51 2615 (funcall indent-relative depth))
yuuji@58 2616 (YaTeX-reindent (or (car (cdr (YaTeX-get-item-info nil inenv)))
yuuji@53 2617 (+ (save-excursion
yuuji@53 2618 (beginning-of-line)
yuuji@53 2619 (YaTeX-current-indentation))
yuuji@52 2620 YaTeX-environment-indent))))
yuuji@48 2621 )
yuuji@49 2622 ((YaTeX-literal-p) ;verbatims
yuuji@48 2623 (tab-to-tab-stop))
yuuji@52 2624 ((and inenv (not (equal "document" inenv)))
yuuji@48 2625 (funcall indent-relative))
yuuji@53 2626 ((YaTeX-on-section-command-p YaTeX-sectioning-regexp)
yuuji@53 2627 (require 'yatexsec) ;to know YaTeX-sectioning-level
yuuji@68 2628 (setq sect (YaTeX-match-string 1))
yuuji@68 2629 (if (string-match "\\*$" sect)
yuuji@68 2630 (setq sect (substring sect 0 -1)))
yuuji@53 2631 (YaTeX-reindent
yuuji@53 2632 (* (max
yuuji@53 2633 (1- ;I want chapter to have indentation 0
yuuji@68 2634 (or (cdr (assoc sect YaTeX-sectioning-level))
yuuji@53 2635 0))
yuuji@53 2636 0)
yuuji@53 2637 YaTeX-environment-indent)))
yuuji@53 2638 ;;Default movement
yuuji@48 2639 ((and (bolp) fill-prefix) (insert fill-prefix))
yuuji@48 2640 (t (save-excursion
yuuji@48 2641 (beginning-of-line)
yuuji@64 2642 (if fill-prefix
yuuji@64 2643 (progn
yuuji@64 2644 (delete-region (point)
yuuji@64 2645 (progn (skip-chars-forward " \t")
yuuji@64 2646 (point)))
yuuji@64 2647 (insert fill-prefix))
yuuji@64 2648 (skip-chars-forward " \t")
yuuji@64 2649 (indent-relative-maybe)))
yuuji@52 2650 (skip-chars-forward " \t")))
yuuji@53 2651 ;;if current line is \begin, re-indent \end too
yuuji@53 2652 (if (and (YaTeX-on-begin-end-p) (match-beginning 1))
yuuji@53 2653 (save-excursion
yuuji@53 2654 ;;(beginning-of-line)
yuuji@53 2655 ;;(search-forward "\\begin")
yuuji@53 2656 (goto-char (match-beginning 0))
yuuji@53 2657 (setq depth (current-column))
yuuji@53 2658 (YaTeX-goto-corresponding-environment)
yuuji@53 2659 (YaTeX-reindent depth)))
yuuji@59 2660 (if (or
yuuji@59 2661 (and NTT-jTeX
yuuji@59 2662 (save-excursion (beginning-of-line) (looking-at "[ \t]")))
yuuji@59 2663 (save-excursion
yuuji@59 2664 (beginning-of-line)
yuuji@59 2665 (backward-char 1)
yuuji@59 2666 (and
yuuji@59 2667 (re-search-backward
yuuji@59 2668 "\\\\\\(\\(page\\)?ref\\|cite\\){" (point-beginning-of-line) t)
yuuji@59 2669 (goto-char (1- (match-end 0)))
yuuji@59 2670 (> (save-excursion
yuuji@59 2671 (condition-case ()
yuuji@59 2672 (progn (forward-list 1) (point))
yuuji@59 2673 (error (point-max))))
yuuji@59 2674 (point-end-of-line)))))
yuuji@52 2675 (save-excursion
yuuji@52 2676 (end-of-line)
yuuji@52 2677 (let ((p (point)))
yuuji@52 2678 (forward-line -1)
yuuji@52 2679 (end-of-line)
yuuji@52 2680 (or (= p (point))
yuuji@52 2681 (looking-at (regexp-quote YaTeX-comment-prefix))
yuuji@52 2682 (bobp) (bolp)
yuuji@52 2683 (save-excursion
yuuji@52 2684 (backward-word 1)
yuuji@52 2685 (looking-at "\\sw+")) ;is not japanese string
yuuji@69 2686 (insert YaTeX-comment-prefix)))))))
yuuji@22 2687
yuuji@70 2688 (defun YaTeX-latex2e-p ()
yuuji@70 2689 (let ((b (current-buffer))
yuuji@70 2690 (ptn (concat YaTeX-ec "documentclass")))
yuuji@70 2691 (unwind-protect
yuuji@70 2692 (or (save-excursion (search-backward ptn nil t))
yuuji@70 2693 (progn
yuuji@70 2694 (YaTeX-visit-main t)
yuuji@70 2695 (save-excursion (search-backward ptn nil t))))
yuuji@70 2696 (set-buffer b))))
yuuji@70 2697
yuuji@7 2698 (provide 'yatex)
yuuji@7 2699 (defvar yatex-mode-load-hook nil
yuuji@7 2700 "*List of functions to be called when yatex.el is loaded.")
yuuji@53 2701 (if (and YaTeX-emacs-19 window-system (not (featurep 'yatex19)))
yuuji@55 2702 (load "yatex19"))
yuuji@51 2703 (load "yatexhks" t)
yuuji@51 2704
yuuji@51 2705 ;;-------------------- Final hook jobs --------------------
yuuji@51 2706 (substitute-all-key-definition
yuuji@51 2707 'fill-paragraph 'YaTeX-fill-paragraph YaTeX-mode-map)
yuuji@7 2708 (run-hooks 'yatex-mode-load-hook)
yuuji@51 2709
yuuji@51 2710 ;; `History' was moved to ChangeLog
yuuji@0 2711 ;----------------------------- End of yatex.el -----------------------------