2

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`}
lawlist
  • 13,099
  • 3
  • 49
  • 158
  • I don't see any \$ in the code sample do you meant the leading backslash? If so, what you are trying to achieve is the how to detect an specific region or just to surround your selection with a yasnippet? – PuercoPop Sep 03 '13 at 06:16
  • Whenever I have a sentence in latex-mode like: "Pursuant to the terms of your retainer agreement, please deposit additional retainer in the amount of `\$500.00` on or before Friday, September 6, 2013", I double-underline `\$500.00` with the yasnippet in my question. The yasnippet I use deletes the backslash -- the backslash is needed in latex-mode when using dollar signs. – lawlist Sep 03 '13 at 06:20
  • It works for me... Which version of yasnippet do you use? – thisirs Sep 03 '13 at 16:11
  • I am using version: 0.8.0. I call the snippet with this function: `(defun underline_selected () (interactive) (yas--expand-or-visit-from-menu (quote latex-mode) "underline_selected"))`. I also use the `tab+u+s` to activate the snippet. – lawlist Sep 03 '13 at 17:29

1 Answers1

1

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.

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
lawlist
  • 13,099
  • 3
  • 49
  • 158