monnier pushed a commit to branch externals/auctex in repository elpa. commit 49cda3042354eb74fc4f69389561e411e42eebf8 Author: Mosè Giordano <giordano.m...@libero.it> Date: Sat Jun 8 00:35:20 2013 +0200
New variable TeX-electric-math. * tex.el (TeX-math-close-double-dollar): Remove. (TeX-math-close-single-dollar): Ditto. (TeX-electric-dollar): Ditto. (TeX-electric-math): New customizable variable, supersedes variables above. (TeX-insert-dollar): Adapt to `TeX-electric-math'. * doc/auctex.texi (Quotes): Document `TeX-electric-math'. --- ChangeLog | 11 ++++ doc/auctex.texi | 73 ++++++++++++++++---------- tex.el | 156 +++++++++++++++++++++++++++++++++++------------------- 3 files changed, 157 insertions(+), 83 deletions(-) diff --git a/ChangeLog b/ChangeLog index 93335f6..22a2156 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2013-06-08 Mos� Giordano <giordano.m...@libero.it> + + * tex.el (TeX-math-close-double-dollar): Remove. + (TeX-math-close-single-dollar): Ditto. + (TeX-electric-dollar): Ditto. + (TeX-electric-math): New customizable variable, supersedes + variables above. + (TeX-insert-dollar): Adapt to `TeX-electric-math'. + + * doc/auctex.texi (Quotes): Document `TeX-electric-math'. + 2013-06-02 Mos� Giordano <giordano.m...@libero.it> * tex-buf.el: Shorten copyright year ranges and update with diff --git a/doc/auctex.texi b/doc/auctex.texi index f50b4bd..2b29a64 100644 --- a/doc/auctex.texi +++ b/doc/auctex.texi @@ -407,40 +407,57 @@ first dollar sign. (@kbd{$}) Insert dollar sign. Show matching dollar sign if this dollar sign end the @TeX{} math mode. -Ensure double dollar signs match up correctly by inserting extra dollar -signs when needed if @code{TeX-math-close-double-dollar} is non-nil. - -If @var{TeX-math-close-single-dollar} is non-nil, when outside math mode -the function insert the opening and closing dollar signs for @TeX{} -inline formula and put the point between them just by typing a single -@samp{$}. With optional @var{arg}, insert that many dollar signs. @end deffn -@defopt TeX-math-close-double-dollar -Control the insertion of double dollar signs for delimiting display -math. (Note that you should not use double dollar signs in @LaTeX{} -because this practice can lead to wrong spacing in typeset documents.) -If the variable is non-nil and you enter a dollar sign that matches a -double dollar sign @samp{$$} @AUCTeX{} will automatically insert two -dollar signs. -@end defopt - -@defopt TeX-math-close-single-dollar -Control the insertion of dollar signs for delimiting inline math. If -the variable is non-nil and you enter a dollar sign outside math mode -@AUCTeX{} will automatically insert the opening and closing dollar signs -and put the point between them. If the variable -@code{blink-matching-paren} is non-nil the opening dollar will blink. +@TeX{} and @LaTeX{} users often look for a way to insert inline +equations like @samp{$...$} or @samp{\(...\)} simply typing @kbd{$}. +@AUCTeX{} helps them through the customizable variable +@code{TeX-electric-math}. + +@defopt TeX-electric-math +If the variable is non-nil and you type @kbd{$} outside math mode, +@AUCTeX{} will automatically insert the opening and closing symbols for +an inline equation and put the point between them. The opening symbol +will blink when @code{blink-matching-paren} is non-nil. If +@code{TeX-electric-math} is nil, typing @kbd{$} simply inserts @samp{$} +at point, this is the default. + +Besides @code{nil}, possible values for this variable are @code{(cons +"$" "$")} for @TeX{} inline equations @samp{$...$}, and @code{(cons +"\\(" "\\)")} for @LaTeX{} inline equations @samp{\(...\)}. + +If the variable is non-nil and point is inside math mode right between a +couple of single dollars, pressing @kbd{$} will insert another pair of +dollar signs and leave the point between them. Thus, if +@code{TeX-electric-math} is set to @code{(cons "$" "$")} you can easily +obtain a @TeX{} display equation @samp{$$...$$} by pressing @kbd{$} +twice in a row. (Note that you should not use double dollar signs in +@LaTeX{} because this practice can lead to wrong spacing in typeset +documents.) + +In addition, when the variable is non-nil and there is an active region +outside math mode, typing @kbd{$} will put around the active region +symbols for opening and closing inline equation and keep the region +active, leaving point after the closing symbol. By pressing repeatedly +@kbd{$} while the region is active you can toggle between an inline +equation, a display equation, and no equation. To be precise, +@samp{$...$} is replaced by @samp{$$...$$}, whereas @samp{\(...\)} is +replaced by @samp{\[...\]}. @end defopt -@defopt TeX-electric-dollar -Control the insertion of dollar signs when there is an active region -outside math mode. If the variable is non-nil, typing @samp{$} will put -a pair of single dollar around the active region and leave point after -the closing dollar. -@end defopt +If you want to automatically insert @samp{$...$} in plain @TeX{} files, +and @samp{\(...\)} in @LaTeX{} files by pressing @kbd{$}, add the +following to your init file +@lisp +(add-hook 'plain-TeX-mode-hook + (lambda () (set (make-variable-buffer-local 'TeX-electric-math) + (cons "$" "$")))) +(add-hook 'LaTeX-mode-hook + (lambda () (set (make-variable-buffer-local 'TeX-electric-math) + (cons "\\(" "\\)")))) +@end lisp @subheading Braces diff --git a/tex.el b/tex.el index af57c9b..23edc03 100644 --- a/tex.el +++ b/tex.el @@ -5153,33 +5153,42 @@ See also `TeX-font-replace' and `TeX-font-replace-function'." :group 'TeX-macro :type 'boolean) -(defcustom TeX-math-close-double-dollar nil - "If non-nil close double dollar math by typing a single `$'." +(defcustom TeX-electric-math nil + "If non-nil, when outside math mode `TeX-insert-dollar' will +insert symbols for opening and closing inline equation and put +the point between them. If there is an active region, +`TeX-insert-dollar' will put around it symbols for opening and +closing inline equation and keep the region active, with point +after closing symbol. If you press `$' again, you can toggle +between inline equation, display equation, and no equation. + +If non-nil and point is inside math mode right between a couple +of single dollars, pressing `$' will insert another pair of +dollar signs and leave the point between them. + +If nil, `TeX-insert-dollar' will simply insert \"$\" at point, +this is the default. + +If non-nil, this variable is a cons cell whose CAR is the string +to insert before point, the CDR is the string to insert after +point. You can choose between \"$...$\" and \"\\(...\\)\"." :group 'TeX-macro - :type 'boolean) - -(defcustom TeX-math-close-single-dollar nil - "If non-nil, when outside math mode insert opening and closing dollar -signs for TeX inline equation and put the point between them, just by -typing a single `$'." - :group 'TeX-macro - :type 'boolean) - -(defcustom TeX-electric-dollar nil - "When outside math mode, if non-nil and there is an active -region, typing `$' will put a pair of single dollar around it and -leave point after the closing dollar." - :group 'TeX-macro - :type 'boolean) + :type '(choice (const :tag "$" nil) + (const :tag "$...$" '("$" . "$")) + (const :tag "\\(...\\)" '("\\(" . "\\)")) + (cons :tag "Other" + (string :tag "Insert before point") + (string :tag "Insert after point")))) (defun TeX-insert-dollar (&optional arg) "Insert dollar sign. If current math mode was not entered with a dollar, refuse to insert one. Show matching dollar sign if this dollar sign ends -the TeX math mode and `blink-matching-paren' is non-nil. Ensure -double dollar signs match up correctly by inserting extra dollar -signs when needed. +the TeX math mode and `blink-matching-paren' is non-nil. + +When outside math mode, the behavior is controlled by the variable +`TeX-electric-math'. With raw \\[universal-argument] prefix, insert exactly one dollar sign. With optional ARG, insert that many dollar signs." @@ -5197,47 +5206,84 @@ sign. With optional ARG, insert that many dollar signs." (insert "$")) ((texmathp) ;; We are inside math mode - (if (and (stringp (car texmathp-why)) - (string-equal (substring (car texmathp-why) 0 1) "\$")) - ;; Math mode was turned on with $ or $$ - so finish it accordingly. - (progn - (if TeX-math-close-double-dollar - (insert (car texmathp-why)) - (insert "$")) - (when (and blink-matching-paren - (or (string= (car texmathp-why) "$") - (zerop (mod (save-excursion - (skip-chars-backward "$")) 2)))) - (save-excursion - (goto-char (cdr texmathp-why)) - (if (pos-visible-in-window-p) - (sit-for blink-matching-delay) - (message "Matches %s" - (buffer-substring - (point) (progn (end-of-line) (point)))))))) + (cond + ((and TeX-electric-math + (eq (preceding-char) ?\$) + (eq (following-char) ?\$)) + ;; Point is between "$$" and `TeX-electric-math' is non-nil - insert + ;; another pair of dollar signs and leave point between them. + (insert "$$") + (backward-char)) + ((and (stringp (car texmathp-why)) + (string-equal (substring (car texmathp-why) 0 1) "\$")) + ;; Math mode was turned on with $ or $$ - insert a single $. + (insert "$") + ;; Compatibility, `TeX-math-close-double-dollar' has been removed + ;; after AUCTeX 11.87. + (if (boundp 'TeX-math-close-double-dollar) + (message + (concat "`TeX-math-close-double-dollar' has been removed," + "\nplease use `TeX-electric-math' instead."))) + (when (and blink-matching-paren + (or (string= (car texmathp-why) "$") + (zerop (mod (save-excursion + (skip-chars-backward "$")) 2)))) + (save-excursion + (goto-char (cdr texmathp-why)) + (if (pos-visible-in-window-p) + (sit-for blink-matching-delay) + (message "Matches %s" + (buffer-substring + (point) (progn (end-of-line) (point)))))))) + (t ;; Math mode was not entered with dollar - we cannot finish it with one. (message "Math mode started with `%s' cannot be closed with dollar" (car texmathp-why)) - (insert "$"))) + (insert "$")))) (t ;; Just somewhere in the text. - (if (and TeX-electric-dollar (TeX-active-mark)) - (progn - (if (> (point) (mark)) - (exchange-point-and-mark)) - (insert "$") - (exchange-point-and-mark) - (insert "$")) - (if TeX-math-close-single-dollar + (cond + ((and TeX-electric-math (TeX-active-mark)) + (if (> (point) (mark)) + (exchange-point-and-mark)) + (cond + ;; $...$ to $$...$$ + ((and (eq last-command 'TeX-insert-dollar) + (re-search-forward "\\=\\$\\([^$][^z-a]*[^$]\\)\\$" (mark) t)) + (replace-match "$$\\1$$") + (push-mark (match-beginning 0) t)) + ;; \(...\) to \[...\] + ((and (eq last-command 'TeX-insert-dollar) + (re-search-forward "\\=\\\\(\\([^z-a]*\\)\\\\)" (mark) t)) + (replace-match "\\\\[\\1\\\\]") + (push-mark (match-beginning 0) t)) + ;; Strip \[...\] or $$...$$ + ((and (eq last-command 'TeX-insert-dollar) + (or (re-search-forward "\\=\\\\\\[\\([^z-a]*\\)\\\\\\]" (mark) t) + (re-search-forward "\\=\\$\\$\\([^z-a]*\\)\\$\\$" (mark) t))) + (replace-match "\\1") + (push-mark (match-beginning 0) t)) + (t + ;; We use `save-excursion' because point must be situated before opening + ;; symbol. + (save-excursion (insert (car TeX-electric-math))) + (exchange-point-and-mark) + (insert (cdr TeX-electric-math)))) + ;; Keep the region active. + (if (featurep 'xemacs) + (zmacs-activate-region) + (setq activate-mark t + deactivate-mark nil))) + (TeX-electric-math + (insert (car TeX-electric-math)) + (save-excursion (insert (cdr TeX-electric-math))) + (if blink-matching-paren (progn - (insert "$$") - (if blink-matching-paren - (progn - (backward-char 2) - (sit-for blink-matching-delay) - (forward-char)) - (backward-char))) - (insert "$"))))) + (backward-char) + (sit-for blink-matching-delay) + (forward-char)))) + ;; In any other case just insert a single $. + ((insert "$"))))) (TeX-math-input-method-off)) (defvar TeX-math-input-method-off-regexp