From 67fdc1c341f2a784940ff4f69bdcdbbb9a9639f7 Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Tue, 9 Jan 2024 21:32:22 -0800 Subject: [PATCH] Add helpful (with custom forward and back functions) --- init.el | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 97 insertions(+), 8 deletions(-) diff --git a/init.el b/init.el index d752f02..380263e 100644 --- a/init.el +++ b/init.el @@ -40,15 +40,15 @@ ;; basic stuff (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) ((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)) :init - (defun my/-emacs-lisp-mode-setup-evil-lookup () - (setq-local evil-lookup-func - #'my/describe-symbol-at-point)) + ;; (defun my/-emacs-lisp-mode-setup-evil-lookup () + ;; (setq-local evil-lookup-func + ;; #'my/describe-symbol-at-point)) (defun my/describe-symbol-at-point () "Calls `describe-symbol' on the return value of `form-at-point'." (interactive) @@ -108,7 +108,8 @@ ;; Some settings for programming (setq-default indent-tabs-mode nil - tab-width 4) + tab-width 4 + fill-column 80) ;; Tree sitter download locations (setq treesit-language-source-alist @@ -313,7 +314,9 @@ visual states." :bind (("C-," . embark-act) ("C-;" . embark-dwim) :map help-map - ("B" . embark-bindings)) + ("B" . embark-bindings) + :map embark-symbol-map + ("h" . helpful-symbol)) :init (setq embark-quit-after-action nil) (add-to-list 'display-buffer-alist @@ -500,8 +503,9 @@ visual states." ;; yasnippet (use-package yasnippet + :demand t :bind ("C-c s" . yas-expand) - :init + :config (yas-global-mode 1)) ;; project.el @@ -1154,6 +1158,91 @@ If no name is given, list all bookmarks instead." (mu4e t) (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 + ("<" . my/helpful-history-back) + (">" . 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 (use-package rainbow-delimiters :hook (prog-mode . rainbow-delimiters-mode))