Echo Area

And old is new again

9 February 2012 0:00 AM (emacs | elisp | config | coding)

Now that I've got my .xbindkeysrc pretty much the way I want it I have some time again for other things. So I thought that I should work on my .emacs.d/init.el again, since it has been a while since I cleaned it up/tweaked it.

I'm returning to a state where my emacs configuration is seperated into several (small) files that get byte compiled and loaded during startup. Of course they should only get compiled if they're not still up-to-date. The advantage this would have is that once I've byte compiled my init.el, the rest (no matter how many git pull's further) just gets automatically recompiled.

The old way I did this seems to be this:

(setq d-dir "~/.emacs.d/")
(setq d-el-files (directory-files d-dir nil "^[0-9]\\{2\\}-\.*el$"))

(byte-recompile-directory d-dir)
(while d-el-files
  (let ((my-file-name (concat d-dir (car d-el-files))))
    (if (not (file-exists-p (concat my-file-name "c")))
        (byte-compile-file my-file-name))
    (load (substring my-file-name 0 (- (length my-file-name) 3)))
    (setq d-el-files (cdr d-el-files))))

If I remember correctly: the byte-recompile-directory function only recompile files, it doesn't compile files that have already been compiled, so after recompiling everything I had to check whether or not there were any uncompiled files left. I also seem to go crazy a little bit with the substring there. And the regexp there doesn't seem quite right…

Just now I wrote (taking the previous code as an example):

(require 'bytecomp)

(setq startup-directory "~/.emacs.d/startup/")
(setq startup-files (directory-files startup-directory nil "^[^.].*\\.el$"))

(while startup-files
  (let ((filename (concat startup-directory (car startup-files))))
    (if (not (eq (byte-recompile-file filename nil 0) nil))
        (load (substring filename 0 -3))))
  (setq startup-files (cdr startup-files)))

I noticed that when I didn't require bytecomp, it didn't recognize byte-recompile-file as a function, so I need that.

I still use a separate load call to load the file, because it seems that using the LOAD parameter of the byte-recompile-file function loads the source file, not the byte compiled one. This seems strange to me, though, I should ask about that.

I check the result of byte-recompile-file against nil, which is what it should return if there are errors, so I don't try loading it when there are errors.

I also discovered that using a negative argument doesn't just work for substring's START parameter, which makes it much cleaner than the old version.

I hope it works at least as well as the old version, but I think it at least looks nicer.

Then, 2 files that I changed, one for all my autoloads and one for my auto-mode-alist variable:

(defun set-autoload (params)
  (autoload (nth 0 params) (nth 1 params) (nth 2 params) (nth 3 params)))

(mapc
 'set-autoload
 '((column-marker-1         "column-marker"      "First column marker"  t)
   (column-marker-2         "column-marker"      "Second column marker" t)
   (rainbow-delimiters-mode "rainbow-delimiters" "Shiny delimiters"     t)
   (go-mode                 "go-mode"            "Major mode for Go"    t))

Which again, in my opinion, looks much nicer than autoload calls spread all over a 500+ lines init.el. Along with:

(setq auto-mode-alist
      (append '(("\\.tpl$" . html-mode)
                ("\\.go$"  . go-mode))
              auto-mode-alist))

Which also looks better than a lot of add-to-list calls spread around.

Of course, for brevity, I didn't include all my settings, that would take up way too much space.

It'll probably get much slower again, but maybe it's worth it if I can get everything cleaned up this much. I hope I can work on this some more over the next few days. Man, lisp programming sure is fun!

No responses

Leave a Reply