6

I have been writing some elisp code, current i have to open the file edit quit and reload emacs seems there must be a better way ?

I know i can recompile the current file but that does not seem to make my changes active with in emacs until i restart is there something else i should be doing to make emacs use the new compiled file ?

Drew
  • 75,699
  • 9
  • 109
  • 225
Oly
  • 583
  • 1
  • 5
  • 15
  • 3
    There are several ways to (re-)evaluate Elisp, regardless of whether it lives in a buffer or file or is byte-compiled. For example, you can [repeatedly read an Elisp file](https://www.gnu.org/software/emacs/manual/html_node/elisp/Repeated-Loading.html) with `M-x load-file RET RET`. See also [how to `eval` Elisp buffers](https://www.gnu.org/software/emacs/manual/html_node/elisp/Eval.html) and the [`auto-compile`](https://github.com/emacscollective/auto-compile) package. – Basil Oct 04 '17 at 09:20
  • Excellent, make that an answer if you like and i will accept. basically eval-buffer is what i needed instead of compile. Just tried and my changes now apply when i run that. – Oly Oct 04 '17 at 10:12
  • Use `load-library`. If the file is in your `load-path` then that will automatically prefer to pick up the byte-compiled file (`*.elc`) rather than the source file (`*.el`). – Drew Oct 04 '17 at 15:25

1 Answers1

12

How do you recompile an .el source file [...]

  1. In Elisp file-visiting buffers, you can run the command M-xemacs-lisp-byte-compileRET to byte-compile the corresponding file.

  2. In Dired buffers, you can type B (dired-do-byte-compile) to byte-compile all specified Elisp files.

  3. In all buffers, you can run the command M-xbyte-compile-fileRET to byte-compile an arbitrary file prompted with completion.

[...] and make it active in my current session

  1. Similar to the aforementioned emacs-lisp-byte-compile, the command M-xemacs-lisp-byte-compile-and-loadRET will byte-compile the current file, if necessary, before loading it.

  2. Typing L (dired-do-load) in a Dired buffer will load the corresponding file.

  3. Passing a prefix argument to C-uM-xbyte-compile-fileRET will prompt for and byte-compile an arbitrary file as usual before additionally loading it.

I have been writing some elisp code, current i have to open the file edit quit and reload emacs seems there must be a better way ?

There are various alternative approaches you could take, depending on your preferred workflow:

  1. Emacs provides several commands for evaluating Elisp at varying degrees of granularity and from various sources, such as in-buffer expressions or expressions read from the minibuffer. See (emacs) Lisp Eval.

  2. You can load Elisp files/libraries via the commands load-file and load-library, as Drew mentions in a comment. See (emacs) Lisp Libraries.

See also the package auto-compile for automatically compiling Elisp files on save/load.

Edit

As Tobias correctly warns in a comment, there are many caveats associated with reevaluation of Elisp, which is not always straight-forward. Two common pitfalls are:

  1. Contrary to defun, defvar and defcustom forms do not reset their INITVALUE and STANDARD arguments, respectively, when reloaded (i.e. when they have already been defined and set). Only a call to C-M-x (eval-defun) in their vicinity will reset their value. [Since Emacs 28, C-xC-e (eval-last-sexp) handles defvar, defcustom, and defface forms in a similar way to eval-defun.]

  2. Reloading/redefining a macro will cause its new definition to be used henceforth, but all previously expanded call sites will have been expanded according to the old definition. Each call site has to be reevaluated and/or byte-compiled for the expansion to occur using the new macro definition.

Basil
  • 12,019
  • 43
  • 69
  • 1
    Re-evaluating changed lisp sources can be nifty. There is the problem with `defcustom` and with `defvar`. If you change the value in such forms this takes no direct effect if you reload. Furthermore, if you change a macro (or a `defsubst`) you need to reload all sources where this macro is used. Otherwise the old macro definition keeps in effect. Perhaps a warning about such effects is in place (even if it does not directly address the question). – Tobias Jan 20 '18 at 19:35
  • 1
    @Tobias Thanks, I've updated the answer. – Basil Jan 20 '18 at 20:15