1 ;;; elmo-util.el -- Utilities for Elmo.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; 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.
34 (eval-when-compile (require 'cl))
36 (require 'eword-decode)
39 (defmacro elmo-set-buffer-multibyte (flag)
40 "Set the multibyte flag of the current buffer to FLAG."
42 (list 'setq 'mc-flag flag))
45 ((and (boundp 'emacs-major-version) (>= emacs-major-version 20))
46 (list 'set-buffer-multibyte flag))
50 (defvar elmo-work-buf-name " *elmo work*")
51 (defvar elmo-temp-buf-name " *elmo temp*")
53 (or (boundp 'default-enable-multibyte-characters)
54 (defvar default-enable-multibyte-characters (featurep 'mule)
55 "The mock variable except for Emacs 20."))
57 (defun elmo-base64-encode-string (string &optional no-line-break))
58 (defun elmo-base64-decode-string (string))
60 ;; base64 encoding/decoding
62 (fset 'elmo-base64-encode-string
63 (mel-find-function 'mime-encode-string "base64"))
64 (fset 'elmo-base64-decode-string
65 (mel-find-function 'mime-decode-string "base64"))
67 ;; Any Emacsen may have add-name-to-file(), because loadup.el requires it. :-p
68 ;; Check make-symbolic-link() instead. -- 981002 by Fuji
69 (if (fboundp 'make-symbolic-link) ;; xxx
70 (defalias 'elmo-add-name-to-file 'add-name-to-file)
71 (defun elmo-add-name-to-file
72 (filename newname &optional ok-if-already-exists)
73 (copy-file filename newname ok-if-already-exists t)))
75 (defsubst elmo-call-func (folder func-name &rest args)
76 (let* ((spec (if (stringp folder)
77 (elmo-folder-get-spec folder)
79 (type (symbol-name (car spec)))
80 (backend-str (concat "elmo-" type))
81 (backend-sym (intern backend-str)))
82 (unless (featurep backend-sym)
83 (require backend-sym))
84 (apply (intern (format "%s-%s" backend-str func-name))
88 ;; Nemacs's `read' is different.
89 (static-if (fboundp 'nemacs-version)
90 (defun elmo-read (obj)
93 (or (bobp) (forward-char -1)))))
94 (defalias 'elmo-read 'read))
96 (defmacro elmo-set-work-buf (&rest body)
97 "Execute BODY on work buffer. Work buffer remains."
99 (set-buffer (get-buffer-create elmo-work-buf-name))
100 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
104 (defmacro elmo-match-substring (pos string from)
105 "Substring of POSth matched string of STRING."
106 (` (substring (, string)
107 (+ (match-beginning (, pos)) (, from))
108 (match-end (, pos)))))
110 (defmacro elmo-match-string (pos string)
111 "Substring POSth matched STRING."
112 (` (substring (, string) (match-beginning (, pos)) (match-end (, pos)))))
114 (defmacro elmo-match-buffer (pos)
115 "Substring POSth matched from the current buffer."
116 (` (buffer-substring-no-properties
117 (match-beginning (, pos)) (match-end (, pos)))))
119 (defmacro elmo-bind-directory (dir &rest body)
120 "Set current directory DIR and execute BODY."
121 (` (let ((default-directory (file-name-as-directory (, dir))))
124 (defmacro elmo-folder-get-type (folder)
125 "Get type of FOLDER."
126 (` (and (stringp (, folder))
127 (cdr (assoc (string-to-char (, folder)) elmo-spec-alist)))))
129 (defun elmo-object-load (filename &optional mime-charset no-err)
130 "Load OBJECT from the file specified by FILENAME.
131 File content is decoded with MIME-CHARSET."
132 (if (not (file-readable-p filename))
135 (as-binary-input-file
136 (insert-file-contents filename))
138 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
139 (decode-mime-charset-region (point-min) (point-max) mime-charset))
141 (read (current-buffer))
142 (error (unless no-err
143 (message "Warning: Loading object from %s failed."
145 (elmo-object-save filename nil))
148 (defsubst elmo-save-buffer (filename &optional mime-charset)
149 "Save current buffer to the file specified by FILENAME.
150 Directory of the file is created if it doesn't exist.
151 File content is encoded with MIME-CHARSET."
152 (let ((dir (directory-file-name (file-name-directory filename))))
153 (if (file-directory-p dir)
155 (unless (file-exists-p dir)
156 (elmo-make-directory dir)))
157 (if (file-writable-p filename)
160 ;;; (elmo-set-buffer-multibyte default-enable-multibyte-characters)
161 (encode-mime-charset-region (point-min) (point-max) mime-charset))
162 (as-binary-output-file
163 (write-region (point-min) (point-max) filename nil 'no-msg)))
164 (message (format "%s is not writable." filename)))))
166 (defun elmo-object-save (filename object &optional mime-charset)
167 "Save OBJECT to the file specified by FILENAME.
168 Directory of the file is created if it doesn't exist.
169 File content is encoded with MIME-CHARSET."
171 (prin1 object (current-buffer))
172 ;;;(princ "\n" (current-buffer))
173 (elmo-save-buffer filename mime-charset)))
175 (defsubst elmo-imap4-decode-folder-string (string)
176 (if elmo-imap4-use-modified-utf7
177 (utf7-decode-string string 'imap)
180 (defsubst elmo-imap4-encode-folder-string (string)
181 (if elmo-imap4-use-modified-utf7
182 (utf7-encode-string string 'imap)
185 (defun elmo-get-network-stream-type (stream-type stream-type-alist)
187 (while stream-type-alist
188 (if (eq (nth 1 (car stream-type-alist)) stream-type)
189 (throw 'found (car stream-type-alist)))
190 (setq stream-type-alist (cdr stream-type-alist)))))
192 (defun elmo-network-get-spec (folder server port stream-type stream-type-alist)
193 (setq stream-type (elmo-get-network-stream-type
194 stream-type stream-type-alist))
195 (when (string-match "\\(@[^@:/!]+\\)?\\(:[0-9]+\\)?\\(!.*\\)?$" folder)
196 (if (match-beginning 1)
197 (setq server (elmo-match-substring 1 folder 1)))
198 (if (match-beginning 2)
199 (setq port (string-to-int (elmo-match-substring 2 folder 1))))
200 (if (match-beginning 3)
201 (setq stream-type (assoc (elmo-match-string 3 folder)
203 (setq folder (substring folder 0 (match-beginning 0))))
204 (cons folder (list server port stream-type)))
206 (defun elmo-imap4-get-spec (folder)
207 (let ((default-user elmo-default-imap4-user)
208 (default-server elmo-default-imap4-server)
209 (default-port elmo-default-imap4-port)
210 (default-stream-type elmo-default-imap4-stream-type)
211 (stream-type-alist elmo-network-stream-type-alist)
212 spec mailbox user auth)
213 (when (string-match "\\(.*\\)@\\(.*\\)" default-server)
214 ;; case: default-imap4-server is specified like
215 ;; "hoge%imap.server@gateway".
216 (setq default-user (elmo-match-string 1 default-server))
217 (setq default-server (elmo-match-string 2 default-server)))
218 (if elmo-imap4-stream-type-alist
219 (setq stream-type-alist
220 (append elmo-imap4-stream-type-alist stream-type-alist)))
221 (setq spec (elmo-network-get-spec
222 folder default-server default-port default-stream-type
224 (setq folder (car spec))
226 "^\\(%\\)\\([^:@!]*\\)\\(:[^/!]+\\)?\\(/[^/:@!]+\\)?"
229 (setq mailbox (if (match-beginning 2)
230 (elmo-match-string 2 folder)
231 elmo-default-imap4-mailbox))
232 (setq user (if (match-beginning 3)
233 (elmo-match-substring 3 folder 1)
235 (setq auth (if (match-beginning 4)
236 (intern (elmo-match-substring 4 folder 1))
237 elmo-default-imap4-authenticate-type))
238 (setq auth (or auth 'clear))
240 (elmo-imap4-encode-folder-string mailbox)
244 (defsubst elmo-imap4-spec-mailbox (spec)
247 (defsubst elmo-imap4-spec-username (spec)
250 (defsubst elmo-imap4-spec-auth (spec)
253 (defsubst elmo-imap4-spec-hostname (spec)
256 (defsubst elmo-imap4-spec-port (spec)
259 (defsubst elmo-imap4-spec-stream-type (spec)
262 (defalias 'elmo-imap4-spec-folder 'elmo-imap4-spec-mailbox)
263 (make-obsolete 'elmo-imap4-spec-folder 'elmo-imap4-spec-mailbox)
265 (defsubst elmo-imap4-connection-get-process (conn)
268 (defsubst elmo-imap4-connection-get-buffer (conn)
271 (defsubst elmo-imap4-connection-get-cwf (conn)
274 (defun elmo-nntp-get-spec (folder)
275 (let ((stream-type-alist elmo-network-stream-type-alist)
277 (if elmo-nntp-stream-type-alist
278 (setq stream-type-alist
279 (append elmo-nntp-stream-type-alist stream-type-alist)))
280 (setq spec (elmo-network-get-spec folder
281 elmo-default-nntp-server
282 elmo-default-nntp-port
283 elmo-default-nntp-stream-type
285 (setq folder (car spec))
287 "^\\(-\\)\\([^:@!]*\\)\\(:[^/!]+\\)?\\(/[^/:@!]+\\)?"
290 (if (match-beginning 2)
291 (elmo-match-string 2 folder)))
293 (if (match-beginning 3)
294 (elmo-match-substring 3 folder 1)
295 elmo-default-nntp-user))
296 (append (list 'nntp group user)
299 (defsubst elmo-nntp-spec-group (spec)
302 (defsubst elmo-nntp-spec-username (spec)
306 ;; (defsubst elmo-nntp-spec-auth (spec))
308 (defsubst elmo-nntp-spec-hostname (spec)
311 (defsubst elmo-nntp-spec-port (spec)
314 (defsubst elmo-nntp-spec-stream-type (spec)
317 (defun elmo-localdir-get-spec (folder)
320 "^\\(\\+\\)\\(.*\\)$"
322 (if (eq (length (setq fld-name
323 (elmo-match-string 2 folder))) 0)
326 (if (file-name-absolute-p fld-name)
327 (setq path (expand-file-name fld-name))
328 ;;; (setq path (expand-file-name fld-name
329 ;;; elmo-localdir-folder-path))
330 (setq path fld-name))
331 (list (if (elmo-folder-maildir-p folder)
335 (defun elmo-maildir-get-spec (folder)
338 "^\\(\\.\\)\\(.*\\)$"
340 (if (eq (length (setq fld-name
341 (elmo-match-string 2 folder))) 0)
343 (if (file-name-absolute-p fld-name)
344 (setq path (expand-file-name fld-name))
345 (setq path fld-name))
346 (list 'maildir path))))
348 (defun elmo-folder-maildir-p (folder)
350 (let ((li elmo-maildir-list))
352 (if (string-match (car li) folder)
354 (setq li (cdr li))))))
356 (defun elmo-localnews-get-spec (folder)
361 (if (eq (length (setq fld-name
362 (elmo-match-string 2 folder))) 0)
366 (elmo-replace-in-string fld-name "\\." "/")))))
368 (defun elmo-cache-get-spec (folder)
373 (if (eq (length (setq fld-name
374 (elmo-match-string 2 folder))) 0)
378 (elmo-replace-in-string fld-name "\\." "/")))))
380 ;; Archive interface by OKUNISHI Fujikazu <fuji0924@mbox.kyoto-inet.or.jp>
381 (defun elmo-archive-get-spec (folder)
382 (require 'elmo-archive)
383 (let (fld-name type prefix)
385 "^\\(\\$\\)\\([^;]*\\);?\\([^;]*\\);?\\([^;]*\\)$"
387 ;; Drive letter is OK!
388 (if (eq (length (setq fld-name
389 (elmo-match-string 2 folder))) 0)
392 (if (eq (length (setq type
393 (elmo-match-string 3 folder))) 0)
394 (setq type (symbol-name elmo-archive-default-type)))
395 (if (eq (length (setq prefix
396 (elmo-match-string 4 folder))) 0)
398 (list 'archive fld-name (intern-soft type) prefix))))
400 (defun elmo-pop3-get-spec (folder)
401 (let ((stream-type-alist elmo-network-stream-type-alist)
403 (if elmo-pop3-stream-type-alist
404 (setq stream-type-alist
405 (append elmo-pop3-stream-type-alist stream-type-alist)))
406 (setq spec (elmo-network-get-spec folder
407 elmo-default-pop3-server
408 elmo-default-pop3-port
409 elmo-default-pop3-stream-type
411 (setq folder (car spec))
413 "^\\(&\\)\\([^:/!]*\\)\\(/[^/:@!]+\\)?"
415 (setq user (if (match-beginning 2)
416 (elmo-match-string 2 folder)))
417 (if (eq (length user) 0)
418 (setq user elmo-default-pop3-user))
419 (setq auth (if (match-beginning 3)
420 (intern (elmo-match-substring 3 folder 1))
421 elmo-default-pop3-authenticate-type))
422 (setq auth (or auth 'user))
423 (append (list 'pop3 user auth)
426 (defsubst elmo-pop3-spec-username (spec)
429 (defsubst elmo-pop3-spec-auth (spec)
432 (defsubst elmo-pop3-spec-hostname (spec)
435 (defsubst elmo-pop3-spec-port (spec)
438 (defsubst elmo-pop3-spec-stream-type (spec)
441 (defun elmo-internal-get-spec (folder)
442 (if (string-match "\\('\\)\\([^/]*\\)/?\\(.*\\)$" folder)
443 (let* ((item (downcase (elmo-match-string 2 folder)))
444 (sym (and (> (length item) 0) (intern item))))
445 (cond ((or (null sym)
447 (list 'internal sym (elmo-match-string 3 folder)))
449 (list 'cache (elmo-match-string 3 folder)))
450 (t (error "Invalid internal folder spec"))))))
452 (defun elmo-multi-get-spec (folder)
455 "^\\(\\*\\)\\(.*\\)$"
457 (append (list 'multi)
459 (elmo-match-string 2 folder)
462 (defun elmo-filter-get-spec (folder)
463 (when (string-match "^\\(/\\)\\(.*\\)$" folder)
464 (let ((folder (elmo-match-string 2 folder))
466 (setq pair (elmo-parse-search-condition folder))
467 (if (string-match "^ */\\(.*\\)$" (cdr pair))
468 (list 'filter (car pair) (elmo-match-string 1 (cdr pair)))
469 (error "Folder syntax error `%s'" folder)))))
471 (defun elmo-pipe-get-spec (folder)
472 (when (string-match "^\\(|\\)\\([^|]*\\)|\\(.*\\)$" folder)
474 (elmo-match-string 2 folder)
475 (elmo-match-string 3 folder))))
477 (defsubst elmo-pipe-spec-src (spec)
480 (defsubst elmo-pipe-spec-dst (spec)
483 (defun elmo-folder-get-spec (folder)
484 "Return spec of FOLDER."
485 (let ((type (elmo-folder-get-type folder)))
488 (funcall (intern (concat "elmo-" (symbol-name type) "-get-spec"))
490 (error "%s is not supported folder type" folder))))
494 (defconst elmo-condition-atom-regexp "[^/ \")|&]*")
496 (defun elmo-read-search-condition (default)
497 "Read search condition string interactively."
498 (elmo-read-search-condition-internal "Search by" default))
500 (defun elmo-read-search-condition-internal (prompt default)
501 (let* ((completion-ignore-case t)
502 (field (completing-read
503 (format "%s (%s): " prompt default)
507 "From" "Subject" "To" "Cc" "Body"
508 "Since" "Before" "ToCc"
509 "!From" "!Subject" "!To" "!Cc" "!Body"
510 "!Since" "!Before" "!ToCc")
511 elmo-msgdb-extra-fields)) nil t))
513 (setq field (if (string= field "")
517 ((or (string= field "AND") (string= field "OR"))
519 (elmo-read-search-condition-internal
520 (concat field "(1) Search by") default)
521 (if (string= field "AND") "&" "|")
522 (elmo-read-search-condition-internal
523 (concat field "(2) Search by") default)
525 ((string-match "Since\\|Before" field)
526 (concat (downcase field) ":"
527 (completing-read (format "Value for '%s': " field)
530 (list (format "%s" (car x)))))
531 elmo-date-descriptions))))
533 (setq value (read-from-minibuffer (format "Value for '%s': " field)))
534 (unless (string-match (concat "^" elmo-condition-atom-regexp "$")
536 (setq value (prin1-to-string value)))
537 (concat (downcase field) ":" value)))))
539 (defsubst elmo-condition-parse-error ()
540 (error "Syntax error in '%s'" (buffer-string)))
542 (defun elmo-parse-search-condition (condition)
544 Return value is a cons cell of (STRUCTURE . REST)"
547 (goto-char (point-min))
548 (cons (elmo-condition-parse) (buffer-substring (point) (point-max)))))
550 ;; condition ::= or-expr
551 (defun elmo-condition-parse ()
552 (or (elmo-condition-parse-or-expr)
553 (elmo-condition-parse-error)))
555 ;; or-expr ::= and-expr /
556 ;; and-expr "|" or-expr
557 (defun elmo-condition-parse-or-expr ()
558 (let ((left (elmo-condition-parse-and-expr)))
559 (if (looking-at "| *")
561 (goto-char (match-end 0))
562 (list 'or left (elmo-condition-parse-or-expr)))
565 ;; and-expr ::= primitive /
566 ;; primitive "&" and-expr
567 (defun elmo-condition-parse-and-expr ()
568 (let ((left (elmo-condition-parse-primitive)))
569 (if (looking-at "& *")
571 (goto-char (match-end 0))
572 (list 'and left (elmo-condition-parse-and-expr)))
575 ;; primitive ::= "(" expr ")" /
576 ;; ["!"] search-key SPACE* ":" SPACE* search-value
577 (defun elmo-condition-parse-primitive ()
580 (goto-char (match-end 0))
581 (prog1 (elmo-condition-parse)
582 (unless (looking-at ") *")
583 (elmo-condition-parse-error))
584 (goto-char (match-end 0))))
585 ;; search-key ::= [A-Za-z-]+
586 ;; ;; "since" / "before" / "last" / "first" /
587 ;; ;; "body" / field-name
588 ((looking-at "\\(!\\)? *\\([A-Za-z-]+\\) *: *")
589 (goto-char (match-end 0))
590 (let ((search-key (vector
591 (if (match-beginning 1) 'unmatch 'match)
592 (elmo-match-buffer 2)
593 (elmo-condition-parse-search-value))))
595 (if (string= (aref search-key 1) "tocc")
596 (if (eq (aref search-key 0) 'match)
598 (vector 'match "to" (aref search-key 2))
599 (vector 'match "cc" (aref search-key 2)))
601 (vector 'unmatch "to" (aref search-key 2))
602 (vector 'unmatch "cc" (aref search-key 2))))
605 ;; search-value ::= quoted / time / number / atom
606 ;; quoted ::= <elisp string expression>
607 ;; time ::= "yesterday" / "lastweek" / "lastmonth" / "lastyear" /
608 ;; number SPACE* "daysago" /
609 ;; number "-" month "-" number ; ex. 10-May-2000
611 ;; month ::= "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
612 ;; "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
613 ;; atom ::= ATOM_CHARS*
614 ;; SPACE ::= <ascii space character, 0x20>
615 ;; ATOM_CHARS ::= <any character except specials>
616 ;; specials ::= SPACE / <"> / </> / <)> / <|> / <&>
617 ;; ;; These characters should be quoted.
618 (defun elmo-condition-parse-search-value ()
621 (elmo-read (current-buffer)))
622 ((or (looking-at "yesterday") (looking-at "lastweek")
623 (looking-at "lastmonth") (looking-at "lastyear")
624 (looking-at "[0-9]+ *daysago")
625 (looking-at "[0-9]+-[A-Za-z]+-[0-9]+")
626 (looking-at "[0-9]+")
627 (looking-at elmo-condition-atom-regexp))
628 (prog1 (elmo-match-buffer 0)
629 (goto-char (match-end 0))))
630 (t (error "Syntax error '%s'" (buffer-string)))))
633 (defun elmo-multi-get-real-folder-number (folder number)
634 (let* ((spec (elmo-folder-get-spec folder))
637 (fld (nth (- (/ num elmo-multi-divide-number) 1) flds)))
638 (cons fld (% num elmo-multi-divide-number))))
640 (defsubst elmo-buffer-replace (regexp &optional newtext)
641 (goto-char (point-min))
642 (while (re-search-forward regexp nil t)
643 (replace-match (or newtext ""))))
645 (defsubst elmo-delete-char (char string &optional unibyte)
648 (let ((coding-system-for-read 'no-conversion)
649 (coding-system-for-write 'no-conversion))
650 (if unibyte (elmo-set-buffer-multibyte nil))
652 (goto-char (point-min))
653 (while (search-forward (char-to-string char) nil t)
657 (defsubst elmo-delete-cr-get-content-type ()
659 (goto-char (point-min))
660 (while (search-forward "\r\n" nil t)
661 (replace-match "\n"))
662 (goto-char (point-min))
663 (or (std11-field-body "content-type")
666 (defun elmo-delete-cr (string)
670 (goto-char (point-min))
671 (while (search-forward "\r\n" nil t)
672 (replace-match "\n"))
675 (defun elmo-uniq-list (lst)
676 "Distractively uniqfy elements of LST."
685 (defun elmo-string-partial-p (string)
686 (and (stringp string) (string-match "message/partial" string)))
688 (defun elmo-get-file-string (filename &optional remove-final-newline)
690 (let (insert-file-contents-pre-hook ; To avoid autoconv-xmas...
691 insert-file-contents-post-hook)
692 (when (file-exists-p filename)
694 (as-binary-input-file (insert-file-contents filename)))
695 (when (and remove-final-newline
697 (= (char-after (1- (point-max))) ?\n))
698 (goto-char (point-max))
699 (delete-backward-char 1))
702 (defun elmo-save-string (string filename)
705 (as-binary-output-file
707 (write-region (point-min) (point-max)
708 filename nil 'no-msg))
711 (defun elmo-max-of-list (nlist)
715 (if (< max-num (car l))
716 (setq max-num (car l)))
720 (defun elmo-concat-path (path filename)
721 (if (not (string= path ""))
722 (if (string= elmo-path-sep (substring path (- (length path) 1)))
723 (concat path filename)
724 (concat path elmo-path-sep filename))
727 (defvar elmo-passwd-alist nil)
729 (defun elmo-passwd-alist-load ()
731 (let ((filename (expand-file-name elmo-passwd-alist-file-name
733 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*"))
734 insert-file-contents-pre-hook ; To avoid autoconv-xmas...
735 insert-file-contents-post-hook
737 (if (not (file-readable-p filename))
739 (set-buffer tmp-buffer)
740 (insert-file-contents filename)
743 (read (current-buffer))
745 (kill-buffer tmp-buffer)
748 (defun elmo-passwd-alist-clear ()
749 "Clear password cache."
751 (setq elmo-passwd-alist nil))
753 (defun elmo-passwd-alist-save ()
754 "Save password into file."
757 (let ((filename (expand-file-name elmo-passwd-alist-file-name
759 (tmp-buffer (get-buffer-create " *elmo-passwd-alist-tmp*")))
760 (set-buffer tmp-buffer)
762 (prin1 elmo-passwd-alist tmp-buffer)
763 (princ "\n" tmp-buffer)
764 ;;; (if (and (file-exists-p filename)
765 ;;; (not (equal 384 (file-modes filename))))
766 ;;; (error "%s is not safe.chmod 600 %s!" filename filename))
767 (if (file-writable-p filename)
769 (write-region (point-min) (point-max)
770 filename nil 'no-msg)
771 (set-file-modes filename 384))
772 (message (format "%s is not writable." filename)))
773 (kill-buffer tmp-buffer))))
775 (defun elmo-get-passwd (key)
776 "Get password from password pool."
778 (if (not elmo-passwd-alist)
779 (setq elmo-passwd-alist (elmo-passwd-alist-load)))
780 (setq pair (assoc key elmo-passwd-alist))
782 (elmo-base64-decode-string (cdr pair))
783 (setq pass (elmo-read-passwd (format "Password for %s: "
785 (setq elmo-passwd-alist
786 (append elmo-passwd-alist
788 (elmo-base64-encode-string pass)))))
789 (if elmo-passwd-life-time
790 (run-with-timer elmo-passwd-life-time nil
791 (` (lambda () (elmo-remove-passwd (, key))))))
794 (defun elmo-remove-passwd (key)
795 "Remove password from password pool (for failure)."
797 (if (setq pass-cons (assoc key elmo-passwd-alist))
800 (fillarray (cdr pass-cons) 0))
801 (setq elmo-passwd-alist
802 (delete pass-cons elmo-passwd-alist))))))
804 (defmacro elmo-read-char-exclusive ()
805 (cond ((featurep 'xemacs)
806 '(let ((table (quote ((backspace . ?\C-h) (delete . ?\C-?)
811 (key-press-event-p (setq event (next-command-event)))
812 (setq key (or (event-to-character event)
813 (cdr (assq (event-key event) table)))))))
815 ((fboundp 'read-char-exclusive)
816 '(read-char-exclusive))
820 (defun elmo-read-passwd (prompt &optional stars)
821 "Read a single line of text from user without echoing, and return it."
825 (cursor-in-echo-area t)
826 (log-message-max-size 0)
827 message-log-max done msg truncate)
829 (if (or (not stars) (string= "" ans))
831 (setq msg (concat prompt (make-string (length ans) ?.)))
833 (1+ (- (length msg) (window-width (minibuffer-window)))))
835 (setq msg (concat "$" (substring msg (1+ truncate))))))
837 (setq c (elmo-read-char-exclusive))
841 ((or (= c ?\r) (= c ?\n) (= c ?\e))
845 ((and (/= c ?\b) (/= c ?\177))
846 (setq ans (concat ans (char-to-string c))))
848 (setq ans (substring ans 0 -1)))))
858 (defun elmo-replace-in-string (str regexp newtext &optional literal)
859 "Replace all matches in STR for REGEXP with NEWTEXT string.
860 And returns the new string.
861 Optional LITERAL non-nil means do a literal replacement.
862 Otherwise treat \\ in NEWTEXT string as special:
863 \\& means substitute original matched text,
864 \\N means substitute match for \(...\) number N,
865 \\\\ means insert one \\."
870 (while (setq match (string-match regexp str start))
871 (setq prev-start start
876 (substring str prev-start match)
877 (cond (literal newtext)
884 (cond ((eq c ?\\) "\\")
886 (elmo-match-string 0 str))
887 ((and (>= c ?0) (<= c ?9))
888 (if (> c (+ ?0 (length
891 (error "Invalid match num: %c" c)
893 (elmo-match-string c str)))
894 (t (char-to-string c))))
895 (if (eq c ?\\) (progn (setq special t) nil)
896 (char-to-string c)))))
898 (concat rtn-str (substring str start))))
900 (defun elmo-string-to-list (string)
903 (goto-char (point-min))
905 (goto-char (point-max))
907 (goto-char (point-min))
908 (read (current-buffer))))
910 (defun elmo-list-to-string (list)
919 (if (symbolp (car tlist))
920 (symbol-name (car tlist))
925 (setq tlist (cdr tlist)))
935 (defun elmo-plug-on-by-servers (alist &optional servers)
936 (let ((server-list (or servers elmo-plug-on-servers)))
939 (if (elmo-plugged-p (car server-list))
941 (setq server-list (cdr server-list))))))
943 (defun elmo-plug-on-by-exclude-servers (alist &optional servers)
944 (let ((server-list (or servers elmo-plug-on-exclude-servers))
945 server other-servers)
947 (when (and (not (member (setq server (caaar alist)) server-list))
948 (not (member server other-servers)))
949 (push server other-servers))
950 (setq alist (cdr alist)))
951 (elmo-plug-on-by-servers alist other-servers)))
953 (defun elmo-plugged-p (&optional server port alist label-exp)
954 (let ((alist (or alist elmo-plugged-alist))
956 (cond ((and (not port) (not server))
957 (cond ((eq elmo-plugged-condition 'one)
960 (if (nth 2 (car alist))
962 (setq alist (cdr alist)))))
963 ((eq elmo-plugged-condition 'all)
966 (if (not (nth 2 (car alist)))
967 (throw 'plugged nil))
968 (setq alist (cdr alist)))
970 ((functionp elmo-plugged-condition)
971 (funcall elmo-plugged-condition alist))
974 ((not port) ;; server
977 (when (string= server (caaar alist))
978 (if (nth 2 (car alist))
980 (setq alist (cdr alist)))))
982 (setq plugged-info (assoc (cons server port) alist))
983 (if (not plugged-info)
984 ;; add elmo-plugged-alist automatically
986 (elmo-set-plugged elmo-plugged server port nil nil label-exp)
988 (if (and elmo-auto-change-plugged
989 (> elmo-auto-change-plugged 0)
990 (nth 3 plugged-info) ;; time
991 (elmo-time-expire (nth 3 plugged-info)
992 elmo-auto-change-plugged))
994 (nth 2 plugged-info)))))))
996 (defun elmo-set-plugged (plugged &optional server port time
998 (let ((alist (or alist elmo-plugged-alist))
1000 (cond ((and (not port) (not server))
1001 (setq elmo-plugged plugged)
1002 ;; set plugged all element of elmo-plugged-alist.
1004 (setcdr (cdar alist) (list plugged time))
1005 (setq alist (cdr alist))))
1007 ;; set plugged all port of server
1009 (when (string= server (caaar alist))
1010 (setcdr (cdar alist) (list plugged time)))
1011 (setq alist (cdr alist))))
1013 ;; set plugged one port of server
1014 (setq plugged-info (assoc (cons server port) alist))
1015 (setq label (if label-exp
1017 (nth 1 plugged-info)))
1019 ;; if add is non-nil, don't reset plug state.
1021 (setcdr plugged-info (list label plugged time)))
1023 (setq elmo-plugged-alist
1024 (nconc elmo-plugged-alist
1026 (list (cons server port) label plugged time))))))))
1029 (defun elmo-delete-plugged (&optional server port alist)
1030 (let* ((alist (or alist elmo-plugged-alist))
1032 (cond ((and (not port) (not server))
1035 ;; delete plugged all port of server
1037 (when (string= server (caaar alist2))
1038 (setq alist (delete (car alist2) alist)))
1039 (setq alist2 (cdr alist2))))
1041 ;; delete plugged one port of server
1043 (delete (assoc (cons server port) alist) alist))))
1046 (defun elmo-disk-usage (path)
1047 "Get disk usage (bytes) in PATH."
1049 (condition-case () (file-attributes path) (error nil))))
1051 (if (nth 0 file-attr) ; directory
1052 (let ((files (condition-case ()
1053 (directory-files path t "^[^\\.]")
1056 ;; (result (nth 7 file-attr))) ... directory size
1058 (setq result (+ result (or (elmo-disk-usage (car files)) 0)))
1059 (setq files (cdr files)))
1061 (float (nth 7 file-attr))))))
1063 (defun elmo-get-last-accessed-time (path &optional dir)
1064 "Return the last accessed time of PATH."
1065 (let ((last-accessed (nth 4 (file-attributes (or (and dir
1070 (setq last-accessed (+ (* (nth 0 last-accessed)
1071 (float 65536)) (nth 1 last-accessed)))
1074 (defun elmo-get-last-modification-time (path &optional dir)
1075 "Return the last accessed time of PATH."
1076 (let ((last-modified (nth 5 (file-attributes (or (and dir
1080 (setq last-modified (+ (* (nth 0 last-modified)
1081 (float 65536)) (nth 1 last-modified)))))
1083 (defun elmo-make-directory (path)
1084 "Create directory recursively."
1085 (let ((parent (directory-file-name (file-name-directory path))))
1086 (if (null (file-directory-p parent))
1087 (elmo-make-directory parent))
1088 (make-directory path)
1089 (if (string= path (expand-file-name elmo-msgdb-dir))
1090 (set-file-modes path (+ (* 64 7) (* 8 0) 0))))) ; chmod 0700
1092 (defun elmo-delete-directory (path &optional no-hierarchy)
1093 "Delete directory recursively."
1094 (let ((dirent (directory-files path))
1095 relpath abspath hierarchy)
1097 (setq relpath (car dirent)
1099 abspath (expand-file-name relpath path))
1100 (when (not (string-match "^\\.\\.?$" relpath))
1101 (if (eq (nth 0 (file-attributes abspath)) t)
1104 (elmo-delete-directory abspath no-hierarchy))
1105 (delete-file abspath))))
1107 (delete-directory path))))
1109 (defun elmo-list-filter (l1 l2)
1112 ;; t means filter all.
1115 (elmo-delete-if (lambda (x) (not (memq x l1))) l2)
1119 (defun elmo-folder-local-p (folder)
1120 "Return whether FOLDER is a local folder or not."
1121 (let ((spec (elmo-folder-get-spec folder)))
1123 (filter (elmo-folder-local-p (nth 2 spec)))
1124 (pipe (elmo-folder-local-p (elmo-pipe-spec-dst spec)))
1126 '(localdir localnews archive maildir internal cache))))))
1128 (defun elmo-folder-writable-p (folder)
1129 (let ((type (elmo-folder-get-type folder)))
1130 (memq type '(imap4 localdir archive))))
1132 (defun elmo-multi-get-intlist-list (numlist &optional as-is)
1133 (let ((numbers (sort numlist '<))
1135 one-list int-list-list)
1137 (setq cur-number (+ cur-number 1))
1142 (* elmo-multi-divide-number cur-number))
1143 elmo-multi-divide-number)))
1144 (setq one-list (nconc
1150 (* elmo-multi-divide-number cur-number))))))
1151 (setq numbers (cdr numbers)))
1152 (setq int-list-list (nconc int-list-list (list one-list))))
1155 (defsubst elmo-list-delete-if-smaller (list number)
1156 (let ((ret-val (copy-sequence list)))
1158 (if (< (car list) number)
1159 (setq ret-val (delq (car list) ret-val)))
1160 (setq list (cdr list)))
1163 (defun elmo-list-diff (list1 list2 &optional mes)
1166 (let ((clist1 (copy-sequence list1))
1167 (clist2 (copy-sequence list2)))
1169 (setq clist1 (delq (car list2) clist1))
1170 (setq list2 (cdr list2)))
1172 (setq clist2 (delq (car list1) clist2))
1173 (setq list1 (cdr list1)))
1175 (message (concat mes "done.")))
1176 (list clist1 clist2)))
1178 (defun elmo-list-bigger-diff (list1 list2 &optional mes)
1179 "Returns a list (- +). + is bigger than max of LIST1, in LIST2."
1184 (max-of-l2 (or (nth (max 0 (1- (length l2))) l2) 0))
1188 (setq num (+ (length l1)))
1190 (if (memq (car l1) l2)
1191 (if (eq (car l1) (car l2))
1194 (if (> (car l1) max-of-l2)
1195 (setq diff1 (nconc diff1 (list (car l1))))))
1199 (setq percent (/ (* i 100) num))
1200 (if (eq (% percent 5) 0)
1201 (elmo-display-progress
1202 'elmo-list-bigger-diff "%s%d%%" percent mes))))
1204 (cons diff1 (list l2)))))
1206 (defun elmo-multi-list-bigger-diff (list1 list2 &optional mes)
1207 (let ((list1-list (elmo-multi-get-intlist-list list1 t))
1208 (list2-list (elmo-multi-get-intlist-list list2 t))
1211 (while (or list1-list list2-list)
1212 (setq result (elmo-list-bigger-diff (car list1-list) (car list2-list)
1214 (setq dels (append dels (car result)))
1215 (setq news (append news (cadr result)))
1216 (setq list1-list (cdr list1-list))
1217 (setq list2-list (cdr list2-list)))
1218 (cons dels (list news))))
1220 (defvar elmo-imap4-name-space-regexp-list nil)
1221 (defun elmo-imap4-identical-name-space-p (fld1 fld2)
1223 (if (or (eq (string-to-char fld1) ?#)
1224 (eq (string-to-char fld2) ?#))
1225 (string= (car (split-string fld1 "/"))
1226 (car (split-string fld2 "/")))
1229 (defun elmo-folder-identical-system-p (folder1 folder2)
1230 "FOLDER1 and FOLDER2 should be real folder (not virtual)."
1231 (cond ((eq (elmo-folder-get-type folder1) 'imap4)
1232 (let ((spec1 (elmo-folder-get-spec folder1))
1233 (spec2 (elmo-folder-get-spec folder2)))
1236 ;;; (elmo-imap4-identical-name-space-p
1237 ;;; (nth 1 spec1) (nth 1 spec2))
1238 (string= (elmo-imap4-spec-hostname spec1)
1239 (elmo-imap4-spec-hostname spec2)) ; hostname
1240 (string= (elmo-imap4-spec-username spec1)
1241 (elmo-imap4-spec-username spec2))))) ; username
1243 (elmo-folder-direct-copy-p folder1 folder2))))
1245 (defun elmo-folder-get-store-type (folder)
1246 (let ((spec (elmo-folder-get-spec folder)))
1248 (filter (elmo-folder-get-store-type (nth 2 spec)))
1249 (pipe (elmo-folder-get-store-type (elmo-pipe-spec-dst spec)))
1250 (multi (elmo-folder-get-store-type (nth 1 spec)))
1253 (defconst elmo-folder-direct-copy-alist
1254 '((localdir . (localdir localnews archive))
1255 (maildir . (maildir localdir localnews archive))
1256 (localnews . (localdir localnews archive))
1257 (archive . (localdir localnews archive))
1258 (cache . (localdir localnews archive))))
1260 (defun elmo-folder-direct-copy-p (src-folder dst-folder)
1261 (let ((src-type (elmo-folder-get-store-type src-folder))
1262 (dst-type (elmo-folder-get-store-type dst-folder))
1264 (and (setq dst-copy-type
1265 (cdr (assq src-type elmo-folder-direct-copy-alist)))
1266 (memq dst-type dst-copy-type))))
1268 (defmacro elmo-filter-type (filter)
1269 (` (aref (, filter) 0)))
1271 (defmacro elmo-filter-key (filter)
1272 (` (aref (, filter) 1)))
1274 (defmacro elmo-filter-value (filter)
1275 (` (aref (, filter) 2)))
1277 (defsubst elmo-buffer-field-primitive-condition-match (condition
1281 (goto-char (point-min))
1283 ((string= (elmo-filter-key condition) "last")
1284 (setq result (<= (length (memq number number-list))
1285 (string-to-int (elmo-filter-value condition)))))
1286 ((string= (elmo-filter-key condition) "first")
1287 (setq result (< (- (length number-list)
1288 (length (memq number number-list)))
1289 (string-to-int (elmo-filter-value condition)))))
1290 ((string= (elmo-filter-key condition) "since")
1291 (let ((date (elmo-date-get-datevec (elmo-filter-value condition))))
1294 (timezone-make-sortable-date (aref date 0)
1297 (timezone-make-time-string
1301 (timezone-make-date-sortable (std11-field-body "date"))))))
1302 ((string= (elmo-filter-key condition) "before")
1303 (let ((date (elmo-date-get-datevec (elmo-filter-value condition))))
1306 (timezone-make-date-sortable (std11-field-body "date"))
1307 (timezone-make-sortable-date (aref date 0)
1310 (timezone-make-time-string
1314 ((string= (elmo-filter-key condition) "body")
1315 (and (re-search-forward "^$" nil t) ; goto body
1316 (setq result (search-forward (elmo-filter-value condition)
1319 (let ((fval (std11-field-body (elmo-filter-key condition))))
1320 (if (eq (length fval) 0) (setq fval nil))
1321 (if fval (setq fval (eword-decode-string fval)))
1322 (setq result (and fval (string-match
1323 (elmo-filter-value condition) fval))))))
1324 (if (eq (elmo-filter-type condition) 'unmatch)
1325 (setq result (not result)))
1328 (defun elmo-condition-find-key-internal (condition key)
1330 ((vectorp condition)
1331 (if (string= (elmo-filter-key condition) key)
1333 ((or (eq (car condition) 'and)
1334 (eq (car condition) 'or))
1335 (elmo-condition-find-key-internal (nth 1 condition) key)
1336 (elmo-condition-find-key-internal (nth 2 condition) key))))
1338 (defun elmo-condition-find-key (condition key)
1340 (elmo-condition-find-key-internal condition key)))
1342 (defun elmo-buffer-field-condition-match (condition number number-list)
1344 ((vectorp condition)
1345 (elmo-buffer-field-primitive-condition-match
1346 condition number number-list))
1347 ((eq (car condition) 'and)
1348 (and (elmo-buffer-field-condition-match
1349 (nth 1 condition) number number-list)
1350 (elmo-buffer-field-condition-match
1351 (nth 2 condition) number number-list)))
1352 ((eq (car condition) 'or)
1353 (or (elmo-buffer-field-condition-match
1354 (nth 1 condition) number number-list)
1355 (elmo-buffer-field-condition-match
1356 (nth 2 condition) number number-list)))))
1358 (defsubst elmo-file-field-condition-match (file condition number number-list)
1360 (as-binary-input-file (insert-file-contents file))
1361 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
1362 ;; Should consider charset?
1363 (decode-mime-charset-region (point-min)(point-max) elmo-mime-charset)
1364 (elmo-buffer-field-condition-match condition number number-list)))
1366 (defmacro elmo-get-hash-val (string hashtable)
1367 (let ((sym (list 'intern-soft string hashtable)))
1368 (list 'if (list 'boundp sym)
1369 (list 'symbol-value sym))))
1371 (defmacro elmo-set-hash-val (string value hashtable)
1372 (list 'set (list 'intern string hashtable) value))
1374 (defmacro elmo-clear-hash-val (string hashtable)
1375 (static-if (fboundp 'unintern)
1376 (list 'unintern string hashtable)
1377 (list 'makunbound (list 'intern string hashtable))))
1379 (defmacro elmo-unintern (string)
1380 "`unintern' symbol named STRING, When can use `unintern'.
1381 Emacs 19.28 or earlier does not have `unintern'."
1382 (static-if (fboundp 'unintern)
1383 (list 'unintern string)))
1385 ;; Make a hash table (default and minimum size is 1024).
1386 (defun elmo-make-hash (&optional hashsize)
1388 (if hashsize (max (min (elmo-create-hash-size hashsize)
1389 elmo-hash-maximum-size) 1024) 1024) 0))
1391 (defsubst elmo-mime-string (string)
1392 "Normalize MIME encoded STRING."
1396 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
1397 (setq str (eword-decode-string
1398 (decode-mime-charset-string string elmo-mime-charset)))
1399 (setq str (encode-mime-charset-string str elmo-mime-charset))
1400 (elmo-set-buffer-multibyte nil)
1403 (defsubst elmo-collect-field (beg end downcase-field-name)
1406 (narrow-to-region beg end)
1407 (goto-char (point-min))
1408 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1410 (while (re-search-forward regexp nil t)
1411 (setq name (buffer-substring-no-properties
1412 (match-beginning 1)(1- (match-end 1))))
1413 (if downcase-field-name
1414 (setq name (downcase name)))
1415 (setq body (buffer-substring-no-properties
1416 (match-end 0) (std11-field-end)))
1417 (or (assoc name dest)
1418 (setq dest (cons (cons name body) dest))))
1421 (defsubst elmo-collect-field-from-string (string downcase-field-name)
1424 (goto-char (point-min))
1425 (let ((regexp (concat "\\(" std11-field-head-regexp "\\)[ \t]*"))
1427 (while (re-search-forward regexp nil t)
1428 (setq name (buffer-substring-no-properties
1429 (match-beginning 1)(1- (match-end 1))))
1430 (if downcase-field-name
1431 (setq name (downcase name)))
1432 (setq body (buffer-substring-no-properties
1433 (match-end 0) (std11-field-end)))
1434 (or (assoc name dest)
1435 (setq dest (cons (cons name body) dest))))
1438 (defun elmo-create-hash-size (min)
1444 (defun elmo-safe-filename (folder)
1445 (elmo-replace-in-string
1446 (elmo-replace-in-string
1447 (elmo-replace-in-string folder "/" " ")
1451 (defvar elmo-msgid-replace-chars nil)
1453 (defsubst elmo-replace-msgid-as-filename (msgid)
1454 "Replace Message-ID string (MSGID) as filename."
1455 (setq msgid (elmo-replace-in-string msgid " " " "))
1456 (if (null elmo-msgid-replace-chars)
1457 (setq elmo-msgid-replace-chars
1458 (regexp-quote (mapconcat
1459 'car elmo-msgid-replace-string-alist ""))))
1460 (while (string-match (concat "[" elmo-msgid-replace-chars "]")
1463 (substring msgid 0 (match-beginning 0))
1466 (match-beginning 0) (match-end 0))
1467 elmo-msgid-replace-string-alist))
1468 (substring msgid (match-end 0)))))
1471 (defsubst elmo-recover-msgid-from-filename (filename)
1472 "Recover Message-ID from FILENAME."
1474 (while (string-match " " filename)
1475 (setq tmp (substring filename
1477 (+ (match-end 0) 1)))
1478 (if (string= tmp " ")
1480 (setq tmp (car (rassoc tmp
1481 elmo-msgid-replace-string-alist))))
1484 (substring filename 0 (match-beginning 0))
1486 (setq filename (substring filename (+ (match-end 0) 1))))
1487 (concat result filename)))
1489 (defsubst elmo-copy-file (src dst)
1491 (elmo-add-name-to-file src dst t)
1492 (error (copy-file src dst t))))
1494 (defsubst elmo-buffer-exists-p (buffer)
1495 (if (bufferp buffer)
1496 (buffer-live-p buffer)
1497 (get-buffer buffer)))
1499 (defsubst elmo-kill-buffer (buffer)
1500 (when (elmo-buffer-exists-p buffer)
1501 (kill-buffer buffer)))
1503 (defun elmo-delete-if (pred lst)
1504 "Return new list contain items which don't satisfy PRED in LST."
1507 (unless (funcall pred (car lst))
1508 (setq result (nconc result (list (car lst)))))
1509 (setq lst (cdr lst)))
1512 (defun elmo-list-delete (list1 list2)
1513 "Delete by side effect any occurrences equal to elements of LIST1 from LIST2.
1514 Return the modified LIST2. Deletion is done with `delete'.
1515 Write `(setq foo (elmo-list-delete bar foo))' to be sure of changing
1516 the value of `foo'."
1518 (setq list2 (delete (car list1) list2))
1519 (setq list1 (cdr list1)))
1522 (defun elmo-list-member (list1 list2)
1523 "If any element of LIST1 is member of LIST2, return t."
1526 (if (member (car list1) list2)
1528 (setq list1 (cdr list1)))))
1530 (defun elmo-count-matches (regexp beg end)
1534 (while (re-search-forward regexp end t)
1535 (setq count (1+ count)))
1538 (if (fboundp 'display-error)
1539 (defalias 'elmo-display-error 'display-error)
1540 (defun elmo-display-error (error-object stream)
1541 "A tiny function to display ERROR-OBJECT to the STREAM."
1543 (errobj error-object)
1546 (setq err-mes (concat err-mes (format
1547 (if (stringp (car errobj))
1549 (if (boundp 'nemacs-version)
1551 "%S")) (car errobj))))
1552 (setq errobj (cdr errobj))
1553 (if errobj (setq err-mes (concat err-mes (if first ": " ", "))))
1555 (princ err-mes stream))))
1557 (if (fboundp 'define-error)
1558 (defalias 'elmo-define-error 'define-error)
1559 (defun elmo-define-error (error doc &optional parents)
1561 (setq parents 'error))
1562 (let ((conds (get parents 'error-conditions)))
1564 (error "Not an error symbol: %s" error))
1566 (list 'error-message doc
1567 'error-conditions (cons error conds))))))
1569 (cond ((fboundp 'lprogress-display)
1570 (defalias 'elmo-display-progress 'lprogress-display))
1571 ((fboundp 'progress-feedback-with-label)
1572 (defalias 'elmo-display-progress 'progress-feedback-with-label))
1574 (defun elmo-display-progress (label format &optional value &rest args)
1575 "Print a progress message."
1576 (if (and (null format) (null args))
1578 (apply (function message) (concat format " %d%%")
1579 (nconc args (list value)))))))
1581 (defun elmo-time-expire (before-time diff-time)
1582 (let* ((current (current-time))
1583 (rest (when (< (nth 1 current) (nth 1 before-time))
1587 (list (- (+ (car current) (if rest -1 0)) (car before-time))
1588 (- (+ (or rest 0) (nth 1 current)) (nth 1 before-time))))
1589 (and (eq (car diff) 0)
1590 (< diff-time (nth 1 diff)))))
1592 (if (fboundp 'std11-fetch-field)
1593 (defalias 'elmo-field-body 'std11-fetch-field) ;;no narrow-to-region
1594 (defalias 'elmo-field-body 'std11-field-body))
1596 (defmacro elmo-string (string)
1597 "STRING without text property."
1598 (` (let ((obj (copy-sequence (, string))))
1599 (set-text-properties 0 (length obj) nil obj)
1602 (defun elmo-flatten (list-of-list)
1603 "Flatten LIST-OF-LIST."
1604 (unless (null list-of-list)
1605 (append (if (and (car list-of-list)
1606 (listp (car list-of-list)))
1608 (list (car list-of-list)))
1609 (elmo-flatten (cdr list-of-list)))))
1611 (defun elmo-y-or-n-p (prompt &optional auto default)
1612 "Same as `y-or-n-p'.
1613 But if optional argument AUTO is non-nil, DEFAULT is returned."
1618 (defun elmo-string-member (string slist)
1619 "Return t if STRING is a member of the SLIST."
1622 (if (and (stringp (car slist))
1623 (string= string (car slist)))
1625 (setq slist (cdr slist)))))
1627 (defun elmo-string-match-member (str list &optional case-ignore)
1628 (let ((case-fold-search case-ignore))
1631 (if (string-match (car list) str)
1632 (throw 'member (car list)))
1633 (setq list (cdr list))))))
1635 (defun elmo-string-matched-member (str list &optional case-ignore)
1636 (let ((case-fold-search case-ignore))
1639 (if (string-match str (car list))
1640 (throw 'member (car list)))
1641 (setq list (cdr list))))))
1643 (defsubst elmo-string-delete-match (string pos)
1644 (concat (substring string
1645 0 (match-beginning pos))
1650 (defun elmo-string-match-assoc (key alist &optional case-ignore)
1651 (let ((case-fold-search case-ignore)
1655 (setq a (car alist))
1658 (string-match key (car a)))
1660 (setq alist (cdr alist))))))
1662 (defun elmo-string-matched-assoc (key alist &optional case-ignore)
1663 (let ((case-fold-search case-ignore)
1667 (setq a (car alist))
1670 (string-match (car a) key))
1672 (setq alist (cdr alist))))))
1674 (defun elmo-string-assoc (key alist)
1678 (setq a (car alist))
1681 (string= key (car a)))
1683 (setq alist (cdr alist))))))
1685 (defun elmo-string-rassoc (key alist)
1689 (setq a (car alist))
1692 (string= key (cdr a)))
1694 (setq alist (cdr alist))))))
1696 (defun elmo-string-rassoc-all (key alist)
1699 (if (string= key (cdr (car alist)))
1703 (setq alist (cdr alist)))
1706 ;;; Number set defined by OKAZAKI Tetsurou <okazaki@be.to>
1708 ;; number ::= [0-9]+
1711 ;; number-range ::= "(" beg " . " end ")" ;; cons cell
1712 ;; number-set-elem ::= number / number-range
1713 ;; number-set ::= "(" *number-set-elem ")" ;; list
1715 (defun elmo-number-set-member (number number-set)
1716 "Return non-nil if NUMBER is an element of NUMBER-SET.
1717 The value is actually the tail of NUMBER-RANGE whose car contains NUMBER."
1718 (or (memq number number-set)
1720 (while (and number-set (not found))
1721 (if (and (consp (car number-set))
1722 (and (<= (car (car number-set)) number)
1723 (<= number (cdr (car number-set)))))
1725 (setq number-set (cdr number-set))))
1728 (defun elmo-number-set-append-list (number-set list)
1729 "Append LIST of numbers to the NUMBER-SET.
1730 NUMBER-SET is altered."
1731 (let ((appended number-set))
1733 (setq appended (elmo-number-set-append appended (car list)))
1734 (setq list (cdr list)))
1737 (defun elmo-number-set-append (number-set number)
1738 "Append NUMBER to the NUMBER-SET.
1739 NUMBER-SET is altered."
1740 (let ((number-set-1 number-set)
1742 (while (and number-set (not found))
1743 (setq elem (car number-set))
1746 (eq (+ 1 (cdr elem)) number))
1747 (setcdr elem number)
1749 ((and (integerp elem)
1750 (eq (+ 1 elem) number))
1751 (setcar number-set (cons elem number))
1753 ((or (and (integerp elem) (eq elem number))
1755 (<= (car elem) number)
1756 (<= number (cdr elem))))
1758 (setq number-set (cdr number-set)))
1760 (setq number-set-1 (nconc number-set-1 (list number))))
1764 (product-provide (provide 'elmo-util) (require 'elmo-version))
1766 ;;; elmo-util.el ends here