summaryrefslogtreecommitdiffstats
path: root/emacs
diff options
context:
space:
mode:
authorGravatar Tom Willemse2014-08-27 11:20:17 +0200
committerGravatar Tom Willemse2014-08-27 11:20:17 +0200
commit48238686d99676b93bcdfa124f499eb13b0edca6 (patch)
tree3aa11f69e231730ff9e02ab4b993247b1fc8d4b4 /emacs
parentb9f699ad07d437fb41851f0c1a7788c082d028d5 (diff)
downloaddotfiles-48238686d99676b93bcdfa124f499eb13b0edca6.tar.gz
dotfiles-48238686d99676b93bcdfa124f499eb13b0edca6.zip
Add a useful function
Diffstat (limited to 'emacs')
-rw-r--r--emacs/.emacs.d/init.org47
1 files changed, 47 insertions, 0 deletions
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