5
$\begingroup$

Edit: As mentioned in the comments below, typing just ResourceFunction["Mo"] results in a list of ResourceFunctions that have a name that starts with Mo. This is good enough for me although it would be convenient if typing just Mo would show both official functions and resource functions (in another color).

I wonder if it is possible to use ToolTip, MouseOver and the likes in a way that, while writing, Mathematica proposes suggestions that incorporates ResourceFunctions rather than the current situation where it only shows official functions. Perhaps the resource functions can be colored in orange as a sort of warning that there is no guarantee that the function will work.

There is already a resource function that searches for other resource functions but it requires the Cloud and I would rather that Mathematica gives suggestions while typing.

Hence, I suppose this question has two parts:

How to get the list of all resource functions ? (Is it allowed to use web scraping to get that ? Maybe it can be extracted from the documentation center instead ?)

How to make a GUI interface that suggests elements in that list (ideally like Mathematica's autocomplete)?

$\endgroup$
8
  • 3
    $\begingroup$ When I type ResourceFunction[" and then start a function name, I get autocomplete suggestions for all resource functions. Does that happen for you? $\endgroup$
    – Jason B.
    Sep 15, 2022 at 18:03
  • $\begingroup$ @JasonB. Hello thank you I did not know that was a feature in some versions. I have Linux 13.1. I will mention my version in my question. $\endgroup$ Sep 15, 2022 at 18:29
  • 2
    $\begingroup$ Do you get a result from PacletObject["ResourceAutoCompletionData"]? $\endgroup$
    – Jason B.
    Sep 15, 2022 at 20:37
  • $\begingroup$ @JasonB. Yes but when I clicked on the Packlet, in the description it says "Missing["NotAvailable"]" $\endgroup$ Sep 15, 2022 at 20:42
  • 1
    $\begingroup$ I think it should be working for you now. You have the paclet, the missing description is just because it is an internal paclet not a user-facing one. When I type ResourceFunction[" and then two letters like Mo then it starts autocompleting for me. If it isn't working for you you may need to report it as a bug. $\endgroup$
    – Jason B.
    Sep 15, 2022 at 22:47

3 Answers 3

4
+100
$\begingroup$

Here's another approach to get the autocompletion working:

$ContextPath = 
  DeleteDuplicates@Append[$ContextPath, "ResourceFunctions`"];

ResourceFunction;
If[Names[#] === {},
    ToExpression["ResourceFunctions`" <> #, StandardForm, 
     Function[s, s := ResourceFunction[#], HoldAll]]
    ] & /@ 
  FunctionResource`Autocomplete`Private`$resourceFunctionNames;

SetOptions[$FrontEndSession,
 FixedPoint[
  Normal,
  Merge[Merge[Replace[{{a__Association} :> <|a|>, {___, l_} :> l}]]]@{
    GeneralUtilities`ToAssociations@
     Options[$FrontEndSession, AutoStyleOptions],
    <|AutoStyleOptions -> <|"SymbolContextStyles" -> \
<|"ResourceFunctions`" -> RGBColor[0.89, 0.37, 0.13]|>|>|>
    }
  ]
 ]

Effectively, this does the following:

  1. Add the context ResourceFunctions` to the $ContextPath
  2. Go through the list of resource functions (FunctionResource`Autocomplete`Private`$resourceFunctionNames), and if the symbol doesn't already exist anywhere on the $ContextPath, add a new definition of the form ResourceFunctions`function = ResourceFunction["function"]
  3. Set the AutoStyleOptions for the ResourceFunctions` context to color the symbols orange

enter image description here

$\endgroup$
4
  • $\begingroup$ Is there an easy way to revert back to the usual autocomplete behavior after using the code you provided ? It works quite well in combination with this answer mathematica.stackexchange.com/a/235055/86543 as it allows searching for resource functions using fuzzy search without having to open a browser. But, the code in this answer creates shadowing problems with hidden internal mathematica functions at times. Hence It would be nice if I could remove it when there are shadowing problems. $\endgroup$ Dec 4, 2022 at 14:20
  • $\begingroup$ I am aware of the existence of the resource function that searches other resource functions but It requires wolfram cloud. I could also maybe set up a function using the code above for searching but I like the fuzzy search with autocomplete. $\endgroup$ Dec 4, 2022 at 14:25
  • 1
    $\begingroup$ @userrandrand Can you give a concrete example of where this causes shadowing warnings? I had tried to add a (very basic) safeguard against this by checking if a symbol with that name already exists, so I assume you are loading other packages afterwards? That being said, you should be able to remove the modifications by doing Remove["ResourceFunctions`<name>"], where <name> is the name of the problematic resource function, or * if you want to remove all functions. If you want to fully reset everything, you can also remove the context from $ContextPath, but that's not strictly needed. $\endgroup$
    – Lukas Lang
    Dec 4, 2022 at 21:41
  • $\begingroup$ Thank you very much. That explains why I usually do not see any shadowing errors. I think one of the system/official mathematica functions I used called a native package and that created the shadowing problem. I do not remember which function but if it happens again I will try to give a precise scenario where it happens. $\endgroup$ Dec 4, 2022 at 22:31
3
$\begingroup$

As mentioned in the comments below, typing just ResourceFunction["Mo"] results in a list of ResourceFunctions that have a name that starts with Mo. This is good enough for me although it would be convenient if typing just Mo would show both official functions and resource functions (in another color).

You can install any ResourceFunction persistently so that it can be used like a built-in function using ResourceFunction["PersistResourceFunction"]. For example, the following installs ResourceFunction["ShortInputForm"] persistently:

ResourceFunction["PersistResourceFunction"]["ShortInputForm"]

output

Now typing "Sh" in an input cell opens the autocompletion menu:

screenshot

$\endgroup$
1
  • $\begingroup$ Very nice ! That's quite convenient. $\endgroup$ Sep 16, 2022 at 12:09
2
$\begingroup$

The answer by Alexey and Jason works. This resource function ResourceFunctionInput also provides autocompletion but with a different design.

Example :

I opted for the default installation.

In that case type esc rfi esc as in the gif below then type directly the name of the resource function.

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Thanks for improving you answer (+1) $\endgroup$
    – rhermans
    Nov 22, 2022 at 13:54

Your Answer

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

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