automatic formatting bib entry and generating proper file name for pdf file

This is the subroutine I use frequently to reformat bib entries downloaded from journal websites.
It can achieve the following things:

1. rename the bib entry name as FirstAuthorLastName-Year-Title
2. it will remove the lengthy abstract and note items
3. it will generate a filename for saving pdf copy in the format of FirstAuthorLastName_Year_Title_Journal.pdf
4. it will move the url item to the end and comment it

Here is the code:

;; Filename: fbib.el
;; Author: Da Zhang
;; Usage:
;; Compile:
;; System:
;; Bugs:
;; Created: Thu Apr 29 23:38:36 2010
;; Last-Updated: Fri Oct 15 14:22:05 2010 (-14400 -0400)
;; Update #: 40
;; Description:
;;;;;;;;;;;;;;;;;;;;;;;;;;; -*- Mode: Emacs-Lisp -*- ;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:

(defun fbib ()
“Format the bib entry copied from websites, and generate the file name for saving the pdf files systematically.”
(interactive)
(goto-char (point-max))
(re-search-backward “@” nil t)
(beginning-of-line)
(setq beg-pos (point))

;; remove the original bib entry name
(re-search-forward “\{” nil t)
(re-search-forward “,”)
(backward-char)
(let ((beg (point)))
(re-search-backward “\{” nil t)
(forward-char)
(delete-region beg (point)))

;; search for author name and copy it to bib entry name
(let ((tmp (point)))
(re-search-forward “author” nil t)
(re-search-forward “\{” nil t)
(let ((start (point)))
(re-search-forward “\}” nil t)
(let ((end (point)))
(if (re-search-in-region “,” start end)
(backward-word 1)
(if (re-search-in-region “and” start end)
(backward-word 2)
(re-search-forward “\}” nil t)
(backward-word 1)))))
;; (re-search-forward “and” nil t)
;; (backward-word 2)
(let ((beg (point)))
(forward-word)
(copy-region-as-kill beg (point)))
(goto-char tmp)
(yank)
(insert “-“))
;; search for year and copy it to bib entry name
(let ((tmp (point)))
(re-search-forward “year” nil t)
(re-search-forward “\{” nil t)
(let ((beg (point)))
(forward-word)
(copy-region-as-kill beg (point)))
(goto-char tmp)
(yank)
(insert “-“))

;; search for article title and copy it to bib entry name
(let ((tmp (point)))
(re-search-forward “title” nil t)
(re-search-forward “\{” nil t)
(let ((beg (point)))
(re-search-forward “\}” nil t)
(backward-char)
(copy-region-as-kill beg (point)))
(goto-char tmp)
(yank)
(let ((bib-name-end (point)))
(replace-in-region ” ” “-” tmp bib-name-end)
(replace-in-region “:” “-” tmp bib-name-end)
))

;; optional: search keywords, and kill it
(goto-char beg-pos)
(if (re-search-forward “keywords” nil t)
(progn
(beginning-of-line)
(let ((beg (point)))
(re-search-forward “\},”)
(forward-char)
(kill-region beg (point)))))

;; optional: search url, and move it to the back of the entry
(goto-char beg-pos)
(if (re-search-forward “url” nil t)
(progn
(beginning-of-line)
(kill-line)
(re-search-forward “^\}” nil t)
(forward-char)
(yank)
(re-search-backward “url” nil t)
(beginning-of-line)
(let ((beg (point)))
(end-of-line)
(comment-region beg (point)))))

;; form the pdf file name and add it to the end of the buffer
(goto-char (point-max))
(let ((tmp (point)))
(goto-char beg-pos)
(re-search-forward “author” nil t)
(re-search-forward “\{” nil t)
(let ((start (point)))
(re-search-forward “\}” nil t)
(let ((end (point)))
(if (re-search-in-region “,” start end)
(backward-word 1)
(if (re-search-in-region “and” start end)
(backward-word 2)
(re-search-forward “\}” nil t)
(backward-word 1)))))
;; (re-search-forward “and” nil t)
;; (backward-word 2)
(let ((beg (point)))
(forward-word)
(copy-region-as-kill beg (point)))
(goto-char tmp)
(yank)
(insert “_”))
(let ((tmp (point)))
(re-search-backward “year” nil t)
(re-search-forward “\{” nil t)
(let ((beg (point)))
(forward-word)
(copy-region-as-kill beg (point)))
(goto-char tmp)
(yank)
(insert “_”))
(let ((tmp (point)))
(re-search-backward “title” nil t)
(re-search-forward “\{” nil t)
(let ((beg (point)))
(re-search-forward “\}” nil t)
(backward-char)
(copy-region-as-kill beg (point)))
(goto-char tmp)
(yank)
(insert “_”))
(let ((tmp (point)))
(re-search-backward “journal” nil t)
(re-search-forward “\{” nil t)
(let ((beg (point)))
(re-search-forward “\}” nil t)
(backward-char)
(copy-region-as-kill beg (point)))
(goto-char tmp)
(yank)
(insert “.pdf”))
(beginning-of-line)
(let ((pdf-name-beg (point)))
(end-of-line)
(replace-in-region “:” “_” pdf-name-beg (point)))

;; optional: search abstract, and delete it
(goto-char beg-pos)
(if (re-search-forward “abstract” nil t)
(progn
(beginning-of-line)
(let ((beg (point)))
(re-search-forward “\}” nil t)
(end-of-line)
(kill-region beg (point)))
(kill-line)))
)

(defun re-search-in-region (pat start end)
“regexp search forward in region specified by start and end.”
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(re-search-forward pat nil t)))

(defun replace-in-region (from-string to-string start end)
“Replace from-string with to-string in region specified by start and end.”
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(while (search-forward from-string nil t) (replace-match to-string nil t))
)
)

(provide ‘fbib)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; fbib.el ends here

About these ads

3 thoughts on “automatic formatting bib entry and generating proper file name for pdf file

  1. Have you think in using reftex for bibtex parsing? it already includes functions that can simplify your code, e.g. reftex-parse-bibtex-entry will generate an alist with the information from the bibtex, like author or abstract

  2. Pingback: Mossi Black Large Men’s Snow Bib | Ice Fishing Tips

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s