Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Emacs lisp toggle-earmuffs
(defun toggle-earmuffs ()
"Add or remove earmuffs (asterisks at front and end) of
variables."
(interactive)
(let* ((saved-point (point))
(variable (thing-at-point 'symbol))
(bounds (bounds-of-thing-at-point 'symbol))
(len (- (cdr bounds) (car bounds)))
(start-char (elt variable 0))
(end-char (elt variable (1- len))))
(save-excursion
(cond
;; remove earmuffs
((and (= ?* start-char) (= ?* end-char) (> len 1))
(kill-region (car bounds) (cdr bounds))
(insert (substring variable 1 (1- len)))
(setq saved-point (1- saved-point)))
;; add earmuffs
((and (/= ?* start-char) (/= ?* end-char))
(kill-region (car bounds) (cdr bounds))
(insert (concat "*" variable "*"))
(setq saved-point (1+ saved-point)))
((and (= ?* start-char) (= ?* end-char) (= len 1))
(message "Don't know what to do with a single asterisk"))
(t (message "The symbol has asterisk at the one end only"))))
(goto-char saved-point)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment