-
Notifications
You must be signed in to change notification settings - Fork 3
/
org-z-selectrum.el
82 lines (66 loc) · 3.58 KB
/
org-z-selectrum.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
;;; org-z-selectrum.el --- Selectrum completion backend for org-z -*- coding: utf-8; lexical-binding: t; -*-
;; Copyright (C) 2020 Mark Hudnall <me@markhudnall.com>
;; Author: Mark Hudnall <me@markhudnall.com>
;; URL: https://github.com/landakram/org-z
;; Version: 0.0.4
;; Package-Requires: ((emacs "27.1") (org-z "0.0.4") (org-ql "0.6-pre") (selectrum "3.0"))
;; Keywords: org-mode, outlines
;; This file is NOT part of GNU Emacs.
;; 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, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Selectrum completion backend for org-z. This is an extra package, since org-z.el doesn't depend
;; on a specific completion backend.
;;; Code:
(require 'cl-lib)
(require 'cl-generic)
(require 'org)
(require 'org-id)
(require 'org-capture)
(require 'org-z)
(require 'org-ql)
(require 'org-ql-search)
(require 'selectrum)
(cl-defstruct org-z--selectrum-backend)
(cl-defmethod org-z--insert-link ((_ org-z--selectrum-backend))
(when (not (fboundp 'selectrum--read))
(user-error "org-z-completion-backend is 'selectrum but selectrum isn't loaded. Make sure selectrum is installed and required."))
(let* ((buffers-files (org-ql-search-directories-files :directories org-z-directories))
(candidate-fn (lambda (pattern)
(let* ((query (org-ql--query-string-to-sexp pattern))
(window-width (window-text-width)))
(when query
(ignore-errors
;; Ignore errors that might be caused by partially typed queries.
(org-ql-select buffers-files query
:action `(org-z--format-org-ql-heading ,window-width)))))))
(result
;; This is sort of an ugly hack. selectrum--read refines candidates using `completion-styles', which filters
;; the org-ql query further by the input. We're already effectively handling our own filtering in the candidate-fn
;; so if the user has default selectrum (they are not using e.g. prescient), we ignore `selectrum-refine-candidates-function'
;; for the completion
(let ((selectrum-refine-candidates-function
(if (eq selectrum-refine-candidates-function #'selectrum-refine-candidates-using-completions-styles)
(lambda (input cands) cands)
selectrum-refine-candidates-function)))
(selectrum--read "Insert link: " candidate-fn
:initial-input (thing-at-point 'symbol 'no-properties)))))
(if-let ((point-marker (get-text-property 0 'point-marker result)))
(org-z--insert-link-to-candidate point-marker)
(org-z--insert-link-to-new-heading result))))
(setq org-z-completion-backends (plist-put org-z-completion-backends 'selectrum (make-org-z--selectrum-backend)))
(setq org-z-completion-backend 'selectrum)
(provide 'org-z-selectrum)
;;; org-z-selectrum.el ends here