3

I'm making some functions to interact with Mercurial. Many Mercurial commands take a repository as an argument, and that argument can be specified either by a path to the root of the repository or by a repo name. These repo names can be configured in the user's .hgrc file, and it is easy to get a list of the names that are defined.

I'd like to make a completing read function that dynamically completes a path to a directory or an item from a list. I'd like it to work just like ido-read-directory-name, but with the items from a list also offered as options.

I've been digging through the documentation, and also through mini_buffer.el and ido.el. The only hint I have so far is that I can override the variable read-file-name-function, but it seems like I'd end up reimplementing a lot of functionality for dynamically completing directory names.

Does anyone have any guidance for me? Or perhaps an example?

4

You can try something like:

(let* ((dir-table (apply-partially #'completion-table-with-predicate
                                 #'completion-file-name-table
                                 #'file-directory-p
                                 'strict))
       (my-table
        (completion-table-in-turn <reponamesfromhg> dir-table)))
  (completing-read "Foo:" my-table))
  • Very cool, thanks! How can I make this only complete directories? And how can I get this to work with ido? – Daniel Matz Mar 30 '16 at 14:35
  • 1
    See updated code to constrain the completion to directories. As for IDO, I do not know why it wouldn't work with it, but it might be difficult to fix it (IOW you might be better off trying to get the standard completion to look more like IDO, e.g. by enabling icomplete-mode). – Stefan Mar 30 '16 at 14:58
  • Should it be apply-partially? – Daniel Matz Mar 30 '16 at 15:02
  • Thanks for getting me this far, Stefan! I use ido-vertical-mode, so unfortunately icomplete-mode isn't quite what I want. I would still love for this to work basically the same as ido-read-directory-name, if anyone else has any ideas. – Daniel Matz Mar 30 '16 at 15:37
  • There's no good reason why you couldn't have an icomplete-vertical-mode. – Stefan Mar 31 '16 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.