Skip to content

Instantly share code, notes, and snippets.

@redblobgames
Created December 20, 2025 18:30
Show Gist options
  • Select an option

  • Save redblobgames/398bf418ca6922a20f69e8aaf1675099 to your computer and use it in GitHub Desktop.

Select an option

Save redblobgames/398bf418ca6922a20f69e8aaf1675099 to your computer and use it in GitHub Desktop.
emacs init.el modules
;; Try to load a package, but continue if it doesn't exist.
;; This allows my emacs startup file to work on a larger variety of
;; systems and Emacs versions.
(defvar try-require--current-module "init"
"Printable module loading chain for try-require")
(defvar try-require--module-errors '()
"List of modules that failed to load")
(defmacro try-require (package &rest forms)
"Execute FORMS only if (require PACKAGE) succeeds."
(declare (indent 1))
(let ((var (make-symbol "now")))
`(when (condition-case e
(let ((,var (current-time))
(file-name-handler-alist nil))
(let ((try-require--current-module (format "%s/%s" try-require--current-module ,package)))
(require ,package))
(message "%s: loaded %s (%.3fs)"
try-require--current-module
,package
(float-time (time-subtract (current-time) ,var))))
(error
(unless (eq (car e) 'file-missing)
(message "%s: fail %s -- %S" try-require--current-module ,package e)
(add-to-list 'try-require--module-errors ,package))
nil))
,@forms)))
(try-require 'clean-directory)
(try-require 'keybindings)
(try-require 'settings)
(try-require 'decent-colors)
(try-require 'my-files)
(try-require 'my-tabbar)
(try-require 'my-modeline)
(try-require 'org-setup)
(try-require 'my-snippets)
(try-require 'my-notes)
;; User-specific customization (amitp vs root, etc)
(try-require (intern (user-login-name)))
;; Hostname-specific customization (foo.stanford.edu becomes
;; host-foo-stanford-edu.el, host-stanford-edu.el, host-edu.el)
(dolist (modulename
(cl-maplist
(lambda (list) (intern (concat "host-" (string-join list "-"))))
(split-string system-name "\\.")))
(try-require modulename))
;; Platform-specific customization (platform-darwin.el,
;; platform-gnu-linux.el, platform-windows-nt.el, etc.)
(try-require (intern (subst-char-in-string ?/ ?- (format "platform-%s" system-type))))
;; Window system customization (window-x.el or window-pgtk.el,
;; window-ns.el or window-mac.el, window-w32.el, window-nil.el for
;; terminals)
(try-require (intern (format "window-%s" (window-system))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment