]> git.donarmstrong.com Git - org-ref.git/blob - jmax-bibtex.el
export 'eqref' in html just as in latex
[org-ref.git] / jmax-bibtex.el
1 ;;; jmax-bibtex.el --- jmax-bibtex utilities
2
3 ;; Copyright(C) 2014 John Kitchin
4
5 ;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
6 ;; URL: https://github.com/jkitchin/org-ref
7 ;; Version: 0.1
8 ;; Keywords: org-mode, bibtex
9 ;; Package-Requires: ((org-ref) (s) (dash) (doi-utils) (key-chord))
10
11 ;; This file is not currently part of GNU Emacs.
12
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program ; see the file COPYING.  If not, write to
25 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;; jmax-bibtex-generate-longtitles
31 ;; jmax-bibtex-generate-shorttitles
32 ;; jmax-stringify-journal-name :: replace a journal name with a string in
33 ;; `jmax-bibtex-journal-abbreviations'
34 ;; jmax-set-journal-string :: in a bibtex entry run this to replace the
35 ;; journal with a string
36 ;;
37 ;; jmax-title-case-article :: title case the title in an article
38 ;; jmax-sentence-case-article :: sentence case the title in an article.
39
40 ;; jmax-replace-nonascii :: replace nonascii characters in a bibtex
41 ;; entry. Replacements are in `jmax-nonascii-latex-replacements'.
42 ;;
43 ;; jmax-title-case-article
44 ;; jmax-sentence-case-article
45 ;;
46 ;; jmax-bibtex-next-entry :: bound to M-n
47 ;; jmax-bibtex-previous-entry :: bound to M-p
48 ;;
49 ;; Functions to act on a bibtex entry or file
50 ;; jmax-bibtex-hydra/body gives a hydra menu to a lot of useful functions.
51 ;; jmax-bibtex-new-entry/body gives a hydra menu to add new bibtex entries.
52 ;; jmax-bibtex-file/body gives a hydra menu of actions for the bibtex file
53 ;;
54 ;; jmax-bibtex :: a deprecated menu of actions
55
56 (require 'hydra)
57 (require 'key-chord)
58
59 ;;; Code:
60 ;; * Custom variables
61 (defgroup jmax-bibtex nil
62   "Customization group for jmax-bibtex.")
63
64
65 (defcustom jmax-bibtex-hydra-key-chord
66   nil
67   "key-chord to run `jmax-bibtex-hydra'.
68 I like \"jj\""
69   :type 'string
70   :group 'jmax-bibtex)
71
72
73 (defcustom jmax-bibtex-hydra-key-binding
74   nil
75   "key-binding to run `jmax-bibtex-hydra'.
76 I like \C-cj."
77   :type 'string
78   :group 'jmax-bibtex)
79
80 ;; * Journal abbreviations
81 (defvar jmax-bibtex-journal-abbreviations
82   '()
83   "List of (string journal-full-name journal-abbreviation).  Find abbreviations at http://cassi.cas.org/search.jsp.")
84
85 (setq jmax-bibtex-journal-abbreviations
86       '(("ACR" "Accounts of Chemical Research" "Acc. Chem. Res.")
87         ("ACAT" "ACS Catalysis" "ACS Catal.")
88         ("AM" "Acta Materialia" "Acta Mater.")
89         ("AMM" "Acta Metallurgica et Materialia" "Acta Metall. Mater.")
90         ("AEM" "Advanced Energy Materials" "Adv. Energy Mater.")
91         ("AAMI" "ACS Applied Materials \\& Interfaces"
92          "ACS Appl. Mater. Interfaces")
93         ("AMiner" "American Mineralogist" "Am. Mineral.")
94         ("AngC" "Angewandte Chemie-International Edition"
95          "Angew. Chem. Int. Edit.")
96         ("APLM" "APL Materials" "APL Mat.")
97         ("ACBE" "Applied Catalysis B: Environmental" "Appl. Catal. B-Environ.")
98         ("APL" "Applied Physics Letters" "Appl. Phys. Lett.")
99         ("ASS" "Applied Surface Science" "Appl. Surf. Sci.")
100         ("CL" "Catalysis Letters" "Catal. Lett.")
101         ("CC" "Catalysis Communications" "Catal. Commun.")
102         ("CST" "Catalysis Science & Technology" "Catal. Sci. Technol.")
103         ("CT" "Catalysis Today" "Catal. Today")
104         ("ChC" "Chemical Communications" "Chem. Commun.")
105         ("CPL" "Chemical Physics Letters" "Chem. Phys. Lett")
106         ("CR" "Chemical Reviews" "Chem. Rev.")
107         ("CSR" "Chemical Society Reviews" "Chem. Soc. Rev.")
108         ("CSR" "Chemical Society Reviews" "Chem. Soc. Rev.")
109         ("CM" "Chemistry of Materials" "Chem. Mater.")
110         ("CSA" "Colloids and Surfaces, A: Physicochemical and Engineering Aspects"
111          "Colloids Surf., A")
112         ("CF" "Combustion and Flame" "Combust. Flame")
113         ("CPMS" "Computational Materials Science" "Comp. Mater. Sci.")
114         ("CPC" "Computer Physics Communications" "Comput. Phys. Commun.")
115         ("CSE" "Computing in Science \\& Engineering" "Comput. Sci. Eng.")
116         ("CGD" "Crystal Growth \\& Design" "Cryst. Growth Des.")
117         ("CEC" "CrystEngComm" "CrystEngComm")
118         ("EA" "Electrochimica Acta" "Electrochim. Acta")
119         ("ECST" "ECS Transactions" "ECS Trans.")
120         ("EES" "Energy \\& Environmental Science" "Energy Environ. Sci.")
121         ("HPR" "High Pressure Research" "High Pressure Res.")
122         ("IC" "Inorganic Chemistry" "Inorg. Chem.")
123         ("IECR" "Industrial \\& Engineering Chemistry Research"
124          "Ind. Eng. Chem. Res.")
125         ("JJAP" "Japanese Journal of Applied Physics" "Jpn. J. Appl. Phys.")
126         ("JMatR" "Journal of  Materials Research" "J. Mater. Res.")
127         ("JALC" "Journal of Alloys and Compounds" "J. Alloy Compd.")
128         ("JAC" "Journal of Applied Crystallography" "J. Appl. Crystallogr.")
129         ("JAE" "Journal of Applied Electrochemistry" "J. Appl. Electrochem.")
130         ("JAP" "Journal of Applied Physics" "J. Appl. Phys.")
131         ("JC" "Journal of Catalysis" "J. Catal.")
132         ("JCP" "Journal of Chemical Physics" "J. Chem. Phys.")
133         ("JCC" "Journal of Computational Chemistry" "J. Comput. Chem.")
134         ("JCG" "Journal of Crystal Growth" "J. Crys. Growth")
135         ("JMC" "Journal of Materials Chemistry" "J. Mater. Chem.")
136         ("JMC" "Journal of Materials Chemistry" "J. Mater. Chem.")
137         ("JMSL" "Journal of Materials Science Letters" "J. Mater. Sci. Lett.")
138         ("JMS" "Journal of Membrane Science" "J. Memb. Sci.")
139         ("JPE" "Journal of Phase Equilibria" "J. Phase Equilib.")
140         ("JPCS" "Journal of Physics and Chemistry of Solids"
141          "J. Phys. Chem. Solids")
142         ("JPCM" "Journal of Physics: Condensed Matter"
143          "J. Phys.: Condens. Matter")
144         ("JPS" "Journal of Power Sources" "J. Power Sources")
145         ("JSSC" "Journal of Solid State Chemistry" "J. Solid State Chem.")
146         ("JACerS" "Journal of the American Ceramic Society" "J. Am. Ceram. Soc.")
147         ("JACS" "Journal of the American Chemical Society" "J. Am. Chem. Soc.")
148         ("JASIST" "Journal of the American Society for Information Science and Technology"
149          "J. Am. Soc. Inf. Sci. Technol.")
150         ("JES" "Journal of The Electrochemical Society" "J. Electrochem. Soc.")
151         ("JEaC" "Journal of Electroanalytical Chemistry" "J. Electroanal. Chem.")
152         ("JMS" "Journal of Membrane Science" "J. Memb. Sci.")
153         ("JRS" "Journal of Raman Spectroscopy" "J. Raman Spectrosc.")
154         ("JVST" "Journal of Vacuum Science \\& Technology A"
155          "J. Vac. Sci. Technol. A")
156         ("ML" "Materials Letters" "Mater. Lett.")
157         ("MSE-BS" "Materials Science and Engineering B" "Mat. Sci. Eng. B-Solid")
158         ("MOLSIM" "Molecular Simulation" "Mol. Sim.")
159         ("Nature" "Nature" "Nature")
160         ("NM" "Nature Materials" "Nat. Mater.")
161         ("NC" "Nature Chemistry" "Nat. Chem.")
162         ("PML" "Philosophical Magazine Letters" "Phil. Mag. Lett.")
163         ("PMA" "Philosophical Magazine A" "Phil. Mag. A")
164         ("PA" "Physica A: Statistical Mechanics and its Applications" "Physica A")
165         ("PB" "Physica B-Condensed Matter" "Physica B")
166         ("PCCP" "Physical Chemistry Chemical Physics" "Phys. Chem. Chem. Phys.")
167         ("PSSB" "physica status solidi (b)" "Phys. Status Solidi B")
168         ("PRA" "Physical Review A" "Phys. Rev. A")
169         ("PRB" "Physical Review B" "Phys. Rev. B")
170         ("PRL" "Physical Review Letters" "Phys. Rev. Lett.")
171         ("PCM" "Physics and Chemistry of Minerals" "Phys. Chem. Miner.")
172         ("PNAS" "Proceedings of the National Academy of Sciences of the United States of America"
173          "Proc. Natl. Acad. Sci. U. S. A.")
174         ("PSurfSci" "Progress in Surface Science" "Prog. Surf. Sci.")
175         ("Science" "Science" "Science")
176         ("SM" "Scripta Materialia" "Scr. Mater.")
177         ("SABC" "Sensors and Actuators B: Chemical" "Sensor. Actuat. B-Chem.")
178         ("SS" "Surface Science" "Surf. Sci.")
179         ("EPJB" "The European Physical Journal B" "Eur. Phys. J. B")
180         ("JPC" "The Journal of Physical Chemistry" "J. Phys. Chem.")
181         ("JPCB" "The Journal of Physical Chemistry B" "J. Phys. Chem. B")
182         ("JPCC" "The Journal of Physical Chemistry C" "J. Phys. Chem. C")
183         ("JPCL" "The Journal of Physical Chemistry Letters"
184          "J. Phys. Chem. Lett.")
185         ("JCP" "The Journal of Chemical Physics" "J. Chem. Phys.")
186         ("MSMSE" "Modelling and Simulation in Materials Science and Engineering"
187          "Modell. Simul. Mater. Sci. Eng.")
188         ("TSF" "Thin Solid Films" "Thin Solid Films")
189         ("TC" "Topics in Catalysis" "Top. Catal.")
190         ("WR" "Water Research" "Water Res.")))
191
192
193 (defun jmax-bibtex-generate-longtitles ()
194   "Generate longtitles.bib which are @string definitions.
195 The full journal names are in `jmax-bibtex-journal-abbreviations'."
196   (interactive)
197   (with-temp-file "longtitles.bib"
198     (dolist (row jmax-bibtex-journal-abbreviations)
199       (insert (format "@string{%s=\"%s\"}\n"
200                       (nth 0 row)
201                       (nth 1 row))))))
202
203
204 (defun jmax-bibtex-generate-shorttitles ()
205     "Generate shorttitles.bib which are @string definitions.
206 The abbreviated journal names in `jmax-bibtex-journal-abbreviations'."
207   (interactive)
208   (with-temp-file "shorttitles.bib"
209     (dolist (row jmax-bibtex-journal-abbreviations)
210       (insert (format "@string{%s=\"%s\"}\n"
211                       (nth 0 row)
212                       (nth 2 row))))))
213
214
215 (defun jmax-stringify-journal-name (&optional key start end)
216   "Replace journal name in a bibtex entry with a string.
217 The strings are defined in
218 `jmax-bibtex-journal-abbreviations'.  The optional arguments KEY,
219 START and END allow you to use this with `bibtex-map-entries'"
220   (interactive)
221   (bibtex-beginning-of-entry)
222   (when
223       (string= "article"
224                (downcase
225                 (cdr (assoc "=type=" (bibtex-parse-entry)))))
226     (let* ((full-names (mapcar
227                         (lambda (row)
228                           (cons  (nth 1 row) (nth 0 row)))
229                         jmax-bibtex-journal-abbreviations))
230            (abbrev-names (mapcar
231                           (lambda (row)
232                             (cons  (nth 2 row) (nth 0 row)))
233                           jmax-bibtex-journal-abbreviations))
234            (journal (s-trim (bibtex-autokey-get-field "journal")))
235            (bstring (or
236                      (cdr (assoc journal full-names))
237                      (cdr (assoc journal abbrev-names)))))
238       (when bstring
239         (bibtex-set-field "journal" bstring t)
240         (bibtex-fill-entry)))))
241
242 (defun jmax-helm-set-journal-string ()
243   "Helm interface to set a journal string."
244   (interactive)
245   (bibtex-set-field
246    "journal"
247    (helm :sources `((name . "journal")
248                     (candidates . ,(mapcar
249                                     (lambda (x)
250                                       (cons (format "%s | %s"  (nth 1 x) (nth 2 x))
251                                             (car x)))
252                                     jmax-bibtex-journal-abbreviations))
253                     (action . (lambda (x) (identity x))))
254          :input (s-trim (bibtex-autokey-get-field "journal")))
255    t)
256   (bibtex-fill-entry)
257   (bibtex-clean-entry))
258
259
260 (defun jmax-set-journal-string (full-journal-name)
261   "Set a bibtex journal name to the string that represents FULL-JOURNAL-NAME.
262 This is defined in `jmax-bibtex-journal-abbreviations'."
263   (interactive (list
264                 (ido-completing-read
265                  "Journal: "
266                  (mapcar
267                   (lambda (x)
268                     (nth 1 x))
269                   jmax-bibtex-journal-abbreviations))))
270   ;; construct data alist for the string lookup.
271   (let ((alist (mapcar
272                 (lambda (x)
273                   (cons (nth 1 x) (nth 0 x)))
274                 jmax-bibtex-journal-abbreviations)))
275     (bibtex-set-field "journal" (cdr (assoc full-journal-name alist)) t)
276     (bibtex-fill-entry)
277     (bibtex-clean-entry)))
278
279 ;; * Non-ascii character replacement
280 ;; see https://github.com/fxcoudert/tools/blob/master/doi2bib for more replacements
281 (defvar jmax-nonascii-latex-replacements
282   '()
283   "Cons list of non-ascii characters and their LaTeX representations.")
284
285 (setq jmax-nonascii-latex-replacements
286       '(("í" . "{\\\\'i}")
287         ("æ" . "{\\\\ae}")
288         ("ć" . "{\\\\'c}")
289         ("é" . "{\\\\'e}")
290         ("ä" . "{\\\\\"a}")
291         ("è" . "{\\\\`e}")
292         ("à" . "{\\\\`a}")
293         ("á" . "{\\\\'a}")
294         ("ø" . "{\\\\o}")
295         ("ë" . "{\\\\\"e}")
296         ("ü" . "{\\\\\"u}")
297         ("ñ" . "{\\\\~n}")
298         ("ņ" . "{\\\\c{n}}")
299         ("å" . "{\\\\aa}")
300         ("ö" . "{\\\\\"o}")
301         ("Á" . "{\\\\'A}")
302         ("á" . "{\\\\'a}")
303         ("í" . "{\\\\'i}")
304         ("ó" . "{\\\\'o}")
305         ("ó" . "{\\\\'o}")
306         ("ú" .  "{\\\\'u}")
307         ("ú" . "{\\\\'u}")
308         ("š" . "{\\\\v{s}}")
309         ("ř"  . "{\\\\v{r}}")
310         ("İ" . "{\\\\.I}")
311         ("ğ" . "{\\\\u{g}}")
312         ("δ" . "$\\\\delta$")
313         ("ç" . "{\\\\c{c}}")
314         ("ß" . "{\\\\ss}")
315         ("≤" . "$\\\\le$")
316         ("<" . "$\\\\lt$")
317         ("θ" . "$\\\\theta$")
318         ("μ" . "$\\\\mu$")
319         ("→" . "$\\\\rightarrow$")
320         ("⇌" . "$\\\\leftrightharpoons$")
321         ("×" . "$\\\\times$")
322         ("°" . "$\\\\deg$")
323         ("ş" . "{\\\\c{s}}")
324         ("ı" . "i")                    ; I think this is a turkish i
325         ;; I think these are non-ascii spaces. there seems to be more than one.
326         (" " . " ")
327         (" " . " ")
328         ("–" . "-")
329         ("−" . "-")
330         ("–" . "-")
331         ("—" . "-")
332         ("‘" . "'")
333         ("’" . "'")
334         ("’" . "'")
335         ("“" . "\"")
336         ("’" . "'")
337         ("”" . "\"")))
338
339 (defun jmax-replace-nonascii ()
340   "Hook function to replace non-ascii characters in a bibtex entry."
341
342   (interactive)
343   (save-restriction
344     (bibtex-narrow-to-entry)
345     (goto-char (point-min))
346     (dolist (char (mapcar (lambda (x) (car x)) jmax-nonascii-latex-replacements))
347       (while (re-search-forward char nil t)
348         (replace-match (cdr (assoc char jmax-nonascii-latex-replacements))))
349       (goto-char (point-min))))
350   (save-buffer))
351
352 (add-hook 'org-ref-clean-bibtex-entry-hook 'jmax-replace-nonascii)
353
354 ;; * Title case transformations
355 (defvar jmax-lower-case-words
356   '("a" "an" "on" "and" "for"
357     "the" "of" "in")
358   "List of words to keep lowercase when changing case in a title.")
359
360
361 (defun jmax-title-case-article (&optional key start end)
362   "Convert a bibtex entry article title to title-case.
363 The arguments KEY, START and END are optional, and are only there
364 so you can use this function with `bibtex-map-entries' to change
365 all the title entries in articles."
366   (interactive)
367   (bibtex-beginning-of-entry)
368
369   (let* ((title (bibtex-autokey-get-field "title"))
370          (words (split-string title))
371          (start 0))
372     (when
373         (string= "article" (downcase (cdr (assoc "=type=" (bibtex-parse-entry)))))
374       (setq words (mapcar
375                    (lambda (word)
376                      (if (or
377                           ;; match words containing {} or \ which are probably
378                           ;; LaTeX or protected words
379                           (string-match "\\$\\|{\\|}\\|\\\\" word)
380                           ;; these words should not be capitalized, unless they
381                           ;; are the first word
382                           (-contains? jmax-lower-case-words (s-downcase word)))
383                          word
384                        (s-capitalize word)))
385                    words))
386
387       ;; Check if first word should be capitalized
388       (when (-contains? jmax-lower-case-words (car words))
389         (setf (car words) (s-capitalize (car words))))
390
391       (setq title (mapconcat 'identity words " "))
392
393       ;; Capitalize letters after a dash
394       (while
395           (string-match "[a-zA-Z]-\\([a-z]\\)" title start)
396         (let ((char (substring title (match-beginning 1) (match-end 1))))
397           (setf (substring title (match-beginning 1) (match-end 1))
398                 (format "%s" (upcase char)))
399           (setq start (match-end 1))))
400
401       ;; this is defined in doi-utils
402       (bibtex-set-field
403        "title"
404        title)
405       (bibtex-fill-entry))))
406
407 (add-hook 'org-ref-clean-bibtex-entry-hook 'jmax-title-case-article)
408
409
410 (defun jmax-sentence-case-article (&optional key start end)
411   "Convert a bibtex entry article title to sentence-case.
412 The arguments KEY, START and END are optional, and are only there
413 so you can use this function with `bibtex-map-entries' to change
414 all the title entries in articles."
415   (interactive)
416   (bibtex-beginning-of-entry)
417
418   (let* ((title (bibtex-autokey-get-field "title"))
419          (words (split-string title))
420          (start 0))
421     (when
422         (string= "article" (downcase (cdr (assoc "=type=" (bibtex-parse-entry)))))
423       (setq words (mapcar
424                    (lambda (word)
425                      (if
426                          ;; match words containing {} or \ which are probably
427                          ;; LaTeX or protected words
428                          (string-match "\\$\\|{\\|}\\|\\\\" word)
429                          word
430                        (s-downcase word)))
431                    words))
432
433       ;; capitalize first word
434       (setf (car words) (s-capitalize (car words)))
435
436       ;; join the words
437       (setq title (mapconcat 'identity words " "))
438
439       ;; capitalize a word after a :, eg. a subtitle, and protect it
440       (while
441           (string-match "[a-z]:\\s-+\\([A-Z]\\)" title start)
442         (let ((char (substring title (match-beginning 1) (match-end 1))))
443           (setf (substring title (match-beginning 1) (match-end 1))
444 ;;              (format "{%s}" (upcase char)))
445                 (format "%s" (upcase char)))
446           (setq start (match-end 1))))
447
448       ;; this is defined in doi-utils
449       (bibtex-set-field
450        "title" title)
451
452       ;; clean and refill entry so it looks nice
453       (bibtex-clean-entry)
454       (bibtex-fill-entry))))
455
456 ;; * Navigation in bibtex file
457 (defun jmax-bibtex-next-entry (&optional n)
458   "Jump to the beginning of the next bibtex entry.
459 N is a prefix argument.  If it is numeric, jump that many entries
460 forward.  Negative numbers do nothing."
461   (interactive "P")
462   ;; Note if we start at the beginning of an entry, nothing
463   ;; happens. We need to move forward a char, and call again.
464   (when (= (point) (save-excursion
465                      (bibtex-beginning-of-entry)))
466     (forward-char)
467     (jmax-bibtex-next-entry))
468
469   ;; search forward for an entry
470   (when
471       (re-search-forward bibtex-entry-head nil t (and (numberp n) n))
472     ;; go to beginning of the entry
473     (bibtex-beginning-of-entry)))
474
475
476 (defun jmax-bibtex-previous-entry (&optional n)
477   "Jump to beginning of the previous bibtex entry.
478 N is a prefix argument.  If it is numeric, jump that many entries back."
479   (interactive "P")
480   (bibtex-beginning-of-entry)
481  (when
482      (re-search-backward bibtex-entry-head nil t (and (numberp n) n))
483    (bibtex-beginning-of-entry)))
484
485
486 (defun jmax-bibtex-mode-keys ()
487   "Modify keymaps used by `bibtex-mode'."
488   (local-set-key (kbd "M-n") 'jmax-bibtex-next-entry)
489   (local-set-key (kbd "M-p") 'jmax-bibtex-previous-entry))
490
491 ;; add to bibtex-mode-hook
492 (add-hook 'bibtex-mode-hook 'jmax-bibtex-mode-keys)
493
494 ;; * Functions to act on an entry with a doi
495 (defun jmax-bibtex-entry-doi ()
496   "Get doi from entry at point."
497   (interactive)
498   (save-excursion
499     (bibtex-beginning-of-entry)
500     (reftex-get-bib-field "doi" (bibtex-parse-entry t))))
501
502
503 (defun jmax-bibtex-wos ()
504   "Open bibtex entry in Web Of Science if there is a DOI."
505   (interactive)
506   (doi-utils-wos (jmax-bibtex-entry-doi)))
507
508
509 (defun jmax-bibtex-wos-citing ()
510   "Open citing articles for bibtex entry in Web Of Science if there is a DOI."
511   (interactive)
512   (doi-utils-wos-citing (jmax-bibtex-entry-doi)))
513
514
515 (defun jmax-bibtex-wos-related ()
516   "Open related articles for bibtex entry in Web Of Science if there is a DOI."
517   (interactive)
518   (doi-utils-wos-related (jmax-bibtex-entry-doi)))
519
520
521 (defun jmax-bibtex-wos-citing ()
522   "Open citing articles for bibtex entry in Web Of Science if there is a DOI."
523   (interactive)
524   (doi-utils-wos-citing (jmax-bibtex-entry-doi)))
525
526
527 (defun jmax-bibtex-crossref ()
528   "Open the bibtex entry in Crossref by its doi."
529   (interactive)
530   (doi-utils-crossref (jmax-bibtex-entry-doi)))
531
532
533 (defun jmax-bibtex-google-scholar ()
534   "Open the bibtex entry at point in google-scholar by its doi."
535   (interactive)
536   (doi-utils-google-scholar (jmax-bibtex-entry-doi)))
537
538
539 (defun jmax-bibtex-pubmed ()
540   "Open the bibtex entry at point in Pubmed by its doi."
541   (interactive)
542   (doi-utils-pubmed (jmax-bibtex-entry-doi)))
543
544
545 (defun jmax-bibtex-pdf (doi)
546   "Open the pdf for the bibtex entry at point.
547 Thin wrapper to get `jmax-bibtex' to open pdf, because it calls
548 functions with a DOI argument."
549   (interactive)
550   (org-ref-open-bibtex-pdf))
551
552
553 ;; * Hydra menus
554 ;; ** Hydra menu for bibtex entries
555 ;; hydra menu for actions on bibtex entries
556 (defhydra jmax-bibtex-hydra (:color blue)
557    "
558 _p_: Open pdf     _y_: Copy key               _n_: New entry     _w_: WOS
559 _b_: Open url     _f_: Copy formatted entry   _o_: Copy entry    _c_: WOS citing
560 _r_: Refile entry _k_: Add keywords           _d_: delete entry  _a_: WOS related
561 _e_: Email entry  _K_: Edit keywords          _L_: clean entry   _P_: Pubmed
562 _U_: Update entry _N_: Open notes             _R_: Crossref      _g_: Google Scholar
563 _s_: Sort entry   _a_: Remove nonascii        _h_: helm-bibtex   _q_: quit
564 _u_: Update field _F_: file funcs
565 _T_: Title case
566 _S_: Sentence case
567 "
568    ("p" org-ref-open-bibtex-pdf)
569    ("P" jmax-bibtex-pubmed)
570    ("w" jmax-bibtex-wos)
571    ("c" jmax-bibtex-wos-citing)
572    ("a" jmax-bibtex-wos-related)
573    ("R" jmax-bibtex-crossref)
574    ("g" jmax-bibtex-google-scholar)
575    ("n" jmax-bibtex-new-entry/body)
576    ("N" org-ref-open-bibtex-notes)
577    ("o" bibtex-copy-entry-as-kill)
578    ("d" bibtex-kill-entry)
579    ("L" org-ref-clean-bibtex-entry)
580    ("y" (kill-new  (bibtex-autokey-get-field "=key=")))
581    ("f" bibtex-copy-summary-as-kill)
582    ("k" helm-tag-bibtex-entry)
583    ("K" (lambda ()
584           (interactive)
585           (org-ref-set-bibtex-keywords
586            (read-string "Keywords: "
587                         (bibtex-autokey-get-field "keywords"))
588            t)))
589    ("b" org-ref-open-in-browser)
590    ("r" (lambda () (interactive)
591           (bibtex-beginning-of-entry)
592           (bibtex-kill-entry)
593           (find-file (ido-completing-read
594                       "Bibtex file: "
595                       (f-entries "." (lambda (f) (f-ext? f "bib")))))
596           (goto-char (point-max))
597           (bibtex-yank)
598           (save-buffer)
599           (kill-buffer)))
600    ("e" email-bibtex-entry)
601    ("U" (doi-utils-update-bibtex-entry-from-doi (jmax-bibtex-entry-doi)))
602    ("u" doi-utils-update-field)
603    ("F" jmax-bibtex-file/body)
604    ("h" helm-bibtex)
605    ("a" jmax-replace-nonascii)
606    ("s" org-ref-sort-bibtex-entry)
607    ("T" jmax-title-case-article)
608    ("S" jmax-sentence-case-article)
609    ("q" nil))
610
611 ;; create key-chord and key binding for hydra
612 (when jmax-bibtex-hydra-key-chord
613   (key-chord-define-global
614    jmax-bibtex-hydra-key-chord
615    'jmax-bibtex-hydra/body))
616
617
618 (when jmax-bibtex-hydra-key-binding
619   (global-set-key jmax-bibtex-hydra-key-binding 'jmax-bibtex-hydra/body))
620
621 ;; ** Hydra menu for new bibtex entries
622 ;; A hydra for adding new bibtex entries.
623 (defhydra jmax-bibtex-new-entry (:color blue)
624   "New Bibtex entry:"
625   ("a" bibtex-Article "Article")
626   ("b" bibtex-Book "Book")
627   ("i" bibtex-InBook "In book")
628   ("l" bibtex-Booklet "Booklet")
629   ("P" bibtex-Proceedings "Proceedings")
630   ("p" bibtex-InProceedings "In proceedings")
631   ("m" bibtex-Misc "Misc.")
632   ("M" bibtex-Manual "Manual")
633   ("T" bibtex-PhdThesis "PhD Thesis")
634   ("t" bibtex-MastersThesis "MS Thesis")
635   ("R" bibtex-TechReport "Report")
636   ("u" bibtex-Unpublished "unpublished")
637   ("c" bibtex-InCollection "Article in collection")
638   ("q" nil "quit"))
639
640
641 ;; ** Hydra menu of functions to act on a bibtex file.
642 (defhydra jmax-bibtex-file (:color blue)
643   "Bibtex file functions: "
644   ("v" bibtex-validate "Validate entries")
645   ("s" bibtex-sort-buffer "Sort entries")
646   ("r" bibtex-reformat "Reformat entries")
647   ("c" bibtex-count-entries "Count entries")
648   ("p" org-ref-build-full-bibliography "PDF bibliography"))
649
650
651 ;; * DEPRECATED bibtex menu
652 (defvar jmax-bibtex-menu-funcs '()
653  "Functions to run in doi menu.
654 Each entry is a list of (key menu-name function).  The function
655 must take one argument, the doi.  This is somewhat deprecated, as
656 I prefer the hydra interfaces above.")
657
658 (setq jmax-bibtex-menu-funcs
659       '(("p" "df" jmax-bibtex-pdf)
660         ("C" "opy" (lambda (doi)
661                      (kill-new (org-ref-bib-citation))
662                      (bury-buffer)))
663         ("w" "os" doi-utils-wos)
664         ("c" "iting articles" doi-utils-wos-citing)
665         ("r" "elated articles" doi-utils-wos-related)
666         ("s" "Google Scholar" doi-utils-google-scholar)
667         ("P" "Pubmed" doi-utils-pubmed)
668         ("f" "CrossRef" doi-utils-crossref)))
669
670 (defun jmax-bibtex ()
671   "Menu command to run in a bibtex entry.
672 Functions from `jmax-bibtex-menu-funcs'.  They all rely on the
673 entry having a doi."
674
675   (interactive)
676   ;; construct menu string as a message
677   (message
678    (concat
679     (mapconcat
680      (lambda (tup)
681        (concat "[" (elt tup 0) "]"
682                (elt tup 1) " "))
683      jmax-bibtex-menu-funcs "") ": "))
684   (let* ((input (read-char-exclusive))
685          (choice (assoc
686                   (char-to-string input) jmax-bibtex-menu-funcs)))
687     (when choice
688       (funcall
689        (elt
690         choice
691         2)
692        (jmax-bibtex-entry-doi)
693        ))))
694
695 (defalias 'jb 'jmax-bibtex)
696
697 ;; * The end
698 (provide 'jmax-bibtex)
699
700 ;;; jmax-bibtex.el ends here