smartparens icon indicating copy to clipboard operation
smartparens copied to clipboard

LaTeX mode: the feature that encloses a region by $ doesn't work

Open ghost opened this issue 6 years ago • 10 comments

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

ghost avatar Dec 26 '17 08:12 ghost

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>)

ghost avatar Dec 27 '17 07:12 ghost

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.

ghost avatar Dec 27 '17 07:12 ghost

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).

Fuco1 avatar Dec 27 '17 17:12 Fuco1

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

ghost avatar Dec 28 '17 06:12 ghost

A workaround in the meantime is to rebind $ to self-insert-command in TeX-mode-map.

jabranham avatar Jan 30 '18 04:01 jabranham

@jabranham Thank you for the suggestion!

ghost avatar Feb 25 '18 13:02 ghost

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.

DrWaleedAYousef avatar Sep 04 '18 13:09 DrWaleedAYousef

For rebind $, you can try this: (define-key LaTeX-mode-map (kbd "$") 'self-insert-command)

ghost avatar Sep 05 '18 04:09 ghost

It works for me with the default config on emacs 26.1, I can't repro this.

Fuco1 avatar Sep 05 '18 09:09 Fuco1

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

pietro-coretto avatar Jul 10 '21 09:07 pietro-coretto

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.

Fuco1 avatar Mar 30 '24 18:03 Fuco1