Modifying the reader would allow introducing new read-syntax (such as #(hash table)
and '(quoted)
). Many Lisps have this capability, but no such facility seems to exist for elisp.
-
Hack the C sources.– wasamasaCommented Nov 11, 2015 at 15:12
-
3Emacs Lisp doesn't have a Lisp-programmable reader.– DrewCommented Nov 11, 2015 at 15:39
-
@wasamasa Well, that's always an option :) Not a portable one, but an option.– Sean AllredCommented Nov 11, 2015 at 15:58
-
I think "Many Lisps have this." is not an actual question. I would much prefer to have a real question. I suggest to shorten the title, and rephrase it into the body.– YoungFrogCommented Mar 10, 2016 at 17:25
-
@YoungFrog edits welcome. I couldn't think of anything more direct or appropriate without being overly wordy. The question is simple; it doesn't have to be long.– Sean AllredCommented Mar 10, 2016 at 17:27
2 Answers
Turns out that the manual implies that you can't actually do reader macros.
According to Appendix C Porting Common Lisp:
Reader macros. Common Lisp includes a second type of macro that works at the level of individual characters. For example, Common Lisp implements the quote notation by a reader macro called ', whereas Emacs Lisp’s parser just treats quote as a special case. Some Lisp packages use reader macros to create special syntaxes for themselves, which the Emacs parser is incapable of reading.
-
I think this is about Common Lisp software which use special reader macros, rather than Elisp. The page is about porting CL code to Elisp. And yes, officially the reader is not customizable, but see my answer below...– mishooCommented Mar 7, 2016 at 16:23
-
The other problem here is even if you've somehow introduced new reader syntax, the rest of Emacs is not aware of it, so you'll need to fix
syntax.c
, too...– wasamasaCommented Mar 8, 2016 at 7:23
Apparently it can be done, but prepare for a lot of work. (or don't, because I'm gonna tackle it some weekend ;-).
(defvar *orig-read* (symbol-function 'read))
(defun read (&optional in)
(message "reading from %s" load-file-name)
(funcall *orig-read* in))
(setq load-read-function (symbol-function 'read))
Now "all" that's left to do is to implement a complete Lisp reader which supports everything that Elisp does and whatever you want more. I think this could be used to provide CL-like defpackage
and package internal symbols, as one example. Also, a special syntax for regexps is something I badly want to do (or more exactly, some string syntax that doesn't interpret backslashes).
Edit: here's a proof-of-concept implementation: https://github.com/mishoo/elisp-reader.el
-
Probably best not to post this as an answer until after you've actually implemented a complete Lisp reader as you suggested (which, I'm guessing, is not a trivial enterprise).– Dan ♦Commented Mar 7, 2016 at 19:51
-
@Dan It is doable, it's just that doing it in C is so much more painful as it sucks for string processing.– wasamasaCommented Mar 8, 2016 at 7:21
-
3@Dan got it working, I edited the answer to add the link to the implementation.– mishooCommented Mar 10, 2016 at 17:05
-
Can you elaborate why you need to both
setq load-read-function
anddefun read
?– mtraceurCommented Aug 11 at 21:40