29
$\begingroup$

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->
$\endgroup$

1 Answer 1

27
$\begingroup$

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

$\endgroup$
14
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Commented May 6, 2015 at 7:42
  • $\begingroup$ Don't be, happens :) I'm not after those temporary placeholders either. $\endgroup$
    – Kuba
    Commented May 6, 2015 at 8:50
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Dec 15, 2015 at 15:38
  • 1
    $\begingroup$ 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 $\endgroup$
    – bobknight
    Commented Aug 13, 2016 at 6:21
  • 4
    $\begingroup$ Ok, found the change: the path has been changed to FileNameJoin[{$InstallationDirectory, "SystemFiles", "Components", "AutoCompletionData", "Main", "OptionValues"}] $\endgroup$
    – bobknight
    Commented Aug 13, 2016 at 7:15

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.