On Sun, May 21, 2023 at 9:33 PM Hongyi Zhao <hongyi.z...@gmail.com> wrote: > > On Sun, May 21, 2023 at 7:14 PM Mosè Giordano <m...@gnu.org> wrote: > > > > Hi Zhao > > > > On Sun, 21 May 2023 at 09:41, Hongyi Zhao <hongyi.z...@gmail.com> wrote: > > > I want to convert `bla` to `$bla$` automatically when be highlighted > > > and typeset a `$'. > > > > I believe you want to look at `TeX-electric-math' > > (https://www.gnu.org/software/auctex/manual/auctex/Quotes.html#index-TeX_002delectric_002dmath): > > > > --8<---------------cut here---------------start------------->8--- > > TeX-electric-math is a variable defined in ‘tex.el’. > > > > Its value is ("\\(" . "\\)") > > Original value was nil > > Local in buffer foo.tex; global value is 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 "\(...\)". > > > > Automatically becomes buffer-local when set. > > You can customize this variable. > > --8<---------------cut here---------------end--------------->8--- > > Based on the above document, I've added the following hook entry in > AUCTeX's use-package configuration: > > (LaTeX-mode . (lambda () (set (make-local-variable 'TeX-electric-math) > (cons "$" "$")))) > > But I still cannot observe the desired behavior, as shown in the > attached screenshot.
I tried a lot, and only find the following code snippet does the trick: (defun xah-insert-bracket-pair (LBracket RBracket &optional WrapMethod) "Insert brackets around selection, word, at point, and maybe move cursor in between. LBracket and RBracket are strings. WrapMethod must be either `line' or `block'. `block' means between empty lines. • if there is a region, add brackets around region. • If WrapMethod is `line', wrap around line. • If WrapMethod is `block', wrap around block. • if cursor is at beginning of line and its not empty line and contain at least 1 space, wrap around the line. • If cursor is at end of a word or buffer, one of the following will happen: xyz▮ → xyz(▮) xyz▮ → (xyz▮) if in one of the lisp modes. • wrap brackets around word if any. e.g. xy▮z → (xyz▮). Or just (▮) URL `http://xahlee.info/emacs/emacs/elisp_insert_brackets_by_pair.html' Version: 2017-01-17 2021-08-12" (if (region-active-p) (progn (let ( ($p1 (region-beginning)) ($p2 (region-end))) (goto-char $p2) (insert RBracket) (goto-char $p1) (insert LBracket) (goto-char (+ $p2 2)))) (let ($p1 $p2) (cond ((eq WrapMethod 'line) (setq $p1 (line-beginning-position) $p2 (line-end-position)) (goto-char $p2) (insert RBracket) (goto-char $p1) (insert LBracket) (goto-char (+ $p2 (length LBracket)))) ((eq WrapMethod 'block) (save-excursion (let (($bds (xah-get-bounds-of-block-or-region))) (setq $p1 (car $bds) $p2 (cdr $bds))) (goto-char $p2) (insert RBracket) (goto-char $p1) (insert LBracket) (goto-char (+ $p2 (length LBracket))))) ( ; do line. line must contain space (and (eq (point) (line-beginning-position)) ;; (string-match " " (buffer-substring-no-properties (line-beginning-position) (line-end-position))) (not (eq (line-beginning-position) (line-end-position)))) (insert LBracket ) (end-of-line) (insert RBracket)) ((and (or ; cursor is at end of word or buffer. i.e. xyz▮ (looking-at "[^-_[:alnum:]]") (eq (point) (point-max))) (not (or (string-equal major-mode "xah-elisp-mode") (string-equal major-mode "emacs-lisp-mode") (string-equal major-mode "lisp-mode") (string-equal major-mode "lisp-interaction-mode") (string-equal major-mode "common-lisp-mode") (string-equal major-mode "clojure-mode") (string-equal major-mode "xah-clojure-mode") (string-equal major-mode "scheme-mode")))) (progn (setq $p1 (point) $p2 (point)) (insert LBracket RBracket) (search-backward RBracket ))) (t (progn ;; wrap around “word”. basically, want all alphanumeric, plus hyphen and underscore, but don't want space or punctuations. Also want chinese chars ;; 我有一帘幽梦,不知与谁能共。多少秘密在其中,欲诉无人能懂。 (skip-chars-backward "-_[:alnum:]") (setq $p1 (point)) (skip-chars-forward "-_[:alnum:]") (setq $p2 (point)) (goto-char $p2) (insert RBracket) (goto-char $p1) (insert LBracket) (goto-char (+ $p2 (length LBracket))))))))) (defun xah-insert-dollar () (interactive) (xah-insert-bracket-pair "$" "$") ) (global-set-key (kbd "M-4") 'xah-insert-dollar) ; $$ Best, Zhao