Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.2 KiB

5 years ago
  1. ;;; org-ref-latex.el --- org-ref functionality for LaTeX files -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015 John Kitchin
  3. ;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
  4. ;; Keywords: languages
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary: Make cites in LaTeX documents clickable, and with tooltips.
  16. ;; We use font-lock to add some functionality to the
  17. ;;
  18. ;;; Code:
  19. (require 'org-ref-core)
  20. (defvar latex-mode-map)
  21. (defvar org-ref-cite-types)
  22. (defvar org-ref-latex-cite-re
  23. (concat "\\\\\\(" (mapconcat
  24. (lambda (x)
  25. (replace-regexp-in-string "\\*" "\\\\*" x))
  26. org-ref-cite-types
  27. "\\|")
  28. "\\)"
  29. "\\(\\[[^}]*\\)?" ; optional []
  30. "\\(\\[[^}]*\\)?" ; optional []
  31. "{\\([^}]*\\)}")
  32. "Regexp for LaTeX citations. \citetype[optional]{some,keys}.
  33. The clickable part are the keys.")
  34. (defun org-ref-latex-get-key ()
  35. "Figure out what key the cursor is on."
  36. (let (start end)
  37. ;; look back for , or {
  38. (save-excursion
  39. (re-search-backward ",\\|{")
  40. (setq start (+ 1 (point))))
  41. ;; look forward to , or }
  42. (save-excursion
  43. (re-search-forward ",\\|}")
  44. (setq end (- (point) 1)))
  45. (buffer-substring-no-properties start end)))
  46. ;;;###autoload
  47. (defun org-ref-latex-debug ()
  48. (interactive)
  49. (message-box "%S\n%S\n%S\n%S"
  50. (org-ref-latex-get-key)
  51. (org-ref-find-bibliography)
  52. (org-ref-get-bibtex-key-and-file (org-ref-latex-get-key))
  53. (ignore-errors
  54. (org-ref-latex-help-echo nil nil (point)))))
  55. (defun org-ref-latex-jump-to-bibtex (&optional key)
  56. "Jump to the KEY at point."
  57. (let ((results (org-ref-get-bibtex-key-and-file
  58. (or key (org-ref-latex-get-key)))))
  59. (find-file (cdr results))
  60. (bibtex-search-entry (car results))))
  61. ;;;###autoload
  62. (defun org-ref-latex-click ()
  63. "Jump to entry clicked on."
  64. (interactive)
  65. (helm :sources '(((name . "Actions")
  66. (candidates . (("Open Bibtex entry" . org-ref-latex-jump-to-bibtex)
  67. ("Bibtex entry menu" . (lambda ()
  68. (org-ref-latex-jump-to-bibtex)
  69. (org-ref-bibtex-hydra/body)))))
  70. (action . (lambda (f)
  71. (funcall f)))))))
  72. (defun org-ref-latex-help-echo (_window _object position)
  73. "Get tool tip for a key in WINDOW for OBJECT at POSITION."
  74. (save-excursion
  75. (goto-char position)
  76. (let* ((key (org-ref-latex-get-key))
  77. (results (org-ref-get-bibtex-key-and-file key))
  78. (bibfile (cdr results))
  79. citation
  80. tooltip)
  81. (setq citation
  82. (if bibfile
  83. (save-excursion
  84. (with-temp-buffer
  85. (insert-file-contents bibfile)
  86. (bibtex-set-dialect
  87. (parsebib-find-bibtex-dialect) t)
  88. (bibtex-search-entry key)
  89. (org-ref-bib-citation)))
  90. "!!! No entry found !!!"))
  91. (setq tooltip
  92. (with-temp-buffer
  93. (insert citation)
  94. (fill-paragraph)
  95. (buffer-string)))
  96. tooltip)))
  97. (defun org-ref-next-latex-cite (&optional limit)
  98. "Font-lock function to make cites in LaTeX documents clickable."
  99. (when (re-search-forward org-ref-latex-cite-re limit t)
  100. (setq font-lock-extra-managed-props (delq 'help-echo font-lock-extra-managed-props))
  101. (add-text-properties
  102. (match-beginning 3)
  103. (match-end 3)
  104. `(mouse-face
  105. highlight
  106. local-map ,(let ((map (copy-keymap latex-mode-map)))
  107. (define-key map [mouse-1]
  108. 'org-ref-latex-click)
  109. map)
  110. help-echo org-ref-latex-help-echo))))
  111. (defun org-ref-latex-cite-on ()
  112. "Add the font-lock on for citations."
  113. (font-lock-add-keywords
  114. nil
  115. '((org-ref-next-latex-cite 3 font-lock-constant-face))))
  116. (add-hook 'latex-mode-hook 'org-ref-latex-cite-on)
  117. (add-hook 'LaTeX-mode-hook 'org-ref-latex-cite-on)
  118. (provide 'org-ref-latex)
  119. ;;; org-ref-latex.el ends here