summaryrefslogtreecommitdiffhomepage
path: root/lib/doc/jao-org-notes.el
diff options
context:
space:
mode:
Diffstat (limited to 'lib/doc/jao-org-notes.el')
-rw-r--r--lib/doc/jao-org-notes.el70
1 files changed, 48 insertions, 22 deletions
diff --git a/lib/doc/jao-org-notes.el b/lib/doc/jao-org-notes.el
index d3f18b8..4ec0221 100644
--- a/lib/doc/jao-org-notes.el
+++ b/lib/doc/jao-org-notes.el
@@ -35,7 +35,7 @@
(defun jao-org-notes--rg-cmd (rgx &rest args)
`("rg" "--null" "--line-buffered" "--color=never" "--max-columns=250"
- "--type=org" "--line-number" "--no-heading" "--smart-case"
+ "--type=org" "--line-number" "--no-heading" "--smart-case" "--follow"
,@args ,default-directory "-e" ,rgx))
(defun jao-org-notes--rg-title-or-tags (str)
@@ -45,7 +45,8 @@
(default-directory
(if (file-directory-p d) (expand-file-name d) default-directory))
(ts (mapconcat #'identity (split-string str "[:,]+" t) ":|"))
- (rgx (format "^#.(title: .*%s|(tags:.*(%s:)))" str ts)))
+ (s (mapconcat #'identity (cons str (split-string str nil t)) "|"))
+ (rgx (format "^#.(title: .*(%s)|(tags:.*(%s:)))" s ts)))
(jao-org-notes--rg-cmd rgx "-m" "2")))
(defun jao-org-notes--clean-match (m)
@@ -57,7 +58,11 @@
(string-to-number (cadr m))))
(defun jao-org-notes--matches (lines)
- (mapcar (lambda (l) (jao-org-notes--clean-match (split-string l "\0" t))) lines))
+ (seq-keep (lambda (l)
+ (let ((m (split-string (or l "") "\0" t)))
+ (when (and (car m) (cadr m))
+ (jao-org-notes--clean-match m))))
+ lines))
(defun jao-org-notes--grep-rx (rx &rest rg-args)
(let ((default-directory jao-org-notes-dir))
@@ -88,7 +93,7 @@
:history '(:input jao-org-notes--grep-history))))
(defun jao-org-notes-cats ()
- (seq-difference (directory-files jao-org-notes-dir) '("." ".." "attic")))
+ (directory-files jao-org-notes-dir nil "^[^.]"))
(defun jao-org-notes--cat ()
(let* ((cat (completing-read "Top level category: " (jao-org-notes-cats))))
@@ -118,7 +123,8 @@
(defvar jao-org-notes--tag-history nil)
(defun jao-org-notes--read-tags ()
- (let* ((tags (completing-read-multiple "Tags: " jao-org-notes--tags nil nil nil
+ (let* ((tags (completing-read-multiple "Tags: "
+ jao-org-notes--tags nil nil nil
'jao-org-notes--tag-history)))
(setq jao-org-notes--tags (seq-union jao-org-notes--tags tags #'string=))
tags))
@@ -138,7 +144,8 @@
(let* ((file-name (or file-name buffer-file-name))
(bn (file-name-base file-name))
(rx (format "%s\\.org$" (regexp-quote bn)))
- (pred (lambda () (string-prefix-p jao-org-notes-dir buffer-file-name))))
+ (pred (lambda ()
+ (string-prefix-p jao-org-notes-dir buffer-file-name))))
(save-some-buffers nil pred)
(or (car (directory-files-recursively jao-org-notes-dir rx))
(let* ((d (completing-read "Notes subdir: " (jao-org-notes-cats) nil t))
@@ -146,32 +153,51 @@
(expand-file-name (concat d bn ".org") jao-org-notes-dir)))))
(defun jao-org-notes-open ()
- "Search for a note file, matching tags and titles with completion."
+ "Search for a note file, matching tags and titles with completion.
+
+If the search string starts with a folder name followed by a
+slash (e.g. `emacs/foo'), the search is performed only in the given
+notes folder (when it exists).
+
+Search terms that start with a colon are interpreted as tags, and can
+appear in any order in the tags list.
+"
(interactive)
- (when-let (f (jao-org-notes--consult-rg "Search notes: "))
+ (when-let (f (jao-org-notes--consult-rg "Search tags and titles: "))
(find-file f)))
+(defvar jao-org-notes-consult-ripgrep-args
+ (format "%s -L" consult-ripgrep-args))
+
+(defun jao-org-notes-consult-ripgrep (&optional ask initial)
+ "Search over notes in given notes categories (or all of them), using rg."
+ (interactive "P")
+ (let ((cat (when ask (completing-read "Category: " (jao-org-notes-cats))))
+ (consult-ripgrep-args jao-org-notes-consult-ripgrep-args))
+ (consult-ripgrep (expand-file-name (or cat "") jao-org-notes-dir) initial)))
+
+(defun jao-org-notes-consult-ripgrep-category (&optional initial)
+ "Search over notes in a given notes category."
+ (interactive "P")
+ (jao-org-notes-consult-ripgrep t initial))
+
(defun jao-org-notes-consult-tags ()
"Search for a note file, matching all tags with completion."
(interactive)
(let* ((tags (jao-org-notes--read-tags))
(init (concat "^..tags: " (mapconcat #'identity tags " "))))
- (consult-ripgrep jao-org-notes-dir init)))
-
-(defun jao-org-notes-consult-ripgrep (&optional initial cat)
- (interactive)
- (consult-ripgrep (expand-file-name (or cat "") jao-org-notes-dir) initial))
+ (jao-org-notes-consult-ripgrep init)))
-(defun jao-org-notes-create (&optional title)
+(defun jao-org-notes-create (&optional title ret-buffer)
"Create a new note file, matching tags and titles with completion."
(interactive)
(when (jao-org-notes--insert-title title)
- (org-insert-time-stamp (current-time) t t "#+date: " "\n")
(insert "#+tags: :"
(mapconcat #'identity (jao-org-notes--read-tags) ":")
- ":\n"))
+ ":\n")
+ (org-insert-time-stamp (current-time) t t "#+date: " "\n"))
(save-buffer)
- (current-buffer))
+ (if ret-buffer (current-buffer) (buffer-file-name)))
(defun jao-org-notes-backlinks ()
"Show a list of note files linking to the current one."
@@ -205,15 +231,15 @@
(interactive)
(message "%d notes, %d tags in %s"
(length (jao-org-notes-list))
- (length jao-org--notes-tags)
+ (length jao-org-notes--tags)
jao-org-notes-dir))
;;;###autoload
(defun jao-org-notes-setup (mnemonic)
- "Set up the notes system, providing a mnemonic character for its org template."
- (setq org-capture-templates
- (add-to-list 'org-capture-templates (jao-org-notes--template mnemonic))
- jao-org-notes--tags (jao-org-notes-all-tags))
+ "Set up the notes system, providing a mnemonic char for its org template."
+ (require 'org-capture)
+ (add-to-list 'org-capture-templates (jao-org-notes--template mnemonic))
+ (setq jao-org-notes--tags (jao-org-notes-all-tags))
(when (fboundp 'org-capture-upgrade-templates)
(org-capture-upgrade-templates org-capture-templates)))