diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2023-03-04 13:15:34 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2023-03-04 14:12:51 -0700 |
commit | 5336ab9665375a80ea136c5464b5e5a98ce379b5 (patch) | |
tree | 342358fce13d561ca10eedaa544470757ff9a6b4 | |
parent | 6d5becbbacfc7b0f47ab70a97dcc59d7ab72c0e7 (diff) | |
download | dotfiles-5336ab9665375a80ea136c5464b5e5a98ce379b5.tar.gz |
replace spw/scratch-paredit-mode with a major mode
-rw-r--r-- | .emacs.d/init.el | 125 |
1 files changed, 59 insertions, 66 deletions
diff --git a/.emacs.d/init.el b/.emacs.d/init.el index 6977be4f..abce0f2e 100644 --- a/.emacs.d/init.el +++ b/.emacs.d/init.el @@ -278,6 +278,7 @@ windows side-by-side in the frame." '(howm-view-use-grep t) '(icomplete-in-buffer t) '(imenu-auto-rescan t) + '(initial-major-mode 'spw/scratch-lisp-interaction-mode) '(kill-read-only-ok t) '(log-edit-hook '(log-edit-insert-message-template log-edit-insert-cvs-template log-edit-insert-changelog spw/log-edit-show-diff) nil nil "Drop log-edit-show-files to avoid its window becoming most recently used for C-x o.") @@ -649,7 +650,7 @@ that's something we can determine.") ;;; But using one buffer for both Lisp evaluation and rough notes means that ;;; we can have a single i3/Sway binding for both purposes. ;;; If another major mode is really required, can just create a new buffer. -;;; This relies for its usability on `spw/scratch-paredit-mode'. +;;; This relies for its usability on `spw/scratch-lisp-interaction-mode'. (defun spw/remember-notes-setup () (when (zerop (buffer-size)) @@ -4279,9 +4280,7 @@ mutt's review view, after exiting EDITOR." haskell-mode)) (spw/when-library-available orgalist - (spw/feature-add-hook orgalist-mode - message - (remember remember-notes-mode-hook))) + (spw/feature-add-hook orgalist-mode message)) (spw/feature-add-hook orgtbl-mode message) @@ -4460,11 +4459,7 @@ likely to keep parentheses balanced." ;;; Paredit -(defun spw/enable-paredit-mode () - (if (eq (current-buffer) (get-buffer "*scratch*")) - (spw/scratch-paredit-mode 1) - (paredit-mode 1))) -(spw/feature-add-hook spw/enable-paredit-mode +(spw/feature-add-hook enable-paredit-mode (nil lisp-data-mode-hook) (nil emacs-lisp-mode-hook) (nil eval-expression-minibuffer-setup-hook) scheme (xscheme xscheme-start-hook) slime-repl) @@ -4554,8 +4549,6 @@ likely to keep parentheses balanced." (advice-add 'paredit-insert-comment :after #'spw/paredit-insert-comment-before-defun) -;;; Spw/Scratch-Paredit - (defun spw/scratch-paredit-in-lisp-p () "Does point's paragraph look to be within a defun?" (or @@ -4592,61 +4585,61 @@ likely to keep parentheses balanced." ((not (eq syn ?\s)) (throw 'done nil)))) (skip-chars-backward "[:space:]\n")))))) -;; This must come after I've made all my modifications to `paredit-mode-map'. -(defvar spw/scratch-paredit-mode-map - (with-current-buffer (get-buffer-create "*scratch*") - (let (;; Bind `minor-mode-map-alist' such that our call to `key-binding' - ;; ignores the bindings of minor modes that have priority over us. - ;; This avoids an infinite loop if one of those minor modes wants to - ;; defer to our binding, e.g. in the way `orgtbl-mode' does. - (minor-mode-map-alist - (or (cdr (cl-member 'spw/scratch-paredit-mode minor-mode-map-alist - :key #'car)) - minor-mode-map-alist)) - ;; Also ensure that `key-binding' ignores `paredit-mode-map'. - ;; We don't need to turn the mode off because we know we are earlier - ;; in `minor-mode-map-alist' as we require the `paredit' feature, - ;; and `paredit-mode-map' is our parent, so lookup can't ever fail - ;; in our map but then succeed in `paredit-mode-map'. - paredit-mode - (map (make-sparse-keymap))) - (set-keymap-parent map paredit-mode-map) - (named-let define-keys ((from paredit-mode-map) (into map) (prefix [])) - (map-keymap - (lambda (event paredit-binding) - ;; `paredit-mode-map' has no parents; otherwise, we should - ;; conditionalise on (eq (lookup-key keymap key) paredit-binding). - (let* ((key (vector event)) - (prefixed (vconcat prefix key))) - (cond - ((keymapp paredit-binding) - (let ((map (make-sparse-keymap))) - (define-key into key map) - (define-keys paredit-binding map prefixed))) - ((commandp paredit-binding) - (when-let* ((normal-binding (key-binding prefixed)) - (new-binding - (intern (format "spw/scratch-%s" - (if (symbolp paredit-binding) - paredit-binding - (gensym)))))) - (defalias new-binding - (lambda () - (interactive) - (call-interactively - (if (spw/scratch-paredit-in-lisp-p) - paredit-binding - normal-binding))) - (format "Like `%s' but for `spw/scratch-paredit-mode'." - paredit-binding)) - (define-key into key new-binding)))))) - from)) - map)) - "Keymap for `spw/scratch-paredit-mode'.") - -(define-minor-mode spw/scratch-paredit-mode - "Less strict Paredit for the *scratch* buffer. -Paredit commands are invoked only when point's paragraph is within a defun.") +(define-derived-mode spw/scratch-lisp-interaction-mode lisp-interaction-mode + "Lisp Interaction" + (let (;; Bind `minor-mode-map-alist' such that our call to `key-binding' + ;; ignores the bindings of minor modes that have priority over + ;; Paredit. This avoids an infinite loop if one of those minor modes + ;; wants to defer to our binding, e.g. in the way `orgtbl-mode' does. + (minor-mode-map-alist + (or (cdr (cl-member 'paredit-mode minor-mode-map-alist :key #'car)) + minor-mode-map-alist)) + (map (make-sparse-keymap))) + (set-keymap-parent map paredit-mode-map) + (named-let define-keys ((from paredit-mode-map) (into map) (prefix [])) + (map-keymap + (lambda (event paredit-binding) + ;; `paredit-mode-map' has no parents; otherwise, we should + ;; conditionalise on (eq (lookup-key keymap key) paredit-binding). + (let* ((key (vector event)) + (prefixed (vconcat prefix key))) + (cond ((keymapp paredit-binding) + (let ((map (make-sparse-keymap))) + (define-key into key map) + (define-keys paredit-binding map prefixed))) + ((commandp paredit-binding) + (when-let* ((normal-binding (key-binding prefixed)) + (new-binding + (intern (format "spw/scratch-%s" + (if (symbolp paredit-binding) + paredit-binding + (gensym)))))) + (defalias new-binding + (lambda () + (interactive) + (call-interactively + (if (spw/scratch-paredit-in-lisp-p) + paredit-binding + normal-binding))) + (format "Like `%s', but for `spw/scratch-lisp-interaction-mode'." + paredit-binding)) + (define-key into key new-binding)))))) + from)) + (push `(paredit-mode . ,map) minor-mode-overriding-map-alist)) + (setq-local fill-paragraph-function + (lambda (&optional justify) + (and (spw/scratch-paredit-in-lisp-p) + (lisp-fill-paragraph justify))) + normal-auto-fill-function + (lambda () + (and (not (spw/scratch-paredit-in-lisp-p)) + (let (comment-start) (do-auto-fill))))) + ;; (orgtbl-mode 1) ; appears to break `eldoc-mode' and/or `show-paren-mode' + (spw/when-library-available orgalist (orgalist-mode 1))) +(define-key spw/scratch-lisp-interaction-mode-map "\M-q" #'fill-paragraph) +;; Override earlier `setq-mode-local' for `prog-mode'. +(setq-mode-local spw/scratch-lisp-interaction-mode + comment-auto-fill-only-comments nil) ;;;; Text mode |