1

As I move all my emacs.d between my machines using Git, and, obviously, I don’t version control byte-compiled files (*.elc), I sometimes get into a situation when my .el files are newer then the corresponding .elc files.

Right now I construct a find(1) command using find-cmd, to find all .elc files, check if they are older then their .el counterpart, and byte-compile them if so.

(defun recompile-stale-elcs ()
  (interactive)

  (with-temp-buffer
    (setq-local default-directory user-emacs-directory)

    (let ((find-command (find-cmd '(prune (name ".git"))
                                  '(name "*.elc"))))

      (shell-command find-command t t))

    (goto-char (point-min))
    (setq more-lines t)

    (while more-lines
      (let ((start (progn (beginning-of-line)
                          (point)))
            (end (progn (end-of-line)
                        (point))))
        (let ((el (buffer-substring start (- end 1)))
              (elc (buffer-substring start end)))

          (if (file-newer-than-file-p el elc)
              (byte-compile-file (buffer-substring start (- end 1))))))
      (setq more-lines (= 0 (forward-line 1))))))

Is there a way to change that find-cmd call to pure Elisp?

Of course, if there is any other solution for the base problem, I’m open to hear that (probably in comment/chat.)

(Also, I was in doubt when selecting tags for this question. Please add some more relevant ones if such exist.)

Drew
  • 75,699
  • 9
  • 109
  • 225
GergelyPolonkai
  • 748
  • 5
  • 12
  • 1
    `byte-recompile-directory` may save you some trouble. – T. Verron Oct 13 '16 at 15:17
  • Am i right to assume it is recursive? The problem usually comes up with ELPA/MELPA packages, of which all has a separate directory. – GergelyPolonkai Oct 13 '16 at 15:27
  • The doc says that it is. – T. Verron Oct 13 '16 at 15:51
  • @T.Verron The docstring says subdirectories are processed but does not say *explicitely* this applies recursively. Looking at the code, it seems it does, though. – JeanPierre Oct 13 '16 at 15:51
  • @JeanPierre What else could it mean? – T. Verron Oct 13 '16 at 15:54
  • @T.Verron Well, that subdirectories of the provided directory are scanned, but not their own subdirectories. I agree this would be unexpected but I would be more comfortable with the docstring using the word "recursively". – JeanPierre Oct 13 '16 at 16:02
  • Have you looked at `auto-compile`? Also, you should avoid compiling your init file. – Tianxiang Xiong Oct 13 '16 at 16:32
  • I don't compile my `init.el`, but `packages` compiles a lot of files (obviously). I sometimes get errors due to this, so I'd like to recompile those files. – GergelyPolonkai Oct 13 '16 at 16:35
  • @JeanPierre Agreed. `:)` – T. Verron Oct 13 '16 at 16:35
  • @GergelyPolonkai You don't need to carry your packages with you, though. You could try `cask` or `use-package` for packages available on the repos, and `el-get` for packages which aren't (non-exhaustive list of options). – T. Verron Oct 13 '16 at 16:37
  • I'm currently experimenting with `use-package`. I don't feel it home ground yet, hence I still carry this burden for a while :) – GergelyPolonkai Oct 13 '16 at 16:46
  • 2
    [Can I avoid outdated byte-compiled elisp files?](http://emacs.stackexchange.com/questions/185/can-i-avoid-outdated-byte-compiled-elisp-files) doesn't answer the specific question about an elisp equivalent of `find`, but I think it answers your use case of keeping up-to-date `.elc` files. – Dan Oct 13 '16 at 16:56
  • Yes, I already switched my own function to `byte-recompile-directory`; yet, I'm curious of an answer for the question for other use cases. – GergelyPolonkai Oct 13 '16 at 17:01
  • Pure Emacs Lisp solution - I don't know. But, in general, this is a task for a build system (of which there are many). All of them have their flaws, but just for the sake of example: you could have SCons track the state of your sources / binaries and recompile them if needed. With some work you could even make it smart enough to figure if the change requires recompilation (eg. if a comment or a docstring were changed - no compilation is needed). – wvxvw Oct 13 '16 at 20:25
  • Yes, `byte-recompile-directory` is *all you need*. And yes, it is recursive. And yes, it won't compile any file that has not yet been compiled (for which there is no `*.elc`). – Drew Oct 13 '16 at 20:53
  • Why is it "obvious" that you don't version-control your byte-compiled files? If you use the same version of Emacs (or newer) as the version which compiled the files, then your `.elc` files are portable, unless you have added some compile-time behaviours which are system-specific. – phils Oct 14 '16 at 00:04
  • Unfortunately, I can’t. I use 25.1 at home, but I can’t do the same on my office machine. And I usually update/byte-compile at home. – GergelyPolonkai Oct 14 '16 at 08:40
  • @wvxvw I’m not necessarily speaking of this scenario. There are cases when I need a bunch of file names I would normally find with `find(1)`. And in most of those cases things like Projectile is an overkill (or doesn’t even work). – GergelyPolonkai Oct 14 '16 at 08:41

0 Answers0