update packages
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
;;; org-ref-latex.el --- org-ref functionality for LaTeX files -*- lexical-binding: t; -*-
|
||||
|
||||
;; Copyright (C) 2015-2021 John Kitchin
|
||||
;; Copyright (C) 2015-2024 John Kitchin
|
||||
|
||||
;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
|
||||
;; Keywords: languages
|
||||
@@ -25,10 +25,34 @@
|
||||
|
||||
(require 'org-ref-citation-links)
|
||||
(require 'bibtex-completion)
|
||||
(require 'tex-mode)
|
||||
|
||||
(defvar latex-mode-map)
|
||||
(defvar org-ref-cite-types)
|
||||
|
||||
(defcustom orl-cite-keymap
|
||||
(let ((map (copy-keymap latex-mode-map)))
|
||||
(define-key map (kbd "S-<left>") #'orl-cite-shift-left)
|
||||
(define-key map (kbd "S-<right>") #'orl-cite-shift-right)
|
||||
(define-key map (kbd "S-<up>") #'orl-sort-cite-keys)
|
||||
(define-key map (kbd "S-<down>") (lambda () (interactive) (orl-sort-cite-keys t)))
|
||||
(define-key map (kbd "<tab>") #'org-ref-latex-insert-citation)
|
||||
|
||||
(define-key map [mouse-1] (lambda ()
|
||||
(interactive)
|
||||
(let ((bibtex-completion-bibliography (org-ref-latex-get-bibliography)))
|
||||
(bibtex-completion-show-entry (list (org-ref-latex-key-at-point)))
|
||||
(bibtex-beginning-of-entry))))
|
||||
map)
|
||||
"Key map for cite keys."
|
||||
:group 'org-ref-latex
|
||||
:type '(restricted-sexp
|
||||
:match-alternatives
|
||||
((lambda (x)
|
||||
(or (keymapp x) ; Can be a keymap var.
|
||||
(and (symbolp x)
|
||||
(boundp x)
|
||||
(keymapp (symbol-value x))))))))
|
||||
|
||||
|
||||
(defvar org-ref-latex-cite-re
|
||||
(concat "\\\\\\(?1:" (mapconcat
|
||||
@@ -37,15 +61,15 @@
|
||||
(mapcar 'car org-ref-cite-types)
|
||||
"\\|")
|
||||
"\\)"
|
||||
"\\(?2:\\[[^]]*\\]\\)?" ; optional []
|
||||
"\\(?3:\\[[^]]*\\]\\)?" ; optional []
|
||||
"\\(?2:\\[[^]]*\\]\\)?" ; optional []
|
||||
"\\(?3:\\[[^]]*\\]\\)?" ; optional []
|
||||
"{\\(?4:[^}]*\\)}") ; group 4 contains the keys
|
||||
"Regexp for LaTeX citations. \\citetype[opti{o}nal][optiona{l}]{some,keys}.
|
||||
The clickable part are the keys.")
|
||||
|
||||
|
||||
(defun org-ref-latex-get-bibliography ()
|
||||
"Find bibliographies in the tex file"
|
||||
"Find bibliographies in the tex file."
|
||||
(save-excursion
|
||||
(let ((bibliography '()))
|
||||
(goto-char (point-min))
|
||||
@@ -65,6 +89,23 @@ The clickable part are the keys.")
|
||||
"Font-lock function to make cites in LaTeX documents clickable."
|
||||
(while (re-search-forward org-ref-latex-cite-re limit t)
|
||||
(setq font-lock-extra-managed-props (delq 'help-echo font-lock-extra-managed-props))
|
||||
|
||||
;; save data on cite. This is useful later for modifying cites, clicking, etc.
|
||||
(add-text-properties (match-beginning 0) (match-end 0)
|
||||
`(org-ref-latex-cite (
|
||||
:cite-begin ,(match-beginning 0)
|
||||
:cite-end ,(match-end 0)
|
||||
:cite-key-begin ,(+ 1 (match-beginning 4))
|
||||
:cite-key-end ,(- (match-end 4) 1)
|
||||
:cite-type ,(match-string-no-properties 1)
|
||||
:optional-1 ,(match-string-no-properties 2)
|
||||
:optional-2 ,(match-string-no-properties 3)
|
||||
:keys ,(mapcar
|
||||
#'s-trim
|
||||
(s-split
|
||||
","
|
||||
(match-string-no-properties 4))))))
|
||||
|
||||
(goto-char (match-beginning 0))
|
||||
(let ((end (match-end 0)))
|
||||
(cl-loop for key in (mapcar #'s-trim (split-string (match-string-no-properties 4) ","))
|
||||
@@ -75,19 +116,14 @@ The clickable part are the keys.")
|
||||
(add-text-properties
|
||||
(match-beginning 0)
|
||||
(match-end 0)
|
||||
`(mouse-face highlight
|
||||
local-map ,(let ((map (copy-keymap latex-mode-map)))
|
||||
(define-key map [mouse-1]
|
||||
`(lambda ()
|
||||
(interactive)
|
||||
(let ((bibtex-completion-bibliography (org-ref-latex-get-bibliography)))
|
||||
(bibtex-completion-show-entry (list ,key))
|
||||
(bibtex-beginning-of-entry))))
|
||||
map)
|
||||
help-echo ,(let* ((bibtex-completion-bibliography (org-ref-latex-get-bibliography)))
|
||||
(condition-case nil
|
||||
(bibtex-completion-apa-format-reference key)
|
||||
(error (display-warning :warning (format "Key %s missing." key)))))))))
|
||||
`(
|
||||
org-ref-cite-key ,key
|
||||
mouse-face highlight
|
||||
local-map ,orl-cite-keymap
|
||||
help-echo ,(let* ((bibtex-completion-bibliography (org-ref-latex-get-bibliography)))
|
||||
(condition-case nil
|
||||
(bibtex-completion-apa-format-reference key)
|
||||
(error (display-warning :warning (format "Key %s missing." key)))))))))
|
||||
(goto-char end))))
|
||||
|
||||
|
||||
@@ -98,6 +134,142 @@ The clickable part are the keys.")
|
||||
'((org-ref-next-latex-cite 0 font-lock-constant-face))))
|
||||
|
||||
(add-hook 'LaTeX-mode-hook 'org-ref-latex-cite-on)
|
||||
(add-hook 'LaTeX-mode-hook 'org-ref-latex-cite-on)
|
||||
|
||||
|
||||
;; * Insert cite link
|
||||
|
||||
(defun org-ref-latex-on-citation-p ()
|
||||
"Return non-nil if on a citation."
|
||||
(get-text-property (point) 'org-ref-latex-cite))
|
||||
|
||||
|
||||
(defun org-ref-latex-key-at-point ()
|
||||
"Return key at point if there is one."
|
||||
(interactive)
|
||||
(get-text-property (point) 'org-ref-cite-key))
|
||||
|
||||
(defun orl-insert-key (key)
|
||||
(cond
|
||||
;; on a key
|
||||
((get-text-property (point) 'org-ref-cite-key)
|
||||
(forward-sexp)
|
||||
(insert (format ",%s" key)))
|
||||
;; on the leading \
|
||||
((and (get-text-property (point) 'org-ref-latex-cite)
|
||||
(looking-at "\\\\"))
|
||||
(search-forward "{")
|
||||
(insert (format "%s," key)))
|
||||
;; on {
|
||||
((and (get-text-property (point) 'org-ref-latex-cite)
|
||||
(looking-at "{"))
|
||||
(forward-char)
|
||||
(insert (format "%s," key)))
|
||||
;; on }
|
||||
((and (get-text-property (point) 'org-ref-latex-cite)
|
||||
(looking-at "}"))
|
||||
(insert (format ",%s" key)))
|
||||
;; on citetype
|
||||
((and (get-text-property (point) 'org-ref-latex-cite)
|
||||
(get-text-property (point) 'org-ref-latex-cite-type))
|
||||
(search-forward "{")
|
||||
(insert (format "%s," key)))
|
||||
;; everything else
|
||||
(t
|
||||
(insert (format "\\cite{%s}" key)))))
|
||||
|
||||
|
||||
(defun org-ref-latex-insert-citation ()
|
||||
"Insert a citation key with completion.
|
||||
Should DWIM inserting into existing or new citations."
|
||||
(interactive)
|
||||
|
||||
;; This initializes bibtex if the variable is not defined.
|
||||
(unless bibtex-completion-display-formats-internal
|
||||
(bibtex-completion-init))
|
||||
|
||||
(let* ((bibtex-completion-bibliography (org-ref-latex-get-bibliography))
|
||||
(candidates (bibtex-completion-candidates)))
|
||||
(ivy-read "org-ref-ivy BibTeX entries: " candidates
|
||||
:preselect (ivy-thing-at-point)
|
||||
:action '(1
|
||||
("o" (lambda (candidate)
|
||||
(orl-insert-key (cdr (assoc "=key=" (cdr candidate)))))
|
||||
"insert")))))
|
||||
|
||||
(defun orl-swap-citation (direction)
|
||||
(let* ((key (org-ref-latex-key-at-point))
|
||||
(data (get-text-property (point) 'org-ref-latex-cite))
|
||||
(keys (plist-get data :keys))
|
||||
(i (seq-position keys key #'string=))
|
||||
cite-string)
|
||||
(if (> direction 0)
|
||||
(org-ref-swap-list-elements i (min (+ i 1) (- (length keys) 1)) keys)
|
||||
(org-ref-swap-list-elements i (max (- i 1) 0) keys))
|
||||
(setq data (plist-put data :keys keys))
|
||||
|
||||
;; now render it
|
||||
(setq cite-string (concat (format "\\%s" (plist-get data :cite-type))
|
||||
(or (plist-get data :optional-1) "")
|
||||
(or (plist-get data :optional-2) "")
|
||||
(format "{%s}" (s-join "," (plist-get data :keys)))))
|
||||
(cl--set-buffer-substring (plist-get data :cite-begin)
|
||||
(plist-get data :cite-end)
|
||||
cite-string)
|
||||
(search-forward key)
|
||||
(goto-char (match-beginning 0))))
|
||||
|
||||
|
||||
(defun orl-cite-shift-left ()
|
||||
"Shift cite key at point to the left."
|
||||
(interactive)
|
||||
(orl-swap-citation -1))
|
||||
|
||||
|
||||
(defun orl-cite-shift-right ()
|
||||
"Shift cite-key at point to the right."
|
||||
(interactive)
|
||||
(orl-swap-citation +1))
|
||||
|
||||
|
||||
(defun orl-sort-cite-keys (&optional descending)
|
||||
"Sort keys in ascending order by year.
|
||||
With prefix arg DESCENDING, sort in descending order."
|
||||
(interactive "P")
|
||||
(let* ((data (get-text-property (point) 'org-ref-latex-cite))
|
||||
(keys (plist-get data :keys))
|
||||
cite-string)
|
||||
|
||||
(setq data (plist-put data :keys
|
||||
(cl-loop for entry in
|
||||
(cl-sort (cl-loop for key in keys collect
|
||||
(list :key key :year (bibtex-completion-get-value
|
||||
"year"
|
||||
(bibtex-completion-get-entry
|
||||
key))))
|
||||
(if descending
|
||||
(lambda (x y)
|
||||
(> (string-to-number (plist-get x :year))
|
||||
(string-to-number (plist-get y :year))))
|
||||
(lambda (x y)
|
||||
(< (string-to-number (plist-get x :year))
|
||||
(string-to-number (plist-get y :year))))))
|
||||
collect
|
||||
(plist-get entry :key))))
|
||||
|
||||
|
||||
|
||||
;; now render it
|
||||
(setq cite-string (concat (format "\\%s" (plist-get data :cite-type))
|
||||
(or (plist-get data :optional-1) "")
|
||||
(or (plist-get data :optional-2) "")
|
||||
(format "{%s}" (s-join "," (plist-get data :keys)))))
|
||||
(cl--set-buffer-substring (plist-get data :cite-begin)
|
||||
(plist-get data :cite-end)
|
||||
cite-string)
|
||||
(search-forward (car (plist-get data :keys)))
|
||||
(goto-char (match-beginning 0))))
|
||||
|
||||
|
||||
|
||||
(provide 'org-ref-latex)
|
||||
|
||||
Reference in New Issue
Block a user