X-Git-Url: https://git.donarmstrong.com/?p=org-ref.git;a=blobdiff_plain;f=org-ref.org;h=14258685dc140c4e2ab3f089b72fcc15607272f0;hp=0c7e0e9549c0e8544482ffe4b86e7fc38aa788df;hb=4420a5ef0e7ca6eba8922b4b3b3758e63736b591;hpb=b561cfab15de147101e070a568f0ba343505ae41 diff --git a/org-ref.org b/org-ref.org index 0c7e0e9..1425868 100644 --- a/org-ref.org +++ b/org-ref.org @@ -128,8 +128,8 @@ There are some variables needed later to tell this library where you store your (defcustom org-ref-insert-cite-function - 'helm-bibtex - "Function to call to insert citation links. The default is `helm-bibtex'. org-ref modifies helm-bibtex a little bit to give org-mode citations, and to reorder default actions. You may use `org-ref-insert-cite-link' if you like the reftex interface." + 'org-ref-helm-insert-cite-link + "Function to call to insert citation links. The default is `org-ref-helm-insert-cite-link' which uses `helm-bibtex'. org-ref modifies helm-bibtex a little bit to give org-mode citations, and to reorder default actions. You may use `org-ref-insert-cite-link' if you like the reftex interface." :type 'function :group 'org-ref) @@ -218,11 +218,35 @@ We setup reftex here. We use a custom insert cite link function defined here: [[ ;; I do not remember why I put this next line in. It doesn't ;; work for org-files. Nothing very bad happens, but it gives ;; an annoying error. Commenting it out for now. - ;(reftex-parse-all) + ;(reftex-parse-all ) (make-local-variable 'reftex-cite-format) - (setq reftex-cite-format 'org) - (define-key org-mode-map (kbd org-ref-insert-cite-key) org-ref-insert-cite-function)) + (setq reftex-cite-format 'org)) + +;; define key for inserting citations +(define-key org-mode-map + (kbd org-ref-insert-cite-key) + org-ref-insert-cite-function) + +(when org-ref-show-citation-on-enter + (setq org-ref-message-timer + (run-with-idle-timer 0.5 t 'org-ref-link-message))) + +(defun org-ref-show-link-messages () + "Turn on link messages. You will see a message in the +minibuffer when on a cite, ref or label link." + (interactive) + (setq org-ref-message-timer + (run-with-idle-timer 0.5 t 'org-ref-link-message)) + ) +(defun org-ref-cancel-link-messages () + "Stop showing messages in minibuffer when on a link." + (interactive) + (cancel-timer org-ref-message-timer)) + +;; this approach caused the selected region to not be highlighted any more. +; (add-hook 'post-command-hook 'org-ref-link-message)) +; (remove-hook 'post-command-hook 'org-ref-link-message)) (add-hook 'org-mode-hook 'org-mode-reftex-setup) @@ -894,18 +918,21 @@ The label link provides a way to create labels in org-mode. We make it clickable *************** END #+BEGIN_SRC emacs-lisp :tangle org-ref.el + +(defun org-ref-count-labels (label) + (+ (count-matches (format "label:%s\\b[^-:]" label) (point-min) (point-max) t) + ;; for tblname, it is not enough to get word boundary + ;; tab-little and tab-little-2 match then. + (count-matches (format "^#\\+tblname:\\s-*%s\\b[^-:]" label) (point-min) (point-max) t) + (count-matches (format "\\label{%s}\\b" label) (point-min) (point-max) t) + ;; this is the org-format #+label: + (count-matches (format "^#\\+label:\\s-*%s\\b[^-:]" label) (point-min) (point-max) t))) + (org-add-link-type "label" (lambda (label) "on clicking count the number of label tags used in the buffer. A number greater than one means multiple labels!" - (message (format "%s occurences" - (+ (count-matches (format "label:%s\\b[^-:]" label) (point-min) (point-max) t) - ;; for tblname, it is not enough to get word boundary - ;; tab-little and tab-little-2 match then. - (count-matches (format "^#\\+tblname:\\s-*%s\\b[^-:]" label) (point-min) (point-max) t) - (count-matches (format "\\label{%s}\\b" label) (point-min) (point-max) t) - ;; this is the org-format #+label: - (count-matches (format "^#\\+label:\\s-*%s\\b[^-:]" label) (point-min) (point-max) t))))) + (message (format "%s occurences" (org-ref-count-labels label)))) (lambda (keyword desc format) (cond ((eq format 'html) (format "()" path)) @@ -2019,119 +2046,6 @@ And at the end of the document put \makeglossaries. (format "\\Glspl{%s}" path))))) #+END_SRC -#+RESULTS: -| Glspl | nil | (lambda (path desc format) (cond ((eq format (quote latex)) (format \Glspl{%s} path)))) | -| Gls | nil | (lambda (path desc format) (cond ((eq format (quote latex)) (format \Gls{%s} path)))) | -| glspl | nil | (lambda (path desc format) (cond ((eq format (quote latex)) (format \glspl{%s} path)))) | -| gls | nil | (lambda (path desc format) (cond ((eq format (quote latex)) (format \gls{%s} path)))) | -| newglossaryentry | nil | (lambda (path desc format) (cond ((eq format (quote latex)) (format \newglossaryentry{%s}{%s} path desc)))) | -| google | (lambda (link-string) (browse-url (format http://www.google.com/search?q=%s (url-hexify-string link-string)))) | nil | -| ResearcherID | (lambda (link-string) (browse-url (format http://www.researcherid.com/rid/%s link-string))) | nil | -| orcid | (lambda (link-string) (browse-url (format http://orcid.org/%s link-string))) | nil | -| message | org-mac-message-open | nil | -| mac-outlook | org-mac-outlook-message-open | nil | -| skim | org-mac-skim-open | nil | -| addressbook | org-mac-addressbook-item-open | nil | -| x-together-item | org-mac-together-item-open | nil | -| ans | (lambda (path) (let* ((fields (split-string path ::)) (label (nth 0 fields)) (data (nth 1 fields)) (data-file (format %s-%s.dat tq-userid label))) (let ((temp-file data-file) (temp-buffer (get-buffer-create (generate-new-buffer-name *temp file*)))) (unwind-protect (prog1 (save-current-buffer (set-buffer temp-buffer) (insert data)) (save-current-buffer (set-buffer temp-buffer) (write-region nil nil temp-file nil 0))) (and (buffer-name temp-buffer) (kill-buffer temp-buffer)))) (mygit (format git add %s data-file)) (mygit (format git commit -m "%s" data-file)) (mygit git push origin master))) | nil | -| mc | (lambda (link) (org-entry-put (point) ANSWER link) (save-restriction (save-excursion (org-narrow-to-subtree) (goto-char (point-max)) (if (bolp) nil (insert \n)) (insert (format # you chose %s link))))) | nil | -| exercise | (lambda (arg) (tq-check-internet) (tq-get-assignment arg)) | nil | -| solution | (lambda (label) (tq-check-internet) (let ((default-directory (file-name-as-directory (expand-file-name tq-root-directory)))) (if (file-exists-p solutions) nil (make-directory solutions)) (let ((default-directory (file-name-as-directory (expand-file-name solutions)))) (if (file-exists-p label) (progn (find-file (concat label / label .org)) (tq-update)) (mygit (format git clone %s@%s:solutions/%s tq-current-course tq-git-server label)) (find-file (concat label / label .org)))))) | nil | -| assignment | (lambda (arg) (tq-check-internet) (tq-get-assignment arg)) | nil | -| doi | doi-link-menu | nil | -| bibentry | org-ref-cite-onclick-minibuffer-menu | org-ref-format-bibentry | -| Autocites | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Autocites | -| autocites | org-ref-cite-onclick-minibuffer-menu | org-ref-format-autocites | -| supercites | org-ref-cite-onclick-minibuffer-menu | org-ref-format-supercites | -| Textcites | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Textcites | -| textcites | org-ref-cite-onclick-minibuffer-menu | org-ref-format-textcites | -| Smartcites | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Smartcites | -| smartcites | org-ref-cite-onclick-minibuffer-menu | org-ref-format-smartcites | -| footcitetexts | org-ref-cite-onclick-minibuffer-menu | org-ref-format-footcitetexts | -| footcites | org-ref-cite-onclick-minibuffer-menu | org-ref-format-footcites | -| Parencites | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Parencites | -| parencites | org-ref-cite-onclick-minibuffer-menu | org-ref-format-parencites | -| Cites | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Cites | -| cites | org-ref-cite-onclick-minibuffer-menu | org-ref-format-cites | -| fnotecite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-fnotecite | -| Pnotecite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Pnotecite | -| pnotecite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-pnotecite | -| Notecite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Notecite | -| notecite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-notecite | -| footfullcite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-footfullcite | -| fullcite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-fullcite | -| citeurl | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citeurl | -| citedate* | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citedate* | -| citedate | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citedate | -| citetitle* | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citetitle* | -| citetitle | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citetitle | -| Citeauthor* | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Citeauthor* | -| Autocite* | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Autocite* | -| autocite* | org-ref-cite-onclick-minibuffer-menu | org-ref-format-autocite* | -| Autocite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Autocite | -| autocite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-autocite | -| supercite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-supercite | -| parencite* | org-ref-cite-onclick-minibuffer-menu | org-ref-format-parencite* | -| cite* | org-ref-cite-onclick-minibuffer-menu | org-ref-format-cite* | -| Smartcite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Smartcite | -| smartcite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-smartcite | -| Textcite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Textcite | -| textcite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-textcite | -| footcitetext | org-ref-cite-onclick-minibuffer-menu | org-ref-format-footcitetext | -| footcite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-footcite | -| Parencite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Parencite | -| parencite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-parencite | -| Cite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Cite | -| Citeauthor | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Citeauthor | -| Citealp | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Citealp | -| Citealt | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Citealt | -| Citep | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Citep | -| Citet | org-ref-cite-onclick-minibuffer-menu | org-ref-format-Citet | -| citeyear* | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citeyear* | -| citeyear | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citeyear | -| citeauthor* | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citeauthor* | -| citeauthor | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citeauthor | -| citetext | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citetext | -| citenum | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citenum | -| citealp* | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citealp* | -| citealp | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citealp | -| citealt* | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citealt* | -| citealt | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citealt | -| citep* | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citep* | -| citep | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citep | -| citet* | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citet* | -| citet | org-ref-cite-onclick-minibuffer-menu | org-ref-format-citet | -| nocite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-nocite | -| cite | org-ref-cite-onclick-minibuffer-menu | org-ref-format-cite | -| eqref | (lambda (label) on clicking goto the label. Navigate back with C-c & (org-mark-ring-push) (widen) (goto-char (point-min)) (if (or (re-search-forward (format label:%s label) nil t) (re-search-forward (format \label{%s} label) nil t) (re-search-forward (format ^#\+label:\s-*\(%s\)\b label) nil t)) nil (org-mark-ring-goto) (error %s not found label)) (message go back with (org-mark-ring-goto) `C-c &`)) | (lambda (keyword desc format) (cond ((eq format (quote html)) (format (%s) path)) ((eq format (quote latex)) (format \eqref{%s} keyword)))) | -| nameref | (lambda (label) on clicking goto the label. Navigate back with C-c & (org-mark-ring-push) (widen) (if (or (progn (goto-char (point-min)) (re-search-forward (format \label{%s} label) nil t))) nil (org-mark-ring-goto) (error %s not found label)) (message go back with (org-mark-ring-goto) `C-c &`)) | (lambda (keyword desc format) (cond ((eq format (quote html)) (format (%s) path)) ((eq format (quote latex)) (format \nameref{%s} keyword)))) | -| pageref | (lambda (label) on clicking goto the label. Navigate back with C-c & (org-mark-ring-push) (widen) (if (or (progn (goto-char (point-min)) (re-search-forward (format label:%s\b label) nil t)) (progn (goto-char (point-min)) (re-search-forward (format \label{%s} label) nil t)) (progn (goto-char (point-min)) (re-search-forward (format ^#\+label:\s-*\(%s\)\b label) nil t)) (progn (goto-char (point-min)) (re-search-forward (format ^#\+tblname:\s-*\(%s\)\b label) nil t))) nil (org-mark-ring-goto) (error %s not found label)) (message go back with (org-mark-ring-goto) `C-c &`)) | (lambda (keyword desc format) (cond ((eq format (quote html)) (format (%s) path)) ((eq format (quote latex)) (format \pageref{%s} keyword)))) | -| ref | (lambda (label) on clicking goto the label. Navigate back with C-c & (org-mark-ring-push) (widen) (if (or (progn (goto-char (point-min)) (re-search-forward (format label:%s\b label) nil t)) (progn (goto-char (point-min)) (re-search-forward (format \label{%s} label) nil t)) (progn (goto-char (point-min)) (re-search-forward (format ^#\+label:\s-*\(%s\)\b label) nil t)) (progn (goto-char (point-min)) (re-search-forward (format ^#\+tblname:\s-*\(%s\)\b label) nil t))) nil (org-mark-ring-goto) (error %s not found label)) (org-show-entry) (message go back with (org-mark-ring-goto) `C-c &`)) | (lambda (keyword desc format) (cond ((eq format (quote html)) (format (%s) path)) ((eq format (quote latex)) (format \ref{%s} keyword)))) | -| label | (lambda (label) on clicking count the number of label tags used in the buffer. A number greater than one means multiple labels! (message (format %s occurences (+ (count-matches (format label:%s\b[^-:] label) (point-min) (point-max) t) (count-matches (format ^#\+tblname:\s-*%s\b[^-:] label) (point-min) (point-max) t) (count-matches (format \label{%s}\b label) (point-min) (point-max) t) (count-matches (format ^#\+label:\s-*%s\b[^-:] label) (point-min) (point-max) t))))) | (lambda (keyword desc format) (cond ((eq format (quote html)) (format () path)) ((eq format (quote latex)) (format \label{%s} keyword)))) | -| list-of-tables | org-ref-list-of-tables | (lambda (keyword desc format) (cond ((eq format (quote latex)) (format \listoftables)))) | -| list-of-figures | org-ref-list-of-figures | (lambda (keyword desc format) (cond ((eq format (quote latex)) (format \listoffigures)))) | -| addbibresource | (lambda (link-string) (let* ((bibfile) (object (org-element-context)) (link-string-beginning) (link-string-end)) (save-excursion (goto-char (org-element-property :begin object)) (search-forward link-string nil nil 1) (setq link-string-beginning (match-beginning 0)) (setq link-string-end (match-end 0))) (set (make-local-variable (quote reftex-default-addbibresource)) (split-string (org-element-property :path object) ,)) (save-excursion (if (search-forward , link-string-end 1 1) (setq key-end (- (match-end 0) 1)) (setq key-end (point)))) (save-excursion (if (search-backward , link-string-beginning 1 1) (setq key-beginning (+ (match-beginning 0) 1)) (setq key-beginning (point)))) (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end))) (find-file bibfile))) | (lambda (keyword desc format) (cond ((eq format (quote html)) (format )) ((eq format (quote latex)) (format \addbibresource{%s} keyword)))) | -| bibliographystyle | (lambda (arg) (message Nothing implemented for clicking here.)) | (lambda (keyword desc format) (cond ((eq format (quote latex)) (format \bibliographystyle{%s} keyword)))) | -| printbibliography | (lambda (arg) (message Nothing implemented for clicking here.)) | (lambda (keyword desc format) (cond ((eq format (quote org)) (org-ref-get-org-bibliography)) ((eq format (quote html)) (org-ref-get-html-bibliography)) ((eq format (quote latex)) \printbibliography))) | -| nobibliography | (lambda (link-string) (let* ((bibfile) (object (org-element-context)) (link-string-beginning) (link-string-end)) (save-excursion (goto-char (org-element-property :begin object)) (search-forward link-string nil nil 1) (setq link-string-beginning (match-beginning 0)) (setq link-string-end (match-end 0))) (set (make-local-variable (quote reftex-default-bibliography)) (split-string (org-element-property :path object) ,)) (save-excursion (if (search-forward , link-string-end 1 1) (setq key-end (- (match-end 0) 1)) (setq key-end (point)))) (save-excursion (if (search-backward , link-string-beginning 1 1) (setq key-beginning (+ (match-beginning 0) 1)) (setq key-beginning (point)))) (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end))) (find-file bibfile))) | (lambda (keyword desc format) (cond ((eq format (quote org)) (org-ref-get-org-bibliography)) ((eq format (quote ascii)) (org-ref-get-ascii-bibliography)) ((eq format (quote html)) (org-ref-get-html-bibliography)) ((eq format (quote latex)) (format \nobibliography{%s} (replace-regexp-in-string \.bib (mapconcat (quote identity) (mapcar (quote expand-file-name) (split-string keyword ,)) ,)))))) | -| bibliography | (lambda (link-string) (let* ((bibfile) (object (org-element-context)) (link-string-beginning) (link-string-end)) (save-excursion (goto-char (org-element-property :begin object)) (search-forward link-string nil nil 1) (setq link-string-beginning (match-beginning 0)) (setq link-string-end (match-end 0))) (set (make-local-variable (quote reftex-default-bibliography)) (split-string (org-element-property :path object) ,)) (save-excursion (if (search-forward , link-string-end 1 1) (setq key-end (- (match-end 0) 1)) (setq key-end (point)))) (save-excursion (if (search-backward , link-string-beginning 1 1) (setq key-beginning (+ (match-beginning 0) 1)) (setq key-beginning (point)))) (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end))) (find-file bibfile))) | (lambda (keyword desc format) (cond ((eq format (quote org)) (org-ref-get-org-bibliography)) ((eq format (quote ascii)) (org-ref-get-ascii-bibliography)) ((eq format (quote html)) (org-ref-get-html-bibliography)) ((eq format (quote latex)) (format \bibliography{%s} (replace-regexp-in-string \.bib (mapconcat (quote identity) (mapcar (quote expand-file-name) (split-string keyword ,)) ,)))))) | -| rmail | org-rmail-open | nil | -| mhe | org-mhe-open | nil | -| irc | org-irc-visit | nil | -| info | org-info-open | nil | -| gnus | org-gnus-open | nil | -| docview | org-docview-open | org-docview-export | -| bibtex | org-bibtex-open | nil | -| bbdb | org-bbdb-open | org-bbdb-export | -| pydoc | (lambda (link-string) (shell-command (format python -m pydoc %s link-string))) | nil | -| index | (lambda (path) (tq-index) (occur path)) | nil | -| attachfile | (lambda (link-string) (org-open-file link-string)) | (lambda (keyword desc format) (cond ((eq format (quote html)) (format )) ((eq format (quote latex)) (format \attachfile{%s} keyword)))) | -| msx | org-msx-open | nil | -| id | org-id-open | nil | -| file+emacs | org-open-file-with-emacs | nil | -| file+sys | org-open-file-with-system | nil | - - * Utilities ** create simple text citation from bibtex entry @@ -2760,6 +2674,98 @@ Sometimes it may be helpful to manually change the order of citations. These fun (add-hook 'org-shiftright-hook (lambda () (org-ref-swap-citation-link 1))) (add-hook 'org-shiftleft-hook (lambda () (org-ref-swap-citation-link -1))) #+END_SRC + +** Lightweight messages about links +To get a lighter weight message about the label, ref and cite links, we define a function that gives us the minibuffer message, without the menu. We run this in an idle timer. + +#+BEGIN_SRC emacs-lisp :tangle org-ref.el +(defun org-ref-get-label-context (label) + "Return a string of context around a label." + (save-excursion + (catch 'result + (goto-char (point-min)) + (when (re-search-forward + (format "label:%s\\b" label) nil t) + (throw 'result (buffer-substring + (progn + (previous-line) + (beginning-of-line) + (point)) + (progn + (forward-line 4) + (point))))) + + (goto-char (point-min)) + (when (re-search-forward + (format "\\label{%s}" label) nil t) + (throw 'result (buffer-substring + (progn + (previous-line) + (beginning-of-line) + (point)) + (progn + (forward-line 4) + (point))))) + + (goto-char (point-min)) + (when (re-search-forward + (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t) + (throw 'result (buffer-substring + (progn + (previous-line) + (beginning-of-line) + (point)) + (progn + (forward-line 4) + (point))))) + + (goto-char (point-min)) + (when (re-search-forward + (format "^#\\+tblname:\\s-*\\(%s\\)\\b" label) nil t) + (throw 'result (buffer-substring + (progn + (previous-line) + (beginning-of-line) + (point)) + (progn + (forward-line 4) + (point)))))))) + + +(defun org-ref-link-message () + "Print a minibuffer message about the link that point is on." + (interactive) + (when (eq major-mode 'org-mode) + (let* ((object (org-element-context)) + (type (org-element-property :type object))) + (save-excursion + (cond + ;; cite links + ((-contains? org-ref-cite-types type) + (message (org-ref-get-citation-string-at-point))) + + ;; message some context about the label we are referring to + ((string= type "ref") + (message (org-ref-get-label-context + (org-element-property :path object)))) + + ((string= type "eqref") + (message (org-ref-get-label-context + (org-element-property :path object)))) + + ;; message the count + ((string= type "label") + (let ((count (org-ref-count-labels + (org-element-property :path object)))) + ;; get plurality on occurrence correct + (message (concat + (number-to-string count) + " occurence" + (when (or (= count 0) + (> count 1)) + "s")))))))))) +#+END_SRC + * Aliases I like convenience. Here are some aliases for faster typing. @@ -2804,43 +2810,66 @@ I like convenience. Here are some aliases for faster typing. Now, let us define a function that inserts the cite links: #+BEGIN_SRC emacs-lisp :tangle org-ref.el (defun helm-bibtex-format-org-ref (keys) - "insert selected KEYS as cite link. Append KEYS if you are on a link." + "Insert selected KEYS as cite link. Append KEYS if you are on a link. +Technically, this function should return a string that is inserted by helm. This function does the insertion and gives helm an empty string to insert. This lets us handle appending to a link properly." (let* ((object (org-element-context))) (cond ;; case where we are in a link ((and (equal (org-element-type object) 'link) - (-contains? org-ref-cite-types (org-element-property :type object))) - (goto-char link-string-end) + (-contains? + org-ref-cite-types + (org-element-property :type object))) + ;;(message-box "in a link") + (goto-char (org-element-property :end object)) ;; sometimes there are spaces at the end of the link ;; this code moves point pack until no spaces are there (while (looking-back " ") (backward-char)) (insert (concat "," (mapconcat 'identity keys ",")))) ;; We are next to a link, and we want to append + ;; next to a link means one character back is on a link. ((save-excursion (backward-char) (and (equal (org-element-type (org-element-context)) 'link) - (-contains? org-ref-cite-types (org-element-property :type (org-element-context))))) + (-contains? + org-ref-cite-types + (org-element-property :type (org-element-context))))) + ;;(message-box "at end of a link") + ;; (goto-char (org-element-property :end object)) (while (looking-back " ") (backward-char)) (insert (concat "," (mapconcat 'identity keys ",")))) ;; insert fresh link (t + ;;(message-box "fresh link") (insert - (concat org-ref-default-citation-link + (concat (if helm-current-prefix-arg + (helm :sources `((name . "link types") + (candidates . ,org-ref-cite-types) + (action . (lambda (x) x)))) + org-ref-default-citation-link) ":" - (s-join keys ","))))))) + (s-join "," keys)))))) + ;; return empty string for helm + "") (setq helm-bibtex-format-citation-functions '((org-mode . helm-bibtex-format-org-ref))) +(defun org-ref-helm-insert-cite-link () + "org-ref function to use helm on the bibliography defined in the org-file." + (interactive) + (let ((helm-bibtex-bibliography (org-ref-find-bibliography))) + (helm-bibtex))) + (require 'helm-bibtex) #+END_SRC ** A helm click menu - +This code provides a helm interface to things you can do when you click on a citation link. This is an alternative to the minibuffer menu. #+BEGIN_SRC emacs-lisp :tangle org-ref.el (defun org-ref-get-citation-string-at-point () + "Get a string of a formatted citation" (interactive) (let* ((results (org-ref-get-bibtex-key-and-file)) (key (car results)) @@ -2852,8 +2881,8 @@ Now, let us define a function that inserts the cite links: (org-ref-bib-citation))))) (defun org-ref-cite-candidates () - "Generate the list of possible candidates. -Check for pdf and doi, and add appropriate functions." + "Generate the list of possible candidates for click actions on a cite link. +Checks for pdf and doi, and add appropriate functions." (interactive) (let* ((results (org-ref-get-bibtex-key-and-file)) (key (car results)) @@ -2871,23 +2900,35 @@ Check for pdf and doi, and add appropriate functions." ;; I like this better than bibtex-url which does not always find ;; the urls (bibtex-autokey-get-field "doi")))) - (candidates `( ;;the first candidate is a brief summary - ("Quit" . org-ref-citation-at-point) + (candidates `(("Quit" . org-ref-citation-at-point) ("Open bibtex entry" . org-ref-open-citation-at-point)))) + ;; for some reason, when there is no doi or url, they are returned as "". I + ;; prefer nil so we correct this here. + (when (string= doi "") (setq doi nil)) + (when (string= url "") (setq url nil)) - - (when (file-exists-p pdf-file) + ;; Conditional pdf functions + (if (file-exists-p pdf-file) + (add-to-list + 'candidates + '("Open pdf" . org-ref-open-pdf-at-point) + t) (add-to-list 'candidates - '("Open pdf" . org-ref-open-pdf-at-point) - t - )) + '("Try to get pdf" . (lambda () + (save-window-excursion + (org-ref-open-citation-at-point) + (bibtex-beginning-of-entry) + (doi-utils-get-bibtex-entry-pdf)))) + t)) + (add-to-list 'candidates '("Open notes" . org-ref-open-notes-at-point) t) + ;; conditional url and doi functions (when (or url doi) (add-to-list 'candidates @@ -2972,20 +3013,6 @@ This is a list of cons cells '((\"description\" . action)). The action function #+RESULTS: : org-ref-cite-click-helm -To get a lighter weight message about the cite link, we define a function that gives us the minibuffer message, without the menu. We add it to a hook that updates after every command, including cursor movements. - -#+BEGIN_SRC emacs-lisp :tangle org-ref.el -(defun org-ref-cite-link-p () (interactive) - (let* ((object (org-element-context)) - (type (org-element-property :type object))) - ;; We only want this to work on citation links - (when (-contains? org-ref-cite-types type) - (message (org-ref-get-citation-string-at-point))))) - -(when org-ref-show-citation-on-enter - (add-hook 'post-command-hook 'org-ref-cite-link-p)) -#+END_SRC - * End of code #+BEGIN_SRC emacs-lisp :tangle org-ref.el (provide 'org-ref)