-
Notifications
You must be signed in to change notification settings - Fork 4
/
org-z-helm.el
118 lines (98 loc) · 4.65 KB
/
org-z-helm.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
;;; org-z-helm.el --- Helm 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
;; Keywords: org-mode
;; Version: 0.0.3
;; Package-Requires: ((emacs "27.1") (org-z "0.0.2") (org-ql "0.6-pre") (helm "3.3") (helm-org "1.0") (helm-org-ql "0.6-pre"))
;; 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:
;; Helm 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 'helm-org-ql)
(defun org-z-insert-link--fallback ()
(helm-build-dummy-source "Create link to new heading"
:action (helm-make-actions
"Insert link to new heading"
#'org-z--insert-link-to-new-heading)))
(defun org-z-helm-candidate-transformer (candidates _)
(cond ((and org-z-use-prescient (fboundp 'prescient-sort-compare))
(sort candidates (lambda (c1 c2)
(prescient-sort-compare (car c1) (car c2)))))
(org-z-use-prescient
(user-error "org-z-use-prescient is t but prescient isn't loaded. Make sure prescient is installed and required."))
(t
candidates)))
(defun org-z-helm-org-ql-source (buffers-files)
(helm-make-source "org-z" 'helm-source-sync
:candidates (lambda ()
(let* ((query (org-ql--query-string-to-sexp helm-pattern))
(window-width (window-width (helm-window))))
(when query
(with-current-buffer (helm-buffer-get)
(setq helm-org-ql-buffers-files buffers-files))
;; Ignore errors that might be caused by partially typed queries.
(ignore-errors
(mapcar (lambda (candidate)
(let ((point-marker (get-text-property 0 'point-marker candidate)))
(cons candidate point-marker)))
(org-ql-select buffers-files query
:action `(org-z--format-org-ql-heading ,window-width)))))))
:match #'identity
:filtered-candidate-transformer #'org-z-helm-candidate-transformer
:fuzzy-match nil
:multimatch nil
:nohighlight t
:volatile t
:keymap helm-org-ql-map
:action helm-org-ql-actions))
(defun org-z-helm-select-action-hook ()
(let ((selection (helm-get-selection nil t)))
(cond
((and org-z-use-prescient (fboundp 'prescient-remember))
(prescient-remember selection))
(org-z-use-prescient
(user-error "org-z-use-prescient is t but prescient isn't loaded. Make sure prescient is installed and required.")))))
(cl-defstruct org-z--helm-backend)
(cl-defmethod org-z--insert-link ((_ org-z--helm-backend))
(when (not (fboundp 'helm))
(user-error "org-z-completion-backend is 'helm but helm isn't loaded. Make sure helm is installed and required."))
(require 'helm-org-ql)
(let* ((helm-candidate-separator " ")
(_ (add-to-list 'helm-org-ql-actions '("Insert link" . org-z--insert-link-to-candidate)))
(org-sources (list (org-z-helm-org-ql-source (org-ql-search-directories-files :directories org-z-directories)))))
(add-hook 'helm-before-action-hook #'org-z-helm-select-action-hook)
(helm
:input (thing-at-point 'symbol 'no-properties)
:sources (append org-sources (list (org-z-insert-link--fallback)))
:buffer "*org-z-insert-link*")
(pop helm-before-action-hook)
(pop helm-org-ql-actions)))
(setq org-z-completion-backends (plist-put org-z-completion-backends 'helm (make-org-z--helm-backend)))
(setq org-z-completion-backend 'helm)
(provide 'org-z-helm)
;;; org-z-helm.el ends here