Skip to content

Instantly share code, notes, and snippets.

@kiwanami
Created October 24, 2010 05:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiwanami/643125 to your computer and use it in GitHub Desktop.
Save kiwanami/643125 to your computer and use it in GitHub Desktop.
;; kill-region or backward-kill-word
(defun kill-region-or-backward-kill-word ()
(interactive)
(if (region-active-p)
(kill-region (point) (mark))
(backward-kill-word 1)))
;; swap quotes
(defvar swap-quotes-list
'((?\" ?\') (?\' ?\`) (?\` ?\")))
(defun transpose-chars-or-swap-quotes (arg)
(interactive "p")
(or (swap-quotes) (transpose-chars arg)))
(defun swap-quotes ()
(interactive)
(catch 'break
(dolist (i swap-quotes-list)
(let ((target-char (car i))
(replaced-char (cadr i))
(prev-pos (point)))
(if (= (char-after (point)) target-char)
(save-excursion
(forward-char 1)
(let ((next-pos (re-search-forward (char-to-string target-char))))
(if next-pos
(subst-char-in-region prev-pos next-pos target-char replaced-char)
(message "The corresponding quote is not found.")))
(throw 'break t)))))
nil))
(defun kill-word-or-delete-horizontal-space (arg)
(interactive "p")
(let ((pos (point)))
(if (and (not (eobp))
(= (char-syntax (char-after pos)) 32)
(= (char-syntax (char-after (1+ pos))) 32))
(prog1 (delete-horizontal-space)
(unless (memq (char-after pos) '(?( ?) ?{ ?} ?[ ?]))
(insert " ")))
(kill-word arg))))
(defun eval-last-sexp-or-region (&optional args)
(interactive "P")
(let (output-buffer)
(if (region-active-p)
(eval-region
(region-beginning) (region-end) t)
(eval-last-sexp args))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment