From 049453597ec1324ff19da2c95cb0ca02448eb806 Mon Sep 17 00:00:00 2001 From: jao Date: Fri, 26 Jun 2026 19:51:07 +0100 Subject: jao-org-focus improvements, jao-words --- lib/doc/jao-org-focus.el | 81 ++++++++++-------------------------------------- 1 file changed, 17 insertions(+), 64 deletions(-) (limited to 'lib/doc/jao-org-focus.el') diff --git a/lib/doc/jao-org-focus.el b/lib/doc/jao-org-focus.el index 6fff238..c506f07 100644 --- a/lib/doc/jao-org-focus.el +++ b/lib/doc/jao-org-focus.el @@ -38,6 +38,11 @@ (string= (car jao-org-focus-siblings) (file-name-nondirectory (or (buffer-file-name) "")))) +(defun jao-org-focus--jump-to-title (file title) + (let* ((file (if (bufferp file) (buffer-file-name file) file)) + (link (format "[[%s::*%s]]" file title))) + (org-link-open-from-string link))) + (defun jao-org-focus-jump-to-sibling () (interactive) (when-let* ((title (save-excursion (jao-org-focus--header-title))) @@ -45,7 +50,7 @@ (cdr jao-org-focus-siblings) (car jao-org-focus-siblings))) (link (format "[[./%s::*%s]]" file title))) - (org-link-open-from-string link))) + (jao-org-focus--jump-to-title (format "./%s" file) title))) ;;; focus on subtree (defun jao-org-focus () @@ -56,7 +61,10 @@ When invoked on an indirect buffer, pops back to its base." (when (jao-org-focus--is-outline) (jao-org-focus-jump-to-sibling)) (if-let* ((b (get-buffer (or jao-org-focus--parent "")))) - (pop-to-buffer b) + (let ((title (save-excursion + (goto-char (point-min)) + (jao-org-focus--header-title)))) + (jao-org-focus--jump-to-title b title)) (when-let* ((title (jao-org-focus--header-title)) (parent (buffer-name)) (bname (format "%s [%s]" title parent))) @@ -91,12 +99,15 @@ sync." (with-current-buffer b (save-excursion (jao-org-focus-redisplay))))) -(defun jao-org-focus-list (&optional any-parent) +(defun jao-org-focus-list (&optional any-parent exclude) "List of buffers that are focusing on a subtree of this one or its parent." (let ((n (or jao-org-focus--parent (buffer-name)))) (seq-filter (lambda (b) (let ((p (buffer-local-value 'jao-org-focus--parent b))) - (and p (or any-parent (string= n p))))) + (and p + (or any-parent (string= n p)) + (or (not exclude) + (not (string= (buffer-name b) exclude)))))) (buffer-list)))) (defvar jao-org-focus--focused-history nil) @@ -106,69 +117,12 @@ sync." With arg, offer to switch to all children, regardless of their parent." (interactive "P") - (let ((fl (mapcar 'buffer-name (jao-org-focus-list arg)))) + (let ((fl (mapcar 'buffer-name (jao-org-focus-list arg (buffer-name))))) (unless fl (error "No focused children")) (pop-to-buffer (completing-read "Focused child: " fl nil t nil 'jao-org-focus--focused-history)))) -(defun jao-org-focus--count-words (raw-word-list) - (cl-loop with result = nil - for elt in raw-word-list - do (cl-incf (cdr (or (assoc elt result) - (first (push (cons elt 0) result))))) - finally return (sort result - (lambda (a b) (string< (car a) (car b)))))) - -(defun jao-org-focus--word-lines (buffer word) - (let ((lines nil) - (last-line nil) - (case-fold-search nil) - (rx (format "\\b%s\\b" (regexp-quote word)))) - (with-current-buffer buffer - (save-excursion - (goto-char (point-min)) - (while (and (re-search-forward rx nil t) (< (length lines) 12)) - (let* ((line (line-number-at-pos)) - (d (- line (or last-line line)))) - (when (<= d 10) - (when last-line - (push last-line lines) - (push line lines))) - (setq last-line (line-number-at-pos)))))) - (mapconcat #'number-to-string (nreverse lines) " "))) - -(defun jao-org-focus-word-stats () - (interactive) - (let* ((words (split-string (downcase (buffer-string)) "\\W+" t)) - (word-list (jao-org-focus--count-words words)) - (buffer (current-buffer))) - (with-current-buffer (get-buffer-create "*word-statistics*") - (erase-buffer) - (insert "| word | occurences | lines | - |-----------+------------|--------------------|\n") - (dolist (elt word-list) - (let* ((word (car elt)) - (count (cdr elt)) - (lines (jao-org-focus--word-lines buffer word))) - (insert (format "| %s | %d | %s |\n" word count lines)))) - (org-mode) - (indent-region (point-min) (point-max)) - (goto-char 5) - (next-line 2) - (org-table-next-field) - (org-table-sort-lines nil ?N))) - (pop-to-buffer "*word-statistics*")) - -(defvar jao-org-focus-consult-buffer-source - `(:name "Focus buffers" - :category jao-org-focus-buffers - :action switch-to-buffer - :hidden t - :narrow ,(cons ?o "focus") - :history jao-org-focus--focused-history - :items ,(lambda () (mapcar 'buffer-name (jao-org-focus-list t))))) - (define-minor-mode org-focus-mode "A mode where keeping track of focused children is on by default." :lighter " ◎" @@ -176,8 +130,7 @@ With arg, offer to switch to all children, regardless of their parent." ("\C-cl" . jao-org-focus-switch) ("\C-cR" . jao-org-focus-redisplay) ("\C-co" . jao-org-focus) - ("\C-cw" . count-words) - ("\C-cW" . jao-org-focus-word-stats)) + ("\C-cw" . count-words)) (if org-focus-mode (add-hook 'after-save-hook #'jao-org-focus-redisplay-children nil t) (remove-hook 'after-save-hook #'jao-org-focus-redisplay-children t))) -- cgit v1.2.3