From db2d27b66c4a8b4bf81fd0f31e856048c2c3557a Mon Sep 17 00:00:00 2001 From: Howard Abrams Date: Thu, 12 May 2022 09:40:35 -0700 Subject: [PATCH] Goto definitions written in org files Got a hacky approach to jumping to function definitions that are written in an literate org file using ripgrep. Hopefully, this will work well. --- ha-programming-elisp.org | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/ha-programming-elisp.org b/ha-programming-elisp.org index bfcafe0..d871f19 100644 --- a/ha-programming-elisp.org +++ b/ha-programming-elisp.org @@ -48,7 +48,7 @@ Show code examples with the [[https://github.com/xuchunyang/elisp-demos][elisp-d #+END_SRC * Navigation and Editing ** Goto Definitions -F’s [[https://github.com/Wilfred/elisp-def][elisp-def]] project does a better job at jumping to the definition of a symbol at the point, so: +Wilfred’s [[https://github.com/Wilfred/elisp-def][elisp-def]] project does a better job at jumping to the definition of a symbol at the point, so: #+BEGIN_SRC emacs-lisp (use-package elisp-def :hook (emacs-lisp-mode . elisp-def-mode)) @@ -59,7 +59,31 @@ This /should work/ with [[help:evil-goto-definition][evil-goto-defintion]], as t - [[help:evil-goto-definition-xref][evil-goto-definition-xref]] … and here is where this package will be called - [[help:evil-goto-definition-search][evil-goto-definition-search]] -I love packages that add functionality but I don’t have to learn anything. +I love packages that add functionality but I don’t have to learn anything. However, I’m running into an issue where I do a lot of my Emacs Lisp programming in org files, and would like to jump to the function definition where it is defined in the org file. Since [[https://github.com/BurntSushi/ripgrep][ripgrep]] is pretty fast, I’ll call it instead of attempting to build a [[https://stackoverflow.com/questions/41933837/understanding-the-ctags-file-format][CTAGS]] table. Oooh, the =rg= takes a =—json= option, which makes it easier to parse. + +#+BEGIN_SRC emacs-lisp + (defun ha-org-code-block-jump (str pos) + "Go to a literate org file containing a symbol, STR. + The POS is ignored." + (ignore-errors + (let* ((default-directory (projectile-project-root)) + (command (format "rg --json '\\(def[^ ]+ %s ' *.org" str)) + (results (->> command + shell-command-to-list + second + json-parse-string)) + (file (->> results + (gethash "data") + (gethash "path") + (gethash "text"))) + (line (->> results + (gethash "data") + (gethash "line_number")))) + (find-file file) + (goto-line line)))) + + (add-to-list 'evil-goto-definition-functions ' ha-org-code-block-jump) +#+END_SRC ** Clever Parenthesis We need to make sure we keep the [[https://github.com/Fuco1/smartparens][smartparens]] project always in /strict mode/, because who wants to worry about paren-matching: #+BEGIN_SRC emacs-lisp