;; bibtexscan.el --- run a `find' command on the bibtex title and ;; dired the output ;; Copyright (C) 2009 Nuno Sucena Almeida ;; Author: Nuno Sucena Almeida ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;; ---------------------------------------------------------------------- ;; I usually store the articles using its own title as filename, so i ;; search for it by getting the bibtex title field text. It needs ;; improvement, but works reasonably well for now. ;; ;; ;; Add this to your .emacs: ;; (add-hook 'bibtex-mode-hook ;; (lambda () ;; (load-file "~/programming/bibtexscan/bibtexscan.el"))) ;; ;; The function that it's actually being used is 'find-article-by-title'. ;; I'm experimenting with some other ways of doing it,so I keep the code ;; in case you find it useful. ;; Code: (defvar nsa/find-article-pdf-location (expand-file-name "~/research/pdfs") "Location of PDF archive") (global-set-key "\C-c\C-a" 'find-article-by-title) ;; emacs 23 properly supports mimetpes, so we can use dired to navigate ;; and load a file using the appropriate viewer. ;; (global-set-key "\C-c\C-a" 'find-article-by-title-dired) (defun find-article-file (path key) " Find first article matching KEY starting from path PATH " (interactive) (progn (let ((args (concat find-program " " path " -iname " key " -print0 -quit"))) (message (concat "args:" args)) (save-excursion (shell-command-to-string (concat args "&")) )) ) ) (defun find-article-by-title-dired() " Find article by bibtex title entry, starting from path PATH then calls the find-dired command. The user can then use the ! command (dired-do-shell-command) to visit a file. " (interactive) (save-excursion ;; go to the beginning og the bibtex entry (bibtex-beginning-of-entry) ;; get text contents of title field (let (( title (bibtex-text-in-field "title"))) (message (concat "title:" title)) (setq key (concat " -iname \"*" title "*\"")) (find-dired nsa/find-article-pdf-location key) ) ) ) (defun find-article-by-title () " Find article by bibtex title entry, starting from path PATH then calls the viewer. If the file is not found, use the regular bibtex-url mechanism to find it online. " (interactive) (save-excursion ;; go to the beginning og the bibtex entry (bibtex-beginning-of-entry) ;; get text contents of title field (let (( keytext (bibtex-text-in-field "title"))) (message (concat "title:" keytext)) ;; remove LaTeX math strings $ $ and replace it with * (setq keytext (replace-regexp-in-string "\\$\\(.\\|\n\\)*?\\$" "*" keytext)) ;; remove Latex [ ] and replace it with * (setq keytext (replace-regexp-in-string "\\[\\(.\\|\n\\)*?\\]" "*" keytext)) ;; remove stuff inside ( ) and replace it with * (setq keytext (replace-regexp-in-string "\(\\(.\\|\n\\)*?\)" "*" keytext)) ;; remove non letters (setq keytext (replace-regexp-in-string "[^a-z0-9]+" "*" keytext)) ;; escape keytext ;;(setq keytext (concat "\"*" (shell-quote-argument keytext) "*\"")) ;; escape keytext for shell, but leave wildcards alone (setq keytext (concat "\"*" (shell-quote-wildcard-pattern keytext) "*\"")) (setq foundfile ;; try to find file (find-article-file nsa/find-article-pdf-location keytext) ) ;; if returned file is non-empty, browse it locally (if (not (equal foundfile "")) (save-excursion (message (concat "found:[" foundfile "]")) (browse-url foundfile) ) ;; if not found, use bibtex-url mechanism to find it online (save-excursion (message "not found, using bibtex-url") (browse-url (bibtex-url (point) t)) ) ) ) ) ) ;; History of path values entered in the minibuffer. (defvar find-article-entry-type-history nil) (defun find-article-by-field (path field) " Find article by bibtex FIELD entry, starting from path PATH then calls the viewer. If the file is not found, use the regular bibtex-url mechanism to find it online. " (interactive (list (read-directory-name "Local article archive:" nsa/find-article-pdf-location) (read-string "Field Type:" "title" 'find-article-entry-type-history "title"))) ;; (completing-read "Field Type:" bibtex-entry-field-alist ;; nil t nil 'bibtex-entry-type-history))) (save-excursion ;; go to the beginning og the bibtex entry (bibtex-beginning-of-entry) ;; get text contents of title field (let (( keytext (bibtex-text-in-field field))) (message (concat field ":" keytext)) (setq keytext (concat "\"*" (shell-quote-argument keytext) "*\"")) (setq foundfile ;; try to find file (find-article-file path keytext) ) ;; if returned file is non-empty, browse it locally (if (not (equal foundfile "")) (save-excursion (message (concat "found:" foundfile "")) (browse-url foundfile) ) ;; if not found, use bibtex-url mechanism to find it online (save-excursion (message "not found, using bibtex-url") ;; (browse-url (bibtex-url (point) t)) ) ) ) ) ) (defun find-article-by-field-dired(path field) "Find article by bibtex FIELD entry, starting from path PATH then calls the `find-dired' command. The user can then use the ! command `dired-do-shell-command' to visit a file by using the appropriate viewer. " (interactive (list (read-directory-name "Local article archive:" nsa/find-article-pdf-location) (read-string "Field Type:" "title" 'find-article-entry-type-history "title"))) (save-excursion ;; go to the beginning og the bibtex entry (bibtex-beginning-of-entry) ;; get text contents of title field (let (( fieldtext (bibtex-text-in-field field))) (message (concat field ":" fieldtext)) (setq key (concat " -iname \"*" (shell-quote-argument fieldtext) "*\"")) (find-dired path key) ) ) ) ;; new url-list, partially adapted from ;; http://74.125.113.132/search?q=cache:0Mf41t8d8YIJ:www.fhi-berlin.mpg.de/mp/jochen/Computer/EmacsLaTeX+bibtex-generate-url-list&cd=3&hl=en&ct=clnk&gl=us ;; ;; I removed the search by key and changed the PR to P.*R.* (setq bibtex-generate-url-list '( (("doi" . "10\\..*/.*") "http://dx.doi.org/%s" ("doi" ".*" 0)) (("url" . ".*:.*")) (("journal" . "\\<\\(P.*R.*[ABCDEL]?\\|RMP\\)\\>") "http://link.aps.org/abstract/%s/v%s/p%s" ("journal" ".*" downcase) ("volume" ".*" 0) ("pages" "\\`\\([0-9]+\\)" 1)) (("journal" . "\\<\\(APL\\|JAP\\|JCP\\|JMP\\|JPCRD\\|RSI\\)\\>") "http://link.aip.org/link/?%s/%s/%s/1" ("journal" ".*" upcase) ("volume" ".*" 0) ("pages" "\\`\\([0-9]+\\)" 1))))