views:

280

answers:

4

I'm not too familiar with elisp, and am trying to learn. In emacs, I'd like to be able to do the following:

  1. Mark via C-space
  2. Go to where I want the the marking to end, so I have a region that is highlighted, suppose it is "highlighted text"
  3. Hit a key-sequence
  4. Have emacs ask me to input some text, say "plot", and
  5. Have that highlighted text change to be "plot(highlighted text)". That is, I'd like to wrap the highlited text with parentheses and precede it with the text I input.

    (defun wrap-text ()
        )
    

I suppose the input of the function would be the highlighted text, but I don't know where to start looking. The other hard part would be the input text part. Could someone guide me? Thanks.

+5  A: 

For your case, this should work:

(defun wrap-text (b e txt)
  "simple wrapper"
  (interactive "r\nMEnter text to wrap with: ")
  (save-restriction
    (narrow-to-region b e)
    (goto-char (point-min))
    (insert txt)
    (insert "(")
    (goto-char (point-max))
    (insert ")")))

(global-set-key (kbd "C-x M-w") 'wrap-text)
Trey Jackson
See also: http://xahlee.org/emacs/wrap-url.html
Török Gábor
Also this question/answer: http://stackoverflow.com/questions/1397113/how-to-tag-text-in-emacs/1397134#1397134 .
Trey Jackson
how exactly would u do something like this with yasnippet? i'm familiar with:1. type in keyword2. expandthis involves adding text at the beginning and end of a region. i'd be interested to hear this solution as well. thanks.
Vinh Nguyen
@Vinh Good point, you do need the initial text to expand for yasnippet, I'll remove that from the answer.
Trey Jackson
+1  A: 

thanks trey jackson. i didn't know u posted a solution so i went to #emacs on the freenode for help. after some research, i came up with the following:

(defun ess-R-wrap-content-vqn ()
  "Wrap marked region with a specified PREFIX and closing parentheses."
  (interactive)
  (set (make-local-variable 'prefix) (read-from-minibuffer "function: "))
  (set (make-local-variable 'prefix) (concat prefix "("))
  (save-excursion (goto-char (region-beginning)) (insert prefix))
  (save-excursion (goto-char (region-end)) (insert ")"))
)
(define-key ess-mode-map "\C-c\M-w" 'ess-R-wrap-content-vqn) ;; w is for wrap

i thought stackoverflow was going to notify me when a solution is posted. again, thanks. learning a little more of elisp from this.

Vinh Nguyen
+1  A: 

Something a bit closer to your version, but with some changes :

  • you can use 'let' to create a local-variable
  • region-beginning and region-end gives you the equivalent of what trey did with

Here is an example :

 (defun wrap-in-function ()
   "Wrap marked region with a specified PREFIX and closing parentheses."
   (interactive)
   (let ((prefix (read-from-minibuffer "function: ")))
     (save-excursion
       (goto-char (region-beginning))
       (insert (concat prefix "(")))
     (save-excursion
       (goto-char (region-end))
       (insert ")"))))

Another difference between the two versions is the position of the point after you called the function ; trey version might be better to use (matter of taste).

EDIT : edited following vinh remarks.

phtrivier
The `interactive' declaration in Trey's answer is a better way to do this. The 'r' indicates that a region must be defined and the rest is used as a prompt to read in the desired text using standard Emacs methods. While those methods may eventually call `read-from-minibuffer' as you did here, you lose any other standard/global Emacs behaviors you've customized.
Joe Casadonte
just tried yours and it doesn't wrap the text completely. it wraps: wrap()mytext.
Vinh Nguyen
Thanks vinh, you're right, my function only works if you select the text to wrap starting from the end. I'll try and see if I can fix that.
phtrivier
A: 

this requires 'cl but is otherwise pretty tiny. been using it for a few years.

(require 'cl) ;;if you haven't elsewhere
(defun decorate-region( beg end prefix suffix )
  (interactive "r\nMPrefix: \nMSuffix: ")
  (cl-set-buffer-substring beg end (concat prefix
                                           (buffer-substring beg end)
                                           suffix)))
rgiar