From 48238686d99676b93bcdfa124f499eb13b0edca6 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Wed, 27 Aug 2014 11:20:17 +0200 Subject: Add a useful function --- emacs/.emacs.d/init.org | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'emacs/.emacs.d/init.org') diff --git a/emacs/.emacs.d/init.org b/emacs/.emacs.d/init.org index b57ee51..3b35d0a 100644 --- a/emacs/.emacs.d/init.org +++ b/emacs/.emacs.d/init.org @@ -896,6 +896,53 @@ dired-subdir-switches "-Alh")) #+END_SRC +* Useful functions + + During your editing in Emacs you will undoubtedly find the need to + define your own editing functions or macros. Here are mine. + +** Delete the contents of the string at point + + First we define the function. It was inspired by [[http://www.masteringemacs.org/][Mickey's post on + swapping quote symbols]], mostly copied even. First we check if we + are even in a string, and if not we throw an error, after that we + move back to the beginning of the string, store that point, go to + the end of the string (using =forward-sexp=) and then delete the + region between the two points (non-inclusive). + + #+BEGIN_SRC emacs-lisp + (defun oni:delete-string-contents () + (interactive) + + (unless (in-string-p) + (error "You must be in a string for this command to work")) + + (save-excursion + (while (in-string-p) (forward-char -1)) + + (let ((bos (point))) + (forward-sexp) + (delete-region (1+ bos) (1- (point)))))) + #+END_SRC + + Since for interactive functions it's kind of a pain to have to use + a personal "namespace" I prefer naming them regularly as if they're + just part of the environment. If ever Emacs comes up with a similar + function with the same name, I'd prefer using the built-in version. + + #+BEGIN_SRC emacs-lisp + (unless (fboundp 'delete-string-contents) + (defalias 'delete-string-contents 'oni:delete-string-contents)) + #+END_SRC + + Lastly, any function worth using often should probably be easily + accessible with a keybinding. In my case the {{{key(C-c i s)}}} is + inspired by the Vim keybindings like {{{key(ci")}}}. + + #+BEGIN_SRC emacs-lisp + (global-set-key (kbd "C-c i s") 'delete-string-contents) + #+END_SRC + * Load custom file I don't really use the Emacs customization interface much, but I -- cgit v1.2.3-54-g00ecf