]> git.donarmstrong.com Git - org-ref.git/blob - jmax-bibtex.el
add helm-config
[org-ref.git] / jmax-bibtex.el
1 ;;; jmax-bibtex.el --- jmax-bibtex utilities
2
3 ;;; Commentary:
4 ;; Requires: s.el, dash.el, org-ref.el, doi-utils.el
5
6 ;;; Code:
7
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.")
89
90
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'."
94   (interactive)
95   (with-temp-file "longtitles.bib"
96     (dolist (row jmax-bibtex-journal-abbreviations)
97       (insert (format "@string{%s=\"%s\"}\n"
98                       (nth 0 row)
99                       (nth 1 row))))))
100
101
102 (defun jmax-bibtex-generate-shorttitles ()
103     "Generate shorttitles.bib which are @string definitions.
104 The abbreviated journal names in `jmax-bibtex-journal-abbreviations'."
105   (interactive)
106   (with-temp-file "shorttitles.bib"
107     (dolist (row jmax-bibtex-journal-abbreviations)
108       (insert (format "@string{%s=\"%s\"}\n"
109                       (nth 0 row)
110                       (nth 2 row))))))
111
112
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'"
118   (interactive)
119   (bibtex-beginning-of-entry)
120   (when
121       (string= "article"
122                (downcase
123                 (cdr (assoc "=type=" (bibtex-parse-entry)))))
124     (let* ((full-names (mapcar
125                         (lambda (row)
126                           (cons  (nth 1 row) (nth 0 row)))
127                         jmax-bibtex-journal-abbreviations))
128            (abbrev-names (mapcar
129                           (lambda (row)
130                             (cons  (nth 2 row) (nth 0 row)))
131                           jmax-bibtex-journal-abbreviations))
132            (journal (s-trim (bibtex-autokey-get-field "journal")))
133            (bstring (or
134                      (cdr (assoc journal full-names))
135                      (cdr (assoc journal abbrev-names)))))
136       (when bstring
137         (bibtex-set-field "journal" bstring t)
138         (bibtex-fill-entry)))))
139
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'."
143   (interactive (list
144                 (ido-completing-read
145                  "Journal: "
146                  (mapcar
147                   (lambda (x)
148                     (nth 1 x))
149                   jmax-bibtex-journal-abbreviations))))
150   ;; construct data alist for the string lookup.
151   (let ((alist (mapcar
152                 (lambda (x)
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)
156     (bibtex-fill-entry)
157     (bibtex-clean-entry)))
158
159 ;; see https://github.com/fxcoudert/tools/blob/master/doi2bib for more replacements
160 (defvar jmax-nonascii-latex-replacements
161   '()
162   "Cons list of non-ascii characters and their LaTeX representations.")
163
164 (setq jmax-nonascii-latex-replacements
165       '(("í" . "{\\\\'i}")
166         ("æ" . "{\\\\ae}")
167         ("ć" . "{\\\\'c}")
168         ("é" . "{\\\\'e}")
169         ("ä" . "{\\\\\"a}")
170         ("è" . "{\\\\`e}")
171         ("à" . "{\\\\`a}")
172         ("á" . "{\\\\'a}")
173         ("ø" . "{\\\\o}")
174         ("ë" . "{\\\\\"e}")
175         ("ü" . "{\\\\\"u}")
176         ("ñ" . "{\\\\~n}")
177         ("ņ" . "{\\\\c{n}}")
178         ("å" . "{\\\\aa}")
179         ("ö" . "{\\\\\"o}")
180         ("Á" . "{\\\\'A}")
181         ("á" . "{\\\\'a}")
182         ("í" . "{\\\\'i}")
183         ("ó" . "{\\\\'o}")
184         ("ó" . "{\\\\'o}")
185         ("ú" .  "{\\\\'u}")
186         ("ú" . "{\\\\'u}")
187         ("š" . "{\\\\v{s}}")
188         ("ř"  . "{\\\\v{r}}")
189         ("İ" . "{\\\\.I}")
190         ("ğ" . "{\\\\u{g}}")
191         ("δ" . "$\\\\delta$")
192         ("ç" . "{\\\\c{c}}")
193         ("ß" . "{\\\\ss}")
194         ("≤" . "$\\\\le$")
195         ("<" . "$\\\\lt$")
196         ("θ" . "$\\\\theta$")
197         ("μ" . "$\\\\mu$")
198         ("→" . "$\\\\rightarrow$")
199         ("⇌" . "$\\\\leftrightharpoons$")
200         ("×" . "$\\\\times$")
201         ("°" . "$\\\\deg$")
202         ;; I think these are non-ascii spaces. there seems to be more than one.
203         (" " . " ")
204         (" " . " ")
205         ("–" . "-")
206         ("−" . "-")
207         ("–" . "-")
208         ("‘" . "'")
209         ("’" . "'")
210         ("“" . "\"")
211         ("”" . "\"")))
212
213 (defun jmax-replace-nonascii ()
214   "Hook function to replace non-ascii characters in a bibtex entry."
215
216   (interactive)
217   (save-restriction
218     (bibtex-narrow-to-entry)
219     (goto-char (point-min))
220     (dolist (char (mapcar (lambda (x) (car x)) jmax-nonascii-latex-replacements))
221       (while (re-search-forward char nil t)
222         (replace-match (cdr (assoc char jmax-nonascii-latex-replacements))))
223       (goto-char (point-min)))))
224
225 (add-hook 'org-ref-clean-bibtex-entry-hook 'jmax-replace-nonascii)
226
227
228 (defvar jmax-lower-case-words
229   '("a" "an" "on" "and" "for"
230     "the" "of" "in")
231   "List of words to keep lowercase when changing case in a title.")
232
233
234 (defun jmax-title-case-article (&optional key start end)
235   "Convert a bibtex entry article title to title-case.
236 The arguments KEY, START and END are optional, and are only there
237 so you can use this function with `bibtex-map-entries' to change
238 all the title entries in articles."
239   (interactive)
240   (bibtex-beginning-of-entry)
241
242   (let* ((title (bibtex-autokey-get-field "title"))
243          (words (split-string title))
244          (start 0))
245     (when
246         (string= "article" (downcase (cdr (assoc "=type=" (bibtex-parse-entry)))))
247       (setq words (mapcar
248                    (lambda (word)
249                      (if (or
250                           ;; match words containing {} or \ which are probably
251                           ;; LaTeX or protected words
252                           (string-match "\\$\\|{\\|}\\|\\\\" word)
253                           ;; these words should not be capitalized, unless they
254                           ;; are the first word
255                           (-contains? jmax-lower-case-words (s-downcase word)))
256                          word
257                        (s-capitalize word)))
258                    words))
259
260       ;; Check if first word should be capitalized
261       (when (-contains? jmax-lower-case-words (car words))
262         (setf (car words) (s-capitalize (car words))))
263
264       (setq title (mapconcat 'identity words " "))
265
266       ;; Capitalize letters after a dash
267       (while
268           (string-match "[a-zA-Z]-\\([a-z]\\)" title start)
269         (let ((char (substring title (match-beginning 1) (match-end 1))))
270           (setf (substring title (match-beginning 1) (match-end 1))
271                 (format "%s" (upcase char)))
272           (setq start (match-end 1))))
273
274       ;; this is defined in doi-utils
275       (bibtex-set-field
276        "title"
277        title)
278       (bibtex-fill-entry))))
279
280 (add-hook 'org-ref-clean-bibtex-entry-hook 'jmax-title-case-article)
281
282
283 (defun jmax-sentence-case-article (&optional key start end)
284   "Convert a bibtex entry article title to sentence-case.
285 The arguments KEY, START and END are optional, and are only there
286 so you can use this function with `bibtex-map-entries' to change
287 all the title entries in articles."
288   (interactive)
289   (bibtex-beginning-of-entry)
290
291   (let* ((title (bibtex-autokey-get-field "title"))
292          (words (split-string title))
293          (start 0))
294     (when
295         (string= "article" (downcase (cdr (assoc "=type=" (bibtex-parse-entry)))))
296       (setq words (mapcar
297                    (lambda (word)
298                      (if
299                          ;; match words containing {} or \ which are probably
300                          ;; LaTeX or protected words
301                          (string-match "\\$\\|{\\|}\\|\\\\" word)
302                          word
303                        (s-downcase word)))
304                    words))
305
306       ;; capitalize first word
307       (setf (car words) (s-capitalize (car words)))
308
309       ;; join the words
310       (setq title (mapconcat 'identity words " "))
311
312       ;; capitalize a word after a :, eg. a subtitle, and protect it
313       (while
314           (string-match "[a-z]:\\s-+\\([A-Z]\\)" title start)
315         (let ((char (substring title (match-beginning 1) (match-end 1))))
316           (setf (substring title (match-beginning 1) (match-end 1))
317 ;;              (format "{%s}" (upcase char)))
318                 (format "%s" (upcase char)))
319           (setq start (match-end 1))))
320
321       ;; this is defined in doi-utils
322       (bibtex-set-field
323        "title" title)
324
325       ;; clean and refill entry so it looks nice
326       (bibtex-clean-entry)
327       (bibtex-fill-entry))))
328
329
330
331
332 (defun jmax-bibtex-next-entry (&optional n)
333   "Jump to the beginning of the next bibtex entry.
334 N is a prefix argument.  If it is numeric, jump that many entries
335 forward.  Negative numbers do nothing."
336   (interactive "P")
337   ;; Note if we start at the beginning of an entry, nothing
338   ;; happens. We need to move forward a char, and call again.
339   (when (= (point) (save-excursion
340                      (bibtex-beginning-of-entry)))
341     (forward-char)
342     (jmax-bibtex-next-entry))
343
344   ;; search forward for an entry
345   (when
346       (re-search-forward bibtex-entry-head nil t (and (numberp n) n))
347     ;; go to beginning of the entry
348     (bibtex-beginning-of-entry)))
349
350
351 (defun jmax-bibtex-previous-entry (&optional n)
352   "Jump to beginning of the previous bibtex entry.
353 N is a prefix argument.  If it is numeric, jump that many entries back."
354   (interactive "P")
355   (bibtex-beginning-of-entry)
356  (when
357      (re-search-backward bibtex-entry-head nil t (and (numberp n) n))
358    (bibtex-beginning-of-entry)))
359
360
361 (defun jmax-bibtex-mode-keys ()
362   "Modify keymaps used by `bibtex-mode'."
363   (local-set-key (kbd "M-n") 'jmax-bibtex-next-entry)
364   (local-set-key (kbd "M-p") 'jmax-bibtex-previous-entry))
365
366 ;; add to bibtex-mode-hook
367 (add-hook 'bibtex-mode-hook 'jmax-bibtex-mode-keys)
368
369
370 (defun jmax-bibtex-entry-doi ()
371   "get doi from entry at point"
372   (interactive)
373   (save-excursion
374     (bibtex-beginning-of-entry)
375     (reftex-get-bib-field "doi" (bibtex-parse-entry t))))
376
377
378 (defun jmax-bibtex-wos ()
379   "Open bibtex entry in Web Of Science if there is a DOI"
380   (interactive)
381   (doi-utils-wos (jmax-bibtex-entry-doi)))
382
383
384 (defun jmax-bibtex-wos-citing ()
385   "Open citing articles for bibtex entry in Web Of Science if there is a DOI"
386   (interactive)
387   (doi-utils-wos-citing (jmax-bibtex-entry-doi)))
388
389
390 (defun jmax-bibtex-wos-related ()
391   "Open related articles for bibtex entry in Web Of Science if there is a DOI"
392   (interactive)
393   (doi-utils-wos-related (jmax-bibtex-entry-doi)))
394
395
396 (defun jmax-bibtex-wos-citing ()
397   "Open citing articles for bibtex entry in Web Of Science if there is a DOI"
398   (interactive)
399   (doi-utils-wos-citing (jmax-bibtex-entry-doi)))
400
401
402 (defun jmax-bibtex-crossref ()
403   (interactive)
404   (doi-utils-crossref (jmax-bibtex-entry-doi)))
405
406
407 (defun jmax-bibtex-google-scholar ()
408   (interactive)
409   (doi-utils-google-scholar (jmax-bibtex-entry-doi)))
410
411
412 (defun jmax-bibtex-pubmed ()
413   (interactive)
414   (doi-utils-pubmed (jmax-bibtex-entry-doi)))
415
416
417 (defun jmax-bibtex-pdf (doi)
418   "thin wrapper to get `jmax-bibtex' to open pdf"
419   (interactive)
420   (org-ref-open-bibtex-pdf))
421
422
423
424 (defvar jmax-bibtex-menu-funcs '()
425  "Functions to run in doi menu. Each entry is a list of (key menu-name function).
426 The function must take one argument, the doi.")
427
428 (setq jmax-bibtex-menu-funcs
429       '(("p" "df" jmax-bibtex-pdf)
430         ("C" "opy" (lambda (doi)
431                      (kill-new (org-ref-bib-citation))
432                      (bury-buffer)))
433         ("w" "os" doi-utils-wos)
434         ("c" "iting articles" doi-utils-wos-citing)
435         ("r" "elated articles" doi-utils-wos-related)
436         ("s" "Google Scholar" doi-utils-google-scholar)
437         ("P" "Pubmed" doi-utils-pubmed)
438         ("f" "CrossRef" doi-utils-crossref)))
439
440 (defun jmax-bibtex ()
441   "Menu command to run in a bibtex entry.
442 Functions from `jmax-bibtex-menu-funcs'. They all rely on the
443 entry having a doi."
444
445   (interactive)
446   ;; construct menu string as a message
447   (message
448    (concat
449     (mapconcat
450      (lambda (tup)
451        (concat "[" (elt tup 0) "]"
452                (elt tup 1) " "))
453      jmax-bibtex-menu-funcs "") ": "))
454   (let* ((input (read-char-exclusive))
455          (choice (assoc
456                   (char-to-string input) jmax-bibtex-menu-funcs)))
457     (when choice
458       (funcall
459        (elt
460         choice
461         2)
462        (jmax-bibtex-entry-doi)
463        ))))
464
465 (defalias 'jb 'jmax-bibtex)
466
467 (provide 'jmax-bibtex)
468
469 ;;; jmax-bibtex.el ends here