about summary refs log tree commit diff stats
path: root/lisp
diff options
context:
space:
mode:
authorCase Duckworth2021-04-06 17:59:45 -0500
committerCase Duckworth2021-04-06 17:59:45 -0500
commitc05d1ba2c8e28f2f55f9e11bcf8e42b1ed43e539 (patch)
treed0faa720fd0c957c2597aa1d3956054d0112395f /lisp
parentRemove acdw/map (diff)
downloademacs-c05d1ba2c8e28f2f55f9e11bcf8e42b1ed43e539.tar.gz
emacs-c05d1ba2c8e28f2f55f9e11bcf8e42b1ed43e539.zip
7
Diffstat (limited to 'lisp')
-rw-r--r--lisp/acdw.el193
1 files changed, 66 insertions, 127 deletions
diff --git a/lisp/acdw.el b/lisp/acdw.el index d412b4b..69f9a7f 100644 --- a/lisp/acdw.el +++ b/lisp/acdw.el
@@ -18,139 +18,79 @@
18;; functions for me, acdw. 18;; functions for me, acdw.
19 19
20;;; Code: 20;;; Code:
21
22;; Utility constants
21 23
22;;; Utilities
23
24;;;; Determine the system I'm on
25(defconst acdw/system (pcase system-type 24(defconst acdw/system (pcase system-type
26 ('gnu/linux :home) 25 ('gnu/linux :home)
27 ((or 'msdos 'windows-nt) :work) 26 ((or 'msdos 'windows-nt) :work)
28 (_ :other)) 27 (_ :other))
29 "Which system is currently being used.") 28 "Which computer system is currently being used.")
30 29
31;;;; Run commands only when unfocused 30
32(defun acdw/when-unfocused (func &rest args) 31;; Utility functions
33 "Call FUNC, with ARGS, iff all Emacs frames are out of focus. 32
34 33(defmacro when-unfocused (name &rest forms)
35Ready for use with `after-focus-change-function'." 34 "Define a function NAME, executing FORMS, that fires when Emacs
36 (when (seq-every-p #'null (mapcar #'frame-focus-state (frame-list))) 35is unfocused."
37 (apply func args))) 36 (declare (indent 1))
38 37 (let ((func-name (intern (concat "when-unfocused-" (symbol-name name)))))
39;;;; Run commands at sunrise and sunset 38 `(progn
40(defun acdw/sunrise-sunset (sunrise-command sunset-command) 39 (defun ,func-name () "Defined by `when-unfocused'."
41 "Run commands at sunrise and sunset." 40 (when (seq-every-p #'null
42 (let* ((times-regex (rx (* nonl) 41 (mapcar #'frame-focus-state (frame-list)))
43 (: (any ?s ?S) "unrise") " " 42 ,@forms))
44 (group (repeat 1 2 digit) ":" 43 (add-function :after after-focus-change-function #',func-name))))
45 (repeat 1 2 digit) 44
46 (: (any ?a ?A ?p ?P) (any ?m ?M))) 45(defmacro hook-defun (name hooks &rest forms)
47 (* nonl) 46 "Define a function NAME that executes FORMS, and add it to
48 (: (any ?s ?S) "unset") " " 47each hook in HOOKS."
49 (group (repeat 1 2 digit) ":" 48 (declare (indent 2))
50 (repeat 1 2 digit) 49 (let ((func-name (intern (concat "hook-defun-" (symbol-name name))))
51 (: (any ?a ?A ?p ?P) (any ?m ?M))) 50 (hook-list (if (consp hooks) hooks (list hooks)))
52 (* nonl))) 51 (hook-defun-add-hook-list))
53 (ss (sunrise-sunset)) 52 `(progn
54 (_m (string-match times-regex ss)) 53 (defun ,func-name () "Defined by `hook-defun'." ,@forms)
55 (sunrise-time (match-string 1 ss)) 54 ,@(dolist (hook hook-list hook-defun-add-hook-list)
56 (sunset-time (match-string 2 ss))) 55 (push `(add-hook ',hook #',func-name) hook-defun-add-hook-list)))))
57 (run-at-time sunrise-time (* 60 60 24) sunrise-command) 56
58 (run-at-time sunset-time (* 60 60 24) sunset-command) 57(defun refresh-emacs ()
59 (run-at-time "12:00am" (* 60 60 24) sunset-command))) 58 "Reload Emacs's configuration files."
60 59 (interactive)
61;;;; Define a function and add it to hooks 60 (dolist (file (list (locate-user-emacs-file "early-init.el")
62(defun defun-with-hooks (hooks function-def) 61 (locate-user-emacs-file "init.el" ".emacs")))
63 "Add FUNCTION-DEF to HOOKS. 62 (when (file-exists-p file)
63 (load-file file))))
64
65(defun acdw/dir (&optional file make-directory)
66 "Place Emacs files in one place.
67
68If called without parameters, `acdw/dir' expands to
69~/.emacs.d/var or similar. If called with FILE, `acdw/dir'
70expands FILE to ~/.emacs.d/var, optionally making its directory
71if MAKE-DIRECTORY is non-nil."
72 (let ((dir (expand-file-name (convert-standard-filename "var/")
73 user-emacs-directory)))
74 (if file
75 (let ((file-name (expand-file-name (convert-standard-filename file)
76 dir)))
77 (when make-directory
78 (make-directory (file-name-directory file-name) 'parents))
79 file-name)
80 dir)))
64 81
65FUNCTION-DEF should be a `defun' form. This function is just to 82(defun acdw/gc-enable ()
66 put functions that only exist for hooks closer to the hooks 83 "Enable the Garbage collector."
67 they bind to." 84 (setq gc-cons-threshold (* 800 1024 1024)
68 (let ((func function-def)) 85 gc-cons-percentage 0.1))
69 (dolist (hook hooks)
70 (add-hook hook func))))
71
72;;; Garbage collection hacks
73
74(defconst acdw/gc-cons-threshold-basis (* 800 1024)
75 "Basis value for `gc-cons-threshold' to return to after jumping.
76800 KB is Emacs's default.")
77
78(defconst acdw/gc-cons-percentage-basis 0.1
79 "Basis value for `gc-cons-percentage' to return to after jumping.
800.1 is Emacs's default.")
81 86
82(defun acdw/gc-disable () 87(defun acdw/gc-disable ()
83 "Disable garbage collection by setting relevant variables to their maxima." 88 "Functionally disable the Garbage collector."
84 (setq gc-cons-threshold most-positive-fixnum 89 (setq gc-cons-threshold most-positive-fixnum
85 gc-cons-percentage 0.8)) 90 gc-cons-percentage 0.8))
86 91
87(defun acdw/gc-enable () 92
88 "Re-enable garbage collection by setting relevant variables back to bases." 93;; Make `C-z' more useful
89 (setq gc-cons-threshold acdw/gc-cons-threshold-basis
90 gc-cons-percentage acdw/gc-cons-percentage-basis))
91
92;;; Directories (think `no-littering')
93
94(defvar acdw/dir (expand-file-name
95 (convert-standard-filename "var/")
96 user-emacs-directory)
97 "A directory to hold extra configuration and emacs data.")
98
99(defun acdw/in-dir (file &optional make-directory)
100 "Expand FILE relative to `acdw/dir', optionally creating its
101directory."
102 (let ((f (expand-file-name (convert-standard-filename file)
103 acdw/dir)))
104 (when make-directory
105 (make-directory (file-name-directory f) 'parents))
106 f))
107
108;;; Reading mode
109
110(define-minor-mode acdw/reading-mode
111 "A mode for reading."
112 :init-value t
113 :lighter " Read"
114 (if acdw/reading-mode
115 (progn ;; turn on
116 ;; settings
117 (setq-local mode-line-format
118 '(:eval
119 (let* ((fmt " Reading %b (%m) ")
120 (len (length (format-mode-line fmt))))
121 (concat
122 (propertize " "
123 'display `((space :align-to (- right
124 ,len)))
125 'face '(:inherit italic))
126 fmt))))
127 ;; modes to disable
128 (dolist (mode '(display-fill-column-indicator-mode))
129 (when (fboundp mode)
130 (funcall mode -1)))
131 ;; modes to enable
132 (dolist (mode '(iscroll-mode
133 olivetti-mode))
134 (when (fboundp mode)
135 (funcall mode +1))))
136 ;; turn off
137 ;; settings
138 (kill-local-variable 'mode-line-format)
139 ;; modes to re-enable
140 (dolist (mode '(display-fill-column-indicator-mode
141 simple-modeline-mode))
142 (when (fboundp mode)
143 (funcall mode +1)))
144 ;; modes to re-disable
145 (dolist (mode '(olivetti-mode
146 iscroll-mode))
147 (when (fboundp mode)
148 (funcall mode -1)))
149 (force-mode-line-update)))
150
151;;; Keymap & Mode
152
153;; Set up a leader key for `acdw/mode'
154(defvar acdw/leader 94(defvar acdw/leader
155 (let ((map (make-sparse-keymap)) 95 (let ((map (make-sparse-keymap))
156 (c-z (global-key-binding "\C-z"))) 96 (c-z (global-key-binding "\C-z")))
@@ -159,5 +99,4 @@ directory."
159 map)) 99 map))
160 100
161(provide 'acdw) 101(provide 'acdw)
162
163;;; acdw.el ends here 102;;; acdw.el ends here