From: Xah Lee
Subject: implementing language keyword completion with ido/icicles
Date: 
Message-ID: <6a181a6a-f0d4-4d08-a6ee-bac5f46fce84@x29g2000prf.googlegroups.com>
recently i implemented a keyword completion feature in xlsl-mode for
lindent scripting lang. (see code below)

however, the conventional emacs completion feature provide the
completion of a given starting string.

I know that ido-mode and icicles provide completion, where the given
string needs not to be the starting string but can be any substring or
even regex.

My question is, can anyone show me a simple example?

Thanks.

Here's my implementation of the traditional completion:

;; this is your lang's keywords
(setq xyz-kwdList '
      ("touch"
       "touch_start"
       "touch_end"
       "for"
       "foreach"
       "forall"
       ))

The following is the code that does the completion.

(defun xyz-complete-symbol ()
  "Perform completion on word under cursor."
  (interactive)
  (let* (
         (cusorPoint (point))
         (meat (thing-at-point 'symbol))
         (maxMatchResult (try-completion meat xyz-kwdList))
         )
    (when meat
      (cond ((eq maxMatchResult t))
            ((null maxMatchResult)
             (message "Can't find completion for “%s”" meat)
             (ding))
            ((not (string= meat maxMatchResult))
             (delete-region (- cusorPoint (length meat)) cusorPoint)
             (insert maxMatchResult))
            (t (message "Making completion list...")
               (with-output-to-temp-buffer "*Completions*"
                 (display-completion-list
                  (all-completions meat xyz-kwdList)
                  meat))
               (message "Making completion list...%s" "done"))))
    ))

it actually took me quite some days to finally figure this out (on and
off in the past months).

• How To Implement Keyword Completion in Emacs
  http://xahlee.org/emacs/elisp_keyword_completion.html

i've been wondering, shouldn't emacs provide something more high
level, so that something like:

(keyword-completion-feature inputString keywordList)

and the function takes care of all the display, buffer, etc by itself?

  Xah
∑ http://xahlee.org/

☄