1 ;;; lsdb.el --- the Lovely Sister Database
3 ;; Copyright (C) 2002 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: adress book
8 ;; This file is part of the Lovely Sister Database.
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
28 ;;; (autoload 'lsdb-gnus-insinuate "lsdb")
29 ;;; (autoload 'lsdb-gnus-insinuate-message "lsdb")
30 ;;; (add-hook 'gnus-startup-hook 'lsdb-gnus-insinuate)
31 ;;; (add-hook 'message-setup-hook
33 ;;; (define-key message-mode-map "\M-\t" 'lsdb-complete-name)))
35 ;;; For Wanderlust, put the following lines into your ~/.wl:
37 ;;; (lsdb-wl-insinuate)
38 ;;; (add-hook 'wl-draft-mode-hook
40 ;;; (define-key wl-draft-mode-map "\M-\t" 'lsdb-complete-name)))
42 ;;; For Mew, put the following lines into your ~/.mew:
43 ;;; (autoload 'lsdb-mew-insinuate "lsdb")
44 ;;; (add-hook 'mew-init-hook 'lsdb-mew-insinuate)
45 ;;; (add-hook 'mew-draft-mode-hook
47 ;;; (define-key mew-draft-header-map "\M-I" 'lsdb-complete-name)))
56 ;;;_* USER CUSTOMIZATION VARIABLES:
58 "The Lovely Sister Database."
62 (defcustom lsdb-file (expand-file-name "~/.lsdb")
63 "The name of the Lovely Sister Database file."
67 (defcustom lsdb-file-coding-system (find-coding-system 'iso-2022-jp)
68 "Coding system for `lsdb-file'."
72 (defcustom lsdb-sender-headers
74 "List of headers to search for senders."
78 (defcustom lsdb-recipients-headers
79 "Resent-To\\|Resent-Cc\\|Reply-To\\|To\\|Cc\\|Bcc"
80 "List of headers to search for recipients."
84 (defcustom lsdb-interesting-header-alist
85 '(("Organization" nil organization)
86 ("\\(X-\\)?User-Agent\\|X-Mailer\\|X-Newsreader" nil user-agent)
87 ("\\(X-\\)?ML-Name" nil mailing-list)
88 ("List-Id" "\\(.*\\)[ \t]+<[^>]+>\\'" mailing-list "\\1")
89 ("X-Sequence" "\\(.*\\)[ \t]+[0-9]+\\'" mailing-list "\\1")
90 ("Delivered-To" "mailing list[ \t]+\\([^@]+\\)@.*" mailing-list "\\1")
91 ("\\(X-URL\\|X-URI\\)" nil www)
92 ("X-Attribution\\|X-cite-me" nil attribution)
93 ("X-Face" nil x-face))
94 "Alist of headers we are interested in.
95 The format of elements of this list should be
96 (FIELD-NAME REGEXP ENTRY STRING)
97 where the last three elements are optional."
101 (defcustom lsdb-entry-type-alist
103 (creation-date 2 ?. t)
104 (last-modified 3 ?. t)
111 "Alist of entry types for presentation.
112 The format of elements of this list should be
113 (ENTRY SCORE [CLASS READ-ONLY])
114 where the last two elements are optional.
115 Possible values for CLASS are `?.' and '?,'. If CLASS is `?.', the
116 entry takes a unique value which is overridden by newly assigned one
117 by `lsdb-mode-edit-entry' or such a command. If CLASS is `?,', the
118 entry can have multiple values separated by commas.
119 If the fourth element READ-ONLY is non-nil, it is assumed that the
120 entry cannot be modified."
124 (defcustom lsdb-decode-field-body-function #'lsdb-decode-field-body
125 "Field body decoder."
129 (defcustom lsdb-canonicalize-full-name-function
130 #'lsdb-canonicalize-spaces-and-dots
131 "Way to canonicalize full name."
135 (defcustom lsdb-window-max-height 7
136 "Maximum number of lines used to display LSDB record."
140 (defcustom lsdb-x-face-command-alist
141 '((pbm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale 0.5")
142 (xpm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale 0.5 | ppmtoxpm"))
143 "An alist from an image type to a command to be executed to display an X-Face header.
144 The command will be executed in a sub-shell asynchronously.
145 The compressed face will be piped to this command."
149 (defcustom lsdb-insert-x-face-function
150 (if (static-if (featurep 'xemacs)
152 (and (>= emacs-major-version 21)
153 (fboundp 'image-type-available-p)
154 (or (image-type-available-p 'pbm)
155 (image-type-available-p 'xpm))))
156 #'lsdb-insert-x-face-asynchronously)
157 "Function to display X-Face."
161 (defcustom lsdb-print-record-hook '(lsdb-expose-x-face)
162 "A hook called after a record is displayed."
166 (defcustom lsdb-display-records-sort-predicate nil
167 "A predicate to sort records."
171 (defgroup lsdb-edit-form nil
172 "A mode for editing forms."
175 (defcustom lsdb-edit-form-mode-hook nil
176 "Hook run in `lsdb-edit-form-mode' buffers."
177 :group 'lsdb-edit-form
180 (defcustom lsdb-shell-file-name "/bin/sh"
181 "File name to load inferior shells from.
182 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
186 (defcustom lsdb-shell-command-switch "-c"
187 "Switch used to have the shell execute its command line argument."
192 (defface lsdb-header-face
193 '((t (:underline t)))
194 "Face for the file header line in `lsdb-mode'."
196 (defvar lsdb-header-face 'lsdb-header-face)
198 (defface lsdb-field-name-face
199 '((((class color) (background dark))
200 (:foreground "PaleTurquoise" :bold t))
202 "Face for the message header line in `lsdb-mode'."
204 (defvar lsdb-field-name-face 'lsdb-field-name-face)
206 (defface lsdb-field-body-face
207 '((((class color) (background dark))
208 (:foreground "turquoise" :italic t))
210 "Face for the message header line in `lsdb-mode'."
212 (defvar lsdb-field-body-face 'lsdb-field-body-face)
214 (defconst lsdb-font-lock-keywords
216 (0 lsdb-header-face))
218 (0 lsdb-field-body-face))
219 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
220 (1 lsdb-field-name-face)
221 (2 lsdb-field-body-face))))
223 (put 'lsdb-mode 'font-lock-defaults '(lsdb-font-lock-keywords t))
225 ;;;_* CODE - no user customizations below
226 ;;;_. Internal Variables
227 (defvar lsdb-hash-table nil
228 "Internal hash table to hold LSDB records.")
230 (defvar lsdb-buffer-name "*LSDB*"
231 "Buffer name to display LSDB record.")
233 (defvar lsdb-hash-table-is-dirty nil
234 "Flag to indicate whether the hash table needs to be saved.")
236 (defvar lsdb-known-entry-names
238 "An obarray used to complete an entry name.")
240 ;;;_. Hash Table Emulation
241 (if (and (fboundp 'make-hash-table)
242 (subrp (symbol-function 'make-hash-table)))
244 (defalias 'lsdb-puthash 'puthash)
245 (defalias 'lsdb-gethash 'gethash)
246 (defalias 'lsdb-remhash 'remhash)
247 (defalias 'lsdb-maphash 'maphash)
248 (defalias 'lsdb-hash-table-size 'hash-table-size)
249 (defalias 'lsdb-hash-table-count 'hash-table-count)
250 (defalias 'lsdb-make-hash-table 'make-hash-table))
251 (defun lsdb-puthash (key value hash-table)
252 "Hash KEY to VALUE in HASH-TABLE."
253 ;; Obarray is regarded as an open hash table, as a matter of
254 ;; fact, rehashing doesn't make sense.
256 (when (> (car hash-table)
257 (* (length (nth 1 hash-table)) 0.7))
258 (setq new-obarray (make-vector (* (length (nth 1 hash-table)) 2) 0))
261 (set (intern (symbol-name symbol) new-obarray)
262 (symbol-value symbol)))
264 (setcdr hash-table (list new-obarray)))
265 (set (intern key (nth 1 hash-table)) value)
266 (setcar hash-table (1+ (car hash-table)))))
267 (defun lsdb-gethash (key hash-table &optional default)
268 "Find hash value for KEY in HASH-TABLE.
269 If there is no corresponding value, return DEFAULT (which defaults to nil)."
270 (let ((symbol (intern-soft key (nth 1 hash-table))))
272 (symbol-value symbol)
274 (defun lsdb-remhash (key hash-table)
275 "Remove the entry for KEY from HASH-TABLE.
276 Do nothing if there is no entry for KEY in HASH-TABLE."
277 (unintern key (nth 1 hash-table))
278 (setcar hash-table (1- (car hash-table))))
279 (defun lsdb-maphash (function hash-table)
280 "Map FUNCTION over entries in HASH-TABLE, calling it with two args,
281 each key and value in HASH-TABLE.
283 FUNCTION may not modify HASH-TABLE, with the one exception that FUNCTION
284 may remhash or puthash the entry currently being processed by FUNCTION."
287 (funcall function (symbol-name symbol) (symbol-value symbol)))
289 (defun lsdb-hash-table-size (hash-table)
290 "Return the size of HASH-TABLE.
291 This is the current number of slots in HASH-TABLE, whether occupied or not."
292 (length (nth 1 hash-table)))
293 (defalias 'lsdb-hash-table-count 'car)
294 (defun lsdb-make-hash-table (&rest args)
295 "Return a new empty hash table object."
296 (list 0 (make-vector (or (plist-get args :size) 29) 0))))
298 ;;;_. Hash Table Reader/Writer
302 ;; In XEmacs, hash tables can also be created by the lisp reader
303 ;; using structure syntax.
304 (read-from-string "#s(hash-table)")
305 (defun lsdb-load-file (file)
306 "Read the contents of FILE into a hash table."
307 (let ((buffer (find-file-noselect file)))
311 (re-search-forward "^#s")
313 (read (point-min-marker)))
314 (kill-buffer buffer)))))
316 (defun lsdb-load-file (file)
317 "Read the contents of FILE into a hash table."
320 (insert-file-contents file)
322 (re-search-forward "^#s")
325 (cdr (read (point-marker))))))
326 (size (plist-get plist 'size))
327 (data (plist-get plist 'data))
328 (hash-table (lsdb-make-hash-table :size size :test 'equal)))
330 (lsdb-puthash (pop data) (pop data) hash-table))
333 (defun lsdb-save-file (file hash-table)
334 "Write the entries within HASH-TABLE into FILE."
335 (let ((coding-system-for-write lsdb-file-coding-system))
337 (if (and (or (featurep 'mule)
338 (featurep 'file-coding))
339 lsdb-file-coding-system)
340 (let ((coding-system-name
341 (if (symbolp lsdb-file-coding-system)
342 (symbol-name lsdb-file-coding-system)
344 (static-if (featurep 'xemacs)
345 (symbol-name (coding-system-name
346 lsdb-file-coding-system))))))
347 (if coding-system-name
348 (insert ";;; -*- coding: " coding-system-name " -*-\n"))))
349 (insert "#s(hash-table size "
350 ;; reduce the actual size of the close hash table
351 (number-to-string (lsdb-hash-table-count hash-table))
352 " test equal data (")
355 (insert (prin1-to-string key) " " (prin1-to-string value) " "))
359 ;;;_. Mail Header Extraction
360 (defun lsdb-fetch-field-bodies (regexp)
362 (goto-char (point-min))
363 (let ((case-fold-search t)
365 (while (re-search-forward (concat "^\\(" regexp "\\):[ \t]*")
367 (push (funcall lsdb-decode-field-body-function
368 (buffer-substring (point) (std11-field-end))
371 (nreverse field-bodies))))
373 (defun lsdb-canonicalize-spaces-and-dots (string)
374 (while (string-match " +\\|[\f\t\n\r\v]+\\|\\." string)
375 (setq string (replace-match " " nil t string)))
378 (defun lsdb-extract-address-components (string)
379 (let ((components (std11-extract-address-components string)))
380 (if (nth 1 components)
382 (list (funcall lsdb-canonicalize-full-name-function
385 (list (nth 1 components) (nth 1 components))))))
387 ;; stolen (and renamed) from nnheader.el
388 (defun lsdb-decode-field-body (field-body field-name
389 &optional mode max-column)
390 (let ((multibyte enable-multibyte-characters))
393 (set-buffer-multibyte t)
394 (mime-decode-field-body field-body
395 (if (stringp field-name)
396 (intern (capitalize field-name))
399 (set-buffer-multibyte multibyte))))
401 ;;;_. Record Management
402 (defun lsdb-maybe-load-file ()
403 (unless lsdb-hash-table
404 (if (file-exists-p lsdb-file)
405 (setq lsdb-hash-table (lsdb-load-file lsdb-file))
406 (setq lsdb-hash-table (lsdb-make-hash-table :test 'equal)))))
408 (defun lsdb-update-record (sender &optional interesting)
409 (let ((old (lsdb-gethash (car sender) lsdb-hash-table))
410 (new (cons (cons 'net (list (nth 1 sender)))
415 (setq new (cons (cons 'creation-date (format-time-string "%Y-%m-%d"))
417 (setq merged (lsdb-merge-record-entries old new)
418 record (cons (car sender) merged))
419 (unless (equal merged old)
420 (let ((entry (assq 'last-modified (cdr record)))
421 (last-modified (format-time-string "%Y-%m-%d")))
423 (setcdr entry last-modified)
424 (setcdr record (cons (cons 'last-modified last-modified)
426 (lsdb-puthash (car record) (cdr record)
428 (setq lsdb-hash-table-is-dirty t))
431 (defun lsdb-update-records ()
432 (lsdb-maybe-load-file)
433 (let (senders recipients interesting alist records bodies entry)
435 (std11-narrow-to-header)
437 (delq nil (mapcar #'lsdb-extract-address-components
438 (lsdb-fetch-field-bodies
439 lsdb-sender-headers)))
441 (delq nil (mapcar #'lsdb-extract-address-components
442 (lsdb-fetch-field-bodies
443 lsdb-recipients-headers))))
444 (setq alist lsdb-interesting-header-alist)
449 (if (nth 1 (car alist))
450 (and (string-match (nth 1 (car alist))
452 (replace-match (nth 3 (car alist))
455 (lsdb-fetch-field-bodies (car (car alist))))))
457 (setq entry (or (nth 2 (car alist))
460 (if (eq ?. (nth 2 (assq entry lsdb-entry-type-alist)))
464 (setq alist (cdr alist))))
466 (setq records (list (lsdb-update-record (pop senders) interesting))))
467 (setq alist (nconc senders recipients))
469 (setq records (cons (lsdb-update-record (pop alist)) records)))
472 (defun lsdb-merge-record-entries (old new)
473 (setq old (copy-sequence old))
475 (let ((entry (assq (car (car new)) old))
478 (setq old (nconc old (list (car new))))
479 (if (listp (cdr entry))
481 (setq list (cdr (car new)) pointer list)
483 (if (member (car pointer) (cdr entry))
484 (setq list (delq (car pointer) list)))
485 (setq pointer (cdr pointer)))
486 (setcdr entry (nconc (cdr entry) list)))
487 (setcdr entry (cdr (car new))))))
488 (setq new (cdr new)))
491 ;;;_. Display Management
492 (defun lsdb-temp-buffer-show-function (buffer)
493 (save-selected-window
494 (let ((window (or (get-buffer-window lsdb-buffer-name)
496 (select-window (get-largest-window))
497 (split-window-vertically))))
499 (set-window-buffer window buffer)
500 (select-window window)
501 (unless (pos-visible-in-window-p (point-max))
502 (enlarge-window (- lsdb-window-max-height (window-height))))
503 (shrink-window-if-larger-than-buffer)
504 (if (> (setq height (window-height))
505 lsdb-window-max-height)
506 (shrink-window (- height lsdb-window-max-height)))
507 (set-window-start window (point-min)))))
509 (defun lsdb-display-record (record)
510 "Display only one RECORD, then shrink the window as possible."
511 (let ((temp-buffer-show-function
512 (function lsdb-temp-buffer-show-function)))
513 (lsdb-display-records (list record))))
515 (defun lsdb-display-records (records)
516 (with-output-to-temp-buffer lsdb-buffer-name
517 (set-buffer standard-output)
519 (sort (copy-sequence records)
520 (or lsdb-display-records-sort-predicate
521 (lambda (record1 record2)
522 (string-lessp (car record1) (car record2))))))
525 (narrow-to-region (point) (point))
526 (lsdb-print-record (car records)))
527 (goto-char (point-max))
528 (setq records (cdr records)))
531 (defsubst lsdb-entry-score (entry)
532 (or (nth 1 (assq (car entry) lsdb-entry-type-alist)) 0))
534 (defun lsdb-insert-entry (entry)
535 (let ((entry-name (capitalize (symbol-name (car entry)))))
536 (intern entry-name lsdb-known-entry-names)
537 (if (>= (lsdb-entry-score entry) 0)
538 (insert "\t" entry-name ": "
539 (if (listp (cdr entry))
541 #'identity (cdr entry)
542 (if (eq ?, (nth 2 (assq (car entry)
543 lsdb-entry-type-alist)))
549 (defun lsdb-print-record (record)
550 (insert (car record) "\n")
552 (sort (copy-sequence (cdr record))
553 (lambda (entry1 entry2)
554 (> (lsdb-entry-score entry1) (lsdb-entry-score entry2))))))
556 (lsdb-insert-entry (car entries))
557 (setq entries (cdr entries))))
558 (add-text-properties (point-min) (point-max)
559 (list 'lsdb-record record))
560 (run-hooks 'lsdb-print-record-hook))
563 (defvar lsdb-last-completion nil)
564 (defvar lsdb-last-candidates nil)
565 (defvar lsdb-last-candidates-pointer nil)
567 (defun lsdb-complete-name ()
568 "Complete the user full-name or net-address before point"
570 (lsdb-maybe-load-file)
573 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
574 (goto-char (match-end 0))
578 (completion-ignore-case t))
579 (unless (eq last-command this-command)
580 (setq lsdb-last-candidates nil
581 lsdb-last-candidates-pointer nil
582 lsdb-last-completion (buffer-substring start (point))
583 pattern (concat "\\<" lsdb-last-completion))
586 (let ((net (cdr (assq 'net value))))
587 (if (string-match pattern key)
588 (setq lsdb-last-candidates
589 (nconc lsdb-last-candidates
590 (mapcar (lambda (address)
591 (if (equal key address)
593 (concat key " <" address ">")))
596 (if (string-match pattern (car net))
597 (push (car net) lsdb-last-candidates))
598 (setq net (cdr net))))))
600 ;; Sort candidates by the position where the pattern occurred.
601 (setq lsdb-last-candidates
602 (sort lsdb-last-candidates
603 (lambda (cand1 cand2)
604 (< (if (string-match pattern cand1)
606 (if (string-match pattern cand2)
607 (match-beginning 0)))))))
608 (unless lsdb-last-candidates-pointer
609 (setq lsdb-last-candidates-pointer lsdb-last-candidates))
610 (when lsdb-last-candidates-pointer
611 (delete-region start (point))
612 (insert (pop lsdb-last-candidates-pointer)))))
614 ;;;_. Major Mode (`lsdb-mode') Implementation
615 ;;;_ : Modeline Buffer Identification
616 (defconst lsdb-pointer-xpm
618 static char * lsdb_pointer_xpm[] = {
640 (static-if (featurep 'xemacs)
642 (defvar lsdb-xemacs-modeline-left-extent
643 (copy-extent modeline-buffer-id-left-extent))
645 (defvar lsdb-xemacs-modeline-right-extent
646 (copy-extent modeline-buffer-id-right-extent))
648 (defun lsdb-modeline-buffer-identification (line)
649 "Decorate 1st element of `mode-line-buffer-identification' LINE.
650 Modify whole identification by side effect."
651 (let ((id (car line)) chopped)
652 (if (and (stringp id) (string-match "^LSDB:" id))
654 (setq chopped (substring id 0 (match-end 0))
655 id (substring id (match-end 0)))
662 (list (vector 'xpm :data lsdb-pointer-xpm)))
663 (list (vector 'string :data chopped))))))
664 (set-glyph-face glyph 'modeline-buffer-id)
665 (cons lsdb-xemacs-modeline-left-extent glyph))
666 (cons lsdb-xemacs-modeline-right-extent id))
672 (defun lsdb-modeline-buffer-identification (line)
673 "Decorate 1st element of `mode-line-buffer-identification' LINE.
674 Modify whole identification by side effect."
675 (let ((id (copy-sequence (car line)))
677 (if (image-type-available-p 'xpm)
678 (create-image lsdb-pointer-xpm 'xpm t :ascent 'center))))
680 (stringp id) (string-match "^LSDB:" id))
681 (add-text-properties 0 (length id)
683 'rear-nonsticky (list 'display))
688 (defalias 'lsdb-modeline-buffer-identification 'identity))))
690 (defvar lsdb-mode-map
691 (let ((keymap (make-sparse-keymap)))
692 (define-key keymap "a" 'lsdb-mode-add-entry)
693 (define-key keymap "d" 'lsdb-mode-delete-entry)
694 (define-key keymap "e" 'lsdb-mode-edit-entry)
695 (define-key keymap "s" 'lsdb-mode-save)
696 (define-key keymap "q" 'lsdb-mode-quit-window)
697 (define-key keymap "g" 'lsdb-mode-lookup)
698 (define-key keymap "p" 'lsdb-mode-previous-record)
699 (define-key keymap "n" 'lsdb-mode-next-record)
700 (define-key keymap " " 'scroll-up)
701 (define-key keymap [delete] 'scroll-down)
702 (define-key keymap "\177" 'scroll-down)
703 (define-key keymap [backspace] 'scroll-down)
707 (defvar lsdb-modeline-string "")
709 (define-derived-mode lsdb-mode fundamental-mode "LSDB"
710 "Major mode for browsing LSDB records."
711 (setq buffer-read-only t)
712 (static-if (featurep 'xemacs)
713 ;; In XEmacs, setting `font-lock-defaults' only affects on
714 ;; `find-file-hooks'.
715 (font-lock-set-defaults)
716 (set (make-local-variable 'font-lock-defaults)
717 '(lsdb-font-lock-keywords t)))
718 (make-local-variable 'post-command-hook)
719 (setq post-command-hook 'lsdb-modeline-update)
720 (make-local-variable 'lsdb-modeline-string)
721 (setq mode-line-buffer-identification
722 (lsdb-modeline-buffer-identification
723 '("LSDB: " lsdb-modeline-string)))
724 (lsdb-modeline-update)
725 (force-mode-line-update))
727 (defun lsdb-modeline-update ()
729 (get-text-property (if (eobp) (point-min) (point)) 'lsdb-record))
733 (setq net (car (cdr (assq 'net (cdr record)))))
734 (if (equal net (car record))
735 (setq lsdb-modeline-string net)
736 (setq lsdb-modeline-string (concat (car record) " <" net ">"))))
737 (setq lsdb-modeline-string ""))))
739 (defun lsdb-narrow-to-record ()
740 "Narrow to the current record."
741 (let ((end (next-single-property-change (point) 'lsdb-record nil
744 (previous-single-property-change (point) 'lsdb-record nil (point-min))
746 (goto-char (point-min))))
748 (defun lsdb-current-record ()
749 "Return the current record name."
750 (let ((record (get-text-property (point) 'lsdb-record)))
752 (error "There is nothing to follow here"))
755 (defun lsdb-current-entry ()
756 "Return the current entry name.
757 If the point is not on a entry line, it prompts to select a entry in
761 (if (looking-at "^[^\t]")
762 (let ((record (lsdb-current-record))
763 (completion-ignore-case t))
765 "Which entry to modify: "
766 (mapcar (lambda (entry)
767 (list (capitalize (symbol-name (car entry)))))
770 (re-search-backward "^\t\\([^\t][^:]+\\):")
773 (defun lsdb-mode-add-entry (entry-name)
774 "Add an entry on the current line."
776 (let ((completion-ignore-case t))
777 (list (completing-read "Entry name: " lsdb-known-entry-names))))
779 (unless (symbolp entry-name)
780 (setq entry-name (intern (downcase entry-name))))
781 (when (assq entry-name (cdr (lsdb-current-record)))
782 (error "The entry already exists"))
783 (let ((marker (point-marker)))
785 nil "Editing the entry."
789 (set-buffer lsdb-buffer-name)
791 (let ((record (lsdb-current-record))
792 (inhibit-read-only t)
794 (setcdr record (cons (cons ',entry-name form) (cdr record)))
795 (lsdb-puthash (car record) (cdr record)
797 (setq lsdb-hash-table-is-dirty t)
798 (beginning-of-line 2)
802 (lsdb-insert-entry (cons ',entry-name form))
804 (list 'lsdb-record record)))))))))
806 (defun lsdb-mode-delete-entry (&optional entry-name dont-update)
807 "Delete the entry on the current line."
809 (let ((record (lsdb-current-record))
812 (setq entry-name (lsdb-current-entry)))
813 (setq entry (assq (intern (downcase entry-name)) (cdr record)))
816 (setcdr record (delq entry (cdr record)))
817 (lsdb-puthash (car record) (cdr record)
819 (setq lsdb-hash-table-is-dirty t))
821 (lsdb-narrow-to-record)
822 (let ((case-fold-search t)
823 (inhibit-read-only t)
825 (goto-char (point-min))
826 (if (re-search-forward
827 (concat "^\t" (or entry-name
828 (lsdb-current-entry))
831 (delete-region (match-beginning 0)
832 (if (re-search-forward
833 "^\t[^\t][^:]+:" nil t)
837 (defun lsdb-mode-edit-entry ()
838 "Edit the entry on the current line."
840 (let* ((record (lsdb-current-record))
841 (entry-name (intern (downcase (lsdb-current-entry))))
842 (entry (assq entry-name (cdr record)))
843 (marker (point-marker)))
845 (cdr entry) "Editing the entry."
847 (unless (equal form ',(cdr entry))
849 (set-buffer lsdb-buffer-name)
851 (let* ((record (lsdb-current-record))
852 (entry (assq ',entry-name (cdr record)))
853 (inhibit-read-only t)
856 (setq lsdb-hash-table-is-dirty t)
857 (lsdb-mode-delete-entry (symbol-name ',entry-name) t)
862 (lsdb-insert-entry (cons ',entry-name form))
864 (list 'lsdb-record record)))))))))
866 (defun lsdb-mode-save (&optional dont-ask)
867 "Save LSDB hash table into `lsdb-file'."
869 (if (not lsdb-hash-table-is-dirty)
870 (message "(No changes need to be saved)")
871 (when (or (interactive-p)
873 (y-or-n-p "Save the LSDB now?"))
874 (lsdb-save-file lsdb-file lsdb-hash-table)
875 (setq lsdb-hash-table-is-dirty nil)
876 (message "The LSDB was saved successfully."))))
878 (defun lsdb-mode-quit-window (&optional kill window)
879 "Quit the current buffer.
880 It partially emulates the GNU Emacs' of `quit-window'."
883 (setq window (selected-window)))
884 (let ((buffer (window-buffer window)))
885 (unless (save-selected-window
886 (select-window window)
888 (delete-window window))
891 (bury-buffer buffer))))
893 (defun lsdb-mode-hide-buffer ()
894 "Hide the LSDB window."
895 (let ((window (get-buffer-window lsdb-buffer-name)))
897 (lsdb-mode-quit-window nil window))))
899 (defun lsdb-lookup-records (regexp &optional entry-name)
900 "Return the all records in the LSDB matching the REGEXP.
901 If the optional 2nd argument ENTRY-NAME is given, matching only
902 performed against the entry field."
907 (unless (symbolp entry-name)
908 (setq entry-name (intern (downcase entry-name))))
910 (let ((entry (cdr (assq entry-name value)))
912 (unless (listp entry)
913 (setq entry (list entry)))
914 (while (and (not found) entry)
915 (if (string-match regexp (pop entry))
918 (push (cons key value) records)))))
920 (if (string-match regexp key)
921 (push (cons key value) records))))
925 (defvar lsdb-mode-lookup-history nil)
927 (defun lsdb-mode-lookup (regexp &optional entry-name)
928 "Display the all records in the LSDB matching the REGEXP.
929 If the optional 2nd argument ENTRY-NAME is given, matching only
930 performed against the entry field."
932 (let* ((completion-ignore-case t)
934 (if current-prefix-arg
935 (completing-read "Entry name: "
936 lsdb-known-entry-names))))
938 (read-from-minibuffer
940 (format "Search records `%s' regexp: " entry-name)
941 "Search records regexp: ")
942 nil nil nil 'lsdb-mode-lookup-history)
944 (lsdb-maybe-load-file)
945 (let ((records (lsdb-lookup-records regexp entry-name)))
947 (lsdb-display-records records))))
950 (defalias 'lsdb 'lsdb-mode-lookup)
952 (defun lsdb-mode-next-record (&optional arg)
953 "Go to the next record."
955 (unless arg ;called noninteractively?
958 (lsdb-mode-previous-record (- arg))
960 (goto-char (next-single-property-change
961 (point) 'lsdb-record nil (point-max)))
962 (setq arg (1- arg)))))
964 (defun lsdb-mode-previous-record (&optional arg)
965 "Go to the previous record."
967 (unless arg ;called noninteractively?
970 (lsdb-mode-next-record (- arg))
972 (goto-char (previous-single-property-change
973 (point) 'lsdb-record nil (point-min)))
974 (setq arg (1- arg)))))
976 ;;;_ : Edit Forms -- stolen (and renamed) from gnus-eform.el
977 (defvar lsdb-edit-form-buffer "*LSDB edit form*")
978 (defvar lsdb-edit-form-done-function nil)
979 (defvar lsdb-previous-window-configuration nil)
981 (defvar lsdb-edit-form-mode-map
982 (let ((keymap (make-sparse-keymap)))
983 (set-keymap-parent keymap emacs-lisp-mode-map)
984 (define-key keymap "\C-c\C-c" 'lsdb-edit-form-done)
985 (define-key keymap "\C-c\C-k" 'lsdb-edit-form-exit)
987 "Edit form's keymap.")
989 (defun lsdb-edit-form-mode ()
990 "Major mode for editing forms.
991 It is a slightly enhanced emacs-lisp-mode.
993 \\{lsdb-edit-form-mode-map}"
995 (kill-all-local-variables)
996 (setq major-mode 'lsdb-edit-form-mode
997 mode-name "LSDB Edit Form")
998 (use-local-map lsdb-edit-form-mode-map)
999 (make-local-variable 'lsdb-edit-form-done-function)
1000 (make-local-variable 'lsdb-previous-window-configuration)
1001 (run-hooks 'lsdb-edit-form-mode-hook))
1003 (defun lsdb-edit-form (form documentation exit-func)
1004 "Edit FORM in a new buffer.
1005 Call EXIT-FUNC on exit. Display DOCUMENTATION in the beginning
1007 (let ((window-configuration
1008 (current-window-configuration)))
1009 (switch-to-buffer (get-buffer-create lsdb-edit-form-buffer))
1010 (lsdb-edit-form-mode)
1011 (setq lsdb-previous-window-configuration window-configuration
1012 lsdb-edit-form-done-function exit-func)
1014 (insert documentation)
1017 (goto-char (point-min))
1021 (insert ";; Type `C-c C-c' after you've finished editing.\n")
1024 (pp form (current-buffer))
1028 (defun lsdb-edit-form-done ()
1029 "Update changes and kill the current buffer."
1031 (goto-char (point-min))
1032 (let ((form (condition-case nil
1033 (read (current-buffer))
1035 (func lsdb-edit-form-done-function))
1036 (lsdb-edit-form-exit)
1037 (funcall func form)))
1039 (defun lsdb-edit-form-exit ()
1040 "Kill the current buffer."
1042 (let ((window-configuration lsdb-previous-window-configuration))
1043 (kill-buffer (current-buffer))
1044 (set-window-configuration window-configuration)))
1046 ;;;_. Interface to Semi-gnus
1048 (defun lsdb-gnus-insinuate ()
1049 "Call this function to hook LSDB into Semi-gnus."
1050 (add-hook 'gnus-article-prepare-hook 'lsdb-gnus-update-record)
1051 (add-hook 'gnus-save-newsrc-hook 'lsdb-mode-save))
1053 (defvar gnus-current-headers)
1054 (defun lsdb-gnus-update-record ()
1055 (let ((entity gnus-current-headers)
1058 (set-buffer-multibyte nil)
1059 (buffer-disable-undo)
1060 (mime-insert-entity entity)
1061 (setq records (lsdb-update-records))
1063 (lsdb-display-record (car records))))))
1065 ;;;_. Interface to Wanderlust
1067 (defun lsdb-wl-insinuate ()
1068 "Call this function to hook LSDB into Wanderlust."
1069 (add-hook 'wl-message-redisplay-hook 'lsdb-wl-update-record)
1070 (add-hook 'wl-summary-exit-hook 'lsdb-mode-hide-buffer)
1071 (add-hook 'wl-exit-hook 'lsdb-mode-save)
1072 (add-hook 'wl-save-hook 'lsdb-mode-save))
1075 (autoload 'wl-message-get-original-buffer "wl-message"))
1076 (defun lsdb-wl-update-record ()
1078 (set-buffer (wl-message-get-original-buffer))
1079 (let ((records (lsdb-update-records)))
1081 (lsdb-display-record (car records))))))
1083 ;;;_. Interface to Mew written by Hideyuki SHIRAI <shirai@rdmg.mgcs.mei.co.jp>
1085 (autoload 'mew-sinfo-get-disp-msg "mew")
1086 (autoload 'mew-current-get-fld "mew")
1087 (autoload 'mew-current-get-msg "mew")
1088 (autoload 'mew-frame-id "mew")
1089 (autoload 'mew-cache-hit "mew"))
1092 (defun lsdb-mew-insinuate ()
1093 "Call this function to hook LSDB into Mew."
1094 (add-hook 'mew-message-hook 'lsdb-mew-update-record)
1095 (add-hook 'mew-summary-toggle-disp-msg-hook
1097 (unless (mew-sinfo-get-disp-msg)
1098 (lsdb-mode-hide-buffer))))
1099 (add-hook 'mew-suspend-hook 'lsdb-mode-hide-buffer)
1100 (add-hook 'mew-quit-hook 'lsdb-mode-save)
1101 (add-hook 'kill-emacs-hook 'lsdb-mode-save))
1103 (defun lsdb-mew-update-record ()
1104 (let* ((fld (mew-current-get-fld (mew-frame-id)))
1105 (msg (mew-current-get-msg (mew-frame-id)))
1106 (cache (mew-cache-hit fld msg 'must-hit))
1110 (make-local-variable 'lsdb-decode-field-body-function)
1111 (setq lsdb-decode-field-body-function
1113 (set-text-properties 0 (length body) nil body)
1115 (when (setq records (lsdb-update-records))
1116 (lsdb-display-record (car records))))))
1118 ;;;_. Interface to MU-CITE
1120 (autoload 'mu-cite-get-value "mu-cite"))
1122 (defun lsdb-mu-attribution (address)
1123 "Extract attribute information from LSDB."
1125 (lsdb-lookup-records (concat "\\<" address "\\>") 'net)))
1127 (cdr (assq 'attribution (cdr (car records)))))))
1129 (defun lsdb-mu-set-attribution (attribution address)
1130 "Add attribute information to LSDB."
1132 (lsdb-lookup-records (concat "\\<" address "\\>") 'net))
1135 (setq entry (assq 'attribution (cdr (car records))))
1137 (setcdr entry attribution)
1138 (setcdr (car records) (cons (cons 'attribution attribution)
1139 (cdr (car records))))
1140 (lsdb-puthash (car (car records)) (cdr (car records))
1142 (setq lsdb-hash-table-is-dirty t)))))
1144 (defun lsdb-mu-get-prefix-method ()
1145 "A mu-cite method to return a prefix from LSDB or \">\".
1146 If an `attribution' value is found in LSDB, the value is returned.
1147 Otherwise \">\" is returned."
1148 (or (lsdb-mu-attribution (mu-cite-get-value 'address))
1151 (defvar minibuffer-allow-text-properties)
1153 (defvar lsdb-mu-history nil)
1155 (defun lsdb-mu-get-prefix-register-method ()
1156 "A mu-cite method to return a prefix from LSDB or register it.
1157 If an `attribution' value is found in LSDB, the value is returned.
1158 Otherwise the function requests a prefix from a user. The prefix will
1159 be registered to LSDB if the user wants it."
1160 (let ((address (mu-cite-get-value 'address)))
1161 (or (lsdb-mu-attribution address)
1162 (let* (minibuffer-allow-text-properties
1163 (result (read-string "Citation name? "
1164 (or (mu-cite-get-value 'x-attribution)
1165 (mu-cite-get-value 'full-name))
1167 (if (and (not (string-equal result ""))
1168 (y-or-n-p (format "Register \"%s\"? " result)))
1169 (lsdb-mu-set-attribution result address))
1172 (defun lsdb-mu-get-prefix-register-verbose-method ()
1173 "A mu-cite method to return a prefix using LSDB.
1175 In this method, a user must specify a prefix unconditionally. If an
1176 `attribution' value is found in LSDB, the value is used as a initial
1177 value to input the prefix. The prefix will be registered to LSDB if
1179 (let* ((address (mu-cite-get-value 'address))
1180 (attribution (lsdb-mu-attribution address))
1181 minibuffer-allow-text-properties
1182 (result (read-string "Citation name? "
1184 (mu-cite-get-value 'x-attribution)
1185 (mu-cite-get-value 'full-name))
1187 (if (and (not (string-equal result ""))
1188 (not (string-equal result attribution))
1189 (y-or-n-p (format "Register \"%s\"? " result)))
1190 (lsdb-mu-set-attribution result address))
1193 (defvar mu-cite-methods-alist)
1195 (defun lsdb-mu-insinuate ()
1196 (add-hook 'mu-cite-instantiation-hook
1198 (setq mu-cite-methods-alist
1200 mu-cite-methods-alist
1203 #'lsdb-mu-get-prefix-method)
1204 (cons 'lsdb-prefix-register
1205 #'lsdb-mu-get-prefix-register-method)
1206 (cons 'lsdb-prefix-register-verbose
1207 #'lsdb-mu-get-prefix-register-verbose-method)))))))
1209 ;;;_. X-Face Rendering
1210 (defvar lsdb-x-face-cache
1211 (lsdb-make-hash-table :test 'equal))
1213 (defun lsdb-x-face-available-image-type ()
1214 (static-if (featurep 'xemacs)
1217 (and (>= emacs-major-version 21)
1218 (fboundp 'image-type-available-p)
1219 (if (image-type-available-p 'pbm)
1221 (if (image-type-available-p 'xpm)
1224 (defun lsdb-expose-x-face ()
1225 (let* ((record (get-text-property (point-min) 'lsdb-record))
1226 (x-face (cdr (assq 'x-face (cdr record))))
1228 (when (and lsdb-insert-x-face-function
1230 (goto-char (point-min))
1232 (put-text-property 0 1 'invisible t delimiter) ;hide "\r"
1238 (funcall lsdb-insert-x-face-function (pop x-face)))
1240 'lsdb-record record))))
1242 (defun lsdb-insert-x-face-image (data type marker)
1243 (static-if (featurep 'xemacs)
1245 (set-buffer (marker-buffer marker))
1247 (let* ((inhibit-read-only t)
1249 (glyph (make-glyph (vector type :data data))))
1250 (set-extent-begin-glyph
1251 (make-extent (point) (point))
1254 (set-buffer (marker-buffer marker))
1256 (let* ((inhibit-read-only t)
1258 (image (create-image data type t :ascent 'center))
1259 (record (get-text-property (point) 'lsdb-record)))
1260 (put-text-property (point) (progn
1261 (insert-image image)
1263 'lsdb-record record)))))
1265 (defun lsdb-insert-x-face-asynchronously (x-face)
1266 (let* ((type (lsdb-x-face-available-image-type))
1267 (shell-file-name lsdb-shell-file-name)
1268 (shell-command-switch lsdb-shell-command-switch)
1269 (process-connection-type nil)
1270 (cached (cdr (assq type (lsdb-gethash x-face lsdb-x-face-cache))))
1271 (marker (point-marker))
1274 (lsdb-insert-x-face-image cached type marker)
1276 (start-process-shell-command
1277 "lsdb-x-face-command" (generate-new-buffer " *lsdb work*")
1279 (nth 1 (assq type lsdb-x-face-command-alist))
1280 "; } 2> /dev/null")))
1281 (process-send-string process (concat x-face "\n"))
1282 (process-send-eof process)
1283 (set-process-sentinel
1285 `(lambda (process string)
1287 (when (and (buffer-live-p (marker-buffer ,marker))
1288 (equal string "finished\n"))
1290 (with-current-buffer (process-buffer process)
1291 (set-buffer-multibyte nil)
1293 (lsdb-insert-x-face-image data ',type ,marker)
1294 (lsdb-puthash ,x-face (list (cons ',type data))
1295 lsdb-x-face-cache)))
1296 (kill-buffer (process-buffer process))))))))
1301 (product-provide 'lsdb
1302 (product-define "LSDB" nil '(0 2)))
1304 ;;;_* Local emacs vars.
1305 ;;; The following `outline-layout' local variable setting:
1306 ;;; - closes all topics from the first topic to just before the third-to-last,
1307 ;;; - shows the children of the third to last (config vars)
1308 ;;; - and the second to last (code section),
1309 ;;; - and closes the last topic (this local-variables section).
1311 ;;;outline-layout: (0 : -1 -1 0)
1314 ;;; lsdb.el ends here