10

In common-lisp we have the library cl-heredoc, is there an equivalent in EmacsLisp?

  • Emacs Lisp doesn't have reader macros, so, off the top of my head I'd say no, there isn't. But maybe if you also elaborated on what were you going to do with these there could be an alternative approach. – wvxvw Jan 31 '15 at 8:42
  • 1
    @wvxvw I can probably do what I need with a multiline quoted string, just escaping interior quotes is the only irritation. – ocodo Jan 31 '15 at 10:19
  • 3
    Well, strings are multiline in ELisp. One way to handle quotes inside quotes would be through something like (format "next word is quoted %S" "word"). – wvxvw Jan 31 '15 at 10:48
  • @wvxvw I didn't know %S did that, I will give it a try. Sounds ideal thank you! Make it an answer, and I'll accept it. – ocodo Feb 1 '15 at 0:50
  • "%S" is very similar to CL format ~s directive (i.e. prin1). So, it will print its argument in a way suitable for the reader, for strings that would be with quotes. – wvxvw Feb 1 '15 at 6:46
6

Ordinary Emacs Lisp strings are multi-line-capable. You can simply put newlines in them.

Glancing at cl-heredoc, it sounds like what you are looking for is "raw" strings. There was a proposal to add these to elisp a while back, but unfortunately it was rejected. (I hope rather than expect it could be resurrected.) Nor does elisp have the reader extensibility features needed to implement this oneself.

  • Last time the topic of user-defined reader macros came up, everyone seemed in favor. So I'm sure the only obstacle is human effort. – Malabarba Feb 1 '15 at 10:14
2

If you are configuring emacs via an init.el file you might want to consider using an init.org instead.

Then you can use constructs like the following:

#+NAME: arbitrary-text
#+begin_src xml
<some>
  <random src="xml">or whatever... includes syntax highlighting!</random>
</some>
#+end_src


#+begin_src emacs-lisp :var arb-text=arbitrary-text
(eval `(defun a-test ()
         (format "whoop there it is: %S" ,arb-text)))
#+end_src

If you look in the generated .el file you will see something like:

(let ((arb-text "escaped text here..."))
  (eval `(defun a-test ()
           (format "whoop there it is: %S" ,arb-text))))

You can put the text and the code in any order. You can add names to Org lists, tables etc.

If this is for a library where you may not want to force collaborators to also use 'literate programming' then you could put the bulk of the code into an .el file and generate an auxiliary .el file using Org -- check the generated code into revision control.

  • 3
    Maybe I'm missing something, but I don't see what init.org has to do with multi line strings. – Malabarba Jan 31 '15 at 18:14
  • 1
    @Malabarba: tl;dr: if you write your elisp in org-mode, you can do something sorta reminiscent of heredocs; otherwise, you've got to escape some stuff inside your multi-line string literals -- mostly ` and "`, iirc. – SamB Jan 31 '15 at 22:55
  • This is an interesting way around the problem, but a little too convoluted for my cases. – ocodo Feb 1 '15 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.