smartparens
smartparens copied to clipboard
LaTeX mode: the feature that encloses a region by $ doesn't work
Expected behavior
in LaTeX mode, when marking a region like foo |zoo bar|> zar
, where |
indicates the region boundary, and >
indicates the cursor and type the $
key, I expected that the text zoo bar
will be enclosed in a pair of $
, like: foo $zoo bar$ zar
.
Actual behavior
The text zoo bar
is deleted. The remain text is foo zar
Steps to reproduce the problem
- In LaTeX mode, type
foo zoo bar zar
, - Mark the region
zoo bar
- Type
$
- Result:
foo zar
(zoo bar
is deleted)
Environment & version information
Linux Mint 18.3, Spacsmacs, GNU Emacs 26.0.90, smartparens-20171201.242
I try to test with a new few pairs in latex mode and here is the result:
(sp-local-pair "^" "^") --> the wrapping feature works normally
(sp-local-pair "^" "$") --> the wrapping feature works normally
(sp-local-pair "$" "^") --> the wrapping feature doesn't work
(sp-local-pair "$" "$") --> the wrapping feature doesn't work
Looking in the code of smartparens, I observed that the variable sp-wrap-overlays
was computed differently, when $
is typed, as opposed to {, (, [
(at the same location of tex)
sp-wrap-overlay for $
:
(#<overlay from 720 to 721 in foo.tex> . #<overlay from 722 to 722 in foo.tex>)
sp-wrap-overlay for {.(,[
:
(#<overlay from 721 to 722 in foo.tex> . #<overlay from 732 to 732 in foo.tex>)
I did more investigation and believed that the issue was introduced in the commit https://github.com/Fuco1/smartparens/commit/7a8dff4c6f0eb433c3f2bb92b04b5aac5d06d416: (Properly integrate strict mode and delete-selection-mode)
The revision https://github.com/Fuco1/smartparens/commit/97e6dd6a8cf2df3ec507c434a217a2061f8af2d4 (Add nil-check to sp-in-docstring-p for string bounds) works normally.
The problem is that $
in latex mode is bound to some command which does not fire self-insert-command
which is what delete-selection-mode
uses as a hook. So the region is deleted prior to the wrapping because delsel does not register the command as "working on the region".
This can be fixed somehow, but is pretty hackish. I'm trying to come up with some nice solution (the issue being that only one predicate can be registered for delete-selection
property and latex mode already adds something by itself).
Thank you for the confirmation and I'm looking forward to your update!
Currently, I am using the working revision https://github.com/Fuco1/smartparens/commit/97e6dd6a8cf2df3ec507c434a217a2061f8af2d4 of smartparens
A workaround in the meantime is to rebind $
to self-insert-command in TeX-mode-map
.
@jabranham Thank you for the suggestion!
Same happen for the quotation; where it is supposed that the marked region is inserted in a quotation like `` " , when I just hit ". How can we fix that? Also, I do not know how to " rebind $ to self-insert-command in TeX-mode-map" as you suggested.
For rebind $
, you can try this: (define-key LaTeX-mode-map (kbd "$") 'self-insert-command)
It works for me with the default config on emacs 26.1, I can't repro this.
For rebind
$
, you can try this:(define-key LaTeX-mode-map (kbd "$") 'self-insert-command)
this doesn't work for me (GNU Emacs 26.3 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.22.30) of 2019-09-16)
Without the code above, when I selected abc
and then press $
, it cancela the whole string abc
.
With the code above, it does differently: it delets abc
and put inside $$
the next 3 chars after the original abc
Seems that this was also happening because before the functions were called TeX-insert-dollar
and TeX-insert-quote
and now are renamed to all lower-case. I added the new variants to the list of functions where we use some special handling and it started to work for me. I also added a test case for future regressions.