forked from alphapapa/org-ql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg-ql-find.el
135 lines (112 loc) · 5.23 KB
/
org-ql-find.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
;;; org-ql-find.el --- Find headings with completion using org-ql -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Adam Porter
;; Author: Adam Porter <adam@alphapapa.net>
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This library provides a way to quickly find and go to Org entries
;; selected with Emacs's built-in completions API (so it works with
;; packages that extend it, like Vertico, Marginalia, etc). It works
;; like `helm-org-ql' but does not require Helm.
;;; Code:
(require 'cl-lib)
(require 'org)
(require 'org-ql)
(require 'org-ql-search)
(require 'org-ql-completing-read)
;;;; Customization
(defgroup org-ql-find nil
"Options for `org-ql-find'."
:group 'org-ql)
(defcustom org-ql-find-goto-hook '(org-show-entry org-reveal)
"Functions called when selecting an entry."
:type 'hook)
(defcustom org-ql-find-display-buffer-action '(display-buffer-same-window)
"Display buffer action list for `org-ql-find'.
See function `display-buffer'."
:type 'sexp)
;;;; Functions
;;;###autoload
(cl-defun org-ql-find (buffers-files &key query-prefix query-filter
(prompt "Find entry: "))
"Go to an Org entry in BUFFERS-FILES selected by searching entries with `org-ql'.
Interactively, with universal prefix, select multiple buffers to
search with completion and PROMPT.
QUERY-PREFIX may be a string to prepend to the query (e.g. use
\"heading:\" to only search headings, easily creating a custom
command that saves the user from having to type it).
QUERY-FILTER may be a function through which the query the user
types is filtered before execution (e.g. it could replace spaces
with commas to turn multiple tokens, which would normally be
treated as multiple predicates, into multiple arguments to a
single predicate)."
(interactive
(list (if current-prefix-arg
(mapcar #'get-buffer
(completing-read-multiple
"Buffers: "
(cl-loop for buffer in (buffer-list)
when (eq 'org-mode (buffer-local-value 'major-mode buffer))
collect (buffer-name buffer))
nil t))
(progn
(unless (eq major-mode 'org-mode)
(user-error "This is not an Org buffer: %S" (current-buffer)))
(current-buffer)))))
(let ((marker (org-ql-completing-read buffers-files
:query-prefix query-prefix
:query-filter query-filter
:prompt prompt)))
(with-current-buffer (marker-buffer marker)
(goto-char marker)
(display-buffer (current-buffer) org-ql-find-display-buffer-action)
(select-window (get-buffer-window (current-buffer)))
(run-hook-with-args 'org-ql-find-goto-hook))))
;;;###autoload
(defun org-ql-refile (marker)
"Refile current entry to MARKER (interactively, one selected with `org-ql').
Interactive completion uses files listed in `org-refile-targets',
which see (but only the files are used)."
(interactive (let ((buffers-files (delete-dups
;; Always include the current buffer.
(cons (current-buffer)
(cl-loop for (files-spec . _candidate-spec) in org-refile-targets
append (cl-typecase files-spec
(null (list (current-buffer)))
(symbol (pcase (funcall files-spec)
((and (pred stringp) file) (list file))
((and (pred listp) files) files)))
(list files-spec)))))))
(list (org-ql-completing-read buffers-files :prompt "Refile to: "))))
(org-refile nil nil
;; The RFLOC argument:
(list
;; Name
(org-with-point-at marker
(nth 4 (org-heading-components)))
;; File
(buffer-file-name (marker-buffer marker))
;; nil
nil
;; Position
marker)))
;;;###autoload
(defun org-ql-find-in-agenda ()
"Call `org-ql-find' on `org-agenda-files'."
(interactive)
(org-ql-find (org-agenda-files)))
;;;###autoload
(defun org-ql-find-in-org-directory ()
"Call `org-ql-find' on files in `org-directory'."
(interactive)
(org-ql-find (org-ql-search-directories-files)))
(provide 'org-ql-find)
;;; org-ql-find.el ends here