From 57e9fa1051202bd926f373c9653c6a218f5a74ca Mon Sep 17 00:00:00 2001 From: Howard Abrams Date: Thu, 6 Jun 2024 23:04:37 -0700 Subject: [PATCH] Experimental jump to a hamacs headline This might make it much easiler to keep code in the right place. --- bootstrap.org | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ ha-general.org | 11 ++++++----- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/bootstrap.org b/bootstrap.org index 8698bcb..5b00265 100644 --- a/bootstrap.org +++ b/bootstrap.org @@ -243,6 +243,56 @@ And the ability to edit the file: (find-file full-file))) #+end_src +Why not edit a file, but select the file based on the header. +#+begin_src emacs-lisp + (defun ha-hamacs-process-heading (rg-input) + "Return list of heading, file and line number. + Parses the line entry, RG-INPUT, from a call to `rg'. + Returns something like: + + (\"Some Heading\" \"some-file.org\" 42)" + (let* ((parts (string-split rg-input ":")) + (file (first parts)) + (lnum (string-to-number (second parts))) + (head (thread-first parts + (third) + (substring 1))) + (disp (string-replace "*" " " head))) + (list disp file lnum))) + + (defun ha-hamacs-filter-heading (rg-input) + "Return non-nil if we should remove RG-INPUT. + These are headings with typical, non-unique entries, + like Introduction and Summary." + (string-match (rx (or " Introduction" + " Install" + " Summary" + " Technical Artifacts")) + rg-input)) + + (defun ha-hamacs-edit-file-heading () + "Edit a file based on a particular heading. + After presenting list of headings from all Org files, + it loads the file, and jumps to the line number where + the heading is located." + (interactive) + (let* ((default-directory hamacs-source-dir) + (file-head-list + (thread-last (concat "rg" + " --no-heading" + " --line-number" + " --max-depth 1" + " --type org" + " -e '^\\*+ '") + (shell-command-to-list) + (seq-remove 'ha-hamacs-filter-heading) + (seq-map 'ha-hamacs-process-heading))) + (file-choice (completing-read "Edit Heading: " file-head-list)) + (file-tuple (alist-get file-choice file-head-list + nil nil 'string-equal))) + (find-file (first file-tuple)) + (goto-line (second file-tuple)))) +#+end_src And this similar function, will /tangle/ one of my files. Notice that in order to increase the speed of the tangling process (and not wanting to pollute a project perspective), I use a /temporary buffer/ instead of =find-file=. #+begin_src emacs-lisp diff --git a/ha-general.org b/ha-general.org index 8c376ff..388985e 100644 --- a/ha-general.org +++ b/ha-general.org @@ -186,10 +186,11 @@ And ways to load my tangled org-files: (ha-leader "h h" '(:ignore t :which-key "hamacs") "h h " '(keyboard-escape-quit :which-key t) - "h h f" '("features" . ha-hamacs-features) - "h h e" '("edit" . ha-hamacs-find-file) - "h h h" '("reload" . ha-hamacs-load) - "h h a" '("reload all" . ha-hamacs-reload-all)) + "h h f" '("features" . ha-hamacs-features) + "h h e" '("edit" . ha-hamacs-find-file) + "h h j" '("heading jump" . ha-hamacs-edit-file-heading) + "h h h" '("reload" . ha-hamacs-load) + "h h a" '("reload all" . ha-hamacs-reload-all)) #+end_src * File Operations While =find-file= is still my bread and butter, I like getting information about the file associated with the buffer. For instance, the file path: @@ -821,7 +822,7 @@ The [[https://github.com/oantolin/embark/][embark]] project offers /actions/ on #+begin_src emacs-lisp (use-package embark :bind - (("s-." . embark-act) ; Work in minibuffer and elsewhere + (("s-'" . embark-act) ; Work in minibuffer and elsewhere ("s-/" . embark-dwim)) :init