commit 91fd902ab27fbc7dbaa2bfbf73e3e69999456262
parent e7e800b73d28b336ba2d5b86bbe1e0126ab0d35d
Author: Lou Woell <lou.woell@posteo.de>
Date:   Sat, 13 Sep 2025 01:54:34 +0200

[haredoc.el] simplify

Diffstat:
Mharedoc.el | 85+++++++++++++++++++++++++++++++++++++------------------------------------------
1 file changed, 40 insertions(+), 45 deletions(-)

diff --git a/haredoc.el b/haredoc.el @@ -34,8 +34,8 @@ (require 'thingatpt) (require 'ansi-color) -(defvar haredoc-helperbindir (f-dirname (macroexp-file-name))) -(defvar haredoc-helperbin (concat haredoc-helperbindir "/harehelper")) +(defconst haredoc-helperbindir (f-dirname (macroexp-file-name))) +(defconst haredoc-helperbin (concat haredoc-helperbindir "/harehelper")) (defun haredoc/root-dir () (interactive) @@ -43,32 +43,24 @@ (project-root p) ".")) -(defun haredoc/exec (strings) - (let ((res (shell-command-to-string (string-join strings " ")))) - (if (string= res "") nil res))) - (defun haredoc/get-modules () "Return list of Hare modules." - (string-split (haredoc/exec (list haredoc-helperbin "-p" (haredoc/root-dir) - "list-modules")) "\n")) + (process-lines haredoc-helperbin "-p" (haredoc/root-dir) "list-modules")) (defun haredoc/read-module (&optional init) (completing-read "Pick Module: " (haredoc/get-modules) nil nil init)) (defun haredoc/identifier-at-point () - (let ((id (cond - ((use-region-p) ;; Use region or the current word - (buffer-substring-no-properties (region-beginning) - (region-end))) - (t (progn - (thing-at-point-looking-at - "\\**\\(\\(\\(\\sw\\|\\s_\\)+::\\)*\\(\\sw\\|\\s_\\)+\\)") - (match-string 1)))))) + (let ((id (if (use-region-p) ;; Use region or the current word + (buffer-substring-no-properties (region-beginning) + (region-end)) + (thing-at-point-looking-at + "\\**\\(\\(\\(\\sw\\|\\s_\\)+::\\)*\\(\\sw\\|\\s_\\)+\\)") + (match-string 1)))) ;; resolve identifier - (or (haredoc/exec (list haredoc-helperbin "-p" (haredoc/root-dir) - "resolve" id - (buffer-file-name))) - id))) + (car (process-lines haredoc-helperbin "-p" (haredoc/root-dir) + "resolve" id + (buffer-file-name))))) ;; Shows haredoc for the selected or word. ;;;###autoload @@ -76,44 +68,47 @@ "Run haredoc with the current word under the cursor and display the results in a new buffer." (interactive) - (let* ((current-word (cond (sign sign) - (t (haredoc/read-module)))) - (output (with-environment-variables - (("HAREPATH" (haredoc/exec (list haredoc-helperbin - "-p" (haredoc/root-dir) - "-P")))) - (haredoc/exec - (list "haredoc" "-a" "-F" "tty" current-word)))) - (buffer-name "*haredoc*")) - (with-output-to-temp-buffer buffer-name - (with-current-buffer buffer-name - (insert "\033[1;4m" current-word "\033[0m\n\n") - (insert output) - (ansi-color-apply-on-region (point-min) (point-max)) - (haredoc-mode))))) + (let ((current-word (or sign (haredoc/read-module)))) + (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)) + (haredoc-mode)))) ;;;###autoload (defun haredoc/describe-thing-at-point () (interactive) (haredoc (haredoc/identifier-at-point))) +(defun haredoc/get-location (name) + (catch 'exit + (car (process-lines-handling-status + haredoc-helperbin + (lambda (x) (unless (= x 0) + (message "Could not find %s" name) + (throw 'exit nil))) + "-p" (haredoc/root-dir) + "locate" name)))) + ;;;###autoload (defun haredoc/goto-defintion () (interactive) (when-let* ((name (haredoc/identifier-at-point)) - (loc (split-string (haredoc/exec (list haredoc-helperbin "-p" - (haredoc/root-dir) - "locate" name)) - ":")) + (loc (haredoc/get-location name)) + (loc (split-string loc ":")) (file (nth 0 loc)) (line (string-to-number (nth 1 loc))) (col (string-to-number (nth 2 loc)))) - (when (not (string= file "")) - (with-current-buffer (find-file file) - (goto-char (point-min)) - (forward-line (1- line)) - (forward-char col) - (forward-to-word))))) + (with-current-buffer (find-file file) + (goto-char (point-min)) + (forward-line (1- line)) + (forward-char col) + (forward-to-word)))) (define-derived-mode haredoc-mode help-mode "🐇")