seperate configure file and auto compile them

As our Emacs configure file gettting larger and larger (my config files are now about 250K), it is a good idea to seperate them into different categories and byte-compile them for speed up.

However, it is boring to manually byte-compile them whenever I make a change, even with dired. So here is my solution.

  • The function to rebuild .emacs if the source has changed I believe I got this somewhere on the Internet and hacked on. Unfortunately I can’t find the original source.
(defun EmacsWait! ()
"If ~/EmacsConfig/.emacs.el exists and is newer than ~/.emacs,
recompile it to ~/EmacsConfig/.emacs.elc and move the compiled
version to ~/.emacs. And then reload it."
(cond
((file-newer-than-file-p "~/EmacsConfig/emacs.el" "~/.emacs")
(let ((mode-line-format
"*** PLEASE STANDBY: RECOMPILING .emacs.el **")
(byte-compile-warnings t))
(yow)
(sit-for 1)
(byte-compile-file "~/EmacsConfig/emacs.el")
(message ".emacs recompiled — reloading …")
(rename-file "~/EmacsConfig/emacs.elc" "~/.emacs" t))
(load "~/.emacs" t t t)
;;  (sit-for 2)
(message "emacs.el recompiled")
)))
(EmacsWait!)

This first define the function that do the thing and then calls it. The call makes sure that everytime I start Emacs (load ~/.emacs), that function is called and make sure .emacs is the newest byte-compiled version.

  • And for my other config files, I have
(defun compile-and-reload-file (filename)
"compile and reload filename.el"
(let (
(el-file-name (concat filename ".el"))
(elc-file-name (concat filename ".elc")))
(let (
(mode-line-string (concat "*** PLEASE STANDBY: RECOMPILING "
el-file-name " **")))
(let (
(mode-line-format mode-line-string))))
(yow)
(sit-for 1)
(byte-compile-file el-file-name)
(message "%s recompiled — reloading …" elc-file-name)
(load elc-file-name t t t)
(message "%s recompiled" el-file-name)))

(defun ConfigFileWait! (filename)
“Check the arg FileName.el. If it’s newer the FileName.elc or
FileName.elc doesn’t exits, compile FileName.el and load
FileName.elc, Else just load filename.elc”
(let (
(el-file-name (concat filename “.el”))
(elc-file-name (concat filename “.elc”))
(byte-compile-warnings t))
(if (file-exists-p el-file-name)
(cond
((file-newer-than-file-p el-file-name elc-file-name)
(compile-and-reload-file filename))
((not (file-exists-p elc-file-name))
(compile-and-reload-file filename))
(t
(load-file elc-file-name))
))))
They said pretty well what they did in the doc string. So I can have my seperate config files loaded as

(ConfigFileWait! "~/EmacsConfig/NewPackages")

And now I can just go ahead and make changes to my config file and the next time Emacs is started, all the new config files will be byte-compiled and reloaed.

Tags: ,

Post a Comment

You could use <code type="name"> to get your code colorized

Your email is never published nor shared. Required fields are marked *

Close
E-mail It