-
Notifications
You must be signed in to change notification settings - Fork 10
/
citar-denote.el
executable file
·566 lines (490 loc) · 20.5 KB
/
citar-denote.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
;;; citar-denote.el --- Minor mode to integrate Citar and Denote -*- lexical-binding: t -*-
;; Copyright (C) 2022-2023 Peter Prevos
;; Author: Peter Prevos <peter@prevos.net>
;; Maintainer: Peter Prevos <peter@prevos.net>
;; Homepage: https://github.com/pprevos/citar-denote
;; Version: 1.8
;; Package-Requires: ((emacs "28.1") (citar "1.3") (denote "2.0") (dash "2.19.1"))
;; 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 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:
;;
;; A minor-mode integrating 'citar' and 'denote'.
;;
;; This package provides functions create and work with bibliographic notes.
;;
;; https://lucidmanager.org/productivity/bibliographic-notes-in-emacs-with-citar-denote/
;;
;;; Code:
(require 'denote)
(require 'dash)
(require 'citar)
(eval-when-compile
(require 'subr-x))
;; Customisable variables
(defgroup citar-denote ()
"Creating and accessing bibliography files with Citar and Denote."
:group 'files)
(defcustom citar-denote-keyword "bib"
"Denote keyword (file tag) to indicate bibliographical notes."
:group 'citar-denote
:type 'string)
(defcustom citar-denote-use-bib-keywords nil
"Extract bibliographic keywords from bibliography and use as Denote keywords."
;; https://github.com/pprevos/citar-denote/issues/17
:group 'citar-denote
:type 'boolean)
(defcustom citar-denote-file-type (or denote-file-type 'org)
"File Type used by Citar-Denote.
Default is `denote-file-type' or org if the former is nil. Users
can use Markdown or plain text for their bibliographic notes.
Please note that citations are not supported in plain text."
:group 'citar-denote
:type '(choice
(const :tag "Org mode (default)" org)
(const :tag "Markdown (YAML front matter)" markdown-yaml)
(const :tag "Markdown (TOML front matter)" markdown-toml)
(const :tag "Plain text" text)))
(defcustom citar-denote-subdir 'nil
"Ask for a subdirectory when creating a new bibliographic note.
When creating a new bibliographic note, the user will be asked for a directory."
;; https://github.com/pprevos/citar-denote/issues/11
:group 'citar-denote
:type 'boolean)
(defcustom citar-denote-title-format "title"
"Title format for new bibliographic notes.
- \"title\": Extract title (or short title) from entry
- \"author-year\": Author-year citation style
- \"author-year-title\": Combine author, year and title
- \"full\": Full citation
- nil: BibTeX citekey
For \"author-year\" and \"author-year-title\" you can configure:
- `citar-denote-title-format-authors'
- `citar-denote-title-format-andstr'."
:group 'citar-denote
:type '(choice
(const :tag "Title" "title")
(const :tag "Author (Year)" "author-year")
(const :tag "Author (Year). Title" "author-year-title")
(const :tag "Full citation" "full")
(const :tag "Citekey" nil)))
(defcustom citar-denote-title-format-authors 1
"Maximum number of authors in \"author-year\" for `citar-denote-title-format`."
:group 'citar-denote
:type 'integer)
(defcustom citar-denote-title-format-andstr "and"
"Connecting string for authors in \"author-year\" for `citar-denote-note-title`."
:group 'citar-denote
:type 'string)
(defvar citar-denote-file-types
`((org
:reference-format "#+reference: %s\n"
:reference-regex "^#\\+reference\\s-*:")
(markdown-yaml
:reference-format "reference: %s\n"
:reference-regex "^reference\\s-*:")
(markdown-toml
:reference-format "reference = %s\n"
:reference-regex "^reference\\s-*=")
(text
:reference-format "reference: %s\n"
:reference-regex "^reference\\s-*:"))
"Alist of `citar-denote-file-types' and their format properties.
Each element is of the form (SYMBOL . PROPERTY-LIST). SYMBOL is
one of those specified in `citar-denote-file-type'.
PROPERTY-LIST is a plist that consists of two elements:
- `:reference-format' Front matter identifier for citation key.
- `:reference-regex' Regexp to look for the citekey in a bibliographic notes.")
(defvar citar-denote-files-regexp (concat "_" citar-denote-keyword)
"Regular expression used to look for file names of bibliographic notes.
The default assumes \"_bib\" tag is part of the file name.
Configurable with `citar-denote-keyword'.")
(defvar citar-denote-citekey-regex "@[a-zA-Z0-9:?-_]+"
"Regular expression to extract citation keys from a text.")
;; Auxiliary functions
(defun citar-denote--reference-format (file-type)
"Return the reference format associated to FILE-TYPE from `citar-denote-file-types'."
(plist-get
(alist-get file-type citar-denote-file-types)
:reference-format))
(defun citar-denote--reference-regex (file-type)
"Return the reference regex associated to FILE-TYPE from `citar-denote-file-types'."
(plist-get
(alist-get file-type citar-denote-file-types)
:reference-regex))
(defun citar-denote--extract-keywords (citekey)
"Extract keywords of CITEKEY from the bibliography."
(if-let* ((KeyWords (citar-get-value "keywords" citekey))
(keywords (downcase KeyWords))
(filetags (split-string keywords ", *")))
(mapcar (lambda (kwd) (replace-regexp-in-string " " "-" kwd))
filetags)))
(defun citar-denote--keywords-prompt (citekey)
"Prompt for one or more keywords and include `citar-denote-keyword'.
When `citar-denote-use-bib-keywords' is not nil, also use keywords
from bibliographic entry with CITEKEY."
(let* ((choice (append (list citar-denote-keyword)
(denote-keywords-prompt)))
(keywords (if citar-denote-use-bib-keywords
(append choice
(citar-denote--extract-keywords citekey))
choice)))
(delete-dups keywords)))
(defun citar-denote--add-reference (citekey)
"Add reference line with CITEKEY in front matter of the Denote file.
`citar-denote-add-citekey' is the interactive version of this function."
(let ((file-type citar-denote-file-type))
(save-excursion
(goto-char (point-min))
(re-search-forward "^\n" nil t)
(forward-line -1)
(if (not (eq file-type 'org))
(forward-line -1))
(insert
(format (citar-denote--reference-format file-type) citekey)))))
(defun citar-denote--retrieve-references (file)
"Return reference citekey(s) from FILE front matter."
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(let ((trims "[ \t\n\r]+")
(file-type (denote-filetype-heuristics file)))
(when (re-search-forward
(citar-denote--reference-regex file-type) nil t 1)
(split-string
(string-trim
(buffer-substring-no-properties (point) (line-end-position))
trims trims) ";")))))
;;;###autoload
(defun citar-denote--get-notes (&optional citekeys)
"Return has table of Denote files associated with the CITEKEYS citation keys.
If CITEKEYS is omitted, return all Denote files tagged with
`citar-denote-keyword'."
(let ((files (make-hash-table :test 'equal)))
(prog1 files
(dolist (file (denote-directory-files-matching-regexp
citar-denote-files-regexp))
(let ((keys-in-file (citar-denote--retrieve-references file)))
(dolist (key keys-in-file)
(if citekeys
(dolist (k citekeys)
(when (string= k key)
(push file (gethash key files))))
(push file (gethash key files))))))
(maphash
(lambda (key filelist)
(puthash key (nreverse filelist) files)) files))))
(defun citar-denote--retrieve-cite-files (citekey)
"Return names of Denote files that contain CITEKEY."
(let ((files (denote-directory-text-only-files)))
(delete-dups
(mapcar
#'xref-location-group
(mapcar #'xref-match-item-location
(xref-matches-in-files (format "@%s" citekey) files))))))
;;;###autoload
(defun citar-denote--has-notes ()
"Return a list of all citekeys referenced in a Denote file.
See documentation for `citar-has-notes'."
(let ((notes (citar-denote--get-notes)))
(unless (hash-table-empty-p notes)
(lambda (citekey) (and (gethash citekey notes) t)))))
(defun citar-denote--has-citekeys (citekeys)
"Return hash table of citar entries with associated CITEKEYS."
(let ((citar-entries (citar-get-entries))
(citekey-entries (make-hash-table :test 'equal)))
(mapc (lambda (key)
(puthash key (gethash key citar-entries) citekey-entries))
citekeys)
(unless (hash-table-empty-p citekey-entries)
(lambda (citekey) (and (gethash citekey citekey-entries) t)))))
(defun citar-denote--remove-bibkey (file)
"Remove `citar-denote-bibkey' file tag from FILE."
(let* ((file-type (denote-filetype-heuristics file))
(keywords (denote-retrieve-keywords-value file file-type)))
(denote-rewrite-keywords
file
(delete citar-denote-keyword keywords)
file-type)
(denote-rename-file-using-front-matter file t)))
(defun citar-denote--extract-citations ()
"Extract citations from all Denote files."
;; Extract lines with citations
(let* ((xrefs (xref-matches-in-files
citar-denote-citekey-regex
(denote-directory-text-only-files)))
(citations (mapcar #'substring-no-properties
(mapcar #'xref-match-item-summary xrefs))))
;; Clean results
(delete-dups
(mapcar
(lambda (cite)
(replace-regexp-in-string
"@\\|\\].*\\|;" ""
(substring
cite (string-match citar-denote-citekey-regex cite))))
citations))))
(defun citar-denote--generate-title (citekey)
"Generate title for new bibliographic note using CITEKEY.
Either title, author/year, full reference or citation key.
Based on `citar-denote-title-format'."
;; https://github.com/pprevos/citar-denote/issues/15
(let ((title (citar-get-value "title" citekey))
(author-names (or (citar-get-value "author" citekey)
(citar-get-value "editor" citekey)))
(year (or (citar-get-value "year" citekey)
(citar-get-value "date" citekey)
(citar-get-value "issued" citekey))))
(cond ((equal citar-denote-title-format "title")
title)
((equal citar-denote-title-format "author-year")
(concat (citar--shorten-names
author-names
citar-denote-title-format-authors
citar-denote-title-format-andstr)
" (" year ")"))
((equal citar-denote-title-format "author-year-title")
(let* ((citar-denote-title-format "author-year")
(author-year (citar-denote--generate-title citekey))
(title (citar-get-value "title" citekey)))
(concat author-year " " title)))
((equal citar-denote-title-format "full")
(let ((ref (citar-format-reference (list citekey))))
(substring ref 0 (- (length ref) 2))))
(t citekey))))
(defun citar-denote--get-nocite ()
"Return list of citekey(s) of Citar entries not cited or referenced in Denote files."
(let* ((all-items (hash-table-keys (citar-get-entries)))
(used-citations (citar-denote--extract-citations))
(references
(hash-table-keys (citar-denote--get-notes)))
(all-citations
(delete-dups (append used-citations references)))
(unused (-difference all-items all-citations)))
(citar-select-refs
:multiple t
:filter (citar-denote--has-citekeys unused))))
;;;###autoload
(defun citar-denote--create-note (citekey &optional _entry)
"Create a bibliographic note for CITEKEY with properties ENTRY.
The file type is determined by `citar-denote-file-type'.
The title format is set by `citar-denote-title-format'.
When `citar-denote-subdir' is non-nil, prompt for a subdirectory."
(denote
(read-string "Title: " (citar-denote--generate-title citekey))
(citar-denote--keywords-prompt citekey)
citar-denote-file-type
(when citar-denote-subdir (denote-subdirectory-prompt)))
(citar-denote--add-reference citekey))
;; Interactive functions
;;;###autoload
(defun citar-denote-open-note ()
"Open a bibliographic note using Citar."
(interactive)
(let* ((citekeys (citar-select-refs :filter (citar-denote--has-notes)))
(file (citar--select-resource
citekeys :notes t :create-notes t)))
(find-file (cdr file))))
;;;###autoload
(defun citar-denote-dwim ()
"Access attachments, notes and links of a bibliographic reference.
When more than one bibliographic item is referenced, select item first."
(interactive)
;; Any citation keys in the note?
(if-let* ((keys (citar-denote--retrieve-references (buffer-file-name)))
(key (if (= (length keys) 1)
(car keys)
(citar-select-ref
:filter (citar-denote--has-citekeys keys)))))
(citar-open (list key))
(if (denote-file-is-note-p (buffer-file-name))
(when (yes-or-no-p "Current buffer does not reference a citation key. Add a reference? ")
(citar-denote-add-citekey)
(citar-denote-dwim))
(user-error "Buffer is not a Denote file"))))
;;;###autoload
(defun citar-denote-open-reference-entry ()
"Open bibliographic source file associated with a bibliographic reference.
When more than one bibliographic item is referenced, select item first."
(interactive)
(if-let* ((keys (citar-denote--retrieve-references (buffer-file-name)))
(key (if (= (length keys) 1)
(car keys)
(citar-select-ref
:filter (citar-denote--has-citekeys keys)))))
(citar-open-entry key)
(if (denote-file-is-note-p (buffer-file-name))
(when (yes-or-no-p "Current buffer does not reference a citation key. Add a reference? ")
(citar-denote-add-citekey)
(citar-denote-open-reference-entry))
(user-error "Buffer is not a Denote file"))))
;;;###autoload
(defun citar-denote-add-citekey ()
"Add citation key(s) to existing note or convert to a bibliographic note."
(interactive)
(if-let* ((file (buffer-file-name))
((denote-file-is-note-p file))
(file-type (denote-filetype-heuristics file))
(citekeys (citar-select-refs))
(references (mapconcat 'identity citekeys ";")))
;; Check whether reference line already exists
(if-let (keys (citar-denote--retrieve-references file))
;; Append reference list
(save-excursion
(goto-char (point-min))
(re-search-forward (citar-denote--reference-regex file-type))
(end-of-line)
(insert (concat ";" references))
(save-buffer))
;; Add new citation keys
(progn (citar-denote--add-reference references)
(denote-keywords-add (list citar-denote-keyword))
(save-buffer)))
(user-error "Buffer is not a Denote file")))
;;;###autoload
(defun citar-denote-remove-citekey ()
"Remove a reference from a bibliographic note."
(interactive)
(if-let* ((file (buffer-file-name))
(file-type (denote-filetype-heuristics file))
(citekeys (citar-denote--retrieve-references file))
(selected (if (< (length citekeys) 2)
(car citekeys)
(citar-select-ref
:filter
(citar-denote--has-citekeys citekeys)))))
(let ((new-citekeys (delete selected citekeys)))
(save-excursion
;; Remove references line
(goto-char (point-min))
(re-search-forward
(citar-denote--reference-regex file-type))
(move-beginning-of-line nil)
(kill-line 1)
;; Add new line or remove file tags when applicable
(if (> (length new-citekeys) 0)
(citar-denote--add-reference
(mapconcat 'identity new-citekeys ";"))
(citar-denote--remove-bibkey file))
(save-buffer)))
(user-error "No references in this buffer, or not a Denote file")))
;;;###autoload
(defun citar-denote-find-reference ()
"Find Denote file(s) citing one of the current reference(s).
When more than one bibliographic item is referenced, select item first."
(interactive)
(let ((file (buffer-file-name)))
(if (denote-file-is-note-p file)
(let* ((citekeys (citar-denote--retrieve-references file))
(citekey (when citekeys
(if (= (length citekeys) 1)
(car citekeys)
(citar-select-ref
:filter
(citar-denote--has-citekeys citekeys)))))
(files (delete file (citar-denote--retrieve-cite-files citekey))))
(cond
(files
(find-file (denote-get-path-by-id
(denote-extract-id-from-string
(denote-link--find-file-prompt files))))
(goto-char (point-min))
(search-forward citekey))
((null citekey)
(when (yes-or-no-p
"This is not a bibliographic note. Add a reference ?")
(citar-denote-add-citekey)
(citar-denote-find-reference)))
(t (user-error "No citation found in other Denote files"))))
(user-error "Buffer is not a Denote file"))))
;;;###autoload
(defun citar-denote-link-reference ()
"Insert a Denote link to a bibliographic note using Citar selection."
;; https://github.com/pprevos/citar-denote/issues/20
(interactive)
(let* ((citekey (citar-select-refs
:filter (citar-denote--has-notes)
:multiple nil))
(files (gethash (car citekey)
(citar-denote--get-notes citekey)))
(file (if (= (length files) 1)
(car files)
(funcall project-read-file-name-function
"Select note: "
files nil nil nil))))
(denote-link file)))
;;;###autoload
(defun citar-denote-find-citation ()
"Find a Denote file that cites a bibliographic entry."
(interactive)
(let* ((citations (citar-denote--extract-citations))
(citekey (citar-select-ref
:filter (citar-denote--has-citekeys citations)))
(files (citar-denote--retrieve-cite-files citekey)))
(find-file (denote-get-path-by-id
(denote-extract-id-from-string
(denote-link--find-file-prompt files))))
(goto-char (point-min))
(search-forward citekey)))
;;;###autoload
(defun citar-denote-cite-nocite ()
"Cite bibliographic entries not cited or referenced in Denote files."
(interactive)
(if (denote-file-is-note-p (buffer-file-name))
(citar-insert-citation (citar-denote--get-nocite))
(user-error "Buffer is not a Denote file")))
(define-obsolete-function-alias
'citar-denote-find-nocite
'citar-denote-cite-nocite
"1.6")
;;;###autoload
(defun citar-denote-reference-nocite ()
"Create note for bibliographic entries not cited or referenced in Denote files."
(interactive)
(citar-denote--create-note (citar-denote--get-nocite)))
;; Citar integration
(defconst citar-denote-config
(list :name "Denote"
:category 'file
:items #'citar-denote--get-notes
:hasitems #'citar-denote--has-notes
:open #'find-file
:create #'citar-denote--create-note)
"Instructing citar to use citar-denote functions.")
(defconst citar-denote-orig-source
citar-notes-source
"Store the `citar-notes-source' value prior to enabling `citar-denote-mode'.")
(defvar citar-notes-source)
(defvar citar-notes-sources)
;; Initialise minor mode
(defun citar-denote-setup ()
"Setup `citar-denote-mode'."
(citar-register-notes-source
'citar-denote-source citar-denote-config)
(setq citar-notes-source 'citar-denote-source))
(defun citar-denote-reset ()
"Reset citar to default values."
(setq citar-notes-source citar-denote-orig-source)
(citar-remove-notes-source 'citar-denote))
;;;###autoload
(define-minor-mode citar-denote-mode
"Toggle integration between Citar and Denote."
:global t
:group 'citar
:lighter " citar-denote"
(if citar-denote-mode
(citar-denote-setup)
(citar-denote-reset)))
(provide 'citar-denote)
;;; citar-denote.el ends here