How to select a region with a leading \$, and surround it with a yasnippet

522 views Asked by At

Could anyone please suggest a method to surround a region that contains a leading \$ and surround it with a snippet. In latex-mode, I am frequently underlining or double-underlining monies due and the yasnippet being used removes the backslash. I'd like to be able to use the same snippet for all situations -- with or without a leading \$.

# -*- mode: snippet -*-
# contributor: lawlist
# key: underline_selected
# group: font
# name: underline_selected
# binding: C-I u s
# --
\uline{`yas/selected-text`}
1

There are 1 answers

0
lawlist On BEST ANSWER

There are few problems that cause the behavior described by the original poster, who uses a custom modified version of tex-mode.el, not AUCTeX.

First, the function yas--snippet-parse-create contains, among other codes, the following functions that do not play well with LaTeX escaped dollar signs:

(yas--protect-escapes nil `(?\\ ?` ?'))
(yas--protect-escapes)
(yas--restore-escapes)
(yas--delete-regions ys--dollar-regions)

Second, the variable yas--simple-mirror-regexp catches dollar amounts, in addition to the standard yasnippet fields such as $1. When the above-mentioned (yas--delete-regions yas--dollar-regions) is called by yas--snippet-parse-create, the result is an erroneous deletion. The author of this answer has modified the regexp to exclude a dollar-sign with a preceding backslash:

(setq yas--simple-mirror-regexp "[^\\]$\\([0-9]+\\)")

The author of this answer does not presently have a fix for yas--protect-escapes and yas--restore-escapes, and has merely commented them out in the meantime. [This would obviously be problematic for anyone doing programming, but appears to be sufficient for merely writing LaTeX documents.] An issue has been opened on Github and the author of this answer will update this thread if a solution is found there.