I thought it would be great to save a Elisp file (in my init), and it compiles automatically a byte-compile Elisp-file.

So I wrote a function for this.

    (defun remove-elc-and-compile-on-save ()
      "If you're saving an elisp file, likely the .elc is no longer valid. And automatically byte-recompile this file whenever it's saved."
      (if (file-exists-p (concat buffer-file-name "c"))
          (delete-file (concat buffer-file-name "c"))
      (byte-compile-file buffer-file-name)

    (add-hook 'emacs-lisp-mode-hook 
              (lambda () 
                (add-hook 'after-save-hook 'remove-elc-and-compile-on-save nil 'make-it-local)))  

For example, the directory structure where my Elisp files are: C:\foobar\Emacs\setup-foobar.el. (It's a Windows structure).

It works perfectly. However, my Emacs directory with the Elips-files will be spoiled with the compiled .elc-files all over the place. So I thought to move the .elc-files to a specific directory.

For example, the directory structure where my Elisp files are: C:\foobar\Emacs\setup-foobar.el. (It's a Windows structure).

Then I would like to move the .elc files to C:\foobar\Emacs\setup\setup-foobar.el. When I checked the documentation of the functions file-exists-p, delete-file, byte-compile-file, there is no optional parameter available to specify the path where the delete/compilation operations should occur. They're executed in the same directory as the buffer-file-name.

I tried another ways like:

(byte-compile-file (concat "C:/foobar/Emacs/setup/" buffer-file-name))

But it didn't worked out, and it's not applicable for the file-exists-p and delete-file functions.

So I'm wondering in which way I could specify the path where the operations should be executed, instead in the same directory as the buffer?

  • This is not an answer to your question, but, typically, the solution to this is to hide the uninteresting files rather than to move them elsewhere. A way to achieve this is typically by placing a directory local settings as described here: gnu.org/software/emacs/manual/html_node/emacs/…
    – wvxvw
    Jul 1, 2015 at 11:43
  • There is no reason to delete a *.elc file if you are going to recompile the *.el to create an updated *.elc. The new *.elc will replace the old one.
    – Drew
    Jul 1, 2015 at 14:12

1 Answer 1

  1. I agree with @wvxvw's comment: You don't need to move the *.elc files; it is enough to not look at them. ;-) You can hide them from a Dired listing, for example.

  2. But if you really want to have byte-compiling automatically put the *.elc result in a different directory then you can set variable byte-compile-dest-file-function to a function that is similar to byte-compile-dest-file but which uses a different directory. Something like this:

(setq byte-compile-dest-file-function #'my-byte-compile-dest-file)

(defvar my-bytecomp-dir "/PATH/TO/YOUR/ELC/DIRECTORY/")

(defun my-byte-compile-dest-file (filename)
  "Like `byte-compile-dest-file', but put `*.elc' in directory `my-bytecomp-dir'."
  (setq filename  (file-name-sans-versions  (byte-compiler-base-file-name filename)))
    (cond ((string-match emacs-lisp-file-regexp filename)
           (concat (substring filename 0 (match-beginning 0)) ".elc"))
          (t (concat filename ".elc"))))
  • Great Drew, your solution solved my issue. I'm aware that the .elc-files are not visible in Helm/Dired/IDO, but I prefer to seperate these concerns. I'm somewhat suprised that you know something like this. How did you figure this out, if I might ask?
    – ReneFroger
    Jul 1, 2015 at 14:43
  • 2
    I looked at the code for byte-compile-file, and saw that it calls the value of byte-compile-dest-file-function (if non-nil) to do everything. The code for my-byte-compile-file is just the code from byte-compile-file, tweaked to (a) not call byte-compile-dest-file-function (since that is already called) and to use a different directory for the result.
    – Drew
    Jul 1, 2015 at 14:50
  • Thanks for the clear explaination. It enables me to figure the things out on my own. I reallly appreciate your help in this. Thanks!
    – ReneFroger
    Jul 2, 2015 at 15:25
  • You're welcome. That's the idea behind this site: help users learn to "ask Emacs" and to help themselves.
    – Drew
    Jul 2, 2015 at 15:43

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.