1 ;;; jmax-bibtex.el --- jmax-bibtex utilities
4 ;; Requires: s.el, dash.el, org-ref.el, doi-utils.el
8 (defvar jmax-bibtex-journal-abbreviations
9 '(("ACAT" "ACS Catalysis" "ACS Catal.")
10 ("AM" "Acta Materialia" "Acta Mater.")
11 ("AMM" "Acta Metallurgica et Materialia" "Acta Metall. Mater.")
12 ("AMiner" "American Mineralogist" "Am. Mineral.")
13 ("AngC" "Angewandte Chemie-International Edition" "Angew. Chem. Int. Edit.")
14 ("APLM" "APL Materials" "APL Mat.")
15 ("ACBE" "Applied Catalysis B: Environmental" "Appl. Catal. B-Environ.")
16 ("APL" "Applied Physics Letters" "Appl. Phys. Lett.")
17 ("ASS" "Applied Surface Science" "Appl. Surf. Sci.")
18 ("CL" "Catalysis Letters" "Catal. Lett.")
19 ("CST" "Catalysis Science & Technology" "Catal. Sci. Technol.")
20 ("CT" "Catalysis Today" "Catal. Today")
21 ("CPL" "Chemical Physics Letters" "Chem. Phys. Lett")
22 ("CR" "Chemical Reviews" "Chem. Rev.")
23 ("CSR" "Chemical Society Reviews" "Chem. Soc. Rev.")
24 ("CSR" "Chemical Society Reviews" "Chem. Soc. Rev.")
25 ("CM" "Chemistry of Materials" "Chem. Mater.")
26 ("CSA" "Colloids and Surfaces, A: Physicochemical and Engineering Aspects" "Colloids Surf., A")
27 ("CPMS" "Computational Materials Science" "Comp. Mater. Sci.")
28 ("CPC" "Computer Physics Communications" "Comput. Phys. Commun.")
29 ("CGD" "Crystal Growth \\& Design" "Cryst. Growth Des.")
30 ("CEC" "CrystEngComm" "CrystEngComm")
31 ("ECST" "ECS Transactions" "ECS Trans.")
32 ("EES" "Energy \\& Environmental Science" "Energy Environ. Sci.")
33 ("HPR" "High Pressure Research" "High Pressure Res.")
34 ("IC" "Inorganic Chemistry" "Inorg. Chem.")
35 ("IECR" "Industrial \\& Engineering Chemistry Research" "Ind. Eng. Chem. Res.")
36 ("JJAP" "Japanese Journal of Applied Physics" "Jpn. J. Appl. Phys.")
37 ("JMatR" "Journal of Materials Research" "J. Mater. Res.")
38 ("JALC" "Journal of Alloys and Compounds" "J. Alloy Compd.")
39 ("JAC" "Journal of Applied Crystallography" "J. Appl. Crystallogr.")
40 ("JAP" "Journal of Applied Physics" "J. Appl. Phys.")
41 ("JC" "Journal of Catalysis" "J. Catal.")
42 ("JCP" "Journal of Chemical Physics" "J. Chem. Phys.")
43 ("JCC" "Journal of Computational Chemistry" "J. Comput. Chem.")
44 ("JCG" "Journal of Crystal Growth" "J. Crys. Growth")
45 ("JMC" "Journal of Materials Chemistry" "J. Mater. Chem.")
46 ("JMC" "Journal of Materials Chemistry" "J. Mater. Chem.")
47 ("JMSL" "Journal of Materials Science Letters" "J. Mater. Sci. Lett.")
48 ("JMS" "Journal of Membrane Science" "J. Memb. Sci.")
49 ("JPE" "Journal of Phase Equilibria" "J. Phase Equilib.")
50 ("JPCS" "Journal of Physics and Chemistry of Solids" "J. Phys. Chem. Solids")
51 ("JPCM" "Journal of Physics: Condensed Matter" "J. Phys.: Condens. Matter")
52 ("JSSC" "Journal of Solid State Chemistry" "J. Solid State Chem.")
53 ("JACerS" "Journal of the American Ceramic Society" "J. Am. Ceram. Soc.")
54 ("JACS" "Journal of the American Chemical Society" "J. Am. Chem. Soc.")
55 ("JES" "Journal of The Electrochemical Society" "J. Electrochem. Soc.")
56 ("JEaC" "Journal of Electroanalytical Chemistry" "J. Electroanal. Chem.")
57 ("JMS" "Journal of Membrane Science" "J. Memb. Sci.")
58 ("JVST" "Journal of Vacuum Science \\& Technology A" "J. Vac. Sci. Technol. A")
59 ("ML" "Materials Letters" "Mater. Lett.")
60 ("MSE-BS" "Materials Science and Engineering B" "Mat. Sci. Eng. B-Solid")
61 ("MOLSIM" "Molecular Simulation" "Mol. Sim.")
62 ("Nature" "Nature" "Nature")
63 ("NM" "Nature Materials" "Nat. Mater.")
64 ("PML" "Philosophical Magazine Letters" "Phil. Mag. Lett.")
65 ("PMA" "Philosophical Magazine A" "Phil. Mag. A")
66 ("PA" "Physica A: Statistical Mechanics and its Applications" "Physica A")
67 ("PB" "Physica B-Condensed Matter" "Physica B")
68 ("PCCP" "Physical Chemistry Chemical Physics" "Phys. Chem. Chem. Phys.")
69 ("PSSB" "physica status solidi (b)" "Phys. Status Solidi B")
70 ("PRA" "Physical Review A" "Phys. Rev. A")
71 ("PRB" "Physical Review B" "Phys. Rev. B")
72 ("PRL" "Physical Review Letters" "Phys. Rev. Lett.")
73 ("PCM" "Physics and Chemistry of Minerals" "Phys. Chem. Miner.")
74 ("PSurfSci" "Progress in Surface Science" "Prog. Surf. Sci.")
75 ("Science" "Science" "Science")
76 ("SABC" "Sensors and Actuators B: Chemical" "Sensor. Actuat. B-Chem.")
77 ("SS" "Surface Science" "Surf. Sci.")
78 ("EPJB" "The European Physical Journal B" "Eur. Phys. J. B")
79 ("JPC" "The Journal of Physical Chemistry" "J. Phys. Chem.")
80 ("JPCB" "The Journal of Physical Chemistry B" "J. Phys. Chem. B")
81 ("JPCC" "The Journal of Physical Chemistry C" "J. Phys. Chem. C")
82 ("JPCL" "The Journal of Physical Chemistry Letters" "J. Phys. Chem. Lett.")
83 ("JCP" "The Journal of Chemical Physics" "J. Chem. Phys.")
84 ("MSMSE" "Modelling and Simulation in Materials Science and Engineering" "Modell. Simul. Mater. Sci. Eng.")
85 ("TSF" "Thin Solid Films" "Thin Solid Films")
86 ("TC" "Topics in Catalysis" "Top. Catal.")
87 ("WR" "Water Research" "Water Res."))
88 "List of (string journal-full-name journal-abbreviation). Find abbreviations at http://cassi.cas.org/search.jsp.")
91 (defun jmax-bibtex-generate-longtitles ()
92 "Generate longtitles.bib which are @string definitions.
93 The full journal names are in `jmax-bibtex-journal-abbreviations'."
95 (with-temp-file "longtitles.bib"
96 (dolist (row jmax-bibtex-journal-abbreviations)
97 (insert (format "@string{%s=\"%s\"}\n"
102 (defun jmax-bibtex-generate-shorttitles ()
103 "Generate shorttitles.bib which are @string definitions.
104 The abbreviated journal names in `jmax-bibtex-journal-abbreviations'."
106 (with-temp-file "shorttitles.bib"
107 (dolist (row jmax-bibtex-journal-abbreviations)
108 (insert (format "@string{%s=\"%s\"}\n"
113 (defun jmax-stringify-journal-name (&optional key start end)
114 "Replace journal name in a bibtex entry with a string.
115 The strings are defined in
116 `jmax-bibtex-journal-abbreviations'. The optional arguments KEY,
117 START and END allow you to use this with `bibtex-map-entries'"
119 (bibtex-beginning-of-entry)
123 (cdr (assoc "=type=" (bibtex-parse-entry)))))
124 (let* ((full-names (mapcar
126 (cons (nth 1 row) (nth 0 row)))
127 jmax-bibtex-journal-abbreviations))
128 (abbrev-names (mapcar
130 (cons (nth 2 row) (nth 0 row)))
131 jmax-bibtex-journal-abbreviations))
132 (journal (s-trim (bibtex-autokey-get-field "journal")))
134 (cdr (assoc journal full-names))
135 (cdr (assoc journal abbrev-names)))))
137 (bibtex-set-field "journal" bstring t)
138 (bibtex-fill-entry)))))
140 (defun jmax-set-journal-string (full-journal-name)
141 "Set a bibtex journal name to the string that represents FULL-JOURNAL-NAME.
142 This is defined in `jmax-bibtex-journal-abbreviations'."
149 jmax-bibtex-journal-abbreviations))))
150 ;; construct data alist for the string lookup.
153 (cons (nth 1 x) (nth 0 x)))
154 jmax-bibtex-journal-abbreviations)))
155 (bibtex-set-field "journal" (cdr (assoc full-journal-name alist)) t)
157 (bibtex-clean-entry)))
159 ;; see https://github.com/fxcoudert/tools/blob/master/doi2bib for more replacements
160 (defvar jmax-nonascii-latex-replacements
162 "Cons list of non-ascii characters and their LaTeX representations.")
164 (setq jmax-nonascii-latex-replacements
189 ("δ" . "$\\\\delta$")
194 ("θ" . "$\\\\theta$")
196 ("→" . "$\\\\rightarrow$")
197 ("⇌" . "$\\\\leftrightharpoons$")
198 ("×" . "$\\\\times$")
200 ;; I think these are non-ascii spaces. there seems to be more than one.
211 (defun jmax-replace-nonascii ()
212 "Hook function to replace non-ascii characters in a bibtex entry."
216 (bibtex-narrow-to-entry)
217 (goto-char (point-min))
218 (dolist (char (mapcar (lambda (x) (car x)) jmax-nonascii-latex-replacements))
219 (while (re-search-forward char nil t)
220 (replace-match (cdr (assoc char jmax-nonascii-latex-replacements))))
221 (goto-char (point-min)))))
223 (add-hook 'org-ref-clean-bibtex-entry-hook 'jmax-replace-nonascii)
226 (defvar jmax-lower-case-words
227 '("a" "an" "on" "and" "for"
229 "List of words to keep lowercase when changing case in a title.")
232 (defun jmax-title-case-article (&optional key start end)
233 "Convert a bibtex entry article title to title-case.
234 The arguments KEY, START and END are optional, and are only there
235 so you can use this function with `bibtex-map-entries' to change
236 all the title entries in articles."
238 (bibtex-beginning-of-entry)
240 (let* ((title (bibtex-autokey-get-field "title"))
241 (words (split-string title))
244 (string= "article" (downcase (cdr (assoc "=type=" (bibtex-parse-entry)))))
248 ;; match words containing {} or \ which are probably
249 ;; LaTeX or protected words
250 (string-match "\\$\\|{\\|}\\|\\\\" word)
251 ;; these words should not be capitalized, unless they
252 ;; are the first word
253 (-contains? jmax-lower-case-words (s-downcase word)))
255 (s-capitalize word)))
258 ;; Check if first word should be capitalized
259 (when (-contains? jmax-lower-case-words (car words))
260 (setf (car words) (s-capitalize (car words))))
262 (setq title (mapconcat 'identity words " "))
264 ;; Capitalize letters after a dash
266 (string-match "[a-zA-Z]-\\([a-z]\\)" title start)
267 (let ((char (substring title (match-beginning 1) (match-end 1))))
268 (setf (substring title (match-beginning 1) (match-end 1))
269 (format "%s" (upcase char)))
270 (setq start (match-end 1))))
272 ;; this is defined in doi-utils
276 (bibtex-fill-entry))))
278 (add-hook 'org-ref-clean-bibtex-entry-hook 'jmax-title-case-article)
281 (defun jmax-sentence-case-article (&optional key start end)
282 "Convert a bibtex entry article title to sentence-case.
283 The arguments KEY, START and END are optional, and are only there
284 so you can use this function with `bibtex-map-entries' to change
285 all the title entries in articles."
287 (bibtex-beginning-of-entry)
289 (let* ((title (bibtex-autokey-get-field "title"))
290 (words (split-string title))
293 (string= "article" (downcase (cdr (assoc "=type=" (bibtex-parse-entry)))))
297 ;; match words containing {} or \ which are probably
298 ;; LaTeX or protected words
299 (string-match "\\$\\|{\\|}\\|\\\\" word)
304 ;; capitalize first word
305 (setf (car words) (s-capitalize (car words)))
308 (setq title (mapconcat 'identity words " "))
310 ;; capitalize a word after a :, eg. a subtitle, and protect it
312 (string-match "[a-z]:\\s-+\\([A-Z]\\)" title start)
313 (let ((char (substring title (match-beginning 1) (match-end 1))))
314 (setf (substring title (match-beginning 1) (match-end 1))
315 ;; (format "{%s}" (upcase char)))
316 (format "%s" (upcase char)))
317 (setq start (match-end 1))))
319 ;; this is defined in doi-utils
323 ;; clean and refill entry so it looks nice
325 (bibtex-fill-entry))))
330 (defun jmax-bibtex-next-entry (&optional n)
331 "Jump to the beginning of the next bibtex entry.
332 N is a prefix argument. If it is numeric, jump that many entries
333 forward. Negative numbers do nothing."
335 ;; Note if we start at the beginning of an entry, nothing
336 ;; happens. We need to move forward a char, and call again.
337 (when (= (point) (save-excursion
338 (bibtex-beginning-of-entry)))
340 (jmax-bibtex-next-entry))
342 ;; search forward for an entry
344 (re-search-forward bibtex-entry-head nil t (and (numberp n) n))
345 ;; go to beginning of the entry
346 (bibtex-beginning-of-entry)))
349 (defun jmax-bibtex-previous-entry (&optional n)
350 "Jump to beginning of the previous bibtex entry.
351 N is a prefix argument. If it is numeric, jump that many entries back."
353 (bibtex-beginning-of-entry)
355 (re-search-backward bibtex-entry-head nil t (and (numberp n) n))
356 (bibtex-beginning-of-entry)))
359 (defun jmax-bibtex-mode-keys ()
360 "Modify keymaps used by `bibtex-mode'."
361 (local-set-key (kbd "M-n") 'jmax-bibtex-next-entry)
362 (local-set-key (kbd "M-p") 'jmax-bibtex-previous-entry))
364 ;; add to bibtex-mode-hook
365 (add-hook 'bibtex-mode-hook 'jmax-bibtex-mode-keys)
368 (defun jmax-bibtex-entry-doi ()
369 "get doi from entry at point"
372 (bibtex-beginning-of-entry)
373 (reftex-get-bib-field "doi" (bibtex-parse-entry t))))
376 (defun jmax-bibtex-wos ()
377 "Open bibtex entry in Web Of Science if there is a DOI"
379 (doi-utils-wos (jmax-bibtex-entry-doi)))
382 (defun jmax-bibtex-wos-citing ()
383 "Open citing articles for bibtex entry in Web Of Science if there is a DOI"
385 (doi-utils-wos-citing (jmax-bibtex-entry-doi)))
388 (defun jmax-bibtex-wos-related ()
389 "Open related articles for bibtex entry in Web Of Science if there is a DOI"
391 (doi-utils-wos-related (jmax-bibtex-entry-doi)))
394 (defun jmax-bibtex-wos-citing ()
395 "Open citing articles for bibtex entry in Web Of Science if there is a DOI"
397 (doi-utils-wos-citing (jmax-bibtex-entry-doi)))
400 (defun jmax-bibtex-crossref ()
402 (doi-utils-crossref (jmax-bibtex-entry-doi)))
405 (defun jmax-bibtex-google-scholar ()
407 (doi-utils-google-scholar (jmax-bibtex-entry-doi)))
410 (defun jmax-bibtex-pubmed ()
412 (doi-utils-pubmed (jmax-bibtex-entry-doi)))
415 (defun jmax-bibtex-pdf (doi)
416 "thin wrapper to get `jmax-bibtex' to open pdf"
418 (org-ref-open-bibtex-pdf))
422 (defvar jmax-bibtex-menu-funcs '()
423 "Functions to run in doi menu. Each entry is a list of (key menu-name function).
424 The function must take one argument, the doi.")
426 (setq jmax-bibtex-menu-funcs
427 '(("p" "df" jmax-bibtex-pdf)
428 ("w" "os" doi-utils-wos)
429 ("c" "iting articles" doi-utils-wos-citing)
430 ("r" "elated articles" doi-utils-wos-related)
431 ("s" "Google Scholar" doi-utils-google-scholar)
432 ("P" "Pubmed" doi-utils-pubmed)
433 ("f" "CrossRef" doi-utils-crossref)))
435 (defun jmax-bibtex ()
436 "Menu command to run in a bibtex entry.
437 Functions from `jmax-bibtex-menu-funcs'. They all rely on the
441 ;; construct menu string as a message
446 (concat "[" (elt tup 0) "]"
448 jmax-bibtex-menu-funcs "") ": "))
449 (let* ((input (read-char-exclusive))
451 (char-to-string input) jmax-bibtex-menu-funcs)))
457 (jmax-bibtex-entry-doi)
460 (defalias 'jb 'jmax-bibtex)
462 (provide 'jmax-bibtex)
464 ;;; jmax-bibtex.el ends here