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