From 407771183e9d70b7e4b8ed9e2d773c9bc8e7af14 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Thu, 29 Apr 2021 12:16:03 -0500 Subject: Massively refactor - Redefine as much as possible as `setup' forms - Reorganize into "Setup", "Basics", and "Packages" sections - Within each section, alphabetize sexps - Also (mostly) alphabetize acdw- files - (Not the ones that are almost completely others' code) - Sidebar: Why is this not a thing in elisp!? Should write a function - Break karthink's thing into another library `acdw-re' - Add a function to `acdw': `acdw/find-emacs-source' - Should refactor that to better find the source I think everything looks much more better now! --- lisp/acdw-modeline.el | 92 +++++++++++++++++------------------ lisp/acdw-re.el | 66 ++++++++++++++++++++++++++ lisp/acdw.el | 129 +++++++++++++++++++++++++++----------------------- 3 files changed, 181 insertions(+), 106 deletions(-) create mode 100644 lisp/acdw-re.el (limited to 'lisp') diff --git a/lisp/acdw-modeline.el b/lisp/acdw-modeline.el index c7b904f..79249bb 100644 --- a/lisp/acdw-modeline.el +++ b/lisp/acdw-modeline.el @@ -22,64 +22,60 @@ (require 'simple-modeline) (require 'minions) -;; modified from `simple-modeline' -(defun acdw-modeline/modified () - "Displays a color-coded buffer modification/read-only +(defun acdw-modeline/buffer-name () ; gonsie + (propertize " %b " + 'face + (if (buffer-modified-p) + 'font-lock-warning-face + 'font-lock-type-face) + 'help-echo (buffer-file-name))) + +(defun acdw-modeline/god-mode-indicator () + (when (bound-and-true-p god-local-mode) + " God")) + +(defun acdw-modeline/modified () ; modified from `simple-modeline' + "Displays a color-coded buffer modification/read-only indicator in the mode-line." - (if (not (string-match-p "\\*.*\\*" (buffer-name))) - (let* ((read-only (and buffer-read-only (buffer-file-name))) + (if (not (string-match-p "\\*.*\\*" (buffer-name))) + (let* ((read-only (and buffer-read-only (buffer-file-name))) (modified (buffer-modified-p))) (propertize (if read-only " =" (if modified " +" " -")) 'help-echo (format - (concat "Buffer is %s and %smodified\n" - "mouse-1: Toggle read-only status.") - (if read-only "read-only" "writable") - (if modified "" "not ")) + (concat "Buffer is %s and %smodified\n" + "mouse-1: Toggle read-only status.") + (if read-only "read-only" "writable") + (if modified "" "not ")) 'local-map (purecopy (simple-modeline-make-mouse-map - 'mouse-1 - (lambda (event) - (interactive "e") - (with-selected-window - (posn-window (event-start event)) - (read-only-mode 'toggle))))) + 'mouse-1 + (lambda (event) + (interactive "e") + (with-selected-window + (posn-window (event-start event)) + (read-only-mode 'toggle))))) 'mouse-face 'mode-line-highlight)))) -;; all me, baby -(defun acdw-modeline/minions () - "Display a button for `minions-minor-modes-menu'." - (concat - " " - (propertize - "&" - 'help-echo (format - "Minor modes menu\nmouse-1: show menu.") - 'local-map (purecopy (simple-modeline-make-mouse-map - 'mouse-1 - (lambda (event) - (interactive "e") - (with-selected-window (posn-window - (event-start event)) - (minions-minor-modes-menu))))) - 'mouse-face 'mode-line-highlight))) +(defun acdw-modeline/minions () ; by me + "Display a button for `minions-minor-modes-menu'." + (concat + " " + (propertize + "&" + 'help-echo (format + "Minor modes menu\nmouse-1: show menu.") + 'local-map (purecopy (simple-modeline-make-mouse-map + 'mouse-1 + (lambda (event) + (interactive "e") + (with-selected-window (posn-window + (event-start event)) + (minions-minor-modes-menu))))) + 'mouse-face 'mode-line-highlight))) -;; from https://www.gonsie.com/blorg/modeline.html, from Doom (defun acdw-modeline/vc-branch () + ;; from https://www.gonsie.com/blorg/modeline.html, from Doom (if-let ((backend (vc-backend buffer-file-name))) - (substring vc-mode (+ (if (eq backend 'Hg) 2 3) 2)))) - -;; from gonsie -(defun acdw-modeline/buffer-name () - (propertize " %b " - 'face - (if (buffer-modified-p) - 'font-lock-warning-face - 'font-lock-type-face) - 'help-echo (buffer-file-name))) - -;; god-mode indicator -(defun acdw-modeline/god-mode-indicator () - (when (bound-and-true-p god-local-mode) - " God")) + (substring vc-mode (+ (if (eq backend 'Hg) 2 3) 2)))) (provide 'acdw-modeline) diff --git a/lisp/acdw-re.el b/lisp/acdw-re.el new file mode 100644 index 0000000..ea1c42a --- /dev/null +++ b/lisp/acdw-re.el @@ -0,0 +1,66 @@ +;;; acdw-re.el -*- lexical-binding: t; coding: utf-8-unix -*- +;; Author: Case Duckworth +;; Created: 2021-04-29 +;; Keywords: configuration +;; URL: https://tildegit.org/acdw/emacs + +;; This file is NOT part of GNU Emacs. + +;;; License: +;; Everyone is permitted to do whatever with this software, without +;; limitation. This software comes without any warranty whatsoever, +;; but with two pieces of advice: +;; - Don't hurt yourself. +;; - Make good choices. + +;;; Commentary: +;; Pulled mostly from karthinks: +;; https://karthinks.com/software/bridging-islands-in-emacs-1/ + +;;; Code: + +(defvar acdw/re-builder-positions nil + "Store point and region bounds before calling re-builder") + +(defun acdw/re-builder-save-state (&rest _) + "Save into `acdw/re-builder-positions' the point and region +positions before calling `re-builder'." + (setq acdw/re-builder-positions + (cons (point) + (when (region-active-p) + (list (region-beginning) + (region-end)))))) + +(defun reb-replace-regexp (&optional delimited) + "Run `query-replace-regexp' with the contents of re-builder. With +non-nil optional argument DELIMITED, only replace matches +surrounded by word boundaries." + (interactive "P") + (reb-update-regexp) + (let* ((re (reb-target-binding reb-regexp)) + (replacement (query-replace-read-to + re + (concat "Query replace" + (if current-prefix-arg + (if (eq current-prefix-arg '-) + " backward" + " word") + "") + " regexp" + (if (with-selected-window reb-target-window + (region-active-p)) " in region" "")) + t)) + (pnt (car acdw/re-builder-positions)) + (beg (cadr acdw/re-builder-positions)) + (end (caddr acdw/re-builder-positions))) + (with-selected-window reb-target-window + ;; replace with (goto-char (match-beginning 0)) if you want to control + ;; where in the buffer the replacement starts with re-builder + (goto-char pnt) + (setq acdw/re-builder-positions nil) + (reb-quit) + (query-replace-regexp re replacement delimited beg end)))) + +(provide 'acdw-re) + +;;; acdw-re.el ends here diff --git a/lisp/acdw.el b/lisp/acdw.el index 54b139c..1093a8d 100644 --- a/lisp/acdw.el +++ b/lisp/acdw.el @@ -19,7 +19,7 @@ ;;; Code: -;; Utility constants +;;; Variables (defconst acdw/system (pcase system-type ('gnu/linux :home) @@ -28,19 +28,15 @@ "Which computer system is currently being used.") -;; Utility functions +;;; 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)))) +(defun expand-file-name-exists-p (&rest expand-file-name-args) + "Call `expand-file-name' on EXPAND-FILE-NAME-ARGS, returning + its name if it exists, or NIL otherwise." + (let ((file (apply #'expand-file-name expand-file-name-args))) + (if (file-exists-p file) + file + nil))) (defmacro hook-defun (name hooks &rest forms) "Define a function NAME that executes FORMS, and add it to @@ -88,13 +84,17 @@ With a prefix argument, run git pull on the repo first." (when (file-exists-p file) (load-file file)))))) -(defun expand-file-name-exists-p (&rest expand-file-name-args) - "Call `expand-file-name' on EXPAND-FILE-NAME-ARGS, returning - its name if it exists, or NIL otherwise." - (let ((file (apply #'expand-file-name expand-file-name-args))) - (if (file-exists-p file) - file - nil))) +(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 with-message (message &rest body) "Execute BODY, messaging 'MESSAGE...' before and 'MESSAGE... Done.' after." @@ -105,6 +105,9 @@ With a prefix argument, run git pull on the repo first." ,@body) (message "%s... Done." ,message))) + +;;; Specialized functions + (defun acdw/dir (&optional file make-directory) "Place Emacs files in one place. @@ -122,37 +125,6 @@ if MAKE-DIRECTORY is non-nil." file-name) dir))) -(defun acdw/gc-enable () - "Enable the Garbage collector." - (setq gc-cons-threshold (* 800 1024 1024) - gc-cons-percentage 0.1)) - -(defun acdw/gc-disable () - "Functionally disable the Garbage collector." - (setq gc-cons-threshold most-positive-fixnum - gc-cons-percentage 0.8)) - -(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))) - (defun acdw/find-emacs-dotfiles () "Finds lisp files in `user-emacs-directory' and passes them to `completing-read'." @@ -161,6 +133,30 @@ if MAKE-DIRECTORY is non-nil." (directory-files-recursively user-emacs-directory "\.el$")))) +(defun acdw/find-emacs-source () + "Find where Emacs keeps its source tree." + (pcase acdw/system + (:work (expand-file-name + (concat "~/src/emacs-" emacs-version "/src"))) + (:home (expand-file-name "~/src/pkg/emacs/src/emacs-git/src")) + (:other nil))) + +(defun acdw/gc-disable () + "Functionally disable the Garbage collector." + (setq gc-cons-threshold most-positive-fixnum + gc-cons-percentage 0.8)) + +(defun acdw/gc-enable () + "Enable the Garbage collector." + (setq gc-cons-threshold (* 800 1024 1024) + gc-cons-percentage 0.1)) + +(defun acdw/insert-iso-date (with-time) + "Insert the ISO-8601-formatted date, with optional time." + (interactive "P") + (let ((format (if with-time "%FT%T%z" "%F"))) + (insert (format-time-string format (current-time))))) + (defun acdw/kill-a-buffer (&optional prefix) "Kill a buffer based on the following rules: @@ -179,14 +175,30 @@ Prompt only if there are unsaved changes." (16 (mapc 'kill-buffer (delq (current-buffer) (buffer-list))) (delete-other-windows)))) -(defun acdw/insert-iso-date (with-time) - "Insert the ISO-8601-formatted date, with optional time." - (interactive "P") - (let ((format (if with-time "%FT%T%z" "%F"))) - (insert (format-time-string format (current-time))))) +(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))) -;; Make `C-z' more useful +;;; Keymaps + (defvar acdw/leader (let ((map (make-sparse-keymap)) (c-z (global-key-binding "\C-z"))) @@ -195,7 +207,8 @@ Prompt only if there are unsaved changes." map)) -;; `acdw/reading-mode' +;;; Minor modes + (define-minor-mode acdw/reading-mode "A mode for reading." :init-value nil -- cgit 1.4.1-21-gabe81