1 ;;; lsdb.el --- the Lovely Sister Database
3 ;; Copyright (C) 2002 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: adress book
8 ;; This file is part of the Lovely Sister Database.
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
28 ;;; (autoload 'lsdb-gnus-insinuate "lsdb")
29 ;;; (autoload 'lsdb-gnus-insinuate-message "lsdb")
30 ;;; (add-hook 'gnus-startup-hook 'lsdb-gnus-insinuate)
31 ;;; (add-hook 'message-setup-hook
33 ;;; (define-key message-mode-map "\M-\t" 'lsdb-complete-name)))
35 ;;; For Wanderlust, put the following lines into your ~/.wl:
37 ;;; (lsdb-wl-insinuate)
38 ;;; (add-hook 'wl-draft-mode-hook
40 ;;; (define-key wl-draft-mode-map "\M-\t" 'lsdb-complete-name)))
42 ;;; For Mew, put the following lines into your ~/.mew:
43 ;;; (autoload 'lsdb-mew-insinuate "lsdb")
44 ;;; (add-hook 'mew-init-hook 'lsdb-mew-insinuate)
45 ;;; (add-hook 'mew-draft-mode-hook
47 ;;; (define-key mew-draft-header-map "\M-I" 'lsdb-complete-name)))
56 ;;;_* USER CUSTOMIZATION VARIABLES:
58 "The Lovely Sister Database."
62 (defcustom lsdb-file (expand-file-name "~/.lsdb")
63 "The name of the Lovely Sister Database file."
67 (defcustom lsdb-file-coding-system (find-coding-system 'iso-2022-jp)
68 "Coding system for `lsdb-file'."
72 (defcustom lsdb-sender-headers
74 "List of headers to search for senders."
78 (defcustom lsdb-recipients-headers
79 "Resent-To\\|Resent-Cc\\|Reply-To\\|To\\|Cc\\|Bcc"
80 "List of headers to search for recipients."
84 (defcustom lsdb-interesting-header-alist
85 '(("Organization" nil organization)
86 ("\\(X-\\)?User-Agent\\|X-Mailer\\|X-Newsreader" nil user-agent)
87 ("\\(X-\\)?ML-Name" nil mailing-list)
88 ("List-Id" "\\(.*\\)[ \t]+<[^>]+>\\'" mailing-list "\\1")
89 ("X-Sequence" "\\(.*\\)[ \t]+[0-9]+\\'" mailing-list "\\1")
90 ("Delivered-To" "mailing list[ \t]+\\([^@]+\\)@.*" mailing-list "\\1")
91 ("\\(X-URL\\|X-URI\\)" nil www)
92 ("X-Attribution\\|X-cite-me" nil attribution)
93 ("X-Face" nil x-face))
94 "Alist of headers we are interested in.
95 The format of elements of this list should be
96 (FIELD-NAME REGEXP ENTRY STRING)
97 where the last three elements are optional."
101 (defcustom lsdb-entry-type-alist
103 (creation-date 2 ?. t)
104 (last-modified 3 ?. t)
112 "Alist of entry types for presentation.
113 The format of elements of this list should be
114 (ENTRY SCORE [CLASS READ-ONLY])
115 where the last two elements are optional.
116 Possible values for CLASS are `?.' and '?,'. If CLASS is `?.', the
117 entry takes a unique value which is overridden by newly assigned one
118 by `lsdb-mode-edit-entry' or such a command. If CLASS is `?,', the
119 entry can have multiple values separated by commas.
120 If the fourth element READ-ONLY is non-nil, it is assumed that the
121 entry cannot be modified."
125 (defcustom lsdb-decode-field-body-function #'lsdb-decode-field-body
126 "Field body decoder."
130 (defcustom lsdb-canonicalize-full-name-function
131 #'lsdb-canonicalize-spaces-and-dots
132 "Way to canonicalize full name."
136 (defcustom lsdb-lookup-full-name-functions
137 '(lsdb-lookup-full-name-from-address-cache)
138 "List of functions to pick up the existing full-name of the sender.
139 The sender is passed to each function as the argument."
143 (defcustom lsdb-update-record-functions
144 '(lsdb-update-address-cache)
145 "List of functions called after a record is updated.
146 The updated record is passed to each function as the argument."
150 (defcustom lsdb-secondary-hash-tables
151 '(lsdb-address-cache)
152 "List of the hash tables for reverse lookup"
156 (defcustom lsdb-window-max-height 7
157 "Maximum number of lines used to display LSDB record."
161 (defcustom lsdb-x-face-command-alist
162 '((pbm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale 0.5")
163 (xpm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale 0.5 | ppmtoxpm"))
164 "An alist from an image type to a command to be executed to display an X-Face header.
165 The command will be executed in a sub-shell asynchronously.
166 The compressed face will be piped to this command."
170 (defcustom lsdb-insert-x-face-function
171 (if (static-if (featurep 'xemacs)
173 (and (>= emacs-major-version 21)
174 (fboundp 'image-type-available-p)
175 (or (image-type-available-p 'pbm)
176 (image-type-available-p 'xpm))))
177 #'lsdb-insert-x-face-asynchronously)
178 "Function to display X-Face."
182 (defcustom lsdb-print-record-hook '(lsdb-expose-x-face)
183 "A hook called after a record is displayed."
187 (defcustom lsdb-display-records-sort-predicate nil
188 "A predicate to sort records."
192 (defgroup lsdb-edit-form nil
193 "A mode for editing forms."
196 (defcustom lsdb-edit-form-mode-hook nil
197 "Hook run in `lsdb-edit-form-mode' buffers."
198 :group 'lsdb-edit-form
201 (defcustom lsdb-shell-file-name "/bin/sh"
202 "File name to load inferior shells from.
203 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
207 (defcustom lsdb-shell-command-switch "-c"
208 "Switch used to have the shell execute its command line argument."
213 (defface lsdb-header-face
214 '((t (:underline t)))
215 "Face for the file header line in `lsdb-mode'."
217 (defvar lsdb-header-face 'lsdb-header-face)
219 (defface lsdb-field-name-face
220 '((((class color) (background dark))
221 (:foreground "PaleTurquoise" :bold t))
223 "Face for the message header line in `lsdb-mode'."
225 (defvar lsdb-field-name-face 'lsdb-field-name-face)
227 (defface lsdb-field-body-face
228 '((((class color) (background dark))
229 (:foreground "turquoise" :italic t))
231 "Face for the message header line in `lsdb-mode'."
233 (defvar lsdb-field-body-face 'lsdb-field-body-face)
235 (defconst lsdb-font-lock-keywords
237 (0 lsdb-header-face))
239 (0 lsdb-field-body-face))
240 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
241 (1 lsdb-field-name-face)
242 (2 lsdb-field-body-face))))
244 (put 'lsdb-mode 'font-lock-defaults '(lsdb-font-lock-keywords t))
246 ;;;_* CODE - no user customizations below
247 ;;;_. Internal Variables
248 (defvar lsdb-hash-table nil
249 "Internal hash table to hold LSDB records.")
251 (defvar lsdb-address-cache nil
252 "The reverse lookup table for `lsdb-hash-table'.
253 It represents address to full-name mapping.")
255 (defvar lsdb-buffer-name "*LSDB*"
256 "Buffer name to display LSDB record.")
258 (defvar lsdb-hash-tables-are-dirty nil
259 "Flag to indicate whether the internal hash tables need to be saved.")
261 (defvar lsdb-known-entry-names
263 "An obarray used to complete an entry name.")
265 ;;;_. Hash Table Emulation
266 (if (and (fboundp 'make-hash-table)
267 (subrp (symbol-function 'make-hash-table)))
269 (defalias 'lsdb-puthash 'puthash)
270 (defalias 'lsdb-gethash 'gethash)
271 (defalias 'lsdb-remhash 'remhash)
272 (defalias 'lsdb-maphash 'maphash)
273 (defalias 'lsdb-hash-table-size 'hash-table-size)
274 (defalias 'lsdb-hash-table-count 'hash-table-count)
275 (defalias 'lsdb-make-hash-table 'make-hash-table))
276 (defun lsdb-puthash (key value hash-table)
277 "Hash KEY to VALUE in HASH-TABLE."
278 ;; Obarray is regarded as an open hash table, as a matter of
279 ;; fact, rehashing doesn't make sense.
281 (when (> (car hash-table)
282 (* (length (nth 1 hash-table)) 0.7))
283 (setq new-obarray (make-vector (* (length (nth 1 hash-table)) 2) 0))
286 (set (intern (symbol-name symbol) new-obarray)
287 (symbol-value symbol)))
289 (setcdr hash-table (list new-obarray)))
290 (set (intern key (nth 1 hash-table)) value)
291 (setcar hash-table (1+ (car hash-table)))))
292 (defun lsdb-gethash (key hash-table &optional default)
293 "Find hash value for KEY in HASH-TABLE.
294 If there is no corresponding value, return DEFAULT (which defaults to nil)."
295 (let ((symbol (intern-soft key (nth 1 hash-table))))
297 (symbol-value symbol)
299 (defun lsdb-remhash (key hash-table)
300 "Remove the entry for KEY from HASH-TABLE.
301 Do nothing if there is no entry for KEY in HASH-TABLE."
302 (unintern key (nth 1 hash-table))
303 (setcar hash-table (1- (car hash-table))))
304 (defun lsdb-maphash (function hash-table)
305 "Map FUNCTION over entries in HASH-TABLE, calling it with two args,
306 each key and value in HASH-TABLE.
308 FUNCTION may not modify HASH-TABLE, with the one exception that FUNCTION
309 may remhash or puthash the entry currently being processed by FUNCTION."
312 (funcall function (symbol-name symbol) (symbol-value symbol)))
314 (defun lsdb-hash-table-size (hash-table)
315 "Return the size of HASH-TABLE.
316 This is the current number of slots in HASH-TABLE, whether occupied or not."
317 (length (nth 1 hash-table)))
318 (defalias 'lsdb-hash-table-count 'car)
319 (defun lsdb-make-hash-table (&rest args)
320 "Return a new empty hash table object."
321 (list 0 (make-vector (or (plist-get args :size) 29) 0))))
323 ;;;_. Hash Table Reader/Writer
324 (defconst lsdb-secondary-hash-table-start-format
327 (defsubst lsdb-secondary-hash-table-start (hash-table)
328 (format lsdb-secondary-hash-table-start-format hash-table))
333 ;; In XEmacs, hash tables can also be created by the lisp reader
334 ;; using structure syntax.
335 (read-from-string "#s(hash-table)")
336 (defalias 'lsdb-read 'read))
338 (defun lsdb-read (&optional marker)
339 "Read one Lisp expression as text from MARKER, return as Lisp object."
342 (if (looking-at "^#s(")
344 (buffer-disable-undo)
345 (insert-buffer-substring (marker-buffer marker) marker)
346 (goto-char (point-min))
348 (let ((object (read (current-buffer)))
350 (if (eq 'hash-table (car object))
353 (lsdb-make-hash-table
354 :size (plist-get (cdr object) 'size)
356 data (plist-get (cdr object) 'data))
358 (lsdb-puthash (pop data) (pop data) hash-table))
362 (defun lsdb-load-hash-tables ()
363 "Read the contents of `lsdb-file' into the internal hash tables."
364 (let ((buffer (find-file-noselect lsdb-file))
369 (goto-char (point-min))
370 (re-search-forward "^#s(")
371 (goto-char (match-beginning 0))
372 (setq lsdb-hash-table (lsdb-read (point-marker)))
373 ;; Load the secondary hash tables following.
374 (setq tables lsdb-secondary-hash-tables)
376 (if (re-search-forward
377 (concat "^" (lsdb-secondary-hash-table-start
380 (set (car tables) (lsdb-read (point-marker))))
381 (setq tables (cdr tables))))
382 (kill-buffer buffer))))
384 (defun lsdb-insert-hash-table (hash-table)
385 (insert "#s(hash-table size "
386 ;; Reduce the actual size of the close hash table, because
387 ;; XEmacs doesn't have a distinction between index-size and
389 (number-to-string (lsdb-hash-table-count hash-table))
390 " test equal data (")
393 (insert (prin1-to-string key) " " (prin1-to-string value) " "))
397 (defun lsdb-save-hash-tables ()
398 "Write the records within the internal hash tables into `lsdb-file'."
399 (let ((coding-system-for-write lsdb-file-coding-system)
401 (with-temp-file lsdb-file
402 (if (and (or (featurep 'mule)
403 (featurep 'file-coding))
404 lsdb-file-coding-system)
405 (let ((coding-system-name
406 (if (symbolp lsdb-file-coding-system)
407 (symbol-name lsdb-file-coding-system)
409 (static-if (featurep 'xemacs)
410 (symbol-name (coding-system-name
411 lsdb-file-coding-system))))))
412 (if coding-system-name
413 (insert ";;; -*- coding: " coding-system-name " -*-\n"))))
414 (lsdb-insert-hash-table lsdb-hash-table)
415 ;; Save the secondary hash tables following.
416 (setq tables lsdb-secondary-hash-tables)
418 (insert "\n" (lsdb-secondary-hash-table-start
420 (lsdb-insert-hash-table (symbol-value (car tables)))
421 (setq tables (cdr tables))))))
423 ;;;_. Mail Header Extraction
424 (defun lsdb-fetch-field-bodies (regexp)
426 (goto-char (point-min))
427 (let ((case-fold-search t)
429 (while (re-search-forward (concat "^\\(" regexp "\\):[ \t]*")
431 (push (funcall lsdb-decode-field-body-function
432 (buffer-substring (point) (std11-field-end))
435 (nreverse field-bodies))))
437 (defun lsdb-canonicalize-spaces-and-dots (string)
438 (while (string-match " +\\|[\f\t\n\r\v]+\\|\\." string)
439 (setq string (replace-match " " nil t string)))
442 (defun lsdb-extract-address-components (string)
443 (let ((components (std11-extract-address-components string)))
444 (if (nth 1 components)
446 (list (funcall lsdb-canonicalize-full-name-function
449 (list (nth 1 components) (nth 1 components))))))
451 ;; stolen (and renamed) from nnheader.el
452 (defun lsdb-decode-field-body (field-body field-name
453 &optional mode max-column)
454 (let ((multibyte enable-multibyte-characters))
457 (set-buffer-multibyte t)
458 (mime-decode-field-body field-body
459 (if (stringp field-name)
460 (intern (capitalize field-name))
463 (set-buffer-multibyte multibyte))))
465 ;;;_. Record Management
466 (defun lsdb-maybe-load-secondary-hash-tables ()
467 (let ((tables lsdb-secondary-hash-tables))
469 (unless (symbol-value (car tables))
470 (set (car tables) (lsdb-make-hash-table :test 'equal))
474 'lsdb-update-record-functions
477 (setq lsdb-hash-tables-are-dirty t))
478 (setq tables (cdr tables)))))
480 (defun lsdb-maybe-load-hash-tables ()
481 (unless lsdb-hash-table
482 (if (file-exists-p lsdb-file)
483 (lsdb-load-hash-tables)
484 (setq lsdb-hash-table (lsdb-make-hash-table :test 'equal)))
485 (lsdb-maybe-load-secondary-hash-tables)))
487 ;;;_ : Fallback Lookup Functions
488 ;;;_ , #1 Address Cache
489 (defun lsdb-lookup-full-name-from-address-cache (sender)
490 (lsdb-gethash (nth 1 sender) lsdb-address-cache))
492 (defun lsdb-update-address-cache (record)
493 (let ((net (cdr (assq 'net record))))
495 (lsdb-puthash (pop net) (car record) lsdb-address-cache))))
497 ;;;_ , #2 Iterate on the All Records (very slow)
498 (defun lsdb-lookup-full-name-by-fuzzy-matching (sender)
501 "\\`\\(.+\\)[ \t]+\\(/[ \t]+\\|(\\([^)]+\\))\\)"
503 (if (match-beginning 3)
504 (list (match-string 1 (car sender))
505 (match-string 3 (car sender)))
506 (list (match-string 1 (car sender))
507 (substring (car sender) (match-end 0))))
508 (list (car sender))))
509 (case-fold-search t))
514 (if (or (string-match
515 (concat "\\<" (regexp-quote (car names)) "\\>")
521 (mapconcat #'identity
522 (nreverse (split-string (car names)))
526 ;; Don't assume that we are using address cache.
527 (member (nth 1 sender) (cdr (assq 'net value))))
529 (setq names (cdr names))))
532 ;;;_ : Update Records
533 (defun lsdb-update-record (sender &optional interesting)
534 (let ((old (lsdb-gethash (car sender) lsdb-hash-table))
535 (new (cons (cons 'net (list (nth 1 sender)))
540 ;; Look for the existing record from the reverse hash table.
541 ;; If it is found, regsiter the current full-name as AKA.
544 (run-hook-with-args-until-success
545 'lsdb-lookup-full-name-functions
548 (setq old (lsdb-gethash full-name lsdb-hash-table)
549 new (cons (list 'aka (car sender)) new))
550 (setcar sender full-name)))
552 (setq new (cons (cons 'creation-date (format-time-string "%Y-%m-%d"))
554 (setq merged (lsdb-merge-record-entries old new)
555 record (cons (car sender) merged))
556 (unless (equal merged old)
557 (let ((entry (assq 'last-modified (cdr record)))
558 (last-modified (format-time-string "%Y-%m-%d")))
560 (setcdr entry last-modified)
561 (setcdr record (cons (cons 'last-modified last-modified)
563 (lsdb-puthash (car record) (cdr record)
565 (run-hook-with-args 'lsdb-update-record-functions record)
566 (setq lsdb-hash-tables-are-dirty t))
569 (defun lsdb-update-records ()
570 (lsdb-maybe-load-hash-tables)
571 (let (senders recipients interesting alist records bodies entry)
573 (std11-narrow-to-header)
575 (delq nil (mapcar #'lsdb-extract-address-components
576 (lsdb-fetch-field-bodies
577 lsdb-sender-headers)))
579 (delq nil (mapcar #'lsdb-extract-address-components
580 (lsdb-fetch-field-bodies
581 lsdb-recipients-headers))))
582 (setq alist lsdb-interesting-header-alist)
587 (if (nth 1 (car alist))
588 (and (string-match (nth 1 (car alist))
590 (replace-match (nth 3 (car alist))
593 (lsdb-fetch-field-bodies (car (car alist))))))
595 (setq entry (or (nth 2 (car alist))
598 (if (eq ?. (nth 2 (assq entry lsdb-entry-type-alist)))
602 (setq alist (cdr alist))))
604 (setq records (list (lsdb-update-record (pop senders) interesting))))
605 (setq alist (nconc senders recipients))
607 (setq records (cons (lsdb-update-record (pop alist)) records)))
610 (defun lsdb-merge-record-entries (old new)
611 (setq old (copy-sequence old))
613 (let ((entry (assq (car (car new)) old))
616 (setq old (nconc old (list (car new))))
617 (if (listp (cdr entry))
619 (setq list (cdr (car new)) pointer list)
621 (if (member (car pointer) (cdr entry))
622 (setq list (delq (car pointer) list)))
623 (setq pointer (cdr pointer)))
624 (setcdr entry (nconc (cdr entry) list)))
625 (setcdr entry (cdr (car new))))))
626 (setq new (cdr new)))
629 ;;;_. Display Management
630 (defun lsdb-temp-buffer-show-function (buffer)
631 (save-selected-window
632 (let ((window (or (get-buffer-window lsdb-buffer-name)
634 (select-window (get-largest-window))
635 (split-window-vertically))))
637 (set-window-buffer window buffer)
638 (select-window window)
639 (unless (pos-visible-in-window-p (point-max))
640 (enlarge-window (- lsdb-window-max-height (window-height))))
641 (shrink-window-if-larger-than-buffer)
642 (if (> (setq height (window-height))
643 lsdb-window-max-height)
644 (shrink-window (- height lsdb-window-max-height)))
645 (set-window-start window (point-min)))))
647 (defun lsdb-display-record (record)
648 "Display only one RECORD, then shrink the window as possible."
649 (let ((temp-buffer-show-function
650 (function lsdb-temp-buffer-show-function)))
651 (lsdb-display-records (list record))))
653 (defun lsdb-display-records (records)
654 (with-output-to-temp-buffer lsdb-buffer-name
655 (set-buffer standard-output)
657 (sort (copy-sequence records)
658 (or lsdb-display-records-sort-predicate
659 (lambda (record1 record2)
660 (string-lessp (car record1) (car record2))))))
663 (narrow-to-region (point) (point))
664 (lsdb-print-record (car records)))
665 (goto-char (point-max))
666 (setq records (cdr records)))
669 (defsubst lsdb-entry-score (entry)
670 (or (nth 1 (assq (car entry) lsdb-entry-type-alist)) 0))
672 (defun lsdb-insert-entry (entry)
673 (let ((entry-name (capitalize (symbol-name (car entry)))))
674 (intern entry-name lsdb-known-entry-names)
675 (if (>= (lsdb-entry-score entry) 0)
676 (insert "\t" entry-name ": "
677 (if (listp (cdr entry))
679 #'identity (cdr entry)
680 (if (eq ?, (nth 2 (assq (car entry)
681 lsdb-entry-type-alist)))
687 (defun lsdb-print-record (record)
688 (insert (car record) "\n")
690 (sort (copy-sequence (cdr record))
691 (lambda (entry1 entry2)
692 (> (lsdb-entry-score entry1) (lsdb-entry-score entry2))))))
694 (lsdb-insert-entry (car entries))
695 (setq entries (cdr entries))))
696 (add-text-properties (point-min) (point-max)
697 (list 'lsdb-record record))
698 (run-hooks 'lsdb-print-record-hook))
701 (defvar lsdb-last-completion nil)
702 (defvar lsdb-last-candidates nil)
703 (defvar lsdb-last-candidates-pointer nil)
705 ;;;_ : Matching Highlight
706 (defvar lsdb-last-highlight-overlay nil)
708 (defun lsdb-complete-name-highlight (start end)
709 (make-local-hook 'pre-command-hook)
710 (add-hook 'pre-command-hook 'lsdb-complete-name-highlight-update nil t)
713 (search-forward lsdb-last-completion end)
714 (setq lsdb-last-highlight-overlay
715 (make-overlay (match-beginning 0) (match-end 0)))
716 (overlay-put lsdb-last-highlight-overlay 'face
717 (or (find-face 'isearch-secondary)
720 (defun lsdb-complete-name-highlight-update ()
721 (unless (eq 'this-command 'lsdb-complete-name)
722 (if lsdb-last-highlight-overlay
723 (delete-overlay lsdb-last-highlight-overlay))
724 (remove-hook 'pre-command-hook
725 'lsdb-complete-name-highlight-update t)))
727 ;;;_ : Name Completion
728 (defun lsdb-complete-name ()
729 "Complete the user full-name or net-address before point"
731 (lsdb-maybe-load-hash-tables)
734 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
735 (goto-char (match-end 0))
739 (completion-ignore-case t))
740 (unless (eq last-command this-command)
741 (setq lsdb-last-candidates nil
742 lsdb-last-candidates-pointer nil
743 lsdb-last-completion (buffer-substring start (point))
744 pattern (concat "\\<" (regexp-quote lsdb-last-completion)))
747 (let ((net (cdr (assq 'net value))))
748 (if (string-match pattern key)
749 (setq lsdb-last-candidates
750 (nconc lsdb-last-candidates
751 (mapcar (lambda (address)
752 (if (equal key address)
754 (concat key " <" address ">")))
757 (if (string-match pattern (car net))
758 (push (car net) lsdb-last-candidates))
759 (setq net (cdr net))))))
761 ;; Sort candidates by the position where the pattern occurred.
762 (setq lsdb-last-candidates
763 (sort lsdb-last-candidates
764 (lambda (cand1 cand2)
765 (< (if (string-match pattern cand1)
767 (if (string-match pattern cand2)
768 (match-beginning 0)))))))
769 (unless lsdb-last-candidates-pointer
770 (setq lsdb-last-candidates-pointer lsdb-last-candidates))
771 (when lsdb-last-candidates-pointer
772 (delete-region start (point))
773 (insert (pop lsdb-last-candidates-pointer))
774 (lsdb-complete-name-highlight start (point)))))
776 ;;;_. Major Mode (`lsdb-mode') Implementation
777 ;;;_ : Modeline Buffer Identification
778 (defconst lsdb-pointer-xpm
780 static char * lsdb_pointer_xpm[] = {
802 (static-if (featurep 'xemacs)
804 (defvar lsdb-xemacs-modeline-left-extent
805 (copy-extent modeline-buffer-id-left-extent))
807 (defvar lsdb-xemacs-modeline-right-extent
808 (copy-extent modeline-buffer-id-right-extent))
810 (defun lsdb-modeline-buffer-identification (line)
811 "Decorate 1st element of `mode-line-buffer-identification' LINE.
812 Modify whole identification by side effect."
813 (let ((id (car line)) chopped)
814 (if (and (stringp id) (string-match "^LSDB:" id))
816 (setq chopped (substring id 0 (match-end 0))
817 id (substring id (match-end 0)))
824 (list (vector 'xpm :data lsdb-pointer-xpm)))
825 (list (vector 'string :data chopped))))))
826 (set-glyph-face glyph 'modeline-buffer-id)
827 (cons lsdb-xemacs-modeline-left-extent glyph))
828 (cons lsdb-xemacs-modeline-right-extent id))
834 (defun lsdb-modeline-buffer-identification (line)
835 "Decorate 1st element of `mode-line-buffer-identification' LINE.
836 Modify whole identification by side effect."
837 (let ((id (copy-sequence (car line)))
839 (if (image-type-available-p 'xpm)
840 (create-image lsdb-pointer-xpm 'xpm t :ascent 'center))))
842 (stringp id) (string-match "^LSDB:" id))
843 (add-text-properties 0 (length id)
845 'rear-nonsticky (list 'display))
850 (defalias 'lsdb-modeline-buffer-identification 'identity))))
852 (defvar lsdb-mode-map
853 (let ((keymap (make-sparse-keymap)))
854 (define-key keymap "a" 'lsdb-mode-add-entry)
855 (define-key keymap "d" 'lsdb-mode-delete-entry)
856 (define-key keymap "e" 'lsdb-mode-edit-entry)
857 (define-key keymap "s" 'lsdb-mode-save)
858 (define-key keymap "q" 'lsdb-mode-quit-window)
859 (define-key keymap "g" 'lsdb-mode-lookup)
860 (define-key keymap "p" 'lsdb-mode-previous-record)
861 (define-key keymap "n" 'lsdb-mode-next-record)
862 (define-key keymap " " 'scroll-up)
863 (define-key keymap [delete] 'scroll-down)
864 (define-key keymap "\177" 'scroll-down)
865 (define-key keymap [backspace] 'scroll-down)
869 (defvar lsdb-modeline-string "")
871 (define-derived-mode lsdb-mode fundamental-mode "LSDB"
872 "Major mode for browsing LSDB records."
873 (setq buffer-read-only t)
874 (static-if (featurep 'xemacs)
875 ;; In XEmacs, setting `font-lock-defaults' only affects on
876 ;; `find-file-hooks'.
877 (font-lock-set-defaults)
878 (set (make-local-variable 'font-lock-defaults)
879 '(lsdb-font-lock-keywords t)))
880 (make-local-hook 'post-command-hook)
881 (add-hook 'post-command-hook 'lsdb-modeline-update nil t)
882 (make-local-variable 'lsdb-modeline-string)
883 (setq mode-line-buffer-identification
884 (lsdb-modeline-buffer-identification
885 '("LSDB: " lsdb-modeline-string)))
886 (lsdb-modeline-update)
887 (force-mode-line-update))
889 (defun lsdb-modeline-update ()
891 (get-text-property (if (eobp) (point-min) (point)) 'lsdb-record))
895 (setq net (car (cdr (assq 'net (cdr record)))))
896 (if (equal net (car record))
897 (setq lsdb-modeline-string net)
898 (setq lsdb-modeline-string (concat (car record) " <" net ">"))))
899 (setq lsdb-modeline-string ""))))
901 (defun lsdb-narrow-to-record ()
902 "Narrow to the current record."
903 (let ((end (next-single-property-change (point) 'lsdb-record nil
906 (previous-single-property-change (point) 'lsdb-record nil (point-min))
908 (goto-char (point-min))))
910 (defun lsdb-current-record ()
911 "Return the current record name."
912 (let ((record (get-text-property (point) 'lsdb-record)))
914 (error "There is nothing to follow here"))
917 (defun lsdb-current-entry ()
918 "Return the current entry name.
919 If the point is not on a entry line, it prompts to select a entry in
923 (if (looking-at "^[^\t]")
924 (let ((record (lsdb-current-record))
925 (completion-ignore-case t))
927 "Which entry to modify: "
928 (mapcar (lambda (entry)
929 (list (capitalize (symbol-name (car entry)))))
932 (re-search-backward "^\t\\([^\t][^:]+\\):")
935 (defun lsdb-mode-add-entry (entry-name)
936 "Add an entry on the current line."
938 (let ((completion-ignore-case t))
939 (list (completing-read "Entry name: " lsdb-known-entry-names))))
941 (unless (symbolp entry-name)
942 (setq entry-name (intern (downcase entry-name))))
943 (when (assq entry-name (cdr (lsdb-current-record)))
944 (error "The entry already exists"))
945 (let ((marker (point-marker)))
947 nil "Editing the entry."
951 (set-buffer lsdb-buffer-name)
953 (let ((record (lsdb-current-record))
954 (inhibit-read-only t)
956 (setcdr record (cons (cons ',entry-name form) (cdr record)))
957 (lsdb-puthash (car record) (cdr record)
959 (run-hook-with-args 'lsdb-update-record-functions record)
960 (setq lsdb-hash-tables-are-dirty t)
961 (beginning-of-line 2)
965 (lsdb-insert-entry (cons ',entry-name form))
967 (list 'lsdb-record record)))))))))
969 (defun lsdb-mode-delete-entry (&optional entry-name dont-update)
970 "Delete the entry on the current line."
972 (let ((record (lsdb-current-record))
975 (setq entry-name (lsdb-current-entry)))
976 (setq entry (assq (intern (downcase entry-name)) (cdr record)))
979 (setcdr record (delq entry (cdr record)))
980 (lsdb-puthash (car record) (cdr record)
982 (run-hook-with-args 'lsdb-update-record-functions record)
983 (setq lsdb-hash-tables-are-dirty t))
985 (lsdb-narrow-to-record)
986 (let ((case-fold-search t)
987 (inhibit-read-only t)
989 (goto-char (point-min))
990 (if (re-search-forward
991 (concat "^\t" (or entry-name
992 (lsdb-current-entry))
995 (delete-region (match-beginning 0)
996 (if (re-search-forward
997 "^\t[^\t][^:]+:" nil t)
1001 (defun lsdb-mode-edit-entry ()
1002 "Edit the entry on the current line."
1004 (let* ((record (lsdb-current-record))
1005 (entry-name (intern (downcase (lsdb-current-entry))))
1006 (entry (assq entry-name (cdr record)))
1007 (marker (point-marker)))
1009 (cdr entry) "Editing the entry."
1011 (unless (equal form ',(cdr entry))
1013 (set-buffer lsdb-buffer-name)
1015 (let* ((record (lsdb-current-record))
1016 (entry (assq ',entry-name (cdr record)))
1017 (inhibit-read-only t)
1020 (run-hook-with-args 'lsdb-update-record-functions record)
1021 (setq lsdb-hash-tables-are-dirty t)
1022 (lsdb-mode-delete-entry (symbol-name ',entry-name) t)
1024 (add-text-properties
1027 (lsdb-insert-entry (cons ',entry-name form))
1029 (list 'lsdb-record record)))))))))
1031 (defun lsdb-mode-save (&optional dont-ask)
1032 "Save LSDB hash table into `lsdb-file'."
1034 (if (not lsdb-hash-tables-are-dirty)
1035 (message "(No changes need to be saved)")
1036 (when (or (interactive-p)
1038 (y-or-n-p "Save the LSDB now?"))
1039 (lsdb-save-hash-tables)
1040 (setq lsdb-hash-tables-are-dirty nil)
1041 (message "The LSDB was saved successfully."))))
1043 (defun lsdb-mode-quit-window (&optional kill window)
1044 "Quit the current buffer.
1045 It partially emulates the GNU Emacs' of `quit-window'."
1048 (setq window (selected-window)))
1049 (let ((buffer (window-buffer window)))
1050 (unless (save-selected-window
1051 (select-window window)
1053 (delete-window window))
1055 (kill-buffer buffer)
1056 (bury-buffer buffer))))
1058 (defun lsdb-mode-hide-buffer ()
1059 "Hide the LSDB window."
1060 (let ((window (get-buffer-window lsdb-buffer-name)))
1062 (lsdb-mode-quit-window nil window))))
1064 (defun lsdb-lookup-records (regexp &optional entry-name)
1065 "Return the all records in the LSDB matching the REGEXP.
1066 If the optional 2nd argument ENTRY-NAME is given, matching only
1067 performed against the entry field."
1072 (unless (symbolp entry-name)
1073 (setq entry-name (intern (downcase entry-name))))
1075 (let ((entry (cdr (assq entry-name value)))
1077 (unless (listp entry)
1078 (setq entry (list entry)))
1079 (while (and (not found) entry)
1080 (if (string-match regexp (pop entry))
1083 (push (cons key value) records)))))
1085 (if (string-match regexp key)
1086 (push (cons key value) records))))
1090 (defvar lsdb-mode-lookup-history nil)
1092 (defun lsdb-mode-lookup (regexp &optional entry-name)
1093 "Display the all records in the LSDB matching the REGEXP.
1094 If the optional 2nd argument ENTRY-NAME is given, matching only
1095 performed against the entry field."
1097 (let* ((completion-ignore-case t)
1099 (if current-prefix-arg
1100 (completing-read "Entry name: "
1101 lsdb-known-entry-names))))
1103 (read-from-minibuffer
1105 (format "Search records `%s' regexp: " entry-name)
1106 "Search records regexp: ")
1107 nil nil nil 'lsdb-mode-lookup-history)
1109 (lsdb-maybe-load-hash-tables)
1110 (let ((records (lsdb-lookup-records regexp entry-name)))
1112 (lsdb-display-records records))))
1115 (defalias 'lsdb 'lsdb-mode-lookup)
1117 (defun lsdb-mode-next-record (&optional arg)
1118 "Go to the next record."
1120 (unless arg ;called noninteractively?
1123 (lsdb-mode-previous-record (- arg))
1125 (goto-char (next-single-property-change
1126 (point) 'lsdb-record nil (point-max)))
1127 (setq arg (1- arg)))))
1129 (defun lsdb-mode-previous-record (&optional arg)
1130 "Go to the previous record."
1132 (unless arg ;called noninteractively?
1135 (lsdb-mode-next-record (- arg))
1137 (goto-char (previous-single-property-change
1138 (point) 'lsdb-record nil (point-min)))
1139 (setq arg (1- arg)))))
1141 ;;;_ : Edit Forms -- stolen (and renamed) from gnus-eform.el
1142 (defvar lsdb-edit-form-buffer "*LSDB edit form*")
1143 (defvar lsdb-edit-form-done-function nil)
1144 (defvar lsdb-previous-window-configuration nil)
1146 (defvar lsdb-edit-form-mode-map
1147 (let ((keymap (make-sparse-keymap)))
1148 (set-keymap-parent keymap emacs-lisp-mode-map)
1149 (define-key keymap "\C-c\C-c" 'lsdb-edit-form-done)
1150 (define-key keymap "\C-c\C-k" 'lsdb-edit-form-exit)
1152 "Edit form's keymap.")
1154 (defun lsdb-edit-form-mode ()
1155 "Major mode for editing forms.
1156 It is a slightly enhanced emacs-lisp-mode.
1158 \\{lsdb-edit-form-mode-map}"
1160 (kill-all-local-variables)
1161 (setq major-mode 'lsdb-edit-form-mode
1162 mode-name "LSDB Edit Form")
1163 (use-local-map lsdb-edit-form-mode-map)
1164 (make-local-variable 'lsdb-edit-form-done-function)
1165 (make-local-variable 'lsdb-previous-window-configuration)
1166 (run-hooks 'lsdb-edit-form-mode-hook))
1168 (defun lsdb-edit-form (form documentation exit-func)
1169 "Edit FORM in a new buffer.
1170 Call EXIT-FUNC on exit. Display DOCUMENTATION in the beginning
1172 (let ((window-configuration
1173 (current-window-configuration)))
1174 (switch-to-buffer (get-buffer-create lsdb-edit-form-buffer))
1175 (lsdb-edit-form-mode)
1176 (setq lsdb-previous-window-configuration window-configuration
1177 lsdb-edit-form-done-function exit-func)
1179 (insert documentation)
1182 (goto-char (point-min))
1186 (insert ";; Type `C-c C-c' after you've finished editing.\n")
1189 (pp form (current-buffer))
1193 (defun lsdb-edit-form-done ()
1194 "Update changes and kill the current buffer."
1196 (goto-char (point-min))
1197 (let ((form (condition-case nil
1198 (read (current-buffer))
1200 (func lsdb-edit-form-done-function))
1201 (lsdb-edit-form-exit)
1202 (funcall func form)))
1204 (defun lsdb-edit-form-exit ()
1205 "Kill the current buffer."
1207 (let ((window-configuration lsdb-previous-window-configuration))
1208 (kill-buffer (current-buffer))
1209 (set-window-configuration window-configuration)))
1211 ;;;_. Interface to Semi-gnus
1213 (defun lsdb-gnus-insinuate ()
1214 "Call this function to hook LSDB into Semi-gnus."
1215 (add-hook 'gnus-article-prepare-hook 'lsdb-gnus-update-record)
1216 (add-hook 'gnus-save-newsrc-hook 'lsdb-mode-save))
1218 (defvar gnus-current-headers)
1219 (defun lsdb-gnus-update-record ()
1220 (let ((entity gnus-current-headers)
1223 (set-buffer-multibyte nil)
1224 (buffer-disable-undo)
1225 (mime-insert-entity entity)
1226 (setq records (lsdb-update-records))
1228 (lsdb-display-record (car records))))))
1230 ;;;_. Interface to Wanderlust
1232 (defun lsdb-wl-insinuate ()
1233 "Call this function to hook LSDB into Wanderlust."
1234 (add-hook 'wl-message-redisplay-hook 'lsdb-wl-update-record)
1235 (add-hook 'wl-summary-exit-hook 'lsdb-mode-hide-buffer)
1236 (add-hook 'wl-exit-hook 'lsdb-mode-save)
1237 (add-hook 'wl-save-hook 'lsdb-mode-save))
1240 (autoload 'wl-message-get-original-buffer "wl-message"))
1241 (defun lsdb-wl-update-record ()
1243 (set-buffer (wl-message-get-original-buffer))
1244 (let ((records (lsdb-update-records)))
1246 (lsdb-display-record (car records))))))
1248 ;;;_. Interface to Mew written by Hideyuki SHIRAI <shirai@rdmg.mgcs.mei.co.jp>
1250 (autoload 'mew-sinfo-get-disp-msg "mew")
1251 (autoload 'mew-current-get-fld "mew")
1252 (autoload 'mew-current-get-msg "mew")
1253 (autoload 'mew-frame-id "mew")
1254 (autoload 'mew-cache-hit "mew"))
1257 (defun lsdb-mew-insinuate ()
1258 "Call this function to hook LSDB into Mew."
1259 (add-hook 'mew-message-hook 'lsdb-mew-update-record)
1260 (add-hook 'mew-summary-toggle-disp-msg-hook
1262 (unless (mew-sinfo-get-disp-msg)
1263 (lsdb-mode-hide-buffer))))
1264 (add-hook 'mew-suspend-hook 'lsdb-mode-hide-buffer)
1265 (add-hook 'mew-quit-hook 'lsdb-mode-save)
1266 (add-hook 'kill-emacs-hook 'lsdb-mode-save))
1268 (defun lsdb-mew-update-record ()
1269 (let* ((fld (mew-current-get-fld (mew-frame-id)))
1270 (msg (mew-current-get-msg (mew-frame-id)))
1271 (cache (mew-cache-hit fld msg 'must-hit))
1275 (make-local-variable 'lsdb-decode-field-body-function)
1276 (setq lsdb-decode-field-body-function
1278 (set-text-properties 0 (length body) nil body)
1280 (when (setq records (lsdb-update-records))
1281 (lsdb-display-record (car records))))))
1283 ;;;_. Interface to MU-CITE
1285 (autoload 'mu-cite-get-value "mu-cite"))
1287 (defun lsdb-mu-attribution (address)
1288 "Extract attribute information from LSDB."
1290 (lsdb-lookup-records (concat "\\<" address "\\>") 'net)))
1292 (cdr (assq 'attribution (cdr (car records)))))))
1294 (defun lsdb-mu-set-attribution (attribution address)
1295 "Add attribute information to LSDB."
1297 (lsdb-lookup-records (concat "\\<" address "\\>") 'net))
1300 (setq entry (assq 'attribution (cdr (car records))))
1302 (setcdr entry attribution)
1303 (setcdr (car records) (cons (cons 'attribution attribution)
1304 (cdr (car records))))
1305 (lsdb-puthash (car (car records)) (cdr (car records))
1307 (run-hook-with-args 'lsdb-update-record-functions (car records))
1308 (setq lsdb-hash-tables-are-dirty t)))))
1310 (defun lsdb-mu-get-prefix-method ()
1311 "A mu-cite method to return a prefix from LSDB or \">\".
1312 If an `attribution' value is found in LSDB, the value is returned.
1313 Otherwise \">\" is returned."
1314 (or (lsdb-mu-attribution (mu-cite-get-value 'address))
1317 (defvar minibuffer-allow-text-properties)
1319 (defvar lsdb-mu-history nil)
1321 (defun lsdb-mu-get-prefix-register-method ()
1322 "A mu-cite method to return a prefix from LSDB or register it.
1323 If an `attribution' value is found in LSDB, the value is returned.
1324 Otherwise the function requests a prefix from a user. The prefix will
1325 be registered to LSDB if the user wants it."
1326 (let ((address (mu-cite-get-value 'address)))
1327 (or (lsdb-mu-attribution address)
1328 (let* (minibuffer-allow-text-properties
1329 (result (read-string "Citation name? "
1330 (or (mu-cite-get-value 'x-attribution)
1331 (mu-cite-get-value 'full-name))
1333 (if (and (not (string-equal result ""))
1334 (y-or-n-p (format "Register \"%s\"? " result)))
1335 (lsdb-mu-set-attribution result address))
1338 (defun lsdb-mu-get-prefix-register-verbose-method ()
1339 "A mu-cite method to return a prefix using LSDB.
1341 In this method, a user must specify a prefix unconditionally. If an
1342 `attribution' value is found in LSDB, the value is used as a initial
1343 value to input the prefix. The prefix will be registered to LSDB if
1345 (let* ((address (mu-cite-get-value 'address))
1346 (attribution (lsdb-mu-attribution address))
1347 minibuffer-allow-text-properties
1348 (result (read-string "Citation name? "
1350 (mu-cite-get-value 'x-attribution)
1351 (mu-cite-get-value 'full-name))
1353 (if (and (not (string-equal result ""))
1354 (not (string-equal result attribution))
1355 (y-or-n-p (format "Register \"%s\"? " result)))
1356 (lsdb-mu-set-attribution result address))
1359 (defvar mu-cite-methods-alist)
1361 (defun lsdb-mu-insinuate ()
1362 (add-hook 'mu-cite-instantiation-hook
1364 (setq mu-cite-methods-alist
1366 mu-cite-methods-alist
1369 #'lsdb-mu-get-prefix-method)
1370 (cons 'lsdb-prefix-register
1371 #'lsdb-mu-get-prefix-register-method)
1372 (cons 'lsdb-prefix-register-verbose
1373 #'lsdb-mu-get-prefix-register-verbose-method)))))))
1375 ;;;_. X-Face Rendering
1376 (defvar lsdb-x-face-cache
1377 (lsdb-make-hash-table :test 'equal))
1379 (defun lsdb-x-face-available-image-type ()
1380 (static-if (featurep 'xemacs)
1383 (and (>= emacs-major-version 21)
1384 (fboundp 'image-type-available-p)
1385 (if (image-type-available-p 'pbm)
1387 (if (image-type-available-p 'xpm)
1390 (defun lsdb-expose-x-face ()
1391 (let* ((record (get-text-property (point-min) 'lsdb-record))
1392 (x-face (cdr (assq 'x-face (cdr record))))
1394 (when (and lsdb-insert-x-face-function
1396 (goto-char (point-min))
1398 (put-text-property 0 1 'invisible t delimiter) ;hide "\r"
1404 (funcall lsdb-insert-x-face-function (pop x-face)))
1406 'lsdb-record record))))
1408 (defun lsdb-insert-x-face-image (data type marker)
1409 (static-if (featurep 'xemacs)
1411 (set-buffer (marker-buffer marker))
1413 (let* ((inhibit-read-only t)
1415 (glyph (make-glyph (vector type :data data))))
1416 (set-extent-begin-glyph
1417 (make-extent (point) (point))
1420 (set-buffer (marker-buffer marker))
1422 (let* ((inhibit-read-only t)
1424 (image (create-image data type t :ascent 'center))
1425 (record (get-text-property (point) 'lsdb-record)))
1426 (put-text-property (point) (progn
1427 (insert-image image)
1429 'lsdb-record record)))))
1431 (defun lsdb-insert-x-face-asynchronously (x-face)
1432 (let* ((type (lsdb-x-face-available-image-type))
1433 (shell-file-name lsdb-shell-file-name)
1434 (shell-command-switch lsdb-shell-command-switch)
1435 (process-connection-type nil)
1436 (cached (cdr (assq type (lsdb-gethash x-face lsdb-x-face-cache))))
1437 (marker (point-marker))
1440 (lsdb-insert-x-face-image cached type marker)
1442 (start-process-shell-command
1443 "lsdb-x-face-command" (generate-new-buffer " *lsdb work*")
1445 (nth 1 (assq type lsdb-x-face-command-alist))
1446 "; } 2> /dev/null")))
1447 (process-send-string process (concat x-face "\n"))
1448 (process-send-eof process)
1449 (set-process-sentinel
1451 `(lambda (process string)
1453 (when (and (buffer-live-p (marker-buffer ,marker))
1454 (equal string "finished\n"))
1456 (with-current-buffer (process-buffer process)
1457 (set-buffer-multibyte nil)
1459 (lsdb-insert-x-face-image data ',type ,marker)
1460 (lsdb-puthash ,x-face (list (cons ',type data))
1461 lsdb-x-face-cache)))
1462 (kill-buffer (process-buffer process))))))))
1467 (product-provide 'lsdb
1468 (product-define "LSDB" nil '(0 2)))
1470 ;;;_* Local emacs vars.
1471 ;;; The following `outline-layout' local variable setting:
1472 ;;; - closes all topics from the first topic to just before the third-to-last,
1473 ;;; - shows the children of the third to last (config vars)
1474 ;;; - and the second to last (code section),
1475 ;;; - and closes the last topic (this local-variables section).
1477 ;;;outline-layout: (0 : -1 -1 0)
1480 ;;; lsdb.el ends here