From 4366fa163d49b573ef523c256d6bbe70dc62ec18 Mon Sep 17 00:00:00 2001 From: Madeleine Sydney Date: Sun, 12 Jan 2025 11:13:15 -0700 Subject: [PATCH] feat: Leader key and General.el --- README.org | 12 +++ users/crumb/programs/emacs/init.el | 1 + .../programs/emacs/modules/syd-keybinds.el | 76 +++++++++++++++++++ users/crumb/programs/emacs/modules/syd-ui.el | 6 -- 4 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 users/crumb/programs/emacs/modules/syd-keybinds.el diff --git a/README.org b/README.org index 28d26d8..faa17ae 100755 --- a/README.org +++ b/README.org @@ -123,6 +123,18 @@ The difference connects to another silly obsession of the Emacs hacker: startup ~on-first-input~ is one of many useful hooks provided by the package [[https://github.com/emacsmirror/on][on.el]] specialised for fine-grained control of package loading. +** Naming conventions in my Emacs config + +As with the rest of the config, these are largely adapted from Doom's ([cite:@lissner2022contributing]). }:3 + +- ~syd-[-]«NAME»~ :: Typical 'namespaced' Elisp convention ([cite:@gnu2020conventions]). + +- ~syd/«NAME»~ :: Commands intended for interactive use. + +- ~+syd-[-]«NAME»~ :: Indicates a [[https://en.wikipedia.org/wiki/Strategy_pattern][strategy]] function. + +- ~syd-«NAME»-initialise~, where ~modules/«NAME» ∈ modules/*.el~ :: Instead of using top-level side-effects, (bar e.g. ~use-package~ invocations) should be wrapped in this kind of initialisation procedure. + >>>>>>> Conflict 1 of 1 ends * Tasks diff --git a/users/crumb/programs/emacs/init.el b/users/crumb/programs/emacs/init.el index 68c9abb..b24895a 100644 --- a/users/crumb/programs/emacs/init.el +++ b/users/crumb/programs/emacs/init.el @@ -14,4 +14,5 @@ (require 'syd-autosave) (require 'syd-display-startup-time) (require 'syd-evil) +(require 'syd-keybinds) (require 'syd-ui) diff --git a/users/crumb/programs/emacs/modules/syd-keybinds.el b/users/crumb/programs/emacs/modules/syd-keybinds.el new file mode 100644 index 0000000..3fb539b --- /dev/null +++ b/users/crumb/programs/emacs/modules/syd-keybinds.el @@ -0,0 +1,76 @@ +;;; syd-keybinds.el -*- lexical-binding: t; -*- +;;; Universal keybindings, not /too/ tied to any particular packages. + +(use-package general + :custom (general-use-package-emit-autoloads t)) + +(require 'general) + +(defvar syd-leader-key "SPC" + "A prefix key akin to Vim's .") + +(defvar syd-localleader-key "SPC m" + "A prefix key akin to Vim's .") + +(defvar syd-alt-leader-key "M-SPC" + "`syd-leader', but for the states specified in `syd-alt-leader-key-states'. + +Often, your \"usual\" leader key will be something unavailable in the Insert +state. This key exists as a fallback for when you need your Leader, but must +remain in the Insert state. Substitute \"Insert state\" for your states of +choice with `syd-alt-leader-key-states'.") + +(defvar syd-alt-localleader-key "M-SPC m" + "`syd-localleader', but for the states specified in `syd-alt-leader-key-states'. + +See `syd-alt-leader-key' for rationale.") + +(defvar syd-leader-key-states '(normal visual motion) + "States for which the Leader keys (`syd-leader-key', `syd-localleader-key') +are active.") + +(defvar syd-alt-leader-key-states '(emacs insert) + "States for which the alternative Leader keys are active. See +`syd-alt-leader-key' and `syd-alt-localleader-key'.") + +(defvar-keymap syd-leader-map + :doc "Leader-prefixed commands") + +(defvar-keymap syd-leader-window-map + :doc "Window commands") + +(defun syd--initialise-leader () + "Set up the (empty) keymap associated with `syd-leader-key', +`syd-localleader-key', `syd-alt-leader-key', and `syd-alt-localleader-key'." + ;; Define `syd/leader' as a command corresponding to the prefix map + ;; `syd-leader-map'. + (define-prefix-command 'syd/leader 'syd-leader-map) + ;; This should help make the Leader key close to universally available. + ;; Ideally, *nothing* takes precedence over Leader — it's an incredibly + ;; important key! + ;; https://github.com/noctuid/evil-guide?tab=readme-ov-file#undoprevent-overridingintercept-maps + ;; Really, we do this because Doom does. I'm honestly not entirely sure what + ;; it does. I can't find anything useful online. + (define-key syd-leader-map [override-state] 'all) + ;; Finally, we shall bind the damned keys. }:) + (let ((map general-override-mode-map)) + (require 'evil) + (evil-define-key* syd-leader-key-states map (kbd syd-leader-key) 'syd/leader) + (evil-define-key* syd-alt-leader-key-states map (kbd syd-leader-key) 'syd/leader)) + (general-override-mode 1)) + +(defun syd-keybinds-initialise () + (syd--initialise-leader) + (general-define-key + :keymaps 'syd-leader-map + "." #'find-file)) + +(syd-keybinds-initialise) + +;; Show possible completions for a partially-entered key sequence. +(use-package which-key + :hook (on-first-input . which-key-mode) + :custom ((which-key-allow-evil-operators t) + (which-key-show-operator-state-maps t))) + +(provide 'syd-keybinds) diff --git a/users/crumb/programs/emacs/modules/syd-ui.el b/users/crumb/programs/emacs/modules/syd-ui.el index 3f70c39..61fab6d 100644 --- a/users/crumb/programs/emacs/modules/syd-ui.el +++ b/users/crumb/programs/emacs/modules/syd-ui.el @@ -1,11 +1,5 @@ ;;; syd-ui.el -*- lexical-binding: t; -*- -;; Show possible completions for a partially-entered key sequence. -(use-package which-key - :hook (on-first-input . which-key-mode) - :custom ((which-key-allow-evil-operators t) - (which-key-show-operator-state-maps t))) - ;; Beautiful theme in dark and light. (use-package kanagawa-themes :config