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