| git.druid.rocks | index | druid520 | radium | radium-editing.el |
radium-editing.el
;;; radium-editing.el, kmacro persistence, structural editing, multi-cursor, undo tree, symbol overlay, in-place grep editing -*- lexical-binding: t; -*-
(use-package iedit
:bind ("C-;" . iedit-mode))
(use-package multiple-cursors
:bind (("C-c m c" . mc/edit-lines)
("C->" . mc/mark-next-like-this)
("C-<" . mc/mark-previous-like-this)
("C-c m a" . mc/mark-all-like-this)))
(use-package elmacro
:commands (elmacro-mode elmacro-show-last-macro))
;; named, persistent keyboard macros: append the last one to
;; macros.el (right next to this file) as an fset, and load them all
;; back on startup, so they survive past this daemon's lifetime.
(defvar radium-macros-file (expand-file-name "macros.el" user-emacs-directory))
(defun radium-kmacro-save (name)
"append the last kbd macro to macros.el under NAME, so it survives
past this daemon (C-c k s). refuses if NAME is already in there -
edit macros.el directly to replace an old one, rather than let the
file quietly accumulate stale duplicate defaliases for the same name
every time i re-save under it."
(interactive "Sname for last kbd macro: ")
(unless last-kbd-macro
(user-error "err: no last kbd macro to save."))
(when (and (file-exists-p radium-macros-file)
(with-temp-buffer
(insert-file-contents radium-macros-file)
(goto-char (point-min))
(re-search-forward
(concat "defalias '" (regexp-quote (symbol-name name)) "\\_>") nil t)))
(user-error "err: %s already saved to macros.el, edit it there or pick a different name." name))
(kmacro-name-last-macro name)
(with-temp-buffer
(insert-kbd-macro name)
(newline)
(append-to-file (point-min) (point-max) radium-macros-file)))
;; a hand-edited or crash-truncated macros.el shouldnt take the rest of
;; this file - iedit/multiple-cursors/paredit/vundo/symbol-overlay/
;; wgrep, none of which have anything to do with kmacros - down with
;; it: radium-require in init.el only isolates failures between whole
;; modules, not within one, so an unguarded load error here would lose
;; all of the above too.
(when (file-exists-p radium-macros-file)
(condition-case e
(load radium-macros-file)
(error (message "err: macros.el failed to load: %s." (error-message-string e)))))
(global-set-key (kbd "C-c k s") #'radium-kmacro-save)
(global-set-key (kbd "C-c k n") #'kmacro-name-last-macro)
(global-set-key (kbd "C-c k e") #'elmacro-show-last-macro)
;; structural editing for the lisps (elisp, scheme) - paren-awareness,
;; not highlighting
(use-package paredit
:hook ((emacs-lisp-mode lisp-interaction-mode scheme-mode) . paredit-mode)
:config
;; paredit's own "[" / "]" handling (vector balancing, and a space
;; before "[" when the prior char is a symbol constituent - "!" counts)
;; breaks the "![name]" macro trigger in radium-macros.el: it turns
;; "![" into "! []", and its own "]" command just moves over the
;; existing bracket instead of self-inserting, so post-self-insert-hook
;; never fires. slurp/barf/wrap only matter for parens anyway, so let
;; square brackets fall through to plain self-insertion.
(define-key paredit-mode-map (kbd "[") nil)
(define-key paredit-mode-map (kbd "]") nil))
(show-paren-mode 1)
(electric-pair-mode -1)
;; visual undo tree: C-x u replaces plain undo with a navigable tree of
;; every undo branch, so nothing gets lost to a stray redo (it still
;; works fine on a tty - it draws with text, not bitmaps).
(use-package vundo
:bind ("C-x u" . vundo)
:custom (vundo-compact-display t))
;; every occurrence of the symbol at point lights up, and i can jump
;; through them (M-n/M-p) or rename them all in the buffer (C-c o r)
;; without any lsp - the overlay itself follows the syntax face colors
;; now that font-lock is back on.
(use-package symbol-overlay
:init (add-hook 'prog-mode-hook #'symbol-overlay-mode)
:custom (symbol-overlay-idle-time 0.1)
:config
;; this package has no globalized minor mode (4.3), so it turns on
;; per prog-mode buffer, and its own sparse keymap carries the
;; jump/rename keys - M-n/M-p only bind inside code buffers, which is
;; where they matter.
(define-key symbol-overlay-mode-map (kbd "M-n") #'symbol-overlay-jump-next)
(define-key symbol-overlay-mode-map (kbd "M-p") #'symbol-overlay-jump-prev)
(define-key symbol-overlay-mode-map (kbd "C-c o r") #'symbol-overlay-rename)
(define-key symbol-overlay-mode-map (kbd "C-c o k") #'symbol-overlay-remove-all)
(define-key symbol-overlay-mode-map (kbd "C-c o t") #'symbol-overlay-toggle-in-scope))
;; edit grep/occur results in place: C-c C-e turns the result buffer
;; writable, edits get written back to every matching file on C-x C-s
;; (wgrep-auto-save-buffer). consult-grep shows an xref buffer instead,
;; so this applies to the classic rgrep/grep/occur outputs.
(use-package wgrep
:config
(setq wgrep-auto-save-buffer t)
(define-key grep-mode-map (kbd "C-c C-e") #'wgrep-change-to-wgrep-mode)
(define-key occur-mode-map (kbd "C-c C-e") #'wgrep-change-to-wgrep-mode))
(provide 'radium-editing)