-
-
Notifications
You must be signed in to change notification settings - Fork 123
272 lines (224 loc) · 6.9 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
;;; [email protected] --- Scimax support for links
;;; Commentary:
;; This is some idea to bind @ to insert links to convenient things.
;; - buffer headings
;; - open headings
;; - recentf
;; - projects
;; - org-db-headings?
;; - contacts
;;
;; it is an experiment inspired by a feature in Google docs that prompts you
;; with a dropdown list of things to insert with @.
;;
;; The main function is `@-insert-link' and when you load this library, that is
;; bound to the @ key.
(require 'org-db)
(require 'org-ref)
(defcustom scimax-links-candidate-functions
'(@-links-this-buffer
@-links-org-buffers
@-links-open-files
@-hashtags
@-links-stored-links
@-counsel-recentf-candidates
@-projectile-relevant-known-projects
org-db-contacts-candidates)
"List of functions that generate candidates"
:group 'scimax-@-link
:type '(repeat function))
(defcustom scimax-insert-link-functions
`(counsel-find-file
counsel-recentf
counsel-projectile-switch-project
,org-ref-insert-cite-function
org-db-contacts)
"Other commands with link actions."
:group 'scimax-@-link
:type '(repeat function))
;; * Augment some projectile functions with links
(defun scimax-projectile-insert-file-link (f)
"Insert a file link to F in a project.
F is going to be a string that is a filename or directory."
(let ((fname (expand-file-name f (projectile-project-root))))
(when (file-exists-p fname)
(with-ivy-window
(insert (format "[[file:%s][%s]]"
fname
f))))))
(ivy-add-actions
'counsel-projectile-find-file
'(("l" scimax-projectile-insert-file-link "Insert link")))
(ivy-add-actions
'counsel-projectile-find-dir
'(("l" scimax-projectile-insert-file-link "Insert link")))
(ivy-add-actions
'counsel-projectile-switch-project
'(("l" scimax-projectile-insert-file-link "Insert link")))
(ivy-add-actions
'counsel-projectile
'(("l" scimax-projectile-insert-file-link "Insert link")))
(ivy-add-actions
'counsel-recentf
'(("l" scimax-projectile-insert-file-link "Insert link")))
;; * Candidate generation functions
(defun @-links-this-buffer ()
"Return list of candidates in the current buffer.
The candidates are to:
1. headings
2. headings with ID properties
3. headings with CUSTOM_ID properties
A candidate is a list of (link function)."
(let ((candidates '()))
;; Links to labels (figures and tables) in this buffer
(setq candidates (cl-loop for label in (org-ref-get-labels)
collect
(list (format "ref:%s" label) #'insert)))
;; headings in this buffer
(save-excursion
(goto-char (point-min))
(while (re-search-forward org-heading-regexp nil t)
(let ((title (fifth (org-heading-components)))
(id (org-entry-get (point) "ID"))
(custom-id (org-entry-get (point) "CUSTOM_ID")))
(cl-pushnew (list
(format "[[*%s][*%s]]"
(org-link-escape title)
(org-link-escape title))
#'insert)
candidates)
;; ID
(when id
(cl-pushnew (list
(format "[[id:%s][*%s]]"
id
(org-link-escape title))
#'insert)
candidates))
;; CUSTOM_ID
(when custom-id
(cl-pushnew (list
(format "[[#%s][%s]]"
custom-id
(org-link-escape title))
#'insert)
candidates)))))
candidates))
(defun open-org-buffers ()
"Return a list of buffers to org-files."
(seq-filter (lambda (b)
(when-let (f (buffer-file-name b))
(string= "org" (file-name-extension f))))
(buffer-list)))
(defun @-hashtags ()
"Get a list of candidate hashtags you have used before."
(let* ((tip (looking-at-hashtag))
(hashtag-data (with-org-db
(sqlite-select org-db "select
hashtag, file_hashtags.begin, files.filename
from hashtags
left join file_hashtags on hashtags.rowid = file_hashtags.hashtag_id
inner join files on files.rowid = file_hashtags.filename_id"))))
(-uniq (cl-loop for (hashtag begin fname) in hashtag-data
collect (concat "#" hashtag)))))
(defun @-links-org-buffers ()
"Return candidates in all open org-buffers.
These candidates are to headings by position and by *links."
(let ((candidates '()))
;; Links to headings in open org-files
(setq candidates (append
candidates
(let* ((org-bufs (open-org-buffers))
title
file
(headings '()))
(cl-loop for buf in org-bufs
do
(setq file (buffer-file-name buf))
(when (file-exists-p file)
(with-current-buffer buf
(save-excursion
(goto-char (point-min))
(while (re-search-forward org-heading-regexp nil t)
(setq title (fifth (org-heading-components)))
(cl-pushnew (list
(format "[[%s::%s][%s]]"
file
(line-number-at-pos)
(org-link-escape title))
#'insert)
headings)
(cl-pushnew (list
(format "[[%s::*%s][%s]]"
file
(org-link-escape title)
(org-link-escape title))
#'insert)
headings))))))
headings)))
candidates))
(defun @-links-open-files ()
"Return links to all open files."
(delete nil
(mapcar (lambda (buf)
(when-let ((fname (buffer-file-name buf)))
(format "[[file:%s]]" fname)))
(buffer-list))))
(defun @-links-stored-links ()
"Return list of stored links"
(mapcar (lambda (lnk)
`(,(car lnk) insert))
org-stored-links))
(defun @-counsel-recentf-candidates ()
"Returns list of links to recentf candidates."
(mapcar (lambda (lnk)
(list
(format "[[file:%s]]" lnk)
'insert))
(counsel-recentf-candidates)))
(defun @-projectile-relevant-known-projects ()
(mapcar (lambda (lnk)
(list
(format "[[file:%s]]" lnk)
'insert))
(projectile-relevant-known-projects)))
(defun @-link-candidates ()
"Return all the candidates"
(append '(("@" insert))
(cl-loop for func in scimax-links-candidate-functions
append
(funcall func))
scimax-insert-link-functions))
(defun @-insert-link ()
"Insert a link or call a function from `scimax-insert-link-functions'."
(interactive)
(let ((candidates (@-link-candidates)))
(ivy-read "Link: " candidates
:action #'(lambda (x)
(unless smex-initialized-p
(smex-initialize))
(cond
((and (symbolp x)
(-contains? smex-ido-cache
(symbol-name x)))
(command-execute (intern-soft x) 'record))
((stringp x)
(insert x))
((and (listp x)
(functionp (second x)))
(funcall (second x) (car x)))
;; a contact
((plist-get (cdr x) :email)
(insert (format "[[contact:%s][%s]]"
(plist-get (cdr x) :email)
(plist-get (cdr x) :title))))
(t
(error "err: %S" x)))))))
;; * @ key-binding
(define-key org-mode-map "@"
'(menu-item "maybe-@" nil
:filter (lambda (&optional _)
(unless (looking-back "[a-zA-Z0-9]" 2)
#'@-insert-link))))
(provide 'scimax-@-links)
;;; [email protected] ends here