| 1 |
;;; mew-func.el --- Basic functions for Mew |
| 2 |
|
| 3 |
;; Author: Kazu Yamamoto <[email protected]> |
| 4 |
;; Created: Mar 23, 1997 |
| 5 |
|
| 6 |
;;; Code: |
| 7 |
|
| 8 |
(require 'mew) |
| 9 |
|
| 10 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 11 |
;;; |
| 12 |
;;; Mode |
| 13 |
;;; |
| 14 |
|
| 15 |
(defun mew-summary-p () |
| 16 |
(eq major-mode 'mew-summary-mode)) |
| 17 |
|
| 18 |
(defun mew-virtual-p () |
| 19 |
(eq major-mode 'mew-virtual-mode)) |
| 20 |
|
| 21 |
(defun mew-message-p () |
| 22 |
(eq major-mode 'mew-message-mode)) |
| 23 |
|
| 24 |
(defun mew-draft-p () |
| 25 |
(eq major-mode 'mew-draft-mode)) |
| 26 |
|
| 27 |
(defun mew-header-p () |
| 28 |
(eq major-mode 'mew-header-mode)) |
| 29 |
|
| 30 |
(defun mew-draft-or-header-p () |
| 31 |
(memq major-mode '(mew-draft-mode mew-header-mode))) |
| 32 |
|
| 33 |
(defun mew-selection-p () |
| 34 |
(eq (mew-vinfo-get-mode) 'selection)) |
| 35 |
|
| 36 |
(defun mew-thread-p () |
| 37 |
(eq (mew-vinfo-get-mode) 'thread)) |
| 38 |
|
| 39 |
(defun mew-summary-or-virtual-p () |
| 40 |
(or (mew-summary-p) (mew-virtual-p))) |
| 41 |
|
| 42 |
(defun mew-virtual-for-one-summary () |
| 43 |
(mew-vinfo-get-physical-folder)) |
| 44 |
|
| 45 |
(defun mew-pickable-p () |
| 46 |
(or (mew-summary-p) (mew-virtual-for-one-summary))) |
| 47 |
|
| 48 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 49 |
;;; |
| 50 |
;;; List functions |
| 51 |
;;; |
| 52 |
|
| 53 |
(defun mew-case-equal (str1 str2) |
| 54 |
(string= (downcase str1) (downcase str2))) |
| 55 |
|
| 56 |
(defun mew-folder-recursive-match (key folder) |
| 57 |
"Initial substring match for folders. |
| 58 |
If FOLDER is a sub-folder of KEY or KEY itself, t is returned." |
| 59 |
;; file-name-as-directory should be first |
| 60 |
;; because the path separator may be regex-non-safe. |
| 61 |
(let ((regex (if (string-match "^[-+%$]$" key) |
| 62 |
(mew-folder-regex key) |
| 63 |
(mew-folder-regex (file-name-as-directory key))))) |
| 64 |
(string-match regex (file-name-as-directory folder)))) |
| 65 |
|
| 66 |
(defun mew-member-case-equal (str list) |
| 67 |
"Return the position equal to STR in LIST. Case is ignored." |
| 68 |
(let ((n 0) (dstr (downcase str))) |
| 69 |
(catch 'member |
| 70 |
(dolist (x list) |
| 71 |
(if (string= (downcase x) dstr) (throw 'member n)) |
| 72 |
(setq n (1+ n)))))) |
| 73 |
|
| 74 |
(defun mew-member* (x list) |
| 75 |
"Member in a nested list." |
| 76 |
(catch 'found |
| 77 |
(dolist (ent list) |
| 78 |
(if (consp ent) |
| 79 |
(if (mew-member* x ent) (throw 'found t)) |
| 80 |
(if (equal x ent) (throw 'found t)))))) |
| 81 |
|
| 82 |
(defun mew-member-match (str list &optional ignore-case) |
| 83 |
"Return the position matched to STR in LIST. If |
| 84 |
IGNORE-CASE is t, matching is performed by ignoring case." |
| 85 |
(let ((n 0) (case-fold-search ignore-case)) |
| 86 |
(catch 'member |
| 87 |
(dolist (x list) |
| 88 |
(if (string-match x str) (throw 'member n)) |
| 89 |
(setq n (1+ n)))))) |
| 90 |
|
| 91 |
(defun mew-member-match2 (regex list &optional ignore-case) |
| 92 |
(let ((case-fold-search ignore-case)) |
| 93 |
(catch 'member |
| 94 |
(dolist (x list) |
| 95 |
(if (string-match regex x) (throw 'member x)))))) |
| 96 |
|
| 97 |
(defun mew-uniq-list (lst) |
| 98 |
"Destructively uniqfy elements of LST. |
| 99 |
This is O(N^2). So, do not use this function with a large LST." |
| 100 |
(let ((tmp lst)) |
| 101 |
(while tmp (setq tmp (setcdr tmp (delete (car tmp) (cdr tmp)))))) |
| 102 |
lst) |
| 103 |
|
| 104 |
(defun mew-uniq-alist (alist) |
| 105 |
"Uniqfy elements of ALIST." |
| 106 |
(let ((vec (make-vector 511 0)) ;; hash |
| 107 |
str ret) |
| 108 |
(dolist (ent alist) |
| 109 |
(setq str (car ent)) |
| 110 |
(cond |
| 111 |
((not (stringp str)) |
| 112 |
(setq ret (cons ent ret))) |
| 113 |
((intern-soft str vec) |
| 114 |
()) |
| 115 |
(t |
| 116 |
(setq ret (cons ent ret)) |
| 117 |
(intern str vec)))) |
| 118 |
(nreverse ret))) |
| 119 |
|
| 120 |
(defun mew-delete (key alist) |
| 121 |
"Destructively delete elements whose first member is equal to key" |
| 122 |
(if (null key) |
| 123 |
alist |
| 124 |
(let (ret) |
| 125 |
(while (and (listp (nth 0 alist)) (equal (car (nth 0 alist)) key)) |
| 126 |
(setq alist (cdr alist))) |
| 127 |
(setq ret alist) |
| 128 |
(while alist ;; cannot use dolist |
| 129 |
(if (and (listp (nth 1 alist)) (equal (car (nth 1 alist)) key)) |
| 130 |
(setcdr alist (cdr (cdr alist))) |
| 131 |
(setq alist (cdr alist)))) |
| 132 |
ret))) |
| 133 |
|
| 134 |
(defmacro mew-ntake (n lst) |
| 135 |
`(if (> (length ,lst) ,n) (setcdr (nthcdr (1- ,n) ,lst) nil))) |
| 136 |
|
| 137 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 138 |
;;; |
| 139 |
;;; Associative list functions |
| 140 |
;;; |
| 141 |
|
| 142 |
(defun mew-assoc-equal (key alist nth &optional equal-func) |
| 143 |
(let ((func (or equal-func 'equal)) |
| 144 |
n) |
| 145 |
(catch 'loop |
| 146 |
(dolist (a alist) |
| 147 |
(setq n (nth nth a)) |
| 148 |
(if (or (funcall func n key) (eq n t)) (throw 'loop a)))))) |
| 149 |
|
| 150 |
(defun mew-assoc-case-equal (key alist nth) |
| 151 |
(let ((skey (downcase key)) n) |
| 152 |
(catch 'loop |
| 153 |
(dolist (a alist) |
| 154 |
(setq n (nth nth a)) |
| 155 |
(if (or (and (stringp n) (string= (downcase n) skey)) |
| 156 |
(eq n t)) |
| 157 |
(throw 'loop a)))))) |
| 158 |
|
| 159 |
(defun mew-assoc-match (key alist nth) |
| 160 |
"Return list in ALIST that KEY regex is matched to its NTH element. |
| 161 |
Case is ignored. Note that the NTH element is 't', |
| 162 |
the list is always selected." |
| 163 |
(let ((case-fold-search t) n) |
| 164 |
(catch 'loop |
| 165 |
(dolist (a alist) |
| 166 |
(setq n (nth nth a)) |
| 167 |
(if (or (and (stringp n) (string-match key n)) |
| 168 |
(equal n key) (eq n t)) |
| 169 |
(throw 'loop a)))))) |
| 170 |
|
| 171 |
(defun mew-assoc-match2 (key alist nth) |
| 172 |
"Return list in ALIST whose NTH regex is matched to KEY. |
| 173 |
Case is ignored. Note that the NTH element is 't', |
| 174 |
the list is always selected." |
| 175 |
(let ((case-fold-search t) n) |
| 176 |
(catch 'loop |
| 177 |
(dolist (a alist) |
| 178 |
(setq n (nth nth a)) |
| 179 |
(if (or (and (stringp n) (string-match n key)) |
| 180 |
(equal n key) (eq n t)) |
| 181 |
(throw 'loop a)))))) |
| 182 |
|
| 183 |
(defun mew-assoc-match3 (key alist nth) |
| 184 |
"Return list in ALIST whose NTH regex is matched to KEY. |
| 185 |
Case is ignored. Note that the NTH element is 't', |
| 186 |
the list is always selected. The deference from mew-assoc-match2 |
| 187 |
is that this returns the position of a selected list in addition |
| 188 |
to the list itself." |
| 189 |
(let ((case-fold-search t) (i 0) n) |
| 190 |
(catch 'loop |
| 191 |
(dolist (a alist) |
| 192 |
(setq n (nth nth a)) |
| 193 |
(if (or (and (stringp n) (string-match n key)) |
| 194 |
(equal n key) (eq n t)) |
| 195 |
(throw 'loop (cons i a))) |
| 196 |
(setq i (1+ i)))))) |
| 197 |
|
| 198 |
(defun mew-assoc-member (key lol nth) |
| 199 |
"Return a list member of LoL whose NTH list contains |
| 200 |
a member equal to KEY." |
| 201 |
(mew-assoc-member-base key lol nth 'member)) |
| 202 |
|
| 203 |
(defun mew-assoc-member-case-equal (key lol nth) |
| 204 |
"Return a list member of LoL whose NTH list contains |
| 205 |
a member equal to KEY ignoring case." |
| 206 |
(mew-assoc-member-base key lol nth 'mew-member-case-equal)) |
| 207 |
|
| 208 |
(defun mew-assoc-member-base (key lol nth func) |
| 209 |
"Return a list member of LoL whose NTH list contains KEY |
| 210 |
in the context of FUNC." |
| 211 |
(catch 'loop |
| 212 |
(dolist (l lol) |
| 213 |
(if (and (listp (nth nth l)) (funcall func key (nth nth l))) |
| 214 |
(throw 'loop l))))) |
| 215 |
|
| 216 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 217 |
;;; |
| 218 |
;;; backward compatibility of alist |
| 219 |
;;; |
| 220 |
|
| 221 |
(defalias 'mew-alist-get-key 'car) |
| 222 |
(defun mew-alist-get-value (ent) |
| 223 |
(let ((value (cdr ent))) |
| 224 |
(if (consp value) |
| 225 |
(car value) ;; new |
| 226 |
value))) ;; old |
| 227 |
|
| 228 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 229 |
;;; |
| 230 |
;;; getting next |
| 231 |
;;; |
| 232 |
|
| 233 |
(defun mew-get-next (LIST MEM) |
| 234 |
"Return of a member in LIST which is the next member of MEM." |
| 235 |
(let (frst next crnt) |
| 236 |
(setq frst (car LIST)) |
| 237 |
(setq LIST (cdr LIST)) |
| 238 |
(setq next (car LIST)) |
| 239 |
(if (equal frst MEM) |
| 240 |
(if next next frst) |
| 241 |
(catch 'loop |
| 242 |
(while LIST ;; cannot use dolist |
| 243 |
(setq crnt next) |
| 244 |
(setq LIST (cdr LIST)) |
| 245 |
(setq next (car LIST)) |
| 246 |
(if (equal crnt MEM) |
| 247 |
(throw 'loop (if next next frst)))))))) |
| 248 |
|
| 249 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 250 |
;;; |
| 251 |
;;; Modifying list |
| 252 |
;;; |
| 253 |
|
| 254 |
(defmacro mew-add-first (variable value) |
| 255 |
`(setq ,variable (cons ,value ,variable))) |
| 256 |
|
| 257 |
(defmacro mew-addq (variable value) |
| 258 |
`(if (and ,value (not (member ,value ,variable))) |
| 259 |
(setq ,variable (cons ,value ,variable)))) |
| 260 |
|
| 261 |
(defmacro mew-insert-after (variable value key) |
| 262 |
`(let ((var ,variable)) |
| 263 |
(catch 'loop |
| 264 |
(while var ;; cannot use dolist |
| 265 |
(if (equal (nth 0 (car var)) ,key) |
| 266 |
(throw 'loop (setcdr var (cons ,value (cdr var))))) |
| 267 |
(setq var (cdr var)))))) |
| 268 |
|
| 269 |
(defmacro mew-replace-with (variable value key) |
| 270 |
`(let ((var ,variable)) |
| 271 |
(catch 'loop |
| 272 |
(while var ;; cannot use dolist |
| 273 |
(if (equal (nth 0 (car var)) ,key) |
| 274 |
(throw 'loop (setcar var ,value))) |
| 275 |
(setq var (cdr var)))))) |
| 276 |
|
| 277 |
(defmacro mew-remove-entry (variable key) |
| 278 |
`(let ((crn ,variable) prv) |
| 279 |
(if (equal (nth 0 (car crn)) ,key) |
| 280 |
(setq ,variable (cdr crn)) |
| 281 |
(setq prv crn) |
| 282 |
(setq crn (cdr crn)) |
| 283 |
(catch 'loop |
| 284 |
(while crn ;; cannot use dolist |
| 285 |
(if (equal (nth 0 (car crn)) ,key) |
| 286 |
(throw 'loop (setcdr prv (cdr crn)))) |
| 287 |
(setq prv crn) |
| 288 |
(setq crn (cdr crn))))))) |
| 289 |
|
| 290 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 291 |
;;; |
| 292 |
;;; String |
| 293 |
;;; |
| 294 |
|
| 295 |
(defun mew-make-string (len) |
| 296 |
(make-string len ?a)) |
| 297 |
|
| 298 |
(defun mew-replace-character (string from to) |
| 299 |
"Replace characters equal to FROM to TO in STRING." |
| 300 |
(dotimes (cnt (length string)) |
| 301 |
(if (char-equal (aref string cnt) from) |
| 302 |
(aset string cnt to))) |
| 303 |
string) |
| 304 |
|
| 305 |
(defun mew-replace-white-space (string) |
| 306 |
"Replace white characters to a space." |
| 307 |
(while (string-match "[\n\t]+" string) |
| 308 |
(setq string (replace-match " " nil t string))) |
| 309 |
(while (string-match " +" string) |
| 310 |
(setq string (replace-match " " nil t string))) |
| 311 |
string) |
| 312 |
|
| 313 |
(defun mew-replace-white-space2 (string) |
| 314 |
"Replace white characters to under score." |
| 315 |
(while (string-match "[\n\t\r ]+" string) |
| 316 |
(setq string (replace-match "_" nil t string))) |
| 317 |
string) |
| 318 |
|
| 319 |
(defun mew-capitalize (ostr) |
| 320 |
"Syntax table independent version of capitalize. |
| 321 |
Words are separated by '/' and '-'." |
| 322 |
(let* ((len (length ostr)) |
| 323 |
(nstr (mew-make-string len)) |
| 324 |
(topp t) c) |
| 325 |
(dotimes (i len) |
| 326 |
(setq c (aref ostr i)) |
| 327 |
(cond |
| 328 |
(topp |
| 329 |
(aset nstr i (upcase c)) |
| 330 |
(setq topp nil)) |
| 331 |
((or (char-equal c ?/) (char-equal c ?-)) |
| 332 |
(aset nstr i c) |
| 333 |
(setq topp t)) |
| 334 |
(t |
| 335 |
(aset nstr i (downcase c))))) |
| 336 |
nstr)) |
| 337 |
|
| 338 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 339 |
;;; |
| 340 |
;;; Insertion |
| 341 |
;;; |
| 342 |
|
| 343 |
(defun mew-insert (form str) |
| 344 |
(when str |
| 345 |
(if form |
| 346 |
(insert (format form str)) |
| 347 |
(insert str)))) |
| 348 |
|
| 349 |
(defun mew-insert-message (fld msg rcs size) |
| 350 |
(let ((file (mew-expand-msg fld msg))) |
| 351 |
(cond |
| 352 |
((not (file-readable-p file)) |
| 353 |
(error "%s does not exist" (mew-concat-folder fld msg))) |
| 354 |
((= (mew-file-get-size file) 0) |
| 355 |
(error "The size of %s is 0" (mew-concat-folder fld msg))) |
| 356 |
((file-readable-p file) |
| 357 |
(let ((old-cs (and (boundp 'buffer-file-coding-system) |
| 358 |
buffer-file-coding-system))) |
| 359 |
(mew-frwlet rcs mew-cs-dummy |
| 360 |
(mew-insert-file-contents file nil 0 size)) |
| 361 |
(if (boundp 'buffer-file-coding-system) |
| 362 |
(setq buffer-file-coding-system old-cs))) |
| 363 |
;; return physical size |
| 364 |
(cons (mew-file-get-time file) (mew-file-get-size file)))))) |
| 365 |
|
| 366 |
(defun mew-insert-manual (&rest args) |
| 367 |
(insert (substitute-command-keys (apply 'concat args)))) |
| 368 |
|
| 369 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 370 |
;;; |
| 371 |
;;; Stolen form Perl |
| 372 |
;;; |
| 373 |
|
| 374 |
(defun mew-join (separator string-list) |
| 375 |
(mapconcat 'identity string-list separator)) |
| 376 |
|
| 377 |
(defun mew-split (str sepchar) |
| 378 |
"Return a list of strings splitting STR with SEPCHAR." |
| 379 |
(let ((len (length str)) (start 0) ret) |
| 380 |
(dotimes (i len) |
| 381 |
(when (char-equal (aref str i) sepchar) |
| 382 |
(setq ret (cons (substring str start i) ret)) |
| 383 |
(setq start (1+ i)))) |
| 384 |
(if (/= start len) |
| 385 |
(setq ret (cons (substring str start) ret))) |
| 386 |
(nreverse ret))) |
| 387 |
|
| 388 |
(defun mew-remove-single-quote (str) |
| 389 |
(let* ((len (length str)) |
| 390 |
(ret (mew-make-string len)) |
| 391 |
(j 0)) |
| 392 |
(dotimes (i len) |
| 393 |
(unless (char-equal (aref str i) ?') |
| 394 |
(aset ret j (aref str i)) |
| 395 |
(setq j (1+ j)))) |
| 396 |
(substring ret 0 j))) |
| 397 |
|
| 398 |
(defun mew-split-quoted (str sepchar &optional qopen qclose no-single) |
| 399 |
"Return a list of strings splitting STR with SEPCHAR. |
| 400 |
SEPCHARs in double-quoted strings are ignored. |
| 401 |
If QUOTEDCHAR is provided, SEPCHARs between QOPEN and QCLOSE are |
| 402 |
also ignored." |
| 403 |
(let ((qlevel 0) (len (length str)) (start 0) dblq sub ret c) |
| 404 |
(if (and qopen (not qclose)) (setq qclose qopen)) |
| 405 |
(dotimes (i len) |
| 406 |
(setq c (aref str i)) |
| 407 |
(cond |
| 408 |
((char-equal ?\\ c) |
| 409 |
(setq i (1+ i))) |
| 410 |
((or (char-equal ?\" c) (and (not no-single) (char-equal ?' c))) |
| 411 |
(setq dblq (not dblq))) |
| 412 |
((and qopen (char-equal c qopen)) |
| 413 |
(setq qlevel (1+ qlevel))) |
| 414 |
((and qclose (char-equal c qclose)) |
| 415 |
(setq qlevel (1- qlevel))) |
| 416 |
((char-equal c sepchar) |
| 417 |
(unless (or dblq (>= qlevel 1)) |
| 418 |
(setq sub (substring str start i)) |
| 419 |
(unless no-single |
| 420 |
(setq sub (mew-remove-single-quote sub))) |
| 421 |
(setq ret (cons sub ret)) |
| 422 |
(setq start (1+ i)))))) |
| 423 |
(when (/= start len) |
| 424 |
(setq sub (substring str start)) |
| 425 |
(unless no-single |
| 426 |
(setq sub (mew-remove-single-quote sub))) |
| 427 |
(setq ret (cons sub ret))) |
| 428 |
(nreverse ret))) |
| 429 |
|
| 430 |
(defun mew-chop (str) |
| 431 |
"Split off preceding and following white spaces." |
| 432 |
(let ((i 0) (j (length str)) c) |
| 433 |
(catch 'loop |
| 434 |
(while (< i j) |
| 435 |
(setq c (aref str i)) |
| 436 |
(if (or (char-equal c mew-sp) (char-equal c ?\t)) |
| 437 |
(setq i (1+ i)) |
| 438 |
(throw 'loop nil)))) |
| 439 |
(setq j (1- j)) |
| 440 |
(catch 'loop |
| 441 |
(while (< i j) |
| 442 |
(setq c (aref str j)) |
| 443 |
(if (or (char-equal c mew-sp) (char-equal c ?\t)) |
| 444 |
(setq j (1- j)) |
| 445 |
(throw 'loop nil)))) |
| 446 |
(substring str i (1+ j)))) |
| 447 |
|
| 448 |
(defun mew-quote-string (str qchar targets) |
| 449 |
"If characters in STR is a member of TARGETS, QCHAR is prepended to them." |
| 450 |
(let* ((len (length str)) |
| 451 |
(ret (mew-make-string (* len 2))) |
| 452 |
(j 0) c) |
| 453 |
(dotimes (i len) |
| 454 |
(setq c (aref str i)) |
| 455 |
(when (member c targets) |
| 456 |
(aset ret j qchar) |
| 457 |
(setq j (1+ j))) |
| 458 |
(aset ret j c) |
| 459 |
(setq j (1+ j))) |
| 460 |
(substring ret 0 j))) |
| 461 |
|
| 462 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 463 |
;;; |
| 464 |
;;; Folder |
| 465 |
;;; |
| 466 |
|
| 467 |
(defun mew-folder-regex (folder) |
| 468 |
(concat "^" (regexp-quote folder))) |
| 469 |
|
| 470 |
;; |
| 471 |
|
| 472 |
(defun mew-case:folder-case (case:folder) |
| 473 |
"Extract case from case:folder. If case does not exist, nil is returned." |
| 474 |
;; "case" must be distinguished from drive |
| 475 |
(if (string-match mew-regex-case1 case:folder) |
| 476 |
(mew-match-string 1 case:folder))) |
| 477 |
|
| 478 |
(defun mew-case:folder-folder (case:folder) |
| 479 |
"Extract folder from case:folder." |
| 480 |
;; "case" must be distinguished from drive |
| 481 |
(if (string-match mew-regex-case1 case:folder) |
| 482 |
(mew-match-string 3 case:folder) |
| 483 |
case:folder)) |
| 484 |
|
| 485 |
(defun mew-case-folder (case folder) |
| 486 |
"Concat case and folder to produce case:folder. |
| 487 |
If case is \"default\", it is not prepended." |
| 488 |
(if (and case |
| 489 |
(not (string= case mew-case-default)) |
| 490 |
(mew-folder-remotep folder)) |
| 491 |
(concat case ":" folder) |
| 492 |
folder)) |
| 493 |
|
| 494 |
;; |
| 495 |
|
| 496 |
(defun mew-folder-absolutep (folder) |
| 497 |
(or (string-match mew-regex-file-absolute folder) |
| 498 |
(string-match mew-regex-drive-letter folder))) |
| 499 |
|
| 500 |
(defun mew-folder-localp (folder) |
| 501 |
(string-match "^\\+" folder)) |
| 502 |
|
| 503 |
(defun mew-folder-remotep (folder) |
| 504 |
(string-match "^[-%$]" folder)) |
| 505 |
|
| 506 |
(defun mew-folder-popp (folder) |
| 507 |
(string-match "^\\$" folder)) |
| 508 |
|
| 509 |
(defun mew-folder-nntpp (folder) |
| 510 |
(string-match "^-" folder)) |
| 511 |
|
| 512 |
(defun mew-folder-imapp (folder) |
| 513 |
(string-match "^%" folder)) |
| 514 |
|
| 515 |
;; (concat "^" (regexp-quote mew-folder-virtual)) |
| 516 |
(defun mew-folder-virtualp (folder) |
| 517 |
(string-match "^\\*" folder)) |
| 518 |
|
| 519 |
(defun mew-virtual-thread-p (vfolder) |
| 520 |
(with-current-buffer vfolder |
| 521 |
(mew-thread-p))) |
| 522 |
|
| 523 |
;; |
| 524 |
|
| 525 |
(defun mew-folder-prefix (folder) |
| 526 |
(substring folder 0 1)) |
| 527 |
|
| 528 |
(defun mew-folder-string (folder) |
| 529 |
(substring folder 1)) |
| 530 |
|
| 531 |
(defun mew-string-to-local (folder) |
| 532 |
(concat mew-folder-local folder)) |
| 533 |
|
| 534 |
(defun mew-folder-path-to-folder (path &optional has-proto) |
| 535 |
(let (case proto) |
| 536 |
;; depends on folder |
| 537 |
(mew-set '(case proto) (mew-summary-case-proto)) |
| 538 |
(if (mew-folder-localp proto) |
| 539 |
(if has-proto |
| 540 |
path |
| 541 |
(mew-string-to-local path)) |
| 542 |
(let* ((base-path (mew-folder-string |
| 543 |
(mew-path-to-folder |
| 544 |
(mew-expand-folder (mew-case-folder case proto))))) |
| 545 |
(path-regex (concat "^" (regexp-quote base-path))) |
| 546 |
(no-proto-path path)) |
| 547 |
(when has-proto |
| 548 |
(setq no-proto-path (mew-case:folder-folder no-proto-path)) |
| 549 |
(setq no-proto-path (mew-folder-string no-proto-path))) |
| 550 |
(if (string-match path-regex no-proto-path) |
| 551 |
(mew-case-folder |
| 552 |
case |
| 553 |
(concat proto (substring no-proto-path (match-end 0)))) |
| 554 |
path))))) |
| 555 |
|
| 556 |
;; |
| 557 |
|
| 558 |
(defun mew-folder-to-selection (folder) |
| 559 |
(concat mew-folder-virtual (mew-folder-basename folder))) |
| 560 |
|
| 561 |
(defun mew-folder-to-thread (folder) |
| 562 |
(concat mew-folder-virtual (mew-folder-basename folder) mew-folder-virtual)) |
| 563 |
|
| 564 |
(defun mew-folder-basename (folder) |
| 565 |
;; Upper functions may use string-match. |
| 566 |
;; So, string-match must not be used here. |
| 567 |
(if (eq (aref folder 0) ?*) |
| 568 |
(if (eq (aref folder (1- (length folder))) ?*) |
| 569 |
(substring folder 1 -1) |
| 570 |
(substring folder 1)) |
| 571 |
folder)) |
| 572 |
|
| 573 |
;; |
| 574 |
|
| 575 |
(defun mew-summary-physical-folder () |
| 576 |
(cond |
| 577 |
((mew-summary-p) |
| 578 |
(mew-summary-folder-name 'ext)) |
| 579 |
((mew-virtual-p) |
| 580 |
(mew-vinfo-get-physical-folder)))) |
| 581 |
|
| 582 |
;; |
| 583 |
|
| 584 |
(defun mew-local-to-dir (folder) |
| 585 |
(if (string-match "^[+]" folder) |
| 586 |
(substring folder 1) |
| 587 |
folder)) |
| 588 |
|
| 589 |
;; |
| 590 |
|
| 591 |
(defun mew-folder-inboxp (folder) |
| 592 |
(member folder mew-inbox-folders)) |
| 593 |
|
| 594 |
(defun mew-folder-queuep (folder) |
| 595 |
(member folder mew-queue-folders)) |
| 596 |
|
| 597 |
(defun mew-folder-postqp (folder) |
| 598 |
(member folder mew-postq-folders)) |
| 599 |
|
| 600 |
(defun mew-folder-draftp (folder) |
| 601 |
(equal folder mew-draft-folder)) |
| 602 |
|
| 603 |
;; |
| 604 |
|
| 605 |
(defun mew-folder-imap-queuep () |
| 606 |
(string= (mew-sinfo-get-folder) |
| 607 |
(mew-imap-queue-folder (mew-sinfo-get-case)))) |
| 608 |
|
| 609 |
;; |
| 610 |
|
| 611 |
(defun mew-canonicalize-folder (folder) |
| 612 |
(cond |
| 613 |
((mew-folder-localp folder) |
| 614 |
folder) |
| 615 |
((mew-folder-imapp folder) |
| 616 |
folder) |
| 617 |
((file-name-absolute-p folder) |
| 618 |
folder) |
| 619 |
(t |
| 620 |
(mew-string-to-local folder)))) |
| 621 |
|
| 622 |
(defun mew-path-to-folder (path) |
| 623 |
(let ((regex (concat "^" (regexp-quote (file-name-as-directory (expand-file-name mew-mail-path)))))) |
| 624 |
(if (string-match regex path) |
| 625 |
(mew-string-to-local (substring path (match-end 0))) |
| 626 |
path))) |
| 627 |
|
| 628 |
(defun mew-canonicalize-case-folder (case:folder) |
| 629 |
(let ((case (mew-case:folder-case case:folder)) |
| 630 |
(folder (mew-case:folder-folder case:folder)) |
| 631 |
len) |
| 632 |
(cond |
| 633 |
((mew-folder-localp folder) (directory-file-name folder)) |
| 634 |
((mew-folder-absolutep folder)(directory-file-name folder)) |
| 635 |
((mew-folder-virtualp folder) folder) |
| 636 |
(t |
| 637 |
(if (null case) |
| 638 |
(setq folder case:folder) |
| 639 |
(if (string= case mew-case-default) |
| 640 |
(progn |
| 641 |
(setq case nil) |
| 642 |
(setq case:folder folder)))) |
| 643 |
(cond |
| 644 |
((mew-folder-popp folder) case:folder) |
| 645 |
((mew-folder-imapp folder) |
| 646 |
(mew-imap-directory-file-name |
| 647 |
case:folder (or case ;; visit |
| 648 |
mew-inherit-case))) ;; refile |
| 649 |
((mew-folder-nntpp folder) |
| 650 |
(setq len (1- (length case:folder))) |
| 651 |
(if (char-equal (aref case:folder len) ?.) |
| 652 |
(substring case:folder 0 len) |
| 653 |
case:folder))))))) |
| 654 |
|
| 655 |
;; |
| 656 |
|
| 657 |
(defun mew-expand-folder2 (case:folder) |
| 658 |
"Expanding FOLDER to a relative path to '+'" |
| 659 |
(let ((case (mew-case:folder-case case:folder)) |
| 660 |
(folder (mew-case:folder-folder case:folder)) |
| 661 |
subdir) |
| 662 |
;; The length of "case" must be longer than or equal to 2. |
| 663 |
(setq subdir (mew-folder-string folder)) |
| 664 |
(cond |
| 665 |
((mew-folder-popp folder) |
| 666 |
(mew-concat-folder (mew-pop-folder case) subdir)) |
| 667 |
((mew-folder-nntpp folder) |
| 668 |
(mew-concat-folder (mew-nntp-folder case) subdir)) |
| 669 |
((mew-folder-imapp folder) |
| 670 |
(mew-concat-folder (mew-imap-folder case) |
| 671 |
(mew-imap-expand-folder |
| 672 |
case |
| 673 |
(mew-imap-utf-7-encode-string subdir)))) |
| 674 |
(t folder)))) |
| 675 |
|
| 676 |
(defun mew-expand-folder (folder) |
| 677 |
"Expanding FOLDER to its absolute path" |
| 678 |
(when (stringp folder) |
| 679 |
(let (dir) |
| 680 |
(setq folder (mew-expand-folder2 folder)) |
| 681 |
(if (mew-folder-localp folder) |
| 682 |
(setq dir (expand-file-name (mew-folder-string folder) mew-mail-path)) |
| 683 |
;; absolute path |
| 684 |
;; "C:/" -> t, "C:" -> nil , "CC:/" -> nil |
| 685 |
(setq dir (expand-file-name folder))) |
| 686 |
dir))) |
| 687 |
|
| 688 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 689 |
;;; |
| 690 |
;;; Suffix support |
| 691 |
;;; |
| 692 |
|
| 693 |
(defvar mew-use-suffix nil) |
| 694 |
|
| 695 |
(defun mew-msg-new-filename (file) |
| 696 |
(if mew-use-suffix |
| 697 |
(format "%s%s" file mew-suffix) |
| 698 |
file)) |
| 699 |
|
| 700 |
(defun mew-msg-get-filename (file) |
| 701 |
(let ((sfile (format "%s%s" file mew-suffix))) |
| 702 |
(if (file-exists-p sfile) |
| 703 |
sfile |
| 704 |
file))) |
| 705 |
|
| 706 |
(defun mew-expand-msg (folder message) |
| 707 |
(let ((file (expand-file-name message (mew-expand-folder folder)))) |
| 708 |
(mew-msg-get-filename file))) |
| 709 |
|
| 710 |
(defun mew-expand-new-msg (folder message) |
| 711 |
(mew-msg-new-filename (expand-file-name message (mew-expand-folder folder)))) |
| 712 |
|
| 713 |
(defun mew-expand-file (folder file) |
| 714 |
(expand-file-name file (mew-expand-folder folder))) |
| 715 |
|
| 716 |
(defun mew-concat-folder (folder subfolder) |
| 717 |
(concat (file-name-as-directory folder) subfolder)) |
| 718 |
|
| 719 |
(defun mew-concat-folder2 (folder subfolder case) |
| 720 |
(if (mew-folder-imapp folder) |
| 721 |
(concat (mew-imap-file-name-as-directory folder case) subfolder) |
| 722 |
(concat (file-name-as-directory folder) subfolder))) |
| 723 |
|
| 724 |
(defun mew-dir-messages (dir &optional regex full) |
| 725 |
;; directory_files uses ENCODE_FILE() for DIR and |
| 726 |
;; DECODE_FILE() for results(FILES). |
| 727 |
;; Both ENCODE_FILE() and DECODE_FILE() are quite slow. |
| 728 |
;; Since DECODE_FILE() is used many times, directory_files is |
| 729 |
;; too slow when {default-,}file-name-coding-system are non-nil. |
| 730 |
;; If {default-,}file-name-coding-system are bound to nil, |
| 731 |
;; ENCODE_FILE() and DECODE_FILE() are skipped, resulting speed up. |
| 732 |
;; So, we need to encode DIR by ourselves. |
| 733 |
(let ((edir (expand-file-name dir)) |
| 734 |
(cs default-file-name-coding-system)) |
| 735 |
(setq edir (mew-cs-encode-string edir cs)) |
| 736 |
(or regex (setq regex mew-regex-message-files)) |
| 737 |
(mew-alet |
| 738 |
(directory-files edir full regex 'no-sort)))) |
| 739 |
|
| 740 |
(defun mew-folder-messages (folder) |
| 741 |
(let* ((dir (mew-expand-folder folder)) |
| 742 |
msgs nums) |
| 743 |
(when (file-directory-p dir) |
| 744 |
(setq msgs (mew-dir-messages dir)) |
| 745 |
(setq nums (sort (mapcar 'string-to-number msgs) '<)) |
| 746 |
(mapcar 'number-to-string nums)))) |
| 747 |
|
| 748 |
(defun mew-folder-new-message (folder &optional num-only cache) |
| 749 |
(let* ((dir (mew-expand-folder folder)) |
| 750 |
(regex (if cache mew-regex-message-files3 mew-regex-message-files)) |
| 751 |
(max 0) |
| 752 |
cur maxfile maxpath) |
| 753 |
;; xxx create if there is no directory? |
| 754 |
(when (file-directory-p dir) |
| 755 |
(dolist (msg (mew-dir-messages dir regex)) |
| 756 |
(setq cur (string-to-number (file-name-sans-extension msg))) |
| 757 |
(if (> cur max) (setq max cur))) |
| 758 |
(setq max (1+ max)) |
| 759 |
(setq maxfile (number-to-string max)) |
| 760 |
(setq maxpath (mew-expand-new-msg folder maxfile)) |
| 761 |
(while (get-file-buffer maxpath) ;; xxx |
| 762 |
;; file not exist but there is a buffer. |
| 763 |
(setq max (1+ max)) |
| 764 |
(setq maxfile (number-to-string max)) |
| 765 |
(setq maxpath (mew-expand-new-msg folder maxfile))) |
| 766 |
(while (file-exists-p maxpath) |
| 767 |
(setq maxfile (read-string (format "%s/%s exists. Input a message number: " folder max))) |
| 768 |
(while (not (string-match mew-regex-message-files maxfile)) |
| 769 |
(setq maxfile (read-string "Input NUMBER: "))) |
| 770 |
(setq maxpath (mew-expand-new-msg folder maxfile))) |
| 771 |
(if num-only |
| 772 |
(mew-msg-new-filename maxfile) |
| 773 |
maxpath)))) |
| 774 |
|
| 775 |
(defun mew-touch-folder (fld) |
| 776 |
(when (stringp mew-summary-touch-file) |
| 777 |
(let ((file (mew-expand-file fld mew-summary-touch-file))) |
| 778 |
(when (file-writable-p file) |
| 779 |
(write-region "touched by Mew." nil file nil 'no-msg) |
| 780 |
(mew-set-file-modes file))))) |
| 781 |
|
| 782 |
;; this kind of defmacro can't recurse |
| 783 |
;; if +foo exists and we insert +foo/bar, |
| 784 |
;; +foo/ should be inserted, too. But +foo/ does not match to +foo... |
| 785 |
(defmacro mew-folder-insert (folder lst subdir) |
| 786 |
`(unless (mew-assoc-equal ,folder ,lst 0) |
| 787 |
(let ((case-fold-search nil) |
| 788 |
(max (1- (length ,lst))) |
| 789 |
(pair (mew-folder-func ,folder ,subdir)) |
| 790 |
(min 0) |
| 791 |
mid crr prv) |
| 792 |
(while (> (- max min) 20) ;; 20 is enough? |
| 793 |
(setq mid (/ (+ min max) 2)) |
| 794 |
(if (string< (car (nth mid ,lst)) ,folder) |
| 795 |
(setq min mid) |
| 796 |
(setq max mid))) |
| 797 |
(setq crr (nthcdr min ,lst)) |
| 798 |
(while (and crr (string< (car (car crr)) ,folder)) |
| 799 |
(setq prv crr) |
| 800 |
(setq crr (cdr crr))) |
| 801 |
(if prv |
| 802 |
(setcdr prv (cons pair crr)) |
| 803 |
(setq ,lst (cons pair crr)))))) |
| 804 |
|
| 805 |
(defmacro mew-folder-delete (folder lst) |
| 806 |
`(setq ,lst (delete (assoc ,folder ,lst) ,lst))) |
| 807 |
|
| 808 |
(defun mew-folder-node-p (folder &optional case) |
| 809 |
(cond |
| 810 |
((mew-folder-localp folder) |
| 811 |
(let* ((dir (file-name-as-directory folder)) |
| 812 |
(regex (concat "^" (regexp-quote dir)))) |
| 813 |
(mew-assoc-match regex (mew-local-folder-alist) 0))) |
| 814 |
((mew-folder-imapp folder) |
| 815 |
(let* ((dir (mew-imap-file-name-as-directory folder case)) |
| 816 |
(regex (concat "^" (regexp-quote dir)))) |
| 817 |
(mew-assoc-match regex (mew-imap-folder-alist case) 0))))) |
| 818 |
|
| 819 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 820 |
;;; |
| 821 |
;;; Folder spec |
| 822 |
;;; |
| 823 |
|
| 824 |
(defconst mew-folder-spec-type |
| 825 |
'((regex . string-match) |
| 826 |
(recursive . mew-folder-recursive-match) |
| 827 |
(string . string=))) |
| 828 |
|
| 829 |
(defun mew-folder-spec-func (type) |
| 830 |
(or (cdr (assq type mew-folder-spec-type)) 'string=)) |
| 831 |
|
| 832 |
(defun mew-folder-spec (folder lst str-type lst-type) |
| 833 |
(let ((str-func (mew-folder-spec-func str-type)) |
| 834 |
(lst-func (mew-folder-spec-func lst-type)) |
| 835 |
keys values ret) |
| 836 |
(setq folder (mew-case:folder-folder folder)) |
| 837 |
(catch 'loop |
| 838 |
(dolist (ent lst) |
| 839 |
(setq keys (car ent)) |
| 840 |
(setq values (cdr ent)) |
| 841 |
(cond |
| 842 |
((eq keys t) |
| 843 |
(throw 'loop (setq ret values))) |
| 844 |
((stringp keys) |
| 845 |
(if (funcall str-func keys folder) |
| 846 |
(throw 'loop (setq ret values)))) |
| 847 |
((listp keys) |
| 848 |
(dolist (key keys) |
| 849 |
(if (funcall lst-func key folder) |
| 850 |
(throw 'loop (setq ret values)))))))) |
| 851 |
ret)) |
| 852 |
|
| 853 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 854 |
;;; |
| 855 |
;;; Directory |
| 856 |
;;; |
| 857 |
|
| 858 |
(defun mew-parent-directory (path) |
| 859 |
(directory-file-name (file-name-directory path))) |
| 860 |
|
| 861 |
(defun mew-rename-directory (src dst) |
| 862 |
(let ((parent (mew-parent-directory (directory-file-name dst)))) |
| 863 |
(mew-make-directory parent) |
| 864 |
(rename-file src dst))) |
| 865 |
|
| 866 |
(defun mew-make-directory (path) |
| 867 |
(let ((parent (mew-parent-directory path))) |
| 868 |
(unless (file-directory-p parent) |
| 869 |
(mew-make-directory parent)) |
| 870 |
(cond |
| 871 |
((file-directory-p path) |
| 872 |
()) ;; do nothing |
| 873 |
((file-exists-p path) |
| 874 |
(delete-file path) |
| 875 |
(make-directory path)) |
| 876 |
(t |
| 877 |
(make-directory path))) |
| 878 |
(if (/= mew-folder-mode (file-modes path)) |
| 879 |
(set-file-modes path mew-folder-mode)))) |
| 880 |
|
| 881 |
(defun mew-delete-directory-recursively (dir) |
| 882 |
(when (file-directory-p dir) |
| 883 |
(dolist (file (directory-files dir 'full mew-regex-files 'no-sort)) |
| 884 |
(cond |
| 885 |
((file-symlink-p file) |
| 886 |
;; never chase symlink which points a directory |
| 887 |
(delete-file file)) |
| 888 |
((file-directory-p file) |
| 889 |
(mew-delete-directory-recursively file)) |
| 890 |
(t |
| 891 |
(delete-file file)))) |
| 892 |
(unless (directory-files dir 'full mew-regex-files 'no-sort) |
| 893 |
(delete-directory dir)))) |
| 894 |
|
| 895 |
(defun mew-check-directory (dir) |
| 896 |
(unless (file-directory-p dir) |
| 897 |
(unless (file-exists-p dir) |
| 898 |
(mew-make-directory dir)))) |
| 899 |
|
| 900 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 901 |
;;; |
| 902 |
;;; File existence |
| 903 |
;;; |
| 904 |
|
| 905 |
(defun mew-which (file path) |
| 906 |
(catch 'loop |
| 907 |
(dolist (dir path) |
| 908 |
(if (file-exists-p (expand-file-name file dir)) |
| 909 |
(throw 'loop (expand-file-name file dir)))))) |
| 910 |
|
| 911 |
(defun mew-which-el (elfile) |
| 912 |
(or (mew-which (concat elfile ".el") load-path) |
| 913 |
(mew-which (concat elfile ".elc") load-path))) |
| 914 |
|
| 915 |
(defvar mew-which-exec-suffixes '("")) |
| 916 |
|
| 917 |
(defun mew-which-exec (execfile) |
| 918 |
(let (file) |
| 919 |
(catch 'detect |
| 920 |
(dolist (suffix mew-which-exec-suffixes) |
| 921 |
(when (setq file (mew-which (concat execfile suffix) exec-path)) |
| 922 |
(throw 'detect file)))))) |
| 923 |
|
| 924 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 925 |
;;; |
| 926 |
;;; File attribute |
| 927 |
;;; |
| 928 |
|
| 929 |
;; Functions to get other attributes are implemented in C level. |
| 930 |
|
| 931 |
(defun mew-file-get-links (file) |
| 932 |
(let ((w32-get-true-file-link-count t)) ;; for Meadow |
| 933 |
(nth 1 (file-attributes file)))) |
| 934 |
|
| 935 |
(defun mew-file-get-time (file) |
| 936 |
(nth 5 (file-attributes file))) |
| 937 |
|
| 938 |
(defun mew-file-get-size (file) |
| 939 |
(nth 7 (file-attributes file))) |
| 940 |
|
| 941 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 942 |
;;; |
| 943 |
;;; File |
| 944 |
;;; |
| 945 |
|
| 946 |
(defun mew-file-chase-links (file) |
| 947 |
"Chase links in FILENAME until a name that is not a link. |
| 948 |
Does not examine containing directories for links." |
| 949 |
(let ((ret file) exp) |
| 950 |
(while (setq exp (file-symlink-p ret)) |
| 951 |
(setq ret (expand-file-name exp (file-name-directory ret)))) |
| 952 |
ret)) |
| 953 |
|
| 954 |
(defun mew-file-from-home (str) |
| 955 |
(if (string-match (expand-file-name mew-home) str) |
| 956 |
(concat mew-home (substring str (match-end 0))) |
| 957 |
str)) |
| 958 |
|
| 959 |
(defun mew-prepend-prefix (file prefix) |
| 960 |
(if (file-name-absolute-p file) |
| 961 |
(concat (file-name-directory file) prefix (file-name-nondirectory file)) |
| 962 |
(concat prefix file))) |
| 963 |
|
| 964 |
(defun mew-rotate-log-files (file-name) |
| 965 |
(let ((i 8) (file (expand-file-name file-name mew-conf-path))) |
| 966 |
(when (and (file-exists-p file) |
| 967 |
(>= (mew-file-get-size file) mew-log-max-size)) |
| 968 |
(while (>= i 0) |
| 969 |
(if (file-exists-p (format "%s.%d" file i)) |
| 970 |
(rename-file (format "%s.%d" file i) |
| 971 |
(format "%s.%d" file (1+ i)) t)) |
| 972 |
(setq i (1- i))) |
| 973 |
(rename-file file (format "%s.0" file))))) |
| 974 |
|
| 975 |
(defun mew-remove-drive-letter (file) |
| 976 |
(if (string-match mew-regex-drive-letter file) |
| 977 |
(substring file 2) |
| 978 |
file)) |
| 979 |
|
| 980 |
(defun mew-get-file-modes (path) |
| 981 |
(let* ((dir (file-name-directory path)) |
| 982 |
(dirmode (file-modes dir))) |
| 983 |
(logand dirmode mew-file-mode-mask))) |
| 984 |
|
| 985 |
(defun mew-set-file-modes (path) |
| 986 |
(set-file-modes path (mew-get-file-modes path))) |
| 987 |
|
| 988 |
(defun mew-delete-file (file) |
| 989 |
(if (and (stringp file) (file-exists-p file)) (delete-file file))) |
| 990 |
|
| 991 |
(defun mew-insert-file-contents (&rest args) |
| 992 |
"A safe version of insert-file-contents. |
| 993 |
This checks -*-coding:ctext;-*- internally when including a file." |
| 994 |
(let ((after-insert-file-functions nil)) |
| 995 |
;; preventing after-insert-file-set-buffer-file-coding-system |
| 996 |
(apply 'mew-insert-file-contents2 args))) |
| 997 |
|
| 998 |
(defun mew-insert-file-contents2 (&rest args) |
| 999 |
"Mew version of insert-file-contents. |
| 1000 |
This sets buffer-file-coding-system." |
| 1001 |
(let ((auto-image-file-mode nil)) |
| 1002 |
(apply 'insert-file-contents args))) |
| 1003 |
|
| 1004 |
(defun mew-find-file-noselect (&rest args) |
| 1005 |
"A safe version of find-file-noselect. |
| 1006 |
This checks -*-coding:ctext;-*- internally when including a file. |
| 1007 |
But this does not set buffer-file-coding-system." |
| 1008 |
(let ((after-insert-file-functions nil)) |
| 1009 |
;; preventing after-insert-file-set-buffer-file-coding-system |
| 1010 |
(apply 'mew-find-file-noselect2 args))) |
| 1011 |
|
| 1012 |
(defun mew-find-file-noselect2 (&rest args) |
| 1013 |
"A safe version of find-file-noselect. |
| 1014 |
This checks -*-coding:ctext;-*- internally when including a file |
| 1015 |
and sets buffer-file-coding-system." |
| 1016 |
(let ((auto-image-file-mode nil) |
| 1017 |
(format-alist nil) |
| 1018 |
(auto-mode-alist nil) |
| 1019 |
(enable-local-variables nil) |
| 1020 |
(find-file-hook nil) |
| 1021 |
(large-file-warning-threshold nil)) |
| 1022 |
(apply 'find-file-noselect args))) |
| 1023 |
|
| 1024 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1025 |
;;; |
| 1026 |
;;; temp name |
| 1027 |
;;; |
| 1028 |
|
| 1029 |
(defun mew-make-temp-name (&optional fname) |
| 1030 |
(unless (file-exists-p mew-temp-dir) |
| 1031 |
(mew-make-directory mew-temp-dir)) ;; just in case |
| 1032 |
(if fname |
| 1033 |
(let ((exist (file-exists-p (expand-file-name fname mew-temp-dir)))) |
| 1034 |
(if (and (not exist) |
| 1035 |
(or (not (eq system-type 'windows-nt)) |
| 1036 |
(string-match "^[ -~]+$" fname))) |
| 1037 |
(expand-file-name fname mew-temp-dir) |
| 1038 |
(if (string-match "\\.[ -~]+$" fname) |
| 1039 |
(concat (make-temp-name mew-temp-file) (mew-match-string 0 fname)) |
| 1040 |
(make-temp-name mew-temp-file)))) |
| 1041 |
(make-temp-name mew-temp-file))) |
| 1042 |
|
| 1043 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1044 |
;;; |
| 1045 |
;;; Random |
| 1046 |
;;; |
| 1047 |
|
| 1048 |
(defvar mew-random-base-09 "0123456789") |
| 1049 |
(defvar mew-random-base-az "abcdefghijklmnopqrstuvwxwz") |
| 1050 |
|
| 1051 |
(defun mew-random () |
| 1052 |
(let* ((vec (recent-keys)) |
| 1053 |
(ran (+ (random) (emacs-pid))) |
| 1054 |
c) |
| 1055 |
(dotimes (i (length vec)) |
| 1056 |
(setq c (aref vec i)) |
| 1057 |
(if (integerp c) (setq ran (+ ran c)))) |
| 1058 |
(abs ran))) |
| 1059 |
|
| 1060 |
(defun mew-random-string (len nump) |
| 1061 |
(let* ((base (if nump mew-random-base-09 mew-random-base-az)) |
| 1062 |
(baselen (length base)) |
| 1063 |
(ret (mew-make-string len))) |
| 1064 |
(dotimes (i len) |
| 1065 |
(aset ret i (aref base (% (mew-random) baselen)))) |
| 1066 |
ret)) |
| 1067 |
|
| 1068 |
(defun mew-random-filename (dir len nump &optional suffix) |
| 1069 |
(let ((cnt 0) (max 20) ;; ad hoc |
| 1070 |
file filepath) |
| 1071 |
(setq file (concat (mew-random-string len nump) suffix)) |
| 1072 |
(setq filepath (expand-file-name file dir)) |
| 1073 |
(while (and (file-exists-p filepath) (< cnt max)) |
| 1074 |
(setq file (concat (mew-random-string len nump) suffix)) |
| 1075 |
(setq filepath (expand-file-name file dir)) |
| 1076 |
(setq cnt (1+ cnt))) |
| 1077 |
(if (file-exists-p filepath) |
| 1078 |
nil |
| 1079 |
filepath))) |
| 1080 |
|
| 1081 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1082 |
;;; |
| 1083 |
;;; Buffer |
| 1084 |
;;; |
| 1085 |
|
| 1086 |
(defun mew-erase-buffer () |
| 1087 |
(mew-elet |
| 1088 |
(widen) |
| 1089 |
(erase-buffer) |
| 1090 |
(buffer-disable-undo))) |
| 1091 |
|
| 1092 |
(defun mew-remove-buffer (buf) |
| 1093 |
(if (and buf (get-buffer buf)) (kill-buffer buf))) |
| 1094 |
|
| 1095 |
(defmacro mew-elet (&rest body) |
| 1096 |
(declare (debug (&rest form))) |
| 1097 |
`(let ((buffer-read-only nil) |
| 1098 |
(inhibit-read-only t) |
| 1099 |
(after-change-functions nil) |
| 1100 |
(mark-active nil)) |
| 1101 |
,@body)) |
| 1102 |
|
| 1103 |
(defun mew-push-mark () |
| 1104 |
"Set the mark for \\[exchange-point-and-mark]" |
| 1105 |
(let ((mark-active nil)) |
| 1106 |
(push-mark (point) t t))) |
| 1107 |
|
| 1108 |
(defun mew-region-bytes (beg end buf) |
| 1109 |
;; string-bytes() acts differently on each Emacs. |
| 1110 |
;; set-buffer-multibyte is also buggy. |
| 1111 |
;; So, use this way. |
| 1112 |
(with-current-buffer buf |
| 1113 |
(if (fboundp 'string-as-unibyte) |
| 1114 |
(length (string-as-unibyte (mew-buffer-substring beg end))) |
| 1115 |
(- end beg)))) |
| 1116 |
|
| 1117 |
(defun mew-count-lines (beg end) |
| 1118 |
"Return number of lines between BEG and END." |
| 1119 |
(save-excursion |
| 1120 |
(goto-char beg) |
| 1121 |
(beginning-of-line) |
| 1122 |
(setq beg (point)) |
| 1123 |
(goto-char end) |
| 1124 |
(beginning-of-line) |
| 1125 |
(setq end (point)) |
| 1126 |
(if (or (not (mew-decode-syntax-p)) |
| 1127 |
(not (equal (mew-decode-syntax-buffer) (current-buffer)))) |
| 1128 |
(mew-count-lines1 beg end) |
| 1129 |
(let ((mbeg (mew-decode-syntax-begin)) |
| 1130 |
(mend (mew-decode-syntax-end))) |
| 1131 |
(if (or (<= end mbeg) (>= beg mend)) |
| 1132 |
(mew-count-lines1 beg end) |
| 1133 |
(+ (mew-count-lines1 beg mbeg) (mew-count-lines1 mend end))))))) |
| 1134 |
|
| 1135 |
(defun mew-count-lines1 (beg end) |
| 1136 |
(if (>= beg end) |
| 1137 |
0 |
| 1138 |
(save-restriction |
| 1139 |
(narrow-to-region beg end) |
| 1140 |
(goto-char (point-min)) |
| 1141 |
(if (not (and (mew-thread-p) mew-use-thread-separator)) |
| 1142 |
(- (buffer-size) (forward-line (buffer-size))) |
| 1143 |
(let ((regex mew-regex-thread-separator) |
| 1144 |
(lines 0)) |
| 1145 |
(while (not (eobp)) |
| 1146 |
(unless (looking-at regex) |
| 1147 |
(setq lines (1+ lines))) |
| 1148 |
(forward-line)) |
| 1149 |
lines))))) |
| 1150 |
|
| 1151 |
(defun mew-buffer-list (regex &optional listp mode) |
| 1152 |
(let (ret) |
| 1153 |
(dolist (buf (mapcar 'buffer-name (buffer-list))) |
| 1154 |
(when (and (string-match regex buf) |
| 1155 |
(or (not mode) |
| 1156 |
(and mode (get-buffer buf) |
| 1157 |
(with-current-buffer buf |
| 1158 |
(eq major-mode mode))))) |
| 1159 |
(if listp |
| 1160 |
(setq ret (cons (list buf) ret)) |
| 1161 |
(setq ret (cons buf ret))))) |
| 1162 |
(nreverse ret))) |
| 1163 |
|
| 1164 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1165 |
;;; |
| 1166 |
;;; process |
| 1167 |
;;; |
| 1168 |
|
| 1169 |
(defmacro mew-filter (&rest body) |
| 1170 |
(declare (debug (&rest form))) |
| 1171 |
`(let ((pbuf (process-buffer process)) ;; MUST use 'process' |
| 1172 |
(obuf (buffer-name)) |
| 1173 |
(inhibit-eol-conversion nil)) ;; \r\n as is |
| 1174 |
(if (and (bufferp pbuf) |
| 1175 |
(buffer-name pbuf)) ;; check a killed buffer |
| 1176 |
;; must use buffer-name instead of current-buffer |
| 1177 |
;; so that get-buffer can detect killed buffer. |
| 1178 |
(unwind-protect |
| 1179 |
(progn |
| 1180 |
;; buffer surely exists. |
| 1181 |
(set-buffer (process-buffer process)) ;; necessary |
| 1182 |
,@body) |
| 1183 |
(if (get-buffer obuf) |
| 1184 |
;; the body sometimes kills obuf. |
| 1185 |
(set-buffer obuf)))))) |
| 1186 |
|
| 1187 |
(defun mew-start-process-disp (name buffer program &rest program-args) |
| 1188 |
(let ((disp (cdr (assq 'display (frame-parameters)))) |
| 1189 |
(process-environment (copy-sequence process-environment))) |
| 1190 |
(if disp (setenv "DISPLAY" disp)) |
| 1191 |
(apply 'start-process name buffer program program-args))) |
| 1192 |
|
| 1193 |
(defun mew-start-process-lang (name buffer program &rest program-args) |
| 1194 |
(let ((process-environment (copy-sequence process-environment))) |
| 1195 |
(setenv "LANGUAGE" "C") |
| •1196 |
(setenv "LC_ALL" "C.UTF-8") |
| 1197 |
(setenv "LANG" "C.UTF-8") |
| 1198 |
(apply 'start-process name buffer program program-args))) |
| 1199 |
|
| 1200 |
(defun mew-call-process-lang (prog &optional infile buffer display &rest args) |
| 1201 |
(let ((process-environment (copy-sequence process-environment))) |
| 1202 |
(setenv "LANGUAGE" "C") |
| •1203 |
(setenv "LC_ALL" "C.UTF-8") |
| 1204 |
(setenv "LANG" "C.UTF-8") |
| 1205 |
(apply 'call-process prog infile buffer display args))) |
| 1206 |
|
| 1207 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1208 |
;;; |
| 1209 |
;;; Global info |
| 1210 |
;;; |
| 1211 |
|
| 1212 |
(defun mew-info (name) |
| 1213 |
(cond |
| 1214 |
((vectorp name) name) ;; just for .mqi |
| 1215 |
((stringp name) |
| 1216 |
(if (or (not (intern-soft name)) |
| 1217 |
(not (boundp (intern name)))) |
| 1218 |
(if (string-match "^mew-[^-]+-info-" name) |
| 1219 |
(let* ((sym (intern (concat (mew-match-string 0 name) "list"))) |
| 1220 |
(lst (symbol-value sym)) |
| 1221 |
(len (length lst))) |
| 1222 |
(set (intern name) (make-vector len nil))))) |
| 1223 |
(symbol-value (intern-soft name))))) |
| 1224 |
|
| 1225 |
(defun mew-info-defun (prefix lst) |
| 1226 |
(let ((i 0)) |
| 1227 |
(dolist (ent lst) |
| 1228 |
(fset (intern (concat prefix "get-" ent)) |
| 1229 |
`(lambda (arg) |
| 1230 |
(cond |
| 1231 |
((stringp arg) (aref (mew-info arg) ,i)) |
| 1232 |
((vectorp arg) (aref arg ,i))))) |
| 1233 |
(fset (intern (concat prefix "set-" ent)) |
| 1234 |
`(lambda (arg value) |
| 1235 |
(cond |
| 1236 |
((stringp arg) (aset (mew-info arg) ,i value)) |
| 1237 |
((vectorp arg) (aset arg ,i value))))) |
| 1238 |
(setq i (1+ i))))) |
| 1239 |
|
| 1240 |
(defun mew-info-clean-up (arg &optional start) |
| 1241 |
(let ((i (or start 0)) vec len) |
| 1242 |
(cond |
| 1243 |
((stringp arg) (setq vec (mew-info arg))) |
| 1244 |
((vectorp arg) (setq vec arg))) |
| 1245 |
(setq len (length vec)) |
| 1246 |
(while (< i len) |
| 1247 |
(aset vec i nil) |
| 1248 |
(setq i (1+ i))))) |
| 1249 |
|
| 1250 |
(defun mew-net-get-ssh-process (pnm) |
| 1251 |
(aref (mew-info pnm) 3)) |
| 1252 |
|
| 1253 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1254 |
;;; |
| 1255 |
;;; Buffer local info |
| 1256 |
;;; |
| 1257 |
|
| 1258 |
(defun mew-blinfo (sym) |
| 1259 |
(let* ((name (symbol-name sym)) |
| 1260 |
(lname (concat name "-list")) |
| 1261 |
(lsym (intern lname)) |
| 1262 |
(lst (symbol-value lsym)) |
| 1263 |
(len (length lst))) |
| 1264 |
(set sym (make-vector len nil)) |
| 1265 |
(symbol-value sym))) |
| 1266 |
|
| 1267 |
(defun mew-blinfo-defun (blv-sym lst) |
| 1268 |
(let ((i 0)) |
| 1269 |
(dolist (ent lst) |
| 1270 |
(fset (intern (concat (symbol-name blv-sym) "-get-" ent)) |
| 1271 |
`(lambda () |
| 1272 |
(cond |
| 1273 |
((null ,blv-sym) (aref (mew-blinfo (quote ,blv-sym)) ,i)) |
| 1274 |
((vectorp ,blv-sym) (aref ,blv-sym ,i))))) |
| 1275 |
(fset (intern (concat (symbol-name blv-sym) "-set-" ent)) |
| 1276 |
`(lambda (value) |
| 1277 |
(cond |
| 1278 |
((null ,blv-sym) (aset (mew-blinfo (quote ,blv-sym)) ,i value)) |
| 1279 |
((vectorp ,blv-sym) (aset ,blv-sym ,i value))))) |
| 1280 |
(setq i (1+ i))))) |
| 1281 |
|
| 1282 |
(defvar mew-ainfo-list '("icon" "win-cfg")) |
| 1283 |
(mew-blinfo-defun 'mew-ainfo mew-ainfo-list) |
| 1284 |
|
| 1285 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1286 |
;;; |
| 1287 |
;;; Address |
| 1288 |
;;; |
| 1289 |
|
| 1290 |
(defun mew-get-my-address () |
| 1291 |
(or (mew-header-parse-address mew-from:) (mew-mail-address))) |
| 1292 |
|
| 1293 |
;; |
| 1294 |
|
| 1295 |
(defun mew-get-my-address-regex-list () |
| 1296 |
"This creates a list of regular expression used to tell |
| 1297 |
whether or not a given address is mine. The list is created |
| 1298 |
from (mew-user), (mew-mail-address), and 'mew-mail-address-list'." |
| 1299 |
(cons (concat "^" (regexp-quote (mew-user)) "$") |
| 1300 |
(cons (concat "^" (regexp-quote (mew-mail-address)) "$") |
| 1301 |
mew-mail-address-list))) |
| 1302 |
|
| 1303 |
(defun mew-is-my-address (addrs from) |
| 1304 |
(and from |
| 1305 |
(let ((case-fold-search t)) |
| 1306 |
(catch (quote match) |
| 1307 |
(car (mapcar (lambda (arg) (and (string-match arg from) |
| 1308 |
(throw (quote match) t))) |
| 1309 |
addrs)))))) |
| 1310 |
|
| 1311 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1312 |
;;; |
| 1313 |
;;; Lisp load/save |
| 1314 |
;;; |
| 1315 |
|
| 1316 |
(defun mew-lisp-load (filename) |
| 1317 |
"Load lisp from FILENAME" |
| 1318 |
(let ((fullname (if (file-name-absolute-p filename) |
| 1319 |
filename |
| 1320 |
(expand-file-name filename mew-conf-path)))) |
| 1321 |
(if (file-readable-p fullname) |
| 1322 |
(with-temp-buffer |
| 1323 |
(mew-frwlet mew-cs-m17n mew-cs-dummy |
| 1324 |
(mew-insert-file-contents fullname)) |
| 1325 |
(goto-char (point-min)) |
| 1326 |
(condition-case nil |
| 1327 |
(read (current-buffer)) |
| 1328 |
(error ())))))) |
| 1329 |
|
| 1330 |
(defun mew-lisp-save (filename lisp &optional nobackup unlimit) |
| 1331 |
"Save LISP to FILENAME. LISP is truncated to mew-lisp-max-length |
| 1332 |
by side-effect." |
| 1333 |
(let* ((fullname (if (file-name-absolute-p filename) |
| 1334 |
filename |
| 1335 |
(expand-file-name filename mew-conf-path))) |
| 1336 |
(backname (concat fullname mew-backup-suffix)) |
| 1337 |
print-length print-level) ;; for Emacs 21 |
| 1338 |
(when (file-writable-p fullname) |
| 1339 |
(if nobackup |
| 1340 |
(mew-delete-file fullname) |
| 1341 |
(if (file-exists-p fullname) |
| 1342 |
(rename-file fullname backname 'override))) |
| 1343 |
(when (and (not unlimit) (> (length lisp) mew-lisp-max-length)) |
| 1344 |
(setq lisp (copy-sequence lisp)) ;; no side effect |
| 1345 |
(mew-ntake mew-lisp-max-length lisp)) |
| 1346 |
(with-temp-buffer |
| 1347 |
(if (> (length lisp) mew-lisp-max-length) |
| 1348 |
(print lisp (current-buffer)) |
| 1349 |
(pp lisp (current-buffer))) |
| 1350 |
(mew-frwlet mew-cs-dummy mew-cs-m17n |
| 1351 |
(write-region (point-min) (point-max) fullname nil 'no-msg))) |
| 1352 |
(mew-set-file-modes fullname)))) |
| 1353 |
|
| 1354 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1355 |
;;; |
| 1356 |
;;; Month |
| 1357 |
;;; |
| 1358 |
|
| 1359 |
(defvar mew-time-mon-alist |
| 1360 |
'(("Jan" . 1) ("Feb" . 2) ("Mar" . 3) ("Apr" . 4) |
| 1361 |
("May" . 5) ("Jun" . 6) ("Jul" . 7) ("Aug" . 8) |
| 1362 |
("Sep" . 9) ("Oct" . 10) ("Nov" . 11) ("Dec" . 12))) |
| 1363 |
|
| 1364 |
(defun mew-time-mon-str-to-int (str) |
| 1365 |
(or (cdr (assoc (capitalize str) mew-time-mon-alist)) 0)) |
| 1366 |
|
| 1367 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1368 |
;;; |
| 1369 |
;;; Time Zone |
| 1370 |
;;; |
| 1371 |
|
| 1372 |
;; Define symbol time zone defined in RFC822 only. |
| 1373 |
(defvar mew-time-tmzn-alist |
| 1374 |
'(("UT" . 0) ("GMT" . 0) |
| 1375 |
("EST" . -5) ("EDT" . -4) |
| 1376 |
("CST" . -6) ("CDT" . -5) |
| 1377 |
("MST" . -7) ("MDT" . -6) |
| 1378 |
("PST" . -8) ("PDT" . -7))) |
| 1379 |
|
| 1380 |
(defun mew-time-tmzn-str-to-int (str) |
| 1381 |
(cdr (assoc (upcase str) mew-time-tmzn-alist))) |
| 1382 |
|
| 1383 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1384 |
;;; |
| 1385 |
;;; RFC 822 |
| 1386 |
;;; Date: Wed, 26 Jul 2000 21:18:35 +0900 (JST) |
| 1387 |
;;; |
| 1388 |
|
| 1389 |
(defvar mew-time-rfc-regex |
| 1390 |
"\\([0-9]+\\)[ \t\n\r]+\\([a-zA-Z]+\\)[ \t\n\r]+\\([0-9]+\\)[ \t\n\r]+\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\([ \t\n\r]+\\([-+0-9a-zA-Z]+\\)\\)?") |
| 1391 |
|
| 1392 |
(defmacro mew-time-rfc-day () |
| 1393 |
'(string-to-number (substring s (match-beginning 1) (match-end 1)))) |
| 1394 |
|
| 1395 |
(defmacro mew-time-rfc-mon () |
| 1396 |
'(substring s (match-beginning 2) (match-end 2))) |
| 1397 |
|
| 1398 |
(defmacro mew-time-rfc-year () |
| 1399 |
'(string-to-number (substring s (match-beginning 3) (match-end 3)))) |
| 1400 |
|
| 1401 |
(defmacro mew-time-rfc-hour () |
| 1402 |
'(string-to-number (substring s (match-beginning 4) (match-end 4)))) |
| 1403 |
|
| 1404 |
(defmacro mew-time-rfc-min () |
| 1405 |
'(string-to-number (substring s (match-beginning 5) (match-end 5)))) |
| 1406 |
|
| 1407 |
(defmacro mew-time-rfc-sec () |
| 1408 |
'(if (match-beginning 7) |
| 1409 |
(string-to-number (substring s (match-beginning 7) (match-end 7))) |
| 1410 |
0)) |
| 1411 |
|
| 1412 |
;; returns seconds |
| 1413 |
(defmacro mew-time-rfc-tmzn () |
| 1414 |
'(if (match-beginning 9) |
| 1415 |
(let ((tmzn (substring s (match-beginning 9) (match-end 9))) |
| 1416 |
int) |
| 1417 |
(cond |
| 1418 |
((string-match "^[-+][0-9]+$" tmzn) |
| 1419 |
(setq int (string-to-number tmzn)) |
| 1420 |
(+ (* (/ int 100) 3600) (* (% int 100) 60))) |
| 1421 |
((setq int (mew-time-tmzn-str-to-int tmzn)) |
| 1422 |
(* int 3600)) |
| 1423 |
(t 0))) |
| 1424 |
0)) |
| 1425 |
|
| 1426 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1427 |
;;; |
| 1428 |
;;; Sort key for Date: |
| 1429 |
;;; |
| 1430 |
|
| 1431 |
;; "20000726121835" |
| 1432 |
(defun mew-time-ctz-to-sortkey (time) |
| 1433 |
(let ((system-time-locale "C")) |
| 1434 |
(format-time-string "%Y%m%d%H%M%S" time))) |
| 1435 |
|
| 1436 |
(defun mew-time-ctz-to-sortkey-invalid (sec min hour day mon year) |
| 1437 |
(format "%04d%02d%02d%02d%02d%02d" year mon day hour min sec)) |
| 1438 |
|
| 1439 |
;; "20000726121835" |
| 1440 |
(defun mew-time-rfc-to-sortkey (s &optional tzadj) |
| 1441 |
(if (string-match mew-time-rfc-regex s) |
| 1442 |
(let ((year (mew-time-rfc-year)) |
| 1443 |
(mon (mew-time-mon-str-to-int (mew-time-rfc-mon))) |
| 1444 |
(day (mew-time-rfc-day)) |
| 1445 |
(hour (mew-time-rfc-hour)) |
| 1446 |
(min (mew-time-rfc-min)) |
| 1447 |
(sec (mew-time-rfc-sec)) |
| 1448 |
(tmzn (mew-time-rfc-tmzn))) |
| 1449 |
(cond |
| 1450 |
((< year 50) |
| 1451 |
(setq year (+ year 2000))) |
| 1452 |
((< year 150) |
| 1453 |
(setq year (+ year 1900)))) |
| 1454 |
(if (or (< year 1970) (>= year 2038)) |
| 1455 |
;; invalid data |
| 1456 |
(mew-time-ctz-to-sortkey-invalid sec min hour day mon year) |
| 1457 |
(setq sec (- sec tmzn)) |
| 1458 |
(if tzadj (setq sec (+ sec (car (current-time-zone))))) |
| 1459 |
(mew-time-ctz-to-sortkey (encode-time sec min hour day mon year)))))) |
| 1460 |
|
| 1461 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1462 |
;;; |
| 1463 |
;;; User friendly time |
| 1464 |
;;; |
| 1465 |
|
| 1466 |
(defun mew-time-tmzn-int () |
| 1467 |
(let ((tz (car (current-time-zone)))) |
| 1468 |
(if (< tz 0) |
| 1469 |
(format "-%02d%02d" (/ (- tz) 3600) (/ (% (- tz) 3600) 60)) |
| 1470 |
(format "+%02d%02d" (/ tz 3600) (/ (% tz 3600) 60))))) |
| 1471 |
|
| 1472 |
;; Wed, 26 Jul 2000 21:18:35 +0900 (JST) |
| 1473 |
(defun mew-time-ctz-to-rfc (time) |
| 1474 |
(let* ((system-time-locale "C") |
| 1475 |
;; A bug of Emacs 22.3 on Windows |
| 1476 |
(time-zone-name (format-time-string "%Z" time)) |
| 1477 |
(date (format-time-string "%a, %d %b %Y %T %z" time))) |
| 1478 |
(if (string= time-zone-name "") |
| 1479 |
date |
| 1480 |
(concat date (format " (%s)" time-zone-name))))) |
| 1481 |
|
| 1482 |
;; 2000/07/12 16:22:30 |
| 1483 |
(defun mew-time-ctz-to-logtime (time) |
| 1484 |
(let ((system-time-locale "C")) |
| 1485 |
(format-time-string "%Y/%m/%d %H:%M:%S" time))) |
| 1486 |
|
| 1487 |
;; 20000712.155559 |
| 1488 |
(defun mew-time-ctz-to-msgid (time) |
| 1489 |
(let ((system-time-locale "C")) |
| 1490 |
(format-time-string "%Y%m%d.%H%M%S" time))) |
| 1491 |
|
| 1492 |
;; |
| 1493 |
|
| 1494 |
(defun mew-time-calc (new old) |
| 1495 |
(let ((million 1000000) |
| 1496 |
(sec (+ (* 65536 (- (nth 0 new) (nth 0 old))) |
| 1497 |
(- (nth 1 new) (nth 1 old)))) |
| 1498 |
(usec (- (nth 2 new) (nth 2 old)))) |
| 1499 |
(if (< usec 0) |
| 1500 |
(setq sec (1- sec) |
| 1501 |
usec (+ usec million)) |
| 1502 |
(if (>= usec million) |
| 1503 |
(setq sec (1+ sec) |
| 1504 |
usec (- usec million)))) |
| 1505 |
(+ sec (/ usec (float million))))) |
| 1506 |
|
| 1507 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1508 |
;;; |
| 1509 |
;;; Multibyte |
| 1510 |
;;; |
| 1511 |
|
| 1512 |
(defun mew-set-buffer-multibyte (arg) |
| 1513 |
(if (fboundp 'set-buffer-multibyte) |
| 1514 |
(set-buffer-multibyte arg))) |
| 1515 |
|
| 1516 |
(defun mew-set-string-multibyte (str) |
| 1517 |
(if (fboundp 'string-as-multibyte) |
| 1518 |
(string-as-multibyte str) |
| 1519 |
str)) |
| 1520 |
|
| 1521 |
(defun mew-multibyte-string-p (str) |
| 1522 |
(if (fboundp 'multibyte-string-p) |
| 1523 |
(multibyte-string-p str))) |
| 1524 |
|
| 1525 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1526 |
;;; |
| 1527 |
;;; Network |
| 1528 |
;;; |
| 1529 |
|
| 1530 |
(defun mew-dot-insert () |
| 1531 |
(goto-char (point-min)) |
| 1532 |
(while (re-search-forward "^\\." nil t) |
| 1533 |
(insert ".") |
| 1534 |
(forward-line))) |
| 1535 |
|
| 1536 |
(defun mew-dot-delete () |
| 1537 |
(goto-char (point-max)) |
| 1538 |
(forward-line -1) |
| 1539 |
(delete-region (point) (point-max)) |
| 1540 |
(goto-char (point-min)) |
| 1541 |
(while (re-search-forward "^\\." nil t) |
| 1542 |
(delete-char -1) |
| 1543 |
(forward-line))) |
| 1544 |
|
| 1545 |
(defun mew-crlf-to-lf () |
| 1546 |
(while (search-forward "\r\n" nil t) (replace-match "\n" nil t))) |
| 1547 |
|
| 1548 |
(defun mew-lf-to-crlf () |
| 1549 |
(while (search-forward "\n" nil t) (replace-match "\r\n" nil t))) |
| 1550 |
|
| 1551 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1552 |
;;; |
| 1553 |
;;; sit-for |
| 1554 |
;;; |
| 1555 |
|
| 1556 |
(defun mew-redraw (&optional time) |
| 1557 |
(sit-for (or time 0))) |
| 1558 |
|
| 1559 |
(defmacro mew-rendezvous (who) |
| 1560 |
;; Wait for the termination of WHO. |
| 1561 |
;; Emacs does not provide synchronize mechanism with |
| 1562 |
;; an asynchronous process. So, take this way. |
| 1563 |
`(while ,who |
| 1564 |
(sit-for 0.1) |
| 1565 |
;; accept-process-output or sleep-for is not enough |
| 1566 |
(discard-input))) |
| 1567 |
|
| 1568 |
(defun mew-let-user-read () |
| 1569 |
(sit-for 1.5)) |
| 1570 |
|
| 1571 |
(defun mew-warn (&rest msg) |
| 1572 |
(apply 'message msg) |
| 1573 |
(ding) |
| 1574 |
(mew-let-user-read)) |
| 1575 |
|
| 1576 |
(defun mew-timing () |
| 1577 |
(sit-for 0.01)) |
| 1578 |
|
| 1579 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1580 |
;;; |
| 1581 |
;;; Multiple set |
| 1582 |
;;; |
| 1583 |
|
| 1584 |
(defun mew-set (vars vals) |
| 1585 |
(dolist (var vars) |
| 1586 |
(if var (set var (car vals))) ;; var can be nil to skip |
| 1587 |
(setq vals (cdr vals)))) |
| 1588 |
|
| 1589 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1590 |
;;; |
| 1591 |
;;; Structure |
| 1592 |
;;; |
| 1593 |
|
| 1594 |
;;; |
| 1595 |
;;; Some old code uses assoc. So, you cannot put the structure name |
| 1596 |
;;; into the car. |
| 1597 |
;;; |
| 1598 |
|
| 1599 |
(defun mew-keyword-number-pair (spec) |
| 1600 |
(let ((len (length spec)) key ret) |
| 1601 |
(dotimes (i len) |
| 1602 |
(setq key (intern (concat ":" (symbol-name (car spec))))) |
| 1603 |
(setq ret (cons (cons key i) ret)) |
| 1604 |
(setq spec (cdr spec))) |
| 1605 |
(nreverse ret))) |
| 1606 |
|
| 1607 |
(defmacro mew-defstruct (type &rest spec) |
| 1608 |
`(progn |
| 1609 |
(mew-defstruct-constructor ,type ,@spec) |
| 1610 |
(mew-defstruct-s/getter ,type ,@spec))) |
| 1611 |
|
| 1612 |
(defmacro mew-defstruct-constructor (type &rest spec) |
| 1613 |
`(defun ,(intern (concat "mew-make-" (symbol-name type))) (&rest args) |
| 1614 |
(let* ((alist (quote ,(mew-keyword-number-pair spec))) |
| 1615 |
(struct (make-list (length alist) nil)) |
| 1616 |
key val key-num) |
| 1617 |
(while args ;; cannot use dolist |
| 1618 |
(setq key (car args)) |
| 1619 |
(setq args (cdr args)) |
| 1620 |
(setq val (car args)) |
| 1621 |
(setq args (cdr args)) |
| 1622 |
(unless (keywordp key) |
| 1623 |
(error "'%s' is not a keyword" key)) |
| 1624 |
(setq key-num (assoc key alist)) |
| 1625 |
(if key-num |
| 1626 |
(setcar (nthcdr (cdr key-num) struct) val) |
| 1627 |
(error "'%s' is unknown" key))) |
| 1628 |
struct))) |
| 1629 |
|
| 1630 |
(defmacro mew-defstruct-s/getter (type &rest spec) |
| 1631 |
`(let* ((type-name (symbol-name ',type)) |
| 1632 |
(keys ',spec) |
| 1633 |
(len (length keys)) |
| 1634 |
member-name setter getter) |
| 1635 |
(dotimes (i len) |
| 1636 |
(setq member-name (symbol-name (car keys))) |
| 1637 |
(setq setter (intern (format "mew-%s-set-%s" type-name member-name))) |
| 1638 |
(fset setter (list 'lambda '(struct value) (list 'setcar (list 'nthcdr i 'struct) 'value) 'struct)) |
| 1639 |
(setq getter (intern (format "mew-%s-get-%s" type-name member-name))) |
| 1640 |
(fset getter (list 'lambda '(struct) (list 'nth i 'struct))) |
| 1641 |
(setq keys (cdr keys))))) |
| 1642 |
|
| 1643 |
(provide 'mew-func) |
| 1644 |
|
| 1645 |
;;; Copyright Notice: |
| 1646 |
|
| 1647 |
;; Copyright (C) 1997-2015 Mew developing team. |
| 1648 |
;; All rights reserved. |
| 1649 |
|
| 1650 |
;; Redistribution and use in source and binary forms, with or without |
| 1651 |
;; modification, are permitted provided that the following conditions |
| 1652 |
;; are met: |
| 1653 |
;; |
| 1654 |
;; 1. Redistributions of source code must retain the above copyright |
| 1655 |
;; notice, this list of conditions and the following disclaimer. |
| 1656 |
;; 2. Redistributions in binary form must reproduce the above copyright |
| 1657 |
;; notice, this list of conditions and the following disclaimer in the |
| 1658 |
;; documentation and/or other materials provided with the distribution. |
| 1659 |
;; 3. Neither the name of the team nor the names of its contributors |
| 1660 |
;; may be used to endorse or promote products derived from this software |
| 1661 |
;; without specific prior written permission. |
| 1662 |
;; |
| 1663 |
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND |
| 1664 |
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 1665 |
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 1666 |
;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE |
| 1667 |
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 1668 |
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 1669 |
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 1670 |
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 1671 |
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 1672 |
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 1673 |
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 1674 |
|
| 1675 |
;;; mew-func.el ends here |