9.5 Shorthands

The symbol shorthands, sometimes known as renamed symbols, are symbolic forms found in Lisp source. They’re just like regular symbolic forms, except that when the Lisp reader encounters them, it produces symbols which have a different and usually longer print name (see Symbol Components).

It is useful to think of shorthands as abbreviating the full names of intended symbols. Despite this, do not confuse shorthands with the Abbrev system (see Abbrevs and Abbrev Expansion).

Shorthands make Emacs Lisp’s namespacing etiquette easier to work with. Since all symbols are stored in a single obarray (see Creating and Interning Symbols), programmers commonly prefix each symbol name with the name of the library where it originates. For example, the functions text-property-search-forward and text-property-search-backward both belong to the text-property-search.el library (see Loading). By properly prefixing symbol names, one effectively prevents clashes between similarly named symbols which belong to different libraries and thus do different things. However, this practice commonly originates very long symbols names, which are inconvenient to type and read after a while. Shorthands solve these issues in a clean way.

Variable: read-symbol-shorthands

This variable’s value is an alist whose elements have the form (shorthand-prefix . longhand-prefix). Each element instructs the Lisp reader to read every symbol form which starts with shorthand-prefix as if it started with longhand-prefix instead.

This variable may only be set in file-local variables (see Local Variables in Files in The GNU Emacs Manual).

Here’s an example of shorthands usage in a hypothetical string manipulating library some-nice-string-utils.el.

(defun some-nice-string-utils-split (separator s &optional omit-nulls)
  "A match-data saving variant of `split-string'."
  (save-match-data (split-string s separator omit-nulls)))

(defun some-nice-string-utils-lines (s)
  "Split string S at newline characters into a list of strings."
  (some-nice-string-utils-split "\\(\r\n\\|[\n\r]\\)" s))

As can be seen, it’s quite tedious to read or develop this code since the symbol names to type are so long. We can use shorthands to alleviate that.

(defun snu-split (separator s &optional omit-nulls)
  "A match-data saving variation on `split-string'."
  (save-match-data (split-string s separator omit-nulls)))

(defun snu-lines (s)
  "Split string S into a list of strings on newline characters."
  (snu-split "\\(\r\n\\|[\n\r]\\)" s))

;; Local Variables:
;; read-symbol-shorthands: (("snu-" . "some-nice-string-utils-"))
;; End:

Even though the two excerpts look different, they are quite identical after the Lisp reader processes them. Both will lead to the very same symbols being interned (see Creating and Interning Symbols). Thus loading or byte-compiling any of the two files has equivalent results. The shorthands snu-split and snu-lines used in the second version are not interned in the obarray. This is easily seen by moving point to the location where the shorthands are used and waiting for ElDoc (see Local Variables in Files in The GNU Emacs Manual) to hint at the true full name of the symbol under point in the echo area.

Since read-symbol-shorthands is a file-local variable, it is possible that multiple libraries depending on some-nice-string-utils-lines.el refer to the same symbols under different shorthands, or not using shorthands at all. In the next example, the my-tricks.el library refers to the symbol some-nice-string-utils-lines using the sns- prefix instead of snu-.

(defun t-reverse-lines (s) (string-join (reverse (sns-lines s)) "\n")

;; Local Variables:
;; read-symbol-shorthands: (("t-" . "my-tricks-")
;;                          ("sns-" . "some-nice-string-utils-"))
;; End:

Note that if you have two shorthands in the same file where one is the prefix of the other, the longer shorthand will be attempted first. This happens regardless of the order you specify shorthands in the local variables section of your file.

'(
  t//foo ; reads to 'my-tricks--foo', not 'my-tricks-/foo'
  t/foo  ; reads to 'my-tricks-foo'
  )

;; Local Variables:
;; read-symbol-shorthands: (("t/" . "my-tricks-")
;;                          ("t//" . "my-tricks--"))
;; End:

9.5.1 Exceptions

There are two exceptions to rules governing Shorthand transformations:

9.5.2 Converting to/from shorthands

Shorthands are automatically expanded by the Lisp reader. If you want to apply read-symbol-shorthands to a symbol name without going through the reader, for example because the symbol comes from another buffer (e.g., the minibuffer), you can use shorthands-to-longhand. Similarly, if you are looking for a symbol in a buffer, you can consider all its possible shorthand forms with the use of shorthands-of-symbol.

Function: shorthands-to-longhand string

Return the full name (so called longhand form) of the symbol whose shorthand name is string. It always returns a string; if there is no use of any shorthand notation in string, it just returns string unchanged.

Function: shorthands-of-symbol string-or-symbol

Return the list of all the alternative ways to write the name of the specified symbol. The argument string-or-symbol can be a symbol or its name, and the return value is a list of strings. Note that the return value includes only the shorthand forms of the argument, not its longhand form, so it is common and normal for the return value to be nil.

Function: shorthands-intern string &optional obarray

Interns the string string in the obarray obarray, just like intern, except that it obeys read-symbol-shorthands and thus expands any shorthand in string if applicable before interning it. Returns the interned symbol.

Function: shorthands-intern-soft string &optional obarray

Same as shorthands-intern, except that it returns nil if there is no symbol by that name in the obarray instead of interning a new symbol.