completing-read.el 17.8 KB
Newer Older
Linus Tolke's avatar
Linus Tolke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;;;;;
;;;;; $Id$
;;;;; Copyright (C) 1991  Lysator Academic Computer Association.
;;;;;
;;;;; This file is part of the LysKOM server.
;;;;; 
;;;;; LysKOM is free software; you can redistribute it and/or modify it
;;;;; under the terms of the GNU General Public License as published by 
;;;;; the Free Software Foundation; either version 1, or (at your option) 
;;;;; any later version.
;;;;; 
;;;;; LysKOM is distributed in the hope that it will be useful, but WITHOUT
;;;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;;;;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
;;;;; for more details.
;;;;; 
;;;;; You should have received a copy of the GNU General Public License
;;;;; along with LysKOM; see the file COPYING.  If not, write to
;;;;; Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
;;;;; or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
;;;;; MA 02139, USA.
;;;;;
;;;;; Please mail bug reports to bug-lyskom@lysator.liu.se. 
;;;;;
Per Cederqvist's avatar
.    
Per Cederqvist committed
25
26
27
28
29
30
31
32
33
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: completing-read.el
;;;;
;;;; This file implements functions for reading a conference name
;;;; or a person name with completion and other help.
;;;;

34
35
36
37
38
(setq lyskom-clientversion-long 
      (concat lyskom-clientversion-long
	      "$Id$\n"))


Per Cederqvist's avatar
.    
Per Cederqvist committed
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
;;; Author: Linus Tolke


;;; ================================================================
;;;      Some entry points into the functions in this file


(defun lyskom-completing-read-conf-stat (queue handler prompt
					       type new initial &rest data)
  "Same as lyskom-completing-read, but give the handler a conf-stat
instead of a conf-no.
ARGS: QUEUE HANDLER PROMPT TYPE NEW INITIAL &rest DATA"
  (apply 'lyskom-completing-read 
	 queue 'lyskom-completing-read-conf-stat-handler
	 prompt type new initial queue handler data))


(defun lyskom-completing-read-conf-stat-handler (conf-no queue handler &rest data)
  "Take CONF-NO (returned by lyskom-completing-read) and send it through QUEUE
to HANDLER using initiate-get-conf-stat. Also send DATA to it."
  (if data
      (apply 'initiate-get-conf-stat queue handler conf-no data)
    (initiate-get-conf-stat queue handler conf-no)))


(defvar lyskom-completing-map nil
  "Keymap to read the conf names with completion.")

(defvar lyskom-initial-completing-map nil
  "Keymap to hack certain lossage wrt scrolling other window in minibuffer.")


(defun lyskom-completing-read (kom-queue handler 
					 prompt type new initial &rest data)
  "Read the name of a person or conf using minibuffer.
Call server for name lookup using KOM-QUEUE. If KOM-QUEUE nil then using 
the kom-queue miniread.
Then call the function HANDLER.
The arguments PROMPT, TYPE, NEW and INITIAL are used to do the read.
PROMPT and INITIAL are strings. TYPE is 'person if only allowed to return a
person, 'conf if only a conference is accepted, nil if you will accept both.
If NEW is nil then only persons and conferences already existing are accepted.
If NEW is t then a name not yet existing and not completing to an existing name
is accepted.
If NEW is 'empty then the empty string is accepted.
The function HANDLER is called with the arguments conf-no DATA.
If the parameter NEW is t and a name not existing is read then HANDLER is 
called with the arguments name DATA.
If the parameter NEW is 'empty and the empty string is chosen then HANDLER is
called with the arguments 0 DATA."
  (if kom-queue
      nil
    (setq kom-queue 'miniread))
  (lyskom-completing-read-2 kom-queue handler
			    prompt type (eq new t) (eq new 'empty) initial
			    data (current-window-configuration)))


;;; ================================================================


(defun lyskom-completing-read-2 (kom-queue handler prompt type new empty
				 initial data curwin
				 &optional completion-buffer)
  "Read the name of person or conf using minibuffer. Use lyskom-completing-read.
This function is called by lyskom-completing-read and calls itself recursively.
It reads a name (or part of it) using the minibuffer and does all the work that
is to be done before lookup-name is called.
When/If it is decided that lookup-name is requested then that is done and the
result is treated by lyskom-completing-read-3.
The args: KOM-QUEUE HANDLER PROMPT TYPE NEW EMPTY INITIAL DATA CURWIN
	  COMPLETION-BUFFER
The TYPE is one of: nil, person and conf. nil means choose both persons and
confs.
The NEW is non-nil then accept existing and non-existing names.
If EMPTY is non-nil then accept existing names or the empty string.
The CURWIN is the window configuration that we should return to when done.
COMPLETION-BUFFER, if given, tells which window the completions are shown in.
When C-G is pressed then lyskom-end-of-command is evaled."
  (let* (window-conf
	 (window (selected-window))
	 (buffer (current-buffer))
	 (return
	  (read-from-minibuffer (if kom-emacs-knows-iso-8859-1
				    prompt
				  (iso-8859-1-to-swascii prompt))
				initial
				lyskom-initial-completing-map))
	 (result (substring return 1))
	 (control (string-to-char return))
	 no)
    (if window-conf
	(progn
	  (set-window-configuration window-conf)
	  (select-window window)))
    (set-buffer buffer)
    (cond
     ((and empty
	   (eq control ?X)
	   (string= result ""))
      (lyskom-complete-return 0 handler data curwin))

     ((and (string= result "")
	   (eq control ?X))
      (lyskom-message (concat prompt " " result " [Not sole completion]"))
      (sit-for 2)
      (lyskom-completing-read-2 kom-queue handler prompt type new empty result
				data curwin))

     ((and (eq control ?X)
	   (string-match
	    (lyskom-get-string 'person-or-conf-no-regexp)
	    result))
      (setq no (string-to-int (substring result
					 (match-beginning 1)
					 (match-end 1))))
      (initiate-get-conf-stat kom-queue 'lyskom-complete-verify-type no
			      kom-queue handler prompt type new empty 
			      result control data curwin))
     ((eq control ?G)			;QUIT
      (set-window-configuration curwin)
      (lyskom-end-of-command))
     (t
      (initiate-lookup-name kom-queue 'lyskom-completing-read-3 result
			    control kom-queue handler prompt type new empty 
			    result data curwin)))))


(defun lyskom-completing-read-3 (conf-list what
				 kom-queue handler prompt type new empty
				 initial data curwin)
  "Treats the result from the lookup-name. Use lyskom-completing-read.
This function is called by lyskom-completing-read-2 and calls 
lyskom-completing-read-2 again if necessary.
The args: CONF-LIST WHAT KOM-QUEUE HANDLER PROMPT TYPE NEW EMPTY
	  INITIAL DATA CURWIN
CONF-LIST is the result from the lookup-name call.
If WHAT is nil then just reads from the minibuffer with PROMPT and INITIAL.
If WHAT is ?L then build a list of completions using lookup to the server.
If WHAT is ?X and CONF-LIST only contains 1 element of TYPE then calls HANDLER
with arguments conf-no DATA.
If WHAT is ?C then completes the buffer contents from the names of the 
conferences in CONF-LIST.
The TYPE is one of: nil, person and conf. nil means choose both persons and
confs.
The NEW is non-nil then accept existing and non-existing names.
If EMPTY is non-nil then accept existing names or the empty string.
The CURWIN is the window configuration that we should return to when done.
When C-G is pressed then lyskom-end-of-command is evaled."
  (cond
   ((eq what ?L)			;Build completions list
    (lyskom-collect kom-queue)
    (lyskom-traverse
     conf-no (lyskom-get-from-conf-list (conf-list->conf-types conf-list)
					(conf-list->conf-nos conf-list)
					type)
     (initiate-get-conf-stat kom-queue nil conf-no))
    (lyskom-list-use kom-queue 'lyskom-show-completions
		     kom-queue handler prompt type new empty
		     initial data curwin))

   ((or (eq what ?X)			;Complete and exit
	(eq what ?C))			;Complete
    (let ((choice (if type
		      (lyskom-get-from-conf-list
		       (conf-list->conf-types conf-list)
		       (conf-list->conf-nos conf-list)
		       type)
		    (conf-list->conf-nos conf-list))))
      (cond
       ((and (= (length choice) 1)
	     (eq what ?X))
	(lyskom-complete-return (elt choice 0) handler data curwin))
       ((and new
	     (= (length choice) 0)
	     (eq what ?X))
	(lyskom-complete-return initial handler data curwin))
       ((or (eq what ?C)
	    (and (eq what ?X)
		 (> (length choice) 1)))
	(lyskom-collect-ignore-err kom-queue)
	(lyskom-traverse
	 conf-no choice
	 (initiate-get-conf-stat kom-queue nil conf-no))
	(lyskom-list-use kom-queue 'lyskom-complete-and-edit
			 what kom-queue handler prompt type new empty initial
			 data curwin))
       (t
	(lyskom-message (concat prompt initial
			 (if (= (length choice)
				0)
			     " [No match]"
   			   " [Next char not unique]")))
	(sit-for 2)
	(lyskom-completing-read-2 kom-queue handler prompt type new empty 
				  initial data curwin)))))

   (t
    (signal 'lyskom-internal-error
	    (list 'lyskom-completing-read
		  (char-to-string what))))))


(defun lyskom-show-completions (conf-stat-list
				kom-queue handler prompt type new empty
				initial data curwin)
  "Show all completions collected."
  (let (completion-buffer)
    (with-output-to-temp-buffer " *Completions*"
      (princ "Possible completions are:\n")
      (mapcar '(lambda (conf-stat)
		 (princ (conf-stat->name conf-stat))
		 (terpri))
	      conf-stat-list)
      (setq completion-buffer standard-output))
    (lyskom-run kom-queue 'lyskom-completing-read-2
		kom-queue handler prompt type new empty initial
		data curwin completion-buffer)))


;;; The problem:
;;;    When getting a list of possible completions, C-M-v scrolls
Per Cederqvist's avatar
Per Cederqvist committed
261
;;;    the wrong window (the LysKOM buffer instead of the
Per Cederqvist's avatar
.    
Per Cederqvist committed
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
;;;    *Completions* buffer, as from M-x and other commands).

;;; Cause:
;;;    Normally, when getting a completion list from other commands
;;;    using 'completing-read', the minibuffer routines sets the
;;;    variable 'minibuffer-scroll-window' to point to the *Completions*
;;;    window.  This is difficult to do from lyskom-completing-read,
;;;    since the minibuffer is temporary exited when getting completions.

;;; Solution:
;;;    Set minibuffer-scroll-window in the minibuffer.  But this is
;;;    tricky.  What we do, is that we give a special keymap to
;;;    read-minibuffer, where *all* keys (except C-g) are bound to
;;;    lyskom-hack-minibuf.  This function sets minibuffer-scroll-window
;;;    to the correct value, switches keymap to lyskom-completing-map,
;;;    which is the map doing the real job, and push back the given
;;;    character to be read again (and thus interpreted in the new
;;;    keymap context).
;;;
;;;    The window to be scrolled is identified by the buffer in the
;;;    parameter completion-buffer to lyskom-completing-read-2, which
;;;    is set in lyskom-show-completions.

(defun lyskom-hack-minibuf ()
  "Fix lossage due to minibuffer deletion when requesting alternatives."
  (interactive)
  (setq minibuffer-scroll-window
	(and completion-buffer
	     (get-buffer-window completion-buffer)))
  (use-local-map lyskom-completing-map)
292
293
294
  (if (fboundp 'map-keymap)		;Special for lucid-emacs.
      (setq unread-command-event last-command-event)
    (setq unread-command-char last-command-char)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411


(defun lyskom-complete-and-edit (conf-stats what
				 kom-queue handler prompt type new empty 
				 initial data curwin)
  "Used to catch the lookup return when typing \\[lyskom-complete].
Or when typing \\[lyskom-complete-and-exit] and there are several alternatives
then completing and trying to read again."
  (let (nos
	(completion-ignore-case t))	;+++ should perhaps be 
					;lyskom-ignore-case-in-names flag
					;instead 
    (cond
     ((null conf-stats)			;+++ Annan felhantering
      (lyskom-completing-read-2 kom-queue handler prompt type new empty initial
				data curwin))
     ((null (cdr conf-stats))		;Sole completion.
      (if (string= initial (conf-stat->name (car conf-stats)))
	  (progn
	    (lyskom-message (concat prompt initial " [Sole completion]"))
	    (sit-for 2)))
      (lyskom-completing-read-2 kom-queue handler prompt type new empty
				(conf-stat->name (car conf-stats))
				data curwin))
     ((and (eq what ?X)			;Exact match is OK even if other
	   (setq nos (car (apply 'append;matches exist.
				 (mapcar
				  'lyskom-complete-test-exact-name
				  conf-stats)))))
      (lyskom-complete-return nos handler data curwin))
     (t					;Difficult one.
      (let ((newname
	     (lyskom-try-complete-partials initial
					   (mapcar 
					    'lyskom-complete-get-name-in-list
					    conf-stats))))
	(if completion-auto-help	;Build list more often.
					;Easier here, we have the conf-stats
	    (lyskom-show-completions conf-stats kom-queue handler prompt 
				     type new empty
				     newname data curwin)
	  (lyskom-completing-read-2 kom-queue handler prompt type new empty
				    newname data curwin)))))))


(defun lyskom-complete-test-exact-name (stat)
  "Returns a list containing the conf-no if CONF-STAT matches exactly.
The variable that the name is tested against is the locally bound initial."
  (and (string= (upcase (conf-stat->name stat))
		(upcase initial))
       (list (conf-stat->conf-no stat))))


(defun lyskom-complete-get-name-in-list (stat)
  "Returns a list of the name (a string) in CONF-STAT."
  (list (conf-stat->name stat)))

(defun lyskom-try-complete-partials (string alist)
  "Returns the longest string matching STRING.
Where every word matches the corresponding word in the car part of ALIST.
parst matching ([^)]) in string and alist are disgarded."
  (let* ((a-whitespace "\\([ \t]\\|([^)]*)\\)+")
	 (endfirstword (string-match a-whitespace string))
	 (firstword (substring string 0 endfirstword))
	 (reststring (and endfirstword
			  (substring string (match-end 0))))
	 (words (or (try-completion firstword alist)
		    string))		;+++ Buggfix. Inget error om []\->{}|
	 (endfirstwords (string-match a-whitespace words))
	 (firstwords (substring words 0 endfirstwords))
	 (restlist (mapcar
		    (function
		     (lambda (part)
		       (cond
			((string-match a-whitespace
				       (car part))
			 (list (substring (car part) (match-end 0))))
			((list "")))))
		    alist)))
    (if	(= (length reststring) 0)
	words
      (concat (if (> (length firstwords) (length firstword))
		  firstwords
		firstword)
	      " " (lyskom-try-complete-partials reststring
						restlist)))))
	

(defun lyskom-complete-verify-type (conf-stat
				    kom-queue handler prompt type new empty
				    initial what data curwin)
  "Used to allow people to write [mp] <number> also."
  (if (and conf-stat			;+++ Annan felhantering
	   (or (null type)
	       (and (eq type 'person)
		    (conf-type->letterbox (conf-stat->conf-type conf-stat)))
	       (and (eq type 'conf)
		    (not (conf-type->letterbox
			  (conf-stat->conf-type conf-stat))))))
      (lyskom-complete-return (conf-stat->conf-no conf-stat) handler
			      data curwin)
    (lyskom-completing-read-2 kom-queue handler prompt type new empty initial
			      data curwin)))


(if lyskom-completing-map
    nil
  (setq lyskom-completing-map (make-sparse-keymap))
  (define-key lyskom-completing-map "\n" 'lyskom-complete-and-exit)
  (define-key lyskom-completing-map "\r" 'lyskom-complete-and-exit)
  (define-key lyskom-completing-map "\t" 'lyskom-complete)
  (define-key lyskom-completing-map "\C-g" 'lyskom-complete-quit)
;  (define-key lyskom-completing-map "\C-\M-v" 'lyskom-scroll-other-minibuffer)
  (define-key lyskom-completing-map "?" 'lyskom-complete-help))

(if lyskom-initial-completing-map
    nil
412
413
414
415
416
417
418
419
420
421
  (setq lyskom-initial-completing-map (make-keymap))
  (if (fboundp 'map-keymap)		;lucid-emacs' way of doing things.
      (map-keymap 
       (function (lambda (keydesc binding)
		   (define-key lyskom-initial-completing-map keydesc 
		     'lyskom-hack-minibuf)))
       global-map)
    (let ((i (length lyskom-initial-completing-map)))
      (while (>= (setq i (1- i)) 0)
	(aset lyskom-initial-completing-map i 'lyskom-hack-minibuf))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
  (define-key lyskom-initial-completing-map "\C-g" 'lyskom-complete-quit))
   

(defun lyskom-complete-and-exit ()
  "Exit the minibuffer if the contents matches a single type."
  (interactive)
  (setq window-conf (current-window-configuration))
  (goto-char (point-min))
  (insert "X")
  (exit-minibuffer))


(defun lyskom-complete-quit ()
  "Exit the minibuffer if the contents matches a single type."
  (interactive)
  (setq window-conf (current-window-configuration))
  (goto-char (point-min))
  (insert "G")
  (exit-minibuffer))


(defun lyskom-complete-help ()
  "Build a list of completions and show it."
  (interactive)
  (setq window-conf (current-window-configuration))
  (goto-char (point-min))
  (insert "L")
  (exit-minibuffer))


(defun lyskom-complete ()
  "Insert the completing parts of the name."
  (interactive)
  (setq window-conf (current-window-configuration))
  (goto-char (point-min))
  (insert "C")
  (exit-minibuffer))


(defun lyskom-scroll-other-minibuffer ()
  "Scrolls the buffer with completions if there is one."
  (interactive)
  (setq minibuffer-scroll-window (get-buffer-window " *Completions*"))
  (scroll-other-window))


(defun lyskom-get-from-conf-list (type-list conf-nos type)
  "Returns a list of all conf-nos of a certain type.
The sequence TYPE-LIST and CONF-NOS have to be corresponding. The TYPE is the 
one chosen. (nil person or conf see lyskom-completing-read)."
  (let* ((list nil)
	 (r 0))
    (while (< r (length type-list))
      (let ((conf-type (elt type-list r))
	    (conf-no (elt conf-nos r)))
	(if (or (null type)
		(and (eq type 'person)
		     (conf-type->letterbox conf-type))
		(and (eq type 'conf)
		     (not (conf-type->letterbox conf-type))))
	    (setq list (cons conf-no list))))
      (setq r (1+ r)))
    (nreverse list)))
      
	

(defun lyskom-complete-return (return handler data curwin)
  "Returns from lyskom-completing-read.
Arguments: RETURN HANDLER DATA CURWIN.
Sets the window configuration back to CURWIN. Then applies HANDLER with 
arguments RETURN and DATA."
  (set-window-configuration curwin)
  (apply handler return data))