0

In order to remind myself of the arguments a function takes and their ordering I find it useful to use the shortcut to show the template (Ctrl+Shift+k). However, I don't write usage prompts for my functions (maybe I should but I find naming the functions and arguments clearly is enough) and so I can't do this for these functions. Therefore I would like to auto generate and run usage messages containing only the function name and the name of the parameters. I.e. for all my user defined functions such as

func[descriptiveArgument1_,descriptiveArgument2_]:= ...

I want to create and run (preferably without actually creating corresponding individual cells)

func::usage = "func[descriptiveArgument1,descriptiveArgument2]";

so that I can see the names descriptiveArgument1 and descriptiveArgument2 in a template with Ctrl+Shift+k.

Kvothe
  • 4,419
  • 9
  • 28
  • In general, this will be very difficult to do as soon as you're using any fancier pattern matching constructs. Having said that, you could take a look at ResourceFunction["InformationPopup"] to quickly access function definitions. Finally, this is probably a duplicate of https://mathematica.stackexchange.com/questions/155184/how-can-i-automatically-generate-usage-messages?rq=1 – Lukas Lang Apr 13 '23 at 14:04
  • Thanks, I will look at those! I am not sure it is a duplicate since that question seems to focus on automatically writing a nice usage message for the purpose of having a nice usage message. I don't care at all about the usage message. It only needs to contain func[arg1,arg2]... – Kvothe Apr 13 '23 at 14:24
  • About ResourceFunction["InformationPopup"] do you happen to know where the shortcut Alt+F1 is defined? It does not seem to be in KeyEventTranslations I guess because it comes from an additional package. I need to change it since Alt+F1 is already a system wide shortcut for me. – Kvothe Apr 13 '23 at 14:39
  • Regarding the potential duplicate: Couldn't you simply remove all the "nice usage message" parts from the other answer to be left with what you need? Regarding InformationPopup: See the last example in the documentation for how to install it with a different shortcut. It will be something like ResourceFunction["InformationPopup"]["AddMenuItem", System`MenuKey["X", System`Modifiers -> {"Command"}]] – Lukas Lang Apr 13 '23 at 14:48
  • Thanks! I see the example in question. When I copy that command and run it, or if I change the letter so that it isn't something that already has a keybinding, either way no menu item is added and I still only have the old Alt+F1 one. Should I be deleting that shortcut first or something?

    About the answer to the other question. Yes I tried whether I could get that to work. It seems it was incomplete and needs the function getCodeValues from this answer: https://mathematica.stackexchange.com/a/164008/45020. It produces the right text which is step 1 towards the answer to my question.

    – Kvothe Apr 13 '23 at 14:59
  • You'll have to restart Mathematica to remove the old menu item, only then will the new one be added. Also note that you can use "InstallMenuItem" instead of "AddMenuItem" to make the addition permanent (until you "Remove" it at least). – Lukas Lang Apr 13 '23 at 15:03
  • 2
  • @Rohit, It was suggested above and I already commented on it. I think it contains an important part of the answer. A small part is left in applying it to all user defined functions. Also note that that answer has incomplete code. I already commented both there and here how to complete it. (I will probably adapt that solution to my problem and answer myself tomorrow.) – Kvothe Apr 13 '23 at 23:52

0 Answers0