;;; alpha-point.el --- Figure out where to put a record in an alphabetically sorted file of records. ;; Copyright (C) 1997 Yale University ;; Author: Bradley C. Kuszmaul ;; This file is distributed under the terms of the GNU General Public ;; License (GPL) as published by the Free Software Foundation; either ;; version 2, or (at your option) any later version. ;; ;; This file 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 GPL ;; for details. ;; ;; You should have received a copy of the GPL along with GNU emacs. ;; Presumably, you need GNU emacs to use this program. ;; Find a key in an emacs file that is sorted alphabetically ;; LCD Archive Entry: ;; alpha-point|Bradley C. Kuszmaul|bradley@ee.yale.edu| ;; Figure out where to put a record in an alphabetically sorted file of records (useful for editing bibtex files and rolodex or address books.)| ;; $Date: 1997/07/30 16:22:13 $|$Id: alpha-point.el,v 1.3 1997/07/30 16:22:13 bradley Exp $| (defvar alpha-find-beginning-of-record "^\n\\s-*\\(%.*\n\\)*" "A regexp to determine where the beginning of the alphabetical part of the record is. The regexp must preceed the key. The default is to find a paragraph (delimited by a blank line) and skip over any lines that begin with optional whitespace followed by a percent. This works well for latex and also for some other files, such as Bradley's name-and-address file.") ;; This is suitable for bibtex mode. ;; It is an @ at the beginning of the line, followed by some letters ;; followed by an open bracket or parent, followed by optional whitespace or endcomments (newlines and control-L are endcomment) (defvar bibtex-find-beginning-of-record "^@[A-Za-z]+[[{(]\\(\\s-\\|\\s>\\)*" "A good value for alpha-find-beginning-of-record in bibtex mode") (add-hook 'bibtex-mode-hook (function (lambda () (set (make-local-variable 'alpha-find-beginning-of-record) bibtex-find-beginning-of-record)))) (defun alpha-point (name) "Find where to put name in an alphabetically sorted file. Finds the first entry in the file which is alphabetically >= the argument. Uses alpha-find-beginning-of-record to find the beginning of the key. For example, in a bibtex file where the labels are kept in ascending alphabetical order, try doing m-x alpha-point foo The point will be placed at the first record whose label is lexically after foo. Bradley binds alpha-point to C-. (which works just fine under X, but won't work under most older systems where the control key is not allowed to modify period.)" (interactive "sName to find:") (goto-char (point-min)) (block nil (let ((nb (get-buffer-create " *Alpha Find*")) (nb-position nil) (nb-end nil) (cb (current-buffer)) (cb-position (point))) (set-buffer nb) (goto-char (point-min)) (delete-char (- (point-max) (point-min)) nil) (insert name) (setq nb-position (point-min)) (setq nb-end (point-max)) (set-buffer cb) (while (search-forward-regexp alpha-find-beginning-of-record (point-max) t) (setq cb-position (point)) (if (null (search-forward-regexp "\\s-" (point-max) t)) (goto-char (point-max))) (setq cb-end (- (point) 1)) (goto-char cb-position) (let ((comp (compare-buffer-substrings nb nb-position nb-end cb cb-position cb-end))) (cond ((<= comp 0) (return nil)))))))) ;;; $Log: alpha-point.el,v $ ;;; Revision 1.3 1997/07/30 16:22:13 bradley ;;; Fix a typo ;;; ;;; Revision 1.2 1997/07/30 02:09:33 bradley ;;; alpha-point modifications ;;; ;;; Revision 1.1 1997/07/30 02:01:59 bradley ;;; Initial version. ;;;