How can I overload autocompletion to work with full contexts?

I would like for the autocomplete feature to search through contexts, for example if I have a symbol named ABCMyFunction, when I type A and press "cmd + shift + k" it will complete it.

Edit

To be clear, I don't want to have to type the path because it's usually very long, and I don't want to have to type the function name again, even if the path itself gets auto completed. I want the following:

If I have these functions:

````Very`Long`Context`For`My`Function1
Very`Long`Context`For`My`Function2
...
````

I want to be able to type Very` and then press CMD+Shift+k, to get a dropdown menu saying exactly

````Very`Long`Context`For`My`Function1
Very`Long`Context`For`My`Function2
...
````
-

I assume you mean `cmd-k`, since `cmd-shift-k` inserts a template. – Brett Champion Jun 15 '12 at 2:06

2 Answers

This is obsolete in Mathematica 9, which automatically includes contexts in completions.

Undocumented function: use at your own risk, subject to change in future versions, etc....

The function you're interested in is `FE`FC`.

It's been around for a while (here's a Mathematica Journal article that references it, near the end) although it has changed argument structure at least once that I'm aware of.

Anyway, here's the code I currently use to a similar end as what Mike would like. (Most of this is boilerplate from the original definition; the main difference is the use of a new function `FE`names`.)

````(* Nice little hack to have command completion (cmd-k) include contexts *)
Unprotect[FE`FC];
ClearAll[FE`FC]

FE`FC[FE`nameString_, FE`ignoreCase_:False] /; $Notebooks:=
    MathLink`CallFrontEnd[FrontEnd`CompletionsListPacket[
        FE`names[FE`nameString<>"*"], FE`ignoreCase],
        FE`NoResult]

FE`names[FE`str_, FE`ignoreCase_:False] :=
    Join[FE`shortContexts[FE`str], Names[FE`str, IgnoreCase -> FE`ignoreCase]];

FE`shortContexts[FE`patt_]:=
 With[{FE`brettclen = Length[StringSplit[FE`patt, "`"]]},
  Union[StringJoin[
      Riffle[Take[#, Min[FE`brettclen, Length[#]]], "`", {2, -1, 2}]] & /@
    StringSplit[Contexts[FE`patt], "`"]]
  ]

Protect[FE`FC];
````

The end result is that when I use command completion, I get contexts that match in addition to symbols. This isn't quite the same as Mike's request, since it gives the contexts one at a time:

since otherwise the list can get a bit overwhelming. For example, if you typed `Int` and then tried to complete to `IntegerPart`, there's a factor of ten difference:

````In[5]:= {Length[Names["Int*"]] + Length[Contexts["Int*"]], 
 Length[Names["Int*`*"]]}

Out[5]= {41, 419}
````
-

wow, I have somehow missed autocomplete for the last 5 years.... thanks for this! – tkott Jun 15 '12 at 15:25
Exactly! I knew I wasn't crazy. – M.R. Jun 15 '12 at 19:52

One option is to put the context on the path:

````$ContextPath = AppendTo[$ContextPath, "A`B`C`"]
````
-

I don't think this works. After appending to the \$ContextPath, if you type "CMD+Shift+k", after typing "A", you don't get a list with ABC(functions) as items to complete, as you would if you typed "MapThre"... – M.R. Jun 14 '12 at 22:58
@Mike, expanding MyF+Shift+k works just fine. If you want something else, I suggest to state that more clearly in your question. – ruebenko Jun 14 '12 at 23:00
@Mike, just that we are on the same page: adding ABCMyFunction ABCMyFunction2 give a list of items. Maybe I don't get what you want. – ruebenko Jun 14 '12 at 23:07
Sorry for the confusion! I just updated the question a bit. – M.R. Jun 14 '12 at 23:10
lang-mma
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 7, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.8830065131187439, "perplexity_flag": "middle"}