Add helpful (with custom forward and back functions)

This commit is contained in:
Alexander Rosenberg 2024-01-09 21:32:22 -08:00
parent a474ebf152
commit 67fdc1c341
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730

105
init.el
View File

@ -40,15 +40,15 @@
;; basic stuff ;; basic stuff
(use-package emacs (use-package emacs
:hook ((emacs-lisp-mode . my/-emacs-lisp-mode-setup-evil-lookup) :hook (;(emacs-lisp-mode . my/-emacs-lisp-mode-setup-evil-lookup)
(prog-mode . electric-pair-local-mode) (prog-mode . electric-pair-local-mode)
((text-mode message-mode tex-mode) . flyspell-mode) ((text-mode message-mode tex-mode) . flyspell-mode)
((text-mode message-mode tex-mode) . auto-fill-mode) ((text-mode message-mode tex-mode prog-mode) . auto-fill-mode)
(prog-mode . flyspell-prog-mode)) (prog-mode . flyspell-prog-mode))
:init :init
(defun my/-emacs-lisp-mode-setup-evil-lookup () ;; (defun my/-emacs-lisp-mode-setup-evil-lookup ()
(setq-local evil-lookup-func ;; (setq-local evil-lookup-func
#'my/describe-symbol-at-point)) ;; #'my/describe-symbol-at-point))
(defun my/describe-symbol-at-point () (defun my/describe-symbol-at-point ()
"Calls `describe-symbol' on the return value of `form-at-point'." "Calls `describe-symbol' on the return value of `form-at-point'."
(interactive) (interactive)
@ -108,7 +108,8 @@
;; Some settings for programming ;; Some settings for programming
(setq-default indent-tabs-mode nil (setq-default indent-tabs-mode nil
tab-width 4) tab-width 4
fill-column 80)
;; Tree sitter download locations ;; Tree sitter download locations
(setq treesit-language-source-alist (setq treesit-language-source-alist
@ -313,7 +314,9 @@ visual states."
:bind (("C-," . embark-act) :bind (("C-," . embark-act)
("C-;" . embark-dwim) ("C-;" . embark-dwim)
:map help-map :map help-map
("B" . embark-bindings)) ("B" . embark-bindings)
:map embark-symbol-map
("h" . helpful-symbol))
:init :init
(setq embark-quit-after-action nil) (setq embark-quit-after-action nil)
(add-to-list 'display-buffer-alist (add-to-list 'display-buffer-alist
@ -500,8 +503,9 @@ visual states."
;; yasnippet ;; yasnippet
(use-package yasnippet (use-package yasnippet
:demand t
:bind ("C-c s" . yas-expand) :bind ("C-c s" . yas-expand)
:init :config
(yas-global-mode 1)) (yas-global-mode 1))
;; project.el ;; project.el
@ -1154,6 +1158,91 @@ If no name is given, list all bookmarks instead."
(mu4e t) (mu4e t)
(mu4e-context-switch nil "Personal") (mu4e-context-switch nil "Personal")
;; helpful
(use-package helpful
:hook (emacs-lisp-mode . my/-helpful-setup-emacs-lisp-mode)
:bind (:map help-map
("f" . helpful-callable)
("v" . helpful-variable)
("k" . helpful-key)
("x" . helpful-command)
("F" . helpful-function)
:map helpful-mode-map
("<normal-state><" . my/helpful-history-back)
("<normal-state>>" . my/helpful-history-forward))
:init
(defun my/-helpful-setup-emacs-lisp-mode ()
(setq-local evil-lookup-func #'helpful-at-point))
(defvar my/helpful-symbol-history-size 20
"Max size of `my/helpful-symbol-history'.")
(defvar my/helpful-symbol-history '()
"History of helpful symbols.")
(defun my/helpful-history-back (count)
"Go back COUNT symbols in `my/helpful-symbol-history'. If called
interactively, COUNT defaults to 1."
(interactive "p")
(my/helpful-history-forward (- count)))
(defun my/helpful-history-forward (count)
"Move COUNT symbols in `my/helpful-symbol-history'. If COUNT is negative,
move back. If COUNT is larger than the history, go to the newest entry. Go to
the oldest entry if -COUNT is larger than the history."
(interactive "p")
(when helpful--sym
(let* ((hist-len (length my/helpful-symbol-history))
(current-pos (seq-position my/helpful-symbol-history
(cons helpful--sym
helpful--callable-p)
'equal))
(new-pos (- current-pos count)))
(cond
;; if already at the newest element, signal an error
((and (> count 0) (= current-pos 0))
(message "%s" "No newer symbol!"))
;; if already at the oldest element, signal an error
((and (< count 0) (= (1+ current-pos) hist-len))
(message "%s" "No older symbol!"))
(t
(let ((entry (cond
((<= new-pos 0)
(seq-first my/helpful-symbol-history))
((>= new-pos hist-len)
(car (last my/helpful-symbol-history)))
(t
(nth new-pos my/helpful-symbol-history)))))
(if (cdr entry)
(helpful-callable (car entry))
(helpful-variable (car entry)))))))))
(defun my/-helpful-switch-buffer-function (helpful-buf)
"Like `pop-to-buffer', but kill previous helpful buffers and save the new
buffers `helpful--sym' to `my/helpful-symbol-history'."
(cl-loop for buf in (buffer-list)
with window = nil
with last-index = nil
when (and
(not (eq buf helpful-buf))
(eq (buffer-local-value 'major-mode buf) 'helpful-mode))
do
(when-let (cur-window (get-buffer-window buf nil))
(setq window cur-window))
(when-let (entry (buffer-local-value 'helpful--sym buf))
(setq last-index (seq-position my/helpful-symbol-history entry 'equal)))
(kill-buffer buf)
finally
(when-let ((entry (cons (buffer-local-value 'helpful--sym helpful-buf)
(buffer-local-value 'helpful--callable-p
helpful-buf)))
((not (seq-contains-p my/helpful-symbol-history entry 'equal))))
(when last-index
(setq my/helpful-symbol-history
(seq-drop my/helpful-symbol-history last-index)))
(push entry my/helpful-symbol-history)
(seq-take my/helpful-symbol-history my/helpful-symbol-history-size))
(if window
(window--display-buffer helpful-buf window 'reuse)
(pop-to-buffer helpful-buf))))
(setq helpful-switch-buffer-function 'my/-helpful-switch-buffer-function
helpful-max-buffers 2))
;; rainbow-delimiters ;; rainbow-delimiters
(use-package rainbow-delimiters (use-package rainbow-delimiters
:hook (prog-mode . rainbow-delimiters-mode)) :hook (prog-mode . rainbow-delimiters-mode))