File: mew-complete.el

package info (click to toggle)
mew 1.93b33-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,072 kB
  • ctags: 1,720
  • sloc: lisp: 13,877; ansic: 2,325; sh: 305; makefile: 174; perl: 23
file content (394 lines) | stat: -rw-r--r-- 10,663 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
;;; mew-complete.el --- Completion magic for Mew

;; Author:  Kazu Yamamoto <Kazu@Mew.org>
;; Created: May 30, 1997
;; Revised: Aug  6, 1997

;;; Code:

(defconst mew-complete-version "mew-complete.el version 0.02")

(require 'mew)

;;
;; Switching completion in Draft
;;

(defun mew-draft-header-comp ()
  (interactive)
  (if (mew-draft-on-field-p)
      (mew-complete-field)
    (let ((func (mew-draft-on-value-p mew-field-completion-switch)))
      (if func 
	  (funcall (cdr func))
	(tab-to-tab-stop))))) ;; default keybinding

(defun mew-draft-on-field-p ()
  (if (bolp)
      (if (bobp) 
	  t
	(save-excursion
	  (forward-line -1)
	  (if (looking-at ".*,[ \t]?$") nil t)
	  )
	)
    (let ((pos (point)))
      (save-excursion
	(beginning-of-line)
	(if (looking-at mew-lwsp)
	    nil
	  (if (re-search-forward ":" pos t) nil t))
	))
    ))
      
(defun mew-draft-on-value-p (switch)
  (save-excursion
    (beginning-of-line)
    (while (and (< (point-min) (point))	(looking-at mew-lwsp))
      (forward-line -1)
      )
    (if (looking-at "\\([^:]*:\\)")
	(mew-assoc-match (mew-match 1) switch 0)
      nil) ;; what a case reach here?
    ))
      
;;
;; Window management for completion candidates
;;

(defvar mew-complete-candidates nil)

(defun mew-complete-window-delete ()
  (if (null mew-complete-window-config)
      ()
    ;; mew-complete-window-config remains when the last completion  
    ;; finished with multiple candidates.
    ;; (e.g. foo<RET> when foo and foobar are displayed.)
    ;; In this case, this function is called in another
    ;; completion thread but setting window configuration is not
    ;; desired. If we set window configuration with the old
    ;; mew-complete-window-config, the cursor jumps to mini buffer.
    ;; This was a stupid bug of Mew. So, let's see if the complete
    ;; buffer is displayed or not.
    (if (get-buffer-window mew-buffer-completions)
	(set-window-configuration mew-complete-window-config))
    (setq mew-complete-window-config nil)
    )
  (and (get-buffer mew-buffer-completions)
       (kill-buffer mew-buffer-completions))
  (setq mew-complete-candidates nil)
  )


(defun mew-complete-window-show (all)
  (or mew-complete-window-config
      (setq mew-complete-window-config (current-window-configuration)))
  (if (and (get-buffer-window mew-buffer-completions)
	   (equal mew-complete-candidates all))
      (let ((win (get-buffer-window mew-buffer-completions)))
	(save-excursion
	  (set-buffer mew-buffer-completions)
	  (if (pos-visible-in-window-p (point-max) win)
	      (set-window-start win 1)
	    (scroll-other-window))))
    (setq mew-complete-candidates all)
    (with-output-to-temp-buffer
	mew-buffer-completions
      (display-completion-list all))))

;;
;; Completion function
;;

(defun mew-complete-field ()
  (interactive)
  (let ((word (mew-delete-key))) ;; capitalized
    (if (null word)
	(mew-complete-window-show mew-fields)
      (mew-complete
       word
       (mew-fields-make-alist mew-fields)
       "field"
       nil) ;; use car
      )
    ))

(defun mew-complete-folder ()
  (interactive)
  (let ((word (mew-delete-backward-char)))
    (if (null word)
	(mew-complete-window-show (list "+" "="))
      (mew-complete
       word
       mew-folder-alist
       "folder"
       nil) ;; use car
      )
    ))

(defun mew-complete-address ()
  (interactive)
  (let ((word (mew-delete-backward-char)))
    (if (null word)
	(tab-to-tab-stop)
      (mew-complete
       word
       mew-alias-alist
       "alias"
       ?@) ;; use cdr
      )
    ))

(defun mew-complete-config ()
  (interactive)
  (let ((word (mew-delete-value ",")))
    (if (null word)
	(tab-to-tab-stop)
      (mew-complete
       word
       (mew-slide-pair mew-config-list)
       "mew-config-list"
       nil) ;; use car
      )
    ))

;;
;; Circular completion: C-cC-t
;;

(defun mew-draft-circular-comp ()
  (interactive)
  (let ((func (mew-draft-on-value-p mew-field-circular-completion-switch)))
    (if func
	(funcall (cdr func))
      (message "No circular completion here"))))

(defun mew-circular-complete-domain ()
  (interactive)
  (let ((word (mew-delete-backward-char "@")))
    (cond
     ((equal word nil) ;; @ doesn't exist.
      (if (null mew-mail-domain-list)
	  (message "For domain circular completion, set mew-mail-domain-list")
	(insert "@")
	(insert (car mew-mail-domain-list))
	(mew-complete-window-delete))
      )
     ((equal word t) ;; just after @
      (if (null mew-mail-domain-list)
	  ()
	(insert (car mew-mail-domain-list))
	(mew-complete-window-delete))
      )
     (t
      (mew-complete
       word
       (mew-slide-pair mew-mail-domain-list)
       "domain"
       t) ;; use cdr
      )
     )
    ))

(defun mew-circular-complete (msg clist cname &optional here)
  (interactive)
  (let ((str (mew-delete-value here)))
    (if (null str)
	(if (car clist)
	    (insert (car clist))
	  (message "For circular completion, set %s" cname))
      (mew-complete
       str
       (mew-slide-pair clist)
       msg
       t) ;; use cdr
      )))

(defun mew-circular-complete-from ()
  (interactive)
  (mew-circular-complete "from" mew-from-list "mew-from-list"))

(defun mew-circular-complete-config ()
  (interactive)
  (mew-circular-complete "config" mew-config-list "mew-config-list" ","))

;;
;; Hart function for completions
;;

(defun mew-complete (WORD ALIST MSG EPAND-CHAR)
  (let ((cmp (try-completion WORD ALIST))
	(all (all-completions WORD ALIST))
	(len (length WORD)))
    (cond
     ;; already completed
     ((eq cmp t)
      (if EPAND-CHAR
	  (insert (cdr (assoc WORD ALIST))) ;; use cdr
	(insert WORD)) ;; use car
      (mew-complete-window-delete))
     ;; EXPAND
     ((and (mew-characterp EPAND-CHAR)
	   (char-equal (aref WORD (1- len)) EPAND-CHAR)
	   (assoc (substring WORD 0 (1- len)) ALIST))
      (insert (cdr (assoc (substring WORD 0 (1- len)) ALIST))) ;; use cdr
      (mew-complete-window-delete))
     ;; just one candidate
     ((equal 1 (length all))
      (insert cmp)
      (mew-complete-window-delete)
      (if (window-minibuffer-p (get-buffer-window (current-buffer)))
	  (mew-temp-minibuffer-message " [Sole completion]")
	(message "Sole completion")))
     ;; two or more candidates
     ((stringp cmp) ;; (length all) > 1
      (insert cmp)
      (mew-complete-window-show all))
     ;; no candidate
     (t
      (insert WORD)
      ;;(mew-complete-window-delete)
      (if (window-minibuffer-p (get-buffer-window (current-buffer)))
	  (mew-temp-minibuffer-message (concat " No matching " MSG))
	(message "No matching %s" MSG))
      )
     )
    ))

;;
;; Minibuf magic
;;

(defun mew-temp-minibuffer-message (m)
  (let ((savemax (point-max)))
    (save-excursion
      (goto-char (point-max))
      (insert m))
    (let ((inhibit-quit t))
      (sit-for 2)
      (delete-region savemax (point-max))
      (if quit-flag (setq quit-flag nil	unread-command-events 7))
      )))


;;
;; Extracting completion key
;;

(defun mew-delete-backward-char (&optional here)
  "Delete appropriate preceeding word and return it."
  (interactive)
  (let ((case-fold-search t)
	(start nil)
	(end (point))
	(regex (concat "[^" mew-address-separator "]")))
    (save-excursion
      (while (and (not (bobp))
		  (string-match regex (mew-buffer-substring
				       (1- (point)) (point))))
	(forward-char -1)
	)
      (if (and here (not (re-search-forward (regexp-quote here) end t)))
	  nil ;; "here" doesn't exist.
	  (setq start (point))
	  (if (= start end)
	      (if here t nil) ;; just after "here",  just after separator
	    (prog1
		(mew-buffer-substring start end)
	      (delete-region start end)))
	  ))
    ))

(defun mew-delete-key ()
  (let ((pos (point)))
    (beginning-of-line)
    (prog1
	(capitalize (mew-buffer-substring (point) pos))
      (delete-region (point) pos)
      )
    ))

(defun mew-delete-value (&optional here)
  (beginning-of-line)
  (if (not (looking-at "[^:]+:"))
      ()
    (goto-char (match-end 0))
    (if (looking-at "[ \t]")
	(forward-char 1)
      (insert " "))
    (if (eolp)
	nil
      (let ((start (point)) ret)
	(end-of-line)
	(if (and here (re-search-backward (regexp-quote here) start t))
	    (progn
	      (setq start (1+ (point)))
	      (end-of-line)))
	(setq ret (buffer-substring start (point)))
	(delete-region start (point))
	ret))))

;;
;; Making alist
;;

(defun mew-fields-make-alist (list)
  (mapcar
   (function (lambda (x) (cons (concat (capitalize x) " ") nil)))
   list)
  )

(defun mew-slide-pair (x)
  (let ((ret nil)
	(first (car x)))
    (cond 
     ((eq x 0) nil)
     ((eq x 1) (cons first first))
     (t
      (while (cdr x)
	(setq ret (cons (cons (nth 0 x) (nth 1 x)) ret))
	(setq x (cdr x))
	)
      (setq ret (cons (cons (car x) first) ret))
      (nreverse ret)
      )
     )
    ))

(provide 'mew-complete)

;;; Copyright Notice:

;; Copyright (C) 1997, 1998 Mew developing team.
;; All rights reserved.

;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;; 
;; 1. Redistributions of source code must retain the above copyright
;;    notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;;    notice, this list of conditions and the following disclaimer in the
;;    documentation and/or other materials provided with the distribution.
;; 3. All advertising materials mentioning features or use of this software
;;    must display the following acknowledgement:
;;       This product includes software developed by 
;;       Mew developing team and its contributors.
;; 4. Neither the name of the team nor the names of its contributors
;;    may be used to endorse or promote products derived from this software
;;    without specific prior written permission.
;; 
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
;; PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

;;; mew-complete.el ends here