Skip to content

Commit 02e25f3

Browse files
perf(org): add and use activate processor
* Define citar-org-cite-basic-activate citar-org-cite-basic-activate is an org-cite activate processor meant to be a more performant version of org-cite-basic-activate * feat(org): Use the citar-org-cite-basic-activate activate processor by default Previously, citar-org-activation-functions used the built-in org-cite-basic-activate` processor. Now, we use citar-org-cite-basic-activate by default instead. * docs(org): Update README
1 parent 0f1786b commit 02e25f3

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

README.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ For example, if point:
361361
- is on an existing citation-reference, you will be prompted to replace it
362362
- follows or precedes a citation-reference, you will be prompted to add a new citation-reference
363363

364-
The "activate processor" runs the list of functions in ~citar-org-activation-functions~, which by default is the ~basic~ processor from ~oc-basic~ to provide fontification, and also a little function that adds a keymap (~citar-org-citation-map~) for editing citations at point.
364+
The "activate processor" runs the list of functions in ~citar-org-activation-functions~, which by default uses ~citar-org-cite-basic-activate~, a version of the ~basic~ processor from ~oc-basic~ to provide fontification that leverages citar's performant caching, as well as a little function that adds a keymap (~citar-org-citation-map~) for editing citations at point.
365365
The ~citar-org-citation-map~ keymap includes the following bindings that provide additional citation and citation-reference editing options.
366366

367367
| key | binding | description |

citar-org.el

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ If nil, use `org-cite-supported-styles'."
6262
:type '(repeat :tag "org-cite export processor" symbol))
6363

6464
(defcustom citar-org-activation-functions
65-
'(org-cite-basic-activate
65+
'(citar-org-cite-basic-activate
6666
citar-org-activate-keymap)
6767
"List of activation functions for a citation.
6868
Each function takes one argument, a citation."
@@ -244,6 +244,54 @@ strings by style."
244244
(seq-difference (org-cite-list-bibliography-files)
245245
org-cite-global-bibliography))
246246

247+
(defun citar-org-cite-basic-activate (citation)
248+
"Set various text properties on CITATION object.
249+
Fontify whole citation with org-cite face. Fontify key with error face
250+
when it does not belong to known keys. Otherwise, use org-cite-key face.
251+
252+
Moreover, when mouse is on a known key, display the corresponding
253+
bibliography. On a wrong key, suggest a list of possible keys, and offer
254+
to substitute one of them with a mouse click.
255+
256+
This function activation function is meant to be added to
257+
`citar-org-activation-functions'. It is a modified version of the
258+
built-in `org-cite-basic-activate' that is more performant by leveraging
259+
citar's caching."
260+
(pcase-let ((`(,beg . ,end) (org-cite-boundaries citation))
261+
;; Use citar to retrieve all entries' keys
262+
(keys (let (keys)
263+
(maphash (lambda (key _value) (push key keys))
264+
(citar-get-entries))
265+
keys)))
266+
(put-text-property beg end 'font-lock-multiline t)
267+
(add-face-text-property beg end 'org-cite)
268+
(dolist (reference (org-cite-get-references citation))
269+
(pcase-let* ((`(,beg . ,end) (org-cite-key-boundaries reference))
270+
(key (org-element-property :key reference)))
271+
;; Highlight key on mouse over.
272+
(put-text-property beg end
273+
'mouse-face
274+
org-cite-basic-mouse-over-key-face)
275+
(if (member key keys)
276+
;; Activate a correct key. Face is `org-cite-key' and `help-echo' displays bibliography entry, for
277+
;; reference. <mouse-1> calls `org-open-at-point'.
278+
(let* ((entry (string-trim (citar-format-reference (list key)))) ; Use citar
279+
(bibliography-entry
280+
(org-element-interpret-data entry)))
281+
(add-face-text-property beg end 'org-cite-key)
282+
(put-text-property beg end 'help-echo bibliography-entry)
283+
(org-cite-basic--set-keymap beg end nil))
284+
;; Activate a wrong key. Face is `error', `help-echo' displays possible suggestions.
285+
(add-face-text-property beg end 'error)
286+
(let ((close-keys (org-cite-basic--close-keys key keys)))
287+
(when close-keys
288+
(put-text-property beg end 'help-echo
289+
(concat "Suggestions (mouse-1 to substitute): "
290+
(mapconcat #'identity close-keys " "))))
291+
;; When the are close know keys, <mouse-1> provides completion to fix the current one. Otherwise,
292+
;; call `org-cite-insert'.
293+
(org-cite-basic--set-keymap beg end (or close-keys 'all))))))))
294+
247295
;;; Org note function
248296

249297
(defun citar-org--id-get-create (&optional force)

0 commit comments

Comments
 (0)