help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Real-life examples of lexical binding in Emacs Lisp


From: Yuri Khan
Subject: Re: Real-life examples of lexical binding in Emacs Lisp
Date: Fri, 29 May 2015 22:50:53 +0600

On Fri, May 29, 2015 at 10:21 PM, Phillip Lord
<phillip.lord@newcastle.ac.uk> wrote:

> And, yes, it's mostly about closures (from the programmers POV). But
> asking for examples of where it makes editing easier seems to not make
> sense to me.

I will give an example where I wanted closures in Emacs but they were
not yet available.

The setup is editing XHTML, using nxml-mode. What I wanted was to have
a set of key bindings that would all behave similarly: namely, to
accept a selected block, wrap it in a pair of tags and extend
selection to include the newly inserted tags. Each binding would use a
different tag. Example (brackets denote mark and point):

lorem [ipsum dolor] sit amet —[C-l b]→ lorem [<strong>ipsum
dolor</strong>] sit amet
lorem [ipsum dolor] sit amet —[C-l i]→ lorem [<em>ipsum dolor</em>] sit amet

I do not remember the exact half-solution I came up with, but it
involved a helper function that accepted a region and a tag name:

(defun yk-nxml-wrap-region (begin end tagname)
  (interactive "rs")
  …implementation omitted…)

The other trivial half would be to bind closures of this function,
with the tag name fixed to a different string for each binding:

(define-key nxml-mode-map (kbd "C-l b") (make-closure
yk-nxml-wrap-region "strong"))
(define-key nxml-mode-map (kbd "C-l i") (make-closure yk-nxml-wrap-region "em"))

But, as I said, lexical binding and therefore closures were not
available at that time. I had to make do with a macro.

(I replaced the whole solution with yasnippet and smartparens since then.)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]