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

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

RE: self-insert-command source code?


From: Doug Lewan
Subject: RE: self-insert-command source code?
Date: Thu, 4 Dec 2014 22:23:47 +0000



> -----Original Message-----
> Behalf Of Marcin Borkowski
> Sent: Thursday, 2014 December 04 16:59
> Subject: self-insert-command source code?
> 
> To my disappointment, I've just found out that self-insert-command is
> written in C and not in Elisp.  I don't speak C very well (and don't
> have the sources for Emacs installed (yet)), so could anyone shed some
> light on these two questions (just in some spare time – it's not that I
> /need/ it now).
> 
> 1. Why is that so?  Since this is an interactive function, performance
> should not be the issue.

Performance comes to mind in a big way.
Even compiled, there's an element of passing it to the lisp interpreter
and handling it as an interactive command.
There's look-up in the lisp name-space (instead of in C-space)
and loading the definition (which would require deciding 
if it's compiled or not and then doing the right thing)
Then there's a check for pre- and post-command hooks.
All that sounds pretty resource intensive to me
compared to the direct look-up and execution that could happen in C.
 
> 2. What it might look like /if/ it was written in Elisp?

Here's a quick implementation that I did.
It's not entirely optimal, but it demonstrates the basic ideas.
See info "(elisp) Command Loop Info"; info "(elisp) Input Events"
should also be informative.

    (defun my-self-insert ()
      "Insert the key sequence that invoked me."
      (interactive)
      (let ((my-keys (this-command-keys)))
        (message (select-random (list "Ouch!"
                                      "Ooh!"
                                      "Ah."
                                      "That tickles!")))
        (insert my-keys)))

M-x local-set-key <RET> a <RET> my-self-insert <RET>
should activate it in the current buffer on key "a".

I hope this helps.

-- 
,Doug
Douglas Lewan
Shubert Ticketing
(201) 489-8600 ext 224 or ext 4335

"This is a slow pup," he said continuing his ascent.

reply via email to

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