29

Recent versions of Mathematica dynamically prompt a list of possible options. For example, when typing

Plot[x, {x,0,1}, PlotStyle->

After typing the arrow, Mathematica prompt a list Gray, Dashed, Thick for choice. How to define similar behavior for user defined functions? For example,

Options[myFun] = {myOpt -> value};
myFun[OptionsPattern[]] := 1;

How to prompt a set of possible options, like opt1, opt2, opt3 right after typing

myFun[myOpt->
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Yi Wang
  • 7,347
  • 4
  • 28
  • 38

1 Answers1

27

Thanks for asking this question; I might not have discovered this customizable area without it.

The auto-completion option values are automatically loaded on Front End start from:

FileNameJoin[{$InstallationDirectory, "SystemFiles", "FrontEnd",
  "SystemResources", "FunctionalFrequency", "OptionValues"}]

This directory contains a series of Package (.m) files each with the name of a function, e.g. ArrayPlot.m. The file name itself determines that this will apply to the function ArrayPlot. It also contains an additional file that appears to apply to all functions: CommonOptions.m

The structure of each file is a list of Rules:

{Extension -> {"Automatic"}, Trig -> {"True", "False"}}
  • The right hand side of each rule is always a list of Strings or an empty list {}
  • Option values that are actual strings must be escaped: "\"string\""
  • Option names must be Symbols (thanks Kuba)

A special Rule form exists for icons within the menu, e.g. from ArrayPlot.m:

PlotTheme ->
 {{"\"Web\"", "PlotTheme-ArrayPlot-Web.png"},
  {"\"Minimal\"", "PlotTheme-ArrayPlot-Minimal.png"},
  {"\"Detailed\"", "PlotTheme-ArrayPlot-Detailed.png"},
  {"\"Business\"", "PlotTheme-ArrayPlot-Business.png"},
  {"\"Marketing\"", "PlotTheme-ArrayPlot-Marketing.png"},
  {"\"Scientific\"", "PlotTheme-ArrayPlot-Scientific.png"},
  {"\"Monochrome\"", "PlotTheme-ArrayPlot-Monochrome.png"},
  {"\"Classic\"", "PlotTheme-ArrayPlot-Classic.png"},
  {"\"Default\"", "PlotTheme-ArrayPlot-Default.png"}}

Each of these icon files reside in:

FileNameJoin @ {$InstallationDirectory, "SystemFiles", "FrontEnd", 
  "SystemResources", "Bitmaps", "Popups", "CodeCompletion", "Thumbnails"}

I have not checked to see if other paths are also searched.

As an example I created a new file foo.m in the OptionValues directory with the content:

{myOption -> {"Doc", "Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey"}}

Now after (re)starting Mathematica:

enter image description here

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • @Kuba Sorry! I guess in this case a stub answer might have been helpful but I was discouraged from that practice. I also tried $UserBaseDirectory without success. Thanks for the note regarding option names. – Mr.Wizard May 06 '15 at 07:42
  • Don't be, happens :) I'm not after those temporary placeholders either. – Kuba May 06 '15 at 08:50
  • 1
    Smashing, I played a bit more with auto-completion feature and I noticed that when you define an enumerated list of options, the way you described above, drop-down list is filtered automatically as you start typing the name of the option, e.g. myOption->Sl.... and drop-down list displays only "Sleepy".

    Two things here, if you want string options then the list becomes {myOption -> { ""Doc"", ""Grumpy"", etc.... and if you want the drop-down list filtering then when you type myOption-> ..... do not start with the quotes, but simply type letters of your option and press tab or enter.

    – Athanassios Dec 15 '15 at 15:38
  • 1
    Unfortunately, this seems changed in version 11. I'm able to successfully replicate your steps in M10.4 but not in M11. Is there any known updates to do that? thanks – bobknight Aug 13 '16 at 06:21
  • 4
    Ok, found the change: the path has been changed to FileNameJoin[{$InstallationDirectory, "SystemFiles", "Components", "AutoCompletionData", "Main", "OptionValues"}] – bobknight Aug 13 '16 at 07:15
  • I have another question, hoping someone else has already had the same need. Normally it is not recommended to change Mathematica's own folders, so I would add my own folder with options values to PrivatePaths. I have seen the suboption "AutoCompletionDataBase" and modified it adding another folder, but it doesn't work. Is there any why to say to Mathematica to look at a different position for options values? – bobknight Aug 13 '16 at 08:22
  • @bobknight I am still using version 10.1 so I cannot help with v11 issues. I don't know if any solution I find for v10.1 will help you but I'll still look. – Mr.Wizard Aug 13 '16 at 13:30
  • Do you know of any ways to integrate this into packages, without modifying Mathematica itself? Argument completion is more or less solved :-) Now I want option completion. I suspect that FE`OVC (OptionValueCompletion) may be of use, but I can't get it to do anything but throw front end errors. – Szabolcs Oct 29 '16 at 16:06
  • @Szabolcs No I do not. It may relate to a longstanding (though only recently posted) question of mine: http://mathematica.stackexchange.com/q/121059/121 -- should you find and anther to that one (too) please let me know! – Mr.Wizard Oct 29 '16 at 17:42
  • @Szabolcs I've been messing with those FE` functions and one thing worth noting is that they might well need to be invoked by the code-assist popup menu. Consider this: FE`FT[1, "Plot"]. It spits out the boxes you'd expect to see in that popup after typing Plot[ and pressing the down-guillemet, but forces them through a NotebookWrite-like mechanism. That calls FrontEnd`TemplateTooltipPacket to create those boxes, by the way. – b3m2a1 Aug 18 '17 at 03:56
  • @bobknight do you still need help with getting it to work without modifying $InstallationDirectory? Just use the "AutoCompletionData" extension in the [`"PacletInfo.m"](https://mathematica.stackexchange.com/questions/132064/pacletinfo-m-documentation-project) – b3m2a1 Aug 18 '17 at 04:06
  • @b3m2a1 thank you for that, I'll take a look to it as soon as I will be back to this issue. – bobknight Aug 18 '17 at 07:30
  • @bobknight alternatively I think you can just stick your directory on CurrentValue[$FrontEndSession, {PrivatePaths, "AutoCompletionData"}] – b3m2a1 Aug 18 '17 at 07:33
  • Hi @b3m2a1 can you explain step by step how to make this work for packages without modifying Mathematica installation? – QuantumDot Aug 27 '22 at 16:57