Skip to content

Instantly share code, notes, and snippets.

@kototama
Created November 1, 2011 16:31
Show Gist options
  • Save kototama/1331024 to your computer and use it in GitHub Desktop.
Save kototama/1331024 to your computer and use it in GitHub Desktop.
(defun earmuffy (&optional arg)
(interactive "P")
(let* ((variable (thing-at-point 'sexp))
(bounds (bounds-of-thing-at-point 'sexp))
(current-point (point))
(earmuffed-variable (concat "*" variable "*")))
(save-excursion)
(kill-region (car bounds) (cdr bounds))
(if arg
;; unearmuffy
(progn
(insert (substring variable 1 (- (length variable) 1)))
(goto-char (- current-point 1)))
;; earmuffy
(progn
(insert earmuffed-variable)
(goto-char (+ current-point 1))))))
@itoshkov
Copy link

This usage of save-excursion is pointless. This special form should be used as a "progn":

(save-excursion &rest body)

Save point, mark, and current buffer; execute body; restore those things.
Executes body just like 'progn'.
The values of point, mark and the current buffer are restored
even in case of abnormal exit (throw or error).
The state of activation of the mark is also restored.

This construct does not save 'deactivate-mark', and therefore
functions that change the buffer will still cause deactivation
of the mark at the end of the command. To prevent that, bind
'deactivate-mark' with 'let'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment