12
$\begingroup$

I would like for the autocomplete feature to search through contexts, for example if I have a symbol named A`B`C`MyFunction, 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
...
$\endgroup$
1
  • $\begingroup$ I assume you mean cmd-k, since cmd-shift-k inserts a template. $\endgroup$ Commented Jun 15, 2012 at 2:06

2 Answers 2

17
$\begingroup$

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:

enter image description here

enter image description here

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}
$\endgroup$
2
  • $\begingroup$ wow, I have somehow missed autocomplete for the last 5 years.... thanks for this! $\endgroup$
    – tkott
    Commented Jun 15, 2012 at 15:25
  • $\begingroup$ Exactly! I knew I wasn't crazy. $\endgroup$
    – M.R.
    Commented Jun 15, 2012 at 19:52
3
$\begingroup$

One option is to put the context on the path:

$ContextPath = AppendTo[$ContextPath, "A`B`C`"]
$\endgroup$
4
  • $\begingroup$ 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"... $\endgroup$
    – M.R.
    Commented Jun 14, 2012 at 22:58
  • $\begingroup$ @Mike, expanding MyF+Shift+k works just fine. If you want something else, I suggest to state that more clearly in your question. $\endgroup$
    – user21
    Commented Jun 14, 2012 at 23:00
  • $\begingroup$ @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. $\endgroup$
    – user21
    Commented Jun 14, 2012 at 23:07
  • $\begingroup$ Sorry for the confusion! I just updated the question a bit. $\endgroup$
    – M.R.
    Commented Jun 14, 2012 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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