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-window-max-height 7
137 "Maximum number of lines used to display LSDB record."
141 (defcustom lsdb-x-face-command-alist
142 '((pbm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale 0.5")
143 (xpm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale 0.5 | ppmtoxpm"))
144 "An alist from an image type to a command to be executed to display an X-Face header.
145 The command will be executed in a sub-shell asynchronously.
146 The compressed face will be piped to this command."
150 (defcustom lsdb-insert-x-face-function
151 (if (static-if (featurep 'xemacs)
153 (and (>= emacs-major-version 21)
154 (fboundp 'image-type-available-p)
155 (or (image-type-available-p 'pbm)
156 (image-type-available-p 'xpm))))
157 #'lsdb-insert-x-face-asynchronously)
158 "Function to display X-Face."
162 (defcustom lsdb-print-record-hook '(lsdb-expose-x-face)
163 "A hook called after a record is displayed."
167 (defcustom lsdb-display-records-sort-predicate nil
168 "A predicate to sort records."
172 (defgroup lsdb-edit-form nil
173 "A mode for editing forms."
176 (defcustom lsdb-edit-form-mode-hook nil
177 "Hook run in `lsdb-edit-form-mode' buffers."
178 :group 'lsdb-edit-form
181 (defcustom lsdb-shell-file-name "/bin/sh"
182 "File name to load inferior shells from.
183 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
187 (defcustom lsdb-shell-command-switch "-c"
188 "Switch used to have the shell execute its command line argument."
193 (defface lsdb-header-face
194 '((t (:underline t)))
195 "Face for the file header line in `lsdb-mode'."
197 (defvar lsdb-header-face 'lsdb-header-face)
199 (defface lsdb-field-name-face
200 '((((class color) (background dark))
201 (:foreground "PaleTurquoise" :bold t))
203 "Face for the message header line in `lsdb-mode'."
205 (defvar lsdb-field-name-face 'lsdb-field-name-face)
207 (defface lsdb-field-body-face
208 '((((class color) (background dark))
209 (:foreground "turquoise" :italic t))
211 "Face for the message header line in `lsdb-mode'."
213 (defvar lsdb-field-body-face 'lsdb-field-body-face)
215 (defconst lsdb-font-lock-keywords
217 (0 lsdb-header-face))
219 (0 lsdb-field-body-face))
220 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
221 (1 lsdb-field-name-face)
222 (2 lsdb-field-body-face))))
224 (put 'lsdb-mode 'font-lock-defaults '(lsdb-font-lock-keywords t))
226 ;;;_* CODE - no user customizations below
227 ;;;_. Internal Variables
228 (defvar lsdb-hash-table nil
229 "Internal hash table to hold LSDB records.")
231 (defvar lsdb-reverse-hash-table nil
232 "The reverse lookup table for `lsdb-hash-table'.
233 It represents address to full-name mapping.")
235 (defvar lsdb-buffer-name "*LSDB*"
236 "Buffer name to display LSDB record.")
238 (defvar lsdb-hash-tables-are-dirty nil
239 "Flag to indicate whether the internal hash tables need to be saved.")
241 (defvar lsdb-known-entry-names
243 "An obarray used to complete an entry name.")
245 ;;;_. Hash Table Emulation
246 (if (and (fboundp 'make-hash-table)
247 (subrp (symbol-function 'make-hash-table)))
249 (defalias 'lsdb-puthash 'puthash)
250 (defalias 'lsdb-gethash 'gethash)
251 (defalias 'lsdb-remhash 'remhash)
252 (defalias 'lsdb-maphash 'maphash)
253 (defalias 'lsdb-hash-table-size 'hash-table-size)
254 (defalias 'lsdb-hash-table-count 'hash-table-count)
255 (defalias 'lsdb-make-hash-table 'make-hash-table))
256 (defun lsdb-puthash (key value hash-table)
257 "Hash KEY to VALUE in HASH-TABLE."
258 ;; Obarray is regarded as an open hash table, as a matter of
259 ;; fact, rehashing doesn't make sense.
261 (when (> (car hash-table)
262 (* (length (nth 1 hash-table)) 0.7))
263 (setq new-obarray (make-vector (* (length (nth 1 hash-table)) 2) 0))
266 (set (intern (symbol-name symbol) new-obarray)
267 (symbol-value symbol)))
269 (setcdr hash-table (list new-obarray)))
270 (set (intern key (nth 1 hash-table)) value)
271 (setcar hash-table (1+ (car hash-table)))))
272 (defun lsdb-gethash (key hash-table &optional default)
273 "Find hash value for KEY in HASH-TABLE.
274 If there is no corresponding value, return DEFAULT (which defaults to nil)."
275 (let ((symbol (intern-soft key (nth 1 hash-table))))
277 (symbol-value symbol)
279 (defun lsdb-remhash (key hash-table)
280 "Remove the entry for KEY from HASH-TABLE.
281 Do nothing if there is no entry for KEY in HASH-TABLE."
282 (unintern key (nth 1 hash-table))
283 (setcar hash-table (1- (car hash-table))))
284 (defun lsdb-maphash (function hash-table)
285 "Map FUNCTION over entries in HASH-TABLE, calling it with two args,
286 each key and value in HASH-TABLE.
288 FUNCTION may not modify HASH-TABLE, with the one exception that FUNCTION
289 may remhash or puthash the entry currently being processed by FUNCTION."
292 (funcall function (symbol-name symbol) (symbol-value symbol)))
294 (defun lsdb-hash-table-size (hash-table)
295 "Return the size of HASH-TABLE.
296 This is the current number of slots in HASH-TABLE, whether occupied or not."
297 (length (nth 1 hash-table)))
298 (defalias 'lsdb-hash-table-count 'car)
299 (defun lsdb-make-hash-table (&rest args)
300 "Return a new empty hash table object."
301 (list 0 (make-vector (or (plist-get args :size) 29) 0))))
303 ;;;_. Hash Table Reader/Writer
304 (defconst lsdb-secondary-hash-table-start-format
307 (defmacro lsdb-secondary-hash-table-start (hash-table)
308 `(format lsdb-secondary-hash-table-start-format ',hash-table))
313 ;; In XEmacs, hash tables can also be created by the lisp reader
314 ;; using structure syntax.
315 (read-from-string "#s(hash-table)")
316 (defalias 'lsdb-read 'read))
318 (defun lsdb-read (&optional marker)
319 "Read one Lisp expression as text from MARKER, return as Lisp object."
322 (if (looking-at "^#s(")
324 (buffer-disable-undo)
325 (insert-buffer-substring (marker-buffer marker) marker)
326 (goto-char (point-min))
328 (let ((object (read (current-buffer)))
330 (if (eq 'hash-table (car object))
333 (lsdb-make-hash-table
334 :size (plist-get (cdr object) 'size)
336 data (plist-get (cdr object) 'data))
338 (lsdb-puthash (pop data) (pop data) hash-table))
342 (defun lsdb-load-hash-tables ()
343 "Read the contents of `lsdb-file' into the internal hash tables."
344 (let ((buffer (find-file-noselect lsdb-file)))
348 (goto-char (point-min))
349 (re-search-forward "^#s(")
350 (goto-char (match-beginning 0))
351 (setq lsdb-hash-table (lsdb-read (point-marker)))
352 (if (re-search-forward
353 (concat "^" (lsdb-secondary-hash-table-start
354 lsdb-reverse-hash-table))
356 (setq lsdb-reverse-hash-table (lsdb-read (point-marker)))))
357 (kill-buffer buffer))))
359 (defun lsdb-insert-hash-table (hash-table)
360 (insert "#s(hash-table size "
361 ;; Reduce the actual size of the close hash table, because
362 ;; XEmacs doesn't have a distinction between index-size and
364 (number-to-string (lsdb-hash-table-count hash-table))
365 " test equal data (")
368 (insert (prin1-to-string key) " " (prin1-to-string value) " "))
372 (defun lsdb-save-hash-tables ()
373 "Write the records within the internal hash tables into `lsdb-file'."
374 (let ((coding-system-for-write lsdb-file-coding-system))
375 (with-temp-file lsdb-file
376 (if (and (or (featurep 'mule)
377 (featurep 'file-coding))
378 lsdb-file-coding-system)
379 (let ((coding-system-name
380 (if (symbolp lsdb-file-coding-system)
381 (symbol-name lsdb-file-coding-system)
383 (static-if (featurep 'xemacs)
384 (symbol-name (coding-system-name
385 lsdb-file-coding-system))))))
386 (if coding-system-name
387 (insert ";;; -*- coding: " coding-system-name " -*-\n"))))
388 (lsdb-insert-hash-table lsdb-hash-table)
389 (insert "\n" (lsdb-secondary-hash-table-start
390 lsdb-reverse-hash-table))
391 (lsdb-insert-hash-table lsdb-reverse-hash-table))))
393 ;;;_. Mail Header Extraction
394 (defun lsdb-fetch-field-bodies (regexp)
396 (goto-char (point-min))
397 (let ((case-fold-search t)
399 (while (re-search-forward (concat "^\\(" regexp "\\):[ \t]*")
401 (push (funcall lsdb-decode-field-body-function
402 (buffer-substring (point) (std11-field-end))
405 (nreverse field-bodies))))
407 (defun lsdb-canonicalize-spaces-and-dots (string)
408 (while (string-match " +\\|[\f\t\n\r\v]+\\|\\." string)
409 (setq string (replace-match " " nil t string)))
412 (defun lsdb-extract-address-components (string)
413 (let ((components (std11-extract-address-components string)))
414 (if (nth 1 components)
416 (list (funcall lsdb-canonicalize-full-name-function
419 (list (nth 1 components) (nth 1 components))))))
421 ;; stolen (and renamed) from nnheader.el
422 (defun lsdb-decode-field-body (field-body field-name
423 &optional mode max-column)
424 (let ((multibyte enable-multibyte-characters))
427 (set-buffer-multibyte t)
428 (mime-decode-field-body field-body
429 (if (stringp field-name)
430 (intern (capitalize field-name))
433 (set-buffer-multibyte multibyte))))
435 ;;;_. Record Management
436 (defun lsdb-maybe-build-reverse-hash-table ()
437 (unless lsdb-reverse-hash-table
438 (setq lsdb-reverse-hash-table (lsdb-make-hash-table :test 'equal))
441 (let ((net (cdr (assq 'net value))))
443 (lsdb-puthash (pop net) key lsdb-reverse-hash-table))))
445 (setq lsdb-hash-tables-are-dirty t))
447 (defun lsdb-maybe-load-hash-tables ()
448 (unless lsdb-hash-table
449 (if (file-exists-p lsdb-file)
450 (lsdb-load-hash-tables)
451 (setq lsdb-hash-table (lsdb-make-hash-table :test 'equal)))
452 (lsdb-maybe-build-reverse-hash-table)))
454 (defun lsdb-update-record (sender &optional interesting)
455 (let ((old (lsdb-gethash (car sender) lsdb-hash-table))
456 (new (cons (cons 'net (list (nth 1 sender)))
461 ;; Look for the existing record from the reverse hash table.
462 ;; If it is found, regsiter the current full-name as AKA.
464 (setq full-name (lsdb-gethash (nth 1 sender) lsdb-reverse-hash-table))
466 (setq old (lsdb-gethash full-name lsdb-hash-table)
467 new (cons (list 'aka (car sender)) new))
468 (setcar sender full-name)))
470 (setq new (cons (cons 'creation-date (format-time-string "%Y-%m-%d"))
472 (setq merged (lsdb-merge-record-entries old new)
473 record (cons (car sender) merged))
474 (unless (equal merged old)
475 (let ((entry (assq 'last-modified (cdr record)))
476 (last-modified (format-time-string "%Y-%m-%d")))
478 (setcdr entry last-modified)
479 (setcdr record (cons (cons 'last-modified last-modified)
481 (lsdb-puthash (car record) (cdr record)
483 (setq lsdb-hash-tables-are-dirty t))
484 (lsdb-puthash (nth 1 sender) (car sender) lsdb-reverse-hash-table)
487 (defun lsdb-update-records ()
488 (lsdb-maybe-load-hash-tables)
489 (let (senders recipients interesting alist records bodies entry)
491 (std11-narrow-to-header)
493 (delq nil (mapcar #'lsdb-extract-address-components
494 (lsdb-fetch-field-bodies
495 lsdb-sender-headers)))
497 (delq nil (mapcar #'lsdb-extract-address-components
498 (lsdb-fetch-field-bodies
499 lsdb-recipients-headers))))
500 (setq alist lsdb-interesting-header-alist)
505 (if (nth 1 (car alist))
506 (and (string-match (nth 1 (car alist))
508 (replace-match (nth 3 (car alist))
511 (lsdb-fetch-field-bodies (car (car alist))))))
513 (setq entry (or (nth 2 (car alist))
516 (if (eq ?. (nth 2 (assq entry lsdb-entry-type-alist)))
520 (setq alist (cdr alist))))
522 (setq records (list (lsdb-update-record (pop senders) interesting))))
523 (setq alist (nconc senders recipients))
525 (setq records (cons (lsdb-update-record (pop alist)) records)))
528 (defun lsdb-merge-record-entries (old new)
529 (setq old (copy-sequence old))
531 (let ((entry (assq (car (car new)) old))
534 (setq old (nconc old (list (car new))))
535 (if (listp (cdr entry))
537 (setq list (cdr (car new)) pointer list)
539 (if (member (car pointer) (cdr entry))
540 (setq list (delq (car pointer) list)))
541 (setq pointer (cdr pointer)))
542 (setcdr entry (nconc (cdr entry) list)))
543 (setcdr entry (cdr (car new))))))
544 (setq new (cdr new)))
547 ;;;_. Display Management
548 (defun lsdb-temp-buffer-show-function (buffer)
549 (save-selected-window
550 (let ((window (or (get-buffer-window lsdb-buffer-name)
552 (select-window (get-largest-window))
553 (split-window-vertically))))
555 (set-window-buffer window buffer)
556 (select-window window)
557 (unless (pos-visible-in-window-p (point-max))
558 (enlarge-window (- lsdb-window-max-height (window-height))))
559 (shrink-window-if-larger-than-buffer)
560 (if (> (setq height (window-height))
561 lsdb-window-max-height)
562 (shrink-window (- height lsdb-window-max-height)))
563 (set-window-start window (point-min)))))
565 (defun lsdb-display-record (record)
566 "Display only one RECORD, then shrink the window as possible."
567 (let ((temp-buffer-show-function
568 (function lsdb-temp-buffer-show-function)))
569 (lsdb-display-records (list record))))
571 (defun lsdb-display-records (records)
572 (with-output-to-temp-buffer lsdb-buffer-name
573 (set-buffer standard-output)
575 (sort (copy-sequence records)
576 (or lsdb-display-records-sort-predicate
577 (lambda (record1 record2)
578 (string-lessp (car record1) (car record2))))))
581 (narrow-to-region (point) (point))
582 (lsdb-print-record (car records)))
583 (goto-char (point-max))
584 (setq records (cdr records)))
587 (defsubst lsdb-entry-score (entry)
588 (or (nth 1 (assq (car entry) lsdb-entry-type-alist)) 0))
590 (defun lsdb-insert-entry (entry)
591 (let ((entry-name (capitalize (symbol-name (car entry)))))
592 (intern entry-name lsdb-known-entry-names)
593 (if (>= (lsdb-entry-score entry) 0)
594 (insert "\t" entry-name ": "
595 (if (listp (cdr entry))
597 #'identity (cdr entry)
598 (if (eq ?, (nth 2 (assq (car entry)
599 lsdb-entry-type-alist)))
605 (defun lsdb-print-record (record)
606 (insert (car record) "\n")
608 (sort (copy-sequence (cdr record))
609 (lambda (entry1 entry2)
610 (> (lsdb-entry-score entry1) (lsdb-entry-score entry2))))))
612 (lsdb-insert-entry (car entries))
613 (setq entries (cdr entries))))
614 (add-text-properties (point-min) (point-max)
615 (list 'lsdb-record record))
616 (run-hooks 'lsdb-print-record-hook))
619 (defvar lsdb-last-completion nil)
620 (defvar lsdb-last-candidates nil)
621 (defvar lsdb-last-candidates-pointer nil)
623 (defun lsdb-complete-name ()
624 "Complete the user full-name or net-address before point"
626 (lsdb-maybe-load-hash-tables)
629 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
630 (goto-char (match-end 0))
634 (completion-ignore-case t))
635 (unless (eq last-command this-command)
636 (setq lsdb-last-candidates nil
637 lsdb-last-candidates-pointer nil
638 lsdb-last-completion (buffer-substring start (point))
639 pattern (concat "\\<" lsdb-last-completion))
642 (let ((net (cdr (assq 'net value))))
643 (if (string-match pattern key)
644 (setq lsdb-last-candidates
645 (nconc lsdb-last-candidates
646 (mapcar (lambda (address)
647 (if (equal key address)
649 (concat key " <" address ">")))
652 (if (string-match pattern (car net))
653 (push (car net) lsdb-last-candidates))
654 (setq net (cdr net))))))
656 ;; Sort candidates by the position where the pattern occurred.
657 (setq lsdb-last-candidates
658 (sort lsdb-last-candidates
659 (lambda (cand1 cand2)
660 (< (if (string-match pattern cand1)
662 (if (string-match pattern cand2)
663 (match-beginning 0)))))))
664 (unless lsdb-last-candidates-pointer
665 (setq lsdb-last-candidates-pointer lsdb-last-candidates))
666 (when lsdb-last-candidates-pointer
667 (delete-region start (point))
668 (insert (pop lsdb-last-candidates-pointer)))))
670 ;;;_. Major Mode (`lsdb-mode') Implementation
671 ;;;_ : Modeline Buffer Identification
672 (defconst lsdb-pointer-xpm
674 static char * lsdb_pointer_xpm[] = {
696 (static-if (featurep 'xemacs)
698 (defvar lsdb-xemacs-modeline-left-extent
699 (copy-extent modeline-buffer-id-left-extent))
701 (defvar lsdb-xemacs-modeline-right-extent
702 (copy-extent modeline-buffer-id-right-extent))
704 (defun lsdb-modeline-buffer-identification (line)
705 "Decorate 1st element of `mode-line-buffer-identification' LINE.
706 Modify whole identification by side effect."
707 (let ((id (car line)) chopped)
708 (if (and (stringp id) (string-match "^LSDB:" id))
710 (setq chopped (substring id 0 (match-end 0))
711 id (substring id (match-end 0)))
718 (list (vector 'xpm :data lsdb-pointer-xpm)))
719 (list (vector 'string :data chopped))))))
720 (set-glyph-face glyph 'modeline-buffer-id)
721 (cons lsdb-xemacs-modeline-left-extent glyph))
722 (cons lsdb-xemacs-modeline-right-extent id))
728 (defun lsdb-modeline-buffer-identification (line)
729 "Decorate 1st element of `mode-line-buffer-identification' LINE.
730 Modify whole identification by side effect."
731 (let ((id (copy-sequence (car line)))
733 (if (image-type-available-p 'xpm)
734 (create-image lsdb-pointer-xpm 'xpm t :ascent 'center))))
736 (stringp id) (string-match "^LSDB:" id))
737 (add-text-properties 0 (length id)
739 'rear-nonsticky (list 'display))
744 (defalias 'lsdb-modeline-buffer-identification 'identity))))
746 (defvar lsdb-mode-map
747 (let ((keymap (make-sparse-keymap)))
748 (define-key keymap "a" 'lsdb-mode-add-entry)
749 (define-key keymap "d" 'lsdb-mode-delete-entry)
750 (define-key keymap "e" 'lsdb-mode-edit-entry)
751 (define-key keymap "s" 'lsdb-mode-save)
752 (define-key keymap "q" 'lsdb-mode-quit-window)
753 (define-key keymap "g" 'lsdb-mode-lookup)
754 (define-key keymap "p" 'lsdb-mode-previous-record)
755 (define-key keymap "n" 'lsdb-mode-next-record)
756 (define-key keymap " " 'scroll-up)
757 (define-key keymap [delete] 'scroll-down)
758 (define-key keymap "\177" 'scroll-down)
759 (define-key keymap [backspace] 'scroll-down)
763 (defvar lsdb-modeline-string "")
765 (define-derived-mode lsdb-mode fundamental-mode "LSDB"
766 "Major mode for browsing LSDB records."
767 (setq buffer-read-only t)
768 (static-if (featurep 'xemacs)
769 ;; In XEmacs, setting `font-lock-defaults' only affects on
770 ;; `find-file-hooks'.
771 (font-lock-set-defaults)
772 (set (make-local-variable 'font-lock-defaults)
773 '(lsdb-font-lock-keywords t)))
774 (make-local-variable 'post-command-hook)
775 (setq post-command-hook 'lsdb-modeline-update)
776 (make-local-variable 'lsdb-modeline-string)
777 (setq mode-line-buffer-identification
778 (lsdb-modeline-buffer-identification
779 '("LSDB: " lsdb-modeline-string)))
780 (lsdb-modeline-update)
781 (force-mode-line-update))
783 (defun lsdb-modeline-update ()
785 (get-text-property (if (eobp) (point-min) (point)) 'lsdb-record))
789 (setq net (car (cdr (assq 'net (cdr record)))))
790 (if (equal net (car record))
791 (setq lsdb-modeline-string net)
792 (setq lsdb-modeline-string (concat (car record) " <" net ">"))))
793 (setq lsdb-modeline-string ""))))
795 (defun lsdb-narrow-to-record ()
796 "Narrow to the current record."
797 (let ((end (next-single-property-change (point) 'lsdb-record nil
800 (previous-single-property-change (point) 'lsdb-record nil (point-min))
802 (goto-char (point-min))))
804 (defun lsdb-current-record ()
805 "Return the current record name."
806 (let ((record (get-text-property (point) 'lsdb-record)))
808 (error "There is nothing to follow here"))
811 (defun lsdb-current-entry ()
812 "Return the current entry name.
813 If the point is not on a entry line, it prompts to select a entry in
817 (if (looking-at "^[^\t]")
818 (let ((record (lsdb-current-record))
819 (completion-ignore-case t))
821 "Which entry to modify: "
822 (mapcar (lambda (entry)
823 (list (capitalize (symbol-name (car entry)))))
826 (re-search-backward "^\t\\([^\t][^:]+\\):")
829 (defun lsdb-mode-add-entry (entry-name)
830 "Add an entry on the current line."
832 (let ((completion-ignore-case t))
833 (list (completing-read "Entry name: " lsdb-known-entry-names))))
835 (unless (symbolp entry-name)
836 (setq entry-name (intern (downcase entry-name))))
837 (when (assq entry-name (cdr (lsdb-current-record)))
838 (error "The entry already exists"))
839 (let ((marker (point-marker)))
841 nil "Editing the entry."
845 (set-buffer lsdb-buffer-name)
847 (let ((record (lsdb-current-record))
848 (inhibit-read-only t)
850 (setcdr record (cons (cons ',entry-name form) (cdr record)))
851 (lsdb-puthash (car record) (cdr record)
853 (setq lsdb-hash-tables-are-dirty t)
854 (beginning-of-line 2)
858 (lsdb-insert-entry (cons ',entry-name form))
860 (list 'lsdb-record record)))))))))
862 (defun lsdb-mode-delete-entry (&optional entry-name dont-update)
863 "Delete the entry on the current line."
865 (let ((record (lsdb-current-record))
868 (setq entry-name (lsdb-current-entry)))
869 (setq entry (assq (intern (downcase entry-name)) (cdr record)))
872 (setcdr record (delq entry (cdr record)))
873 (lsdb-puthash (car record) (cdr record)
875 (setq lsdb-hash-tables-are-dirty t))
877 (lsdb-narrow-to-record)
878 (let ((case-fold-search t)
879 (inhibit-read-only t)
881 (goto-char (point-min))
882 (if (re-search-forward
883 (concat "^\t" (or entry-name
884 (lsdb-current-entry))
887 (delete-region (match-beginning 0)
888 (if (re-search-forward
889 "^\t[^\t][^:]+:" nil t)
893 (defun lsdb-mode-edit-entry ()
894 "Edit the entry on the current line."
896 (let* ((record (lsdb-current-record))
897 (entry-name (intern (downcase (lsdb-current-entry))))
898 (entry (assq entry-name (cdr record)))
899 (marker (point-marker)))
901 (cdr entry) "Editing the entry."
903 (unless (equal form ',(cdr entry))
905 (set-buffer lsdb-buffer-name)
907 (let* ((record (lsdb-current-record))
908 (entry (assq ',entry-name (cdr record)))
909 (inhibit-read-only t)
912 (setq lsdb-hash-tables-are-dirty t)
913 (lsdb-mode-delete-entry (symbol-name ',entry-name) t)
918 (lsdb-insert-entry (cons ',entry-name form))
920 (list 'lsdb-record record)))))))))
922 (defun lsdb-mode-save (&optional dont-ask)
923 "Save LSDB hash table into `lsdb-file'."
925 (if (not lsdb-hash-tables-are-dirty)
926 (message "(No changes need to be saved)")
927 (when (or (interactive-p)
929 (y-or-n-p "Save the LSDB now?"))
930 (lsdb-save-hash-tables)
931 (setq lsdb-hash-tables-are-dirty nil)
932 (message "The LSDB was saved successfully."))))
934 (defun lsdb-mode-quit-window (&optional kill window)
935 "Quit the current buffer.
936 It partially emulates the GNU Emacs' of `quit-window'."
939 (setq window (selected-window)))
940 (let ((buffer (window-buffer window)))
941 (unless (save-selected-window
942 (select-window window)
944 (delete-window window))
947 (bury-buffer buffer))))
949 (defun lsdb-mode-hide-buffer ()
950 "Hide the LSDB window."
951 (let ((window (get-buffer-window lsdb-buffer-name)))
953 (lsdb-mode-quit-window nil window))))
955 (defun lsdb-lookup-records (regexp &optional entry-name)
956 "Return the all records in the LSDB matching the REGEXP.
957 If the optional 2nd argument ENTRY-NAME is given, matching only
958 performed against the entry field."
963 (unless (symbolp entry-name)
964 (setq entry-name (intern (downcase entry-name))))
966 (let ((entry (cdr (assq entry-name value)))
968 (unless (listp entry)
969 (setq entry (list entry)))
970 (while (and (not found) entry)
971 (if (string-match regexp (pop entry))
974 (push (cons key value) records)))))
976 (if (string-match regexp key)
977 (push (cons key value) records))))
981 (defvar lsdb-mode-lookup-history nil)
983 (defun lsdb-mode-lookup (regexp &optional entry-name)
984 "Display the all records in the LSDB matching the REGEXP.
985 If the optional 2nd argument ENTRY-NAME is given, matching only
986 performed against the entry field."
988 (let* ((completion-ignore-case t)
990 (if current-prefix-arg
991 (completing-read "Entry name: "
992 lsdb-known-entry-names))))
994 (read-from-minibuffer
996 (format "Search records `%s' regexp: " entry-name)
997 "Search records regexp: ")
998 nil nil nil 'lsdb-mode-lookup-history)
1000 (lsdb-maybe-load-hash-tables)
1001 (let ((records (lsdb-lookup-records regexp entry-name)))
1003 (lsdb-display-records records))))
1006 (defalias 'lsdb 'lsdb-mode-lookup)
1008 (defun lsdb-mode-next-record (&optional arg)
1009 "Go to the next record."
1011 (unless arg ;called noninteractively?
1014 (lsdb-mode-previous-record (- arg))
1016 (goto-char (next-single-property-change
1017 (point) 'lsdb-record nil (point-max)))
1018 (setq arg (1- arg)))))
1020 (defun lsdb-mode-previous-record (&optional arg)
1021 "Go to the previous record."
1023 (unless arg ;called noninteractively?
1026 (lsdb-mode-next-record (- arg))
1028 (goto-char (previous-single-property-change
1029 (point) 'lsdb-record nil (point-min)))
1030 (setq arg (1- arg)))))
1032 ;;;_ : Edit Forms -- stolen (and renamed) from gnus-eform.el
1033 (defvar lsdb-edit-form-buffer "*LSDB edit form*")
1034 (defvar lsdb-edit-form-done-function nil)
1035 (defvar lsdb-previous-window-configuration nil)
1037 (defvar lsdb-edit-form-mode-map
1038 (let ((keymap (make-sparse-keymap)))
1039 (set-keymap-parent keymap emacs-lisp-mode-map)
1040 (define-key keymap "\C-c\C-c" 'lsdb-edit-form-done)
1041 (define-key keymap "\C-c\C-k" 'lsdb-edit-form-exit)
1043 "Edit form's keymap.")
1045 (defun lsdb-edit-form-mode ()
1046 "Major mode for editing forms.
1047 It is a slightly enhanced emacs-lisp-mode.
1049 \\{lsdb-edit-form-mode-map}"
1051 (kill-all-local-variables)
1052 (setq major-mode 'lsdb-edit-form-mode
1053 mode-name "LSDB Edit Form")
1054 (use-local-map lsdb-edit-form-mode-map)
1055 (make-local-variable 'lsdb-edit-form-done-function)
1056 (make-local-variable 'lsdb-previous-window-configuration)
1057 (run-hooks 'lsdb-edit-form-mode-hook))
1059 (defun lsdb-edit-form (form documentation exit-func)
1060 "Edit FORM in a new buffer.
1061 Call EXIT-FUNC on exit. Display DOCUMENTATION in the beginning
1063 (let ((window-configuration
1064 (current-window-configuration)))
1065 (switch-to-buffer (get-buffer-create lsdb-edit-form-buffer))
1066 (lsdb-edit-form-mode)
1067 (setq lsdb-previous-window-configuration window-configuration
1068 lsdb-edit-form-done-function exit-func)
1070 (insert documentation)
1073 (goto-char (point-min))
1077 (insert ";; Type `C-c C-c' after you've finished editing.\n")
1080 (pp form (current-buffer))
1084 (defun lsdb-edit-form-done ()
1085 "Update changes and kill the current buffer."
1087 (goto-char (point-min))
1088 (let ((form (condition-case nil
1089 (read (current-buffer))
1091 (func lsdb-edit-form-done-function))
1092 (lsdb-edit-form-exit)
1093 (funcall func form)))
1095 (defun lsdb-edit-form-exit ()
1096 "Kill the current buffer."
1098 (let ((window-configuration lsdb-previous-window-configuration))
1099 (kill-buffer (current-buffer))
1100 (set-window-configuration window-configuration)))
1102 ;;;_. Interface to Semi-gnus
1104 (defun lsdb-gnus-insinuate ()
1105 "Call this function to hook LSDB into Semi-gnus."
1106 (add-hook 'gnus-article-prepare-hook 'lsdb-gnus-update-record)
1107 (add-hook 'gnus-save-newsrc-hook 'lsdb-mode-save))
1109 (defvar gnus-current-headers)
1110 (defun lsdb-gnus-update-record ()
1111 (let ((entity gnus-current-headers)
1114 (set-buffer-multibyte nil)
1115 (buffer-disable-undo)
1116 (mime-insert-entity entity)
1117 (setq records (lsdb-update-records))
1119 (lsdb-display-record (car records))))))
1121 ;;;_. Interface to Wanderlust
1123 (defun lsdb-wl-insinuate ()
1124 "Call this function to hook LSDB into Wanderlust."
1125 (add-hook 'wl-message-redisplay-hook 'lsdb-wl-update-record)
1126 (add-hook 'wl-summary-exit-hook 'lsdb-mode-hide-buffer)
1127 (add-hook 'wl-exit-hook 'lsdb-mode-save)
1128 (add-hook 'wl-save-hook 'lsdb-mode-save))
1131 (autoload 'wl-message-get-original-buffer "wl-message"))
1132 (defun lsdb-wl-update-record ()
1134 (set-buffer (wl-message-get-original-buffer))
1135 (let ((records (lsdb-update-records)))
1137 (lsdb-display-record (car records))))))
1139 ;;;_. Interface to Mew written by Hideyuki SHIRAI <shirai@rdmg.mgcs.mei.co.jp>
1141 (autoload 'mew-sinfo-get-disp-msg "mew")
1142 (autoload 'mew-current-get-fld "mew")
1143 (autoload 'mew-current-get-msg "mew")
1144 (autoload 'mew-frame-id "mew")
1145 (autoload 'mew-cache-hit "mew"))
1148 (defun lsdb-mew-insinuate ()
1149 "Call this function to hook LSDB into Mew."
1150 (add-hook 'mew-message-hook 'lsdb-mew-update-record)
1151 (add-hook 'mew-summary-toggle-disp-msg-hook
1153 (unless (mew-sinfo-get-disp-msg)
1154 (lsdb-mode-hide-buffer))))
1155 (add-hook 'mew-suspend-hook 'lsdb-mode-hide-buffer)
1156 (add-hook 'mew-quit-hook 'lsdb-mode-save)
1157 (add-hook 'kill-emacs-hook 'lsdb-mode-save))
1159 (defun lsdb-mew-update-record ()
1160 (let* ((fld (mew-current-get-fld (mew-frame-id)))
1161 (msg (mew-current-get-msg (mew-frame-id)))
1162 (cache (mew-cache-hit fld msg 'must-hit))
1166 (make-local-variable 'lsdb-decode-field-body-function)
1167 (setq lsdb-decode-field-body-function
1169 (set-text-properties 0 (length body) nil body)
1171 (when (setq records (lsdb-update-records))
1172 (lsdb-display-record (car records))))))
1174 ;;;_. Interface to MU-CITE
1176 (autoload 'mu-cite-get-value "mu-cite"))
1178 (defun lsdb-mu-attribution (address)
1179 "Extract attribute information from LSDB."
1181 (lsdb-lookup-records (concat "\\<" address "\\>") 'net)))
1183 (cdr (assq 'attribution (cdr (car records)))))))
1185 (defun lsdb-mu-set-attribution (attribution address)
1186 "Add attribute information to LSDB."
1188 (lsdb-lookup-records (concat "\\<" address "\\>") 'net))
1191 (setq entry (assq 'attribution (cdr (car records))))
1193 (setcdr entry attribution)
1194 (setcdr (car records) (cons (cons 'attribution attribution)
1195 (cdr (car records))))
1196 (lsdb-puthash (car (car records)) (cdr (car records))
1198 (setq lsdb-hash-tables-are-dirty t)))))
1200 (defun lsdb-mu-get-prefix-method ()
1201 "A mu-cite method to return a prefix from LSDB or \">\".
1202 If an `attribution' value is found in LSDB, the value is returned.
1203 Otherwise \">\" is returned."
1204 (or (lsdb-mu-attribution (mu-cite-get-value 'address))
1207 (defvar minibuffer-allow-text-properties)
1209 (defvar lsdb-mu-history nil)
1211 (defun lsdb-mu-get-prefix-register-method ()
1212 "A mu-cite method to return a prefix from LSDB or register it.
1213 If an `attribution' value is found in LSDB, the value is returned.
1214 Otherwise the function requests a prefix from a user. The prefix will
1215 be registered to LSDB if the user wants it."
1216 (let ((address (mu-cite-get-value 'address)))
1217 (or (lsdb-mu-attribution address)
1218 (let* (minibuffer-allow-text-properties
1219 (result (read-string "Citation name? "
1220 (or (mu-cite-get-value 'x-attribution)
1221 (mu-cite-get-value 'full-name))
1223 (if (and (not (string-equal result ""))
1224 (y-or-n-p (format "Register \"%s\"? " result)))
1225 (lsdb-mu-set-attribution result address))
1228 (defun lsdb-mu-get-prefix-register-verbose-method ()
1229 "A mu-cite method to return a prefix using LSDB.
1231 In this method, a user must specify a prefix unconditionally. If an
1232 `attribution' value is found in LSDB, the value is used as a initial
1233 value to input the prefix. The prefix will be registered to LSDB if
1235 (let* ((address (mu-cite-get-value 'address))
1236 (attribution (lsdb-mu-attribution address))
1237 minibuffer-allow-text-properties
1238 (result (read-string "Citation name? "
1240 (mu-cite-get-value 'x-attribution)
1241 (mu-cite-get-value 'full-name))
1243 (if (and (not (string-equal result ""))
1244 (not (string-equal result attribution))
1245 (y-or-n-p (format "Register \"%s\"? " result)))
1246 (lsdb-mu-set-attribution result address))
1249 (defvar mu-cite-methods-alist)
1251 (defun lsdb-mu-insinuate ()
1252 (add-hook 'mu-cite-instantiation-hook
1254 (setq mu-cite-methods-alist
1256 mu-cite-methods-alist
1259 #'lsdb-mu-get-prefix-method)
1260 (cons 'lsdb-prefix-register
1261 #'lsdb-mu-get-prefix-register-method)
1262 (cons 'lsdb-prefix-register-verbose
1263 #'lsdb-mu-get-prefix-register-verbose-method)))))))
1265 ;;;_. X-Face Rendering
1266 (defvar lsdb-x-face-cache
1267 (lsdb-make-hash-table :test 'equal))
1269 (defun lsdb-x-face-available-image-type ()
1270 (static-if (featurep 'xemacs)
1273 (and (>= emacs-major-version 21)
1274 (fboundp 'image-type-available-p)
1275 (if (image-type-available-p 'pbm)
1277 (if (image-type-available-p 'xpm)
1280 (defun lsdb-expose-x-face ()
1281 (let* ((record (get-text-property (point-min) 'lsdb-record))
1282 (x-face (cdr (assq 'x-face (cdr record))))
1284 (when (and lsdb-insert-x-face-function
1286 (goto-char (point-min))
1288 (put-text-property 0 1 'invisible t delimiter) ;hide "\r"
1294 (funcall lsdb-insert-x-face-function (pop x-face)))
1296 'lsdb-record record))))
1298 (defun lsdb-insert-x-face-image (data type marker)
1299 (static-if (featurep 'xemacs)
1301 (set-buffer (marker-buffer marker))
1303 (let* ((inhibit-read-only t)
1305 (glyph (make-glyph (vector type :data data))))
1306 (set-extent-begin-glyph
1307 (make-extent (point) (point))
1310 (set-buffer (marker-buffer marker))
1312 (let* ((inhibit-read-only t)
1314 (image (create-image data type t :ascent 'center))
1315 (record (get-text-property (point) 'lsdb-record)))
1316 (put-text-property (point) (progn
1317 (insert-image image)
1319 'lsdb-record record)))))
1321 (defun lsdb-insert-x-face-asynchronously (x-face)
1322 (let* ((type (lsdb-x-face-available-image-type))
1323 (shell-file-name lsdb-shell-file-name)
1324 (shell-command-switch lsdb-shell-command-switch)
1325 (process-connection-type nil)
1326 (cached (cdr (assq type (lsdb-gethash x-face lsdb-x-face-cache))))
1327 (marker (point-marker))
1330 (lsdb-insert-x-face-image cached type marker)
1332 (start-process-shell-command
1333 "lsdb-x-face-command" (generate-new-buffer " *lsdb work*")
1335 (nth 1 (assq type lsdb-x-face-command-alist))
1336 "; } 2> /dev/null")))
1337 (process-send-string process (concat x-face "\n"))
1338 (process-send-eof process)
1339 (set-process-sentinel
1341 `(lambda (process string)
1343 (when (and (buffer-live-p (marker-buffer ,marker))
1344 (equal string "finished\n"))
1346 (with-current-buffer (process-buffer process)
1347 (set-buffer-multibyte nil)
1349 (lsdb-insert-x-face-image data ',type ,marker)
1350 (lsdb-puthash ,x-face (list (cons ',type data))
1351 lsdb-x-face-cache)))
1352 (kill-buffer (process-buffer process))))))))
1357 (product-provide 'lsdb
1358 (product-define "LSDB" nil '(0 2)))
1360 ;;;_* Local emacs vars.
1361 ;;; The following `outline-layout' local variable setting:
1362 ;;; - closes all topics from the first topic to just before the third-to-last,
1363 ;;; - shows the children of the third to last (config vars)
1364 ;;; - and the second to last (code section),
1365 ;;; - and closes the last topic (this local-variables section).
1367 ;;;outline-layout: (0 : -1 -1 0)
1370 ;;; lsdb.el ends here