My ~/.emacs.d/init.el
file looks like this:
(package-initialize)
(require 'init-my-emacs-settings)
(require 'init-my-emacs-performance)
(require 'init-my-emacs-font)
(require 'init-my-emacs-color-theme)
(require 'init-my-emacs-appearance)
(require 'init-my-tool-calendar)
(require 'init-my-tool-dictionary)
(require 'init-my-tool-clock)
(require 'init-my-tool-speak)
(require 'init-my-prog-programming)
(require 'init-my-prog-license)
(require 'init-my-prog-code)
(require 'init-my-prog-comment)
;;; Programming Languages
(require 'init-my-prog-lang-lisp)
(require 'init-my-prog-lang-emacs-lisp)
(require 'init-my-prog-lang-common-lisp)
(require 'init-my-prog-lang-clojure)
(require 'init-my-prog-lang-ruby)
(require 'init-my-prog-lang-python)
I hope to load them only when needed. If for init files like
init-my-emacs-settings
can’t be lazy load. Then I hope lazy load for programming
languages init files like init-my-prog-lang-lisp
.
Here is my simple idea to load programming language init files only when load major-mode hook.
;;; TODO: convert code into an macro.
(defvar init-my-prog-lang-lisp-loaded nil)
(add-hook 'lisp-mode-hook
(lambda ()
(unless init-my-prog-lang-lisp-loaded
(require 'init-my-prog-lang-lisp)
(setq init-my-prog-lang-lisp-loaded t))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun symbol-concat (symbol string)
(intern (concat (symbol-name symbol) string)))
;; (type-of (symbol-concat 'ruby-mode-hook "-p"))
(defmacro my-lazy-load-init-file (init-file mode)
"Lazy load my `INIT-FILE' only when `MODE' loaded."
(let ((state (symbol-concat init-file "-loaded")))
;; state variable
(defvar state nil)
;; load on major-mode hook
(add-hook (symbol-concat mode "-hook")
(lambda ()
(unless state
(require init-file)
(setq state t)
)))
)
)
My questions: - how to improve this macro? - Do you have better solution? If have, please show me. Thanks