1 ;;; lsdb.el --- the Lovely Sister Database
3 ;; Copyright (C) 2002 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: address 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)))
34 ;;; (add-hook 'gnus-summary-mode-hook
36 ;;; (define-key gnus-summary-mode-map ":" 'lsdb-toggle-buffer)))
38 ;;; For Wanderlust, put the following lines into your ~/.wl:
39 ;;; (autoload 'lsdb-wl-insinuate "lsdb")
40 ;;; (add-hook 'wl-init-hook 'lsdb-wl-insinuate)
41 ;;; (add-hook 'wl-draft-mode-hook
43 ;;; (define-key wl-draft-mode-map "\M-\t" 'lsdb-complete-name)))
44 ;;; (add-hook 'wl-summary-mode-hook
46 ;;; (define-key wl-summary-mode-map ":" 'lsdb-wl-toggle-buffer)))
48 ;;; For Mew, put the following lines into your ~/.mew:
49 ;;; (autoload 'lsdb-mew-insinuate "lsdb")
50 ;;; (add-hook 'mew-init-hook 'lsdb-mew-insinuate)
51 ;;; (add-hook 'mew-draft-mode-hook
53 ;;; (define-key mew-draft-header-map "\M-I" 'lsdb-complete-name)))
54 ;;; (add-hook 'mew-summary-mode-hook
56 ;;; (define-key mew-summary-mode-map "L" 'lsdb-toggle-buffer)))
65 ;;;_* USER CUSTOMIZATION VARIABLES:
67 "The Lovely Sister Database."
71 (defcustom lsdb-file (expand-file-name "~/.lsdb")
72 "The name of the Lovely Sister Database file."
76 (defcustom lsdb-file-coding-system (find-coding-system 'ctext)
77 "Coding system for `lsdb-file'."
81 (defcustom lsdb-sender-headers
83 "List of headers to search for senders."
87 (defcustom lsdb-recipients-headers
88 "Resent-To\\|Resent-Cc\\|Reply-To\\|To\\|Cc\\|Bcc"
89 "List of headers to search for recipients."
93 (defcustom lsdb-interesting-header-alist
94 `(("Organization" nil organization)
95 ("\\(X-\\)?User-Agent\\|X-Mailer\\|X-Newsreader" nil user-agent)
96 ("\\(X-\\)?ML-Name" nil mailing-list)
97 ("List-Id" "\\(.*\\)[ \t]+<[^>]+>\\'" mailing-list "\\1")
98 ("X-Sequence" "\\(.*\\)[ \t]+[0-9]+\\'" mailing-list "\\1")
99 ("Delivered-To" "mailing list[ \t]+\\([^@]+\\)@.*" mailing-list "\\1")
100 ("\\(X-URL\\|X-URI\\)" nil www)
101 ("X-Attribution\\|X-cite-me" nil attribution)
102 ("X-Face" nil x-face)
104 (,lsdb-sender-headers nil sender))
105 "Alist of headers we are interested in.
106 The format of elements of this list should be
107 (FIELD-NAME REGEXP ENTRY STRING)
108 where the last three elements are optional."
112 (defcustom lsdb-entry-type-alist
114 (creation-date 2 ?. t)
115 (last-modified 3 ?. t)
125 "Alist of entry types for presentation.
126 The format of elements of this list should be
127 (ENTRY SCORE [CLASS READ-ONLY])
128 where the last two elements are optional.
129 Possible values for CLASS are `?.' and '?,'. If CLASS is `?.', the
130 entry takes a unique value which is overridden by newly assigned one
131 by `lsdb-mode-edit-entry' or such a command. If CLASS is `?,', the
132 entry can have multiple values separated by commas.
133 If the fourth element READ-ONLY is non-nil, it is assumed that the
134 entry cannot be modified."
138 (defcustom lsdb-decode-field-body-function #'lsdb-decode-field-body
139 "Field body decoder."
143 (defcustom lsdb-canonicalize-full-name-function
144 #'lsdb-canonicalize-spaces-and-dots
145 "Way to canonicalize full name."
149 (defcustom lsdb-lookup-full-name-functions
150 '(lsdb-lookup-full-name-from-address-cache)
151 "List of functions to pick up the existing full-name of the sender.
152 The sender is passed to each function as the argument."
156 (defcustom lsdb-after-update-record-functions
157 '(lsdb-update-address-cache)
158 "List of functions called after a record is updated.
159 The updated record is passed to each function as the argument."
163 (defcustom lsdb-after-delete-record-functions
164 '(lsdb-delete-address-cache)
165 "List of functions called after a record is removed.
166 The removed record is passed to each function as the argument."
170 (defcustom lsdb-secondary-hash-tables
171 '(lsdb-address-cache)
172 "List of the hash tables for reverse lookup"
176 (defcustom lsdb-window-max-height 7
177 "Maximum number of lines used to display LSDB record."
181 (defcustom lsdb-x-face-image-type nil
182 "A image type of displayed x-face.
183 If non-nil, supersedes the return value of `lsdb-x-face-available-image-type'."
187 (defcustom lsdb-x-face-scale-factor 0.5
188 "A number used to scale down or scale up X-Face images."
192 (defcustom lsdb-x-face-command-alist
193 '((pbm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale " scale-factor)
194 (xpm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale " scale-factor " | ppmtoxpm"))
195 "An alist from an image type to a command to be executed to display an X-Face header.
196 The command will be executed in a sub-shell asynchronously.
197 The compressed face will be piped to this command."
201 (defcustom lsdb-insert-x-face-function
202 (if (static-if (featurep 'xemacs)
204 (and (>= emacs-major-version 21)
205 (fboundp 'image-type-available-p)
206 (or (image-type-available-p 'pbm)
207 (image-type-available-p 'xpm))))
208 #'lsdb-insert-x-face-asynchronously)
209 "Function to display X-Face."
213 (defcustom lsdb-face-image-type nil
214 "A image type of displayed face.
215 If non-nil, supersedes the return value of `lsdb-x-face-available-image-type'."
219 (defcustom lsdb-face-scale-factor 0.5
220 "A number used to scale down or scale up Face images."
224 (defcustom lsdb-face-command-alist
225 '((png "pngtopnm | pnmscale " scale-factor " | pnmtopng")
226 (xpm "pngtopnm | pnmscale " scale-factor " | ppmtoxpm"))
227 "An alist from an image type to a command to be executed to display a Face header.
228 The command will be executed in a sub-shell asynchronously.
229 The decoded field-body (actually a PNG data) will be piped to this command."
233 (defcustom lsdb-insert-face-function
234 (if (static-if (featurep 'xemacs)
237 (and (>= emacs-major-version 21)
238 (fboundp 'image-type-available-p)
239 (or (image-type-available-p 'png)
240 (image-type-available-p 'xpm))))
241 #'lsdb-insert-face-asynchronously)
242 "Function to display Face."
246 (defcustom lsdb-print-record-hook '(lsdb-expose-x-face lsdb-expose-face)
247 "A hook called after a record is displayed."
251 (defcustom lsdb-display-records-sort-predicate nil
252 "A predicate to sort records."
256 (defcustom lsdb-display-records-belong-to-user t
257 "Non-nil means LSDB displays records belong to yourself.
258 When this option is equal to nil and a message is sent by the user
259 whose address is `user-mail-address', the LSDB record for the To: line
260 will be shown instead of the one for the From: line."
264 (defcustom lsdb-pop-up-windows t
265 "Non-nil means LSDB should make new windows to display records."
269 (defgroup lsdb-edit-form nil
270 "A mode for editing forms."
273 (defcustom lsdb-edit-form-mode-hook nil
274 "Hook run in `lsdb-edit-form-mode' buffers."
275 :group 'lsdb-edit-form
278 (defcustom lsdb-shell-file-name "/bin/sh"
279 "File name to load inferior shells from.
280 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
284 (defcustom lsdb-shell-command-switch "-c"
285 "Switch used to have the shell execute its command line argument."
289 (defcustom lsdb-verbose t
290 "If non-nil, confirm user to submit changes to lsdb-hash-table."
294 (defcustom lsdb-strip-address nil
295 "If non-nil, strip display-name from sender address before completion."
299 (defcustom lsdb-use-migemo nil
300 "If non-nil, use `migemo' when complete address."
305 (defface lsdb-header-face
306 '((t (:underline t)))
307 "Face for the file header line in `lsdb-mode'."
309 (defvar lsdb-header-face 'lsdb-header-face)
311 (defface lsdb-field-name-face
312 '((((class color) (background dark))
313 (:foreground "PaleTurquoise" :bold t))
315 "Face for the message header line in `lsdb-mode'."
317 (defvar lsdb-field-name-face 'lsdb-field-name-face)
319 (defface lsdb-field-body-face
320 '((((class color) (background dark))
321 (:foreground "turquoise" :italic t))
323 "Face for the message header line in `lsdb-mode'."
325 (defvar lsdb-field-body-face 'lsdb-field-body-face)
327 (defconst lsdb-font-lock-keywords
329 (0 lsdb-header-face))
331 (0 lsdb-field-body-face))
332 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
333 (1 lsdb-field-name-face)
334 (2 lsdb-field-body-face))))
336 (put 'lsdb-mode 'font-lock-defaults '(lsdb-font-lock-keywords t))
338 ;;;_* CODE - no user customizations below
339 ;;;_. Internal Variables
340 (defvar lsdb-hash-table nil
341 "Internal hash table to hold LSDB records.")
343 (defvar lsdb-address-cache nil
344 "The reverse lookup table for `lsdb-hash-table'.
345 It represents address to full-name mapping.")
347 (defvar lsdb-buffer-name "*LSDB*"
348 "Buffer name to display LSDB record.")
350 (defvar lsdb-hash-tables-are-dirty nil
351 "Flag to indicate whether the internal hash tables need to be saved.")
353 (defvar lsdb-known-entry-names
355 "An obarray used to complete an entry name.")
357 (defvar lsdb-temp-buffer-show-function
358 #'lsdb-temp-buffer-show-function
359 "Non-nil means call as function to display a help buffer.
360 The function is called with one argument, the buffer to be displayed.
361 Overrides `temp-buffer-show-function'.")
363 ;;;_. Utility functions
364 (defun lsdb-substitute-variables (program variable value)
365 (setq program (copy-sequence program))
366 (let ((pointer program))
368 (setq pointer (memq variable program))
370 (setcar pointer value)))
373 ;;;_. Hash Table Emulation
374 (if (and (fboundp 'make-hash-table)
375 (subrp (symbol-function 'make-hash-table)))
377 (defalias 'lsdb-puthash 'puthash)
378 (defalias 'lsdb-gethash 'gethash)
379 (defalias 'lsdb-remhash 'remhash)
380 (defalias 'lsdb-maphash 'maphash)
381 (defalias 'lsdb-hash-table-size 'hash-table-size)
382 (defalias 'lsdb-hash-table-count 'hash-table-count)
383 (defalias 'lsdb-make-hash-table 'make-hash-table))
384 (defun lsdb-puthash (key value hash-table)
385 "Hash KEY to VALUE in HASH-TABLE."
386 ;; Obarray is regarded as an open hash table, as a matter of
387 ;; fact, rehashing doesn't make sense.
389 (when (> (car hash-table)
390 (* (length (nth 1 hash-table)) 0.7))
391 (setq new-obarray (make-vector (* (length (nth 1 hash-table)) 2) 0))
394 (set (intern (symbol-name symbol) new-obarray)
395 (symbol-value symbol)))
397 (setcdr hash-table (list new-obarray)))
398 (set (intern key (nth 1 hash-table)) value)
399 (setcar hash-table (1+ (car hash-table)))))
400 (defun lsdb-gethash (key hash-table &optional default)
401 "Find hash value for KEY in HASH-TABLE.
402 If there is no corresponding value, return DEFAULT (which defaults to nil)."
403 (let ((symbol (intern-soft key (nth 1 hash-table))))
405 (symbol-value symbol)
407 (defun lsdb-remhash (key hash-table)
408 "Remove the entry for KEY from HASH-TABLE.
409 Do nothing if there is no entry for KEY in HASH-TABLE."
410 (unintern key (nth 1 hash-table))
411 (setcar hash-table (1- (car hash-table))))
412 (defun lsdb-maphash (function hash-table)
413 "Map FUNCTION over entries in HASH-TABLE, calling it with two args,
414 each key and value in HASH-TABLE.
416 FUNCTION may not modify HASH-TABLE, with the one exception that FUNCTION
417 may remhash or puthash the entry currently being processed by FUNCTION."
420 (funcall function (symbol-name symbol) (symbol-value symbol)))
422 (defun lsdb-hash-table-size (hash-table)
423 "Return the size of HASH-TABLE.
424 This is the current number of slots in HASH-TABLE, whether occupied or not."
425 (length (nth 1 hash-table)))
426 (defalias 'lsdb-hash-table-count 'car)
427 (defun lsdb-make-hash-table (&rest args)
428 "Return a new empty hash table object."
429 (list 0 (make-vector (or (plist-get args :size) 29) 0))))
431 ;;;_. Hash Table Reader/Writer
432 (defconst lsdb-secondary-hash-table-start-format
435 (defsubst lsdb-secondary-hash-table-start (hash-table)
436 (format lsdb-secondary-hash-table-start-format hash-table))
441 ;; In XEmacs, hash tables can also be created by the lisp reader
442 ;; using structure syntax.
443 (read-from-string "#s(hash-table)")
444 (defalias 'lsdb-read 'read))
446 (defun lsdb-read (&optional marker)
447 "Read one Lisp expression as text from MARKER, return as Lisp object."
450 (if (looking-at "^#s(")
453 (forward-char 2) ;skip "#s"
454 (forward-sexp) ;move to the left paren
457 (buffer-disable-undo)
458 (insert-buffer-substring (marker-buffer marker)
460 (goto-char (point-min))
462 (let ((object (read (current-buffer)))
464 (if (eq 'hash-table (car object))
467 (lsdb-make-hash-table
468 :size (plist-get (cdr object) 'size)
470 data (plist-get (cdr object) 'data))
472 (lsdb-puthash (pop data) (pop data) hash-table))
477 (defun lsdb-load-hash-tables ()
478 "Read the contents of `lsdb-file' into the internal hash tables."
479 (let ((buffer (find-file-noselect lsdb-file))
484 (goto-char (point-min))
485 (re-search-forward "^#s(")
486 (goto-char (match-beginning 0))
487 (setq lsdb-hash-table (lsdb-read (point-marker)))
488 ;; Load the secondary hash tables following.
489 (setq tables lsdb-secondary-hash-tables)
491 (if (re-search-forward
492 (concat "^" (lsdb-secondary-hash-table-start
495 (set (car tables) (lsdb-read (point-marker))))
496 (setq tables (cdr tables))))
497 (kill-buffer buffer))))
499 (defun lsdb-insert-hash-table (hash-table)
500 (insert "#s(hash-table size "
501 ;; Reduce the actual size of the close hash table, because
502 ;; XEmacs doesn't have a distinction between index-size and
504 (number-to-string (lsdb-hash-table-count hash-table))
505 " test equal data (\n")
508 (let (print-level print-length)
509 (insert (prin1-to-string key) " " (prin1-to-string value) "\n")))
513 (defun lsdb-save-hash-tables ()
514 "Write the records within the internal hash tables into `lsdb-file'."
515 (let ((coding-system-for-write lsdb-file-coding-system)
517 (with-temp-file lsdb-file
518 (if (and (or (featurep 'mule)
519 (featurep 'file-coding))
520 lsdb-file-coding-system)
521 (let ((coding-system-name
522 (if (symbolp lsdb-file-coding-system)
523 (symbol-name lsdb-file-coding-system)
525 (static-if (featurep 'xemacs)
526 (symbol-name (coding-system-name
527 lsdb-file-coding-system))))))
528 (if coding-system-name
529 (insert ";;; -*- mode: emacs-lisp; coding: "
530 coding-system-name " -*-\n"))))
531 (lsdb-insert-hash-table lsdb-hash-table)
532 ;; Save the secondary hash tables following.
533 (setq tables lsdb-secondary-hash-tables)
535 (insert "\n" (lsdb-secondary-hash-table-start
537 (lsdb-insert-hash-table (symbol-value (car tables)))
538 (setq tables (cdr tables))))))
540 ;;;_. Mail Header Extraction
541 (defun lsdb-fetch-fields (regexp)
543 (goto-char (point-min))
544 (let ((case-fold-search t)
546 (while (re-search-forward (concat "^\\(" regexp "\\):[ \t]*")
548 (push (cons (match-string 1)
549 (buffer-substring (point) (std11-field-end)))
551 (nreverse field-bodies))))
553 (defun lsdb-canonicalize-spaces-and-dots (string)
554 (while (string-match " +\\|[\f\t\n\r\v]+\\|\\." string)
555 (setq string (replace-match " " nil t string)))
558 (defun lsdb-extract-address-components (string)
559 (let ((components (std11-extract-address-components string)))
560 (if (and (nth 1 components)
561 ;; When parsing a group address,
562 ;; std11-extract-address-components is likely to return
563 ;; the ("GROUP" "") form.
564 (not (equal (nth 1 components) "")))
566 (list (funcall lsdb-canonicalize-full-name-function
569 (list (nth 1 components) (nth 1 components))))))
571 ;; stolen (and renamed) from nnheader.el
572 (defun lsdb-decode-field-body (field-body field-name
573 &optional mode max-column)
574 (let ((multibyte enable-multibyte-characters))
577 (set-buffer-multibyte t)
578 (mime-decode-field-body field-body
579 (if (stringp field-name)
580 (intern (capitalize field-name))
583 (set-buffer-multibyte multibyte))))
585 ;;;_. Record Management
586 (defun lsdb-rebuild-secondary-hash-tables (&optional force)
587 (let ((tables lsdb-secondary-hash-tables))
589 (when (or force (not (symbol-value (car tables))))
590 (set (car tables) (lsdb-make-hash-table :test 'equal))
591 (setq lsdb-hash-tables-are-dirty t))
592 (setq tables (cdr tables))))
593 (if lsdb-hash-tables-are-dirty
597 'lsdb-after-update-record-functions
601 (defun lsdb-maybe-load-hash-tables ()
602 (unless lsdb-hash-table
603 (if (file-exists-p lsdb-file)
604 (lsdb-load-hash-tables)
605 (setq lsdb-hash-table (lsdb-make-hash-table :test 'equal)))
606 (lsdb-rebuild-secondary-hash-tables)))
608 ;;;_ : Fallback Lookup Functions
609 ;;;_ , #1 Address Cache
610 (defun lsdb-lookup-full-name-from-address-cache (sender)
611 (lsdb-gethash (nth 1 sender) lsdb-address-cache))
613 (defun lsdb-update-address-cache (record)
614 (let ((net (cdr (assq 'net record))))
616 (lsdb-puthash (pop net) (car record) lsdb-address-cache))))
618 (defun lsdb-delete-address-cache (record)
619 (let ((net (cdr (assq 'net record))))
621 (lsdb-remhash (pop net) lsdb-address-cache))))
623 ;;;_ , #2 Iterate on the All Records (very slow)
624 (defun lsdb-lookup-full-name-by-fuzzy-matching (sender)
627 "\\`\\(.+\\)[ \t]+\\(/[ \t]+\\|(\\([^)]+\\))\\)"
629 (if (match-beginning 3)
630 (list (match-string 1 (car sender))
631 (match-string 3 (car sender)))
632 (list (match-string 1 (car sender))
633 (substring (car sender) (match-end 0))))
634 (list (car sender))))
635 (case-fold-search t))
640 (if (or (string-match
641 (concat "\\<" (regexp-quote (car names)) "\\>")
647 (mapconcat #'identity
648 (nreverse (split-string (car names)))
652 ;; Don't assume that we are using address cache.
653 (member (nth 1 sender) (cdr (assq 'net value))))
655 (setq names (cdr names))))
658 ;;;_ : Update Records
659 (defun lsdb-update-record (sender &optional interesting)
660 (let ((old (lsdb-gethash (car sender) lsdb-hash-table))
661 (new (if (nth 1 sender)
662 (cons (cons 'net (list (nth 1 sender)))
668 ;; Look for the existing record from the reverse hash table.
669 ;; If it is found, regsiter the current full-name as AKA.
672 (run-hook-with-args-until-success
673 'lsdb-lookup-full-name-functions
676 (setq old (lsdb-gethash full-name lsdb-hash-table)
677 new (cons (list 'aka (car sender)) new))
678 (setcar sender full-name)))
680 (setq new (cons (cons 'creation-date (format-time-string "%Y-%m-%d"))
682 (setq merged (lsdb-merge-record-entries old new)
683 record (cons (car sender) merged))
684 (unless (equal merged old)
685 (let ((entry (assq 'last-modified (cdr record)))
686 (last-modified (format-time-string "%Y-%m-%d")))
688 (setcdr entry last-modified)
689 (setcdr record (cons (cons 'last-modified last-modified)
691 (lsdb-puthash (car record) (cdr record)
693 (run-hook-with-args 'lsdb-after-update-record-functions record)
694 (setq lsdb-hash-tables-are-dirty t))
697 (defun lsdb-update-records ()
698 (lsdb-maybe-load-hash-tables)
699 (let (senders recipients interesting alist records bodies entry)
701 (std11-narrow-to-header)
703 (delq nil (mapcar (lambda (field)
705 (lsdb-extract-address-components
710 (funcall lsdb-decode-field-body-function
711 (car components) (car field))))
714 lsdb-sender-headers)))
716 (delq nil (mapcar (lambda (field)
718 (lsdb-extract-address-components
723 (funcall lsdb-decode-field-body-function
724 (car components) (car field))))
727 lsdb-recipients-headers))))
728 (setq alist lsdb-interesting-header-alist)
734 (funcall lsdb-decode-field-body-function
735 (cdr field) (car field))))
736 (if (nth 1 (car alist))
737 (and (string-match (nth 1 (car alist))
739 (replace-match (nth 3 (car alist))
742 (lsdb-fetch-fields (car (car alist))))))
744 (setq entry (or (nth 2 (car alist))
747 (if (eq ?. (nth 2 (assq entry lsdb-entry-type-alist)))
751 (setq alist (cdr alist))))
753 (setq records (list (lsdb-update-record (pop senders) interesting))))
754 (setq alist (nconc senders recipients))
756 (setq records (cons (lsdb-update-record (pop alist)) records)))
759 (defun lsdb-merge-record-entries (old new)
760 (setq old (copy-sequence old))
762 (let ((entry (assq (car (car new)) old))
765 (setq old (nconc old (list (car new))))
766 (if (listp (cdr entry))
768 (setq list (cdr (car new)) pointer list)
770 (if (member (car pointer) (cdr entry))
771 (setq list (delq (car pointer) list)))
772 (setq pointer (cdr pointer)))
773 (setcdr entry (nconc (cdr entry) list)))
774 (setcdr entry (cdr (car new))))))
775 (setq new (cdr new)))
778 ;;;_. Display Management
779 (defun lsdb-fit-window-to-buffer (&optional window)
780 (save-selected-window
782 (select-window window))
783 (unless (pos-visible-in-window-p (point-max))
784 (enlarge-window (- lsdb-window-max-height (window-height))))
785 (shrink-window-if-larger-than-buffer)
786 (let ((height (window-height)))
787 (if (> height lsdb-window-max-height)
788 (shrink-window (- height lsdb-window-max-height)))
789 (set-window-start window (point-min)))))
791 (defun lsdb-temp-buffer-show-function (buffer)
792 (when lsdb-pop-up-windows
793 (save-selected-window
794 (let ((window (or (get-buffer-window lsdb-buffer-name)
796 (select-window (get-largest-window))
797 (split-window-vertically)))))
798 (set-window-buffer window buffer)
799 (lsdb-fit-window-to-buffer window)))))
801 (defun lsdb-update-records-and-display ()
802 (let ((records (lsdb-update-records)))
803 (if lsdb-display-records-belong-to-user
805 (lsdb-display-record (car records))
807 (catch 'lsdb-show-record
809 (if (member user-mail-address (cdr (assq 'net (car records))))
810 (setq records (cdr records))
811 (lsdb-display-record (car records))
812 (throw 'lsdb-show-record t)))
813 (lsdb-hide-buffer)))))
815 (defun lsdb-display-record (record)
816 "Display only one RECORD, then shrink the window as possible."
817 (let ((temp-buffer-show-function lsdb-temp-buffer-show-function))
818 (lsdb-display-records (list record))))
820 (defun lsdb-display-records (records)
821 (with-current-buffer (get-buffer-create lsdb-buffer-name)
822 (let ((standard-output (current-buffer))
823 (inhibit-read-only t)
825 (buffer-disable-undo)
828 (sort (copy-sequence records)
829 (or lsdb-display-records-sort-predicate
830 (lambda (record1 record2)
831 (string-lessp (car record1) (car record2))))))
834 (narrow-to-region (point) (point))
835 (lsdb-print-record (car records)))
836 (goto-char (point-max))
837 (setq records (cdr records))))
839 (set-buffer-modified-p lsdb-hash-tables-are-dirty)
840 (goto-char (point-min))
841 (if temp-buffer-show-function
842 (funcall temp-buffer-show-function (current-buffer))
843 (pop-to-buffer (current-buffer)))))
845 (defsubst lsdb-entry-score (entry)
846 (or (nth 1 (assq (car entry) lsdb-entry-type-alist)) 0))
848 (defun lsdb-insert-entry (entry)
849 (let ((entry-name (capitalize (symbol-name (car entry)))))
850 (intern entry-name lsdb-known-entry-names)
851 (if (>= (lsdb-entry-score entry) 0)
852 (insert "\t" entry-name ": "
853 (if (listp (cdr entry))
855 #'identity (cdr entry)
856 (if (eq ?, (nth 2 (assq (car entry)
857 lsdb-entry-type-alist)))
863 (defun lsdb-print-record (record)
864 (insert (car record) "\n")
866 (sort (copy-sequence (cdr record))
867 (lambda (entry1 entry2)
868 (> (lsdb-entry-score entry1) (lsdb-entry-score entry2))))))
870 (lsdb-insert-entry (car entries))
871 (setq entries (cdr entries))))
872 (add-text-properties (point-min) (point-max)
873 (list 'lsdb-record record))
874 (run-hooks 'lsdb-print-record-hook))
877 (defvar lsdb-last-completion nil)
878 (defvar lsdb-last-candidates nil)
879 (defvar lsdb-last-candidates-pointer nil)
880 (defvar lsdb-complete-marker nil)
882 ;;;_ : Matching Highlight
883 (defvar lsdb-last-highlight-overlay nil)
885 ;;; avoid byte-compile warning for migemo
887 (autoload 'migemo-get-pattern "migemo"))
889 (defun lsdb-complete-name-highlight (start end)
890 (make-local-hook 'pre-command-hook)
891 (add-hook 'pre-command-hook 'lsdb-complete-name-highlight-update nil t)
894 (if (and lsdb-use-migemo (fboundp 'migemo-get-pattern))
895 (re-search-forward lsdb-last-completion end)
896 (search-forward lsdb-last-completion end))
897 (setq lsdb-last-highlight-overlay
898 (make-overlay (match-beginning 0) (match-end 0)))
899 (overlay-put lsdb-last-highlight-overlay 'face
900 (or (find-face 'isearch-secondary)
901 (find-face 'isearch-lazy-highlight-face)
904 (defun lsdb-complete-name-highlight-update ()
905 (unless (eq this-command 'lsdb-complete-name)
906 (if lsdb-last-highlight-overlay
907 (delete-overlay lsdb-last-highlight-overlay))
908 (set-marker lsdb-complete-marker nil)
909 (remove-hook 'pre-command-hook
910 'lsdb-complete-name-highlight-update t)))
912 ;;;_ : Name Completion
913 (defun lsdb-complete-name ()
914 "Complete the user full-name or net-address before point"
916 (lsdb-maybe-load-hash-tables)
917 (unless (markerp lsdb-complete-marker)
918 (setq lsdb-complete-marker (make-marker)))
920 (or (and (eq (marker-buffer lsdb-complete-marker) (current-buffer))
921 (marker-position lsdb-complete-marker))
923 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
924 (set-marker lsdb-complete-marker (match-end 0)))))
927 (completion-ignore-case t))
928 (unless (eq last-command this-command)
929 (setq lsdb-last-candidates nil
930 lsdb-last-candidates-pointer nil
931 lsdb-last-completion (buffer-substring start (point)))
932 (if (and lsdb-use-migemo (fboundp 'migemo-get-pattern))
933 (setq lsdb-last-completion (migemo-get-pattern lsdb-last-completion)
934 pattern (concat "\\<\\(" lsdb-last-completion "\\)"))
935 (setq pattern (concat "\\<" (regexp-quote lsdb-last-completion))))
938 (setq lsdb-last-candidates
939 (nconc lsdb-last-candidates
942 (if (string-match pattern candidate)
944 (if lsdb-strip-address
945 (cdr (assq 'net value))
946 (append (cdr (assq 'net value))
947 (cdr (assq 'sender value)))))))))
949 ;; Sort candidates by the position where the pattern occurred.
950 (setq lsdb-last-candidates
951 (sort lsdb-last-candidates
952 (lambda (cand1 cand2)
953 (< (if (string-match pattern cand1)
955 (if (string-match pattern cand2)
956 (match-beginning 0)))))))
957 (unless lsdb-last-candidates-pointer
958 (setq lsdb-last-candidates-pointer lsdb-last-candidates))
959 (when lsdb-last-candidates-pointer
960 (delete-region start (point))
961 (insert (pop lsdb-last-candidates-pointer))
962 (lsdb-complete-name-highlight start (point)))))
964 ;;;_. Major Mode (`lsdb-mode') Implementation
965 ;;;_ : Modeline Buffer Identification
966 (defconst lsdb-pointer-xpm
968 static char * lsdb_pointer_xpm[] = {
990 (static-if (featurep 'xemacs)
992 (defvar lsdb-xemacs-modeline-left-extent
993 (copy-extent modeline-buffer-id-left-extent))
995 (defvar lsdb-xemacs-modeline-right-extent
996 (copy-extent modeline-buffer-id-right-extent))
998 (defun lsdb-modeline-buffer-identification (line)
999 "Decorate 1st element of `mode-line-buffer-identification' LINE.
1000 Modify whole identification by side effect."
1001 (let ((id (car line)) chopped)
1002 (if (and (stringp id) (string-match "^LSDB:" id))
1004 (setq chopped (substring id 0 (match-end 0))
1005 id (substring id (match-end 0)))
1012 (list (vector 'xpm :data lsdb-pointer-xpm)))
1013 (list (vector 'string :data chopped))))))
1014 (set-glyph-face glyph 'modeline-buffer-id)
1015 (cons lsdb-xemacs-modeline-left-extent glyph))
1016 (cons lsdb-xemacs-modeline-right-extent id))
1022 (defun lsdb-modeline-buffer-identification (line)
1023 "Decorate 1st element of `mode-line-buffer-identification' LINE.
1024 Modify whole identification by side effect."
1025 (let ((id (copy-sequence (car line)))
1027 (if (image-type-available-p 'xpm)
1028 (create-image lsdb-pointer-xpm 'xpm t :ascent 'center))))
1030 (stringp id) (string-match "^LSDB:" id))
1031 (add-text-properties 0 (length id)
1032 (list 'display image
1033 'rear-nonsticky (list 'display))
1038 (defalias 'lsdb-modeline-buffer-identification 'identity))))
1040 (defvar lsdb-mode-map
1041 (let ((keymap (make-sparse-keymap)))
1042 (define-key keymap "a" 'lsdb-mode-add-entry)
1043 (define-key keymap "d" 'lsdb-mode-delete-entry)
1044 (define-key keymap "D" 'lsdb-mode-delete-record)
1045 (define-key keymap "e" 'lsdb-mode-edit-entry)
1046 (define-key keymap "E" 'lsdb-mode-edit-record)
1047 (define-key keymap "l" 'lsdb-mode-load)
1048 (define-key keymap "s" 'lsdb-mode-save)
1049 (define-key keymap "q" 'lsdb-mode-quit-window)
1050 (define-key keymap "g" 'lsdb-mode-lookup)
1051 (define-key keymap "p" 'lsdb-mode-previous-record)
1052 (define-key keymap "n" 'lsdb-mode-next-record)
1053 (define-key keymap " " 'scroll-up)
1054 (define-key keymap [delete] 'scroll-down)
1055 (define-key keymap "\177" 'scroll-down)
1056 (define-key keymap [backspace] 'scroll-down)
1060 (defvar lsdb-modeline-string "")
1062 (define-derived-mode lsdb-mode fundamental-mode "LSDB"
1063 "Major mode for browsing LSDB records."
1064 (setq buffer-read-only t)
1065 (static-if (featurep 'xemacs)
1066 ;; In XEmacs, setting `font-lock-defaults' only affects on
1067 ;; `find-file-hooks'.
1068 (font-lock-set-defaults)
1069 (set (make-local-variable 'font-lock-defaults)
1070 '(lsdb-font-lock-keywords t)))
1071 (make-local-hook 'post-command-hook)
1072 (add-hook 'post-command-hook 'lsdb-modeline-update nil t)
1073 (make-local-variable 'lsdb-modeline-string)
1074 (setq mode-line-buffer-identification
1075 (lsdb-modeline-buffer-identification
1076 '("LSDB: " lsdb-modeline-string)))
1077 (lsdb-modeline-update)
1078 (force-mode-line-update))
1080 (defun lsdb-modeline-update ()
1082 (get-text-property (if (eobp) (point-min) (point)) 'lsdb-record))
1086 (setq net (car (cdr (assq 'net (cdr record)))))
1087 (if (and net (equal net (car record)))
1088 (setq lsdb-modeline-string net)
1089 (setq lsdb-modeline-string (concat (car record) " <" net ">"))))
1090 (setq lsdb-modeline-string ""))))
1092 (defun lsdb-narrow-to-record ()
1093 "Narrow to the current record."
1094 (let ((end (next-single-property-change (point) 'lsdb-record nil
1097 (previous-single-property-change end 'lsdb-record nil (point-min))
1099 (goto-char (point-min))))
1101 (defun lsdb-current-record ()
1102 "Return the current record name."
1103 (get-text-property (point) 'lsdb-record))
1105 (defun lsdb-delete-record (record)
1106 "Delete given RECORD."
1107 (lsdb-remhash (car record) lsdb-hash-table)
1108 (run-hook-with-args 'lsdb-after-delete-record-functions record)
1109 (setq lsdb-hash-tables-are-dirty t))
1111 (defun lsdb-current-entry ()
1112 "Return the current entry name in canonical form."
1115 (if (looking-at "^\t\\([^\t][^:]+\\):")
1116 (intern (downcase (match-string 1))))))
1118 (defun lsdb-read-entry (record &optional prompt)
1119 "Prompt to select an entry in the given RECORD."
1120 (let* ((completion-ignore-case t)
1125 (mapcar (lambda (entry)
1126 (list (capitalize (symbol-name (car entry)))))
1129 (unless (equal entry-name "")
1130 (intern (downcase entry-name)))))
1132 (defun lsdb-delete-entry (record entry)
1133 "Delete given ENTRY from RECORD."
1134 (setcdr record (delq entry (cdr record)))
1135 (lsdb-puthash (car record) (cdr record)
1137 (run-hook-with-args 'lsdb-after-update-record-functions record)
1138 (setq lsdb-hash-tables-are-dirty t))
1140 (defun lsdb-mode-add-entry (entry-name)
1141 "Add an entry on the current line."
1143 (let ((completion-ignore-case t))
1144 (list (completing-read "Entry name: " lsdb-known-entry-names))))
1146 (unless (symbolp entry-name)
1147 (setq entry-name (intern (downcase entry-name))))
1148 (when (assq entry-name (cdr (lsdb-current-record)))
1149 (error "The entry already exists"))
1150 (let ((marker (point-marker)))
1152 nil "Editing the entry."
1156 (set-buffer lsdb-buffer-name)
1158 (let ((record (lsdb-current-record))
1159 (inhibit-read-only t)
1161 (setcdr record (cons (cons ',entry-name form) (cdr record)))
1162 (lsdb-puthash (car record) (cdr record)
1164 (run-hook-with-args 'lsdb-after-update-record-functions record)
1165 (setq lsdb-hash-tables-are-dirty t)
1166 (beginning-of-line 2)
1167 (add-text-properties
1170 (lsdb-insert-entry (cons ',entry-name form))
1172 (list 'lsdb-record record)))))))))
1174 (defun lsdb-mode-delete-entry-1 (entry)
1175 "Delete text contents of the ENTRY from the current buffer."
1177 (lsdb-narrow-to-record)
1178 (let ((case-fold-search t)
1179 (inhibit-read-only t)
1181 (goto-char (point-min))
1182 (if (re-search-forward
1183 (concat "^\t" (capitalize (symbol-name (car entry))) ":")
1185 (delete-region (match-beginning 0)
1186 (if (re-search-forward
1187 "^\t[^\t][^:]+:" nil t)
1191 (defun lsdb-mode-delete-entry ()
1192 "Delete the entry on the current line."
1194 (let ((record (lsdb-current-record))
1197 (error "There is nothing to follow here"))
1198 (setq entry-name (or (lsdb-current-entry)
1199 (lsdb-read-entry record "Which entry to delete: "))
1200 entry (assq entry-name (cdr record)))
1202 (or (not lsdb-verbose)
1204 (format "Do you really want to delete entry `%s' of `%s'? "
1205 entry-name (car record)))))
1206 (lsdb-delete-entry record entry)
1207 (lsdb-mode-delete-entry-1 entry))))
1209 (defun lsdb-mode-delete-record ()
1210 "Delete the record on the current line."
1212 (let ((record (lsdb-current-record)))
1214 (error "%s" "There is nothing to follow here"))
1215 (when (or (not lsdb-verbose)
1217 (format "Do you really want to delete entire record of `%s'? "
1219 (lsdb-delete-record record)
1221 (lsdb-narrow-to-record)
1222 (let ((inhibit-read-only t)
1224 (delete-region (point-min) (point-max)))))))
1226 (defun lsdb-mode-delete-entry-or-record ()
1227 "Delete the entry on the current line.
1228 If the cursor is on the first line of a database entry (the name line)
1229 then the entire entry will be deleted."
1231 (if (lsdb-current-entry)
1232 (lsdb-mode-delete-entry)
1233 (lsdb-mode-delete-record)))
1235 (defun lsdb-mode-edit-entry ()
1236 "Edit the entry on the current line."
1238 (let ((record (lsdb-current-record)))
1240 (error "There is nothing to follow here"))
1241 (let ((entry-name (or (lsdb-current-entry)
1242 (lsdb-read-entry record "Which entry to edit: "))))
1244 (cdr (assq entry-name (cdr record))) "Editing the entry."
1246 (let* ((record ',record)
1247 (entry-name ',entry-name)
1248 (entry (assq entry-name (cdr record))))
1249 (unless (equal form (cdr entry))
1251 (run-hook-with-args 'lsdb-after-update-record-functions record)
1252 (setq lsdb-hash-tables-are-dirty t)
1253 (with-current-buffer lsdb-buffer-name
1254 (let ((inhibit-read-only t)
1256 (pos (text-property-any (point-min) (point-max)
1257 'lsdb-record record)))
1259 (error "%s" "The entry currently in editing is discarded"))
1260 (lsdb-mode-delete-entry-1 entry)
1262 (add-text-properties
1265 (lsdb-insert-entry (cons entry-name form))
1267 (list 'lsdb-record record)))))))))))
1269 (defun lsdb-mode-edit-record ()
1270 "Edit the name of the record on the current line."
1272 (let ((record (lsdb-current-record)))
1274 (error "There is nothing to follow here"))
1276 (car record) "Editing the name."
1278 (unless (stringp new-name)
1279 (error "String is required: `%s'" new-name))
1280 (let* ((record ',record)
1281 (old-name (car record)))
1282 (unless (equal new-name old-name)
1283 (lsdb-delete-record record)
1284 (setcar record new-name)
1285 (lsdb-puthash new-name (cdr record) lsdb-hash-table)
1286 (run-hook-with-args 'lsdb-after-update-record-functions record)
1287 (setq lsdb-hash-tables-are-dirty t)
1288 (with-current-buffer lsdb-buffer-name
1289 (let ((inhibit-read-only t)
1291 (pos (text-property-any (point-min) (point-max)
1292 'lsdb-record record)))
1294 (error "%s" "The entry currently in editing is discarded"))
1295 (delete-region (point) (+ (point) (length old-name)))
1296 (add-text-properties (point)
1297 (progn (insert form) (point))
1298 (list 'lsdb-record record))))))))))
1300 (defun lsdb-mode-edit-entry-or-record ()
1301 "Edit the entry on the current line.
1302 If the cursor is on the first line of a database entry (the name line)
1303 then the name of this record will be edited."
1305 (if (lsdb-current-entry)
1306 (lsdb-mode-edit-entry)
1307 (lsdb-mode-edit-record)))
1309 (defun lsdb-mode-save (&optional force)
1310 "Save LSDB hash table into `lsdb-file'."
1313 lsdb-hash-tables-are-dirty))
1314 (message "(No changes need to be saved)")
1315 (when (or (interactive-p) ;Don't ask user if this
1316 ;function is called as a
1319 (y-or-n-p "Save the LSDB now? "))
1320 (lsdb-save-hash-tables)
1321 (set-buffer-modified-p (setq lsdb-hash-tables-are-dirty nil))
1322 (message "The LSDB was saved successfully."))))
1324 (defun lsdb-mode-load ()
1325 "Load LSDB hash table from `lsdb-file'."
1327 (let (lsdb-secondary-hash-tables)
1328 (lsdb-load-hash-tables))
1329 (message "Rebuilding secondary hash tables...")
1330 (lsdb-rebuild-secondary-hash-tables t)
1331 (message "Rebuilding secondary hash tables...done"))
1333 (defun lsdb-mode-quit-window (&optional kill window)
1334 "Quit the current buffer.
1335 It partially emulates the GNU Emacs' of `quit-window'."
1338 (setq window (selected-window)))
1339 (let ((buffer (window-buffer window)))
1340 (unless (save-selected-window
1341 (select-window window)
1343 (delete-window window))
1345 (kill-buffer buffer)
1346 (bury-buffer (unless (eq buffer (current-buffer)) buffer)))))
1348 (defun lsdb-hide-buffer ()
1349 "Hide the LSDB window."
1350 (let ((window (get-buffer-window lsdb-buffer-name)))
1352 (lsdb-mode-quit-window nil window))))
1354 (defun lsdb-show-buffer ()
1355 "Show the LSDB window."
1356 (if (get-buffer lsdb-buffer-name)
1357 (if lsdb-temp-buffer-show-function
1358 (let ((lsdb-pop-up-windows t))
1359 (funcall lsdb-temp-buffer-show-function lsdb-buffer-name))
1360 (pop-to-buffer lsdb-buffer-name))))
1362 (defun lsdb-toggle-buffer (&optional arg)
1363 "Toggle hiding of the LSDB window.
1364 If given a negative prefix, always show; if given a positive prefix,
1367 (list (if current-prefix-arg
1368 (prefix-numeric-value current-prefix-arg)
1370 (unless arg ;called noninteractively?
1375 (not (get-buffer-window lsdb-buffer-name))))
1379 (get-buffer-window lsdb-buffer-name)))
1380 (lsdb-hide-buffer))))
1382 (defun lsdb-lookup-records (regexp &optional entry-name)
1383 "Return the all records in the LSDB matching the REGEXP.
1384 If the optional 2nd argument ENTRY-NAME is given, matching only
1385 performed against the entry field."
1391 (let ((entry (cdr (assq entry-name value)))
1393 (unless (listp entry)
1394 (setq entry (list entry)))
1395 (while (and (not found) entry)
1396 (if (string-match regexp (pop entry))
1399 (push (cons key value) records)))))
1401 (if (string-match regexp key)
1402 (push (cons key value) records))))
1406 (defvar lsdb-mode-lookup-history nil)
1408 (defun lsdb-mode-lookup (regexp &optional entry-name)
1409 "Display the all records in the LSDB matching the REGEXP.
1410 If the optional 2nd argument ENTRY-NAME is given, matching only
1411 performed against the entry field."
1413 (let* ((completion-ignore-case t)
1415 (if current-prefix-arg
1416 (completing-read "Entry name: "
1417 lsdb-known-entry-names))))
1419 (read-from-minibuffer
1421 (format "Search records `%s' regexp: " entry-name)
1422 "Search records regexp: ")
1423 nil nil nil 'lsdb-mode-lookup-history)
1424 (if (and entry-name (not (equal entry-name "")))
1425 (intern (downcase entry-name))))))
1426 (lsdb-maybe-load-hash-tables)
1427 (let ((records (lsdb-lookup-records regexp entry-name)))
1429 (lsdb-display-records records))))
1432 (defalias 'lsdb 'lsdb-mode-lookup)
1434 (defun lsdb-mode-next-record (&optional arg)
1435 "Go to the next record."
1437 (unless arg ;called noninteractively?
1440 (lsdb-mode-previous-record (- arg))
1442 (goto-char (next-single-property-change
1443 (point) 'lsdb-record nil (point-max)))
1444 (setq arg (1- arg)))))
1446 (defun lsdb-mode-previous-record (&optional arg)
1447 "Go to the previous record."
1449 (unless arg ;called noninteractively?
1452 (lsdb-mode-next-record (- arg))
1454 (goto-char (previous-single-property-change
1455 (point) 'lsdb-record nil (point-min)))
1456 (setq arg (1- arg)))))
1458 ;;;_ : Edit Forms -- stolen (and renamed) from gnus-eform.el
1459 (defvar lsdb-edit-form-buffer "*LSDB edit form*")
1460 (defvar lsdb-edit-form-done-function nil)
1461 (defvar lsdb-previous-window-configuration nil)
1463 (defvar lsdb-edit-form-mode-map
1464 (let ((keymap (make-sparse-keymap)))
1465 (set-keymap-parent keymap emacs-lisp-mode-map)
1466 (define-key keymap "\C-c\C-c" 'lsdb-edit-form-done)
1467 (define-key keymap "\C-c\C-k" 'lsdb-edit-form-exit)
1469 "Edit form's keymap.")
1471 (defun lsdb-edit-form-mode ()
1472 "Major mode for editing forms.
1473 It is a slightly enhanced emacs-lisp-mode.
1475 \\{lsdb-edit-form-mode-map}"
1477 (kill-all-local-variables)
1478 (setq major-mode 'lsdb-edit-form-mode
1479 mode-name "LSDB Edit Form")
1480 (use-local-map lsdb-edit-form-mode-map)
1481 (make-local-variable 'lsdb-edit-form-done-function)
1482 (make-local-variable 'lsdb-previous-window-configuration)
1483 (run-hooks 'lsdb-edit-form-mode-hook))
1485 (defun lsdb-edit-form (form documentation exit-func)
1486 "Edit FORM in a new buffer.
1487 Call EXIT-FUNC on exit. Display DOCUMENTATION in the beginning
1489 (let ((window-configuration
1490 (current-window-configuration)))
1491 (switch-to-buffer (get-buffer-create lsdb-edit-form-buffer))
1492 (lsdb-edit-form-mode)
1493 (setq lsdb-previous-window-configuration window-configuration
1494 lsdb-edit-form-done-function exit-func)
1496 (insert documentation)
1499 (goto-char (point-min))
1503 (insert ";; Type `C-c C-c' after you've finished editing.\n")
1506 (pp form (current-buffer))
1510 (defun lsdb-edit-form-done ()
1511 "Update changes and kill the current buffer."
1513 (goto-char (point-min))
1514 (let ((form (condition-case nil
1515 (read (current-buffer))
1517 (func lsdb-edit-form-done-function))
1518 (lsdb-edit-form-exit)
1519 (funcall func form)))
1521 (defun lsdb-edit-form-exit ()
1522 "Kill the current buffer."
1524 (let ((window-configuration lsdb-previous-window-configuration))
1525 (kill-buffer (current-buffer))
1526 (set-window-configuration window-configuration)))
1528 ;;;_. Interface to Semi-gnus
1530 (defun lsdb-gnus-insinuate ()
1531 "Call this function to hook LSDB into Semi-gnus."
1532 (add-hook 'gnus-article-prepare-hook 'lsdb-gnus-update-record)
1533 (add-hook 'gnus-save-newsrc-hook 'lsdb-mode-save))
1535 (defvar gnus-article-current-summary)
1536 (defvar gnus-original-article-buffer)
1537 (defun lsdb-gnus-update-record ()
1538 (with-current-buffer (with-current-buffer gnus-article-current-summary
1539 gnus-original-article-buffer)
1540 (lsdb-update-records-and-display)))
1542 ;;;_. Interface to Wanderlust
1544 (defun lsdb-wl-insinuate ()
1545 "Call this function to hook LSDB into Wanderlust."
1546 (add-hook 'wl-message-redisplay-hook 'lsdb-wl-update-record)
1547 (add-hook 'wl-summary-exit-hook 'lsdb-hide-buffer)
1548 (add-hook 'wl-summary-toggle-disp-off-hook 'lsdb-hide-buffer)
1549 (add-hook 'wl-summary-toggle-disp-folder-on-hook 'lsdb-hide-buffer)
1550 (add-hook 'wl-summary-toggle-disp-folder-off-hook 'lsdb-hide-buffer)
1551 (add-hook 'wl-summary-toggle-disp-folder-message-resumed-hook
1552 'lsdb-wl-show-buffer)
1553 (add-hook 'wl-exit-hook 'lsdb-mode-save)
1554 (add-hook 'wl-save-hook 'lsdb-mode-save))
1557 (autoload 'wl-message-get-original-buffer "wl-message"))
1558 (defun lsdb-wl-update-record ()
1560 (set-buffer (wl-message-get-original-buffer))
1561 (let ((lsdb-temp-buffer-show-function
1562 #'lsdb-wl-temp-buffer-show-function))
1563 (lsdb-update-records-and-display))))
1565 (defun lsdb-wl-toggle-buffer (&optional arg)
1566 "Toggle hiding of the LSDB window for Wanderlust.
1567 If given a negative prefix, always show; if given a positive prefix,
1570 (list (if current-prefix-arg
1571 (prefix-numeric-value current-prefix-arg)
1573 (let ((lsdb-temp-buffer-show-function
1574 #'lsdb-wl-temp-buffer-show-function))
1575 (lsdb-toggle-buffer arg)))
1577 (defun lsdb-wl-show-buffer ()
1578 (when lsdb-pop-up-windows
1579 (let ((lsdb-temp-buffer-show-function
1580 #'lsdb-wl-temp-buffer-show-function))
1581 (lsdb-show-buffer))))
1583 (defvar wl-current-summary-buffer)
1584 (defvar wl-message-buffer)
1585 (defun lsdb-wl-temp-buffer-show-function (buffer)
1586 (when lsdb-pop-up-windows
1587 (save-selected-window
1588 (let ((window (or (get-buffer-window lsdb-buffer-name)
1592 (if (buffer-live-p wl-current-summary-buffer)
1593 (set-buffer wl-current-summary-buffer))
1594 (get-buffer-window wl-message-buffer))
1595 (get-largest-window)))
1596 (split-window-vertically)))))
1597 (set-window-buffer window buffer)
1598 (lsdb-fit-window-to-buffer window)))))
1600 ;;;_. Interface to Mew written by Hideyuki SHIRAI <shirai@meadowy.org>
1605 ;; Avoid macro `mew-cache-hit' expand (Mew 1.94.2 or earlier).
1606 ;; Changed `mew-cache-hit' from macro to function at Mew 2.0.
1607 (if (not (fboundp 'mew-current-get-fld))
1608 (setq byte-compile-macro-environment
1609 (cons '(mew-cache-hit . nil)
1610 byte-compile-macro-environment))))
1612 ;; Silence byte compiler for environments where Mew does not installed.
1613 (autoload 'mew-sinfo-get-disp-msg "mew")
1614 (autoload 'mew-current-get-fld "mew")
1615 (autoload 'mew-current-get-msg "mew")
1616 (autoload 'mew-frame-id "mew")
1617 (autoload 'mew-cache-hit "mew")
1618 (autoload 'mew-xinfo-get-decode-err "mew")
1619 (autoload 'mew-xinfo-get-action "mew"))))
1622 (defun lsdb-mew-insinuate ()
1623 "Call this function to hook LSDB into Mew."
1624 (add-hook 'mew-message-hook 'lsdb-mew-update-record)
1625 (add-hook 'mew-summary-toggle-disp-msg-hook
1627 (unless (mew-sinfo-get-disp-msg)
1628 (lsdb-hide-buffer))))
1629 (add-hook 'mew-suspend-hook 'lsdb-hide-buffer)
1630 (add-hook 'mew-quit-hook 'lsdb-mode-save)
1631 (add-hook 'kill-emacs-hook 'lsdb-mode-save)
1634 ((fboundp 'mew-summary-visit-folder)
1635 (defadvice mew-summary-visit-folder (before lsdb-hide-buffer activate)
1636 (lsdb-hide-buffer)))
1638 ((fboundp 'mew-summary-switch-to-folder)
1639 (defadvice mew-summary-switch-to-folder (before lsdb-hide-buffer activate)
1640 (lsdb-hide-buffer)))))
1642 (defun lsdb-mew-update-record ()
1643 (let* ((fld (mew-current-get-fld (mew-frame-id)))
1644 (msg (mew-current-get-msg (mew-frame-id)))
1645 (cache (mew-cache-hit fld msg)))
1649 (unless (or (mew-xinfo-get-decode-err) (mew-xinfo-get-action))
1650 (make-local-variable 'lsdb-decode-field-body-function)
1651 (setq lsdb-decode-field-body-function
1653 (set-text-properties 0 (length body) nil body)
1655 (lsdb-update-records-and-display))))))
1657 ;;;_. Interface to MU-CITE
1659 (autoload 'mu-cite-get-value "mu-cite"))
1661 (defun lsdb-mu-attribution (address)
1662 "Extract attribute information from LSDB."
1664 (lsdb-lookup-records (concat "\\<" address "\\>") 'net)))
1666 (cdr (assq 'attribution (cdr (car records)))))))
1668 (defun lsdb-mu-set-attribution (attribution address)
1669 "Add attribute information to LSDB."
1671 (lsdb-lookup-records (concat "\\<" address "\\>") 'net))
1674 (setq entry (assq 'attribution (cdr (car records))))
1676 (setcdr entry attribution)
1677 (setcdr (car records) (cons (cons 'attribution attribution)
1678 (cdr (car records))))
1679 (lsdb-puthash (car (car records)) (cdr (car records))
1681 (run-hook-with-args 'lsdb-after-update-record-functions (car records))
1682 (setq lsdb-hash-tables-are-dirty t)))))
1684 (defun lsdb-mu-get-prefix-method ()
1685 "A mu-cite method to return a prefix from LSDB or \">\".
1686 If an `attribution' value is found in LSDB, the value is returned.
1687 Otherwise \">\" is returned."
1688 (or (lsdb-mu-attribution (mu-cite-get-value 'address))
1691 (defvar minibuffer-allow-text-properties)
1693 (defvar lsdb-mu-history nil)
1695 (defun lsdb-mu-get-prefix-register-method ()
1696 "A mu-cite method to return a prefix from LSDB or register it.
1697 If an `attribution' value is found in LSDB, the value is returned.
1698 Otherwise the function requests a prefix from a user. The prefix will
1699 be registered to LSDB if the user wants it."
1700 (let ((address (mu-cite-get-value 'address)))
1701 (or (lsdb-mu-attribution address)
1702 (let* (minibuffer-allow-text-properties
1703 (result (read-string "Citation name? "
1704 (or (mu-cite-get-value 'x-attribution)
1705 (mu-cite-get-value 'full-name))
1707 (if (and (not (string-equal result ""))
1708 (y-or-n-p (format "Register \"%s\"? " result)))
1709 (lsdb-mu-set-attribution result address))
1712 (defun lsdb-mu-get-prefix-register-verbose-method ()
1713 "A mu-cite method to return a prefix using LSDB.
1715 In this method, a user must specify a prefix unconditionally. If an
1716 `attribution' value is found in LSDB, the value is used as a initial
1717 value to input the prefix. The prefix will be registered to LSDB if
1719 (let* ((address (mu-cite-get-value 'address))
1720 (attribution (lsdb-mu-attribution address))
1721 minibuffer-allow-text-properties
1722 (result (read-string "Citation name? "
1724 (mu-cite-get-value 'x-attribution)
1725 (mu-cite-get-value 'full-name))
1727 (if (and (not (string-equal result ""))
1728 (not (string-equal result attribution))
1729 (y-or-n-p (format "Register \"%s\"? " result)))
1730 (lsdb-mu-set-attribution result address))
1733 (defvar mu-cite-methods-alist)
1735 (defun lsdb-mu-insinuate ()
1736 (add-hook 'mu-cite-instantiation-hook
1738 (setq mu-cite-methods-alist
1740 mu-cite-methods-alist
1743 #'lsdb-mu-get-prefix-method)
1744 (cons 'lsdb-prefix-register
1745 #'lsdb-mu-get-prefix-register-method)
1746 (cons 'lsdb-prefix-register-verbose
1747 #'lsdb-mu-get-prefix-register-verbose-method)))))))
1749 ;;;_. X-Face Rendering
1750 (defvar lsdb-x-face-cache
1751 (lsdb-make-hash-table :test 'equal))
1753 (defun lsdb-x-face-available-image-type ()
1754 (static-if (featurep 'xemacs)
1757 (and (>= emacs-major-version 21)
1758 (fboundp 'image-type-available-p)
1759 (if (image-type-available-p 'pbm)
1761 (if (image-type-available-p 'xpm)
1764 (defun lsdb-expose-x-face ()
1765 (let* ((record (get-text-property (point-min) 'lsdb-record))
1766 (x-face (cdr (assq 'x-face (cdr record))))
1768 (when (and lsdb-insert-x-face-function
1770 (goto-char (point-min))
1772 (put-text-property 0 1 'invisible t delimiter) ;hide "\r"
1778 (funcall lsdb-insert-x-face-function (pop x-face)))
1780 'lsdb-record record))))
1782 (defun lsdb-insert-x-face-image (data type marker)
1783 (static-if (featurep 'xemacs)
1785 (set-buffer (marker-buffer marker))
1787 (let* ((inhibit-read-only t)
1789 (glyph (make-glyph (vector type :data data))))
1790 (set-extent-begin-glyph
1791 (make-extent (point) (point))
1794 (set-buffer (marker-buffer marker))
1796 (let* ((inhibit-read-only t)
1798 (image (create-image data type t :ascent 'center))
1799 (record (get-text-property (point) 'lsdb-record)))
1800 (put-text-property (point) (progn
1801 (insert-image image)
1803 'lsdb-record record)))))
1805 (defun lsdb-insert-x-face-asynchronously (x-face)
1806 (let* ((type (or lsdb-x-face-image-type
1807 (lsdb-x-face-available-image-type)))
1808 (shell-file-name lsdb-shell-file-name)
1809 (shell-command-switch lsdb-shell-command-switch)
1810 (coding-system-for-read 'binary)
1811 (process-connection-type nil)
1812 (cached (cdr (assq type (lsdb-gethash x-face lsdb-x-face-cache))))
1813 (marker (point-marker))
1814 (buffer (generate-new-buffer " *lsdb work*"))
1817 (lsdb-insert-x-face-image cached type marker)
1819 (start-process-shell-command
1820 "lsdb-x-face-command" buffer
1823 (lsdb-substitute-variables
1824 (cdr (assq type lsdb-x-face-command-alist))
1826 (number-to-string lsdb-x-face-scale-factor)))
1827 "; } 2> /dev/null")))
1830 `(lambda (process string)
1832 (set-buffer ,buffer)
1833 (goto-char (point-max))
1835 (set-process-sentinel
1837 `(lambda (process string)
1839 (if (equal string "finished\n")
1841 (with-current-buffer ,buffer
1842 (set-buffer-multibyte nil)
1844 (lsdb-insert-x-face-image data ',type ,marker)
1845 (lsdb-puthash ,x-face (list (cons ',type data))
1846 lsdb-x-face-cache)))
1847 (kill-buffer ,buffer))))
1848 (process-send-string process (concat x-face "\n"))
1849 (process-send-eof process))))
1851 ;;;_. Face Rendering
1852 (defvar lsdb-face-cache
1853 (lsdb-make-hash-table :test 'equal))
1855 (defun lsdb-face-available-image-type ()
1856 (static-if (featurep 'xemacs)
1861 (and (>= emacs-major-version 21)
1862 (fboundp 'image-type-available-p)
1863 (if (image-type-available-p 'png)
1865 (if (image-type-available-p 'xpm)
1868 (defun lsdb-expose-face ()
1869 (let* ((record (get-text-property (point-min) 'lsdb-record))
1870 (face (cdr (assq 'face (cdr record))))
1872 (when (and lsdb-insert-face-function
1874 (goto-char (point-min))
1876 (put-text-property 0 1 'invisible t delimiter) ;hide "\r"
1882 (funcall lsdb-insert-face-function (pop face)))
1884 'lsdb-record record))))
1886 (defun lsdb-insert-face-image (data type marker)
1887 (static-if (featurep 'xemacs)
1889 (set-buffer (marker-buffer marker))
1891 (let* ((inhibit-read-only t)
1893 (glyph (make-glyph (vector type :data data))))
1894 (set-extent-begin-glyph
1895 (make-extent (point) (point))
1898 (set-buffer (marker-buffer marker))
1900 (let* ((inhibit-read-only t)
1902 (image (create-image data type t :ascent 'center))
1903 (record (get-text-property (point) 'lsdb-record)))
1904 (put-text-property (point) (progn
1905 (insert-image image)
1907 'lsdb-record record)))))
1909 (defun lsdb-insert-face-asynchronously (face)
1910 (let* ((type (or lsdb-face-image-type
1911 (lsdb-face-available-image-type)))
1912 (shell-file-name lsdb-shell-file-name)
1913 (shell-command-switch lsdb-shell-command-switch)
1914 (coding-system-for-read 'binary)
1915 (coding-system-for-write 'binary)
1916 (process-connection-type nil)
1917 (cached (cdr (assq type (lsdb-gethash face lsdb-face-cache))))
1918 (marker (point-marker))
1919 (buffer (generate-new-buffer " *lsdb work*"))
1922 (lsdb-insert-face-image cached type marker)
1924 (start-process-shell-command
1925 "lsdb-face-command" buffer
1928 (lsdb-substitute-variables
1929 (cdr (assq type lsdb-face-command-alist))
1931 (number-to-string lsdb-face-scale-factor)))
1932 "; } 2> /dev/null")))
1935 `(lambda (process string)
1937 (set-buffer ,buffer)
1938 (goto-char (point-max))
1940 (set-process-sentinel
1942 `(lambda (process string)
1944 (if (equal string "finished\n")
1946 (with-current-buffer ,buffer
1947 (set-buffer-multibyte nil)
1949 (lsdb-insert-face-image data ',type ,marker)
1950 (lsdb-puthash ,face (list (cons ',type data))
1952 (kill-buffer ,buffer))))
1953 (process-send-string process (base64-decode-string face))
1954 (process-send-eof process))))
1959 (product-provide 'lsdb
1960 (product-define "LSDB" nil '(0 11)))
1962 ;;;_* Local emacs vars.
1963 ;;; The following `allout-layout' local variable setting:
1964 ;;; - closes all topics from the first topic to just before the third-to-last,
1965 ;;; - shows the children of the third to last (config vars)
1966 ;;; - and the second to last (code section),
1967 ;;; - and closes the last topic (this local-variables section).
1969 ;;;allout-layout: (0 : -1 -1 0)
1972 ;;; lsdb.el ends here