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
60 (defgroup jmax-bibtex nil
61 "Customization group for jmax-bibtex.")
64 (defcustom jmax-bibtex-hydra-key-chord
66 "key-chord to run `jmax-bibtex-hydra'.
72 (defcustom jmax-bibtex-hydra-key-binding
74 "key-binding to run `jmax-bibtex-hydra'.
80 (defvar jmax-bibtex-journal-abbreviations
81 '(("ACAT" "ACS Catalysis" "ACS Catal.")
82 ("AM" "Acta Materialia" "Acta Mater.")
83 ("AMM" "Acta Metallurgica et Materialia" "Acta Metall. Mater.")
84 ("AMiner" "American Mineralogist" "Am. Mineral.")
85 ("AngC" "Angewandte Chemie-International Edition" "Angew. Chem. Int. Edit.")
86 ("APLM" "APL Materials" "APL Mat.")
87 ("ACBE" "Applied Catalysis B: Environmental" "Appl. Catal. B-Environ.")
88 ("APL" "Applied Physics Letters" "Appl. Phys. Lett.")
89 ("ASS" "Applied Surface Science" "Appl. Surf. Sci.")
90 ("CL" "Catalysis Letters" "Catal. Lett.")
91 ("CST" "Catalysis Science & Technology" "Catal. Sci. Technol.")
92 ("CT" "Catalysis Today" "Catal. Today")
93 ("CPL" "Chemical Physics Letters" "Chem. Phys. Lett")
94 ("CR" "Chemical Reviews" "Chem. Rev.")
95 ("CSR" "Chemical Society Reviews" "Chem. Soc. Rev.")
96 ("CSR" "Chemical Society Reviews" "Chem. Soc. Rev.")
97 ("CM" "Chemistry of Materials" "Chem. Mater.")
98 ("CSA" "Colloids and Surfaces, A: Physicochemical and Engineering Aspects" "Colloids Surf., A")
99 ("CPMS" "Computational Materials Science" "Comp. Mater. Sci.")
100 ("CPC" "Computer Physics Communications" "Comput. Phys. Commun.")
101 ("CGD" "Crystal Growth \\& Design" "Cryst. Growth Des.")
102 ("CEC" "CrystEngComm" "CrystEngComm")
103 ("ECST" "ECS Transactions" "ECS Trans.")
104 ("EES" "Energy \\& Environmental Science" "Energy Environ. Sci.")
105 ("HPR" "High Pressure Research" "High Pressure Res.")
106 ("IC" "Inorganic Chemistry" "Inorg. Chem.")
107 ("IECR" "Industrial \\& Engineering Chemistry Research" "Ind. Eng. Chem. Res.")
108 ("JJAP" "Japanese Journal of Applied Physics" "Jpn. J. Appl. Phys.")
109 ("JMatR" "Journal of Materials Research" "J. Mater. Res.")
110 ("JALC" "Journal of Alloys and Compounds" "J. Alloy Compd.")
111 ("JAC" "Journal of Applied Crystallography" "J. Appl. Crystallogr.")
112 ("JAP" "Journal of Applied Physics" "J. Appl. Phys.")
113 ("JC" "Journal of Catalysis" "J. Catal.")
114 ("JCP" "Journal of Chemical Physics" "J. Chem. Phys.")
115 ("JCC" "Journal of Computational Chemistry" "J. Comput. Chem.")
116 ("JCG" "Journal of Crystal Growth" "J. Crys. Growth")
117 ("JMC" "Journal of Materials Chemistry" "J. Mater. Chem.")
118 ("JMC" "Journal of Materials Chemistry" "J. Mater. Chem.")
119 ("JMSL" "Journal of Materials Science Letters" "J. Mater. Sci. Lett.")
120 ("JMS" "Journal of Membrane Science" "J. Memb. Sci.")
121 ("JPE" "Journal of Phase Equilibria" "J. Phase Equilib.")
122 ("JPCS" "Journal of Physics and Chemistry of Solids" "J. Phys. Chem. Solids")
123 ("JPCM" "Journal of Physics: Condensed Matter" "J. Phys.: Condens. Matter")
124 ("JSSC" "Journal of Solid State Chemistry" "J. Solid State Chem.")
125 ("JACerS" "Journal of the American Ceramic Society" "J. Am. Ceram. Soc.")
126 ("JACS" "Journal of the American Chemical Society" "J. Am. Chem. Soc.")
127 ("JES" "Journal of The Electrochemical Society" "J. Electrochem. Soc.")
128 ("JEaC" "Journal of Electroanalytical Chemistry" "J. Electroanal. Chem.")
129 ("JMS" "Journal of Membrane Science" "J. Memb. Sci.")
130 ("JVST" "Journal of Vacuum Science \\& Technology A" "J. Vac. Sci. Technol. A")
131 ("ML" "Materials Letters" "Mater. Lett.")
132 ("MSE-BS" "Materials Science and Engineering B" "Mat. Sci. Eng. B-Solid")
133 ("MOLSIM" "Molecular Simulation" "Mol. Sim.")
134 ("Nature" "Nature" "Nature")
135 ("NM" "Nature Materials" "Nat. Mater.")
136 ("PML" "Philosophical Magazine Letters" "Phil. Mag. Lett.")
137 ("PMA" "Philosophical Magazine A" "Phil. Mag. A")
138 ("PA" "Physica A: Statistical Mechanics and its Applications" "Physica A")
139 ("PB" "Physica B-Condensed Matter" "Physica B")
140 ("PCCP" "Physical Chemistry Chemical Physics" "Phys. Chem. Chem. Phys.")
141 ("PSSB" "physica status solidi (b)" "Phys. Status Solidi B")
142 ("PRA" "Physical Review A" "Phys. Rev. A")
143 ("PRB" "Physical Review B" "Phys. Rev. B")
144 ("PRL" "Physical Review Letters" "Phys. Rev. Lett.")
145 ("PCM" "Physics and Chemistry of Minerals" "Phys. Chem. Miner.")
146 ("PSurfSci" "Progress in Surface Science" "Prog. Surf. Sci.")
147 ("Science" "Science" "Science")
148 ("SABC" "Sensors and Actuators B: Chemical" "Sensor. Actuat. B-Chem.")
149 ("SS" "Surface Science" "Surf. Sci.")
150 ("EPJB" "The European Physical Journal B" "Eur. Phys. J. B")
151 ("JPC" "The Journal of Physical Chemistry" "J. Phys. Chem.")
152 ("JPCB" "The Journal of Physical Chemistry B" "J. Phys. Chem. B")
153 ("JPCC" "The Journal of Physical Chemistry C" "J. Phys. Chem. C")
154 ("JPCL" "The Journal of Physical Chemistry Letters" "J. Phys. Chem. Lett.")
155 ("JCP" "The Journal of Chemical Physics" "J. Chem. Phys.")
156 ("MSMSE" "Modelling and Simulation in Materials Science and Engineering" "Modell. Simul. Mater. Sci. Eng.")
157 ("TSF" "Thin Solid Films" "Thin Solid Films")
158 ("TC" "Topics in Catalysis" "Top. Catal.")
159 ("WR" "Water Research" "Water Res."))
160 "List of (string journal-full-name journal-abbreviation). Find abbreviations at http://cassi.cas.org/search.jsp.")
163 (defun jmax-bibtex-generate-longtitles ()
164 "Generate longtitles.bib which are @string definitions.
165 The full journal names are in `jmax-bibtex-journal-abbreviations'."
167 (with-temp-file "longtitles.bib"
168 (dolist (row jmax-bibtex-journal-abbreviations)
169 (insert (format "@string{%s=\"%s\"}\n"
174 (defun jmax-bibtex-generate-shorttitles ()
175 "Generate shorttitles.bib which are @string definitions.
176 The abbreviated journal names in `jmax-bibtex-journal-abbreviations'."
178 (with-temp-file "shorttitles.bib"
179 (dolist (row jmax-bibtex-journal-abbreviations)
180 (insert (format "@string{%s=\"%s\"}\n"
185 (defun jmax-stringify-journal-name (&optional key start end)
186 "Replace journal name in a bibtex entry with a string.
187 The strings are defined in
188 `jmax-bibtex-journal-abbreviations'. The optional arguments KEY,
189 START and END allow you to use this with `bibtex-map-entries'"
191 (bibtex-beginning-of-entry)
195 (cdr (assoc "=type=" (bibtex-parse-entry)))))
196 (let* ((full-names (mapcar
198 (cons (nth 1 row) (nth 0 row)))
199 jmax-bibtex-journal-abbreviations))
200 (abbrev-names (mapcar
202 (cons (nth 2 row) (nth 0 row)))
203 jmax-bibtex-journal-abbreviations))
204 (journal (s-trim (bibtex-autokey-get-field "journal")))
206 (cdr (assoc journal full-names))
207 (cdr (assoc journal abbrev-names)))))
209 (bibtex-set-field "journal" bstring t)
210 (bibtex-fill-entry)))))
212 (defun jmax-set-journal-string (full-journal-name)
213 "Set a bibtex journal name to the string that represents FULL-JOURNAL-NAME.
214 This is defined in `jmax-bibtex-journal-abbreviations'."
221 jmax-bibtex-journal-abbreviations))))
222 ;; construct data alist for the string lookup.
225 (cons (nth 1 x) (nth 0 x)))
226 jmax-bibtex-journal-abbreviations)))
227 (bibtex-set-field "journal" (cdr (assoc full-journal-name alist)) t)
229 (bibtex-clean-entry)))
231 ;; see https://github.com/fxcoudert/tools/blob/master/doi2bib for more replacements
232 (defvar jmax-nonascii-latex-replacements
234 "Cons list of non-ascii characters and their LaTeX representations.")
236 (setq jmax-nonascii-latex-replacements
263 ("δ" . "$\\\\delta$")
268 ("θ" . "$\\\\theta$")
270 ("→" . "$\\\\rightarrow$")
271 ("⇌" . "$\\\\leftrightharpoons$")
272 ("×" . "$\\\\times$")
275 ("ı" . "i") ; I think this is a turkish i
276 ;; I think these are non-ascii spaces. there seems to be more than one.
289 (defun jmax-replace-nonascii ()
290 "Hook function to replace non-ascii characters in a bibtex entry."
294 (bibtex-narrow-to-entry)
295 (goto-char (point-min))
296 (dolist (char (mapcar (lambda (x) (car x)) jmax-nonascii-latex-replacements))
297 (while (re-search-forward char nil t)
298 (replace-match (cdr (assoc char jmax-nonascii-latex-replacements))))
299 (goto-char (point-min))))
302 (add-hook 'org-ref-clean-bibtex-entry-hook 'jmax-replace-nonascii)
305 (defvar jmax-lower-case-words
306 '("a" "an" "on" "and" "for"
308 "List of words to keep lowercase when changing case in a title.")
311 (defun jmax-title-case-article (&optional key start end)
312 "Convert a bibtex entry article title to title-case.
313 The arguments KEY, START and END are optional, and are only there
314 so you can use this function with `bibtex-map-entries' to change
315 all the title entries in articles."
317 (bibtex-beginning-of-entry)
319 (let* ((title (bibtex-autokey-get-field "title"))
320 (words (split-string title))
323 (string= "article" (downcase (cdr (assoc "=type=" (bibtex-parse-entry)))))
327 ;; match words containing {} or \ which are probably
328 ;; LaTeX or protected words
329 (string-match "\\$\\|{\\|}\\|\\\\" word)
330 ;; these words should not be capitalized, unless they
331 ;; are the first word
332 (-contains? jmax-lower-case-words (s-downcase word)))
334 (s-capitalize word)))
337 ;; Check if first word should be capitalized
338 (when (-contains? jmax-lower-case-words (car words))
339 (setf (car words) (s-capitalize (car words))))
341 (setq title (mapconcat 'identity words " "))
343 ;; Capitalize letters after a dash
345 (string-match "[a-zA-Z]-\\([a-z]\\)" title start)
346 (let ((char (substring title (match-beginning 1) (match-end 1))))
347 (setf (substring title (match-beginning 1) (match-end 1))
348 (format "%s" (upcase char)))
349 (setq start (match-end 1))))
351 ;; this is defined in doi-utils
355 (bibtex-fill-entry))))
357 (add-hook 'org-ref-clean-bibtex-entry-hook 'jmax-title-case-article)
360 (defun jmax-sentence-case-article (&optional key start end)
361 "Convert a bibtex entry article title to sentence-case.
362 The arguments KEY, START and END are optional, and are only there
363 so you can use this function with `bibtex-map-entries' to change
364 all the title entries in articles."
366 (bibtex-beginning-of-entry)
368 (let* ((title (bibtex-autokey-get-field "title"))
369 (words (split-string title))
372 (string= "article" (downcase (cdr (assoc "=type=" (bibtex-parse-entry)))))
376 ;; match words containing {} or \ which are probably
377 ;; LaTeX or protected words
378 (string-match "\\$\\|{\\|}\\|\\\\" word)
383 ;; capitalize first word
384 (setf (car words) (s-capitalize (car words)))
387 (setq title (mapconcat 'identity words " "))
389 ;; capitalize a word after a :, eg. a subtitle, and protect it
391 (string-match "[a-z]:\\s-+\\([A-Z]\\)" title start)
392 (let ((char (substring title (match-beginning 1) (match-end 1))))
393 (setf (substring title (match-beginning 1) (match-end 1))
394 ;; (format "{%s}" (upcase char)))
395 (format "%s" (upcase char)))
396 (setq start (match-end 1))))
398 ;; this is defined in doi-utils
402 ;; clean and refill entry so it looks nice
404 (bibtex-fill-entry))))
407 (defun jmax-bibtex-next-entry (&optional n)
408 "Jump to the beginning of the next bibtex entry.
409 N is a prefix argument. If it is numeric, jump that many entries
410 forward. Negative numbers do nothing."
412 ;; Note if we start at the beginning of an entry, nothing
413 ;; happens. We need to move forward a char, and call again.
414 (when (= (point) (save-excursion
415 (bibtex-beginning-of-entry)))
417 (jmax-bibtex-next-entry))
419 ;; search forward for an entry
421 (re-search-forward bibtex-entry-head nil t (and (numberp n) n))
422 ;; go to beginning of the entry
423 (bibtex-beginning-of-entry)))
426 (defun jmax-bibtex-previous-entry (&optional n)
427 "Jump to beginning of the previous bibtex entry.
428 N is a prefix argument. If it is numeric, jump that many entries back."
430 (bibtex-beginning-of-entry)
432 (re-search-backward bibtex-entry-head nil t (and (numberp n) n))
433 (bibtex-beginning-of-entry)))
436 (defun jmax-bibtex-mode-keys ()
437 "Modify keymaps used by `bibtex-mode'."
438 (local-set-key (kbd "M-n") 'jmax-bibtex-next-entry)
439 (local-set-key (kbd "M-p") 'jmax-bibtex-previous-entry))
441 ;; add to bibtex-mode-hook
442 (add-hook 'bibtex-mode-hook 'jmax-bibtex-mode-keys)
445 (defun jmax-bibtex-entry-doi ()
446 "Get doi from entry at point."
449 (bibtex-beginning-of-entry)
450 (reftex-get-bib-field "doi" (bibtex-parse-entry t))))
453 (defun jmax-bibtex-wos ()
454 "Open bibtex entry in Web Of Science if there is a DOI."
456 (doi-utils-wos (jmax-bibtex-entry-doi)))
459 (defun jmax-bibtex-wos-citing ()
460 "Open citing articles for bibtex entry in Web Of Science if there is a DOI."
462 (doi-utils-wos-citing (jmax-bibtex-entry-doi)))
465 (defun jmax-bibtex-wos-related ()
466 "Open related articles for bibtex entry in Web Of Science if there is a DOI."
468 (doi-utils-wos-related (jmax-bibtex-entry-doi)))
471 (defun jmax-bibtex-wos-citing ()
472 "Open citing articles for bibtex entry in Web Of Science if there is a DOI."
474 (doi-utils-wos-citing (jmax-bibtex-entry-doi)))
477 (defun jmax-bibtex-crossref ()
478 "Open the bibtex entry in Crossref by its doi."
480 (doi-utils-crossref (jmax-bibtex-entry-doi)))
483 (defun jmax-bibtex-google-scholar ()
484 "Open the bibtex entry at point in google-scholar by its doi."
486 (doi-utils-google-scholar (jmax-bibtex-entry-doi)))
489 (defun jmax-bibtex-pubmed ()
490 "Open the bibtex entry at point in Pubmed by its doi."
492 (doi-utils-pubmed (jmax-bibtex-entry-doi)))
495 (defun jmax-bibtex-pdf (doi)
496 "Open the pdf for the bibtex entry at point.
497 Thin wrapper to get `jmax-bibtex' to open pdf, because it calls
498 functions with a DOI argument."
500 (org-ref-open-bibtex-pdf))
503 ;; hydra menu for actions on bibtex entries
504 (defhydra jmax-bibtex-hydra (:color blue)
506 _p_: Open pdf _y_: Copy key _n_: New entry _w_: WOS
507 _b_: Open url _f_: Copy formatted entry _o_: Copy entry _c_: WOS citing
508 _r_: Refile entry _k_: Add keywords _d_: delete entry _a_: WOS related
509 _e_: Email entry _K_: Edit keywords _L_: clean entry _P_: Pubmed
510 _U_: Update entry _N_: Open notes _R_: Crossref _g_: Google Scholar
511 _s_: Sort entry _a_: Remove nonascii _h_: helm-bibtex _q_: quit
512 _u_: Update field _f_: file funcs
514 ("p" org-ref-open-bibtex-pdf)
515 ("P" jmax-bibtex-pubmed)
516 ("w" jmax-bibtex-wos)
517 ("c" jmax-bibtex-wos-citing)
518 ("a" jmax-bibtex-wos-related)
519 ("R" jmax-bibtex-crossref)
520 ("g" jmax-bibtex-google-scholar)
521 ("n" jmax-bibtex-new-entry/body)
522 ("N" org-ref-open-bibtex-notes)
523 ("o" bibtex-copy-entry-as-kill)
524 ("d" bibtex-kill-entry)
525 ("L" org-ref-clean-bibtex-entry)
526 ("y" (kill-new (bibtex-autokey-get-field "=key=")))
527 ("f" bibtex-copy-summary-as-kill)
528 ("k" helm-tag-bibtex-entry)
531 (org-ref-set-bibtex-keywords
532 (read-input "Keywords: "
533 (bibtex-autokey-get-field "keywords"))
535 ("b" org-ref-open-in-browser)
536 ("r" (lambda () (interactive)
537 (bibtex-beginning-of-entry)
539 (find-file (ido-completing-read
541 (f-entries "." (lambda (f) (f-ext? f "bib")))))
542 (goto-char (point-max))
546 ("e" email-bibtex-entry)
547 ("U" (doi-utils-update-bibtex-entry-from-doi (jmax-bibtex-entry-doi)))
548 ("u" doi-utils-update-field)
549 ("f" jmax-bibtex-file/body)
551 ("a" jmax-replace-nonascii)
552 ("s" org-ref-sort-bibtex-entry)
555 ;; create key-chord and key binding for hydra
556 (when jmax-bibtex-hydra-key-chord
557 (key-chord-define-global
558 jmax-bibtex-hydra-key-chord
559 'jmax-bibtex-hydra/body))
562 (when jmax-bibtex-hydra-key-binding
563 (global-set-key jmax-bibtex-hydra-key-binding 'jmax-bibtex-hydra/body))
566 ;; A hydra for adding new bibtex entries.
567 (defhydra jmax-bibtex-new-entry (:color blue)
569 ("a" bibtex-Article "Article")
570 ("b" bibtex-Book "Book")
571 ("i" bibtex-InBook "In book")
572 ("l" bibtex-Booklet "Booklet")
573 ("P" bibtex-Proceedings "Proceedings")
574 ("p" bibtex-InProceedings "In proceedings")
575 ("m" bibtex-Misc "Misc.")
576 ("M" bibtex-Manual "Manual")
577 ("T" bibtex-PhdThesis "PhD Thesis")
578 ("t" bibtex-MastersThesis "MS Thesis")
579 ("R" bibtex-TechReport "Report")
580 ("u" bibtex-Unpublished "unpublished")
581 ("c" bibtex-InCollection "Article in collection")
585 ;; a hydra menu of functions to act on a bibtex file.
586 (defhydra jmax-bibtex-file (:color blue)
587 "Bibtex file functions: "
588 ("v" bibtex-validate "Validate entries")
589 ("s" bibtex-sort-buffer "Sort entries")
590 ("r" bibtex-reformat "Reformat entries")
591 ("c" bibtex-count-entries "Count entries")
592 ("p" org-ref-build-full-bibliography "PDF bibliography"))
595 (defvar jmax-bibtex-menu-funcs '()
596 "Functions to run in doi menu.
597 Each entry is a list of (key menu-name function). The function
598 must take one argument, the doi. This is somewhat deprecated, as
599 I prefer the hydra interfaces above.")
601 (setq jmax-bibtex-menu-funcs
602 '(("p" "df" jmax-bibtex-pdf)
603 ("C" "opy" (lambda (doi)
604 (kill-new (org-ref-bib-citation))
606 ("w" "os" doi-utils-wos)
607 ("c" "iting articles" doi-utils-wos-citing)
608 ("r" "elated articles" doi-utils-wos-related)
609 ("s" "Google Scholar" doi-utils-google-scholar)
610 ("P" "Pubmed" doi-utils-pubmed)
611 ("f" "CrossRef" doi-utils-crossref)))
613 (defun jmax-bibtex ()
614 "Menu command to run in a bibtex entry.
615 Functions from `jmax-bibtex-menu-funcs'. They all rely on the
619 ;; construct menu string as a message
624 (concat "[" (elt tup 0) "]"
626 jmax-bibtex-menu-funcs "") ": "))
627 (let* ((input (read-char-exclusive))
629 (char-to-string input) jmax-bibtex-menu-funcs)))
635 (jmax-bibtex-entry-doi)
638 (defalias 'jb 'jmax-bibtex)
640 (provide 'jmax-bibtex)
642 ;;; jmax-bibtex.el ends here