1 ;;; jmax-bibtex.el --- jmax-bibtex utilities
3 ;; Copyright(C) 2014 John Kitchin
5 ;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
6 ;; URL: https://github.com/jkitchin/org-ref
8 ;; Keywords: org-mode, bibtex
9 ;; Package-Requires: ((org-ref) (s) (dash) (doi-utils) (key-chord))
11 ;; This file is not currently part of GNU Emacs.
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.
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.
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.
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
37 ;; jmax-title-case-article :: title case the title in an article
38 ;; jmax-sentence-case-article :: sentence case the title in an article.
40 ;; jmax-replace-nonascii :: replace nonascii characters in a bibtex
41 ;; entry. Replacements are in `jmax-nonascii-latex-replacements'.
43 ;; jmax-title-case-article
44 ;; jmax-sentence-case-article
46 ;; jmax-bibtex-next-entry :: bound to M-n
47 ;; jmax-bibtex-previous-entry :: bound to M-p
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
54 ;; jmax-bibtex :: a deprecated menu of actions
61 (defgroup jmax-bibtex nil
62 "Customization group for jmax-bibtex.")
65 (defcustom jmax-bibtex-hydra-key-chord
67 "key-chord to run `jmax-bibtex-hydra'.
73 (defcustom jmax-bibtex-hydra-key-binding
75 "key-binding to run `jmax-bibtex-hydra'.
80 ;; * Journal abbreviations
81 (defvar jmax-bibtex-journal-abbreviations
82 '(("ACAT" "ACS Catalysis" "ACS Catal.")
83 ("AM" "Acta Materialia" "Acta Mater.")
84 ("AMM" "Acta Metallurgica et Materialia" "Acta Metall. Mater.")
85 ("AMiner" "American Mineralogist" "Am. Mineral.")
86 ("AngC" "Angewandte Chemie-International Edition" "Angew. Chem. Int. Edit.")
87 ("APLM" "APL Materials" "APL Mat.")
88 ("ACBE" "Applied Catalysis B: Environmental" "Appl. Catal. B-Environ.")
89 ("APL" "Applied Physics Letters" "Appl. Phys. Lett.")
90 ("ASS" "Applied Surface Science" "Appl. Surf. Sci.")
91 ("CL" "Catalysis Letters" "Catal. Lett.")
92 ("CST" "Catalysis Science & Technology" "Catal. Sci. Technol.")
93 ("CT" "Catalysis Today" "Catal. Today")
94 ("CPL" "Chemical Physics Letters" "Chem. Phys. Lett")
95 ("CR" "Chemical Reviews" "Chem. Rev.")
96 ("CSR" "Chemical Society Reviews" "Chem. Soc. Rev.")
97 ("CSR" "Chemical Society Reviews" "Chem. Soc. Rev.")
98 ("CM" "Chemistry of Materials" "Chem. Mater.")
99 ("CSA" "Colloids and Surfaces, A: Physicochemical and Engineering Aspects" "Colloids Surf., A")
100 ("CPMS" "Computational Materials Science" "Comp. Mater. Sci.")
101 ("CPC" "Computer Physics Communications" "Comput. Phys. Commun.")
102 ("CGD" "Crystal Growth \\& Design" "Cryst. Growth Des.")
103 ("CEC" "CrystEngComm" "CrystEngComm")
104 ("ECST" "ECS Transactions" "ECS Trans.")
105 ("EES" "Energy \\& Environmental Science" "Energy Environ. Sci.")
106 ("HPR" "High Pressure Research" "High Pressure Res.")
107 ("IC" "Inorganic Chemistry" "Inorg. Chem.")
108 ("IECR" "Industrial \\& Engineering Chemistry Research" "Ind. Eng. Chem. Res.")
109 ("JJAP" "Japanese Journal of Applied Physics" "Jpn. J. Appl. Phys.")
110 ("JMatR" "Journal of Materials Research" "J. Mater. Res.")
111 ("JALC" "Journal of Alloys and Compounds" "J. Alloy Compd.")
112 ("JAC" "Journal of Applied Crystallography" "J. Appl. Crystallogr.")
113 ("JAP" "Journal of Applied Physics" "J. Appl. Phys.")
114 ("JC" "Journal of Catalysis" "J. Catal.")
115 ("JCP" "Journal of Chemical Physics" "J. Chem. Phys.")
116 ("JCC" "Journal of Computational Chemistry" "J. Comput. Chem.")
117 ("JCG" "Journal of Crystal Growth" "J. Crys. Growth")
118 ("JMC" "Journal of Materials Chemistry" "J. Mater. Chem.")
119 ("JMC" "Journal of Materials Chemistry" "J. Mater. Chem.")
120 ("JMSL" "Journal of Materials Science Letters" "J. Mater. Sci. Lett.")
121 ("JMS" "Journal of Membrane Science" "J. Memb. Sci.")
122 ("JPE" "Journal of Phase Equilibria" "J. Phase Equilib.")
123 ("JPCS" "Journal of Physics and Chemistry of Solids" "J. Phys. Chem. Solids")
124 ("JPCM" "Journal of Physics: Condensed Matter" "J. Phys.: Condens. Matter")
125 ("JSSC" "Journal of Solid State Chemistry" "J. Solid State Chem.")
126 ("JACerS" "Journal of the American Ceramic Society" "J. Am. Ceram. Soc.")
127 ("JACS" "Journal of the American Chemical Society" "J. Am. Chem. Soc.")
128 ("JES" "Journal of The Electrochemical Society" "J. Electrochem. Soc.")
129 ("JEaC" "Journal of Electroanalytical Chemistry" "J. Electroanal. Chem.")
130 ("JMS" "Journal of Membrane Science" "J. Memb. Sci.")
131 ("JVST" "Journal of Vacuum Science \\& Technology A" "J. Vac. Sci. Technol. A")
132 ("ML" "Materials Letters" "Mater. Lett.")
133 ("MSE-BS" "Materials Science and Engineering B" "Mat. Sci. Eng. B-Solid")
134 ("MOLSIM" "Molecular Simulation" "Mol. Sim.")
135 ("Nature" "Nature" "Nature")
136 ("NM" "Nature Materials" "Nat. Mater.")
137 ("PML" "Philosophical Magazine Letters" "Phil. Mag. Lett.")
138 ("PMA" "Philosophical Magazine A" "Phil. Mag. A")
139 ("PA" "Physica A: Statistical Mechanics and its Applications" "Physica A")
140 ("PB" "Physica B-Condensed Matter" "Physica B")
141 ("PCCP" "Physical Chemistry Chemical Physics" "Phys. Chem. Chem. Phys.")
142 ("PSSB" "physica status solidi (b)" "Phys. Status Solidi B")
143 ("PRA" "Physical Review A" "Phys. Rev. A")
144 ("PRB" "Physical Review B" "Phys. Rev. B")
145 ("PRL" "Physical Review Letters" "Phys. Rev. Lett.")
146 ("PCM" "Physics and Chemistry of Minerals" "Phys. Chem. Miner.")
147 ("PSurfSci" "Progress in Surface Science" "Prog. Surf. Sci.")
148 ("Science" "Science" "Science")
149 ("SABC" "Sensors and Actuators B: Chemical" "Sensor. Actuat. B-Chem.")
150 ("SS" "Surface Science" "Surf. Sci.")
151 ("EPJB" "The European Physical Journal B" "Eur. Phys. J. B")
152 ("JPC" "The Journal of Physical Chemistry" "J. Phys. Chem.")
153 ("JPCB" "The Journal of Physical Chemistry B" "J. Phys. Chem. B")
154 ("JPCC" "The Journal of Physical Chemistry C" "J. Phys. Chem. C")
155 ("JPCL" "The Journal of Physical Chemistry Letters" "J. Phys. Chem. Lett.")
156 ("JCP" "The Journal of Chemical Physics" "J. Chem. Phys.")
157 ("MSMSE" "Modelling and Simulation in Materials Science and Engineering" "Modell. Simul. Mater. Sci. Eng.")
158 ("TSF" "Thin Solid Films" "Thin Solid Films")
159 ("TC" "Topics in Catalysis" "Top. Catal.")
160 ("WR" "Water Research" "Water Res."))
161 "List of (string journal-full-name journal-abbreviation). Find abbreviations at http://cassi.cas.org/search.jsp.")
164 (defun jmax-bibtex-generate-longtitles ()
165 "Generate longtitles.bib which are @string definitions.
166 The full journal names are in `jmax-bibtex-journal-abbreviations'."
168 (with-temp-file "longtitles.bib"
169 (dolist (row jmax-bibtex-journal-abbreviations)
170 (insert (format "@string{%s=\"%s\"}\n"
175 (defun jmax-bibtex-generate-shorttitles ()
176 "Generate shorttitles.bib which are @string definitions.
177 The abbreviated journal names in `jmax-bibtex-journal-abbreviations'."
179 (with-temp-file "shorttitles.bib"
180 (dolist (row jmax-bibtex-journal-abbreviations)
181 (insert (format "@string{%s=\"%s\"}\n"
186 (defun jmax-stringify-journal-name (&optional key start end)
187 "Replace journal name in a bibtex entry with a string.
188 The strings are defined in
189 `jmax-bibtex-journal-abbreviations'. The optional arguments KEY,
190 START and END allow you to use this with `bibtex-map-entries'"
192 (bibtex-beginning-of-entry)
196 (cdr (assoc "=type=" (bibtex-parse-entry)))))
197 (let* ((full-names (mapcar
199 (cons (nth 1 row) (nth 0 row)))
200 jmax-bibtex-journal-abbreviations))
201 (abbrev-names (mapcar
203 (cons (nth 2 row) (nth 0 row)))
204 jmax-bibtex-journal-abbreviations))
205 (journal (s-trim (bibtex-autokey-get-field "journal")))
207 (cdr (assoc journal full-names))
208 (cdr (assoc journal abbrev-names)))))
210 (bibtex-set-field "journal" bstring t)
211 (bibtex-fill-entry)))))
213 (defun jmax-set-journal-string (full-journal-name)
214 "Set a bibtex journal name to the string that represents FULL-JOURNAL-NAME.
215 This is defined in `jmax-bibtex-journal-abbreviations'."
222 jmax-bibtex-journal-abbreviations))))
223 ;; construct data alist for the string lookup.
226 (cons (nth 1 x) (nth 0 x)))
227 jmax-bibtex-journal-abbreviations)))
228 (bibtex-set-field "journal" (cdr (assoc full-journal-name alist)) t)
230 (bibtex-clean-entry)))
232 ;; * Non-ascii character replacement
233 ;; see https://github.com/fxcoudert/tools/blob/master/doi2bib for more replacements
234 (defvar jmax-nonascii-latex-replacements
236 "Cons list of non-ascii characters and their LaTeX representations.")
238 (setq jmax-nonascii-latex-replacements
265 ("δ" . "$\\\\delta$")
270 ("θ" . "$\\\\theta$")
272 ("→" . "$\\\\rightarrow$")
273 ("⇌" . "$\\\\leftrightharpoons$")
274 ("×" . "$\\\\times$")
277 ("ı" . "i") ; I think this is a turkish i
278 ;; I think these are non-ascii spaces. there seems to be more than one.
292 (defun jmax-replace-nonascii ()
293 "Hook function to replace non-ascii characters in a bibtex entry."
297 (bibtex-narrow-to-entry)
298 (goto-char (point-min))
299 (dolist (char (mapcar (lambda (x) (car x)) jmax-nonascii-latex-replacements))
300 (while (re-search-forward char nil t)
301 (replace-match (cdr (assoc char jmax-nonascii-latex-replacements))))
302 (goto-char (point-min))))
305 (add-hook 'org-ref-clean-bibtex-entry-hook 'jmax-replace-nonascii)
307 ;; * Title case transformations
308 (defvar jmax-lower-case-words
309 '("a" "an" "on" "and" "for"
311 "List of words to keep lowercase when changing case in a title.")
314 (defun jmax-title-case-article (&optional key start end)
315 "Convert a bibtex entry article title to title-case.
316 The arguments KEY, START and END are optional, and are only there
317 so you can use this function with `bibtex-map-entries' to change
318 all the title entries in articles."
320 (bibtex-beginning-of-entry)
322 (let* ((title (bibtex-autokey-get-field "title"))
323 (words (split-string title))
326 (string= "article" (downcase (cdr (assoc "=type=" (bibtex-parse-entry)))))
330 ;; match words containing {} or \ which are probably
331 ;; LaTeX or protected words
332 (string-match "\\$\\|{\\|}\\|\\\\" word)
333 ;; these words should not be capitalized, unless they
334 ;; are the first word
335 (-contains? jmax-lower-case-words (s-downcase word)))
337 (s-capitalize word)))
340 ;; Check if first word should be capitalized
341 (when (-contains? jmax-lower-case-words (car words))
342 (setf (car words) (s-capitalize (car words))))
344 (setq title (mapconcat 'identity words " "))
346 ;; Capitalize letters after a dash
348 (string-match "[a-zA-Z]-\\([a-z]\\)" title start)
349 (let ((char (substring title (match-beginning 1) (match-end 1))))
350 (setf (substring title (match-beginning 1) (match-end 1))
351 (format "%s" (upcase char)))
352 (setq start (match-end 1))))
354 ;; this is defined in doi-utils
358 (bibtex-fill-entry))))
360 (add-hook 'org-ref-clean-bibtex-entry-hook 'jmax-title-case-article)
363 (defun jmax-sentence-case-article (&optional key start end)
364 "Convert a bibtex entry article title to sentence-case.
365 The arguments KEY, START and END are optional, and are only there
366 so you can use this function with `bibtex-map-entries' to change
367 all the title entries in articles."
369 (bibtex-beginning-of-entry)
371 (let* ((title (bibtex-autokey-get-field "title"))
372 (words (split-string title))
375 (string= "article" (downcase (cdr (assoc "=type=" (bibtex-parse-entry)))))
379 ;; match words containing {} or \ which are probably
380 ;; LaTeX or protected words
381 (string-match "\\$\\|{\\|}\\|\\\\" word)
386 ;; capitalize first word
387 (setf (car words) (s-capitalize (car words)))
390 (setq title (mapconcat 'identity words " "))
392 ;; capitalize a word after a :, eg. a subtitle, and protect it
394 (string-match "[a-z]:\\s-+\\([A-Z]\\)" title start)
395 (let ((char (substring title (match-beginning 1) (match-end 1))))
396 (setf (substring title (match-beginning 1) (match-end 1))
397 ;; (format "{%s}" (upcase char)))
398 (format "%s" (upcase char)))
399 (setq start (match-end 1))))
401 ;; this is defined in doi-utils
405 ;; clean and refill entry so it looks nice
407 (bibtex-fill-entry))))
409 ;; * Navigation in bibtex file
410 (defun jmax-bibtex-next-entry (&optional n)
411 "Jump to the beginning of the next bibtex entry.
412 N is a prefix argument. If it is numeric, jump that many entries
413 forward. Negative numbers do nothing."
415 ;; Note if we start at the beginning of an entry, nothing
416 ;; happens. We need to move forward a char, and call again.
417 (when (= (point) (save-excursion
418 (bibtex-beginning-of-entry)))
420 (jmax-bibtex-next-entry))
422 ;; search forward for an entry
424 (re-search-forward bibtex-entry-head nil t (and (numberp n) n))
425 ;; go to beginning of the entry
426 (bibtex-beginning-of-entry)))
429 (defun jmax-bibtex-previous-entry (&optional n)
430 "Jump to beginning of the previous bibtex entry.
431 N is a prefix argument. If it is numeric, jump that many entries back."
433 (bibtex-beginning-of-entry)
435 (re-search-backward bibtex-entry-head nil t (and (numberp n) n))
436 (bibtex-beginning-of-entry)))
439 (defun jmax-bibtex-mode-keys ()
440 "Modify keymaps used by `bibtex-mode'."
441 (local-set-key (kbd "M-n") 'jmax-bibtex-next-entry)
442 (local-set-key (kbd "M-p") 'jmax-bibtex-previous-entry))
444 ;; add to bibtex-mode-hook
445 (add-hook 'bibtex-mode-hook 'jmax-bibtex-mode-keys)
447 ;; * Functions to act on an entry with a doi
448 (defun jmax-bibtex-entry-doi ()
449 "Get doi from entry at point."
452 (bibtex-beginning-of-entry)
453 (reftex-get-bib-field "doi" (bibtex-parse-entry t))))
456 (defun jmax-bibtex-wos ()
457 "Open bibtex entry in Web Of Science if there is a DOI."
459 (doi-utils-wos (jmax-bibtex-entry-doi)))
462 (defun jmax-bibtex-wos-citing ()
463 "Open citing articles for bibtex entry in Web Of Science if there is a DOI."
465 (doi-utils-wos-citing (jmax-bibtex-entry-doi)))
468 (defun jmax-bibtex-wos-related ()
469 "Open related articles for bibtex entry in Web Of Science if there is a DOI."
471 (doi-utils-wos-related (jmax-bibtex-entry-doi)))
474 (defun jmax-bibtex-wos-citing ()
475 "Open citing articles for bibtex entry in Web Of Science if there is a DOI."
477 (doi-utils-wos-citing (jmax-bibtex-entry-doi)))
480 (defun jmax-bibtex-crossref ()
481 "Open the bibtex entry in Crossref by its doi."
483 (doi-utils-crossref (jmax-bibtex-entry-doi)))
486 (defun jmax-bibtex-google-scholar ()
487 "Open the bibtex entry at point in google-scholar by its doi."
489 (doi-utils-google-scholar (jmax-bibtex-entry-doi)))
492 (defun jmax-bibtex-pubmed ()
493 "Open the bibtex entry at point in Pubmed by its doi."
495 (doi-utils-pubmed (jmax-bibtex-entry-doi)))
498 (defun jmax-bibtex-pdf (doi)
499 "Open the pdf for the bibtex entry at point.
500 Thin wrapper to get `jmax-bibtex' to open pdf, because it calls
501 functions with a DOI argument."
503 (org-ref-open-bibtex-pdf))
507 ;; ** Hydra menu for bibtex entries
508 ;; hydra menu for actions on bibtex entries
509 (defhydra jmax-bibtex-hydra (:color blue)
511 _p_: Open pdf _y_: Copy key _n_: New entry _w_: WOS
512 _b_: Open url _f_: Copy formatted entry _o_: Copy entry _c_: WOS citing
513 _r_: Refile entry _k_: Add keywords _d_: delete entry _a_: WOS related
514 _e_: Email entry _K_: Edit keywords _L_: clean entry _P_: Pubmed
515 _U_: Update entry _N_: Open notes _R_: Crossref _g_: Google Scholar
516 _s_: Sort entry _a_: Remove nonascii _h_: helm-bibtex _q_: quit
517 _u_: Update field _f_: file funcs
519 ("p" org-ref-open-bibtex-pdf)
520 ("P" jmax-bibtex-pubmed)
521 ("w" jmax-bibtex-wos)
522 ("c" jmax-bibtex-wos-citing)
523 ("a" jmax-bibtex-wos-related)
524 ("R" jmax-bibtex-crossref)
525 ("g" jmax-bibtex-google-scholar)
526 ("n" jmax-bibtex-new-entry/body)
527 ("N" org-ref-open-bibtex-notes)
528 ("o" bibtex-copy-entry-as-kill)
529 ("d" bibtex-kill-entry)
530 ("L" org-ref-clean-bibtex-entry)
531 ("y" (kill-new (bibtex-autokey-get-field "=key=")))
532 ("f" bibtex-copy-summary-as-kill)
533 ("k" helm-tag-bibtex-entry)
536 (org-ref-set-bibtex-keywords
537 (read-input "Keywords: "
538 (bibtex-autokey-get-field "keywords"))
540 ("b" org-ref-open-in-browser)
541 ("r" (lambda () (interactive)
542 (bibtex-beginning-of-entry)
544 (find-file (ido-completing-read
546 (f-entries "." (lambda (f) (f-ext? f "bib")))))
547 (goto-char (point-max))
551 ("e" email-bibtex-entry)
552 ("U" (doi-utils-update-bibtex-entry-from-doi (jmax-bibtex-entry-doi)))
553 ("u" doi-utils-update-field)
554 ("f" jmax-bibtex-file/body)
556 ("a" jmax-replace-nonascii)
557 ("s" org-ref-sort-bibtex-entry)
560 ;; create key-chord and key binding for hydra
561 (when jmax-bibtex-hydra-key-chord
562 (key-chord-define-global
563 jmax-bibtex-hydra-key-chord
564 'jmax-bibtex-hydra/body))
567 (when jmax-bibtex-hydra-key-binding
568 (global-set-key jmax-bibtex-hydra-key-binding 'jmax-bibtex-hydra/body))
570 ;; ** Hydra menu for new bibtex entries
571 ;; A hydra for adding new bibtex entries.
572 (defhydra jmax-bibtex-new-entry (:color blue)
574 ("a" bibtex-Article "Article")
575 ("b" bibtex-Book "Book")
576 ("i" bibtex-InBook "In book")
577 ("l" bibtex-Booklet "Booklet")
578 ("P" bibtex-Proceedings "Proceedings")
579 ("p" bibtex-InProceedings "In proceedings")
580 ("m" bibtex-Misc "Misc.")
581 ("M" bibtex-Manual "Manual")
582 ("T" bibtex-PhdThesis "PhD Thesis")
583 ("t" bibtex-MastersThesis "MS Thesis")
584 ("R" bibtex-TechReport "Report")
585 ("u" bibtex-Unpublished "unpublished")
586 ("c" bibtex-InCollection "Article in collection")
590 ;; ** Hydra menu of functions to act on a bibtex file.
591 (defhydra jmax-bibtex-file (:color blue)
592 "Bibtex file functions: "
593 ("v" bibtex-validate "Validate entries")
594 ("s" bibtex-sort-buffer "Sort entries")
595 ("r" bibtex-reformat "Reformat entries")
596 ("c" bibtex-count-entries "Count entries")
597 ("p" org-ref-build-full-bibliography "PDF bibliography"))
600 ;; * DEPRECATED bibtex menu
601 (defvar jmax-bibtex-menu-funcs '()
602 "Functions to run in doi menu.
603 Each entry is a list of (key menu-name function). The function
604 must take one argument, the doi. This is somewhat deprecated, as
605 I prefer the hydra interfaces above.")
607 (setq jmax-bibtex-menu-funcs
608 '(("p" "df" jmax-bibtex-pdf)
609 ("C" "opy" (lambda (doi)
610 (kill-new (org-ref-bib-citation))
612 ("w" "os" doi-utils-wos)
613 ("c" "iting articles" doi-utils-wos-citing)
614 ("r" "elated articles" doi-utils-wos-related)
615 ("s" "Google Scholar" doi-utils-google-scholar)
616 ("P" "Pubmed" doi-utils-pubmed)
617 ("f" "CrossRef" doi-utils-crossref)))
619 (defun jmax-bibtex ()
620 "Menu command to run in a bibtex entry.
621 Functions from `jmax-bibtex-menu-funcs'. They all rely on the
625 ;; construct menu string as a message
630 (concat "[" (elt tup 0) "]"
632 jmax-bibtex-menu-funcs "") ": "))
633 (let* ((input (read-char-exclusive))
635 (char-to-string input) jmax-bibtex-menu-funcs)))
641 (jmax-bibtex-entry-doi)
644 (defalias 'jb 'jmax-bibtex)
647 (provide 'jmax-bibtex)
649 ;;; jmax-bibtex.el ends here