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