Search code examples
formselisp

Pass functor to compile-command


I've created my own elisp function which calculates the compile-command string each time I want to compile a whole project (which has usually a makefile in the root of the project's directory tree) or a single file (which usually has no makefile and uses the make's implicit rules).

It allows me to find automatically the location of a recently created, renamed or moved makefile in the directory tree of a project, without having to close and reopen the editor to reload the file-local variables at the beginning of a project.

The Emacs' compile function implementation uses the expression (let ((command (eval compile-command))) body) in case compile-command is bound to a form (for instance, compilation-read-command, as stated in the compile-command documentation).

I want to take adventage of this to get a dynamic compile-command string, but I don't know how to create those "forms" encapsulating a function call, as required by eval. All the examples I've found in the emacs-lisp documentation about eval use simple variables, not function captures.

I've tried with something simple as (setq-default compile-command 'my-compile-command-fun), but of course it didn't work.

my-compile-command-fun has no parameters.


Solution

  • ELISP> (defun foo () "bar")
    foo
    
    ELISP> (foo)
    "bar"
    
    ELISP> (eval '(foo))
    "bar"
    

    Therefore you presumably wanted this:

    (setq-default compile-command '(my-compile-command-fun))