/[cmucl]/src/hemlock/completion.lisp
ViewVC logotype

Contents of /src/hemlock/completion.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Mon Oct 31 04:50:12 1994 UTC (19 years, 5 months ago) by ram
Branch: MAIN
CVS Tags: sparc-tramp-assem-base, double-double-array-base, post-merge-intl-branch, release-19b-pre1, release-19b-pre2, merged-unicode-utf16-extfmt-2009-06-11, double-double-init-sparc-2, unicode-utf16-extfmt-2009-03-27, double-double-base, snapshot-2007-09, snapshot-2007-08, snapshot-2008-08, snapshot-2008-09, ppc_gencgc_snap_2006-01-06, sse2-packed-2008-11-12, snapshot-2008-05, snapshot-2008-06, snapshot-2008-07, snapshot-2007-05, snapshot-2008-01, snapshot-2008-02, snapshot-2008-03, intl-branch-working-2010-02-19-1000, snapshot-2006-11, snapshot-2006-10, double-double-init-sparc, snapshot-2006-12, unicode-string-buffer-impl-base, sse2-base, release-20b-pre1, release-20b-pre2, unicode-string-buffer-base, RELEASE_18d, sse2-packed-base, sparc-tramp-assem-2010-07-19, amd64-dd-start, snapshot-2003-10, snapshot-2004-10, release-18e-base, release-19f-pre1, snapshot-2008-12, snapshot-2008-11, intl-2-branch-base, snapshot-2004-08, snapshot-2004-09, remove_negative_zero_not_zero, snapshot-2007-01, snapshot-2007-02, snapshot-2004-05, snapshot-2004-06, snapshot-2004-07, release-19e, release-19d, GIT-CONVERSION, double-double-init-ppc, release-19c, dynamic-extent-base, unicode-utf16-sync-2008-12, LINKAGE_TABLE, release-19c-base, cross-sol-x86-merged, label-2009-03-16, release-19f-base, PRE_LINKAGE_TABLE, merge-sse2-packed, mod-arith-base, sparc_gencgc_merge, merge-with-19f, snapshot-2004-12, snapshot-2004-11, intl-branch-working-2010-02-11-1000, RELEASE_18a, RELEASE_18b, RELEASE_18c, unicode-snapshot-2009-05, unicode-snapshot-2009-06, amd64-merge-start, ppc_gencgc_snap_2005-12-17, double-double-init-%make-sparc, unicode-utf16-sync-2008-07, release-18e-pre2, unicode-utf16-sync-2008-09, unicode-utf16-extfmts-sync-2008-12, prm-before-macosx-merge-tag, cold-pcl-base, RELEASE_20b, snapshot-2008-04, snapshot-2003-11, snapshot-2005-07, unicode-utf16-sync-label-2009-03-16, RELEASE_19f, snapshot-2007-03, release-20a-base, cross-sol-x86-base, unicode-utf16-char-support-2009-03-26, unicode-utf16-char-support-2009-03-25, release-19a-base, unicode-utf16-extfmts-pre-sync-2008-11, snapshot-2008-10, sparc_gencgc, snapshot-2007-04, snapshot-2010-12, snapshot-2010-11, unicode-utf16-sync-2008-11, snapshot-2007-07, snapshot-2011-09, snapshot-2011-06, snapshot-2011-07, snapshot-2011-04, snapshot-2007-06, snapshot-2011-02, snapshot-2011-03, snapshot-2011-01, snapshot-2003-12, release-19a-pre1, release-19a-pre3, release-19a-pre2, pre-merge-intl-branch, release-19a, UNICODE-BASE, double-double-array-checkpoint, double-double-reader-checkpoint-1, release-19d-base, release-19e-pre1, double-double-irrat-end, release-19e-pre2, snapshot-2010-05, snapshot-2010-04, snapshot-2010-07, snapshot-2010-06, snapshot-2010-01, snapshot-2010-03, snapshot-2010-02, release-19d-pre2, release-19d-pre1, snapshot-2010-08, release-18e, double-double-init-checkpoint-1, double-double-reader-base, label-2009-03-25, snapshot-2005-03, release-19b-base, cross-sol-x86-2010-12-20, double-double-init-x86, sse2-checkpoint-2008-10-01, intl-branch-2010-03-18-1300, snapshot-2005-11, double-double-sparc-checkpoint-1, snapshot-2004-04, sse2-merge-with-2008-11, sse2-merge-with-2008-10, snapshot-2005-10, RELEASE_20a, snapshot-2005-12, release-20a-pre1, snapshot-2005-01, snapshot-2009-11, snapshot-2009-12, unicode-utf16-extfmt-2009-06-11, portable-clx-import-2009-06-16, unicode-utf16-string-support, release-19c-pre1, cross-sparc-branch-base, release-19e-base, intl-branch-base, double-double-irrat-start, snapshot-2005-06, snapshot-2005-05, snapshot-2005-04, ppc_gencgc_snap_2005-05-14, snapshot-2005-02, unicode-utf16-base, portable-clx-base, snapshot-2005-09, snapshot-2005-08, lisp-executable-base, snapshot-2009-08, snapshot-2007-12, snapshot-2007-10, snapshot-2007-11, snapshot-2009-02, snapshot-2009-01, snapshot-2009-07, snapshot-2009-05, snapshot-2009-04, snapshot-2006-02, snapshot-2006-03, release-18e-pre1, snapshot-2006-01, snapshot-2006-06, snapshot-2006-07, snapshot-2006-04, snapshot-2006-05, pre-telent-clx, snapshot-2006-08, snapshot-2006-09, HEAD
Branch point for: release-19b-branch, double-double-reader-branch, double-double-array-branch, mod-arith-branch, RELEASE-19F-BRANCH, portable-clx-branch, sparc_gencgc_branch, cross-sparc-branch, RELEASE-20B-BRANCH, RELENG_18, unicode-string-buffer-branch, sparc-tramp-assem-branch, dynamic-extent, UNICODE-BRANCH, release-19d-branch, ppc_gencgc_branch, sse2-packed-branch, lisp-executable, RELEASE-20A-BRANCH, amd64-dd-branch, double-double-branch, unicode-string-buffer-impl-branch, intl-branch, release-18e-branch, cold-pcl, unicode-utf16-branch, cross-sol-x86-branch, release-19e-branch, sse2-branch, release-19a-branch, release-19c-branch, intl-2-branch, unicode-utf16-extfmt-branch
Changes since 1.4: +1 -3 lines
Fix headed boilerplate.
1 ram 1.1 ;;; -*- Log: hemlock.log; Package: Hemlock -*-
2     ;;;
3     ;;; **********************************************************************
4 ram 1.3 ;;; This code was written as part of the CMU Common Lisp project at
5     ;;; Carnegie Mellon University, and has been placed in the public domain.
6     ;;;
7     (ext:file-comment
8 ram 1.5 "$Header: /tiger/var/lib/cvsroots/cmucl/src/hemlock/completion.lisp,v 1.5 1994/10/31 04:50:12 ram Rel $")
9 ram 1.3 ;;;
10 ram 1.1 ;;; **********************************************************************
11     ;;;
12     ;;; Written by Skef Wholey and Blaine Burks.
13     ;;; General idea stolen from Jim Salem's TMC LISPM completion code.
14     ;;;
15    
16     (in-package "HEMLOCK")
17    
18    
19    
20     ;;;; The Completion Database.
21    
22     ;;; The top level structure here is an array that gets indexed with the
23     ;;; first three characters of the word to be completed. That will get us to
24     ;;; a list of the strings with that prefix sorted in most-recently-used order.
25     ;;; The number of strings in any given bucket will never exceed
26     ;;; Completion-Bucket-Size-Limit. Strings are stored in the database in
27     ;;; lowercase form always.
28    
29     (defconstant completion-table-size 991)
30    
31 mbb 1.4 (defvar *completions* (make-array completion-table-size :initial-element nil))
32 ram 1.1
33     (defhvar "Completion Bucket Size"
34     "This limits the number of completions saved for a particular combination of
35     the first three letters of any word."
36     :value 20)
37    
38    
39     ;;; Mapping strings into buckets.
40    
41     ;;; The characters that are considered parts of "words" change from mode
42     ;;; to mode.
43     ;;;
44     (defattribute "Completion Wordchar"
45     "1 for characters we consider to be constituents of words.")
46    
47     (defconstant default-other-wordchars
48     '(#\- #\* #\' #\_))
49    
50     (do-alpha-chars (char :both)
51     (setf (character-attribute :completion-wordchar char) 1))
52    
53     (dolist (char '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9))
54     (setf (character-attribute :completion-wordchar char) 1))
55    
56     (dolist (char default-other-wordchars)
57     (setf (character-attribute :completion-wordchar char) 1))
58    
59    
60     ;;; The difference between Lisp mode and the other modes is pretty radical in
61     ;;; this respect. These are interesting too, but they're on by default: #\*,
62     ;;; #\-, and #\_. #\' is on by default too, but it's uninteresting in "Lisp"
63     ;;; mode.
64     ;;;
65     (defconstant default-lisp-wordchars
66     '(#\~ #\! #\@ #\$ #\% #\^ #\& #\+ #\= #\: #\< #\> #\. #\/ #\?))
67    
68     (dolist (char default-lisp-wordchars)
69     (shadow-attribute :completion-wordchar char 1 "Lisp"))
70    
71     (shadow-attribute :completion-wordchar #\' 0 "Lisp")
72    
73     (defmacro completion-char-p (char)
74     `(= (the fixnum (character-attribute :completion-wordchar ,char)) 1))
75    
76     ;;; COMPLETION-BUCKET-FOR returns the Completion-Bucket that might hold a
77     ;;; completion for the given String. With optional Value, sets the bucket.
78     ;;;
79     (defun completion-bucket-for (string length &optional (value nil value-p))
80     (declare (simple-string string)
81     (fixnum length))
82     (when (and (>= length 3)
83     (completion-char-p (char string 0))
84     (completion-char-p (char string 1))
85     (completion-char-p (char string 2)))
86     (let ((index (mod (logxor (ash
87     (logxor
88     (ash (hi::search-hash-code (schar string 0))
89     5)
90     (hi::search-hash-code (schar string 1)))
91     3)
92     (hi::search-hash-code (schar string 2)))
93     completion-table-size)))
94     (declare (fixnum index))
95     (if value-p
96     (setf (svref *completions* index) value)
97     (svref *completions* index)))))
98    
99     (defsetf completion-bucket-for completion-bucket-for)
100    
101    
102     ;;; FIND-COMPLETION returns the most recent string matching the given
103     ;;; Prefix, or Nil if nothing appropriate is in the database. We assume
104     ;;; the Prefix is passed to us in lowercase form so we can use String=. If
105     ;;; we find something appropriate, we bring it to the front of the list.
106     ;;; Prefix-Length, if supplied restricts us to look at just the start of
107     ;;; the string...
108     ;;;
109     (defun find-completion (prefix &optional (prefix-length (length prefix)))
110     (declare (simple-string prefix)
111     (fixnum prefix-length))
112     (let ((bucket (completion-bucket-for prefix prefix-length)))
113     (do ((list bucket (cdr list)))
114     ((null list))
115     (let ((completion (car list)))
116     (declare (simple-string completion))
117     (when (and (>= (length completion) prefix-length)
118     (string= prefix completion
119     :end1 prefix-length
120     :end2 prefix-length))
121     (unless (eq list bucket)
122     (rotatef (car list) (car bucket)))
123     (return completion))))))
124    
125     ;;; RECORD-COMPLETION saves string in the completion database as the first item
126     ;;; in the bucket, that's the most recently used completion. If the bucket is
127     ;;; full, drop the oldest item in the list. If string is already in the
128     ;;; bucket, simply move it to the front. The way we move an element to the
129     ;;; front requires a full bucket to be at least three elements long.
130     ;;;
131     (defun record-completion (string)
132     (declare (simple-string string))
133     (let ((string-length (length string)))
134     (declare (fixnum string-length))
135     (when (> string-length 3)
136     (let ((bucket (completion-bucket-for string string-length))
137     (limit (value completion-bucket-size)))
138     (do ((list bucket (cdr list))
139     (last nil list)
140     (length 1 (1+ length)))
141     ((null list)
142     (setf (completion-bucket-for string string-length)
143     (cons string bucket)))
144     (cond ((= length limit)
145     (setf (car list) string)
146     (setf (completion-bucket-for string string-length) list)
147     (setf (cdr list) bucket)
148     (setf (cdr last) nil)
149     (return))
150     ((string= string (the simple-string (car list)))
151     (unless (eq list bucket)
152     (rotatef (car list) (car bucket)))
153     (return))))))))
154    
155     ;;; ROTATE-COMPLETIONS rotates the completion bucket for the given Prefix.
156     ;;; We just search for the first thing in the bucket with the Prefix, then
157     ;;; move that to the end of the list. If there ain't no such thing there,
158     ;;; or if it's already at the end, we do nothing.
159     ;;;
160     (defun rotate-completions (prefix &optional (prefix-length (length prefix)))
161     (declare (simple-string prefix))
162     (let ((bucket (completion-bucket-for prefix prefix-length)))
163     (do ((list bucket (cdr list))
164     (prev nil list))
165     ((null list))
166     (let ((completion (car list)))
167     (declare (simple-string completion))
168     (when (and (>= (length completion) prefix-length)
169     (string= prefix completion
170     :end1 prefix-length :end2 prefix-length))
171     (when (cdr list)
172     (if prev
173     (setf (cdr prev) (cdr list))
174     (setf (completion-bucket-for prefix prefix-length) (cdr list)))
175     (setf (cdr (last list)) list)
176     (setf (cdr list) nil))
177     (return nil))))))
178    
179    
180    
181     ;;;; Hemlock interface.
182    
183     (defmode "Completion" :transparent-p t :precedence 10.0
184     :documentation
185     "This is a minor mode that saves words greater than three characters in length,
186     allowing later completion of those words. This is very useful for often
187     long identifiers used in Lisp code. All words with the same first three
188     letters are in one list sorted by most recently used. \"Completion Bucket
189     Size\" limits the number of completions saved in each list.")
190    
191     (defcommand "Completion Mode" (p)
192     "Toggles Completion Mode in the current buffer."
193     "Toggles Completion Mode in the current buffer."
194     (declare (ignore p))
195     (setf (buffer-minor-mode (current-buffer) "Completion")
196     (not (buffer-minor-mode (current-buffer) "Completion"))))
197    
198    
199     ;;; Consecutive alphanumeric keystrokes that start a word cause a possible
200     ;;; completion to be displayed in the echo area's modeline, the status line.
201     ;;; Since most insertion is building up a word that was already started, we
202     ;;; keep track of the word in *completion-prefix* that the user is typing. The
203     ;;; length of the thing is kept in *completion-prefix-length*.
204     ;;;
205     (defconstant completion-prefix-max-size 100)
206    
207     (defvar *completion-prefix* (make-string completion-prefix-max-size))
208    
209     (defvar *completion-prefix-length* 0)
210    
211    
212     ;;; "Completion Self Insert" does different stuff depending on whether or
213     ;;; not the thing to be inserted is Completion-Char-P. If it is, then we
214     ;;; try to come up with a possible completion, using Last-Command-Type to
215     ;;; tense things up a bit. Otherwise, if Last-Command-Type says we were
216     ;;; just doing a word, then we record that word in the database.
217     ;;;
218     (defcommand "Completion Self Insert" (p)
219     "Insert the last character typed, showing possible completions. With prefix
220     argument insert the character that many times."
221     "Implements \"Completion Self Insert\". Calling this function is not
222     meaningful."
223 ram 1.2 (let ((char (ext:key-event-char *last-key-event-typed*)))
224 ram 1.1 (unless char (editor-error "Can't insert that character."))
225     (cond ((completion-char-p char)
226     ;; If start of word not already in *completion-prefix*, put it
227     ;; there.
228     (unless (eq (last-command-type) :completion-self-insert)
229     (set-completion-prefix))
230     ;; Then add new stuff.
231     (cond ((and p (> p 1))
232     (fill *completion-prefix* (char-downcase char)
233     :start *completion-prefix-length*
234     :end (+ *completion-prefix-length* p))
235     (incf *completion-prefix-length* p))
236     (t
237     (setf (schar *completion-prefix* *completion-prefix-length*)
238     (char-downcase char))
239     (incf *completion-prefix-length*)))
240     ;; Display possible completion, if any.
241     (display-possible-completion *completion-prefix*
242     *completion-prefix-length*)
243     (setf (last-command-type) :completion-self-insert))
244     (t
245     (when (eq (last-command-type) :completion-self-insert)
246     (record-completion (subseq *completion-prefix*
247     0 *completion-prefix-length*)))))))
248    
249     ;;; SET-COMPLETION-PREFIX grabs any completion-wordchars immediately before
250     ;;; point and stores these into *completion-prefix*.
251     ;;;
252     (defun set-completion-prefix ()
253     (let* ((point (current-point))
254     (point-line (mark-line point)))
255     (cond ((and (previous-character point)
256     (completion-char-p (previous-character point)))
257     (with-mark ((mark point))
258     (reverse-find-attribute mark :completion-wordchar #'zerop)
259     (unless (eq (mark-line mark) point-line)
260     (editor-error "No completion wordchars on this line!"))
261     (let ((insert-string (nstring-downcase
262     (region-to-string
263     (region mark point)))))
264     (replace *completion-prefix* insert-string)
265     (setq *completion-prefix-length* (length insert-string)))))
266     (t
267     (setq *completion-prefix-length* 0)))))
268    
269    
270     (defcommand "Completion Complete Word" (p)
271     "Complete the word if we've got a completion, fixing up the case. Invoking
272     this immediately in succession rotates through possible completions in the
273     buffer. If there is no currently displayed completion, this tries to choose
274     a completion from text immediately before the point and displays the
275     completion if found."
276     "Complete the word if we've got a completion, fixing up the case."
277     (declare (ignore p))
278     (let ((last-command-type (last-command-type)))
279     ;; If the user has been cursoring around and then tries to complete,
280     ;; let him.
281     ;;
282     (unless (member last-command-type '(:completion-self-insert :completion))
283     (set-completion-prefix)
284     (setf last-command-type :completion-self-insert))
285     (case last-command-type
286     (:completion-self-insert
287     (do-completion))
288     (:completion
289     (rotate-completions *completion-prefix* *completion-prefix-length*)
290     (do-completion))))
291     (setf (last-command-type) :completion))
292    
293     (defcommand "List Possible Completions" (p)
294     "List all possible completions of the prefix the user has typed."
295     "List all possible completions of the prefix the user has typed."
296     (declare (ignore p))
297     (let ((last-command-type (last-command-type)))
298     (unless (member last-command-type '(:completion-self-insert :completion))
299     (set-completion-prefix))
300     (let* ((prefix *completion-prefix*)
301     (prefix-length *completion-prefix-length*)
302     (bucket (completion-bucket-for prefix prefix-length)))
303     (with-pop-up-display (s)
304     (dolist (completion bucket)
305     (when (and (> (length completion) prefix-length)
306     (string= completion prefix
307     :end1 prefix-length
308     :end2 prefix-length))
309     (write-line completion s))))))
310     ;; Keep the redisplay hook from clearing any possibly displayed completion.
311     (setf (last-command-type) :completion-self-insert))
312    
313     (defvar *last-completion-mark* nil)
314    
315     (defun do-completion ()
316     (let ((completion (find-completion *completion-prefix*
317     *completion-prefix-length*))
318     (point (current-point)))
319     (when completion
320     (if *last-completion-mark*
321     (move-mark *last-completion-mark* point)
322     (setq *last-completion-mark* (copy-mark point :temporary)))
323     (let ((mark *last-completion-mark*))
324     (reverse-find-attribute mark :completion-wordchar #'zerop)
325     (let* ((region (region mark point))
326     (string (region-to-string region)))
327     (declare (simple-string string))
328     (delete-region region)
329     (let* ((first (position-if #'alpha-char-p string))
330     (next (if first (position-if #'alpha-char-p string
331     :start (1+ first)))))
332 ram 1.2 ;; Often completions start with asterisks when hacking on Lisp
333     ;; code, so we look for alphabetic characters.
334 ram 1.1 (insert-string point
335 ram 1.2 ;; Leave the cascading IF's alone.
336     ;; Writing this as a COND, using LOWER-CASE-P as
337     ;; the test is not equivalent to this code since
338     ;; numbers (and such) are nil for LOWER-CASE-P and
339     ;; UPPER-CASE-P.
340     (if (and first (upper-case-p (schar string first)))
341     (if (and next
342     (upper-case-p (schar string next)))
343     (string-upcase completion)
344     (word-capitalize completion))
345     completion))))))))
346 ram 1.1
347    
348     ;;; WORD-CAPITALIZE is like STRING-CAPITALIZE except that it treats apostrophes
349     ;;; the Right Way.
350     ;;;
351     (defun word-capitalize (string)
352     (let* ((length (length string))
353     (strung (make-string length)))
354     (do ((i 0 (1+ i))
355     (new-word t))
356     ((= i length))
357     (let ((char (schar string i)))
358     (cond ((or (alphanumericp char)
359     (char= char #\'))
360     (setf (schar strung i)
361     (if new-word (char-upcase char) (char-downcase char)))
362     (setq new-word nil))
363     (t
364     (setf (schar strung i) char)
365     (setq new-word t)))))
366     strung))
367    
368     (defcommand "Completion Rotate Completions" (p)
369     "Show another possible completion in the status line, if there is one.
370     If there is no currently displayed completion, this tries to choose a
371     completion from text immediately before the point and displays the
372     completion if found. With an argument, rotate the completion ring that many
373     times."
374     "Show another possible completion in the status line, if there is one.
375     With an argument, rotate the completion ring that many times."
376     (unless (eq (last-command-type) :completion-self-insert)
377     (set-completion-prefix)
378     (setf (last-command-type) :completion-self-insert))
379     (dotimes (i (or p 1))
380     (rotate-completions *completion-prefix* *completion-prefix-length*))
381     (display-possible-completion *completion-prefix* *completion-prefix-length*)
382     (setf (last-command-type) :completion-self-insert))
383    
384    
385     ;;;; Nifty database and parsing machanisms.
386    
387     (defhvar "Completion Database Filename"
388     "The file that \"Save Completions\" and \"Read Completions\" will
389     respectively write and read the completion database to and from."
390     :value nil)
391    
392     (defvar *completion-default-default-database-filename*
393     "hemlock-completions.txt"
394     "The file that will be defaultly written to and read from by \"Save
395     Completions\" and \"Read Completions\".")
396    
397     (defcommand "Save Completions" (p)
398     "Writes the current completion database to a file, defaultly the value of
399     \"Completion Database Filename\". With an argument, prompts for a
400     filename."
401     "Writes the current completion database to a file, defaultly the value of
402     \"Completion Database Filename\". With an argument, prompts for a
403     filename."
404     (let ((filename (or (and (not p) (value completion-database-filename))
405     (prompt-for-file
406     :must-exist nil
407     :default *completion-default-default-database-filename*
408     :prompt "File to write completions to: "))))
409     (with-open-file (s filename
410     :direction :output
411     :if-exists :rename-and-delete
412     :if-does-not-exist :create)
413     (message "Saving completions...")
414     (dotimes (i (length *completions*))
415     (let ((bucket (svref *completions* i)))
416     (when bucket
417     (write i :stream s :base 10 :radix 10)
418     (write-char #\newline s)
419     (dolist (completion bucket)
420     (write-line completion s))
421     (terpri s))))
422     (message "Done."))))
423    
424     (defcommand "Read Completions" (p)
425     "Reads some completions from a file, defaultly the value of \"Completion
426     Database File\". With an argument, prompts for a filename."
427     "Reads some completions from a file, defaultly the value of \"Completion
428     Database File\". With an argument, prompts for a filename."
429     (let ((filename (or (and (not p) (value completion-database-filename))
430     (prompt-for-file
431     :must-exist nil
432     :default *completion-default-default-database-filename*
433     :prompt "File to read completions from: ")))
434     (index nil)
435     (completion nil))
436     (with-open-file (s filename :if-does-not-exist :error)
437     (message "Reading in completions...")
438     (loop
439     (let ((new-completions '()))
440     (unless (setf index (read-preserving-whitespace s nil nil))
441     (return))
442     ;; Zip past the newline that I know is directly after the number.
443     ;; All this to avoid consing. I love it.
444     (read-char s)
445     (loop
446     (setf completion (read-line s))
447     (when (string= completion "") (return))
448     (unless (member completion (svref *completions* index))
449     (push completion new-completions)))
450     (let ((new-bucket (nconc (nreverse new-completions)
451     (svref *completions* index))))
452     (setf (svref *completions* index) new-bucket)
453     (do ((completion new-bucket (cdr completion))
454     (end (1- (value completion-bucket-size)))
455     (i 0 (1+ i)))
456     ((endp completion))
457     (when (= i end) (setf (cdr completion) nil))))))
458     (message "Done."))))
459    
460     (defcommand "Parse Buffer for Completions" (p)
461     "Zips over a buffer slamming everything that is a valid completion word
462     into the completion hashtable."
463     "Zips over a buffer slamming everything that is a valid completion word
464     into the completion hashtable."
465     (declare (ignore p))
466     (let ((buffer (prompt-for-buffer :prompt "Buffer to parse: "
467     :must-exist t
468     :default (current-buffer)
469     :default-string (buffer-name
470     (current-buffer)))))
471     (with-mark ((word-start (buffer-start-mark buffer) :right-inserting)
472     (word-end (buffer-start-mark buffer) :left-inserting)
473     (buffer-end-mark (buffer-start-mark buffer)))
474     (message "Starting parse of ~S..." (buffer-name buffer))
475     (loop
476     (unless (find-attribute word-start :completion-wordchar) (return))
477     (record-completion
478     (region-to-string (region word-start
479     (or (find-attribute
480     (move-mark word-end word-start)
481     :completion-wordchar #'zerop)
482     buffer-end-mark))))
483     (move-mark word-start word-end))
484     (message "Done."))))
485    
486    
487    
488     ;;;; Modeline hackery:
489    
490     (defvar *completion-mode-possibility* "")
491    
492     (defvar *completion-modeline-field* (modeline-field :completion))
493    
494     (defun display-possible-completion (prefix
495     &optional (prefix-length (length prefix)))
496     (let ((old *completion-mode-possibility*))
497     (setq *completion-mode-possibility*
498     (or (find-completion prefix prefix-length) ""))
499     (unless (eq old *completion-mode-possibility*)
500     (update-modeline-field *echo-area-buffer* *echo-area-window*
501     *completion-modeline-field*))))
502    
503     (defun clear-completion-display ()
504     (unless (= (length (the simple-string *completion-mode-possibility*)) 0)
505     (setq *completion-mode-possibility* "")
506     (update-modeline-field *echo-area-buffer* *echo-area-window*
507     *completion-modeline-field*)))
508    
509    
510     ;;; COMPLETION-REDISPLAY-FUN erases any completion displayed in the status line.
511     ;;;
512     (defun completion-redisplay-fun (window)
513     (declare (ignore window))
514     (unless (eq (last-command-type) :completion-self-insert)
515     (clear-completion-display)))
516     ;;;
517     (add-hook redisplay-hook #'completion-redisplay-fun)

  ViewVC Help
Powered by ViewVC 1.1.5