Over a million developers have joined DZone.

Emacs Lisp: Toggle Between a Clojure String and Keyword

DZone's Guide to

Emacs Lisp: Toggle Between a Clojure String and Keyword

· Java Zone ·
Free Resource

Take 60 minutes to understand the Power of the Actor Model with "Designing Reactive Systems: The Role Of Actors In Distributed Architecture". Brought to you in partnership with Lightbend.

When I was doing a fair bit of Ruby I often used the TextMate's shortcut (Ctrl+:) to convert a Ruby String to a Symbol or a Ruby Symbol to a String. It's something I've periodically missed while doing Clojure, and yesterday I found myself in the middle of a refactoring that was going to force the conversion of 5+ Clojure Keywords to Strings.

The following emacs lisp is my solution for toggling between Clojure Strings and Keywords. The standard disclaimers apply - it works on my machine, and I've never claimed to know emacs lisp well.

(defun char-at-point ()
(buffer-substring-no-properties (point) (+ 1 (point))))
(defun clj-string-name (s)
(substring s 1 -1))
(defun clj-keyword-name (s)
(substring s 1))
(defun delete-and-extract-sexp ()
(let* ((begin (point)))
(let* ((result (buffer-substring-no-properties begin (point))))
(delete-region begin (point))
(defun toggle-clj-keyword-string ()
(if (equal 1 (point))
(message "beginning of file reached, this was probably a mistake.")
(cond ((equal "\"" (char-at-point))
(insert ":" (clj-string-name (delete-and-extract-sexp))))
((equal ":" (char-at-point))
(insert "\"" (clj-keyword-name (delete-and-extract-sexp)) "\""))
(t (progn
(global-set-key (kbd "C-:") 'toggle-clj-keyword-string)

A quick video of the behaviour:

Learn how the Actor model provides a simple but powerful way to design and implement reactive applications that can distribute work across clusters of cores and servers. Brought to you in partnership with Lightbend.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}