From aa964254075f9963f3773be38ad6679e54f8e659 Mon Sep 17 00:00:00 2001 From: John Kitchin Date: Tue, 25 Jun 2024 11:04:25 -0400 Subject: [PATCH] make some things project aware --- org-db-fulltext.el | 26 +++++++++++++++++++------- org-db-images.el | 14 ++++++++++---- org-db.el | 34 +++++++++++++++++++++++----------- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/org-db-fulltext.el b/org-db-fulltext.el index 746979e8..774752aa 100644 --- a/org-db-fulltext.el +++ b/org-db-fulltext.el @@ -48,10 +48,15 @@ This does not use the arguments pased in update functions." QUERY should be in fts5 syntax. We use MATCH for the select. https://www.sqlite.org/fts5.html" (or - (ivy-more-chars) + (ivy-more-chars) (let ((org-db-ft (sqlite-open (expand-file-name org-db-fulltext org-db-root))) (statement (format "select snippet(fulltext, 1, '', '', '', 8), filename -from fulltext where contents match '%s'" query))) +from fulltext where contents match '%s'%s" + query + (if org-db-project-p + (format " and filename match '\"^%s\"'" (projectile-project-root)) + "")))) + (prog1 (cl-loop for (snippet fname) in (sqlite-select org-db-ft statement) @@ -61,8 +66,7 @@ from fulltext where contents match '%s'" query))) ;; the full text might have :: in it. collect ;; (cons snippet fname) - (format "%s :: %s" snippet fname) - ) + (format "%s :: %s" snippet fname)) (sqlite-close org-db-ft))))) @@ -75,10 +79,15 @@ from fulltext where contents match '%s'" query))) (search-forward snippet nil t))) -(defun org-db-fulltext-search () +(defvar org-db-project-p nil + "Boolean for limiting search to current project.") + +(defun org-db-fulltext-search (&optional project) "Search the fulltext database. -This function opens the file that matches your query string." - (interactive) +This function opens the file that matches your query string. +With optional prefix arg PROJECT limit query to current project." + (interactive "P") + (setq org-db-project-p project) (ivy-read "query: " #'org-db-fulltext-candidates :dynamic-collection t :action #'org-db-fulltext-open)) @@ -115,3 +124,6 @@ This function opens the file that matches your query string." (provide 'org-db-fulltext) ;;; org-db-fulltext.el ends here +;; Local Variables: +;; eval: (sem-mode) +;; End: diff --git a/org-db-images.el b/org-db-images.el index 20ae55e2..d6cc599b 100644 --- a/org-db-images.el +++ b/org-db-images.el @@ -72,13 +72,19 @@ This is much faster than putting it on the candidates at creation time" (ivy-configure 'org-db-images :display-transformer-fn #'org-db-candidate-transformer) -(defun org-db-images () +(defun org-db-images (&optional project) "Search the images database." - (interactive) + (interactive "P") (let* ((db-candidates (with-org-db - (sqlite-select org-db "select images.text, images.img_filename, images.position, files.filename + (sqlite-select + org-db + (format + "select images.text, images.img_filename, images.position, files.filename from images -inner join files on files.rowid = images.filename_id"))) +inner join files on files.rowid = images.filename_id%s" + (if project + (format " where files.filename like \"%s%%\"" (projectile-project-root)) + ""))))) (candidates (cl-loop for (text fname position ofile) in db-candidates collect (list (s-join " " (s-split "\n" text)) diff --git a/org-db.el b/org-db.el index 3faba527..f0e2f1d6 100644 --- a/org-db.el +++ b/org-db.el @@ -1275,13 +1275,19 @@ where properties.property = \"ADDRESS\"")))) ;; * org-db src-blocks -(defun org-db-src-blocks () - "Search src blocks." - (interactive) +(defun org-db-src-blocks (&optional project) + "Search src blocks. +Optional PROJECT prefix arg to limit to current project." + (interactive "P") (let* ((src-blocks (with-org-db - (sqlite-select org-db "select src_blocks.language, src_blocks.contents, -src_blocks.begin, files.filename from src_blocks inner join files on files.rowid = src_blocks.filename_id"))) + (sqlite-select + org-db + (format "select src_blocks.language, src_blocks.contents, +src_blocks.begin, files.filename from src_blocks inner join files on files.rowid = src_blocks.filename_id%s" + (if project + (format " where files.filename like \"%s%%\"" (projectile-project-root)) + ""))))) (candidates (cl-loop for (language contents begin filename) in src-blocks collect (list (format "%s: %s" language contents) :filename filename :begin begin)))) @@ -1293,15 +1299,21 @@ src_blocks.begin, files.filename from src_blocks inner join files on files.rowid ;; * org-db headings -(defun org-db-heading-candidates () +(defun org-db-heading-candidates (&optional project) "Return heading candidates completion." (let* ((headings (with-org-db - (sqlite-select org-db " + (sqlite-select + org-db + (format " select headlines.level, headlines.todo_keyword, headlines.title, headlines.tags, files.filename, headlines.begin, files.last_updated from headlines inner join files on files.rowid = headlines.filename_id -order by files.last_updated desc"))) +%s +order by files.last_updated desc" + (if project + (format "where files.filename like \"%s%%\"" (projectile-project-root)) + ""))))) (candidates (cl-loop for (level todo title tags filename begin last-updated) in headings collect (cons @@ -1347,10 +1359,10 @@ order by files.last_updated desc"))) ;;;###autoload -(defun org-db-headings () +(defun org-db-headings (&optional project) "Use ivy to open a heading with completion." - (interactive) - (let* ((candidates (org-db-heading-candidates))) + (interactive "P") + (let* ((candidates (org-db-heading-candidates project))) (ivy-read "heading: " candidates :action '(1