2
$\begingroup$

When developing packages I need to Get my packages repeatedly, and it will be convenient to set a shortcut to reload the packages.

Suppose the structure of the notebook is like:

First cell

PacletDataRebuild[]
Get["Notation`"]
MemoryInUse[]

Lots of cells in several pages

Print/@Range@20

The cell I'm working with

foo[1]

Other cells

foo1[1]

What I want

I want to evaluate the first cell using a shortcut, without changing the current selection of the notebook. Is there convenient way to achieve this?

What I tried

I tried two methods by modifying KeyEventTranslations.tr but failed

Method 1

    Item[KeyEvent["t", Modifiers -> {Command}],
        FrontEndExecute[
            FrontEnd`SelectionMove[EvaluationNotebook[],Before,Notebook]
        ];
        FrontEndExecute[
            FrontEnd`SelectionMove[EvaluationNotebook[],Next,Cell]
        ];
        FrontEndExecute[
            FrontEnd`SelectionEvaluate[EvaluationNotebook[]]
        ]
    ]

Firstly these codes just don't work, and secondly they change the position of current selection.

Method 2

    Item[KeyEvent["t", Modifiers -> {Command}],
        KernelExecute[
            Get["Notation`"]
        ]
    ]

I found the undocumented function KernelExecute in other questions, but don't know how to use it.

$\endgroup$
2
  • $\begingroup$ You can make the top cells an initialization cell and then use Evaluation -> Evaluate Initialization Cells. $\endgroup$ Commented Sep 26, 2022 at 13:17
  • $\begingroup$ @SjoerdSmit Thx! but I use code as the default input cell style, and the code cells are initialization cells. $\endgroup$
    – Lacia
    Commented Sep 26, 2022 at 13:24

2 Answers 2

1
$\begingroup$

How about a DockedCell with a Button?

SetOptions[InputNotebook[], DockedCells -> Cell[BoxData[ToBoxes[
     Button["<<Notation`", (<< "Notation`";)]]]
   ]]
$\endgroup$
2
  • $\begingroup$ Thx! It's more flexible than modifying MenuSetup.tr. Is there a shortcut to evaluate the DockedCell ? $\endgroup$
    – Lacia
    Commented Sep 27, 2022 at 3:56
  • $\begingroup$ @lilyric Not that I know of, but I'm far from an expert on such tricks. $\endgroup$
    – Chris K
    Commented Sep 27, 2022 at 4:37
2
$\begingroup$

After trial and error I find the following method:

    Menu["Others", 
    {
        MenuItem["Reload xx`",
            FrontEnd`KernelExecute[
                Get["xx.wl",Path->FileNameJoin[{$UserBaseDirectory,"Applications"}]]//Timing//First//Print;
            ],
            MenuKey["t", Modifiers -> {Command}],
            MenuEvaluator -> Automatic, Method -> "Queued"
        ],
    }]

add these lines into MenuSetup.tr. The context FrontEnd` and the options MenuEvaluator -> Automatic, Method -> "Queued" are necessary.

It has one drawback left. After reloading the package xx.wl, the symbols in xx.wl will not be auto-completed unless evaluating something.

$\endgroup$
2
  • 1
    $\begingroup$ BTW, you can write FrontEnd`AddMenuCommands to init.m instead of modifying front-end files. $\endgroup$
    – rnotlnglgq
    Commented Sep 29, 2022 at 12:22
  • $\begingroup$ @rnotlnglgq Thx! I'll try it later. $\endgroup$
    – Lacia
    Commented Sep 29, 2022 at 15:36

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.