Skip to content

Commit 421e67b

Browse files
committed
Make citar-markdown-insert-citation context-sensitive
1 parent 623024e commit 421e67b

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

citar-markdown.el

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,28 @@
4545

4646
;;;###autoload
4747
(defun citar-markdown-insert-citation (keys)
48-
"Insert a pandoc-style citation consisting of KEYS."
49-
(let* ((prenote (if citar-markdown-prompt-for-extra-arguments
50-
(read-from-minibuffer "Prenote: ")))
51-
(postnote (if citar-markdown-prompt-for-extra-arguments
52-
(read-from-minibuffer "Postnote: ")))
53-
(prenote (if (string= "" prenote) "" (concat prenote " ")))
54-
(postnote (if (string= "" postnote) "" (concat ", " postnote))))
55-
(insert (format "[%s%s%s]"
56-
prenote
57-
(mapconcat (lambda (k) (concat "@" k)) keys "; ")
58-
postnote))))
48+
"Insert a pandoc-style citation consisting of KEYS.
49+
If the point is inside a citation, add new keys after the current
50+
key. If point is immediately after the opening \[, add new keys
51+
to the beginning of the citation."
52+
(let* ((citation (citar-markdown-citation-at-point))
53+
(keys (if citation (seq-difference keys (car citation)) keys))
54+
(keyconcat (mapconcat (lambda (k) (concat "@" k)) keys "; ")))
55+
(when keys
56+
(if (or (not citation)
57+
(= (point) (cadr citation))
58+
(= (point) (cddr citation)))
59+
(let* ((prenote (when citar-markdown-prompt-for-extra-arguments
60+
(read-from-minibuffer "Prenote: ")))
61+
(postnote (when citar-markdown-prompt-for-extra-arguments
62+
(read-from-minibuffer "Postnote: ")))
63+
(prenote (if (string= "" prenote) "" (concat prenote " ")))
64+
(postnote (if (string= "" postnote) "" (concat ", " postnote))))
65+
(insert (format "[%s%s%s]" prenote keyconcat postnote)))
66+
(if (= (point) (1+ (cadr citation)))
67+
(save-excursion (insert keyconcat "; "))
68+
(skip-chars-forward "^;]" (cddr citation))
69+
(insert "; " keyconcat))))))
5970

6071
(defconst citar-markdown-citation-key-regexp
6172
(concat "-?@" ; @ preceded by optional -

citar.el

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ If you use 'org-roam' and 'org-roam-bibtex', you can use
200200
(citation-at-point . citar-latex-citation-at-point)))
201201
((markdown-mode) .
202202
((insert-keys . citar-markdown-insert-keys)
203+
(insert-citation . citar-markdown-insert-citation)
203204
(key-at-point . citar-markdown-key-at-point)
204-
(citation-at-point . citar-markdown-citation-at-point)
205-
(insert-citation . citar-markdown-insert-citation)))
205+
(citation-at-point . citar-markdown-citation-at-point)))
206206
(t .
207207
((insert-keys . citar--insert-keys-comma-separated))))
208208
"The variable determining the major mode specific functionality.

0 commit comments

Comments
 (0)