├── .dir-locals.el ├── .gitignore ├── CHANGES.org ├── Makefile ├── README └── cdlatex.el /.dir-locals.el: -------------------------------------------------------------------------------- 1 | ;;; Directory Local Variables 2 | ;;; For more information see (info "(emacs) Directory Variables") 3 | 4 | ((emacs-lisp-mode . ((indent-tabs-mode . nil) 5 | (show-trailing-whitespace . t)))) 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | test/* 3 | *.elc 4 | /cdlatex-pkg.el 5 | /cdlatex-autoloads.el 6 | -------------------------------------------------------------------------------- /CHANGES.org: -------------------------------------------------------------------------------- 1 | This file documents some more visible changes, in particular if they 2 | change previous behavior. 3 | 4 | 5 | * Version 4.18 6 | - Fix bug with intersion of ' in normal text when not modifier is 7 | defined. 8 | - Fix the display of automatic help for the many-characger mode. 9 | - Added an equation* template, F to insert \Phi, and a key to insert 10 | mathfrak characters. Thanks to Doxdrum. 11 | * Version 4.17 12 | - Fixed bug when cdlatex-math-symbol-alist has more than 3 levels. 13 | Thanks to Yuchen Lea for reporting this issue. 14 | 15 | * Version 4.16 16 | - Integrated cdlatex-electricindex-mode 17 | - Environments keep the indentation when called from a position with 18 | indentation. 19 | * Version 4.15 20 | When the region is active, pressing dollar will bracket the region 21 | with two $ characters, putting it into math mode. 22 | 23 | * Version 4.13 24 | - The default value of 25 | `cdlatex-cdlatex-make-sub-superscript-roman-if-pressed-twice' is now 26 | t, meaning the feature is turned on by default. 27 | - We used to use _{\rm ...} to create roman subscripts. This is not 28 | longer up to standards with LaTeX, so we have now switched to using 29 | \mathrm instead. Thanks to Sinthoras for pointing this out. 30 | - The selection characters for the \overbrace construct has changed. 31 | It used to be '{ and it is now '] . The changes was needed to free 32 | up the { character for the next point. 33 | - It is now easier to inject a \left( ... \right) construct after the 34 | content has already been typed. Just select the content and type '( 35 | Similarly, you can inject left-right pairs or {, [, <, and | in the 36 | same way. Thanks to Sinthoras for this proposal. 37 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | 3 | README: cdlatex.el 4 | perl -ne 'if (/^;;; Commentary/../^;;;;;;;;;/) {s/^;;;? ?//;print}' cdlatex.el > README 5 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Commentary: 2 | 3 | CDLaTeX is a minor mode supporting fast insertion of environment 4 | templates and math stuff in LaTeX. 5 | 6 | To turn CDLaTeX Minor Mode on and off in a particular buffer, use 7 | `M-x cdlatex-mode'. 8 | 9 | To turn on CDLaTeX Minor Mode for all LaTeX files, add one of the 10 | following lines to your .emacs file: 11 | 12 | (add-hook 'LaTeX-mode-hook #'turn-on-cdlatex) ; with AUCTeX LaTeX mode 13 | (add-hook 'latex-mode-hook #'turn-on-cdlatex) ; with Emacs latex mode 14 | 15 | For key bindings, see further down in this documentation. 16 | 17 | CDLaTeX requires texmathp.el (which is distributed with AUCTeX) to 18 | auto-insert $...$ when needed, so we recommend you install AUCTeX to get 19 | this functionality. 20 | 21 | -------------------------------------------------------------------------- 22 | 23 | OVERVIEW 24 | ======== 25 | 26 | CDLaTeX is a minor mode supporting mainly mathematical and scientific 27 | text development with LaTeX. CDLaTeX is really about speed. AUCTeX 28 | (the major mode I recommend for editing LaTeX files) does have a hook 29 | based system for inserting environments and macros - but while this is 30 | useful and general, it is sometimes slow to use. CDLaTeX tries to be 31 | quick, with very few and easy to remember keys, and intelligent 32 | on-the-fly help. 33 | 34 | 1. ABBREVIATIONS. 35 | ------------- 36 | CDLaTeX has an abbrev-like mechanism to insert full LaTeX 37 | environments and other templates into the buffer. Abbreviation 38 | expansion is triggered with the TAB key only, not with SPC or RET. 39 | For example, typing "ite" inserts an itemize environment. A 40 | full list of defined abbreviations is available with the command 41 | `C-c ?' (`cdlatex-command-help'). 42 | 43 | 1a. ENVIRONMENT TEMPLATES 44 | --------------------- 45 | Typing `C-c {' (`cdlatex-environment') uses the minibuffer to 46 | complete the name of a LaTeX environment and inserts a template 47 | for this environment into the buffer. These environment 48 | templates also contain labels created with RefTeX. In a 49 | template, text needs to be filled in at various places, which we 50 | call "points of interest". You can use the TAB key to jump to 51 | the next point of interest in the template. If there is an 52 | active region, the region will be wrapped into the environment, 53 | ignoring the template content. 54 | 55 | For many frequently used LaTeX environments, abbreviations are 56 | available. Most of the time, the abbreviation consists of the 57 | first three letters of the environment name: `equ' expands 58 | into 59 | \begin{equation} 60 | \label{eq:1} 61 | 62 | \end{equation} 63 | 64 | Similarly, `ali' inserts an AMS-LaTeX align environment 65 | template etc. For a full list of environment abbreviations, use 66 | `C-c ?'. 67 | 68 | Use the command `C-c -' (`cdlatex-item') to insert a generalized 69 | new "item" in any "list"-like environment. For example, in an 70 | itemize environment, this inserts "\item", in an enumerate 71 | environment it inserts "\item\label{item:25}" and in an eqnarray 72 | environment, it inserts "\label{eq:25} \n & &". When 73 | appropriate, newlines are inserted, and the previous item is also 74 | closed with "\\". `cdlatex-item' can also be invoked with the 75 | abbreviation "it". 76 | 77 | 1b. MATH TEMPLATES 78 | -------------- 79 | Abbreviations are also used to insert simple math templates 80 | into the buffer. The cursor will be positioned properly. For 81 | example, typing `fr' will insert "\frac{}{}" with the 82 | cursor in the first pair of parenthesis. Typing `lr(' 83 | will insert a "\left( \right)" pair and position the cursor in 84 | between, etc. Again, the TAB key can be used to jump to the 85 | points in the template where additional text has to be 86 | inserted. For example in the `\frac{}{}' template, it will 87 | move you from the first argument to the second and then out of 88 | the second. For a list of available templates, type `C-c ?'. 89 | As a special case, the left-right parenthesis combinations can 90 | also be applied after writing the content by selecting the 91 | content and typing '( or '[ or '{ or '< or '| . 92 | 93 | 2. MATHEMATICAL SYMBOLS 94 | -------------------- 95 | This feature is similar to the functionality in the Math minor mode 96 | of AUCTeX, and to the input methods of the X-Symbol package. It is 97 | introduced by the backquote character. Backquote followed by any 98 | character inserts a LaTeX math macro into the buffer. If 99 | necessary, a pair of "$" is inserted to switch to math mode. For 100 | example, typing "`a" inserts "$\alpha$". Since LaTeX defines many 101 | more mathematical symbols than the alphabet has letters, different 102 | sets of math macros are provided. We call the different sets 103 | "levels". On each level, another LaTeX macro is assigned to a 104 | given letter. To select the different levels, simply press the 105 | backquote character several times before pressing the letter. For 106 | example, typing "`d" inserts "\delta" (level 1), and typing "``d" 107 | inserts "\partial" (level 2). Similarly, "`e" inserts "\epsilon" 108 | and "``e" inserts "\vareppsilon". 109 | 110 | On each level, on-thy-fly help will pop up automatically if you 111 | hesitate to press the next key. The help screen is a window which 112 | lists all math macros available on the current level. Initially, 113 | when you type slowly, this window will pop up each time you press 114 | backquote. However, after you have learned the different keys, you 115 | will type more quickly and the help window is not shown. Try it 116 | out: First press "`" (backquote), wait for the help window and then 117 | press "a" to get "\alpha". Then press "`" and "b" as a quick 118 | sequence to get "\beta", without the help window. 119 | 120 | The LaTeX macros available through this mechanism are fully 121 | configurable - see the variable `cdlatex-math-symbol-alist'. 122 | 123 | 3. ACCENTS AND FONTS 124 | ----------------- 125 | Putting accents on mathematical characters and/or changing the font 126 | of a character uses key combinations with the quote character "'" 127 | as a prefix. The accent or font change is applied to the character 128 | or LaTeX macro *before* point. For example 129 | 130 | Keys Result 131 | -------------------------------------------------------------------- 132 | a'~ ERROR % in text mode 133 | $a'~ \tilde{a} % in math mode 134 | a': \ddot{a} 135 | ab'b \textbf{ab} % in text mode 136 | $ab'b a\mathbf{b} % in math mode 137 | \alpha'. \dot{\alpha} 138 | r_{dust}'r r_\mathrm{dust} % in math mode 139 | 'e \emph{} 140 | this is important M-2 'b this \textbf{is important} 141 | 142 | As you can see: 143 | - using math accents like ~ outside math mode will throw an error. 144 | - the font change used automatically adapts to math mode. 145 | - if the item before point is a LaTeX macro, the change applies to 146 | the whole macro. 147 | - in text mode, the change applies to the entire word before point, 148 | while in math mode only the last character is modified. 149 | - if the character before point is white space, a dollar or an 150 | opening parenthesis, this command just opens an empty template 151 | and positions the cursor inside. 152 | - when a numeric prefix argument is supplied, the command acts on 153 | whole words before the cursor. 154 | - Furthermore, if there is an active region, the change will apply 155 | to the content of that region. 156 | 157 | In order to insert a normal quote, you can press the quote 158 | character twice. Also, if the key character is not associated with 159 | an accent or font, the quote will be inserted. For example, "'t" 160 | and "'s" insert just that, so that normal text typing will not be 161 | disturbed. Just like during the insertion of math macros (see above 162 | under (4.)), automatic on-the-fly help will pop up when you pause 163 | after hitting the quote character, but will be suppressed when you 164 | continue quickly. The available accents and also the prefix key 165 | can be can be configured - see documentation of the variables 166 | `cdlatex-math-modify-alist' and `cdlatex-math-modify-prefix'. 167 | 168 | 4. PAIR INSERTION of (), [], {}, and $$ 169 | ------------------------------------ 170 | Dollars and parens can be inserted as pairs. When you type the 171 | opening delimiter, the closing delimiter will be inserted as well, 172 | and the cursor positioned between them. You can configure which 173 | delimiter are inserted pairwise by configuring the variable 174 | `cdlatex-paired-parens'. 175 | 176 | Also, the keys `_' and `^' will insert "_{}" and "^{}", 177 | respectively, and, if necessary, also a pair of dollar signs to 178 | switch to math mode. You can use TAB to exit paired parenthesis. 179 | As a special case, when you use TAB to exit a pair of braces that 180 | belong to a subscript or superscript, CDLaTeX removes the braces if 181 | the sub/superscript consists of a single character. For example 182 | typing "$10^3" inserts "$10^3$", but typing "$10^34" 183 | inserts "$10^{34}$". 184 | 185 | If you press `_' or `^' twice, the template inserted will be 186 | `_{\mathrm{}}' or `^{\mathrm }', respectively, to insert a roman 187 | sub/super-script. Style guides require that all sub- and 188 | superscripts that are descriptive (so not a mathematical or 189 | physical quantity themselves) need to be roman. So $x_i$ is i 190 | is an index, but $x_{\rm max}$ to indicate the maximum value. You 191 | can disable this behavior through the variable 192 | `cdlatex-make-sub-superscript-roman-if-pressed-twice'. 193 | 194 | 5. THE OVERLOADED TAB KEY 195 | ---------------------- 196 | You may have noticed that we use the TAB key for many different 197 | purposes in this package. While this may seem confusing, I have 198 | gotten used to this very much. Hopefully this will work for you as 199 | well: "when in doubt, press TAB". Here is a summary of what happens 200 | when you press the TAB key: 201 | 202 | The function first tries to expand any abbreviation before point. 203 | 204 | If there is none, it cleans up short subscripts and superscripts at 205 | point. I.e., is the cursor is just before the closing brace in 206 | "a^{2}", it changes it to "a^2", since this is more readable. If 207 | you want to keep the braces also for simple superscripts and 208 | subscripts, set the variable `cdlatex-simplify-sub-super-scripts' 209 | to nil. 210 | 211 | After that, the TAB function jumps to the next point of interest in 212 | a LaTeX text where one would reasonably expect that more input can 213 | be put in. This does *not* use special markers in the template, 214 | but a heuristic method which works quite well. For the detailed 215 | rules which govern this feature, check the documentation of the 216 | function `cdlatex-tab'. 217 | 218 | ----------------------------------------------------------------------------- 219 | 220 | CONFIGURATION EXAMPLES 221 | ====================== 222 | 223 | Check out the documentation of the variables in the configuration 224 | section. The variables must be set before cdlatex-mode is turned on, 225 | or, at the latext, in `cdlatex-mode-hook', in order to be effective. 226 | When changing the variables, toggle the mode off and on to make sure 227 | that everything is up to date. 228 | 229 | Here is how you might configure CDLaTeX to provide environment templates 230 | (including automatic labels) for two theorem-like environments. 231 | 232 | (setq cdlatex-env-alist 233 | '(("axiom" "\\begin{axiom}\nAUTOLABEL\n?\n\\end{axiom}\n" nil) 234 | ("theorem" "\\begin{theorem}\nAUTOLABEL\n?\n\\end{theorem}\n" nil))) 235 | 236 | The "AUTOLABEL" indicates the place where an automatic label should be 237 | inserted, using RefTeX. The question mark defines the position of the 238 | cursor after the template has been inserted into the buffer. 239 | 240 | You could also define your own keyword commands "axm" and "thr" to make 241 | the template insertion quicker (e.g. `axm' and `thm'): 242 | 243 | (setq cdlatex-command-alist 244 | '(("axm" "Insert axiom env" "" cdlatex-environment ("axiom") t nil) 245 | ("thr" "Insert theorem env" "" cdlatex-environment ("theorem") t nil))) 246 | 247 | Here is how to add new math symbols to CDLaTeX's list: In order to put 248 | all rightarrow commands onto `>, ``>, ```>, and ````> (i.e. several 249 | backquotes followed by >) and all leftarrow commands onto '<, ``<, ```<, 250 | and ````<, you could do this in .emacs: 251 | 252 | (setq cdlatex-math-symbol-alist 253 | '((?< ("\\leftarrow" "\\Leftarrow" "\\longleftarrow" "\\Longleftarrow")) 254 | (?> ("\\rightarrow" "\\Rightarrow" "\\longrightarrow" "\\Longrightarrow")) 255 | )) 256 | 257 | To change the prefix key for math accents and font switching, you could 258 | do something like 259 | 260 | (setq cdlatex-math-modify-prefix [f7]) 261 | ----------------------------------------------------------------------------- 262 | 263 | KEY BINDINGS 264 | 265 | Here is the default set of keybindings from CDLaTeX. A menu is also 266 | installed. 267 | 268 | $ cdlatex-dollar 269 | ( cdlatex-pbb 270 | { cdlatex-pbb 271 | [ cdlatex-pbb 272 | | cdlatex-pbb 273 | < cdlatex-pbb 274 | ^ cdlatex-sub-superscript 275 | _ cdlatex-sub-superscript 276 | 277 | TAB cdlatex-tab 278 | C-c ? cdlatex-command-help 279 | C-c { cdlatex-environment 280 | C-c - cdlatex-item 281 | ` cdlatex-math-symbol 282 | ' cdlatex-math-modify 283 | 284 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 285 | -------------------------------------------------------------------------------- /cdlatex.el: -------------------------------------------------------------------------------- 1 | ;;; cdlatex.el --- Fast input methods for LaTeX environments and math -*- lexical-binding: t; -*- 2 | ;; Copyright (c) 2010-2022 Free Software Foundation, Inc. 3 | ;; 4 | ;; Author: Carsten Dominik 5 | ;; Keywords: tex 6 | ;; Version: 4.18e 7 | ;; 8 | ;; This file is not part of GNU Emacs. 9 | ;; 10 | ;; This file is free software: you can redistribute it and/or modify 11 | ;; it under the terms of the GNU General Public License as published by 12 | ;; the Free Software Foundation, either version 3 of the License, or 13 | ;; (at your option) any later version. 14 | 15 | ;; cdlatex.el is distributed in the hope that it will be useful, 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | ;; GNU General Public License for more details. 19 | 20 | ;; You should have received a copy of the GNU General Public License 21 | ;; along with cdlatex.el. If not, see . 22 | ;; 23 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 24 | ;; 25 | ;;; Commentary: 26 | ;; 27 | ;; CDLaTeX is a minor mode supporting fast insertion of environment 28 | ;; templates and math stuff in LaTeX. 29 | ;; 30 | ;; To turn CDLaTeX Minor Mode on and off in a particular buffer, use 31 | ;; `M-x cdlatex-mode'. 32 | ;; 33 | ;; To turn on CDLaTeX Minor Mode for all LaTeX files, add one of the 34 | ;; following lines to your .emacs file: 35 | ;; 36 | ;; (add-hook 'LaTeX-mode-hook #'turn-on-cdlatex) ; with AUCTeX LaTeX mode 37 | ;; (add-hook 'latex-mode-hook #'turn-on-cdlatex) ; with Emacs latex mode 38 | ;; 39 | ;; For key bindings, see further down in this documentation. 40 | ;; 41 | ;; CDLaTeX requires texmathp.el (which is distributed with AUCTeX) to 42 | ;; auto-insert $...$ when needed, so we recommend you install AUCTeX to get 43 | ;; this functionality. 44 | ;; 45 | ;;-------------------------------------------------------------------------- 46 | ;; 47 | ;; OVERVIEW 48 | ;; ======== 49 | ;; 50 | ;; CDLaTeX is a minor mode supporting mainly mathematical and scientific 51 | ;; text development with LaTeX. CDLaTeX is really about speed. AUCTeX 52 | ;; (the major mode I recommend for editing LaTeX files) does have a hook 53 | ;; based system for inserting environments and macros - but while this is 54 | ;; useful and general, it is sometimes slow to use. CDLaTeX tries to be 55 | ;; quick, with very few and easy to remember keys, and intelligent 56 | ;; on-the-fly help. 57 | ;; 58 | ;; 1. ABBREVIATIONS. 59 | ;; ------------- 60 | ;; CDLaTeX has an abbrev-like mechanism to insert full LaTeX 61 | ;; environments and other templates into the buffer. Abbreviation 62 | ;; expansion is triggered with the TAB key only, not with SPC or RET. 63 | ;; For example, typing "ite" inserts an itemize environment. A 64 | ;; full list of defined abbreviations is available with the command 65 | ;; `C-c ?' (`cdlatex-command-help'). 66 | ;; 67 | ;; 1a. ENVIRONMENT TEMPLATES 68 | ;; --------------------- 69 | ;; Typing `C-c {' (`cdlatex-environment') uses the minibuffer to 70 | ;; complete the name of a LaTeX environment and inserts a template 71 | ;; for this environment into the buffer. These environment 72 | ;; templates also contain labels created with RefTeX. In a 73 | ;; template, text needs to be filled in at various places, which we 74 | ;; call "points of interest". You can use the TAB key to jump to 75 | ;; the next point of interest in the template. If there is an 76 | ;; active region, the region will be wrapped into the environment, 77 | ;; ignoring the template content. 78 | ;; 79 | ;; For many frequently used LaTeX environments, abbreviations are 80 | ;; available. Most of the time, the abbreviation consists of the 81 | ;; first three letters of the environment name: `equ' expands 82 | ;; into 83 | ;; \begin{equation} 84 | ;; \label{eq:1} 85 | ;; 86 | ;; \end{equation} 87 | ;; 88 | ;; Similarly, `ali' inserts an AMS-LaTeX align environment 89 | ;; template etc. For a full list of environment abbreviations, use 90 | ;; `C-c ?'. 91 | ;; 92 | ;; Use the command `C-c -' (`cdlatex-item') to insert a generalized 93 | ;; new "item" in any "list"-like environment. For example, in an 94 | ;; itemize environment, this inserts "\item", in an enumerate 95 | ;; environment it inserts "\item\label{item:25}" and in an eqnarray 96 | ;; environment, it inserts "\label{eq:25} \n & &". When 97 | ;; appropriate, newlines are inserted, and the previous item is also 98 | ;; closed with "\\". `cdlatex-item' can also be invoked with the 99 | ;; abbreviation "it". 100 | ;; 101 | ;; 1b. MATH TEMPLATES 102 | ;; -------------- 103 | ;; Abbreviations are also used to insert simple math templates 104 | ;; into the buffer. The cursor will be positioned properly. For 105 | ;; example, typing `fr' will insert "\frac{}{}" with the 106 | ;; cursor in the first pair of parenthesis. Typing `lr(' 107 | ;; will insert a "\left( \right)" pair and position the cursor in 108 | ;; between, etc. Again, the TAB key can be used to jump to the 109 | ;; points in the template where additional text has to be 110 | ;; inserted. For example in the `\frac{}{}' template, it will 111 | ;; move you from the first argument to the second and then out of 112 | ;; the second. For a list of available templates, type `C-c ?'. 113 | ;; As a special case, the left-right parenthesis combinations can 114 | ;; also be applied after writing the content by selecting the 115 | ;; content and typing '( or '[ or '{ or '< or '| . 116 | ;; 117 | ;; 2. MATHEMATICAL SYMBOLS 118 | ;; -------------------- 119 | ;; This feature is similar to the functionality in the Math minor mode 120 | ;; of AUCTeX, and to the input methods of the X-Symbol package. It is 121 | ;; introduced by the backquote character. Backquote followed by any 122 | ;; character inserts a LaTeX math macro into the buffer. If 123 | ;; necessary, a pair of "$" is inserted to switch to math mode. For 124 | ;; example, typing "`a" inserts "$\alpha$". Since LaTeX defines many 125 | ;; more mathematical symbols than the alphabet has letters, different 126 | ;; sets of math macros are provided. We call the different sets 127 | ;; "levels". On each level, another LaTeX macro is assigned to a 128 | ;; given letter. To select the different levels, simply press the 129 | ;; backquote character several times before pressing the letter. For 130 | ;; example, typing "`d" inserts "\delta" (level 1), and typing "``d" 131 | ;; inserts "\partial" (level 2). Similarly, "`e" inserts "\epsilon" 132 | ;; and "``e" inserts "\varepsilon". 133 | ;; 134 | ;; On each level, on-thy-fly help will pop up automatically if you 135 | ;; hesitate to press the next key. The help screen is a window which 136 | ;; lists all math macros available on the current level. Initially, 137 | ;; when you type slowly, this window will pop up each time you press 138 | ;; backquote. However, after you have learned the different keys, you 139 | ;; will type more quickly and the help window is not shown. Try it 140 | ;; out: First press "`" (backquote), wait for the help window and then 141 | ;; press "a" to get "\alpha". Then press "`" and "b" as a quick 142 | ;; sequence to get "\beta", without the help window. 143 | ;; 144 | ;; The LaTeX macros available through this mechanism are fully 145 | ;; configurable - see the variable `cdlatex-math-symbol-alist'. 146 | ;; 147 | ;; 3. ACCENTS AND FONTS 148 | ;; ----------------- 149 | ;; Putting accents on mathematical characters and/or changing the font 150 | ;; of a character uses key combinations with the quote character "'" 151 | ;; as a prefix. The accent or font change is applied to the character 152 | ;; or LaTeX macro *before* point. For example 153 | ;; 154 | ;; Keys Result 155 | ;; -------------------------------------------------------------------- 156 | ;; a'~ ERROR % in text mode 157 | ;; $a'~ \tilde{a} % in math mode 158 | ;; a': \ddot{a} 159 | ;; ab'b \textbf{ab} % in text mode 160 | ;; $ab'b a\mathbf{b} % in math mode 161 | ;; \alpha'. \dot{\alpha} 162 | ;; r_{dust}'r r_\mathrm{dust} % in math mode 163 | ;; 'e \emph{} 164 | ;; this is important M-2 'b this \textbf{is important} 165 | ;; 166 | ;; As you can see: 167 | ;; - using math accents like ~ outside math mode will throw an error. 168 | ;; - the font change used automatically adapts to math mode. 169 | ;; - if the item before point is a LaTeX macro, the change applies to 170 | ;; the whole macro. 171 | ;; - in text mode, the change applies to the entire word before point, 172 | ;; while in math mode only the last character is modified. 173 | ;; - if the character before point is white space, a dollar or an 174 | ;; opening parenthesis, this command just opens an empty template 175 | ;; and positions the cursor inside. 176 | ;; - when a numeric prefix argument is supplied, the command acts on 177 | ;; whole words before the cursor. 178 | ;; - Furthermore, if there is an active region, the change will apply 179 | ;; to the content of that region. 180 | ;; 181 | ;; In order to insert a normal quote, you can press the quote 182 | ;; character twice. Also, if the key character is not associated with 183 | ;; an accent or font, the quote will be inserted. For example, "'t" 184 | ;; and "'s" insert just that, so that normal text typing will not be 185 | ;; disturbed. Just like during the insertion of math macros (see above 186 | ;; under (4.)), automatic on-the-fly help will pop up when you pause 187 | ;; after hitting the quote character, but will be suppressed when you 188 | ;; continue quickly. The available accents and also the prefix key 189 | ;; can be can be configured - see documentation of the variables 190 | ;; `cdlatex-math-modify-alist' and `cdlatex-math-modify-prefix'. 191 | ;; 192 | ;; 4. PAIR INSERTION of (), [], {}, and $$ 193 | ;; ------------------------------------ 194 | ;; Dollars and parens can be inserted as pairs. When you type the 195 | ;; opening delimiter, the closing delimiter will be inserted as well, 196 | ;; and the cursor positioned between them. You can configure which 197 | ;; delimiter are inserted pairwise by configuring the variable 198 | ;; `cdlatex-paired-parens'. 199 | ;; 200 | ;; If the region is active when you press $, the region will be 201 | ;; bracketed by a pair of $ characters. 202 | ;; 203 | ;; Also, the keys `_' and `^' will insert "_{}" and "^{}", 204 | ;; respectively, and, if necessary, also a pair of dollar signs to 205 | ;; switch to math mode. You can use TAB to exit paired parenthesis. 206 | ;; As a special case, when you use TAB to exit a pair of braces that 207 | ;; belong to a subscript or superscript, CDLaTeX removes the braces if 208 | ;; the sub/superscript consists of a single character. For example 209 | ;; typing "$10^3" inserts "$10^3$", but typing "$10^34" 210 | ;; inserts "$10^{34}$". 211 | ;; 212 | ;; If you press `_' or `^' twice, the template inserted will be 213 | ;; `_{\mathrm{}}' or `^{\mathrm }', respectively, to insert a roman 214 | ;; sub/super-script. Style guides require that all sub- and 215 | ;; superscripts that are descriptive (so not a mathematical or 216 | ;; physical quantity themselves) need to be roman. So $x_i$ is i 217 | ;; is an index, but $x_{\rm max}$ to indicate the maximum value. You 218 | ;; can disable this behavior through the variable 219 | ;; `cdlatex-make-sub-superscript-roman-if-pressed-twice'. 220 | ;; 221 | ;; 5. THE OVERLOADED TAB KEY 222 | ;; ---------------------- 223 | ;; You may have noticed that we use the TAB key for many different 224 | ;; purposes in this package. While this may seem confusing, I have 225 | ;; gotten used to this very much. Hopefully this will work for you as 226 | ;; well: "when in doubt, press TAB". Here is a summary of what happens 227 | ;; when you press the TAB key: 228 | ;; 229 | ;; The function first tries to expand any abbreviation before point. 230 | ;; 231 | ;; If there is none, it cleans up short subscripts and superscripts at 232 | ;; point. I.e., is the cursor is just before the closing brace in 233 | ;; "a^{2}", it changes it to "a^2", since this is more readable. If 234 | ;; you want to keep the braces also for simple superscripts and 235 | ;; subscripts, set the variable `cdlatex-simplify-sub-super-scripts' 236 | ;; to nil. 237 | ;; 238 | ;; After that, the TAB function jumps to the next point of interest in 239 | ;; a LaTeX text where one would reasonably expect that more input can 240 | ;; be put in. This does *not* use special markers in the template, 241 | ;; but a heuristic method which works quite well. For the detailed 242 | ;; rules which govern this feature, check the documentation of the 243 | ;; function `cdlatex-tab'. 244 | ;; 245 | ;;----------------------------------------------------------------------------- 246 | ;; 247 | ;; CONFIGURATION EXAMPLES 248 | ;; ====================== 249 | ;; 250 | ;; Check out the documentation of the variables in the configuration 251 | ;; section. The variables must be set before cdlatex-mode is turned on, 252 | ;; or, at the latext, in `cdlatex-mode-hook', in order to be effective. 253 | ;; When changing the variables, toggle the mode off and on to make sure 254 | ;; that everything is up to date. 255 | ;; 256 | ;; Here is how you might configure CDLaTeX to provide environment templates 257 | ;; (including automatic labels) for two theorem-like environments. 258 | ;; 259 | ;; (setq cdlatex-env-alist 260 | ;; '(("axiom" "\\begin{axiom}\nAUTOLABEL\n?\n\\end{axiom}\n" nil) 261 | ;; ("theorem" "\\begin{theorem}\nAUTOLABEL\n?\n\\end{theorem}\n" nil))) 262 | ;; 263 | ;; The "AUTOLABEL" indicates the place where an automatic label should be 264 | ;; inserted, using RefTeX. The question mark defines the position of the 265 | ;; cursor after the template has been inserted into the buffer. 266 | ;; 267 | ;; You could also define your own keyword commands "axm" and "thr" to make 268 | ;; the template insertion quicker (e.g. `axm' and `thm'): 269 | ;; 270 | ;; (setq cdlatex-command-alist 271 | ;; '(("axm" "Insert axiom env" "" cdlatex-environment ("axiom") t nil) 272 | ;; ("thr" "Insert theorem env" "" cdlatex-environment ("theorem") t nil))) 273 | ;; 274 | ;; Here is how to add new math symbols to CDLaTeX's list: In order to put 275 | ;; all rightarrow commands onto `>, ``>, ```>, and ````> (i.e. several 276 | ;; backquotes followed by >) and all leftarrow commands onto '<, ``<, ```<, 277 | ;; and ````<, you could do this in .emacs: 278 | ;; 279 | ;; (setq cdlatex-math-symbol-alist 280 | ;; '((?< ("\\leftarrow" "\\Leftarrow" "\\longleftarrow" "\\Longleftarrow")) 281 | ;; (?> ("\\rightarrow" "\\Rightarrow" "\\longrightarrow" "\\Longrightarrow")) 282 | ;; )) 283 | ;; 284 | ;; To change the prefix key for math accents and font switching, you could 285 | ;; do something like 286 | ;; 287 | ;; (setq cdlatex-math-modify-prefix [f7]) 288 | ;;----------------------------------------------------------------------------- 289 | ;; 290 | ;; KEY BINDINGS 291 | ;; 292 | ;; Here is the default set of keybindings from CDLaTeX. A menu is also 293 | ;; installed. 294 | ;; 295 | ;; $ cdlatex-dollar 296 | ;; ( cdlatex-pbb 297 | ;; { cdlatex-pbb 298 | ;; [ cdlatex-pbb 299 | ;; | cdlatex-pbb 300 | ;; < cdlatex-pbb 301 | ;; ^ cdlatex-sub-superscript 302 | ;; _ cdlatex-sub-superscript 303 | ;; 304 | ;; TAB cdlatex-tab 305 | ;; C-c ? cdlatex-command-help 306 | ;; C-c { cdlatex-environment 307 | ;; C-c - cdlatex-item 308 | ;; ` cdlatex-math-symbol 309 | ;; ' cdlatex-math-modify 310 | ;; 311 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 312 | ;; 313 | ;; FAQ 314 | ;; 315 | ;; - Some people find it disturbing that the quote character (') is active 316 | ;; for math accents and font switching. I have tried to avoid any letters 317 | ;; which are frequently following ' in normal text. For example, 's and 't 318 | ;; insert just this. If you still prefer a different prefix key, just 319 | ;; configure the variable `cdlatex-math-modify-prefix'. 320 | ;; 321 | ;; - To insert a backquote into the buffer, use C-q ` 322 | ;; 323 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 324 | ;; 325 | ;;;;;; 326 | 327 | ;;; Code: 328 | 329 | (eval-when-compile (require 'cl-lib)) 330 | (require 'reftex) 331 | 332 | ;;; Begin of Configuration Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 333 | 334 | ;; Configuration Variables and User Options for CDLaTeX ------------------ 335 | 336 | (defgroup cdlatex nil 337 | "LaTeX label and citation support." 338 | :tag "CDLaTeX" 339 | :link '(url-link :tag "Home Page" "http://zon.astro.uva.nl/~dominik/Tools/") 340 | :prefix "cdlatex-" 341 | :group 'tex) 342 | 343 | (defun cdlatex-customize () 344 | "Call the customize function with cdlatex as argument." 345 | (interactive) 346 | (cond 347 | ((fboundp 'customize-browse) 348 | (customize-browse 'cdlatex)) 349 | ((fboundp 'customize-group) 350 | (customize-group 'cdlatex)) 351 | (t (error "No customization available")))) 352 | 353 | (defun cdlatex-create-customize-menu () 354 | "Create a full customization menu for CDLaTeX." 355 | (interactive) 356 | (if (fboundp 'customize-menu-create) 357 | (progn 358 | (easy-menu-change 359 | '("CDLTX") "Customize" 360 | `(["Browse CDLaTeX group" cdlatex-customize t] 361 | "---" 362 | ,(customize-menu-create 'cdlatex) 363 | ["Set" Custom-set t] 364 | ["Save" Custom-save t] 365 | ["Reset to Current" Custom-reset-current t] 366 | ["Reset to Saved" Custom-reset-saved t] 367 | ["Reset to Standard Settings" Custom-reset-standard t])) 368 | (message "\"CDLTX\"-menu now contains full customization menu")) 369 | (error "Cannot expand menu (outdated version of cus-edit.el)"))) 370 | 371 | ;; Configuration of KEYWORD commands ------------------------------------ 372 | 373 | (defgroup cdlatex-keyword-commands nil 374 | "How to type a keyword in the buffer and hit TAB to execute." 375 | :group 'cdlatex) 376 | 377 | (defcustom cdlatex-command-alist nil 378 | "List of abbrev-like commands, available with keyword and TAB. 379 | See `cdlatex-command-alist-default' for examples. This list only 380 | defines additions to the defaults. For a full list of active commands, 381 | press \\[cdlatex-command-help]. 382 | Each element of this list is again a list with the following items: 383 | 0. KEYWORD The key that has to be typed into the text. 384 | 1. DOCSTRING A documentation string, less than 60 characters long. 385 | 2. REPLACE The text to be substituted for the keyword, if any. 386 | 3. HOOK A function to be called. 387 | 4. ARGS Optional list of arguments to the function. 388 | 5. TEXTFLAG non-nil means this keyword command is active in textmode. 389 | 6. MATHFLAG non-nil means this keyword command is active in math mode." 390 | :group 'cdlatex-keyword-commands 391 | :type '(repeat 392 | (list (string :tag "Keyword ") 393 | (string :tag "Docstring ") 394 | (string :tag "Replacement") 395 | (function :tag "Hook ") 396 | (sexp :tag "Arguments ") 397 | (boolean :tag "Available in Text mode") 398 | (boolean :tag "Available in Math mode")))) 399 | 400 | (defcustom cdlatex-tab-hook nil 401 | "List of functions called by TAB before the default command is executed. 402 | These functions are called each time TAB is pressed. They may parse the 403 | environment and take an action. The function should return t when it 404 | successful executed an action and other TAB actions should *not* be tried. 405 | When a return value is nil, other hook functions are tried, followed by the 406 | default action of TAB (see documentation of the command `cdlatex-tab'." 407 | :group 'cdlatex-keyword-commands 408 | :type '(repeat (function :tag "Function" :value nil))) 409 | 410 | ;; Configuration of environment templates ------------------------------- 411 | 412 | (defgroup cdlatex-environment-support nil 413 | "Template-based insertion of LaTeX environments." 414 | :group 'cdlatex) 415 | 416 | (defcustom cdlatex-env-alist nil 417 | "Association list of LaTeX environments and the corresponding templates. 418 | The car of the list is a keyword to identify the environment. 419 | the two following items in the list are the templates for environment 420 | and item. See `cdlatex-env-alist-default' for examples. Any entries 421 | in this variable will be added to the default definitions." 422 | :group 'cdlatex-environment-support 423 | :type '(repeat 424 | (list :tag "" 425 | (string :format "ENVIRONMENT %v" "") 426 | (text :format "ENVIRONMENT TEMPLATE\n%v" "") 427 | (choice :tag "ITEM" 428 | (const :tag "none" nil) 429 | (text :tag "template" :format "TEMPLATE\n%v" ""))))) 430 | 431 | (defcustom cdlatex-insert-auto-labels-in-env-templates t 432 | "Non-nil means the environment templates CDLaTeX will contain labels. 433 | This variable may be set to t, nil, or a string of label type letters 434 | indicating the label types for which it should be true." 435 | :group 'cdlatex-making-and-inserting-labels 436 | :type '(choice :tag "Insert labels in templates" 437 | (const :tag "always" t) 438 | (const :tag "never" nil) 439 | (string :tag "selected label types" ""))) 440 | 441 | ;; Configuration of Math character insertion and accents ---------------- 442 | 443 | (defgroup cdlatex-math-support nil 444 | "Support for mathematical symbols and accents in CDLaTeX." 445 | :group 'cdlatex) 446 | 447 | (defcustom cdlatex-math-symbol-prefix ?` 448 | "Prefix key for `cdlatex-math-symbol'. 449 | This may be a character, a string readable with `read-kbd-macro', or a 450 | Lisp vector." 451 | :group 'cdlatex-math-support 452 | :type '(choice 453 | (character) 454 | (string :value "" :tag "kbd readable string") 455 | (sexp :value [] :tag "a lisp vector"))) 456 | 457 | (defcustom cdlatex-math-symbol-direct-bindings '(nil nil nil) 458 | "How to bind the math symbols directly. 459 | This is a list of key binding descriptions for different levels of 460 | math symbols. First entry for level 1 etc. 461 | Each entry consists of a prefix key and a list of modifiers for the 462 | character. The prefix key can be nil, or any of a character, a 463 | `read-kbd-macro' readable string or a vector. 464 | Examples: 465 | `((nil alt))' bind `\\delta' to `A-d'. 466 | `((\"C-c C-f\"))' bind `\\delta' to `C-c C-f d'. 467 | `((nil alt) (nil alt control))' bind `\\delta' to `A-d' and 468 | `\\partial' (which is on level 2) 469 | to `A-C-d'" 470 | :group 'cdlatex-math-support 471 | :type '(repeat 472 | (choice 473 | (const :tag "No binding of this level" nil) 474 | (cons 475 | :tag "Specify a binding" 476 | :value (nil alt) 477 | (choice 478 | (const :tag "No prefix" nil) 479 | (character :value ?@) 480 | (string :value "" :tag "kbd readable string") 481 | (sexp :value [] :tag "a lisp vector")) 482 | (set :tag "Modifiers for the final character" :greedy t 483 | (const control) 484 | (const meta) 485 | (const alt) 486 | (const super) 487 | (const hyper)))))) 488 | 489 | (defcustom cdlatex-math-symbol-alist nil 490 | "Key characters and math symbols for fast access with the prefix key. 491 | First element is a character, followed by a number of strings attached to 492 | this key. When the string contains a question mark, this is where the 493 | cursor will be positioned after insertion of the string into the buffer. 494 | See `cdlatex-math-symbol-alist-default' for an example. Any entry defined 495 | here will replace the corresponding entry of the default list. The 496 | defaults implement 3 levels of symbols so far: Level 1 for Greek letters 497 | and standard symbols, level 2 for variations of level 1, and level 3 for 498 | functions and operators." 499 | :group 'cdlatex-math-support 500 | :type '(repeat 501 | (list 502 | (character ?a) 503 | (repeat (string :tag "macro" ""))))) 504 | 505 | (defcustom cdlatex-math-modify-prefix ?' 506 | "Prefix key for `cdlatex-math-modify'. 507 | It can be a character, a string interpretable with `read-kbd-macro', 508 | or a Lisp vector." 509 | :group 'cdlatex-math-support 510 | :type '(choice 511 | (character) 512 | (string :value "" :tag "kbd readable string") 513 | (sexp :value [] :tag "a lisp vector"))) 514 | 515 | (defcustom cdlatex-modify-backwards t 516 | "Non-nil means, `cdlatex-math-modify' modifies char before point. 517 | Nil means, always insert only an empty modification form. This is also 518 | the case if the character before point is white or some punctuation." 519 | :group 'cdlatex-math-support 520 | :type 'boolean) 521 | 522 | (defcustom cdlatex-math-modify-alist nil 523 | "List description of the LaTeX math accents. 524 | See `cdlatex-math-modify-alist-default' for an example. Any entries in this 525 | variable will be added to the default. 526 | Each element contains 6 items: 527 | 0. key: The character that is the key for a the accent. 528 | 1. mathcmd: The LaTeX command associated with the accent in math mode 529 | 2. textcmd: The LaTeX command associated with the accent in text mode 530 | 3. type: t if command with argument (e.g. \\tilde{a}). 531 | nil if style (e.g. {\\cal a}). 532 | 4. rmdot: t if the dot on i and j has to be removed. 533 | 5. it t if italic correction is required." 534 | :group 'cdlatex-math-support 535 | :type '(repeat 536 | (list (character :tag "Key character ") 537 | (choice :tag "TeX macro inside math mode" 538 | (string "") 539 | (const :tag "none" nil)) 540 | (choice :tag "TeX macro outside math mode" 541 | (string "") 542 | (const :tag "none" nil)) 543 | (boolean :tag "Type " :on "Command" :off "Style") 544 | (boolean :tag "Remove dot in i/j") 545 | (boolean :tag "Italic correction")))) 546 | 547 | (defcustom cdlatex-make-sub-superscript-roman-if-pressed-twice t 548 | "Non-nil means, pressing `^' or `_' twice inserts roman sub/superscript." 549 | :group 'cdlatex-math-support 550 | :type 'boolean) 551 | 552 | (defcustom cdlatex-use-dollar-to-ensure-math t 553 | "Non-nil means, use $...$ to force a math mode setting where needed. 554 | When nil, use \\(...\\) instead." 555 | :group 'cdlatex-math-support 556 | :type '(boolean)) 557 | 558 | ;; Miscellaneous configurations ----------------------------------------- 559 | 560 | (defgroup cdlatex-miscellaneous-configurations nil 561 | "Collection of further configurations." 562 | :group 'cdlatex) 563 | 564 | (defcustom cdlatex-use-fonts t 565 | "Non-nil means, use fonts in label menu and on-the-fly help. 566 | Font-lock must be loaded as well to actually get fontified display." 567 | :group 'cdlatex-miscellaneous-configurations 568 | :type '(boolean)) 569 | 570 | (defcustom cdlatex-takeover-parenthesis t 571 | "Non-nil means, cdlatex is allowed to take over the parenthesis insertion. 572 | This means it will redefine the `(', `{', and `[' keys." 573 | :group 'cdlatex-miscellaneous-configurations 574 | :type '(boolean)) 575 | 576 | (defcustom cdlatex-takeover-dollar t 577 | "Non-nil means, cdlatex is allowed to take over the $. 578 | This means it will redefine the `$' keys." 579 | :group 'cdlatex-miscellaneous-configurations 580 | :type '(boolean)) 581 | 582 | (defcustom cdlatex-takeover-subsuperscript t 583 | "Non-nil means, cdlatex is allowed to take over the ^ and _ keys." 584 | :group 'cdlatex-miscellaneous-configurations 585 | :type '(boolean)) 586 | 587 | (defcustom cdlatex-paired-parens "$[{" 588 | "String with the opening parens you want to have inserted paired. 589 | The following parens are allowed here: `$([{|<'. 590 | I recommend to set this to '$[{' as these have syntactical meaning in 591 | TeX and are required to be paired. TAB is a good way to move out of paired 592 | parens." 593 | :group 'cdlatex-miscellaneous-configurations 594 | :type '(string :tag "Opening delimiters")) 595 | 596 | (defcustom cdlatex-simplify-sub-super-scripts t 597 | "Non-nil means, TAB will simplify sub- and superscripts at point. 598 | When you use TAB to exit from a sub- or superscript which is a single 599 | letter, the parenthesis will be removed." 600 | :group 'cdlatex-miscellaneous-configurations 601 | :type '(boolean)) 602 | 603 | (defcustom cdlatex-sub-super-scripts-outside-math-mode t 604 | "Non-nil means, inserting ^ or _ will add dollars outside math environment. 605 | So in text mode surrounding dollars and braces will be added with `_' and `^'. 606 | When nil, `_' and `^' will just self-insert." 607 | :group 'cdlatex-miscellaneous-configurations 608 | :type '(boolean)) 609 | 610 | (defcustom cdlatex-auto-help-delay 1.5 611 | "Number of idle seconds before display of auto-help. 612 | When executing `cdlatex-math-symbol' or `cdlatex-math-modify', display 613 | automatic help when idle for more than this amount of time." 614 | :group 'cdlatex-miscellaneous-configurations 615 | :type 'number) 616 | 617 | ;;;============================================================================ 618 | ;;; 619 | ;;; Define the formal stuff for a minor mode named CDLaTeX. 620 | ;;; 621 | 622 | (defun cdlatex-show-commentary () 623 | "Use the finder to view the file documentation from `cdlatex.el'." 624 | (interactive) 625 | (require 'finder) 626 | (finder-commentary "cdlatex.el")) 627 | 628 | (defvar cdlatex-mode nil 629 | "Determines if CDLaTeX minor mode is active.") 630 | (make-variable-buffer-local 'cdlatex-mode) 631 | 632 | (defun cdlatex-get-kbd-vector (obj) 633 | "Ensure OBJ can be used as a keybinding." 634 | (cond ((vectorp obj) obj) 635 | ((integerp obj) (vector obj)) 636 | ((stringp obj) (read-kbd-macro obj)) 637 | ((and (fboundp 'characterp) (characterp obj)) 638 | (vector obj)) ; XEmacs only 639 | (t nil))) 640 | 641 | (defvar cdlatex-mode-map 642 | (let ((map (make-sparse-keymap))) 643 | (if cdlatex-takeover-dollar 644 | (define-key map "$" #'cdlatex-dollar)) 645 | (if cdlatex-takeover-parenthesis 646 | (progn 647 | (define-key map "(" #'cdlatex-pbb) 648 | (define-key map "{" #'cdlatex-pbb) 649 | (define-key map "[" #'cdlatex-pbb) 650 | (define-key map "|" #'cdlatex-pbb) 651 | (define-key map "<" #'cdlatex-pbb))) 652 | (if cdlatex-takeover-subsuperscript 653 | (progn 654 | (define-key map "^" #'cdlatex-sub-superscript) 655 | (define-key map "_" #'cdlatex-sub-superscript))) 656 | 657 | (define-key map "\t" #'cdlatex-tab) 658 | (define-key map "\C-c?" #'cdlatex-command-help) 659 | (define-key map "\C-c{" #'cdlatex-environment) 660 | (define-key map [(control return)] #'cdlatex-item) 661 | 662 | (define-key map (cdlatex-get-kbd-vector cdlatex-math-symbol-prefix) 663 | #'cdlatex-math-symbol) 664 | (define-key map (cdlatex-get-kbd-vector cdlatex-math-modify-prefix) 665 | #'cdlatex-math-modify) 666 | map) 667 | "Keymap for CDLaTeX minor mode.") 668 | 669 | ;;;###autoload 670 | (defun turn-on-cdlatex () 671 | "Turn on CDLaTeX minor mode." 672 | (cdlatex-mode t)) 673 | 674 | ;;;###autoload 675 | (define-minor-mode cdlatex-mode 676 | "Minor mode for editing scientific LaTeX documents. 677 | 678 | Here is a list of features: \\ 679 | 680 | KEYWORD COMMANDS 681 | ---------------- 682 | 683 | Many CDLaTeX commands are activated with an abbrev-like 684 | mechanism. When a keyword is typed followed \\[cdlatex-tab], the 685 | keyword is deleted from the buffer and a command is executed. 686 | You can get a full list of these commands with 687 | \\[cdlatex-command-help]. For example, when you type `fr', 688 | CDLaTeX will insert \\frac{}{}. 689 | 690 | When inserting templates like \\='\\frac{}{}\\=', the cursor is 691 | positioned properly. Use \\[cdlatex-tab] to move through 692 | templates. \\[cdlatex-tab] also kills unnecessary braces around 693 | subscripts and superscripts at point. 694 | 695 | MATH CHARACTERS AND ACCENTS 696 | --------------------------- 697 | 698 | \\[cdlatex-math-symbol] followed by any character inserts a LaTeX 699 | math character 700 | e.g. \\[cdlatex-math-symbol]e 701 | => \\epsilon 702 | 703 | \\[cdlatex-math-symbol]\\[cdlatex-math-symbol] followed by any 704 | character inserts other LaTeX math character 705 | e.g. \\[cdlatex-math-symbol]\\[cdlatex-math-symbol]e 706 | => \\varepsilon 707 | \\[cdlatex-math-modify] followed by character puts a math 708 | accent on a letter or symbol 709 | e.g. \\[cdlatex-math-symbol]a\\[cdlatex-math-modify]~ 710 | => \\tilde{\\alpha} 711 | 712 | CDLaTeX is aware of the math environments in LaTeX and modifies 713 | the workings of some functions according to the current status. 714 | 715 | ONLINE HELP 716 | ----------- 717 | 718 | After pressing \\[cdlatex-math-symbol] or 719 | \\[cdlatex-math-modify], CDLaTeX waits for a short time for the 720 | second character. If that does not come, it will pop up a window 721 | displaying the available characters and their meanings. 722 | 723 | KEY BINDINGS 724 | ------------ 725 | \\{cdlatex-mode-map} 726 | 727 | Under X, many functions will be available also in a menu on the menu bar. 728 | 729 | Entering `cdlatex-mode' calls the hook cdlatex-mode-hook." 730 | :lighter " CDL" 731 | (when cdlatex-mode 732 | (cdlatex-compute-tables))) 733 | 734 | (defalias 'cdlatex--texmathp 735 | (if (fboundp 'texmathp) #'texmathp 736 | ;; FIXME: Maybe we could do better, but why bother: the users who want it 737 | ;; can install AUCTeX. Tho maybe we should move texmathp into its 738 | ;; own package so it can be used even when AUCTeX is not 739 | ;; installed/activated. 740 | #'ignore)) 741 | 742 | ;;; =========================================================================== 743 | ;;; 744 | ;;; Functions that check out the surroundings 745 | 746 | (defun cdlatex-dollars-balanced-to-here (&optional from) 747 | "Non-nil if the dollars are balanced between start of paragraph and point. 748 | Unless the optional argument FROM specifies the position from 749 | where to check for balance, the beginning of the current 750 | paragraph is used." 751 | (save-excursion 752 | (let ((answer t) (pos (point))) 753 | (if from 754 | (goto-char from) 755 | (backward-paragraph 1)) 756 | (if (not (bobp)) (backward-char 1)) 757 | (while (re-search-forward "[^\\]\\$+" pos t) 758 | (if (/= (char-after (match-beginning 0)) ?\\) 759 | (setq answer (not answer)))) 760 | (setq answer answer)))) 761 | 762 | (defun cdlatex-number-of-backslashes-is-odd () 763 | "Count backslashes before point and return t if number is odd." 764 | (let ((odd nil)) 765 | (save-excursion 766 | (while (equal (preceding-char) ?\\) 767 | (progn 768 | (forward-char -1) 769 | (setq odd (not odd))))) 770 | (setq odd odd))) 771 | 772 | ;; ============================================================================ 773 | ;; 774 | ;; Some generally useful functions 775 | 776 | (defun cdlatex-uniquify (alist &optional keep-list) 777 | "Return a list of all elements in ALIST, but each car only once. 778 | Elements of KEEP-LIST are not removed even if duplicate." 779 | (let (new elm) 780 | (while alist 781 | (setq elm (car alist) 782 | alist (cdr alist)) 783 | (if (or (member (car elm) keep-list) 784 | (not (assoc (car elm) new))) 785 | (setq new (cons elm new)))) 786 | (setq new (nreverse new)) 787 | new)) 788 | 789 | (defun cdlatex-use-fonts () 790 | "Return t if we can and want to use fonts." 791 | (and window-system 792 | cdlatex-use-fonts 793 | (boundp 'font-lock-keyword-face))) 794 | 795 | ;;; --------------------------------------------------------------------------- 796 | ;;; 797 | ;;; Insert pairs of $$ (), etc. 798 | 799 | ;; Alist connection opening with closing delimiters 800 | (defconst cdlatex-parens-pairs '(("(".")") ("["."]") ("{"."}") 801 | ("|"."|") ("<".">"))) 802 | 803 | (defun cdlatex-pbb () 804 | "Insert a pair of parens, brackets or braces." 805 | (interactive) 806 | (let ((paren (char-to-string (event-basic-type last-command-event)))) 807 | (if (and (stringp cdlatex-paired-parens) 808 | (string-match (regexp-quote paren) cdlatex-paired-parens) 809 | (not (cdlatex-number-of-backslashes-is-odd))) 810 | (progn 811 | (insert paren) 812 | (insert (cdr (assoc paren cdlatex-parens-pairs))) 813 | (forward-char -1)) 814 | (insert paren)))) 815 | 816 | (defun cdlatex-ensure-math () 817 | "Make sure we are in math mode." 818 | (unless (if (fboundp 'texmathp) (texmathp) t) 819 | (if cdlatex-use-dollar-to-ensure-math 820 | (cdlatex-dollar) 821 | (insert "\\(\\)") 822 | (backward-char 2)))) 823 | 824 | (defun cdlatex-dollar (&optional arg) 825 | "Insert a pair of dollars unless number of backslashes before point is odd. 826 | With ARG, insert pair of double dollars." 827 | (interactive "P") 828 | (cond 829 | ((region-active-p) 830 | (let ((s (region-beginning)) (e (region-end))) 831 | (goto-char s) 832 | (insert "$") 833 | (goto-char (1+ e)) 834 | (insert "$"))) 835 | ((cdlatex-number-of-backslashes-is-odd) 836 | (insert "$")) 837 | ((cdlatex--texmathp) 838 | (defvar texmathp-why) 839 | (if (and (stringp (car texmathp-why)) 840 | (equal (substring (car texmathp-why) 0 1) "$")) 841 | (progn 842 | (insert (car texmathp-why)) 843 | (save-excursion 844 | (goto-char (cdr texmathp-why)) 845 | (if (pos-visible-in-window-p) 846 | (sit-for 1)))) 847 | (message "No dollars inside a math environment!") 848 | (ding))) 849 | ((and (stringp cdlatex-paired-parens) 850 | (string-match "\\$" cdlatex-paired-parens)) 851 | (if arg 852 | (if (bolp) 853 | (progn (insert "$$\n\n$$\n") (backward-char 4)) 854 | (insert "$$ $$") (backward-char 3)) 855 | (insert "$$") (backward-char 1))) 856 | (arg 857 | (if (bolp) (insert "$$\n") (insert "$$"))) 858 | (t (insert "$")))) 859 | 860 | (defun cdlatex-sub-superscript () 861 | "Insert ^{} or _{} unless the number of backslashes before point is odd. 862 | When not in LaTeX math environment, _{} and ^{} will have dollars. 863 | When pressed twice, make the sub/superscript roman." 864 | (interactive) 865 | (if (and cdlatex-make-sub-superscript-roman-if-pressed-twice 866 | (equal this-command last-command)) 867 | (progn 868 | (insert "\\mathrm{}") 869 | (backward-char 1)) 870 | (if (cdlatex-number-of-backslashes-is-odd) 871 | ;; Quoted 872 | (insert (event-basic-type last-command-event)) 873 | ;; Check if we are in math mode, if not switch to or only add _ or ^ 874 | (if (not (or (cdlatex--texmathp) 875 | cdlatex-sub-super-scripts-outside-math-mode)) 876 | (insert (event-basic-type last-command-event)) 877 | (cdlatex-ensure-math) 878 | ;; Insert the normal template. 879 | (insert (event-basic-type last-command-event)) 880 | (insert "{}") 881 | (forward-char -1))))) 882 | 883 | (defun cdlatex-lr-pair () 884 | "Insert a \\left-\\right pair of parens." 885 | (interactive) 886 | (let* ((paren (char-to-string (preceding-char))) 887 | (close (cdr (assoc paren cdlatex-parens-pairs))) 888 | (paren1 paren) 889 | (close1 close)) 890 | (if (string= paren "<") (setq paren1 "\\langle" close1 "\\rangle")) 891 | (if (string= paren "{") (setq paren1 "\\{" close1 "\\}")) 892 | (backward-delete-char 1) 893 | (if (and (stringp cdlatex-paired-parens) 894 | (string-match (regexp-quote paren) cdlatex-paired-parens) 895 | (string= close (char-to-string (following-char)))) 896 | ; parens are inserted paired, and there is already a closing parenthesis 897 | (delete-char 1)) 898 | (insert "\\left" paren1 " ? \\right" close1) 899 | (cdlatex-position-cursor))) 900 | 901 | ;;; =========================================================================== 902 | ;;; 903 | ;;; Keyword controlled commands and cursor movement 904 | 905 | (defvar cdlatex-command-alist-comb nil) 906 | 907 | (defun cdlatex-tab () 908 | "This function is intended to do many cursor movements. 909 | It is bound to the tab key since tab does nothing useful in a TeX file. 910 | 911 | This function first calls all functions in `cdlatex-tab-hook', which see. 912 | 913 | If none of those functions returns t, the command first tries to expand 914 | any command keyword before point. 915 | 916 | If there is none, it cleans up short subscripts and superscripts at point. 917 | I.e. it changes a^{2} into a^2, since this is more readable. This feature 918 | can be disabled by setting `cdlatex-simplify-sub-super-scripts' to nil. 919 | 920 | Then it jumps to the next point in a LaTeX text where one would reasonably 921 | expect that more input can be put in. 922 | To do that, the cursor is moved according to the following rules: 923 | 924 | The cursor stops... 925 | - before closing brackets if `preceding-char' is any of -({[]}) 926 | - after closing brackets, but not if `following-char' is any of ({[_^ 927 | - just after $, if the cursor was before that $. 928 | - at end of non-empty lines 929 | - at the beginning of empty lines 930 | - before a SPACE at beginning of line 931 | - after first of several SPACE 932 | 933 | Sounds strange? Try it out!" 934 | (interactive) 935 | (catch 'stop 936 | 937 | ;; try hook stuff 938 | (let ((funcs cdlatex-tab-hook)) 939 | (while funcs (if (funcall (pop funcs)) (throw 'stop t)))) 940 | 941 | ;; try command expansion 942 | (let ((pos (point)) exp math-mode) 943 | (backward-word 1) 944 | (while (eq (following-char) ?$) (forward-char 1)) 945 | (setq exp (buffer-substring-no-properties (point) pos)) 946 | (setq exp (assoc exp cdlatex-command-alist-comb)) 947 | (when exp 948 | (setq math-mode (cdlatex--texmathp)) 949 | (when (or (and (not math-mode) (nth 5 exp)) 950 | (and math-mode (nth 6 exp))) 951 | (delete-char (- pos (point))) 952 | (insert (nth 2 exp)) 953 | ;; call the function if there is one defined 954 | (and (nth 3 exp) 955 | (if (nth 4 exp) 956 | (apply (nth 3 exp) (nth 4 exp)) 957 | (funcall (nth 3 exp)))) 958 | (throw 'stop t))) 959 | (goto-char pos)) 960 | 961 | ;; Check for simplification of sub and superscripts 962 | (cond 963 | ((looking-at "}\\|\\]\\|)") 964 | (forward-char -3) 965 | (if (and (looking-at "[_^]{[-+0-9a-zA-Z]}") 966 | cdlatex-simplify-sub-super-scripts) 967 | ;; simplify sub/super script 968 | (progn (forward-char 1) 969 | (delete-char 1) 970 | (forward-char 1) 971 | (delete-char 1)) 972 | (forward-char 4)) 973 | (if (looking-at "[^_^({[]") 974 | ;; stop after closing bracket, unless ^_[{( follow 975 | (throw 'stop t))) 976 | ((= (following-char) ?$) 977 | (while (= (following-char) ?$) (forward-char 1)) 978 | (throw 'stop t)) 979 | ((= (following-char) ?\ ) 980 | ;; stop after first of many spaces 981 | (forward-char 1) 982 | (re-search-forward "[^ ]") 983 | (if (/= (preceding-char) ?\n) (forward-char -1))) 984 | (t 985 | (forward-char 1))) 986 | 987 | ;; move to next possible stopping site and check out the place 988 | (while (re-search-forward "[ )}\n]\\|\\]" (point-max) t) 989 | (forward-char -1) 990 | (cond 991 | ((= (following-char) ?\ ) 992 | ;; stop at first space or b-o-l 993 | (if (not (bolp)) (forward-char 1)) (throw 'stop t)) 994 | ((= (following-char) ?\n) 995 | ;; stop at line end, but not after \\ 996 | (if (and (bolp) (not (eobp))) 997 | (throw 'stop t) 998 | (if (equal "\\\\" (buffer-substring-no-properties 999 | (- (point) 2) (point))) 1000 | (forward-char 1) 1001 | (throw 'stop t)))) 1002 | (t 1003 | ;; Stop before )}] if preceding-char is any parenthesis 1004 | (if (or (= (char-syntax (preceding-char)) ?\() 1005 | (= (char-syntax (preceding-char)) ?\)) 1006 | (= (preceding-char) ?-)) 1007 | (throw 'stop t) 1008 | (forward-char 1) 1009 | (if (looking-at "[^_^({[]") 1010 | ;; stop after closing bracket, unless ^_[{( follow 1011 | (throw 'stop t)))))))) 1012 | 1013 | (defun cdlatex-command-help (&optional arg) 1014 | "Show the available cdlatex commands in the help buffer. 1015 | With a non-nil ARG, or a prefix argument, run texdoc instead." 1016 | (interactive "P") 1017 | (if arg 1018 | (call-interactively 'TeX-documentation-texdoc) 1019 | (with-output-to-temp-buffer " *CDLaTeX Help*" 1020 | (princ " AVAILABLE KEYWORD COMMANDS WITH CDLaTeX\n") 1021 | (princ " --------------------------------------\n") 1022 | (princ "To execute, type keyword into buffer followed by TAB.\n\n") 1023 | (let ((cmdlist cdlatex-command-alist-comb) item key doc text math) 1024 | (while cmdlist 1025 | (setq item (car cmdlist) 1026 | cmdlist (cdr cmdlist) 1027 | key (car item) 1028 | doc (nth 1 item) 1029 | text (nth 5 item) 1030 | math (nth 6 item)) 1031 | (princ (format "%-10.10s %-58.58s %4s/%4s\n" key 1032 | (if (> (length doc) 59) 1033 | (substring doc 0 59) 1034 | doc) 1035 | (if text "TEXT" "") 1036 | (if math "MATH" "")))))))) 1037 | 1038 | ;;; --------------------------------------------------------------------------- 1039 | ;;; 1040 | ;;; Cursor position after insertion of forms 1041 | 1042 | (defun cdlatex-position-cursor () 1043 | "Search back to question mark, delete it, leave point there." 1044 | (if (search-backward "\?" (- (point) 100) t) 1045 | (delete-char 1))) 1046 | 1047 | ;;; --------------------------------------------------------------------------- 1048 | ;;; 1049 | ;;; Environments 1050 | ;;; 1051 | ;; 1052 | ;; The following code implements insertion of LaTeX environments 1053 | ;; I prefer these environment over AUCTeX's definitions, since they give 1054 | ;; my memory more support and don't prompt for anything. 1055 | 1056 | (defvar cdlatex-env-alist-comb nil) 1057 | 1058 | (defun cdlatex-environment (&optional environment item) 1059 | "Complete the name of an ENVIRONMENT and insert it. 1060 | If the environment is not found in the list, a \\begin \\end pair is 1061 | inserted. Any keywords AUTOLABEL will be replaced by an automatic label 1062 | statement. Any keywords AUTOFILE will prompt the user for a file name 1063 | \(with completion) and insert that file name. If a template starts with 1064 | \"\\\\\", the function will make sure that a double backslash occurs before 1065 | the template. This is mainly useful for \"items\" of environments, where 1066 | \"\\\\\" is often needed as separator." 1067 | (interactive) 1068 | (if (cdlatex-region-active-p) 1069 | (cdlatex-wrap-environment environment) 1070 | (let ((env environment) begpos (endmarker (make-marker)) 1071 | (auto-label cdlatex-insert-auto-labels-in-env-templates) 1072 | template prefix) 1073 | (if (not env) 1074 | (setq env (completing-read "Environment: " cdlatex-env-alist-comb nil nil ""))) 1075 | (if (looking-back "^\\([ \t]*\\)\\([^ \t].*\\)?" (point-at-bol)) 1076 | (progn 1077 | (setq prefix (match-string 1)) 1078 | (if (match-end 2) (progn (newline) (insert prefix))))) 1079 | (setq begpos (point)) 1080 | (if (try-completion env cdlatex-env-alist-comb) 1081 | (progn 1082 | (setq template (nth (if item 2 1) 1083 | (assoc env cdlatex-env-alist-comb))) 1084 | (if (string= (substring template 0 2) "\\\\") 1085 | ;; Need a double backslash to teminate previous item 1086 | (progn 1087 | (setq template (substring template 2)) 1088 | (if (not (save-excursion 1089 | (re-search-backward "\\\\\\\\[ \t\n]*\\=" 1090 | (- (point) 20) t))) 1091 | (save-excursion 1092 | (skip-chars-backward " \t\n") 1093 | (insert "\\\\"))))) 1094 | (setq template 1095 | (replace-regexp-in-string "\n" (concat "\n" prefix) template)) 1096 | (insert template)) 1097 | (insert "\\begin{" env "}\n?\n\\end{" env "}\n")) 1098 | (move-marker endmarker (point)) 1099 | 1100 | ;; Look for AUTOFILE requests 1101 | (goto-char begpos) 1102 | (while (search-forward "AUTOFILE" (marker-position endmarker) t) 1103 | (backward-delete-char 8) 1104 | (call-interactively 'cdlatex-insert-filename)) 1105 | 1106 | ;; Look for AUTOLABEL requests 1107 | (goto-char begpos) 1108 | (while (search-forward "AUTOLABEL" (marker-position endmarker) t) 1109 | (backward-delete-char 9) 1110 | (if (and auto-label (fboundp 'reftex-label)) 1111 | (reftex-label env) 1112 | (save-excursion 1113 | (beginning-of-line 1) 1114 | (if (looking-at "[ \t]*\n") 1115 | (kill-line 1))))) 1116 | 1117 | ;; Position cursor at the first question-mark 1118 | (goto-char begpos) 1119 | (if (search-forward "?" (marker-position endmarker) t) 1120 | (backward-delete-char 1))))) 1121 | 1122 | (defun cdlatex-wrap-environment (&optional environment) 1123 | "Wrap the active region into ENVIRONMENT. 1124 | If the environment is not given, ask for it using completion." 1125 | (let ((env environment) 1126 | (beg (move-marker (make-marker) (region-beginning))) 1127 | (end (move-marker (make-marker) (region-end)))) 1128 | (if (not env) 1129 | (setq env (completing-read "Environment: " 1130 | cdlatex-env-alist-comb nil nil ""))) 1131 | (goto-char beg) 1132 | (if (not (looking-back "^[ \t]*" (point-at-bol))) (newline)) 1133 | (insert "\\begin{" env "}\n") 1134 | (goto-char end) 1135 | (if (not (looking-back "^[ \t]*" (point-at-bol))) (newline)) 1136 | (insert "\\end{" env "}\n") 1137 | )) 1138 | 1139 | (defun cdlatex-item () 1140 | "Insert an \\item and provide a label if the environments supports that. 1141 | In eqnarrays this inserts a new line with two ampersands. It will also 1142 | add two backslashes to the previous line if required." 1143 | (interactive) 1144 | (let* ((env (car (car (reftex-what-environment t)))) 1145 | (envl (assoc env cdlatex-env-alist-comb))) 1146 | 1147 | (if (not env) (error "No open environment at point")) 1148 | (if (or (< (length envl) 3) 1149 | (null (nth 2 envl)) 1150 | (and (stringp (nth 2 envl)) 1151 | (string= (nth 2 envl) ""))) 1152 | (error "No item defined for %s environment" env)) 1153 | (cdlatex-environment env t))) 1154 | 1155 | (defun cdlatex-comment-at-point () 1156 | "Return t if point is inside a TeX comment." 1157 | (let ((end (point)) 1158 | (start (progn (beginning-of-line 1) (point)))) 1159 | (goto-char end) 1160 | (save-match-data 1161 | (string-match "^%\\|[^\\]%" (buffer-substring start end))))) 1162 | 1163 | (defun cdlatex-insert-filename (&optional absolute) 1164 | "Insert a file name, with completion. 1165 | The path to the file will be relative to the current directory if the file 1166 | is in the current directory or a subdirectory. Otherwise, the link will 1167 | be as completed in the minibuffer (i.e. normally relative to the users 1168 | HOME directory). 1169 | With optional prefix ABSOLUTE, insert the absolute path." 1170 | (interactive "P") 1171 | (let ((file (read-file-name "File: " nil ""))) 1172 | (let ((pwd (file-name-as-directory (expand-file-name ".")))) 1173 | (cond 1174 | (absolute 1175 | (insert (expand-file-name file))) 1176 | ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)") 1177 | (expand-file-name file)) 1178 | (insert (match-string 1 (expand-file-name file)))) 1179 | (t (insert (expand-file-name file))))))) 1180 | 1181 | 1182 | ;;; =========================================================================== 1183 | ;;; 1184 | ;;; Math characters and modifiers 1185 | 1186 | ;; The actual value of the following variable is calculated 1187 | ;; by `cdlatex-compute-tables'. It holds the number of levels of math symbols 1188 | (defvar cdlatex-math-symbol-no-of-levels 1) 1189 | (defvar cdlatex-math-symbol-alist-comb nil) 1190 | (defvar cdlatex-math-modify-alist-comb nil) 1191 | 1192 | (defvar zmacs-regions) 1193 | (defun cdlatex-region-active-p () 1194 | "Is `transient-mark-mode' on and the region active? 1195 | Works on both Emacs and XEmacs." 1196 | (if (featurep 'xmeacs) 1197 | (and zmacs-regions (region-active-p)) 1198 | (and transient-mark-mode mark-active))) 1199 | 1200 | (defun cdlatex-math-symbol () 1201 | "Read a char from keyboard and insert corresponding math char. 1202 | The combinations are defined in `cdlatex-math-symbol-alist'. If not in a LaTeX 1203 | math environment, you also get a pair of dollars." 1204 | (interactive) 1205 | (let* ((cell (cdlatex-read-char-with-help 1206 | cdlatex-math-symbol-alist-comb 1207 | 1 cdlatex-math-symbol-no-of-levels 1208 | "Math symbol level %d of %d: " 1209 | "AVAILABLE MATH SYMBOLS. [%c]=next level " 1210 | cdlatex-math-symbol-prefix 1211 | (get 'cdlatex-math-symbol-alist-comb 'cdlatex-bindings))) 1212 | (char (car cell)) 1213 | (level (cdr cell)) 1214 | (entry (assoc char cdlatex-math-symbol-alist-comb)) 1215 | (symbol (nth level entry))) 1216 | 1217 | (if (or (not symbol) 1218 | (not (stringp symbol)) 1219 | (equal symbol "")) 1220 | (error "No such math symbol %c on level %d" char level)) 1221 | 1222 | (cdlatex-ensure-math) 1223 | 1224 | (insert symbol) 1225 | (when (string-match "\\?" symbol) 1226 | (cdlatex-position-cursor)))) 1227 | 1228 | (defun cdlatex-read-char-with-help (alist start-level max-level prompt-format 1229 | header-format prefix bindings) 1230 | "Read a char from keyboard and provide help if necessary." 1231 | (interactive) 1232 | (let (char (help-is-on nil) 1233 | (level start-level)) 1234 | (catch 'exit 1235 | (save-window-excursion 1236 | (while t 1237 | (if help-is-on 1238 | (progn 1239 | (cdlatex-turn-on-help 1240 | (concat (format header-format prefix) 1241 | (if (assoc level bindings) 1242 | (concat " Direct binding are `" 1243 | (cdr (assoc level bindings)) "' etc.") 1244 | "")) 1245 | level alist help-is-on nil))) 1246 | (message prompt-format level max-level) 1247 | (if (and (not help-is-on) 1248 | (sit-for cdlatex-auto-help-delay)) 1249 | (setq char ?\?) 1250 | (setq char (read-char))) 1251 | (cond 1252 | ((= char ?\C-g) 1253 | (keyboard-quit)) 1254 | ((= char ?\?) 1255 | (if help-is-on 1256 | (progn 1257 | (setq help-is-on (+ help-is-on (- (window-height) 1))) 1258 | (if (> help-is-on (count-lines (point-min) (point-max))) 1259 | (setq help-is-on 1))) 1260 | (setq help-is-on 1))) 1261 | ((or (= char ?\ ) 1262 | (equal char prefix)) 1263 | (setq level (if (= level cdlatex-math-symbol-no-of-levels) 1264 | 1 1265 | (1+ level)))) 1266 | (t (throw 'exit (cons char level))))))))) 1267 | 1268 | ;; The following code implements the possibility to modify a character 1269 | ;; by an accent or style when point is behind it. This is more naturally 1270 | ;; then the usual way. E.g. \tilde{a} can be typed as a'~ 1271 | 1272 | (defun cdlatex-math-modify (arg) 1273 | "Modify previous char/group/macro with math accent/style. 1274 | This macro modifies the character or TeX macro or TeX group BEFORE point 1275 | with a math accent or a style. 1276 | If the character before point is white space, an empty modifying form 1277 | is inserted and the cursor positioned properly. 1278 | If the object before point looks like word, this macro modifies the last 1279 | character of it. The math-related changes happen only when the cursor 1280 | is actually inside a LaTeX math environment. 1281 | Some of the modification characters denote font style changes like bold 1282 | or italic. These also work in text mode, on the word before point or, 1283 | if applicable, the active region. 1284 | In normal text, it does just a self-insert. 1285 | The accent and style commands and their properties are defined in the 1286 | constant `cdlatex-math-modify-alist'." 1287 | (interactive "P") 1288 | (catch 'exit 1289 | 1290 | (let ((inside-math (cdlatex--texmathp)) 1291 | (win (selected-window)) 1292 | (savedpos (make-marker)) 1293 | char (help-is-on nil) ass acc rmdot it cmd extrabrac 1294 | before after) 1295 | (catch 'exit1 1296 | (save-window-excursion 1297 | (while t 1298 | (if help-is-on 1299 | (progn 1300 | (cdlatex-turn-on-help 1301 | "AVAILABLE MODIFIERS. (?=SCROLL)" 1302 | (if inside-math 1 2) 1303 | cdlatex-math-modify-alist-comb help-is-on t) 1304 | (message "Math modify: ")) 1305 | (message "Math modify: (?=HELP)")) 1306 | 1307 | (if (and (not help-is-on) 1308 | (sit-for cdlatex-auto-help-delay)) 1309 | (setq char ?\?) 1310 | (setq char (read-char))) 1311 | 1312 | (cond 1313 | ((= char ?\C-g) 1314 | (keyboard-quit)) 1315 | ((= char ?\?) 1316 | (if help-is-on 1317 | (progn 1318 | (setq help-is-on (+ help-is-on (- (window-height) 1))) 1319 | (if (> help-is-on (count-lines (point-min) (point-max))) 1320 | (setq help-is-on 1))) 1321 | (setq help-is-on 1))) 1322 | ((equal char cdlatex-math-modify-prefix) 1323 | (select-window win) 1324 | (insert cdlatex-math-modify-prefix) 1325 | (message "") 1326 | (throw 'exit t)) 1327 | (t (throw 'exit1 t)))))) 1328 | (message "") 1329 | (setq ass (assoc char cdlatex-math-modify-alist-comb)) 1330 | (if (not ass) 1331 | (progn 1332 | (insert cdlatex-math-modify-prefix char) 1333 | (throw 'exit t))) 1334 | (setq ass (cdr ass)) 1335 | (setq cmd (nth (if inside-math 0 1) ass)) 1336 | (setq acc (nth 2 ass)) 1337 | (setq rmdot (nth 3 ass)) 1338 | (setq it (nth 4 ass)) 1339 | (if (not cmd) 1340 | (progn 1341 | (message "No such modifier `%c' %s math mode" char 1342 | (if inside-math "inside" "outside")) 1343 | (insert cdlatex-math-modify-prefix char) 1344 | (throw 'exit t))) 1345 | (if (string-match "\\(.*\\)\\?\\(.*\\)" cmd) 1346 | (setq before (match-string 1 cmd) after (match-string 2 cmd))) 1347 | (cond 1348 | ((cdlatex-region-active-p) 1349 | (let ((beg (min (region-beginning) (region-end))) 1350 | (end (max (region-beginning) (region-end)))) 1351 | (goto-char end) 1352 | (move-marker savedpos (point)) 1353 | (goto-char beg) 1354 | (if before 1355 | (insert before) 1356 | (insert "{") 1357 | (if acc (forward-char -1)) 1358 | (insert cmd) 1359 | (if (not acc) (insert " "))) 1360 | (goto-char savedpos) 1361 | (if after 1362 | (insert after) 1363 | (insert "}")))) 1364 | (arg 1365 | (move-marker savedpos (point)) 1366 | (backward-word arg) 1367 | (if before 1368 | (insert before) 1369 | (insert "{") 1370 | (if acc (forward-char -1)) 1371 | (insert cmd) 1372 | (if (not acc) (insert " "))) 1373 | (goto-char savedpos) 1374 | (if after 1375 | (insert after) 1376 | (insert "}"))) 1377 | ((or (bolp) 1378 | (not cdlatex-modify-backwards) 1379 | (memq (preceding-char) '(?\ ?$ ?- ?{ ?\( ))) 1380 | ;; Just insert empty form and position cursor 1381 | (if (string-match "\\?" cmd) 1382 | (insert cmd) 1383 | (if acc 1384 | (insert cmd "{?") 1385 | (insert "{" cmd " ?")) 1386 | (if it (insert "\\/")) 1387 | (insert "}")) 1388 | (search-backward "?") 1389 | (delete-char 1)) 1390 | (t 1391 | ;; Modify preceding character or word 1392 | (move-marker savedpos (point)) 1393 | (if (= (preceding-char) ?\}) 1394 | ;; its a group 1395 | (progn (setq extrabrac nil) 1396 | (backward-list 1) 1397 | (if (not acc) (forward-char 1))) 1398 | ;; not a group 1399 | (forward-char -1) 1400 | (if (looking-at "[a-zA-Z]") 1401 | ;; a character: look if word or macro 1402 | (progn 1403 | (setq extrabrac t) 1404 | (re-search-backward "[^a-zA-Z]") 1405 | (cond 1406 | ((= (following-char) ?\\)) 1407 | ((not inside-math) (forward-char 1)) 1408 | (t (goto-char savedpos) 1409 | (forward-char -1) 1410 | (if (and rmdot (let (case-fold-search) (looking-at "[ij]"))) 1411 | (progn (insert "\\") 1412 | (forward-char 1) 1413 | (insert "math") 1414 | (move-marker savedpos (point)) 1415 | (forward-char -6)))))) 1416 | (setq extrabrac t))) 1417 | (if extrabrac (progn (insert "{") 1418 | (if acc (forward-char -1)))) 1419 | (insert cmd) 1420 | (if (not acc) (insert " ")) 1421 | (goto-char savedpos) 1422 | (if extrabrac (insert "}"))))))) 1423 | 1424 | ;; And here is the help function for the symbol insertions stuff 1425 | 1426 | (defun cdlatex-turn-on-help (header level alist offset &optional sparse) 1427 | "Show help-window for ALIST." 1428 | (let ((cnt 0) (all-chars "") 1429 | (flock (cdlatex-use-fonts)) this-char value) 1430 | (if sparse 1431 | (setq all-chars (concat (mapcar #'car alist))) 1432 | (setq all-chars "aA0 bB1!cC2@dD3#eE4$fF5%gG6^hH7&iI8 1433 | jJ9?kK+~lL-_mM*|nN/\\oO=\"pP()qQ[]rR{}sS<>tT`'uU.:vV 1434 | 1435 | wW 1436 | 1437 | xX 1438 | 1439 | yY 1440 | 1441 | zZ 1442 | 1443 | ")) 1444 | (if (get-buffer-window " *CDLaTeX Help*") 1445 | (select-window (get-buffer-window " *CDLaTeX Help*")) 1446 | (switch-to-buffer-other-window " *CDLaTeX Help*")) 1447 | (if buffer-read-only (read-only-mode 'toggle)) 1448 | (erase-buffer) 1449 | (make-local-variable 'truncate-lines) 1450 | (setq truncate-lines t) 1451 | (insert (concat header "\n\n")) 1452 | 1453 | (while (not (equal "" all-chars)) 1454 | (setq cnt (1+ cnt)) 1455 | (setq this-char (string-to-char all-chars)) 1456 | (setq all-chars (substring all-chars 1)) 1457 | (cond 1458 | ( (= this-char ?\?) 1459 | (setq value "SCROLL")) 1460 | ( (member this-char '(?\C-j ?\C-m)) 1461 | (setq this-char ?\ ) 1462 | (setq value "")) 1463 | ( t 1464 | (setq value (nth level (assoc this-char alist))) 1465 | (if (not value) (setq value "")) 1466 | (if (string-match "\\(.*?\\) \\? \\(.*\\)" value) 1467 | (setq value (concat (match-string 1 value) (match-string 2 value)))))) 1468 | (setq this-char (char-to-string this-char) 1469 | value (if (> (length value) 15) 1470 | (concat (substring value 0 13) "..") 1471 | (substring (concat value " ") 0 15))) 1472 | (if flock 1473 | (put-text-property 0 15 1474 | 'face 'font-lock-keyword-face value)) 1475 | 1476 | (insert this-char " " value " ") 1477 | (if (= (* 4 (/ cnt 4)) cnt) (insert "\n"))) 1478 | (unless (one-window-p t) 1479 | (enlarge-window (1+(- (count-lines 1 (point)) (window-height))))) 1480 | (goto-char (point-min)) (forward-line (1- offset)) 1481 | (beginning-of-line 1) 1482 | (recenter 0))) 1483 | 1484 | ;;; --------------------------------------------------------------------------- 1485 | ;;; 1486 | ;;; Data Section: Definition of large constants 1487 | 1488 | (defconst cdlatex-command-alist-default 1489 | '( 1490 | ("pref" "Make page reference" 1491 | "" reftex-reference nil t t) 1492 | ("ref" "Make reference" 1493 | "" reftex-reference nil t t) 1494 | 1495 | ("lbl" "Insert automatic label at point" 1496 | "" reftex-label nil t t) 1497 | 1498 | ("ct" "Insert \\cite" 1499 | "\\cite{?}" cdlatex-position-cursor nil t nil) 1500 | ("cte" "Make a citation interactively" 1501 | "" reftex-citation nil t nil) 1502 | ("cite{" "Make a citation interactively" 1503 | "cite{" reftex-citation nil t nil) 1504 | 1505 | ("beg" "Complete an environment name and insert template" 1506 | "" cdlatex-environment nil t t) 1507 | ("env" "Complete an environment name and insert template" 1508 | "" cdlatex-environment nil t t) 1509 | ("it" "New item in current environment" 1510 | "" cdlatex-item nil t t) 1511 | ("ite" "Insert an ITEMIZE environment template" 1512 | "" cdlatex-environment ("itemize") t nil) 1513 | ("enu" "Insert an ENUMERATE environment template" 1514 | "" cdlatex-environment ("enumerate") t nil) 1515 | ("equ" "Insert an EQUATION environment template" 1516 | "" cdlatex-environment ("equation") t nil) 1517 | ("eq*" "Insert an EQUATION* environment template" 1518 | "" cdlatex-environment ("equation*") t nil) 1519 | ("eqn" "Insert an EQNARRAY environment template" 1520 | "" cdlatex-environment ("eqnarray") t nil) 1521 | ("ali" "Insert an ALIGN environment template" 1522 | "" cdlatex-environment ("align") t nil) 1523 | ("ali*" "Insert an ALIGN* environment template" 1524 | "" cdlatex-environment ("align*") t nil) 1525 | ("alit" "Insert an ALIGNAT environment template" 1526 | "" cdlatex-environment ("alignat") t nil) 1527 | ("alit*" "Insert an ALIGNAT* environment template" 1528 | "" cdlatex-environment ("alignat*") t nil) 1529 | ("xal" "Insert a XALIGNAT environment template" 1530 | "" cdlatex-environment ("xalignat") t nil) 1531 | ("xal*" "Insert a XALIGNAT* environment template" 1532 | "" cdlatex-environment ("xalignat*") t nil) 1533 | ("xxa" "Insert a XXALIGNAT environment template" 1534 | "" cdlatex-environment ("xxalignat") t nil) 1535 | ("xxa*" "Insert a XXALIGNAT environment template" 1536 | "" cdlatex-environment ("xxalignat") t nil) 1537 | ("mul" "Insert a MULTINE environment template" 1538 | "" cdlatex-environment ("multline") t nil) 1539 | ("mul*" "Insert a MULTINE* environment template" 1540 | "" cdlatex-environment ("multline*") t nil) 1541 | ("gat" "Insert a GATHER environment template" 1542 | "" cdlatex-environment ("gather") t nil) 1543 | ("gat*" "Insert a GATHER* environment template" 1544 | "" cdlatex-environment ("gather*") t nil) 1545 | ("spl" "Insert SPLIT environment template" 1546 | "" cdlatex-environment ("split") nil t) 1547 | ("fla" "Insert a FLALIGN environment template" 1548 | "" cdlatex-environment ("flalign") t nil) 1549 | ("fla*" "Insert a FLALIGN* environment template" 1550 | "" cdlatex-environment ("flalign*") t nil) 1551 | ("fg" "Insert a FIGURE environment template" 1552 | "" cdlatex-environment ("figure") t nil) 1553 | 1554 | ("sn" "Insert a \\section{} statement" 1555 | "\\section{?}" cdlatex-position-cursor nil t nil) 1556 | ("ss" "Insert a \\subsection{} statement" 1557 | "\\subsection{?}" cdlatex-position-cursor nil t nil) 1558 | ("sss" "Insert a \\subsubsection{} statement" 1559 | "\\subsubsection{?}" cdlatex-position-cursor nil t nil) 1560 | ("pf" "Insert a \\paragraph{} statement" 1561 | "\\paragraph{?}" cdlatex-position-cursor nil t nil) 1562 | ("sp" "Insert a \\subparagraph{} statement" 1563 | "\\subparagraph{?}" cdlatex-position-cursor nil t nil) 1564 | ("ssp" "Insert a \\subsubparagraph{} statement" 1565 | "\\subsubparagraph{?}" cdlatex-position-cursor nil t nil) 1566 | 1567 | ("cl" "Insert \\centerline" 1568 | "\\centerline{?}" cdlatex-position-cursor nil t nil) 1569 | ("inc" "Insert \\includegraphics with file name" 1570 | "\\includegraphics[]{?}" (lambda () 1571 | (cdlatex-position-cursor) 1572 | (call-interactively 'cdlatex-insert-filename) 1573 | (forward-char 1)) 1574 | nil t nil) 1575 | ("lr(" "Insert a \\left( \\right) pair" 1576 | "(" cdlatex-lr-pair nil nil t) 1577 | ("lr[" "Insert a \\left[ \\right] pair" 1578 | "[" cdlatex-lr-pair nil nil t) 1579 | ("lr{" "Insert a \\left{ \\right} pair" 1580 | "{" cdlatex-lr-pair nil nil t) 1581 | ("lr<" "Insert a \\left\\langle \\right\\rangle pair" 1582 | "<" cdlatex-lr-pair nil nil t) 1583 | ("lr|" "Insert a \\left| \\right| pair" 1584 | "|" cdlatex-lr-pair nil nil t) 1585 | ("caseeq" "Insert a = { construct" 1586 | "\\left\\{ \n\\begin{array}{l@{\\quad:\\quad}l}\n? & \\\\\n & \n\\end{array}\\right." cdlatex-position-cursor nil nil t) 1587 | ("fr" "Insert \\frac{}{}" 1588 | "\\frac{?}{}" cdlatex-position-cursor nil nil t) 1589 | ("sq" "Insert \\sqrt{}" 1590 | "\\sqrt{?}" cdlatex-position-cursor nil nil t) 1591 | ("intl" "Insert \\int\\limits_{}^{}" 1592 | "\\int\\limits_{?}^{}" cdlatex-position-cursor nil nil t) 1593 | ("suml" "Insert \\sum\\limits_{}^{}" 1594 | "\\sum\\limits_{?}^{}" cdlatex-position-cursor nil nil t) 1595 | ("nonum" "Insert \\nonumber\\\\" 1596 | "\\nonumber\\\\\n" nil nil nil t) 1597 | ("fn" "Make a footnote" 1598 | "\\footnote{?}" cdlatex-position-cursor nil t nil) 1599 | ("qq" "Insert \\quad" 1600 | "\\quad" nil nil t t) 1601 | ("qqq" "Insert \\qquad" 1602 | "\\qquad" nil nil t t) 1603 | ) 1604 | "Default for `cdlatex-command-alist'.") 1605 | 1606 | (defconst cdlatex-math-modify-alist-default 1607 | '( 1608 | ( ?\. "\\dot" nil t t nil ) 1609 | ( ?\: "\\ddot" nil t t nil ) 1610 | ( ?\~ "\\tilde" nil t t nil ) 1611 | ( ?N "\\widetilde" nil t t nil ) 1612 | ( ?o "\\mathring" nil t t nil ) 1613 | ( ?^ "\\hat" nil t t nil ) 1614 | ( ?H "\\widehat" nil t t nil ) 1615 | ( ?\- "\\bar" nil t t nil ) 1616 | ( ?T "\\overline" nil t nil nil ) 1617 | ( ?\_ "\\underline" nil t nil nil ) 1618 | ( ?\] "\\overbrace" nil t nil nil ) 1619 | ( ?\} "\\underbrace" nil t nil nil ) 1620 | ( ?\> "\\vec" nil t t nil ) 1621 | ( ?/ "\\grave" nil t t nil ) 1622 | ( ?\\ "\\acute" nil t t nil ) 1623 | ( ?v "\\check" nil t t nil ) 1624 | ( ?u "\\breve" nil t t nil ) 1625 | ( ?m "\\mbox" nil t nil nil ) 1626 | ( ?t "\\text" nil t nil nil ) 1627 | ( ?c "\\mathcal" nil t nil nil ) 1628 | ( ?r "\\mathrm" "\\textrm" t nil nil ) 1629 | ( ?i "\\mathit" "\\textit" t nil nil ) 1630 | ( ?l nil "\\textsl" t nil nil ) 1631 | ( ?b "\\mathbf" "\\textbf" t nil nil ) 1632 | ( ?e "\\mathem" "\\emph" t nil nil ) 1633 | ( ?y "\\mathtt" "\\texttt" t nil nil ) 1634 | ( ?f "\\mathsf" "\\textsf" t nil nil ) 1635 | ( ?F "\\mathfrak" nil t nil nil ) 1636 | ( ?0 "\\textstyle" nil nil nil nil ) 1637 | ( ?1 "\\displaystyle" nil nil nil nil ) 1638 | ( ?2 "\\scriptstyle" nil nil nil nil ) 1639 | ( ?3 "\\scriptscriptstyle" nil nil nil nil ) 1640 | ( ?\( "\\left( ? \\right)" nil nil nil nil ) 1641 | ( ?\[ "\\left[ ? \\right]" nil nil nil nil ) 1642 | ( ?\{ "\\left\\{ ? \\right\\}" nil nil nil nil ) 1643 | ( ?< "\\left< ? \\right>" nil nil nil nil ) 1644 | ( ?| "\\left| ? \\right|" nil nil nil nil ) 1645 | ) 1646 | "Default for `cdlatex-math-modify-alist'.") 1647 | 1648 | (defconst cdlatex-math-symbol-alist-default 1649 | '( 1650 | ( ?a ("\\alpha" )) 1651 | ( ?A ("\\forall" "\\aleph")) 1652 | ( ?b ("\\beta" )) 1653 | ( ?B ("" )) 1654 | ( ?c ("" "" "\\cos")) 1655 | ( ?C ("" "" "\\arccos")) 1656 | ( ?d ("\\delta" "\\partial")) 1657 | ( ?D ("\\Delta" "\\nabla")) 1658 | ( ?e ("\\epsilon" "\\varepsilon" "\\exp")) 1659 | ( ?E ("\\exists" "" "\\ln")) 1660 | ( ?f ("\\phi" "\\varphi")) 1661 | ( ?F ("\\Phi" )) 1662 | ( ?g ("\\gamma" "" "\\lg")) 1663 | ( ?G ("\\Gamma" "" "10^{?}")) 1664 | ( ?h ("\\eta" "\\hbar")) 1665 | ( ?H ("" )) 1666 | ( ?i ("\\in" "\\imath")) 1667 | ( ?I ("" "\\Im")) 1668 | ( ?j ("" "\\jmath")) 1669 | ( ?J ("" )) 1670 | ( ?k ("\\kappa" )) 1671 | ( ?K ("" )) 1672 | ( ?l ("\\lambda" "\\ell" "\\log")) 1673 | ( ?L ("\\Lambda" )) 1674 | ( ?m ("\\mu" )) 1675 | ( ?M ("" )) 1676 | ( ?n ("\\nu" "" "\\ln")) 1677 | ( ?N ("\\nabla" "" "\\exp")) 1678 | ( ?o ("\\omega" )) 1679 | ( ?O ("\\Omega" "\\mho")) 1680 | ( ?p ("\\pi" "\\varpi")) 1681 | ( ?P ("\\Pi" )) 1682 | ( ?q ("\\theta" "\\vartheta")) 1683 | ( ?Q ("\\Theta" )) 1684 | ( ?r ("\\rho" "\\varrho")) 1685 | ( ?R ("" "\\Re")) 1686 | ( ?s ("\\sigma" "\\varsigma" "\\sin")) 1687 | ( ?S ("\\Sigma" "" "\\arcsin")) 1688 | ( ?t ("\\tau" "" "\\tan")) 1689 | ( ?T ("" "" "\\arctan")) 1690 | ( ?u ("\\upsilon" )) 1691 | ( ?U ("\\Upsilon" )) 1692 | ( ?v ("\\vee" )) 1693 | ( ?V ("" )) 1694 | ( ?w ("\\xi" )) 1695 | ( ?W ("\\Xi" )) 1696 | ( ?x ("\\chi" )) 1697 | ( ?X ("" )) 1698 | ( ?y ("\\psi" )) 1699 | ( ?Y ("\\Psi" )) 1700 | ( ?z ("\\zeta" )) 1701 | ( ?Z ("" )) 1702 | ( ? ("" )) 1703 | ( ?0 ("\\emptyset" )) 1704 | ( ?1 ("" )) 1705 | ( ?2 ("" )) 1706 | ( ?3 ("" )) 1707 | ( ?4 ("" )) 1708 | ( ?5 ("" )) 1709 | ( ?6 ("" )) 1710 | ( ?7 ("" )) 1711 | ( ?8 ("\\infty" )) 1712 | ( ?9 ("" )) 1713 | ( ?! ("\\neg" )) 1714 | ( ?@ ("" )) 1715 | ( ?# ("" )) 1716 | ( ?$ ("" )) 1717 | ( ?% ("" )) 1718 | ( ?^ ("\\uparrow" )) 1719 | ( ?& ("\\wedge" )) 1720 | ( ?\? ("" )) 1721 | ( ?~ ("\\approx" "\\simeq")) 1722 | ( ?_ ("\\downarrow" )) 1723 | ( ?+ ("\\cup" )) 1724 | ( ?- ("\\leftrightarrow" "\\longleftrightarrow" )) 1725 | ( ?* ("\\times" )) 1726 | ( ?/ ("\\not" )) 1727 | ( ?| ("\\mapsto" "\\longmapsto")) 1728 | ( ?\\ ("\\setminus" )) 1729 | ( ?\" ("" )) 1730 | ( ?= ("\\Leftrightarrow" "\\Longleftrightarrow")) 1731 | ( ?\( ("\\langle" )) 1732 | ( ?\) ("\\rangle" )) 1733 | ( ?\[ ("\\Leftarrow" "\\Longleftarrow")) 1734 | ( ?\] ("\\Rightarrow" "\\Longrightarrow")) 1735 | ( ?{ ("\\subset" )) 1736 | ( ?} ("\\supset" )) 1737 | ( ?< ("\\leftarrow" "\\longleftarrow" "\\min")) 1738 | ( ?> ("\\rightarrow" "\\longrightarrow" "\\max")) 1739 | ( ?` ("" )) 1740 | ( ?' ("\\prime" )) 1741 | ( ?. ("\\cdot" )) 1742 | ) 1743 | "Default for `cdlatex-math-symbol-alist'.") 1744 | 1745 | ;;; --------------------------------------------------------------------------- 1746 | 1747 | (defconst cdlatex-env-alist-default 1748 | '( 1749 | ;;------------------------------------ 1750 | ( "abstract" 1751 | "\\begin{abstract} 1752 | ? 1753 | \\end{abstract}" 1754 | nil 1755 | ) 1756 | ;;------------------------------------ 1757 | ( "appendix" 1758 | "\\begin{appendix} 1759 | ? 1760 | \\end{appendix}" 1761 | nil 1762 | ) 1763 | ;;------------------------------------ 1764 | ( "array" 1765 | "\\begin{array}[tb]{?lcrp{width}*{num}{lcrp{}}|} 1766 | & & & \\\\ 1767 | \\end{array}" 1768 | " & & &" 1769 | ) 1770 | ;;------------------------------------ 1771 | ( "center" 1772 | "\\begin{center} 1773 | ? \\\\ 1774 | \\end{center}" 1775 | nil 1776 | ) 1777 | ;;------------------------------------ 1778 | ( "deflist" 1779 | "\\begin{deflist}{width-text} 1780 | \\item ? 1781 | \\end{deflist}" 1782 | "\\item ?" 1783 | ) 1784 | ;;------------------------------------ 1785 | ( "description" 1786 | "\\begin{description} 1787 | \\item[?] 1788 | \\end{description}" 1789 | "\\item[?] " 1790 | ) 1791 | ;;------------------------------------ 1792 | ( "displaymath" 1793 | "\\begin{displaymath} 1794 | ? 1795 | \\end{displaymath}" 1796 | nil 1797 | ) 1798 | ;;------------------------------------ 1799 | ( "document" 1800 | "\\begin{document} 1801 | ? 1802 | \\end{document}" 1803 | nil 1804 | ) 1805 | ;;------------------------------------ 1806 | ( "enumerate" 1807 | "\\begin{enumerate} 1808 | \\itemAUTOLABEL ? 1809 | \\end{enumerate}" 1810 | "\\itemAUTOLABEL ?" 1811 | ) 1812 | ;;------------------------------------ 1813 | ( "eqnarray" 1814 | "\\begin{eqnarray} 1815 | AUTOLABEL 1816 | ? & & \\\\ 1817 | \\end{eqnarray}" 1818 | "\\\\AUTOLABEL 1819 | ? & & " 1820 | ) 1821 | ;;------------------------------------ 1822 | ( "eqnarray*" 1823 | "\\begin{eqnarray*} 1824 | ? & & \\\\ 1825 | \\end{eqnarray*}" 1826 | "\\\\? & & " 1827 | ) 1828 | ;;------------------------------------ 1829 | ( "equation" 1830 | "\\begin{equation} 1831 | AUTOLABEL 1832 | ? 1833 | \\end{equation}" 1834 | nil 1835 | ) 1836 | ;;------------------------------------ 1837 | ( "figure" 1838 | "\\begin{figure}[htbp] 1839 | \\centerline{\\includegraphics[]{AUTOFILE}} 1840 | \\caption[]{AUTOLABEL ?} 1841 | \\end{figure}" 1842 | nil 1843 | ) 1844 | ;;------------------------------------ 1845 | ( "figure*" 1846 | "\\begin{figure*}[htbp] 1847 | \\centerline{\includegraphics[]{AUTOFILE} 1848 | \\end{figure*}" 1849 | nil 1850 | ) 1851 | ;;------------------------------------ 1852 | ( "flushleft" 1853 | "\\begin{flushleft} 1854 | ? \\\\ 1855 | \\end{flushleft}" 1856 | "\\\\?" 1857 | ) 1858 | ;;------------------------------------ 1859 | ( "flushright" 1860 | "\\begin{flushright} 1861 | ? \\\\ 1862 | \\end{flushright}" 1863 | "\\\\?" 1864 | ) 1865 | ;;------------------------------------ 1866 | ( "fussypar" 1867 | "\\begin{fussypar} 1868 | ? 1869 | \\end{fussypar}" 1870 | nil 1871 | ) 1872 | ;;------------------------------------ 1873 | ( "itemize" 1874 | "\\begin{itemize} 1875 | \\item ? 1876 | \\end{itemize}" 1877 | "\\item ?" 1878 | ) 1879 | ;;------------------------------------ 1880 | ( "letter" 1881 | "\\begin{letter} 1882 | ? 1883 | \\end{letter}" 1884 | nil 1885 | ) 1886 | ;;------------------------------------ 1887 | ( "list" 1888 | "\\begin{list}{}{} 1889 | \\item ? 1890 | \\end{list}" 1891 | "\\item ?" 1892 | ) 1893 | ;;------------------------------------ 1894 | ( "math" 1895 | "\\begin{math} 1896 | ? 1897 | \\end{math}" 1898 | nil 1899 | ) 1900 | ;;------------------------------------ 1901 | ( "minipage" 1902 | "\\begin{minipage}[bt]{?.cm} 1903 | 1904 | \\end{minipage}" 1905 | nil 1906 | ) 1907 | ;;------------------------------------ 1908 | ( "picture" 1909 | "\\begin{picture}(,)(,) 1910 | ? 1911 | \\end{picture}" 1912 | nil 1913 | ) 1914 | ;;------------------------------------ 1915 | ( "quotation" 1916 | "\\begin{quotation} 1917 | ? 1918 | \\end{quotation}" 1919 | nil 1920 | ) 1921 | ;;------------------------------------ 1922 | ( "quote" 1923 | "\\begin{quote} 1924 | ? 1925 | \\end{quote}" 1926 | nil 1927 | ) 1928 | ;;------------------------------------ 1929 | ( "sloppypar" 1930 | "\\begin{sloppypar} 1931 | ? 1932 | \\end{sloppypar}" 1933 | nil 1934 | ) 1935 | ;;------------------------------------ 1936 | ( "tabbing" 1937 | "\\begin{tabbing} 1938 | ? \\= \\= \\= \\= \\\\[0.5ex] 1939 | \\> \\> \\> \\> \\\\ 1940 | \\end{tabbing}" 1941 | "\\\\?" 1942 | ) 1943 | ;;------------------------------------ 1944 | ( "table" 1945 | "\\begin{table}[htbp] 1946 | \\caption[]{AUTOLABEL ?} 1947 | \\vspace{4mm} 1948 | 1949 | \\end{table}" 1950 | nil 1951 | ) 1952 | ;;------------------------------------ 1953 | ( "tabular" 1954 | "\\begin{tabular}[tb]{lcrp{width}*{num}{lcrp{}}|} 1955 | ? 1956 | \\end{tabular}" 1957 | nil 1958 | ) 1959 | ;;------------------------------------ 1960 | ( "tabular*" 1961 | "\\begin{tabular*}{width}[tb]{lcrp{width}*{num}{lcrp{}}|} 1962 | ? 1963 | \\end{tabular*}" 1964 | nil 1965 | ) 1966 | ;;------------------------------------ 1967 | ( "thebibliography" 1968 | "\\begin{thebibliography}{} 1969 | 1970 | \\bibitem[?]{} 1971 | 1972 | \\end{thebibliography}" 1973 | " 1974 | \\bibitem[?]{} 1975 | ") 1976 | ;;------------------------------------ 1977 | ( "theindex" 1978 | "\\begin{theindex} 1979 | ? 1980 | \\end{theindex}" 1981 | nil 1982 | ) 1983 | ;;------------------------------------ 1984 | ( "titlepage" 1985 | "\\begin{titlepage} 1986 | 1987 | \\title{?} 1988 | 1989 | \\author{ 1990 | \\\\ 1991 | \\\\ 1992 | %\\thanks{} 1993 | %\\and 1994 | } 1995 | 1996 | \\date{\\today} 1997 | 1998 | \\end{titlepage}" 1999 | nil 2000 | ) 2001 | ;;------------------------------------ 2002 | ( "trivlist" 2003 | "\\begin{trivlist} 2004 | ? 2005 | \\end{trivlist}" 2006 | nil 2007 | ) 2008 | ;;------------------------------------ 2009 | ( "verbatim" 2010 | "\\begin{verbatim} 2011 | ? 2012 | \\end{verbatim}" 2013 | nil 2014 | ) 2015 | ;;------------------------------------ 2016 | ( "verbatim*" 2017 | "\\begin{verbatim*} 2018 | ? 2019 | \\end{verbatim*}" 2020 | nil 2021 | ) 2022 | ;;------------------------------------ 2023 | ( "verse" 2024 | "\\begin{verse} 2025 | ? \\\\ 2026 | \\end{verse}" 2027 | nil 2028 | ) 2029 | ;;------------------------------------ 2030 | ;; AMS-LaTeX 2031 | ( "align" 2032 | "\\begin{align} 2033 | AUTOLABEL 2034 | ? 2035 | \\end{align}" 2036 | "\\\\AUTOLABEL 2037 | ?") 2038 | ;;------------------------------------ 2039 | ( "align*" 2040 | "\\begin{align*} 2041 | ? 2042 | \\end{align*}" 2043 | "\\\\?") 2044 | ;;------------------------------------ 2045 | ( "alignat" 2046 | "\\begin{alignat}{?} 2047 | AUTOLABEL 2048 | 2049 | \\end{alignat}" 2050 | "\\\\AUTOLABEL 2051 | ?") 2052 | ;;------------------------------------ 2053 | ( "alignat*" 2054 | "\\begin{alignat*}{?} 2055 | 2056 | \\end{alignat*}" 2057 | "\\\\?") 2058 | ;;------------------------------------ 2059 | ( "xalignat" 2060 | "\\begin{xalignat}{?} 2061 | AUTOLABEL 2062 | 2063 | \\end{xalignat}" 2064 | "\\\\AUTOLABEL 2065 | ?") 2066 | ;;------------------------------------ 2067 | ( "xalignat*" 2068 | "\\begin{xalignat*}{?} 2069 | 2070 | \\end{xalignat*}" 2071 | "\\\\?") 2072 | ;;------------------------------------ 2073 | ( "xxalignat" 2074 | "\\begin{xxalignat}{?} 2075 | 2076 | \\end{xxalignat}" 2077 | "\\\\?") 2078 | ;;------------------------------------ 2079 | ("multline" 2080 | "\\begin{multline} 2081 | AUTOLABEL 2082 | ? 2083 | \\end{multline}" 2084 | "\\\\AUTOLABEL 2085 | ?") 2086 | ;;------------------------------------ 2087 | ("multline*" 2088 | "\\begin{multline*} 2089 | ? 2090 | \\end{multline*}" 2091 | "?") 2092 | ;;------------------------------------ 2093 | ( "flalign" 2094 | "\\begin{flalign} 2095 | AUTOLABEL 2096 | ? 2097 | \\end{flalign}" 2098 | "\\\\AUTOLABEL 2099 | ?" 2100 | ) 2101 | ;;------------------------------------ 2102 | ( "flalign*" 2103 | "\\begin{flalign*} 2104 | ? 2105 | \\end{flalign*}" 2106 | "\\\\?" 2107 | ) 2108 | ;;------------------------------------ 2109 | ( "gather" 2110 | "\\begin{gather} 2111 | AUTOLABEL 2112 | ? 2113 | \\end{gather}" 2114 | "\\\\AUTOLABEL 2115 | ?") 2116 | ;;------------------------------------ 2117 | ( "gather*" 2118 | "\\begin{gather*} 2119 | ? 2120 | \\end{gather*}" 2121 | "\\\\?") 2122 | ;;------------------------------------ 2123 | ( "split" 2124 | "\\begin{split} 2125 | ? 2126 | \\end{split}" 2127 | "\\\\?") 2128 | ;;------------------------------------ 2129 | ;;; SOME NON-STANDARD ENVIRONMENTS 2130 | ;; figure environment for the epsf macro package 2131 | ( "epsfigure" 2132 | "\\begin{figure}[htbp] 2133 | \\centerline{\\epsfxsize=\\textwidth \\epsffile{?.eps}} 2134 | \\caption[]{AUTOLABEL} 2135 | \\end{figure}" 2136 | nil 2137 | ) 2138 | ;;------------------------------------ 2139 | ;; table environment for AASTeX 2140 | ( "deluxetable" 2141 | "\\begin{deluxetable}{?lcrp{width}*{num}{lcrp{}}} 2142 | \\tablecolumns{} 2143 | \\tablewidth{0pt} 2144 | \\tablecaption{AUTOLABEL } 2145 | \\tablehead{ \\colhead{} & \colhead{} & \\multicolumn{3}{c}{} } 2146 | \\startdata 2147 | & & \\nl 2148 | \\enddata 2149 | \\end{deluxetable}" 2150 | nil 2151 | ) 2152 | ;;------------------------------------ 2153 | ;; figure environments for A&A 2154 | ( "aafigure" 2155 | "\\begin{figure} 2156 | \\resizebox{\\hsize}{!}{\\includegraphics{?.eps}} 2157 | \\caption[]{AUTOLABEL} 2158 | \\end{figure}" 2159 | nil 2160 | ) 2161 | ;;------------------------------------ 2162 | ( "aafigure*" 2163 | "\\begin{figure*} 2164 | \\resizebox{12cm}{!}{\\includegraphics{?.eps}} 2165 | \\caption[]{AUTOLABEL} 2166 | \\end{figure*}" 2167 | nil 2168 | ) 2169 | )) 2170 | 2171 | ;;; --------------------------------------------------------------------------- 2172 | ;;; 2173 | ;;; Functions to compile the tables, reset the mode etc. 2174 | 2175 | (defun cdlatex-reset-mode () 2176 | "Reset CDLaTeX Mode. 2177 | Required to implement changes to some list variables. This 2178 | function will compile the information in `cdlatex-label-alist' 2179 | and similar variables. It is called when CDLaTeX is first used, 2180 | and after changes to these variables via 2181 | `cdlatex-add-to-label-alist'." 2182 | (interactive) 2183 | (cdlatex-compute-tables)) 2184 | 2185 | (defalias 'cdlatex-nop #'ignore) 2186 | 2187 | (defun cdlatex-compute-tables () 2188 | "Update tables not connected with ref and cite support." 2189 | (setq cdlatex-env-alist-comb 2190 | (cdlatex-uniquify 2191 | (append cdlatex-env-alist 2192 | cdlatex-env-alist-default))) 2193 | ;; Merge the user specified lists with the defaults 2194 | (setq cdlatex-command-alist-comb 2195 | (cdlatex-uniquify 2196 | (append cdlatex-command-alist 2197 | cdlatex-command-alist-default))) 2198 | (setq cdlatex-math-symbol-alist-comb 2199 | (cdlatex-uniquify 2200 | (mapcar (lambda (x) 2201 | (if (listp (nth 1 x)) 2202 | (cons (car x) (nth 1 x)) 2203 | x)) 2204 | (append cdlatex-math-symbol-alist 2205 | cdlatex-math-symbol-alist-default)))) 2206 | (setq cdlatex-math-modify-alist-comb 2207 | (cdlatex-uniquify 2208 | (append cdlatex-math-modify-alist 2209 | cdlatex-math-modify-alist-default))) 2210 | 2211 | ;; find out how many levels are needed for the math symbol stuff 2212 | (setq cdlatex-math-symbol-no-of-levels 2213 | (1- (apply #'max (mapcar #'length cdlatex-math-symbol-alist-comb)))) 2214 | 2215 | ;; The direct key bindings. 2216 | (let (map dummy-map prefix modifiers symbol bindings) 2217 | (cl-loop for level from 1 to cdlatex-math-symbol-no-of-levels do 2218 | (setq dummy-map (make-sparse-keymap)) 2219 | (setq prefix (car (nth (1- level) 2220 | cdlatex-math-symbol-direct-bindings))) 2221 | (setq modifiers (cdr 2222 | (nth (1- level) 2223 | cdlatex-math-symbol-direct-bindings))) 2224 | (when (or prefix modifiers) 2225 | (cond 2226 | ((stringp prefix) (setq prefix (read-kbd-macro prefix))) 2227 | ((integerp prefix) (setq prefix (vector prefix)))) 2228 | 2229 | (if (null prefix) 2230 | (setq map cdlatex-mode-map) 2231 | (setq map (make-keymap)) 2232 | (define-key cdlatex-mode-map prefix (setq map 2233 | (make-keymap)))) 2234 | (define-key dummy-map 2235 | (vector (append modifiers (list ?a))) #'cdlatex-nop) 2236 | (push (cons level (substitute-command-keys 2237 | "\\\\[cdlatex-nop]")) 2238 | bindings) 2239 | (mapc (lambda (entry) 2240 | (setq symbol (nth level entry)) 2241 | (when (and symbol (stringp symbol) 2242 | (not (equal "" symbol))) 2243 | (define-key 2244 | map (vector (append modifiers (list (car entry)))) 2245 | (list 'lambda '() '(interactive) 2246 | (list 'cdlatex-insert-math symbol))))) 2247 | cdlatex-math-symbol-alist-comb))) 2248 | (put 'cdlatex-math-symbol-alist-comb 'cdlatex-bindings bindings))) 2249 | 2250 | (defun cdlatex-insert-math (string) 2251 | "Insert STRING in math-mode." 2252 | (cdlatex-ensure-math) 2253 | (insert string) 2254 | (if (string-match "\\?" string) 2255 | (cdlatex-position-cursor))) 2256 | 2257 | ;;; cdlatex-electric-index-mode 2258 | 2259 | (defgroup cdlatex-electricindex nil 2260 | "LaTeX electric digit indices." 2261 | :tag "cdlatex-electricindex" 2262 | :link '(url-link :tag "Home Page" "https://github.com/cdominik/cdlatex") 2263 | :prefix "cdlatex-electricindex-" 2264 | :group 'tex) 2265 | 2266 | (defvar cdlatex-electricindex-mode nil 2267 | "Determines if cdlatex-electricindex minor mode is active.") 2268 | (make-variable-buffer-local 'cdlatex-electricindex-mode) 2269 | 2270 | (defvar cdlatex-electricindex-mode-map 2271 | (let ((map (make-sparse-keymap))) 2272 | (define-key map "1" #'cdlatex-electricindex-digit) 2273 | (define-key map "2" #'cdlatex-electricindex-digit) 2274 | (define-key map "3" #'cdlatex-electricindex-digit) 2275 | (define-key map "4" #'cdlatex-electricindex-digit) 2276 | (define-key map "5" #'cdlatex-electricindex-digit) 2277 | (define-key map "6" #'cdlatex-electricindex-digit) 2278 | (define-key map "7" #'cdlatex-electricindex-digit) 2279 | (define-key map "8" #'cdlatex-electricindex-digit) 2280 | (define-key map "9" #'cdlatex-electricindex-digit) 2281 | (define-key map "0" #'cdlatex-electricindex-digit) 2282 | map) 2283 | "Keymap for cdlatex-electricindex minor mode.") 2284 | 2285 | ;;;###autoload 2286 | (defun turn-on-cdlatex-electricindex () 2287 | "Turn on cdlatex-electricindex minor mode." 2288 | (cdlatex-electricindex-mode t)) 2289 | 2290 | ;;;###autoload 2291 | (define-minor-mode cdlatex-electricindex-mode 2292 | "Minor mode for electric insertion of numbered indixes. 2293 | 2294 | cdlatex-electricindex is a minor mode supporting fast digit index 2295 | insertation in LaTeX math. For example typing x 1 2 will insert 2296 | x_{12}. 2297 | 2298 | To turn cdlatex-electricindex Minor Mode on and off in a 2299 | particular buffer, use `M-x cdlatex-electricindex-mode'. 2300 | 2301 | To turn on cdlatex-electricindex Minor Mode for all LaTeX files, 2302 | add one of the following lines to your .emacs file: 2303 | 2304 | (add-hook 'latex-mode-hook #'turn-on-cdlatex-electricindex) 2305 | 2306 | This index insertion will only work when the cursor is in a LaTeX 2307 | math environment, based on (texmathp). If texmathp is not 2308 | available, math math-mode will be assumed. 2309 | 2310 | Entering `cdlatex-electricindex-mode' calls the hook 2311 | `cdlatex-electricindex-mode-hook'." 2312 | :lighter " EI") 2313 | 2314 | (defun cdlatex-electricindex-active-here () 2315 | (if (eq major-mode 'latex-mode) 2316 | (if (fboundp 'texmathp) 2317 | (texmathp) 2318 | t) 2319 | t)) 2320 | 2321 | (defun cdlatex-electricindex-digit () 2322 | "Insert digit, maybe as an index to a quantity in math environment." 2323 | (interactive) 2324 | (if (not (cdlatex-electricindex-active-here)) 2325 | (self-insert-command 1) 2326 | (let ((digit (char-to-string (event-basic-type last-command-event)))) 2327 | (if (looking-back "[a-zA-Z]" (1- (point))) 2328 | (insert "_" digit " ") 2329 | (if (looking-back "\\(_[0-9]\\) ?" (- (point) 3)) 2330 | (progn 2331 | (goto-char (match-beginning 1)) 2332 | (forward-char 1) 2333 | (insert "{") 2334 | (forward-char 1) 2335 | (insert digit "}") 2336 | (if (looking-at " ") 2337 | (forward-char 1) 2338 | (insert " "))) 2339 | (if (looking-back "_{\\([0-9]+\\)} ?" 2340 | (max (- (point) 10) (point-min))) 2341 | (save-excursion 2342 | (goto-char (match-end 1)) 2343 | (insert digit)) 2344 | (self-insert-command 1))))))) 2345 | 2346 | ;;; Menus -------------------------------------------------------------------- 2347 | 2348 | ;; Define a menu for the menu bar if Emacs is running under X 2349 | 2350 | (require 'easymenu) 2351 | 2352 | (easy-menu-define 2353 | cdlatex-mode-menu cdlatex-mode-map 2354 | "Menu used in CDLaTeX mode." 2355 | '("CDLTX" 2356 | ["\\begin{...} \\label" cdlatex-environment t] 2357 | ["\\item \\label" cdlatex-item t] 2358 | "----" 2359 | ["Insert Math Symbol" cdlatex-math-symbol t] 2360 | ["Modify Math Symbol" cdlatex-math-modify t] 2361 | "----" 2362 | ("Customize" 2363 | ["Browse CDLaTeX group" cdlatex-customize t] 2364 | "---" 2365 | ["Build Full Customize Menu" cdlatex-create-customize-menu 2366 | (fboundp 'customize-menu-create)]) 2367 | "----" 2368 | ["Toggle electricindex" cdlatex-electricindex-mode t] 2369 | "----" 2370 | ["Show documentation" cdlatex-show-commentary t] 2371 | ["Help with KEYWORD Cmds" cdlatex-command-help t] 2372 | ["Reset CDLaTeX Mode" cdlatex-reset-mode t])) 2373 | 2374 | ;(eval-after-load "cus-edit" 2375 | ; '(and (fboundp 'customize-menu-create) (cdlatex-create-customize-menu))) 2376 | 2377 | ;;; Run Hook ------------------------------------------------------------------ 2378 | 2379 | (run-hooks 'cdlatex-load-hook) 2380 | 2381 | ;;; That's it! ---------------------------------------------------------------- 2382 | 2383 | ; Make sure tables are compiled 2384 | (cdlatex-compute-tables) 2385 | 2386 | (provide 'cdlatex) 2387 | 2388 | ;;;============================================================================ 2389 | 2390 | ;;; cdlatex.el ends here 2391 | --------------------------------------------------------------------------------