defaultcontent.el - @@LISP tag - read-closest-sexp

2019-08-18 19:08发布

问题:

I started using defaultcontent.el to fill newly-created buffers with content. Apparently this module is not widely used. I think there are 3 people including me and the author who use it, because when I do a search on it, my published emacs.el comes up as the first hit.

Despite that, I find it useful. I specify a template for each file type, and every time I create a new file of that type (or extension), it gets filled with the content in the template file. The template supports well-known tags set off with "@@", that get substituted at runtime:

  • AUTHOR inserts the user name;
  • DATE (obvious);
  • FILENAME, inserts the name of the file being created;
  • ENV(xxx), inserts the value of the environment variable xxx;
  • and there are a few other tags.

eg, whereever @@AUTHOR@@ is found in the template, it gets replaced with your user name at runtime in the newly created file.

ok, this isn't an advertisement for defaultcontent.el, I just thought I'd explain it a little.


here's the question.

One of the well-known tags in the template is LISP - it purports to run arbitrary elisp code to generate content to insert into the new buffer. (usage: @@LISP(lisp content here)@@). It depends on a function read-closest-sexp, which I guess would just read the sexp at point.

I can't find this function. It's not included in defaultcontent.el, and I'm not up enough on elisp to create it easily. I looked in emacs-lisp\lisp.el for hints but it seemed non obvious.

Question: how can I read the sexp at point into a variable?

I'm guessing this is 2 lines in elisp...

回答1:

Try thing-at-point:

(require 'thingatpt)
(let ((sexp (thing-at-point 'sexp)))
  (do-something-with sexp))

Indeed two lines if you ignore the do-something :)



标签: emacs elisp