(wl-summary-test-spam): Call `wl-summary-unmark-spam' for the message not
[elisp/wanderlust.git] / wl / wl-util.el
index d543210..1613ea1 100644 (file)
 (require 'bytecomp)
 (require 'elmo-util)
 (require 'elmo-flag)
+(require 'wl-vars)
+(eval-when-compile (require 'elmo-pop3))
+(eval-when-compile (require 'cl))
+(eval-when-compile (require 'static))
 
 (condition-case nil (require 'pp) (error nil))
 
@@ -114,7 +118,7 @@ If HACK-ADDRESSES is t, then the strings are considered to be mail addresses,
                    (string-to-char (format "%s" (this-command-keys))))))
     (message "%s" mes-string)
     (setq key (car (setq keve (wl-read-event-char))))
-    (if (or (equal key ?\ )
+    (if (or (equal key (string-to-char " "))
            (and cmd
                 (equal key cmd)))
        (progn
@@ -127,14 +131,14 @@ If HACK-ADDRESSES is t, then the strings are considered to be mail addresses,
   (or (string= name wl-draft-folder)
       (string= name wl-queue-folder)))
 
-;(defalias 'wl-make-hash 'elmo-make-hash)
-;;(make-obsolete 'wl-make-hash 'elmo-make-hash)
+;;;(defalias 'wl-make-hash 'elmo-make-hash)
+;;;(make-obsolete 'wl-make-hash 'elmo-make-hash)
 
-;;(defalias 'wl-get-hash-val 'elmo-get-hash-val)
-;;(make-obsolete 'wl-get-hash-val 'elmo-get-hash-val)
+;;;(defalias 'wl-get-hash-val 'elmo-get-hash-val)
+;;;(make-obsolete 'wl-get-hash-val 'elmo-get-hash-val)
 
-;;(defalias 'wl-set-hash-val 'elmo-set-hash-val)
-;;(make-obsolete 'wl-set-hash-val 'elmo-set-hash-val)
+;;;(defalias 'wl-set-hash-val 'elmo-set-hash-val)
+;;;(make-obsolete 'wl-set-hash-val 'elmo-set-hash-val)
 
 (defsubst wl-set-string-width (width string &optional padding ignore-invalid)
   "Make a new string which have specified WIDTH and content of STRING.
@@ -159,7 +163,7 @@ even when invalid character is contained."
                                        (abs width))))
       (let ((paddings (make-string
                       (max 0 (- (abs width) (string-width string)))
-                      (or padding ?\ ))))
+                      (or padding (string-to-char " ")))))
        (if (< width 0)
            (concat paddings string)
          (concat string paddings)))))
@@ -176,7 +180,7 @@ even when invalid character is contained."
      (if (= (current-column) (abs width))
         string
        (let ((paddings (make-string (- (abs width) (current-column))
-                                   (or padding ?\ ))))
+                                   (or padding (string-to-char " ")))))
         (if (< width 0)
             (concat paddings string)
           (concat string paddings))))))))
@@ -237,14 +241,14 @@ even when invalid character is contained."
        (setq alist (cdr alist)))
       value)))
 
-(defmacro wl-match-string (pos string)
+(defun wl-match-string (pos string)
   "Substring POSth matched STRING."
-  (` (substring (, string) (match-beginning (, pos)) (match-end (, pos)))))
+  (substring string (match-beginning pos) (match-end pos)))
 
-(defmacro wl-match-buffer (pos)
+(defun wl-match-buffer (pos)
   "Substring POSth matched from the current buffer."
-  (` (buffer-substring-no-properties
-      (match-beginning (, pos)) (match-end (, pos)))))
+  (buffer-substring-no-properties
+   (match-beginning pos) (match-end pos)))
 
 (put 'wl-as-coding-system 'lisp-indent-function 1)
 (put 'wl-as-mime-charset 'lisp-indent-function 1)
@@ -253,21 +257,21 @@ even when invalid character is contained."
   (cond
    (wl-on-mule3
     (defmacro wl-as-coding-system (coding-system &rest body)
-      (` (let ((coding-system-for-read (, coding-system))
-              (coding-system-for-write (, coding-system)))
-          (,@ body)))))
+      `(let ((coding-system-for-read ,coding-system)
+            (coding-system-for-write ,coding-system))
+        ,@body)))
    (wl-on-mule
     (defmacro wl-as-coding-system (coding-system &rest body)
-      (` (let ((file-coding-system-for-read (, coding-system))
-              (file-coding-system (, coding-system)))
-          (,@ body)))))
+      `(let ((file-coding-system-for-read ,coding-system)
+            (file-coding-system ,coding-system))
+        ,@body)))
    (t
     (defmacro wl-as-coding-system (coding-system &rest body)
-      (` (progn (,@ body)))))))
+      `(progn ,@body)))))
 
 (defmacro wl-as-mime-charset (mime-charset &rest body)
-  (` (wl-as-coding-system (mime-charset-to-coding-system (, mime-charset))
-       (,@ body))))
+  `(wl-as-coding-system (mime-charset-to-coding-system ,mime-charset)
+     ,@body))
 
 (defalias 'wl-string 'elmo-string)
 (make-obsolete 'wl-string 'elmo-string)
@@ -361,8 +365,6 @@ The objects mapped (cdrs of elements of the ALIST) are shared."
       (setq keys (cdr keys)))
     result))
 
-(eval-when-compile
-  (require 'static))
 (static-unless (fboundp 'pp)
   (defvar pp-escape-newlines t)
   (defun pp (object &optional stream)
@@ -473,7 +475,7 @@ that `read' can handle, whenever this is possible."
              (setq fld-name nil))
          (if (eq (length (setq port
                                (elmo-match-string 2 url))) 0)
-             (setq port (int-to-string elmo-nntp-default-port)))
+             (setq port (number-to-string elmo-nntp-default-port)))
          (if (eq (length (setq server
                                (elmo-match-string 1 url))) 0)
              (setq server elmo-nntp-default-server))
@@ -489,7 +491,7 @@ that `read' can handle, whenever this is possible."
       (message "Not a nntp: url."))))
 
 (defmacro wl-concat-list (list separator)
-  (` (mapconcat 'identity (delete "" (delq nil (, list))) (, separator))))
+  `(mapconcat 'identity (delete "" (delq nil ,list)) ,separator))
 
 (defun wl-current-message-buffer ()
   (when (buffer-live-p wl-current-summary-buffer)
@@ -502,25 +504,24 @@ that `read' can handle, whenever this is possible."
                wl-summary-buffer-display-mime-mode
                nil nil))))))
 
-(defmacro wl-kill-buffers (regexp)
-  (` (mapcar (function
-             (lambda (x)
-               (if (and (buffer-name x)
-                        (string-match (, regexp) (buffer-name x)))
-                   (and (get-buffer x)
-                        (kill-buffer x)))))
-            (buffer-list))))
+(defun wl-kill-buffers (regexp)
+  (mapc
+   (lambda (x)
+     (if (and (buffer-name x)
+             (string-match regexp (buffer-name x)))
+        (and (get-buffer x)
+             (kill-buffer x))))
+   (buffer-list)))
 
 (defun wl-collect-summary ()
   (let (result)
-    (mapcar
-     (function (lambda (x)
-                (if (and (string-match "^Summary"
-                                       (buffer-name x))
-                         (save-excursion
-                           (set-buffer x)
-                           (equal major-mode 'wl-summary-mode)))
-                    (setq result (nconc result (list x))))))
+    (mapc
+     (lambda (x)
+       (if (and (string-match "^Summary"
+                             (buffer-name x))
+               (with-current-buffer x
+                 (eq major-mode 'wl-summary-mode)))
+          (setq result (nconc result (list x)))))
      (buffer-list))
     result))
 
@@ -568,8 +569,8 @@ that `read' can handle, whenever this is possible."
 (static-if (fboundp 'local-variable-p)
     (defalias 'wl-local-variable-p 'local-variable-p)
   (defmacro wl-local-variable-p (symbol &optional buffer)
-    (` (if (assq (, symbol) (buffer-local-variables (, buffer)))
-          t))))
+    `(if (assq ,symbol (buffer-local-variables ,buffer))
+        t)))
 
 (defun wl-number-base36 (num len)
   (if (if (< len 0)
@@ -600,11 +601,11 @@ that `read' can handle, whenever this is possible."
                                      ("Jul" . "07") ("Aug" . "08")
                                      ("Sep" . "09") ("Oct" . "10")
                                      ("Nov" . "11") ("Dec" . "12"))))))
-               (list (string-to-int (concat (nth 6 cts) m
-                                            (substring (nth 2 cts) 0 1)))
-                     (string-to-int (concat (substring (nth 2 cts) 1)
-                                            (nth 4 cts) (nth 5 cts)
-                                            (nth 6 cts))))))))
+               (list (string-to-number (concat (nth 6 cts) m
+                                               (substring (nth 2 cts) 0 1)))
+                     (string-to-number (concat (substring (nth 2 cts) 1)
+                                               (nth 4 cts) (nth 5 cts)
+                                               (nth 6 cts))))))))
     (concat
      (if (memq system-type '(ms-dos emx vax-vms))
         (let ((user (downcase (user-login-name))))
@@ -652,10 +653,8 @@ that `read' can handle, whenever this is possible."
 
 ;;;
 
-(defmacro wl-count-lines ()
-  (` (save-excursion
-       (beginning-of-line)
-       (count-lines 1 (point)))))
+(defsubst wl-count-lines ()
+  (count-lines 1 (point-at-bol)))
 
 (defun wl-horizontal-recenter ()
   "Recenter the current buffer horizontally."
@@ -871,6 +870,7 @@ This function is imported from Emacs 20.7."
            (while flist
              (setq folder (wl-folder-get-elmo-folder (car flist))
                    flist (cdr flist))
+             (elmo-folder-set-biff-internal folder t)
              (when (and (elmo-folder-plugged-p folder)
                         (elmo-folder-exists-p folder))
                (setq new-mails
@@ -1014,15 +1014,15 @@ is enclosed by at least one regexp grouping construct."
     (append (list 'format f) specs)))
 
 (defmacro wl-line-formatter-setup (formatter format alist)
-  (` (let (byte-compile-warnings)
-       (setq (, formatter)
-            (byte-compile
-             (list 'lambda ()
-                   (wl-line-parse-format (, format) (, alist)))))
-       (when (get-buffer "*Compile-Log*")
-        (bury-buffer "*Compile-Log*"))
-       (when (get-buffer "*Compile-Log-Show*")
-        (bury-buffer "*Compile-Log-Show*")))))
+  `(let (byte-compile-warnings)
+     (setq ,formatter
+          (byte-compile
+           (list 'lambda ()
+                 (wl-line-parse-format ,format ,alist))))
+     (when (get-buffer "*Compile-Log*")
+       (bury-buffer "*Compile-Log*"))
+     (when (get-buffer "*Compile-Log-Show*")
+       (bury-buffer "*Compile-Log-Show*"))))
 
 (defsubst wl-copy-local-variables (src dst local-variables)
   "Copy value of LOCAL-VARIABLES from SRC buffer to DST buffer."
@@ -1033,23 +1033,28 @@ is enclosed by at least one regexp grouping construct."
             (symbol-value variable))))))
 
 ;;; Search Condition
+(defun wl-search-condition-fields ()
+  (let ((denial-fields
+        (nconc (mapcar 'capitalize elmo-msgdb-extra-fields)
+               (mapcar 'capitalize wl-additional-search-condition-fields)
+               '("Flag" "Since" "Before"
+                 "From" "Subject" "To" "Cc" "Body" "ToCc"
+                 "Larger" "Smaller"))))
+    (append '("Last" "First")
+           denial-fields
+           (mapcar (lambda (f) (concat "!" f))
+                   denial-fields))))
+
 (defun wl-read-search-condition (default)
   "Read search condition string interactively."
   (wl-read-search-condition-internal "Search by" default))
 
 (defun wl-read-search-condition-internal (prompt default &optional paren)
   (let* ((completion-ignore-case t)
-        (denial-fields (nconc (mapcar 'capitalize elmo-msgdb-extra-fields)
-                              '("Flag" "Since" "Before"
-                                "From" "Subject" "To" "Cc" "Body" "ToCc"
-                                "Larger" "Smaller")))
         (field (completing-read
                 (format "%s (%s): " prompt default)
-                (mapcar 'list
-                        (append '("AND" "OR" "Last" "First")
-                                denial-fields
-                                (mapcar (lambda (f) (concat "!" f))
-                                        denial-fields)))))
+                (mapcar #'list
+                        (append '("AND" "OR") (wl-search-condition-fields)))))
         value)
     (setq field (if (string= field "")
                    (setq field default)
@@ -1067,10 +1072,10 @@ is enclosed by at least one regexp grouping construct."
       (let ((default (format-time-string "%Y-%m-%d")))
        (setq value (completing-read
                     (format "Value for '%s' [%s]: " field default)
-                    (mapcar (function
-                             (lambda (x)
-                               (list (format "%s" (car x)))))
-                            elmo-date-descriptions)))
+                    (mapcar
+                     (lambda (x)
+                       (list (format "%s" (car x))))
+                     elmo-date-descriptions)))
        (concat (downcase field) ":"
                (if (equal value "") default value))))
      ((string-match "!?Flag" field)
@@ -1107,10 +1112,10 @@ is enclosed by at least one regexp grouping construct."
       (while t
        (discard-input)
        (case (let ((cursor-in-echo-area t))
-               (read-event prompt))
+               (cdr (wl-read-event-char prompt)))
          ((?y ?Y)
           (throw 'done t))
-         (? 
+         ((string-to-char " ")
           (if scroll-by-SPC
               (ignore-errors (scroll-up))
             (throw 'done t)))
@@ -1121,6 +1126,43 @@ is enclosed by at least one regexp grouping construct."
          (t
           (throw 'done nil)))))))
 
+(defun wl-find-region (beg-regexp end-regexp)
+  (if (or (re-search-forward end-regexp nil t)
+         (re-search-backward end-regexp nil t))
+      (let ((end (match-end 0))
+           (beg (re-search-backward beg-regexp nil t)))
+       (if beg
+           (cons beg end)))))
+
+(defun wl-simple-display-progress (label action current total)
+  (message "%s... %d%%"
+          action
+          (if (> total 0) (floor (* (/ current (float total)) 100)) 0)))
+
+(when (fboundp 'progress-feedback-with-label)
+  (defun wl-display-progress-with-gauge (label action current total)
+    (progress-feedback-with-label
+     label
+     "%s..."
+     (if (> total 0) (floor (* (/ current (float total)) 100)) 0)
+     action)))
+
+(defun wl-progress-callback-function (label action current total)
+  (case current
+    (query
+     (let ((threshold (if (consp wl-display-progress-threshold)
+                         (cdr (or (assq label wl-display-progress-threshold)
+                                  (assq t wl-display-progress-threshold)))
+                       wl-display-progress-threshold)))
+       (and threshold
+           (>= total threshold))))
+    (start
+     (message "%s..." action))
+    (done
+     (message "%s...done" action))
+    (t
+     (when wl-display-progress-function
+       (funcall wl-display-progress-function label action current total)))))
 
 ;; read multiple strings with completion
 (defun wl-completing-read-multiple-1 (prompt
@@ -1168,9 +1210,18 @@ is enclosed by at least one regexp grouping construct."
                                     nil initial-input
                                     hist def inherit-input-method))))
  (t
-  (defalias 'wl-completing-read-multiple 'wl-completing-read-multiple-2)))
+  (defalias 'wl-completing-read-multiple 'completing-read-multiple)))
 
 
+(cond
+ ((fboundp 'shell-command-read-minibuffer)
+  (defun wl-read-shell-command (prompt &optional
+                                      initial-contents keymap read hist)
+    (shell-command-read-minibuffer prompt default-directory
+                                  initial-contents keymap read hist)))
+ (t
+  (defalias 'wl-read-shell-command 'read-from-minibuffer)))
+
 (require 'product)
 (product-provide (provide 'wl-util) (require 'wl-version))