| 1 | ;;; -*- coding: utf-8; mode: emacs-lisp; -*- |
|---|
| 2 | ;;; init-loader.el --- |
|---|
| 3 | |
|---|
| 4 | ;; Author: IMAKADO <ken.imakado@gmail.com> |
|---|
| 5 | ;; Author's blog: http://d.hatena.ne.jp/IMAKADO (japanese) |
|---|
| 6 | ;; Prefix: init-loader- |
|---|
| 7 | |
|---|
| 8 | ;; This file is free software; you can redistribute it and/or modify |
|---|
| 9 | ;; it under the terms of the GNU General Public License as published by |
|---|
| 10 | ;; the Free Software Foundation; either version 2, or (at your option) |
|---|
| 11 | ;; any later version. |
|---|
| 12 | |
|---|
| 13 | ;; This file is distributed in the hope that it will be useful, |
|---|
| 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | ;; GNU General Public License for more details. |
|---|
| 17 | |
|---|
| 18 | ;; You should have received a copy of the GNU General Public License |
|---|
| 19 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
|---|
| 20 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|---|
| 21 | ;; Boston, MA 02110-1301, USA. |
|---|
| 22 | |
|---|
| 23 | ;;; 使い方 |
|---|
| 24 | ;; load-pathの通った場所に置いて |
|---|
| 25 | ;; (require 'init-loader) |
|---|
| 26 | ;; (init-loader-load "/path/to/init-directory") |
|---|
| 27 | |
|---|
| 28 | ;; デフォルト設定の場合,以下の順序で引数に渡したディレクトリ以下のファイルをロードする. |
|---|
| 29 | ;; 引数が省略された場合は,変数`init-loader-directory'の値を使用する.デフォルトは"~/.emacs.d/inits". |
|---|
| 30 | |
|---|
| 31 | ;; 1. ソートされた,二桁の数字から始まるファイル. e.x, "00_utils.el" "01_ik-cmd.el" "21_javascript.el" "99_global-keys.el" |
|---|
| 32 | ;; 2. meadowの場合, meadow から始まる名前のファイル. e.x, "meadow-cmd.el" "meadow-config.el" |
|---|
| 33 | ;; 3. carbon-emacsの場合, carbon-emacs から始まる名前のファイル. e.x, "carbon-emacs-config.el" "carbon-emacs-migemo.el" |
|---|
| 34 | ;; 4. windowシステム以外の場合(terminal), nw から始まる名前のファイル e.x, "nw-config.el" |
|---|
| 35 | |
|---|
| 36 | ;; ファイルロード後,変数`init-loader-show-log-after-init'の値がnon-nilなら,ログバッファを表示する関数を`after-init-hook'へ追加する. |
|---|
| 37 | ;; ログの表示は, M-x init-loader-show-log でも可能. |
|---|
| 38 | |
|---|
| 39 | (eval-when-compile (require 'cl)) |
|---|
| 40 | (require 'benchmark) |
|---|
| 41 | |
|---|
| 42 | ;;; customize-variables |
|---|
| 43 | (defgroup init-loader nil |
|---|
| 44 | "init loader" |
|---|
| 45 | :group 'init-loader) |
|---|
| 46 | |
|---|
| 47 | (defcustom init-loader-directory (expand-file-name "~/.emacs.d/inits") |
|---|
| 48 | "inits directory" |
|---|
| 49 | :type 'directory |
|---|
| 50 | :group 'init-loader) |
|---|
| 51 | |
|---|
| 52 | (defcustom init-loader-show-log-after-init t |
|---|
| 53 | "non-nilだと起動時にログバッファを表示する" |
|---|
| 54 | :type 'boolean |
|---|
| 55 | :group 'init-loader) |
|---|
| 56 | |
|---|
| 57 | (defcustom init-loader-default-regexp "\\(?:^[[:digit:]]\\{2\\}\\)" |
|---|
| 58 | "起動時に読み込まれる設定ファイルにマッチする正規表現. |
|---|
| 59 | デフォルトは二桁の数字から始まるファイルにマッチする正規表現. |
|---|
| 60 | e.x, 00_hoge.el, 01_huga.el ... 99_keybind.el" |
|---|
| 61 | :type 'regexp |
|---|
| 62 | :group 'init-loader ) |
|---|
| 63 | |
|---|
| 64 | (defcustom init-loader-meadow-regexp "^meadow-" |
|---|
| 65 | "meadow 使用時に読み込まれる設定ファイルにマッチする正規表現" |
|---|
| 66 | :group 'init-loader |
|---|
| 67 | :type 'regexp) |
|---|
| 68 | |
|---|
| 69 | (defcustom init-loader-carbon-emacs-regexp "^carbon-emacs-" |
|---|
| 70 | "carbon-emacs 使用時に読み込まれる設定ファイルにマッチする正規表現" |
|---|
| 71 | :group 'init-loader |
|---|
| 72 | :type 'regexp) |
|---|
| 73 | |
|---|
| 74 | (defcustom init-loader-cocoa-emacs-regexp "^cocoa-emacs-" |
|---|
| 75 | "cocoa-emacs 使用時に読み込まれる設定ファイルにマッチする正規表現" |
|---|
| 76 | :group 'init-loader |
|---|
| 77 | :type 'regexp) |
|---|
| 78 | |
|---|
| 79 | (defcustom init-loader-nw-regexp "^nw-" |
|---|
| 80 | "no-window環境での起動時に読み込まれる設定ファイルにマッチする正規表現" |
|---|
| 81 | :group 'init-loader |
|---|
| 82 | :type 'regexp) |
|---|
| 83 | |
|---|
| 84 | ;;; Code |
|---|
| 85 | (defun* init-loader-load (&optional (init-dir init-loader-directory)) |
|---|
| 86 | (let ((init-dir (init-loader-follow-symlink init-dir))) |
|---|
| 87 | (assert (and (stringp init-dir) (file-directory-p init-dir))) |
|---|
| 88 | (init-loader-re-load init-loader-default-regexp init-dir t) |
|---|
| 89 | ;; meadow |
|---|
| 90 | (and (featurep 'meadow) |
|---|
| 91 | (init-loader-re-load init-loader-meadow-regexp init-dir)) |
|---|
| 92 | ;; carbon emacs |
|---|
| 93 | (and (featurep 'carbon-emacs-package) |
|---|
| 94 | (init-loader-re-load init-loader-carbon-emacs-regexp init-dir)) |
|---|
| 95 | ;; cocoa emacs |
|---|
| 96 | (and (equal window-system 'ns) |
|---|
| 97 | (init-loader-re-load init-loader-cocoa-emacs-regexp init-dir)) |
|---|
| 98 | ;; no window |
|---|
| 99 | (and (null window-system) |
|---|
| 100 | (init-loader-re-load init-loader-nw-regexp init-dir)) |
|---|
| 101 | |
|---|
| 102 | (when init-loader-show-log-after-init |
|---|
| 103 | (add-hook 'after-init-hook 'init-loader-show-log)))) |
|---|
| 104 | |
|---|
| 105 | (defun init-loader-follow-symlink (dir) |
|---|
| 106 | (cond ((file-symlink-p dir) |
|---|
| 107 | (expand-file-name (file-symlink-p dir))) |
|---|
| 108 | (t (expand-file-name dir)))) |
|---|
| 109 | |
|---|
| 110 | (lexical-let (logs) |
|---|
| 111 | (defun init-loader-log (&optional s) |
|---|
| 112 | (if s (and (stringp s) (push s logs)) (mapconcat 'identity (reverse logs) "\n")))) |
|---|
| 113 | |
|---|
| 114 | (lexical-let (err-logs) |
|---|
| 115 | (defun init-loader-error-log (&optional s) |
|---|
| 116 | (if s (and (stringp s) (push s err-logs)) (mapconcat 'identity (reverse err-logs) "\n")))) |
|---|
| 117 | |
|---|
| 118 | (defun init-loader-re-load (re dir &optional sort) |
|---|
| 119 | (let ((load-path (cons dir load-path))) |
|---|
| 120 | (dolist (el (init-loader--re-load-files re dir sort)) |
|---|
| 121 | (condition-case e |
|---|
| 122 | (let ((time (car (benchmark-run (load (file-name-sans-extension el)))))) |
|---|
| 123 | (init-loader-log (format "loaded %s. %s" (locate-library el) time))) |
|---|
| 124 | (error |
|---|
| 125 | (init-loader-error-log (error-message-string e))))))) |
|---|
| 126 | |
|---|
| 127 | (defun init-loader--re-load-files (re dir &optional sort) |
|---|
| 128 | (loop for el in (directory-files dir t) |
|---|
| 129 | when (string-match re (file-name-nondirectory el)) |
|---|
| 130 | collect (file-name-nondirectory el) into ret |
|---|
| 131 | finally return (if sort (sort ret 'string<) ret))) |
|---|
| 132 | |
|---|
| 133 | (defun init-loader-show-log () |
|---|
| 134 | "return buffer" |
|---|
| 135 | (interactive) |
|---|
| 136 | (let ((b (get-buffer-create "*init log*"))) |
|---|
| 137 | (with-current-buffer b |
|---|
| 138 | (erase-buffer) |
|---|
| 139 | (insert "------- error log -------\n\n" |
|---|
| 140 | (init-loader-error-log) |
|---|
| 141 | "\n\n") |
|---|
| 142 | (insert "------- init log -------\n\n" |
|---|
| 143 | (init-loader-log) |
|---|
| 144 | "\n\n") |
|---|
| 145 | ;; load-path |
|---|
| 146 | (insert "------- load path -------\n\n" |
|---|
| 147 | (mapconcat 'identity load-path "\n")) |
|---|
| 148 | (goto-char (point-min))) |
|---|
| 149 | (switch-to-buffer b))) |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | ;;;; Test |
|---|
| 153 | (defvar init-loader-test-files |
|---|
| 154 | '("00_utils.el" |
|---|
| 155 | "23_yaml.el" |
|---|
| 156 | "01_ik-cmd.el" |
|---|
| 157 | "96_color.el" |
|---|
| 158 | "20_elisp.el" |
|---|
| 159 | "21_javascript.el" |
|---|
| 160 | "25_perl.el" |
|---|
| 161 | "98_emacs-config.el" |
|---|
| 162 | "99_global-keys.el" |
|---|
| 163 | "carbon-emacs-config.el" |
|---|
| 164 | "carbon-emacs-migemo.el" |
|---|
| 165 | "nw-config.el" |
|---|
| 166 | "emacs-migemo.el" |
|---|
| 167 | "meadow-cmd.el" |
|---|
| 168 | "meadow-config.el" |
|---|
| 169 | "meadow-gnuserv.el" |
|---|
| 170 | "meadow-shell.el" |
|---|
| 171 | "meadow-w32-symlinks.el")) |
|---|
| 172 | |
|---|
| 173 | (dont-compile |
|---|
| 174 | (when (fboundp 'expectations) |
|---|
| 175 | (expectations |
|---|
| 176 | (desc "init-loader--re-load-files") |
|---|
| 177 | (expect '("00_utils.el" "01_ik-cmd.el" "20_elisp.el" "21_javascript.el" "23_yaml.el" "25_perl.el" "96_color.el" "98_emacs-config.el" "99_global-keys.el") |
|---|
| 178 | (stub directory-files => init-loader-test-files) |
|---|
| 179 | (init-loader--re-load-files init-loader-default-regexp "" t)) |
|---|
| 180 | (expect '("meadow-cmd.el" "meadow-config.el" "meadow-gnuserv.el" "meadow-shell.el" "meadow-w32-symlinks.el") |
|---|
| 181 | (stub directory-files => init-loader-test-files) |
|---|
| 182 | (init-loader--re-load-files init-loader-meadow-regexp "" t)) |
|---|
| 183 | |
|---|
| 184 | (expect '("carbon-emacs-config.el" "carbon-emacs-migemo.el") |
|---|
| 185 | (stub directory-files => init-loader-test-files) |
|---|
| 186 | (init-loader--re-load-files init-loader-carbon-emacs-regexp "" t)) |
|---|
| 187 | (expect '("nw-config.el") |
|---|
| 188 | (stub directory-files => init-loader-test-files) |
|---|
| 189 | (init-loader--re-load-files init-loader-nw-regexp "" t)) |
|---|
| 190 | ;; 環境依存 |
|---|
| 191 | (desc "follow symlink") |
|---|
| 192 | (expect "c/.emacs.d/inits" |
|---|
| 193 | (file-relative-name (file-symlink-p "~/tmp/el-inits"))) ; symlink |
|---|
| 194 | (desc "init-loader-follow-symlink") |
|---|
| 195 | (expect "c/.emacs.d/inits" |
|---|
| 196 | (file-relative-name (init-loader-follow-symlink "~/tmp/el-inits"))) |
|---|
| 197 | (expect "c/.emacs.d/inits" |
|---|
| 198 | (file-relative-name (init-loader-follow-symlink "~/tmp/el-inits"))) |
|---|
| 199 | ))) |
|---|
| 200 | |
|---|
| 201 | (provide 'init-loader) |
|---|
| 202 | |
|---|