(lsdb-insert-x-face-asynchronously): Make " *lsdb work*" buffer
[elisp/lsdb.git] / lsdb.el
diff --git a/lsdb.el b/lsdb.el
index 4f8fa9b..5c4b266 100644 (file)
--- a/lsdb.el
+++ b/lsdb.el
@@ -61,7 +61,6 @@
 (require 'pces)
 (require 'mime)
 (require 'static)
-(require 'timezone)
 
 ;;;_* USER CUSTOMIZATION VARIABLES:
 (defgroup lsdb nil
     ("\\(X-URL\\|X-URI\\)" nil www)
     ("X-Attribution\\|X-cite-me" nil attribution)
     ("X-Face" nil x-face)
+    ("Face" nil face)
     (,lsdb-sender-headers nil sender))
   "Alist of headers we are interested in.
 The format of elements of this list should be
@@ -120,8 +120,8 @@ where the last three elements are optional."
     (aka 4 ?,)
     (score -1)
     (x-face -1)
-    (sender -1)
-    (last-date -1))
+    (face -1)
+    (sender -1))
   "Alist of entry types for presentation.
 The format of elements of this list should be
      (ENTRY SCORE [CLASS READ-ONLY])
@@ -184,9 +184,14 @@ If non-nil, supersedes the return value of `lsdb-x-face-available-image-type'."
   :group 'lsdb
   :type 'symbol)
 
+(defcustom lsdb-x-face-scale-factor 0.5
+  "A number used to scale down or scale up X-Face images."
+  :group 'lsdb
+  :type 'number)
+  
 (defcustom lsdb-x-face-command-alist
-  '((pbm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale 0.5")
-    (xpm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale 0.5 | ppmtoxpm"))
+  '((pbm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale " scale-factor)
+    (xpm "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pnmscale " scale-factor " | ppmtoxpm"))
   "An alist from an image type to a command to be executed to display an X-Face header.
 The command will be executed in a sub-shell asynchronously.
 The compressed face will be piped to this command."
@@ -205,7 +210,40 @@ The compressed face will be piped to this command."
   :group 'lsdb
   :type 'function)
 
-(defcustom lsdb-print-record-hook '(lsdb-expose-x-face)
+(defcustom lsdb-face-image-type nil
+  "A image type of displayed face.
+If non-nil, supersedes the return value of `lsdb-x-face-available-image-type'."
+  :group 'lsdb
+  :type 'symbol)
+
+(defcustom lsdb-face-scale-factor 0.5
+  "A number used to scale down or scale up Face images."
+  :group 'lsdb
+  :type 'number)
+
+(defcustom lsdb-face-command-alist
+  '((png "pngtopnm | pnmscale " scale-factor " | pnmtopng")
+    (xpm "pngtopnm | pnmscale " scale-factor " | ppmtoxpm"))
+  "An alist from an image type to a command to be executed to display a Face header.
+The command will be executed in a sub-shell asynchronously.
+The decoded field-body (actually a PNG data) will be piped to this command."
+  :group 'lsdb
+  :type 'list)
+
+(defcustom lsdb-insert-face-function
+  (if (static-if (featurep 'xemacs)
+         (or (featurep 'png)
+             (featurep 'xpm))
+       (and (>= emacs-major-version 21)
+            (fboundp 'image-type-available-p)
+            (or (image-type-available-p 'png)
+                (image-type-available-p 'xpm))))
+      #'lsdb-insert-face-asynchronously)
+  "Function to display Face."
+  :group 'lsdb
+  :type 'function)
+
+(defcustom lsdb-print-record-hook '(lsdb-expose-x-face lsdb-expose-face)
   "A hook called after a record is displayed."
   :group 'lsdb
   :type 'hook)
@@ -258,6 +296,11 @@ Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
   :group 'lsdb
   :type 'boolean)
 
+(defcustom lsdb-use-migemo nil
+  "If non-nil, use `migemo' when complete address."
+  :type 'boolean
+  :group 'lsdb)
+
 ;;;_. Faces
 (defface lsdb-header-face
   '((t (:underline t)))
@@ -317,6 +360,16 @@ It represents address to full-name mapping.")
 The function is called with one argument, the buffer to be displayed.
 Overrides `temp-buffer-show-function'.")
 
+;;;_. Utility functions
+(defun lsdb-substitute-variables (program variable value)
+  (setq program (copy-sequence program))
+  (let ((pointer program))
+    (while pointer
+      (setq pointer (memq variable program))
+      (if pointer
+         (setcar pointer value)))
+    program))
+
 ;;;_. Hash Table Emulation
 (if (and (fboundp 'make-hash-table)
         (subrp (symbol-function 'make-hash-table)))
@@ -449,11 +502,11 @@ This is the current number of slots in HASH-TABLE, whether occupied or not."
          ;; XEmacs doesn't have a distinction between index-size and
          ;; hash-table-size.
          (number-to-string (lsdb-hash-table-count hash-table))
-         " test equal data (")
+         " test equal data (\n")
   (lsdb-maphash
    (lambda (key value)
      (let (print-level print-length)
-       (insert (prin1-to-string key) " " (prin1-to-string value) " ")))
+       (insert (prin1-to-string key) " " (prin1-to-string value) "\n")))
    hash-table)
   (insert "))"))
 
@@ -485,17 +538,16 @@ This is the current number of slots in HASH-TABLE, whether occupied or not."
        (setq tables (cdr tables))))))
 
 ;;;_. Mail Header Extraction
-(defun lsdb-fetch-field-bodies (regexp)
+(defun lsdb-fetch-fields (regexp)
   (save-excursion
     (goto-char (point-min))
     (let ((case-fold-search t)
          field-bodies)
       (while (re-search-forward (concat "^\\(" regexp "\\):[ \t]*")
                                nil t)
-       (push (funcall lsdb-decode-field-body-function
-                            (buffer-substring (point) (std11-field-end))
-                            (match-string 1))
-                   field-bodies))
+       (push (cons (match-string 1)
+                   (buffer-substring (point) (std11-field-end)))
+             field-bodies))
       (nreverse field-bodies))))
 
 (defun lsdb-canonicalize-spaces-and-dots (string)
@@ -606,8 +658,10 @@ This is the current number of slots in HASH-TABLE, whether occupied or not."
 ;;;_ : Update Records
 (defun lsdb-update-record (sender &optional interesting)
   (let ((old (lsdb-gethash (car sender) lsdb-hash-table))
-       (new (cons (cons 'net (list (nth 1 sender)))
-                  interesting))
+       (new (if (nth 1 sender)
+                (cons (cons 'net (list (nth 1 sender)))
+                      interesting)
+              interesting))
        merged
        record
        full-name)
@@ -640,31 +694,52 @@ This is the current number of slots in HASH-TABLE, whether occupied or not."
       (setq lsdb-hash-tables-are-dirty t))
     record))
 
-(defun lsdb-update-records (&optional last-date)
+(defun lsdb-update-records ()
   (lsdb-maybe-load-hash-tables)
   (let (senders recipients interesting alist records bodies entry)
     (save-restriction
       (std11-narrow-to-header)
       (setq senders
-           (delq nil (mapcar #'lsdb-extract-address-components
-                             (lsdb-fetch-field-bodies
+           (delq nil (mapcar (lambda (field)
+                               (let ((components
+                                      (lsdb-extract-address-components
+                                       (cdr field))))
+                                 (if components
+                                     (setcar
+                                      components
+                                      (funcall lsdb-decode-field-body-function
+                                               (car components) (car field))))
+                                 components))
+                             (lsdb-fetch-fields
                               lsdb-sender-headers)))
            recipients
-           (delq nil (mapcar #'lsdb-extract-address-components
-                             (lsdb-fetch-field-bodies
+           (delq nil (mapcar (lambda (field)
+                               (let ((components
+                                      (lsdb-extract-address-components
+                                       (cdr field))))
+                                 (if components
+                                     (setcar
+                                      components
+                                      (funcall lsdb-decode-field-body-function
+                                               (car components) (car field))))
+                                 components))
+                             (lsdb-fetch-fields
                               lsdb-recipients-headers))))
       (setq alist lsdb-interesting-header-alist)
       (while alist
        (setq bodies
              (delq nil (mapcar
-                        (lambda (field-body)
-                          (if (nth 1 (car alist))
-                              (and (string-match (nth 1 (car alist))
-                                                 field-body)
-                                   (replace-match (nth 3 (car alist))
-                                                  nil nil field-body))
-                            field-body))
-                        (lsdb-fetch-field-bodies (car (car alist))))))
+                        (lambda (field)
+                          (let ((field-body
+                                 (funcall lsdb-decode-field-body-function
+                                          (cdr field) (car field))))
+                            (if (nth 1 (car alist))
+                                (and (string-match (nth 1 (car alist))
+                                                   field-body)
+                                     (replace-match (nth 3 (car alist))
+                                                    nil nil field-body))
+                              field-body)))
+                        (lsdb-fetch-fields (car (car alist))))))
        (when bodies
          (setq entry (or (nth 2 (car alist))
                          'notes))
@@ -674,8 +749,6 @@ This is the current number of slots in HASH-TABLE, whether occupied or not."
                        bodies))
                interesting))
        (setq alist (cdr alist))))
-    (if last-date
-       (push (cons 'last-date last-date) interesting))
     (if senders
        (setq records (list (lsdb-update-record (pop senders) interesting))))
     (setq alist (nconc senders recipients))
@@ -726,17 +799,23 @@ This is the current number of slots in HASH-TABLE, whether occupied or not."
        (lsdb-fit-window-to-buffer window)))))
 
 (defun lsdb-update-records-and-display ()
-  (lsdb-maybe-load-hash-tables)
-  (lsdb-display-record (car (lsdb-update-records))))
+  (let ((records (lsdb-update-records)))
+    (if lsdb-display-records-belong-to-user
+       (if records
+           (lsdb-display-record (car records))
+         (lsdb-hide-buffer))
+      (catch 'lsdb-show-record
+       (while records
+         (if (member user-mail-address (cdr (assq 'net (car records))))
+             (setq records (cdr records))
+           (lsdb-display-record (car records))
+           (throw 'lsdb-show-record t)))
+       (lsdb-hide-buffer)))))
 
 (defun lsdb-display-record (record)
   "Display only one RECORD, then shrink the window as possible."
-  (if (and record
-          (or lsdb-display-records-belong-to-user
-              (member user-mail-address (cdr (assq 'net record)))))
-      (let ((temp-buffer-show-function lsdb-temp-buffer-show-function))
-       (lsdb-display-records (list record)))
-    (lsdb-hide-buffer)))
+  (let ((temp-buffer-show-function lsdb-temp-buffer-show-function))
+    (lsdb-display-records (list record))))
 
 (defun lsdb-display-records (records)
   (with-current-buffer (get-buffer-create lsdb-buffer-name)
@@ -803,12 +882,18 @@ This is the current number of slots in HASH-TABLE, whether occupied or not."
 ;;;_ : Matching Highlight
 (defvar lsdb-last-highlight-overlay nil)
 
+;;; avoid byte-compile warning for migemo
+(eval-when-compile
+  (autoload 'migemo-get-pattern "migemo"))
+
 (defun lsdb-complete-name-highlight (start end)
   (make-local-hook 'pre-command-hook)
   (add-hook 'pre-command-hook 'lsdb-complete-name-highlight-update nil t)
   (save-excursion
     (goto-char start)
-    (search-forward lsdb-last-completion end)
+    (if (and lsdb-use-migemo (fboundp 'migemo-get-pattern))
+       (re-search-forward lsdb-last-completion end)
+      (search-forward lsdb-last-completion end))
     (setq lsdb-last-highlight-overlay
          (make-overlay (match-beginning 0) (match-end 0)))
     (overlay-put lsdb-last-highlight-overlay 'face
@@ -843,8 +928,11 @@ This is the current number of slots in HASH-TABLE, whether occupied or not."
     (unless (eq last-command this-command)
       (setq lsdb-last-candidates nil
            lsdb-last-candidates-pointer nil
-           lsdb-last-completion (buffer-substring start (point))
-           pattern (concat "\\<" (regexp-quote lsdb-last-completion)))
+           lsdb-last-completion (buffer-substring start (point)))
+      (if (and lsdb-use-migemo (fboundp 'migemo-get-pattern))
+         (setq lsdb-last-completion (migemo-get-pattern lsdb-last-completion)
+               pattern (concat "\\<\\(" lsdb-last-completion "\\)"))
+       (setq pattern (concat "\\<" (regexp-quote lsdb-last-completion))))
       (lsdb-maphash
        (lambda (key value)
         (setq lsdb-last-candidates
@@ -1218,12 +1306,15 @@ then the name of this record will be edited."
       (lsdb-mode-edit-entry)
     (lsdb-mode-edit-record)))
 
-(defun lsdb-mode-save (&optional dont-ask)
+(defun lsdb-mode-save (&optional force)
   "Save LSDB hash table into `lsdb-file'."
-  (interactive (list t))
-  (if (not lsdb-hash-tables-are-dirty)
+  (interactive "P")
+  (if (not (or force
+              lsdb-hash-tables-are-dirty))
       (message "(No changes need to be saved)")
-    (when (or dont-ask
+    (when (or (interactive-p)          ;Don't ask user if this
+                                       ;function is called as a
+                                       ;command.
              (not lsdb-verbose)
              (y-or-n-p "Save the LSDB now? "))
       (lsdb-save-hash-tables)
@@ -1435,11 +1526,6 @@ of the buffer."
     (set-window-configuration window-configuration)))
 
 ;;;_. Interface to Semi-gnus
-(eval-when-compile
-  (condition-case nil
-      (require 'nnheader)
-    (error)))
-
 ;;;###autoload
 (defun lsdb-gnus-insinuate ()
   "Call this function to hook LSDB into Semi-gnus."
@@ -1448,30 +1534,10 @@ of the buffer."
 
 (defvar gnus-article-current-summary)
 (defvar gnus-original-article-buffer)
-(defvar gnus-current-headers)
 (defun lsdb-gnus-update-record ()
   (with-current-buffer (with-current-buffer gnus-article-current-summary
                         gnus-original-article-buffer)
-    (lsdb-maybe-load-hash-tables)
-    (let* ((sender (lsdb-extract-address-components
-                   (mail-header-from gnus-current-headers)))
-          (date (mail-header-date gnus-current-headers))
-          (current-date (format-time-string "%Y%m%d%H:%M:%S"))
-          (record (lsdb-gethash (car sender) lsdb-hash-table))
-          (last-date (or (cdr (assq 'last-date record))
-                         current-date))
-          article-date)
-      (if (or (null record)
-             (null date)               ;Without Date: header?
-             (and (string-lessp (setq article-date
-                                      (timezone-make-date-sortable date))
-                                current-date)
-                  (string-lessp last-date article-date)))
-         (setq record (car (lsdb-update-records
-                            (or article-date current-date)))))
-      (lsdb-display-record (if (stringp (car record)) ;Record is up to date.
-                              record
-                            (cons (car sender) record))))))
+    (lsdb-update-records-and-display)))
 
 ;;;_. Interface to Wanderlust
 ;;;###autoload
@@ -1745,37 +1811,157 @@ the user wants it."
         (process-connection-type nil)
         (cached (cdr (assq type (lsdb-gethash x-face lsdb-x-face-cache))))
         (marker (point-marker))
+        buffer
         process)
     (if cached
        (lsdb-insert-x-face-image cached type marker)
+      (with-current-buffer (setq buffer (generate-new-buffer " *lsdb work*"))
+       (buffer-disable-undo)
+       (set-buffer-multibyte nil))
       (setq process
            (start-process-shell-command
-            "lsdb-x-face-command" (generate-new-buffer " *lsdb work*")
+            "lsdb-x-face-command" buffer
             (concat "{ "
-                    (nth 1 (assq type lsdb-x-face-command-alist))
+                    (apply #'concat
+                           (lsdb-substitute-variables
+                            (cdr (assq type lsdb-x-face-command-alist))
+                            'scale-factor
+                            (number-to-string lsdb-x-face-scale-factor)))
                     "; } 2> /dev/null")))
+      (set-process-filter
+       process
+       `(lambda (process string)
+         (save-excursion
+           (set-buffer ,buffer)
+           (goto-char (point-max))
+           (insert string))))
+      (set-process-sentinel
+       process
+       `(lambda (process string)
+         (unwind-protect
+             (if (equal string "finished\n")
+                 (let ((data
+                        (with-current-buffer ,buffer
+                          (buffer-string))))
+                   (lsdb-insert-x-face-image data ',type ,marker)
+                   (lsdb-puthash ,x-face (list (cons ',type data))
+                                 lsdb-x-face-cache)))
+           (kill-buffer ,buffer))))
       (process-send-string process (concat x-face "\n"))
-      (process-send-eof process)
+      (process-send-eof process))))
+
+;;;_. Face Rendering
+(defvar lsdb-face-cache
+  (lsdb-make-hash-table :test 'equal))
+
+(defun lsdb-face-available-image-type ()
+  (static-if (featurep 'xemacs)
+      (if (featurep 'png)
+         'png
+       (if (featurep 'xpm)
+           'xpm))
+    (and (>= emacs-major-version 21)
+        (fboundp 'image-type-available-p)
+        (if (image-type-available-p 'png)
+            'png
+          (if (image-type-available-p 'xpm)
+              'xpm)))))
+
+(defun lsdb-expose-face ()
+  (let* ((record (get-text-property (point-min) 'lsdb-record))
+        (face (cdr (assq 'face (cdr record))))
+        (delimiter "\r "))
+    (when (and lsdb-insert-face-function
+              face)
+      (goto-char (point-min))
+      (end-of-line)
+      (put-text-property 0 1 'invisible t delimiter) ;hide "\r"
+      (put-text-property
+       (point)
+       (progn
+        (insert delimiter)
+        (while face
+          (funcall lsdb-insert-face-function (pop face)))
+        (point))
+       'lsdb-record record))))
+
+(defun lsdb-insert-face-image (data type marker)
+  (static-if (featurep 'xemacs)
+      (save-excursion
+       (set-buffer (marker-buffer marker))
+       (goto-char marker)
+       (let* ((inhibit-read-only t)
+              buffer-read-only
+              (glyph (make-glyph (vector type :data data))))
+         (set-extent-begin-glyph
+          (make-extent (point) (point))
+          glyph)))
+    (save-excursion
+      (set-buffer (marker-buffer marker))
+      (goto-char marker)
+      (let* ((inhibit-read-only t)
+            buffer-read-only
+            (image (create-image data type t :ascent 'center))
+            (record (get-text-property (point) 'lsdb-record)))
+       (put-text-property (point) (progn
+                                    (insert-image image)
+                                    (point))
+                          'lsdb-record record)))))
+
+(defun lsdb-insert-face-asynchronously (face)
+  (let* ((type (or lsdb-face-image-type
+                  (lsdb-face-available-image-type)))
+        (shell-file-name lsdb-shell-file-name)
+        (shell-command-switch lsdb-shell-command-switch)
+        (coding-system-for-read 'binary)
+        (coding-system-for-write 'binary)
+        (process-connection-type nil)
+        (cached (cdr (assq type (lsdb-gethash face lsdb-face-cache))))
+        (marker (point-marker))
+        buffer
+        process)
+    (if cached
+       (lsdb-insert-face-image cached type marker)
+      (with-current-buffer (setq buffer (generate-new-buffer " *lsdb work*"))
+       (buffer-disable-undo)
+       (set-buffer-multibyte nil))
+      (setq process
+           (start-process-shell-command
+            "lsdb-face-command" buffer
+            (concat "{ "
+                    (apply #'concat
+                           (lsdb-substitute-variables
+                            (cdr (assq type lsdb-face-command-alist))
+                            'scale-factor
+                            (number-to-string lsdb-face-scale-factor)))
+                    "; } 2> /dev/null")))
+      (set-process-filter
+       process
+       `(lambda (process string)
+         (save-excursion
+           (set-buffer ,buffer)
+           (goto-char (point-max))
+           (insert string))))
       (set-process-sentinel
        process
        `(lambda (process string)
          (unwind-protect
-             (when (and (buffer-live-p (marker-buffer ,marker))
-                        (equal string "finished\n"))
-               (let ((data
-                      (with-current-buffer (process-buffer process)
-                        (set-buffer-multibyte nil)
-                        (buffer-string))))
-                 (lsdb-insert-x-face-image data ',type ,marker)
-                 (lsdb-puthash ,x-face (list (cons ',type data))
-                               lsdb-x-face-cache)))
-           (kill-buffer (process-buffer process))))))))
+             (if (equal string "finished\n")
+                 (let ((data
+                        (with-current-buffer ,buffer
+                          (buffer-string))))
+                   (lsdb-insert-face-image data ',type ,marker)
+                   (lsdb-puthash ,face (list (cons ',type data))
+                                 lsdb-face-cache)))
+           (kill-buffer ,buffer))))
+      (process-send-string process (base64-decode-string face))
+      (process-send-eof process))))
 
 (require 'product)
 (provide 'lsdb)
 
 (product-provide 'lsdb
-  (product-define "LSDB" nil '(0 10)))
+  (product-define "LSDB" nil '(0 11)))
 
 ;;;_* Local emacs vars.
 ;;; The following `allout-layout' local variable setting: