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)))
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:
40 ;;; (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))
103 "Alist of headers we are interested in.
104 The format of elements of this list should be
105 (FIELD-NAME REGEXP ENTRY STRING)
106 where the last three elements are optional."
110 (defcustom lsdb-entry-type-alist
112 (creation-date 2 ?. t)
113 (last-modified 3 ?. t)
121 "Alist of entry types for presentation.
122 The format of elements of this list should be
123 (ENTRY SCORE [CLASS READ-ONLY])
124 where the last two elements are optional.
125 Possible values for CLASS are `?.' and '?,'. If CLASS is `?.', the
126 entry takes a unique value which is overridden by newly assigned one
127 by `lsdb-mode-edit-entry' or such a command. If CLASS is `?,', the
128 entry can have multiple values separated by commas.
129 If the fourth element READ-ONLY is non-nil, it is assumed that the
130 entry cannot be modified."
134 (defcustom lsdb-decode-field-body-function #'lsdb-decode-field-body
135 "Field body decoder."
139 (defcustom lsdb-canonicalize-full-name-function
140 #'lsdb-canonicalize-spaces-and-dots
141 "Way to canonicalize full name."
145 (defcustom lsdb-lookup-full-name-functions
146 '(lsdb-lookup-full-name-from-address-cache)
147 "List of functions to pick up the existing full-name of the sender.
148 The sender is passed to each function as the argument."
152 (defcustom lsdb-update-record-functions
153 '(lsdb-update-address-cache)
154 "List of functions called after a record is updated.
155 The updated record is passed to each function as the argument."
159 (defcustom lsdb-secondary-hash-tables
160 '(lsdb-address-cache)
161 "List of the hash tables for reverse lookup"
165 (defcustom lsdb-window-max-height 7
166 "Maximum number of lines used to display LSDB record."
170 (defcustom lsdb-x-face-image-type nil
171 "A image type of displayed x-face.
172 If non-nil, supersedes the return value of `lsdb-x-face-available-image-type'."
176 (defcustom lsdb-x-face-command-alist
177 '((pbm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale 0.5")
178 (xpm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale 0.5 | ppmtoxpm"))
179 "An alist from an image type to a command to be executed to display an X-Face header.
180 The command will be executed in a sub-shell asynchronously.
181 The compressed face will be piped to this command."
185 (defcustom lsdb-insert-x-face-function
186 (if (static-if (featurep 'xemacs)
188 (and (>= emacs-major-version 21)
189 (fboundp 'image-type-available-p)
190 (or (image-type-available-p 'pbm)
191 (image-type-available-p 'xpm))))
192 #'lsdb-insert-x-face-asynchronously)
193 "Function to display X-Face."
197 (defcustom lsdb-print-record-hook '(lsdb-expose-x-face)
198 "A hook called after a record is displayed."
202 (defcustom lsdb-display-records-sort-predicate nil
203 "A predicate to sort records."
207 (defcustom lsdb-pop-up-windows t
208 "Non-nil means LSDB should make new windows to display records."
212 (defgroup lsdb-edit-form nil
213 "A mode for editing forms."
216 (defcustom lsdb-edit-form-mode-hook nil
217 "Hook run in `lsdb-edit-form-mode' buffers."
218 :group 'lsdb-edit-form
221 (defcustom lsdb-shell-file-name "/bin/sh"
222 "File name to load inferior shells from.
223 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
227 (defcustom lsdb-shell-command-switch "-c"
228 "Switch used to have the shell execute its command line argument."
232 (defcustom lsdb-verbose t
233 "If non-nil, confirm user to submit changes to lsdb-hash-table."
238 (defface lsdb-header-face
239 '((t (:underline t)))
240 "Face for the file header line in `lsdb-mode'."
242 (defvar lsdb-header-face 'lsdb-header-face)
244 (defface lsdb-field-name-face
245 '((((class color) (background dark))
246 (:foreground "PaleTurquoise" :bold t))
248 "Face for the message header line in `lsdb-mode'."
250 (defvar lsdb-field-name-face 'lsdb-field-name-face)
252 (defface lsdb-field-body-face
253 '((((class color) (background dark))
254 (:foreground "turquoise" :italic t))
256 "Face for the message header line in `lsdb-mode'."
258 (defvar lsdb-field-body-face 'lsdb-field-body-face)
260 (defconst lsdb-font-lock-keywords
262 (0 lsdb-header-face))
264 (0 lsdb-field-body-face))
265 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
266 (1 lsdb-field-name-face)
267 (2 lsdb-field-body-face))))
269 (put 'lsdb-mode 'font-lock-defaults '(lsdb-font-lock-keywords t))
271 ;;;_* CODE - no user customizations below
272 ;;;_. Internal Variables
273 (defvar lsdb-hash-table nil
274 "Internal hash table to hold LSDB records.")
276 (defvar lsdb-address-cache nil
277 "The reverse lookup table for `lsdb-hash-table'.
278 It represents address to full-name mapping.")
280 (defvar lsdb-buffer-name "*LSDB*"
281 "Buffer name to display LSDB record.")
283 (defvar lsdb-hash-tables-are-dirty nil
284 "Flag to indicate whether the internal hash tables need to be saved.")
286 (defvar lsdb-known-entry-names
288 "An obarray used to complete an entry name.")
290 (defvar lsdb-temp-buffer-show-function
291 #'lsdb-temp-buffer-show-function
292 "Non-nil means call as function to display a help buffer.
293 The function is called with one argument, the buffer to be displayed.
294 Overrides `temp-buffer-show-function'.")
296 ;;;_. Hash Table Emulation
297 (if (and (fboundp 'make-hash-table)
298 (subrp (symbol-function 'make-hash-table)))
300 (defalias 'lsdb-puthash 'puthash)
301 (defalias 'lsdb-gethash 'gethash)
302 (defalias 'lsdb-remhash 'remhash)
303 (defalias 'lsdb-maphash 'maphash)
304 (defalias 'lsdb-hash-table-size 'hash-table-size)
305 (defalias 'lsdb-hash-table-count 'hash-table-count)
306 (defalias 'lsdb-make-hash-table 'make-hash-table))
307 (defun lsdb-puthash (key value hash-table)
308 "Hash KEY to VALUE in HASH-TABLE."
309 ;; Obarray is regarded as an open hash table, as a matter of
310 ;; fact, rehashing doesn't make sense.
312 (when (> (car hash-table)
313 (* (length (nth 1 hash-table)) 0.7))
314 (setq new-obarray (make-vector (* (length (nth 1 hash-table)) 2) 0))
317 (set (intern (symbol-name symbol) new-obarray)
318 (symbol-value symbol)))
320 (setcdr hash-table (list new-obarray)))
321 (set (intern key (nth 1 hash-table)) value)
322 (setcar hash-table (1+ (car hash-table)))))
323 (defun lsdb-gethash (key hash-table &optional default)
324 "Find hash value for KEY in HASH-TABLE.
325 If there is no corresponding value, return DEFAULT (which defaults to nil)."
326 (let ((symbol (intern-soft key (nth 1 hash-table))))
328 (symbol-value symbol)
330 (defun lsdb-remhash (key hash-table)
331 "Remove the entry for KEY from HASH-TABLE.
332 Do nothing if there is no entry for KEY in HASH-TABLE."
333 (unintern key (nth 1 hash-table))
334 (setcar hash-table (1- (car hash-table))))
335 (defun lsdb-maphash (function hash-table)
336 "Map FUNCTION over entries in HASH-TABLE, calling it with two args,
337 each key and value in HASH-TABLE.
339 FUNCTION may not modify HASH-TABLE, with the one exception that FUNCTION
340 may remhash or puthash the entry currently being processed by FUNCTION."
343 (funcall function (symbol-name symbol) (symbol-value symbol)))
345 (defun lsdb-hash-table-size (hash-table)
346 "Return the size of HASH-TABLE.
347 This is the current number of slots in HASH-TABLE, whether occupied or not."
348 (length (nth 1 hash-table)))
349 (defalias 'lsdb-hash-table-count 'car)
350 (defun lsdb-make-hash-table (&rest args)
351 "Return a new empty hash table object."
352 (list 0 (make-vector (or (plist-get args :size) 29) 0))))
354 ;;;_. Hash Table Reader/Writer
355 (defconst lsdb-secondary-hash-table-start-format
358 (defsubst lsdb-secondary-hash-table-start (hash-table)
359 (format lsdb-secondary-hash-table-start-format hash-table))
364 ;; In XEmacs, hash tables can also be created by the lisp reader
365 ;; using structure syntax.
366 (read-from-string "#s(hash-table)")
367 (defalias 'lsdb-read 'read))
369 (defun lsdb-read (&optional marker)
370 "Read one Lisp expression as text from MARKER, return as Lisp object."
373 (if (looking-at "^#s(")
376 (forward-char 2) ;skip "#s"
377 (forward-sexp) ;move to the left paren
380 (buffer-disable-undo)
381 (insert-buffer-substring (marker-buffer marker)
383 (goto-char (point-min))
385 (let ((object (read (current-buffer)))
387 (if (eq 'hash-table (car object))
390 (lsdb-make-hash-table
391 :size (plist-get (cdr object) 'size)
393 data (plist-get (cdr object) 'data))
395 (lsdb-puthash (pop data) (pop data) hash-table))
400 (defun lsdb-load-hash-tables ()
401 "Read the contents of `lsdb-file' into the internal hash tables."
402 (let ((buffer (find-file-noselect lsdb-file))
407 (goto-char (point-min))
408 (re-search-forward "^#s(")
409 (goto-char (match-beginning 0))
410 (setq lsdb-hash-table (lsdb-read (point-marker)))
411 ;; Load the secondary hash tables following.
412 (setq tables lsdb-secondary-hash-tables)
414 (if (re-search-forward
415 (concat "^" (lsdb-secondary-hash-table-start
418 (set (car tables) (lsdb-read (point-marker))))
419 (setq tables (cdr tables))))
420 (kill-buffer buffer))))
422 (defun lsdb-insert-hash-table (hash-table)
423 (insert "#s(hash-table size "
424 ;; Reduce the actual size of the close hash table, because
425 ;; XEmacs doesn't have a distinction between index-size and
427 (number-to-string (lsdb-hash-table-count hash-table))
428 " test equal data (")
431 (let (print-level print-length)
432 (insert (prin1-to-string key) " " (prin1-to-string value) " ")))
436 (defun lsdb-save-hash-tables ()
437 "Write the records within the internal hash tables into `lsdb-file'."
438 (let ((coding-system-for-write lsdb-file-coding-system)
440 (with-temp-file lsdb-file
441 (if (and (or (featurep 'mule)
442 (featurep 'file-coding))
443 lsdb-file-coding-system)
444 (let ((coding-system-name
445 (if (symbolp lsdb-file-coding-system)
446 (symbol-name lsdb-file-coding-system)
448 (static-if (featurep 'xemacs)
449 (symbol-name (coding-system-name
450 lsdb-file-coding-system))))))
451 (if coding-system-name
452 (insert ";;; -*- coding: " coding-system-name " -*-\n"))))
453 (lsdb-insert-hash-table lsdb-hash-table)
454 ;; Save the secondary hash tables following.
455 (setq tables lsdb-secondary-hash-tables)
457 (insert "\n" (lsdb-secondary-hash-table-start
459 (lsdb-insert-hash-table (symbol-value (car tables)))
460 (setq tables (cdr tables))))))
462 ;;;_. Mail Header Extraction
463 (defun lsdb-fetch-field-bodies (regexp)
465 (goto-char (point-min))
466 (let ((case-fold-search t)
468 (while (re-search-forward (concat "^\\(" regexp "\\):[ \t]*")
470 (push (funcall lsdb-decode-field-body-function
471 (buffer-substring (point) (std11-field-end))
474 (nreverse field-bodies))))
476 (defun lsdb-canonicalize-spaces-and-dots (string)
477 (while (string-match " +\\|[\f\t\n\r\v]+\\|\\." string)
478 (setq string (replace-match " " nil t string)))
481 (defun lsdb-extract-address-components (string)
482 (let ((components (std11-extract-address-components string)))
483 (if (and (nth 1 components)
484 ;; When parsing a group address,
485 ;; std11-extract-address-components is likely to return
486 ;; the ("GROUP" "") form.
487 (not (equal (nth 1 components) "")))
489 (list (funcall lsdb-canonicalize-full-name-function
492 (list (nth 1 components) (nth 1 components))))))
494 ;; stolen (and renamed) from nnheader.el
495 (defun lsdb-decode-field-body (field-body field-name
496 &optional mode max-column)
497 (let ((multibyte enable-multibyte-characters))
500 (set-buffer-multibyte t)
501 (mime-decode-field-body field-body
502 (if (stringp field-name)
503 (intern (capitalize field-name))
506 (set-buffer-multibyte multibyte))))
508 ;;;_. Record Management
509 (defun lsdb-rebuild-secondary-hash-tables (&optional force)
510 (let ((tables lsdb-secondary-hash-tables))
512 (when (or force (not (symbol-value (car tables))))
513 (set (car tables) (lsdb-make-hash-table :test 'equal))
517 'lsdb-update-record-functions
520 (setq lsdb-hash-tables-are-dirty t))
521 (setq tables (cdr tables)))))
523 (defun lsdb-maybe-load-hash-tables ()
524 (unless lsdb-hash-table
525 (if (file-exists-p lsdb-file)
526 (lsdb-load-hash-tables)
527 (setq lsdb-hash-table (lsdb-make-hash-table :test 'equal)))
528 (lsdb-rebuild-secondary-hash-tables)))
530 ;;;_ : Fallback Lookup Functions
531 ;;;_ , #1 Address Cache
532 (defun lsdb-lookup-full-name-from-address-cache (sender)
533 (lsdb-gethash (nth 1 sender) lsdb-address-cache))
535 (defun lsdb-update-address-cache (record)
536 (let ((net (cdr (assq 'net record))))
538 (lsdb-puthash (pop net) (car record) lsdb-address-cache))))
540 ;;;_ , #2 Iterate on the All Records (very slow)
541 (defun lsdb-lookup-full-name-by-fuzzy-matching (sender)
544 "\\`\\(.+\\)[ \t]+\\(/[ \t]+\\|(\\([^)]+\\))\\)"
546 (if (match-beginning 3)
547 (list (match-string 1 (car sender))
548 (match-string 3 (car sender)))
549 (list (match-string 1 (car sender))
550 (substring (car sender) (match-end 0))))
551 (list (car sender))))
552 (case-fold-search t))
557 (if (or (string-match
558 (concat "\\<" (regexp-quote (car names)) "\\>")
564 (mapconcat #'identity
565 (nreverse (split-string (car names)))
569 ;; Don't assume that we are using address cache.
570 (member (nth 1 sender) (cdr (assq 'net value))))
572 (setq names (cdr names))))
575 ;;;_ : Update Records
576 (defun lsdb-update-record (sender &optional interesting)
577 (let ((old (lsdb-gethash (car sender) lsdb-hash-table))
578 (new (cons (cons 'net (list (nth 1 sender)))
583 ;; Look for the existing record from the reverse hash table.
584 ;; If it is found, regsiter the current full-name as AKA.
587 (run-hook-with-args-until-success
588 'lsdb-lookup-full-name-functions
591 (setq old (lsdb-gethash full-name lsdb-hash-table)
592 new (cons (list 'aka (car sender)) new))
593 (setcar sender full-name)))
595 (setq new (cons (cons 'creation-date (format-time-string "%Y-%m-%d"))
597 (setq merged (lsdb-merge-record-entries old new)
598 record (cons (car sender) merged))
599 (unless (equal merged old)
600 (let ((entry (assq 'last-modified (cdr record)))
601 (last-modified (format-time-string "%Y-%m-%d")))
603 (setcdr entry last-modified)
604 (setcdr record (cons (cons 'last-modified last-modified)
606 (lsdb-puthash (car record) (cdr record)
608 (run-hook-with-args 'lsdb-update-record-functions record)
609 (setq lsdb-hash-tables-are-dirty t))
612 (defun lsdb-update-records ()
613 (lsdb-maybe-load-hash-tables)
614 (let (senders recipients interesting alist records bodies entry)
616 (std11-narrow-to-header)
618 (delq nil (mapcar #'lsdb-extract-address-components
619 (lsdb-fetch-field-bodies
620 lsdb-sender-headers)))
622 (delq nil (mapcar #'lsdb-extract-address-components
623 (lsdb-fetch-field-bodies
624 lsdb-recipients-headers))))
625 (setq alist lsdb-interesting-header-alist)
630 (if (nth 1 (car alist))
631 (and (string-match (nth 1 (car alist))
633 (replace-match (nth 3 (car alist))
636 (lsdb-fetch-field-bodies (car (car alist))))))
638 (setq entry (or (nth 2 (car alist))
641 (if (eq ?. (nth 2 (assq entry lsdb-entry-type-alist)))
645 (setq alist (cdr alist))))
647 (setq records (list (lsdb-update-record (pop senders) interesting))))
648 (setq alist (nconc senders recipients))
650 (setq records (cons (lsdb-update-record (pop alist)) records)))
653 (defun lsdb-merge-record-entries (old new)
654 (setq old (copy-sequence old))
656 (let ((entry (assq (car (car new)) old))
659 (setq old (nconc old (list (car new))))
660 (if (listp (cdr entry))
662 (setq list (cdr (car new)) pointer list)
664 (if (member (car pointer) (cdr entry))
665 (setq list (delq (car pointer) list)))
666 (setq pointer (cdr pointer)))
667 (setcdr entry (nconc (cdr entry) list)))
668 (setcdr entry (cdr (car new))))))
669 (setq new (cdr new)))
672 ;;;_. Display Management
673 (defun lsdb-fit-window-to-buffer (&optional window)
674 (save-selected-window
676 (select-window window))
677 (unless (pos-visible-in-window-p (point-max))
678 (enlarge-window (- lsdb-window-max-height (window-height))))
679 (shrink-window-if-larger-than-buffer)
680 (let ((height (window-height)))
681 (if (> height lsdb-window-max-height)
682 (shrink-window (- height lsdb-window-max-height)))
683 (set-window-start window (point-min)))))
685 (defun lsdb-temp-buffer-show-function (buffer)
686 (when lsdb-pop-up-windows
687 (save-selected-window
688 (let ((window (or (get-buffer-window lsdb-buffer-name)
690 (select-window (get-largest-window))
691 (split-window-vertically)))))
692 (set-window-buffer window buffer)
693 (lsdb-fit-window-to-buffer window)))))
695 (defun lsdb-display-record (record)
696 "Display only one RECORD, then shrink the window as possible."
697 (let ((temp-buffer-show-function lsdb-temp-buffer-show-function))
698 (lsdb-display-records (list record))))
700 (defun lsdb-display-records (records)
701 (with-output-to-temp-buffer lsdb-buffer-name
702 (set-buffer standard-output)
704 (sort (copy-sequence records)
705 (or lsdb-display-records-sort-predicate
706 (lambda (record1 record2)
707 (string-lessp (car record1) (car record2))))))
710 (narrow-to-region (point) (point))
711 (lsdb-print-record (car records)))
712 (goto-char (point-max))
713 (setq records (cdr records)))
716 (defsubst lsdb-entry-score (entry)
717 (or (nth 1 (assq (car entry) lsdb-entry-type-alist)) 0))
719 (defun lsdb-insert-entry (entry)
720 (let ((entry-name (capitalize (symbol-name (car entry)))))
721 (intern entry-name lsdb-known-entry-names)
722 (if (>= (lsdb-entry-score entry) 0)
723 (insert "\t" entry-name ": "
724 (if (listp (cdr entry))
726 #'identity (cdr entry)
727 (if (eq ?, (nth 2 (assq (car entry)
728 lsdb-entry-type-alist)))
734 (defun lsdb-print-record (record)
735 (insert (car record) "\n")
737 (sort (copy-sequence (cdr record))
738 (lambda (entry1 entry2)
739 (> (lsdb-entry-score entry1) (lsdb-entry-score entry2))))))
741 (lsdb-insert-entry (car entries))
742 (setq entries (cdr entries))))
743 (add-text-properties (point-min) (point-max)
744 (list 'lsdb-record record))
745 (run-hooks 'lsdb-print-record-hook))
748 (defvar lsdb-last-completion nil)
749 (defvar lsdb-last-candidates nil)
750 (defvar lsdb-last-candidates-pointer nil)
751 (defvar lsdb-complete-marker nil)
753 ;;;_ : Matching Highlight
754 (defvar lsdb-last-highlight-overlay nil)
756 (defun lsdb-complete-name-highlight (start end)
757 (make-local-hook 'pre-command-hook)
758 (add-hook 'pre-command-hook 'lsdb-complete-name-highlight-update nil t)
761 (search-forward lsdb-last-completion end)
762 (setq lsdb-last-highlight-overlay
763 (make-overlay (match-beginning 0) (match-end 0)))
764 (overlay-put lsdb-last-highlight-overlay 'face
765 (or (find-face 'isearch-secondary)
766 (find-face 'isearch-lazy-highlight-face)
769 (defun lsdb-complete-name-highlight-update ()
770 (unless (eq this-command 'lsdb-complete-name)
771 (if lsdb-last-highlight-overlay
772 (delete-overlay lsdb-last-highlight-overlay))
773 (set-marker lsdb-complete-marker nil)
774 (remove-hook 'pre-command-hook
775 'lsdb-complete-name-highlight-update t)))
777 ;;;_ : Name Completion
778 (defun lsdb-complete-name ()
779 "Complete the user full-name or net-address before point"
781 (lsdb-maybe-load-hash-tables)
782 (unless (markerp lsdb-complete-marker)
783 (setq lsdb-complete-marker (make-marker)))
785 (or (and (eq (marker-buffer lsdb-complete-marker) (current-buffer))
786 (marker-position lsdb-complete-marker))
788 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
789 (set-marker lsdb-complete-marker (match-end 0)))))
792 (completion-ignore-case t))
793 (unless (eq last-command this-command)
794 (setq lsdb-last-candidates nil
795 lsdb-last-candidates-pointer nil
796 lsdb-last-completion (buffer-substring start (point))
797 pattern (concat "\\<" (regexp-quote lsdb-last-completion)))
800 (let ((net (cdr (assq 'net value))))
801 (if (string-match pattern key)
802 (setq lsdb-last-candidates
803 (nconc lsdb-last-candidates
804 (mapcar (lambda (address)
805 (if (equal key address)
807 (concat key " <" address ">")))
810 (if (string-match pattern (car net))
811 (push (car net) lsdb-last-candidates))
812 (setq net (cdr net))))))
814 ;; Sort candidates by the position where the pattern occurred.
815 (setq lsdb-last-candidates
816 (sort lsdb-last-candidates
817 (lambda (cand1 cand2)
818 (< (if (string-match pattern cand1)
820 (if (string-match pattern cand2)
821 (match-beginning 0)))))))
822 (unless lsdb-last-candidates-pointer
823 (setq lsdb-last-candidates-pointer lsdb-last-candidates))
824 (when lsdb-last-candidates-pointer
825 (delete-region start (point))
826 (insert (pop lsdb-last-candidates-pointer))
827 (lsdb-complete-name-highlight start (point)))))
829 ;;;_. Major Mode (`lsdb-mode') Implementation
830 ;;;_ : Modeline Buffer Identification
831 (defconst lsdb-pointer-xpm
833 static char * lsdb_pointer_xpm[] = {
855 (static-if (featurep 'xemacs)
857 (defvar lsdb-xemacs-modeline-left-extent
858 (copy-extent modeline-buffer-id-left-extent))
860 (defvar lsdb-xemacs-modeline-right-extent
861 (copy-extent modeline-buffer-id-right-extent))
863 (defun lsdb-modeline-buffer-identification (line)
864 "Decorate 1st element of `mode-line-buffer-identification' LINE.
865 Modify whole identification by side effect."
866 (let ((id (car line)) chopped)
867 (if (and (stringp id) (string-match "^LSDB:" id))
869 (setq chopped (substring id 0 (match-end 0))
870 id (substring id (match-end 0)))
877 (list (vector 'xpm :data lsdb-pointer-xpm)))
878 (list (vector 'string :data chopped))))))
879 (set-glyph-face glyph 'modeline-buffer-id)
880 (cons lsdb-xemacs-modeline-left-extent glyph))
881 (cons lsdb-xemacs-modeline-right-extent id))
887 (defun lsdb-modeline-buffer-identification (line)
888 "Decorate 1st element of `mode-line-buffer-identification' LINE.
889 Modify whole identification by side effect."
890 (let ((id (copy-sequence (car line)))
892 (if (image-type-available-p 'xpm)
893 (create-image lsdb-pointer-xpm 'xpm t :ascent 'center))))
895 (stringp id) (string-match "^LSDB:" id))
896 (add-text-properties 0 (length id)
898 'rear-nonsticky (list 'display))
903 (defalias 'lsdb-modeline-buffer-identification 'identity))))
905 (defvar lsdb-mode-map
906 (let ((keymap (make-sparse-keymap)))
907 (define-key keymap "a" 'lsdb-mode-add-entry)
908 (define-key keymap "d" 'lsdb-mode-delete-entry)
909 (define-key keymap "e" 'lsdb-mode-edit-entry)
910 (define-key keymap "l" 'lsdb-mode-load)
911 (define-key keymap "s" 'lsdb-mode-save)
912 (define-key keymap "q" 'lsdb-mode-quit-window)
913 (define-key keymap "g" 'lsdb-mode-lookup)
914 (define-key keymap "p" 'lsdb-mode-previous-record)
915 (define-key keymap "n" 'lsdb-mode-next-record)
916 (define-key keymap " " 'scroll-up)
917 (define-key keymap [delete] 'scroll-down)
918 (define-key keymap "\177" 'scroll-down)
919 (define-key keymap [backspace] 'scroll-down)
923 (defvar lsdb-modeline-string "")
925 (define-derived-mode lsdb-mode fundamental-mode "LSDB"
926 "Major mode for browsing LSDB records."
927 (setq buffer-read-only t)
928 (static-if (featurep 'xemacs)
929 ;; In XEmacs, setting `font-lock-defaults' only affects on
930 ;; `find-file-hooks'.
931 (font-lock-set-defaults)
932 (set (make-local-variable 'font-lock-defaults)
933 '(lsdb-font-lock-keywords t)))
934 (make-local-hook 'post-command-hook)
935 (add-hook 'post-command-hook 'lsdb-modeline-update nil t)
936 (make-local-variable 'lsdb-modeline-string)
937 (setq mode-line-buffer-identification
938 (lsdb-modeline-buffer-identification
939 '("LSDB: " lsdb-modeline-string)))
940 (lsdb-modeline-update)
941 (force-mode-line-update))
943 (defun lsdb-modeline-update ()
945 (get-text-property (if (eobp) (point-min) (point)) 'lsdb-record))
949 (setq net (car (cdr (assq 'net (cdr record)))))
950 (if (and net (equal net (car record)))
951 (setq lsdb-modeline-string net)
952 (setq lsdb-modeline-string (concat (car record) " <" net ">"))))
953 (setq lsdb-modeline-string ""))))
955 (defun lsdb-narrow-to-record ()
956 "Narrow to the current record."
957 (let ((end (next-single-property-change (point) 'lsdb-record nil
960 (previous-single-property-change end 'lsdb-record nil (point-min))
962 (goto-char (point-min))))
964 (defun lsdb-current-record ()
965 "Return the current record name."
966 (get-text-property (point) 'lsdb-record))
968 (defun lsdb-current-entry ()
969 "Return the current entry name in canonical form."
972 (if (looking-at "^\t\\([^\t][^:]+\\):")
973 (intern (downcase (match-string 1))))))
975 (defun lsdb-read-entry (record &optional prompt)
976 "Prompt to select an entry in the given RECORD."
977 (let* ((completion-ignore-case t)
982 (mapcar (lambda (entry)
983 (list (capitalize (symbol-name (car entry)))))
986 (unless (equal entry-name "")
987 (intern (downcase entry-name)))))
989 (defun lsdb-delete-entry (record entry)
990 "Delete given ENTRY from RECORD."
991 (setcdr record (delq entry (cdr record)))
992 (lsdb-puthash (car record) (cdr record)
994 (run-hook-with-args 'lsdb-update-record-functions record)
995 (setq lsdb-hash-tables-are-dirty t))
997 (defun lsdb-mode-add-entry (entry-name)
998 "Add an entry on the current line."
1000 (let ((completion-ignore-case t))
1001 (list (completing-read "Entry name: " lsdb-known-entry-names))))
1003 (unless (symbolp entry-name)
1004 (setq entry-name (intern (downcase entry-name))))
1005 (when (assq entry-name (cdr (lsdb-current-record)))
1006 (error "The entry already exists"))
1007 (let ((marker (point-marker)))
1009 nil "Editing the entry."
1013 (set-buffer lsdb-buffer-name)
1015 (let ((record (lsdb-current-record))
1016 (inhibit-read-only t)
1018 (setcdr record (cons (cons ',entry-name form) (cdr record)))
1019 (lsdb-puthash (car record) (cdr record)
1021 (run-hook-with-args 'lsdb-update-record-functions record)
1022 (setq lsdb-hash-tables-are-dirty t)
1023 (beginning-of-line 2)
1024 (add-text-properties
1027 (lsdb-insert-entry (cons ',entry-name form))
1029 (list 'lsdb-record record)))))))))
1031 (defun lsdb-mode-delete-entry-1 (entry)
1032 "Delete text contents of the ENTRY from the current buffer."
1034 (lsdb-narrow-to-record)
1035 (let ((case-fold-search t)
1036 (inhibit-read-only t)
1038 (goto-char (point-min))
1039 (if (re-search-forward
1040 (concat "^\t" (capitalize (symbol-name (car entry))) ":")
1042 (delete-region (match-beginning 0)
1043 (if (re-search-forward
1044 "^\t[^\t][^:]+:" nil t)
1048 (defun lsdb-mode-delete-entry ()
1049 "Delete the entry on the current line."
1051 (let ((record (lsdb-current-record))
1054 (error "There is nothing to follow here"))
1055 (setq entry-name (or (lsdb-current-entry)
1056 (lsdb-read-entry record "Which entry to delete: "))
1057 entry (assq entry-name (cdr record)))
1059 (or (not (interactive-p))
1062 (format "Do you really want to delete entry `%s' of `%s'?"
1063 entry-name (car record)))))
1064 (lsdb-delete-entry record entry)
1065 (lsdb-mode-delete-entry-1 entry))))
1067 (defun lsdb-mode-edit-entry ()
1068 "Edit the entry on the current line."
1070 (let ((record (lsdb-current-record))
1071 entry-name entry marker)
1073 (error "There is nothing to follow here"))
1074 (setq entry-name (or (lsdb-current-entry)
1075 (lsdb-read-entry record "Which entry to edit: "))
1076 entry (assq entry-name (cdr record))
1077 marker (point-marker))
1079 (cdr entry) "Editing the entry."
1081 (unless (equal form ',(cdr entry))
1083 (set-buffer lsdb-buffer-name)
1085 (let ((record (lsdb-current-record))
1087 (inhibit-read-only t)
1090 (error "The entry currently in editing is discarded"))
1091 (setq entry (assq ',entry-name (cdr record)))
1093 (run-hook-with-args 'lsdb-update-record-functions record)
1094 (setq lsdb-hash-tables-are-dirty t)
1095 (lsdb-mode-delete-entry-1 entry)
1097 (add-text-properties
1100 (lsdb-insert-entry (cons ',entry-name form))
1102 (list 'lsdb-record record)))))))))
1104 (defun lsdb-mode-save (&optional dont-ask)
1105 "Save LSDB hash table into `lsdb-file'."
1107 (if (not lsdb-hash-tables-are-dirty)
1108 (message "(No changes need to be saved)")
1109 (when (or (interactive-p)
1112 (y-or-n-p "Save the LSDB now? "))
1113 (lsdb-save-hash-tables)
1114 (setq lsdb-hash-tables-are-dirty nil)
1115 (message "The LSDB was saved successfully."))))
1117 (defun lsdb-mode-load ()
1118 "Load LSDB hash table from `lsdb-file'."
1120 (let (lsdb-secondary-hash-tables)
1121 (lsdb-load-hash-tables))
1122 (message "Rebuilding secondary hash tables...")
1123 (lsdb-rebuild-secondary-hash-tables t)
1124 (message "Rebuilding secondary hash tables...done"))
1126 (defun lsdb-mode-quit-window (&optional kill window)
1127 "Quit the current buffer.
1128 It partially emulates the GNU Emacs' of `quit-window'."
1131 (setq window (selected-window)))
1132 (let ((buffer (window-buffer window)))
1133 (unless (save-selected-window
1134 (select-window window)
1136 (delete-window window))
1138 (kill-buffer buffer)
1139 (bury-buffer (unless (eq buffer (current-buffer)) buffer)))))
1141 (defun lsdb-hide-buffer ()
1142 "Hide the LSDB window."
1143 (let ((window (get-buffer-window lsdb-buffer-name)))
1145 (lsdb-mode-quit-window nil window))))
1147 (defun lsdb-show-buffer ()
1148 "Show the LSDB window."
1149 (if (get-buffer lsdb-buffer-name)
1150 (if lsdb-temp-buffer-show-function
1151 (let ((lsdb-pop-up-windows t))
1152 (funcall lsdb-temp-buffer-show-function lsdb-buffer-name))
1153 (pop-to-buffer lsdb-buffer-name))))
1155 (defun lsdb-toggle-buffer (&optional arg)
1156 "Toggle hiding of the LSDB window.
1157 If given a negative prefix, always show; if given a positive prefix,
1160 (list (if current-prefix-arg
1161 (prefix-numeric-value current-prefix-arg)
1163 (unless arg ;called noninteractively?
1168 (not (get-buffer-window lsdb-buffer-name))))
1172 (get-buffer-window lsdb-buffer-name)))
1173 (lsdb-hide-buffer))))
1175 (defun lsdb-lookup-records (regexp &optional entry-name)
1176 "Return the all records in the LSDB matching the REGEXP.
1177 If the optional 2nd argument ENTRY-NAME is given, matching only
1178 performed against the entry field."
1184 (let ((entry (cdr (assq entry-name value)))
1186 (unless (listp entry)
1187 (setq entry (list entry)))
1188 (while (and (not found) entry)
1189 (if (string-match regexp (pop entry))
1192 (push (cons key value) records)))))
1194 (if (string-match regexp key)
1195 (push (cons key value) records))))
1199 (defvar lsdb-mode-lookup-history nil)
1201 (defun lsdb-mode-lookup (regexp &optional entry-name)
1202 "Display the all records in the LSDB matching the REGEXP.
1203 If the optional 2nd argument ENTRY-NAME is given, matching only
1204 performed against the entry field."
1206 (let* ((completion-ignore-case t)
1208 (if current-prefix-arg
1209 (completing-read "Entry name: "
1210 lsdb-known-entry-names))))
1212 (read-from-minibuffer
1214 (format "Search records `%s' regexp: " entry-name)
1215 "Search records regexp: ")
1216 nil nil nil 'lsdb-mode-lookup-history)
1217 (if (and entry-name (not (equal entry-name "")))
1218 (intern (downcase entry-name))))))
1219 (lsdb-maybe-load-hash-tables)
1220 (let ((records (lsdb-lookup-records regexp entry-name)))
1222 (lsdb-display-records records))))
1225 (defalias 'lsdb 'lsdb-mode-lookup)
1227 (defun lsdb-mode-next-record (&optional arg)
1228 "Go to the next record."
1230 (unless arg ;called noninteractively?
1233 (lsdb-mode-previous-record (- arg))
1235 (goto-char (next-single-property-change
1236 (point) 'lsdb-record nil (point-max)))
1237 (setq arg (1- arg)))))
1239 (defun lsdb-mode-previous-record (&optional arg)
1240 "Go to the previous record."
1242 (unless arg ;called noninteractively?
1245 (lsdb-mode-next-record (- arg))
1247 (goto-char (previous-single-property-change
1248 (point) 'lsdb-record nil (point-min)))
1249 (setq arg (1- arg)))))
1251 ;;;_ : Edit Forms -- stolen (and renamed) from gnus-eform.el
1252 (defvar lsdb-edit-form-buffer "*LSDB edit form*")
1253 (defvar lsdb-edit-form-done-function nil)
1254 (defvar lsdb-previous-window-configuration nil)
1256 (defvar lsdb-edit-form-mode-map
1257 (let ((keymap (make-sparse-keymap)))
1258 (set-keymap-parent keymap emacs-lisp-mode-map)
1259 (define-key keymap "\C-c\C-c" 'lsdb-edit-form-done)
1260 (define-key keymap "\C-c\C-k" 'lsdb-edit-form-exit)
1262 "Edit form's keymap.")
1264 (defun lsdb-edit-form-mode ()
1265 "Major mode for editing forms.
1266 It is a slightly enhanced emacs-lisp-mode.
1268 \\{lsdb-edit-form-mode-map}"
1270 (kill-all-local-variables)
1271 (setq major-mode 'lsdb-edit-form-mode
1272 mode-name "LSDB Edit Form")
1273 (use-local-map lsdb-edit-form-mode-map)
1274 (make-local-variable 'lsdb-edit-form-done-function)
1275 (make-local-variable 'lsdb-previous-window-configuration)
1276 (run-hooks 'lsdb-edit-form-mode-hook))
1278 (defun lsdb-edit-form (form documentation exit-func)
1279 "Edit FORM in a new buffer.
1280 Call EXIT-FUNC on exit. Display DOCUMENTATION in the beginning
1282 (let ((window-configuration
1283 (current-window-configuration)))
1284 (switch-to-buffer (get-buffer-create lsdb-edit-form-buffer))
1285 (lsdb-edit-form-mode)
1286 (setq lsdb-previous-window-configuration window-configuration
1287 lsdb-edit-form-done-function exit-func)
1289 (insert documentation)
1292 (goto-char (point-min))
1296 (insert ";; Type `C-c C-c' after you've finished editing.\n")
1299 (pp form (current-buffer))
1303 (defun lsdb-edit-form-done ()
1304 "Update changes and kill the current buffer."
1306 (goto-char (point-min))
1307 (let ((form (condition-case nil
1308 (read (current-buffer))
1310 (func lsdb-edit-form-done-function))
1311 (lsdb-edit-form-exit)
1312 (funcall func form)))
1314 (defun lsdb-edit-form-exit ()
1315 "Kill the current buffer."
1317 (let ((window-configuration lsdb-previous-window-configuration))
1318 (kill-buffer (current-buffer))
1319 (set-window-configuration window-configuration)))
1321 ;;;_. Interface to Semi-gnus
1323 (defun lsdb-gnus-insinuate ()
1324 "Call this function to hook LSDB into Semi-gnus."
1325 (add-hook 'gnus-article-prepare-hook 'lsdb-gnus-update-record)
1326 (add-hook 'gnus-save-newsrc-hook 'lsdb-mode-save))
1328 (defvar gnus-current-headers)
1329 (defun lsdb-gnus-update-record ()
1330 (let ((entity gnus-current-headers)
1333 (set-buffer-multibyte nil)
1334 (buffer-disable-undo)
1335 (mime-insert-entity entity)
1336 (setq records (lsdb-update-records))
1338 (lsdb-display-record (car records))))))
1340 ;;;_. Interface to Wanderlust
1342 (defun lsdb-wl-insinuate ()
1343 "Call this function to hook LSDB into Wanderlust."
1344 (add-hook 'wl-message-redisplay-hook 'lsdb-wl-update-record)
1345 (add-hook 'wl-summary-exit-hook 'lsdb-hide-buffer)
1346 (add-hook 'wl-summary-toggle-disp-off-hook 'lsdb-hide-buffer)
1347 (add-hook 'wl-summary-toggle-disp-folder-on-hook 'lsdb-hide-buffer)
1348 (add-hook 'wl-summary-toggle-disp-folder-off-hook 'lsdb-hide-buffer)
1349 (add-hook 'wl-summary-toggle-disp-folder-message-resumed-hook
1350 'lsdb-wl-show-buffer)
1351 (add-hook 'wl-exit-hook 'lsdb-mode-save)
1352 (add-hook 'wl-save-hook 'lsdb-mode-save))
1355 (autoload 'wl-message-get-original-buffer "wl-message"))
1356 (defun lsdb-wl-update-record ()
1358 (set-buffer (wl-message-get-original-buffer))
1359 (let ((records (lsdb-update-records)))
1361 (let ((lsdb-temp-buffer-show-function
1362 #'lsdb-wl-temp-buffer-show-function))
1363 (lsdb-display-record (car records)))))))
1365 (defun lsdb-wl-toggle-buffer (&optional arg)
1366 "Toggle hiding of the LSDB window for Wanderlust.
1367 If given a negative prefix, always show; if given a positive prefix,
1370 (list (if current-prefix-arg
1371 (prefix-numeric-value current-prefix-arg)
1373 (let ((lsdb-temp-buffer-show-function
1374 #'lsdb-wl-temp-buffer-show-function))
1375 (lsdb-toggle-buffer arg)))
1377 (defun lsdb-wl-show-buffer ()
1378 (when lsdb-pop-up-windows
1379 (let ((lsdb-temp-buffer-show-function
1380 #'lsdb-wl-temp-buffer-show-function))
1381 (lsdb-show-buffer))))
1383 (defvar wl-current-summary-buffer)
1384 (defvar wl-message-buffer)
1385 (defun lsdb-wl-temp-buffer-show-function (buffer)
1386 (when lsdb-pop-up-windows
1387 (save-selected-window
1388 (let ((window (or (get-buffer-window lsdb-buffer-name)
1392 (if (buffer-live-p wl-current-summary-buffer)
1393 (set-buffer wl-current-summary-buffer))
1394 (get-buffer-window wl-message-buffer))
1395 (get-largest-window)))
1396 (split-window-vertically)))))
1397 (set-window-buffer window buffer)
1398 (lsdb-fit-window-to-buffer window)))))
1400 ;;;_. Interface to Mew written by Hideyuki SHIRAI <shirai@meadowy.org>
1405 ;; Avoid macro `mew-cache-hit' expand (Mew 1.94.2 or earlier).
1406 ;; Changed `mew-cache-hit' from macro to function at Mew 2.0.
1407 (if (not (fboundp 'mew-current-get-fld))
1408 (setq byte-compile-macro-environment
1409 (cons '(mew-cache-hit . nil)
1410 byte-compile-macro-environment))))
1412 ;; Silence byte compiler for environments where Mew does not installed.
1413 (autoload 'mew-sinfo-get-disp-msg "mew")
1414 (autoload 'mew-current-get-fld "mew")
1415 (autoload 'mew-current-get-msg "mew")
1416 (autoload 'mew-frame-id "mew")
1417 (autoload 'mew-cache-hit "mew")
1418 (autoload 'mew-xinfo-get-decode-err "mew")
1419 (autoload 'mew-xinfo-get-action "mew"))))
1422 (defun lsdb-mew-insinuate ()
1423 "Call this function to hook LSDB into Mew."
1424 (add-hook 'mew-message-hook 'lsdb-mew-update-record)
1425 (add-hook 'mew-summary-toggle-disp-msg-hook
1427 (unless (mew-sinfo-get-disp-msg)
1428 (lsdb-hide-buffer))))
1429 (add-hook 'mew-suspend-hook 'lsdb-hide-buffer)
1430 (add-hook 'mew-quit-hook 'lsdb-mode-save)
1431 (add-hook 'kill-emacs-hook 'lsdb-mode-save)
1434 ((fboundp 'mew-summary-visit-folder)
1435 (defadvice mew-summary-visit-folder (before lsdb-hide-buffer activate)
1436 (lsdb-hide-buffer)))
1438 ((fboundp 'mew-summary-switch-to-folder)
1439 (defadvice mew-summary-switch-to-folder (before lsdb-hide-buffer activate)
1440 (lsdb-hide-buffer)))))
1442 (defun lsdb-mew-update-record ()
1443 (let* ((fld (mew-current-get-fld (mew-frame-id)))
1444 (msg (mew-current-get-msg (mew-frame-id)))
1445 (cache (mew-cache-hit fld msg))
1450 (unless (or (mew-xinfo-get-decode-err) (mew-xinfo-get-action))
1451 (make-local-variable 'lsdb-decode-field-body-function)
1452 (setq lsdb-decode-field-body-function
1454 (set-text-properties 0 (length body) nil body)
1456 (when (setq records (lsdb-update-records))
1457 (lsdb-display-record (car records))))))))
1459 ;;;_. Interface to MU-CITE
1461 (autoload 'mu-cite-get-value "mu-cite"))
1463 (defun lsdb-mu-attribution (address)
1464 "Extract attribute information from LSDB."
1466 (lsdb-lookup-records (concat "\\<" address "\\>") 'net)))
1468 (cdr (assq 'attribution (cdr (car records)))))))
1470 (defun lsdb-mu-set-attribution (attribution address)
1471 "Add attribute information to LSDB."
1473 (lsdb-lookup-records (concat "\\<" address "\\>") 'net))
1476 (setq entry (assq 'attribution (cdr (car records))))
1478 (setcdr entry attribution)
1479 (setcdr (car records) (cons (cons 'attribution attribution)
1480 (cdr (car records))))
1481 (lsdb-puthash (car (car records)) (cdr (car records))
1483 (run-hook-with-args 'lsdb-update-record-functions (car records))
1484 (setq lsdb-hash-tables-are-dirty t)))))
1486 (defun lsdb-mu-get-prefix-method ()
1487 "A mu-cite method to return a prefix from LSDB or \">\".
1488 If an `attribution' value is found in LSDB, the value is returned.
1489 Otherwise \">\" is returned."
1490 (or (lsdb-mu-attribution (mu-cite-get-value 'address))
1493 (defvar minibuffer-allow-text-properties)
1495 (defvar lsdb-mu-history nil)
1497 (defun lsdb-mu-get-prefix-register-method ()
1498 "A mu-cite method to return a prefix from LSDB or register it.
1499 If an `attribution' value is found in LSDB, the value is returned.
1500 Otherwise the function requests a prefix from a user. The prefix will
1501 be registered to LSDB if the user wants it."
1502 (let ((address (mu-cite-get-value 'address)))
1503 (or (lsdb-mu-attribution address)
1504 (let* (minibuffer-allow-text-properties
1505 (result (read-string "Citation name? "
1506 (or (mu-cite-get-value 'x-attribution)
1507 (mu-cite-get-value 'full-name))
1509 (if (and (not (string-equal result ""))
1510 (y-or-n-p (format "Register \"%s\"? " result)))
1511 (lsdb-mu-set-attribution result address))
1514 (defun lsdb-mu-get-prefix-register-verbose-method ()
1515 "A mu-cite method to return a prefix using LSDB.
1517 In this method, a user must specify a prefix unconditionally. If an
1518 `attribution' value is found in LSDB, the value is used as a initial
1519 value to input the prefix. The prefix will be registered to LSDB if
1521 (let* ((address (mu-cite-get-value 'address))
1522 (attribution (lsdb-mu-attribution address))
1523 minibuffer-allow-text-properties
1524 (result (read-string "Citation name? "
1526 (mu-cite-get-value 'x-attribution)
1527 (mu-cite-get-value 'full-name))
1529 (if (and (not (string-equal result ""))
1530 (not (string-equal result attribution))
1531 (y-or-n-p (format "Register \"%s\"? " result)))
1532 (lsdb-mu-set-attribution result address))
1535 (defvar mu-cite-methods-alist)
1537 (defun lsdb-mu-insinuate ()
1538 (add-hook 'mu-cite-instantiation-hook
1540 (setq mu-cite-methods-alist
1542 mu-cite-methods-alist
1545 #'lsdb-mu-get-prefix-method)
1546 (cons 'lsdb-prefix-register
1547 #'lsdb-mu-get-prefix-register-method)
1548 (cons 'lsdb-prefix-register-verbose
1549 #'lsdb-mu-get-prefix-register-verbose-method)))))))
1551 ;;;_. X-Face Rendering
1552 (defvar lsdb-x-face-cache
1553 (lsdb-make-hash-table :test 'equal))
1555 (defun lsdb-x-face-available-image-type ()
1556 (static-if (featurep 'xemacs)
1559 (and (>= emacs-major-version 21)
1560 (fboundp 'image-type-available-p)
1561 (if (image-type-available-p 'pbm)
1563 (if (image-type-available-p 'xpm)
1566 (defun lsdb-expose-x-face ()
1567 (let* ((record (get-text-property (point-min) 'lsdb-record))
1568 (x-face (cdr (assq 'x-face (cdr record))))
1570 (when (and lsdb-insert-x-face-function
1572 (goto-char (point-min))
1574 (put-text-property 0 1 'invisible t delimiter) ;hide "\r"
1580 (funcall lsdb-insert-x-face-function (pop x-face)))
1582 'lsdb-record record))))
1584 (defun lsdb-insert-x-face-image (data type marker)
1585 (static-if (featurep 'xemacs)
1587 (set-buffer (marker-buffer marker))
1589 (let* ((inhibit-read-only t)
1591 (glyph (make-glyph (vector type :data data))))
1592 (set-extent-begin-glyph
1593 (make-extent (point) (point))
1596 (set-buffer (marker-buffer marker))
1598 (let* ((inhibit-read-only t)
1600 (image (create-image data type t :ascent 'center))
1601 (record (get-text-property (point) 'lsdb-record)))
1602 (put-text-property (point) (progn
1603 (insert-image image)
1605 'lsdb-record record)))))
1607 (defun lsdb-insert-x-face-asynchronously (x-face)
1608 (let* ((type (or lsdb-x-face-image-type
1609 (lsdb-x-face-available-image-type)))
1610 (shell-file-name lsdb-shell-file-name)
1611 (shell-command-switch lsdb-shell-command-switch)
1612 (process-connection-type nil)
1613 (cached (cdr (assq type (lsdb-gethash x-face lsdb-x-face-cache))))
1614 (marker (point-marker))
1617 (lsdb-insert-x-face-image cached type marker)
1619 (start-process-shell-command
1620 "lsdb-x-face-command" (generate-new-buffer " *lsdb work*")
1622 (nth 1 (assq type lsdb-x-face-command-alist))
1623 "; } 2> /dev/null")))
1624 (process-send-string process (concat x-face "\n"))
1625 (process-send-eof process)
1626 (set-process-sentinel
1628 `(lambda (process string)
1630 (when (and (buffer-live-p (marker-buffer ,marker))
1631 (equal string "finished\n"))
1633 (with-current-buffer (process-buffer process)
1634 (set-buffer-multibyte nil)
1636 (lsdb-insert-x-face-image data ',type ,marker)
1637 (lsdb-puthash ,x-face (list (cons ',type data))
1638 lsdb-x-face-cache)))
1639 (kill-buffer (process-buffer process))))))))
1644 (product-provide 'lsdb
1645 (product-define "LSDB" nil '(0 9)))
1647 ;;;_* Local emacs vars.
1648 ;;; The following `outline-layout' local variable setting:
1649 ;;; - closes all topics from the first topic to just before the third-to-last,
1650 ;;; - shows the children of the third to last (config vars)
1651 ;;; - and the second to last (code section),
1652 ;;; - and closes the last topic (this local-variables section).
1654 ;;;outline-layout: (0 : -1 -1 0)
1657 ;;; lsdb.el ends here