commit cfed349db16f04bf4215fc3a8eac6ca19676c568
parent a80d437444365c6a7a03ed82fc5bacb64efd8bce
Author: Lou Woell <lou.woell@posteo.de>
Date:   Sat, 13 Sep 2025 23:53:36 +0200

[haredoc.el] basic back/forw buttons

Diffstat:
Mharedoc.el | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 63 insertions(+), 21 deletions(-)

diff --git a/haredoc.el b/haredoc.el @@ -66,10 +66,17 @@ (buffer-file-name))))) (define-button-type 'haredoc - 'follow-link t 'action #'haredoc/button-action 'help-echo (purecopy "mouse-2, RET: describe this")) +(define-button-type 'haredoc-back + 'action #'haredoc/back + 'help-echo (purecopy "go back")) + +(define-button-type 'haredoc-forw + 'action #'haredoc/forward + 'help-echo (purecopy "go forward")) + (defun haredoc/button-action (button) (let* ((name (button-get button :args)) (module (button-get button :module)) @@ -79,11 +86,29 @@ (haredoc target))) (defun haredoc/make-button (match-number module) - (make-button (match-beginning match-number) - (match-end match-number) - :type 'haredoc - :module module - :args (match-string match-number))) + (make-text-button (match-beginning match-number) + (match-end match-number) + :type 'haredoc + :module module + :args (match-string match-number))) + +(defvar haredoc-backward-stack '()) +(defvar haredoc-forward-stack '()) +(defvar haredoc-stack-item nil) + +(defun haredoc/back (&optional args) + (interactive) + (when-let ((item (pop haredoc-backward-stack))) + (push haredoc-stack-item haredoc-forward-stack) + (setq haredoc-stack-item nil) + (haredoc item))) + +(defun haredoc/forward (&optional args) + (interactive) + (when-let ((item (pop haredoc-forward-stack))) + (push haredoc-stack-item haredoc-backward-stack) + (setq haredoc-stack-item nil) + (haredoc item))) ;; Shows haredoc for the selected or word. ;;;###autoload @@ -101,21 +126,35 @@ the results in a new buffer." (col (string-to-number (nth 2 location))) (module? (= col line 0)) (namespace (if module? current-word - (string-join (butlast components) "::")))) - (with-current-buffer-window "*haredoc*" - 'display-buffer-pop-up-window nil - (insert "\033[1;4m" current-word "\033[0m\n\n") - (with-environment-variables - (("HAREPATH" (car (process-lines haredoc-helperbin - "-p" (haredoc/root-dir) - "-P")))) - (call-process "haredoc" nil t nil "-a" "-F" "tty" current-word)) - (ansi-color-apply-on-region (point-min) (point-max)) - (save-excursion - (goto-char (point-min)) - (while (re-search-forward "\\[\\[\\(.*?\\)\\]\\]" nil t) - (haredoc/make-button 1 namespace))) - (haredoc-mode)))) + (string-join (butlast components) "::"))) + (buffer (get-buffer-create "*haredoc*"))) + (when haredoc-stack-item + (push haredoc-stack-item haredoc-backward-stack) + (setq haredoc-forward-stack nil)) + (setq haredoc-stack-item current-word) + (let ((inhibit-read-only t)) + (with-current-buffer buffer + (erase-buffer) + ;; 'display-buffer-same-window nil + (insert "\033[1;4m" current-word "\033[0m\n\n") + (with-environment-variables + (("HAREPATH" (car (process-lines haredoc-helperbin + "-p" (haredoc/root-dir) + "-P")))) + (call-process "haredoc" nil t nil "-a" "-F" "tty" current-word)) + (ansi-color-apply-on-region (point-min) (point-max)) + (save-excursion + (goto-char (point-min)) + (while (re-search-forward "\\[\\[\\(.*?\\)\\]\\]" nil t) + (haredoc/make-button 1 namespace))) + (ensure-empty-lines 2) + (when haredoc-backward-stack + (insert-text-button "[back]" :type 'haredoc-back) + (insert "\t")) + (when haredoc-forward-stack + (insert-text-button "[forw]" :type 'haredoc-forw)) + (haredoc-mode) + (display-buffer buffer))))) ;;;###autoload (defun haredoc/describe-thing-at-point () @@ -149,5 +188,8 @@ the results in a new buffer." (define-derived-mode haredoc-mode help-mode "🐇") +(define-key haredoc-mode-map (kbd "b") 'haredoc/back) +(define-key haredoc-mode-map (kbd "f") 'haredoc/forward) + (provide 'haredoc) ;;; haredoc.el ends here