SHOW:
|
|
- or go back to the newest paste.
1 | (defmacro util:aif (pred then &rest else) | |
2 | "PRED を評価し、結果が non-nil ならば THEN、nil ならば ELSE の評価結果を返す. | |
3 | THEN、ELSE 内では PRED の評価結果を `it' で参照出来る." | |
4 | `(let ((it ,pred)) | |
5 | (if it ,then ,@else))) | |
6 | ||
7 | (defmacro util:awhen (pred &rest body) | |
8 | "PRED を評価し、結果が non-nil ならば BODY を評価し、最後の式の結果を返す. | |
9 | BODY 内では PRED の評価結果を `it' で参照出来る." | |
10 | - | `(ini:aif ,pred (progn ,@body))) |
10 | + | `(util:aif ,pred (progn ,@body))) |
11 | ||
12 | (defvar scratch-save-file (locate-user-emacs-file "scratch") | |
13 | "`*scratch' バッファの保存先ファイル名.") | |
14 | ||
15 | (defvar scratch-buffer-save-interval 1 | |
16 | "`*scratch*' バッファの自動保存間隔.") | |
17 | ||
18 | (defvar prev-scratch-modified-tick 0 | |
19 | "`*scratch*' バッファの前回保存時の更新状態.") | |
20 | ||
21 | (defun resume-scratch-buffer () | |
22 | "`*scratch*' バッファの内容を復帰する." | |
23 | (interactive) | |
24 | (let ((scratch (get-buffer-create "*scratch*")) | |
25 | (file (expand-file-name scratch-save-file)) | |
26 | (buffer-undo-list t)) | |
27 | (with-current-buffer scratch | |
28 | (when (file-exists-p file) | |
29 | (erase-buffer) | |
30 | (insert-file-contents file) | |
31 | (set-buffer-modified-p nil) | |
32 | ||
33 | (setq scratch-modified-tick (buffer-chars-modified-tick)) | |
34 | )))) | |
35 | ||
36 | (defun save-scratch-buffer () | |
37 | "`*scratch*' バッファの内容を保存する." | |
38 | (interactive) | |
39 | (util:awhen (get-buffer "*scratch*") | |
40 | (with-current-buffer it | |
41 | (let ((modified-tick (buffer-chars-modified-tick))) | |
42 | (unless (eq modified-tick prev-scratch-modified-tick) | |
43 | (setq prev-scratch-modified-tick modified-tick) | |
44 | (save-restriction | |
45 | (widen) | |
46 | (write-region (point-min) (point-max) | |
47 | (expand-file-name scratch-save-file) | |
48 | nil 'slient) | |
49 | )))))) | |
50 | ||
51 | (add-hook 'after-init-hook | |
52 | (lambda () | |
53 | (resume-scratch-buffer) | |
54 | ||
55 | ;; 読み込みに成功したら自動保存を有効化 | |
56 | (run-with-idle-timer scratch-buffer-save-interval t 'save-scratch-buffer) | |
57 | (add-hook 'kill-emacs-hook 'save-scratch-buffer) | |
58 | )) |