From c05d1ba2c8e28f2f55f9e11bcf8e42b1ed43e539 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Tue, 6 Apr 2021 17:59:45 -0500 Subject: 7 --- lisp/acdw.el | 193 ++++++++++++++++++++--------------------------------------- 1 file changed, 66 insertions(+), 127 deletions(-) (limited to 'lisp/acdw.el') 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 @@ ;; functions for me, acdw. ;;; Code: + +;; Utility constants -;;; Utilities - -;;;; Determine the system I'm on (defconst acdw/system (pcase system-type - ('gnu/linux :home) - ((or 'msdos 'windows-nt) :work) - (_ :other)) - "Which system is currently being used.") - -;;;; Run commands only when unfocused -(defun acdw/when-unfocused (func &rest args) - "Call FUNC, with ARGS, iff all Emacs frames are out of focus. - -Ready for use with `after-focus-change-function'." - (when (seq-every-p #'null (mapcar #'frame-focus-state (frame-list))) - (apply func args))) - -;;;; Run commands at sunrise and sunset -(defun acdw/sunrise-sunset (sunrise-command sunset-command) - "Run commands at sunrise and sunset." - (let* ((times-regex (rx (* nonl) - (: (any ?s ?S) "unrise") " " - (group (repeat 1 2 digit) ":" - (repeat 1 2 digit) - (: (any ?a ?A ?p ?P) (any ?m ?M))) - (* nonl) - (: (any ?s ?S) "unset") " " - (group (repeat 1 2 digit) ":" - (repeat 1 2 digit) - (: (any ?a ?A ?p ?P) (any ?m ?M))) - (* nonl))) - (ss (sunrise-sunset)) - (_m (string-match times-regex ss)) - (sunrise-time (match-string 1 ss)) - (sunset-time (match-string 2 ss))) - (run-at-time sunrise-time (* 60 60 24) sunrise-command) - (run-at-time sunset-time (* 60 60 24) sunset-command) - (run-at-time "12:00am" (* 60 60 24) sunset-command))) - -;;;; Define a function and add it to hooks -(defun defun-with-hooks (hooks function-def) - "Add FUNCTION-DEF to HOOKS. + ('gnu/linux :home) + ((or 'msdos 'windows-nt) :work) + (_ :other)) + "Which computer system is currently being used.") + + +;; Utility functions + +(defmacro when-unfocused (name &rest forms) + "Define a function NAME, executing FORMS, that fires when Emacs +is unfocused." + (declare (indent 1)) + (let ((func-name (intern (concat "when-unfocused-" (symbol-name name))))) + `(progn + (defun ,func-name () "Defined by `when-unfocused'." + (when (seq-every-p #'null + (mapcar #'frame-focus-state (frame-list))) + ,@forms)) + (add-function :after after-focus-change-function #',func-name)))) + +(defmacro hook-defun (name hooks &rest forms) + "Define a function NAME that executes FORMS, and add it to +each hook in HOOKS." + (declare (indent 2)) + (let ((func-name (intern (concat "hook-defun-" (symbol-name name)))) + (hook-list (if (consp hooks) hooks (list hooks))) + (hook-defun-add-hook-list)) + `(progn + (defun ,func-name () "Defined by `hook-defun'." ,@forms) + ,@(dolist (hook hook-list hook-defun-add-hook-list) + (push `(add-hook ',hook #',func-name) hook-defun-add-hook-list))))) + +(defun refresh-emacs () + "Reload Emacs's configuration files." + (interactive) + (dolist (file (list (locate-user-emacs-file "early-init.el") + (locate-user-emacs-file "init.el" ".emacs"))) + (when (file-exists-p file) + (load-file file)))) + +(defun acdw/dir (&optional file make-directory) + "Place Emacs files in one place. + +If called without parameters, `acdw/dir' expands to +~/.emacs.d/var or similar. If called with FILE, `acdw/dir' +expands FILE to ~/.emacs.d/var, optionally making its directory +if MAKE-DIRECTORY is non-nil." + (let ((dir (expand-file-name (convert-standard-filename "var/") + user-emacs-directory))) + (if file + (let ((file-name (expand-file-name (convert-standard-filename file) + dir))) + (when make-directory + (make-directory (file-name-directory file-name) 'parents)) + file-name) + dir))) -FUNCTION-DEF should be a `defun' form. This function is just to - put functions that only exist for hooks closer to the hooks - they bind to." - (let ((func function-def)) - (dolist (hook hooks) - (add-hook hook func)))) - -;;; Garbage collection hacks - -(defconst acdw/gc-cons-threshold-basis (* 800 1024) - "Basis value for `gc-cons-threshold' to return to after jumping. -800 KB is Emacs's default.") - -(defconst acdw/gc-cons-percentage-basis 0.1 - "Basis value for `gc-cons-percentage' to return to after jumping. -0.1 is Emacs's default.") +(defun acdw/gc-enable () + "Enable the Garbage collector." + (setq gc-cons-threshold (* 800 1024 1024) + gc-cons-percentage 0.1)) (defun acdw/gc-disable () - "Disable garbage collection by setting relevant variables to their maxima." + "Functionally disable the Garbage collector." (setq gc-cons-threshold most-positive-fixnum - gc-cons-percentage 0.8)) + gc-cons-percentage 0.8)) -(defun acdw/gc-enable () - "Re-enable garbage collection by setting relevant variables back to bases." - (setq gc-cons-threshold acdw/gc-cons-threshold-basis - gc-cons-percentage acdw/gc-cons-percentage-basis)) - -;;; Directories (think `no-littering') - -(defvar acdw/dir (expand-file-name - (convert-standard-filename "var/") - user-emacs-directory) - "A directory to hold extra configuration and emacs data.") - -(defun acdw/in-dir (file &optional make-directory) - "Expand FILE relative to `acdw/dir', optionally creating its -directory." - (let ((f (expand-file-name (convert-standard-filename file) - acdw/dir))) - (when make-directory - (make-directory (file-name-directory f) 'parents)) - f)) - -;;; Reading mode - -(define-minor-mode acdw/reading-mode - "A mode for reading." - :init-value t - :lighter " Read" - (if acdw/reading-mode - (progn ;; turn on - ;; settings - (setq-local mode-line-format - '(:eval - (let* ((fmt " Reading %b (%m) ") - (len (length (format-mode-line fmt)))) - (concat - (propertize " " - 'display `((space :align-to (- right - ,len))) - 'face '(:inherit italic)) - fmt)))) - ;; modes to disable - (dolist (mode '(display-fill-column-indicator-mode)) - (when (fboundp mode) - (funcall mode -1))) - ;; modes to enable - (dolist (mode '(iscroll-mode - olivetti-mode)) - (when (fboundp mode) - (funcall mode +1)))) - ;; turn off - ;; settings - (kill-local-variable 'mode-line-format) - ;; modes to re-enable - (dolist (mode '(display-fill-column-indicator-mode - simple-modeline-mode)) - (when (fboundp mode) - (funcall mode +1))) - ;; modes to re-disable - (dolist (mode '(olivetti-mode - iscroll-mode)) - (when (fboundp mode) - (funcall mode -1))) - (force-mode-line-update))) - -;;; Keymap & Mode - -;; Set up a leader key for `acdw/mode' + +;; Make `C-z' more useful (defvar acdw/leader (let ((map (make-sparse-keymap)) (c-z (global-key-binding "\C-z"))) @@ -159,5 +99,4 @@ directory." map)) (provide 'acdw) - ;;; acdw.el ends here -- cgit 1.4.1-21-gabe81