bib-mode.el 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. ;;; bib-mode.el --- major mode for editing bib files
  2. ;; Copyright (C) 1989, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Henry Kautz
  4. ;; (according to authors.el)
  5. ;; Maintainer: FSF
  6. ;; Keywords: bib
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; GNU Emacs code to help maintain databases compatible with (troff)
  20. ;; refer and lookbib. The file bib-file should be set to your
  21. ;; bibliography file. Keys are automagically inserted as you type,
  22. ;; and appropriate keys are presented for various kinds of entries.
  23. ;;; Code:
  24. (defgroup bib nil
  25. "Major mode for editing bib files."
  26. :prefix "bib-"
  27. :group 'external
  28. :group 'wp)
  29. (defcustom bib-file "~/my-bibliography.bib"
  30. "Default name of file used by `addbib'."
  31. :type 'file
  32. :group 'bib)
  33. (defcustom unread-bib-file "~/to-be-read.bib"
  34. "Default name of file used by `unread-bib' in Bib mode."
  35. :type 'file
  36. :group 'bib)
  37. (defvar bib-mode-map
  38. (let ((map (make-sparse-keymap)))
  39. (set-keymap-parent map text-mode-map)
  40. (define-key map "\C-M" 'return-key-bib)
  41. (define-key map "\C-c\C-u" 'unread-bib)
  42. (define-key map "\C-c\C-@" 'mark-bib)
  43. (define-key map "\e`" 'abbrev-mode)
  44. map))
  45. (defun addbib ()
  46. "Set up editor to add to troff bibliography file specified
  47. by global variable `bib-file'. See description of `bib-mode'."
  48. (interactive)
  49. (find-file bib-file)
  50. (goto-char (point-max))
  51. (bib-mode)
  52. )
  53. (define-derived-mode bib-mode text-mode "Bib"
  54. "Mode for editing `lookbib' style bibliographies.
  55. Hit RETURN to get next % field key.
  56. If you want to ignore this field, just hit RETURN again.
  57. Use `text-mode' to turn this feature off.
  58. journal papers: A* T D J V N P K W X
  59. articles in books & proceedings: A* T D B E* I C P K W X
  60. tech reports: A* T D R I C K W X
  61. books: A* T D I C K W X
  62. Fields:
  63. A uthor T itle D ate J ournal
  64. V olume N umber P age K eywords
  65. B in book or proceedings E ditor C ity & state
  66. I nstitution, school, or publisher
  67. R eport number or 'phd thesis' or 'masters thesis' or 'draft' or
  68. 'unnumbered' or 'unpublished'
  69. W here can be found locally (login name, or ailib, etc.)
  70. X comments (not used in indexing)
  71. \\[unread-bib] appends current entry to a different file (for example,
  72. a file of papers to be read in the future), given by the value of the
  73. variable `unread-bib-file'.
  74. \\[mark-bib] marks current or previous entry.
  75. Abbreviations are saved in `bib-mode-abbrev-table'.
  76. Hook can be stored in `bib-mode-hook'.
  77. Field keys given by variable `bib-assoc'.
  78. Commands:
  79. \\{bib-mode-map}"
  80. (abbrev-mode 1))
  81. (defconst bib-assoc
  82. '((" *$" . "%A ")
  83. ("%A ." . "%A ")
  84. ("%A $" . "%T ")
  85. ("%T " . "%D ")
  86. ("%D " . "%J ")
  87. ("%J ." . "%V ")
  88. ("%V " . "%N ")
  89. ("%N " . "%P ")
  90. ("%P " . "%K ")
  91. ("%K " . "%W ")
  92. ("%W " . "%X ")
  93. ("%X " . "")
  94. ("%J $" . "%B ")
  95. ("%B ." . "%E ")
  96. ("%E ." . "%E ")
  97. ("%E $" . "%I ")
  98. ("%I " . "%C ")
  99. ("%C " . "%P ")
  100. ("%B $" . "%R ")
  101. ("%R " . "%I "))
  102. "Describes bibliographic database format.
  103. A line beginning with the car of an entry is followed by one beginning
  104. with the cdr.")
  105. (defun bib-find-key (slots)
  106. (cond
  107. ((null slots)
  108. (if (bobp)
  109. ""
  110. (progn (forward-line -1) (bib-find-key bib-assoc))))
  111. ((looking-at (car (car slots)))
  112. (cdr (car slots)))
  113. (t (bib-find-key (cdr slots)))
  114. ))
  115. (defcustom bib-auto-capitalize t
  116. "*True to automatically capitalize appropriate fields in Bib mode."
  117. :type 'boolean
  118. :group 'bib)
  119. (defconst bib-capitalized-fields "%[AETCBIJR]")
  120. (defun return-key-bib ()
  121. "Magic when user hits return, used by `bib-mode'."
  122. (interactive)
  123. (if (eolp)
  124. (let (empty new-key beg-current end-current)
  125. (beginning-of-line)
  126. (setq empty (looking-at "%. $"))
  127. (if (not empty)
  128. (progn
  129. (end-of-line)
  130. (newline)
  131. (forward-line -1)
  132. ))
  133. (end-of-line)
  134. (setq end-current (point))
  135. (beginning-of-line)
  136. (setq beg-current (point))
  137. (setq new-key (bib-find-key bib-assoc))
  138. (if (and (not empty) bib-auto-capitalize
  139. (looking-at bib-capitalized-fields))
  140. (save-excursion
  141. (bib-capitalize-title-region (+ (point) 3) end-current)))
  142. (goto-char beg-current)
  143. (if empty
  144. (kill-line nil)
  145. (forward-line 1)
  146. )
  147. (insert new-key))
  148. (newline)))
  149. (defun mark-bib ()
  150. "Set mark at beginning of current or previous bib entry, point at end."
  151. (interactive)
  152. (beginning-of-line nil)
  153. (if (looking-at "^ *$") (re-search-backward "[^ \n]" nil 2))
  154. (re-search-backward "^ *$" nil 2)
  155. (re-search-forward "^%")
  156. (beginning-of-line nil)
  157. (push-mark (point))
  158. (re-search-forward "^ *$" nil 2)
  159. (forward-line 1)
  160. (beginning-of-line nil))
  161. (defun unread-bib ()
  162. "Append current or previous entry to file of unread papers
  163. named by variable `unread-bib-file'."
  164. (interactive)
  165. (mark-bib)
  166. (if (get-file-buffer unread-bib-file)
  167. (append-to-buffer (get-file-buffer unread-bib-file) (mark) (point))
  168. (append-to-file (mark) (point) unread-bib-file)))
  169. (defvar bib-capitalize-title-stop-words
  170. (concat
  171. "the\\|and\\|of\\|is\\|a\\|an\\|of\\|for\\|in\\|to\\|in\\|on\\|at\\|"
  172. "by\\|with\\|that\\|its")
  173. "Words not to be capitalized in a title (unless the first word).")
  174. (defvar bib-capitalize-title-stop-regexp
  175. (concat "\\(" bib-capitalize-title-stop-words "\\)\\(\\b\\|'\\)"))
  176. (defun bib-capitalize-title-region (begin end)
  177. "Like `capitalize-region', but don't capitalize stop words, except the first."
  178. (interactive "r")
  179. (let ((case-fold-search nil) (orig-syntax-table (syntax-table)))
  180. (unwind-protect
  181. (save-restriction
  182. (set-syntax-table text-mode-syntax-table)
  183. (narrow-to-region begin end)
  184. (goto-char (point-min))
  185. (if (looking-at "[A-Z][a-z]*[A-Z]")
  186. (forward-word 1)
  187. (capitalize-word 1))
  188. (while (re-search-forward "\\<" nil t)
  189. (if (looking-at "[A-Z][a-z]*[A-Z]")
  190. (forward-word 1)
  191. (if (let ((case-fold-search t))
  192. (looking-at bib-capitalize-title-stop-regexp))
  193. (downcase-word 1)
  194. (capitalize-word 1)))
  195. ))
  196. (set-syntax-table orig-syntax-table))))
  197. (defun bib-capitalize-title (s)
  198. "Like `capitalize', but don't capitalize stop words, except the first."
  199. (with-current-buffer (get-buffer-create "$$$Scratch$$$")
  200. (erase-buffer)
  201. (insert s)
  202. (bib-capitalize-title-region (point-min) (point-max))
  203. (buffer-string)))
  204. (provide 'bib-mode)
  205. ;;; bib-mode.el ends here