0

I have just (yesterday) installed emacs and am in the process of learning the very basics of its usage (I have never used emacs before, but I am maybe on the way to switch from SciTE and lua to emacs and elisp).

In between I mean to have already gained some basic understanding how emacs works under the hood and if I got it right there must be a lisp function there which is taking all keystrokes from user in order to dispatch them to other functions if the keystrokes are representing a keyboard shortcut or to the buffer if not, but was not able to find any information which one it is yet.

Is my understanding of how emacs handles user input the right one?

Or are the user input events (keystrokes, mouse action) sent by the OS to the emacs window dispatched by the emacs binary to the commands after being in a first stage internally evaluated by the binary, so there is no elisp script there which receives them all?


UPDATE responding to comment by Drew:

I have started to read the recommended documentation https://www.gnu.org/software/emacs/manual/html_node/elisp/Command-Overview.html but if I understand it right there should be somewhere an elisp function read-key-sequence so I should be able to find it using:

~ $ locate read-key-sequence

or if this fails

~ $ grep "defun read-key-sequence" $(locate -b *.el)

but both of the above commands fail to find something.

Is this a hint that the read-key-sequence is hard coded in the emacs binary and not available as elisp code which with appropriate modification would be able to trigger an own elisp script receiving all the user input as it comes in?

8
  • Do C-h f self-insert-command: that's the command that's bound to most keys. Its effect is to insert the character corresponding to that key into the buffer. The basic notion is that of a keymap: a keymap maps a key (which may actually be a key sequence) to a command, so what Emacs does is figure out what key was pressed, look it up in a keymap and call the command that corresponds to it. There are all sorts of complications, but that's a decent high level view and a good approximation to the truth.
    – NickD
    Commented Mar 15, 2023 at 20:13
  • @NickD : keymap called using C-h b is a great help to get an overview of key bindings. What I don't understand is why M-p is not mentioned in that list? Where I can find the description what M-p does? Calling C-h k -> M-p results in the response that M-p is not defined ... but I can get the last command retrieved using it, so it is defined ... Hmmm ...
    – oOosys
    Commented Mar 15, 2023 at 23:32
  • The thing is that there isn't just one keymap: different buffers may have different major modes and each major mode has its own keymap. It may be that you have a buffer in Fundamental mode e.g. and the keymap for that mode does not have a binding for M-p. But if you try to do M-x something, you end up in the minibuffer which has its own keymap and in that keymap there is a binding for M-p.
    – NickD
    Commented Mar 16, 2023 at 0:40
  • I recommend that you go through the Emacs tutorial (C-h t) and read the manual (C-h i g(emacs)) in tandem. There is no royal road to Emacs...
    – NickD
    Commented Mar 16, 2023 at 0:43
  • I am more or less through C-h t . I will check out Ch i g(emacs) ...
    – oOosys
    Commented Mar 16, 2023 at 0:46

1 Answer 1

1

You really should just ask a single question at a time, and not modify that question over time, but I want to address this part:

… but both of the above commands fail to find something.

Is this a hint that the read-key-sequence is hard coded in the emacs binary and not available as elisp code …

To find more information about a function that someone tells you about, use C-h f. It will prompt you for a function name, with autocompletion. If you put in read-key-sequence you will get a help buffer that says “read-key-sequence is a function defined in keyboard.c.”, and then goes on to describe the arguments it takes and to quote the documentation for it.

Note that the filename is always a link that you can click on to jump directly to the source code for the function; in this case it is a Lisp function which is defined directly in C. The C source code is not generally installed, so very likely this will prompt you again for the location of the source code. Just point it to a copy of the source code that you have downloaded or checked out from git.

To get back to your original question, the low–level keyboard handling is done in C, with different code for when Emacs is using a GUI toolkit like GTK or is inside of a terminal. There is a middle layer made of Lisp functions mostly written in C, like read-key-sequence and read-char, plus some Lisp variables like unread-command-events. There is a layer of Lisp code on top of that which implements Emacs’s built in Input Method Editors.

3
  • How does it work that Lisp functions are written in C, but there is no library file? Is the executable of emacs at the same time a library with methods for Lisp? Or is something wrong with my understanding how a language uses libraries written in another language?
    – oOosys
    Commented Mar 16, 2023 at 18:27
  • 1
    That should be it’s own question. In short, it is sufficient that the elisp interpreter and compiler know what methods are available and how to call them. For methods written in C, this is accomplished by recording metadata about each one, including the address of the C function. When it comes time to call one, Emacs calls it using that function pointer.
    – db48x
    Commented Mar 17, 2023 at 0:45
  • 1
    Library files are only used when a C program (or a program in a language compatible with C) wants to be able to load at run time code compiled at a different time and/or place. For example, Emacs dynamically loads libpng when it comes time to decode a PNG image file. But dynamic libraries only provide C functions, not Lisp functions.
    – db48x
    Commented Mar 17, 2023 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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