2

There is built-in emacs function for surrounding a region with parenthesis, as I found it here: https://stackoverflow.com/a/2952021/1635919. Is there an analogous way to surround a region with dollar ($)?

C-h f insert-pair tells that this function is able to surround sexp with any character, so how to bind surrounding with $ to M-$ as in the linked answer?

My Emacs: GNU Emacs 24.3.1 (i686-pc-linux-gnu, GTK+ Version 3.10.7)

Community
  • 1
  • 1
danek
  • 127
  • 1
  • 7

4 Answers4

3

You can duplicate what M-( does by looking at the insert-parentheses function. All it does is call insert-pair with hardcoded parameters, so you can analagously bind M-$ to the following function:

    (defun insert-dolla-dolla-bills-yall (&optional arg)
      (interactive "P")
      (insert-pair arg ?\$ ?\$))
neal
  • 557
  • 2
  • 9
  • 1
    Yes, that's it! :) But how did you find the function code? I tried all of [this tips](https://www.gnu.org/software/emacs/manual/html_node/eintr/Finding-More.html) but did not found any `.el` or other files containing the source. `C-h f insert-parenthesis` doesn't tell where to look neither. – danek Oct 24 '14 at 20:41
  • Interesting - it's possible that your linux distribution does not install the elisp source by default, only the byte-compiled versions. If you had the elisp source, when you do M-x describe-function, the help buffer has a link to the source code location, which is where I found the source for insert-pair. You can try searching through the package repository for your distribution to see if there's a package of the Emacs elisp source code. – neal Oct 24 '14 at 21:06
  • 1
    @neal - Is it possible to do this with (insert-pair arg ?\$\$ ?\$\$)). This code doesn't seem to work so anyway to insert double dollar signs using insert-pair? – J Spen Mar 05 '19 at 07:31
  • @JSpen It isn't possible to do it using character literals, or as I tried, calling the function twice in a row (seems to be something with insert-pair requiring transient mark mode but it being turned off for the region after the first insertion). It does, however, seem to work for me if you use character strings, e.g. (defun insert-dolla-dolla-bills-yall (&optional arg) (interactive "P") (save-mark-and-excursion (insert-pair arg "$$$" "$$$"))) Does that work for you? – neal Mar 05 '19 at 19:50
  • 1
    @Neail - No but this does work from realizing that so maybe can add to your answer, (defun jj/insert-double-dollar-dollar-sign (&optional arg) (interactive "P") (save-mark-and-excursion (insert-pair arg ?\$ ?\$)) (let (( parens-require-spaces nil)) (insert-pair arg ?\$ ?\$)) (forward-char)) – J Spen Mar 06 '19 at 03:32
  • 1
    @neal - Yours doesn't work for me because I think insert-pair only accepts a single character argument. Anyway, I tried to put in more than one character I was getting errors. Eg...."$$$". Does mine above work for you? – J Spen Mar 06 '19 at 03:33
  • Thanks JSpen. insert-pair accepts strings for arguments in the Emacs I am using (26.1) so maybe it's implementation has changed from previous versions. `insert-pair` just calls `insert` with the `open`/`close` arguments, which takes strings, so even if you call insert-pair with strings instead of characters, it should work. That said, yours does work just fine in my testing! If someone needed to do different characters or more/less than 2 times, having a parameter with the number of times and looping may be useful in those situations, too. But yours works great for the original request. – neal Mar 06 '19 at 04:51
  • @JSpen It may also be better to have the function take the number of characters and the character as arguments, so it can loop and call insert-pair that number of times, which would help if you needed different characters or a different number of repetitions. – neal Mar 06 '19 at 05:55
  • @neal I'm using 27.0.5 HEAD built like two weeks ago and that doesn't work for me. It says: `save-excursion: Wrong type argument: characterp, "$$$"`. So it is expecting a character I think but I'm giving it a string. Anyways, I only need double dollar signs for now but yeah writing a loop to do this would be a much better idea. – J Spen Mar 06 '19 at 16:23
2

Set variable insert-pair-alist to include (?\$ ?\$):

(add-to-list 'insert-pair-alist (list ?\$ ?\$))

Then bind a key to insert-pair, to do what you want:

(global-set-key (kbd "M-$") 'insert-pair)
Drew
  • 29,895
  • 7
  • 74
  • 104
  • Tried it: pasted this two lines to *scratch* and then did 'M-x eval-current-buffer'. The result: 'M-$' produces just one $ character after a marked region. Did I make something wrong? – danek Oct 24 '14 at 19:32
  • 1
    Sorry, somehow I typed `?\?` when I meant to type `?\$`. Corrected, above. – Drew Oct 24 '14 at 20:19
2

If you use the library YASnippet, it has built-in support for wrapping a surround region with custom code on either end of the selected region:

NOTE: It looks like the latest version of YASnippet has changed the name of the function from yas/selected-text to yas-selected-text. However, there appears to also be backwards compatible version.

# -*- mode: snippet -*-
# name: dollar-surround
# key: dollar-surround
# binding: C-I $
# --
$`yas/selected-text`$
BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
lawlist
  • 13,099
  • 3
  • 49
  • 158
0

There is a toolkit which delivers this alongside with a bunch of related one.

It's called ar-dollar-region-atpt

Get it here:

https://launchpad.net/s-x-emacs-werkstatt/

Andreas Röhler
  • 4,804
  • 14
  • 18
  • For some reason package expected **sh-beg-end.el** file and did not get it (while evaluating '(require 'thing-at-point-utils)' emacs said 'Cannot open load file: sh-beg-end'). After I downloaded sh-beg-end from [here](https://raw.githubusercontent.com/pdee/pdee/master/sh-beg-end.el) everything worked well. Calling ar-region-dollar-atpt did the job, thank you :) – danek Oct 24 '14 at 19:56
  • @danek Sorry, it's a bug. You may safely comment out that "(require ..." - it will only affect edits in sh-script-mode. Will upload a fix with next release ASAP. – Andreas Röhler Oct 26 '14 at 08:03
  • @danek Released new version, which should not have that bug: https://launchpad.net/s-x-emacs-werkstatt/+download – Andreas Röhler Nov 11 '14 at 12:26