home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / modes / bib-mode.el < prev    next >
Encoding:
Text File  |  1992-06-29  |  7.0 KB  |  239 lines

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