Function Repository Resource:

SetIdleEventHandler

Source Notebook

Create an event handler for a cell that evaluates whenever editing has stopped

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["SetIdleEventHandler"][cell,code]

evaluates code whenever editing stops in the given cell.

ResourceFunction["SetIdleEventHandler"][cell,{active,idle}]

evaluates active whenever edits are made and evaluates idle a short time after editing has stopped.

ResourceFunction["SetIdleEventHandler"][cell,code,delay]

waits delay seconds after editing to consider the cell idle.

Details and Options

The value for cell can be either a Cell expression or a CellObject.
The default delay is one second.
ResourceFunction["SetIdleEventHandler"][code] is equivalent to ResourceFunction["SetIdleEventHandler"][EvaluationCell[],code].
Notebook history tracking and dynamic updating must be enabled in order for ResourceFunction["SetIdleEventHandler"] to work.
ResourceFunction["SetIdleEventHandler"] uses CellDynamicExpression to track changes.

Examples

Basic Examples (4) 

Evaluate code when typing stops in a cell:

In[1]:=
ResourceFunction["SetIdleEventHandler"][
  Cell["Edit this text", "Text"], SetOptions[EvaluationCell[], Background -> RandomColor[]]] // CellPrint

Edit this text and see the color change

In[2]:=
ResourceFunction["SetIdleEventHandler"][
  Cell["Edit this text", "Text"], AttachCell[EvaluationCell[], Style[Framed["typing stopped", Background -> Pink], "Text"], "CellBracket", RemovalConditions -> {"EvaluatorQuit", "ParentChanged"}]] // CellPrint

Edit this text


Set a value while editing is active:

In[3]:=
Dynamic[active]
ResourceFunction["SetIdleEventHandler"][
  Cell["Edit this text", "Text"], {active = True, active = False}] // CellPrint
Out[3]=

Edit this text


Count edits made to a cell:

In[4]:=
Grid[{{"Edit counter:", Dynamic[edits]}, {"Idle counter:", Dynamic[stops]}}]
ResourceFunction["SetIdleEventHandler"][
  Cell["Edit this text", "Text", Initialization :> (edits = stops = 0)], {edits++, stops++}] // CellPrint
Out[4]=

Edit this text and watch the counter values change


Show an icon while a cell is being modified:

In[5]:=
ResourceFunction["SetIdleEventHandler"][
  Cell["Edit this text", "Text"], {NotebookDelete[attached]; attached = AttachCell[EvaluationCell[], Style["\[WatchIcon]", Orange], Left, 0, Right], NotebookDelete[attached]}] // CellPrint

Edit this text

Scope (2) 

Create an input cell that automatically evaluates five seconds after typing stops:

In[6]:=
ResourceFunction["SetIdleEventHandler"][
  Cell[BoxData[""], "Input"],
  SelectionMove[EvaluationCell[], All, Cell];
  SelectionEvaluateCreateCell[ParentNotebook[EvaluationCell[]]],
  5
  ] // CellPrint
In[7]:=
1 + 1
Out[7]=

Use on a CellObject:

In[8]:=
cell = EvaluationCell[]
Out[8]=
In[9]:=
With[{cell = cell}, ResourceFunction["SetIdleEventHandler"][cell, content = RawBoxes[NotebookRead[cell]]]]
In[10]:=
Framed[Dynamic[content]]
Out[10]=

Applications (1) 

Create a notebook that automatically analyzes code when and input cell becomes idle:

In[11]:=
stylesheet = Notebook[{
    Cell[StyleData[StyleDefinitions -> "Default.nb"]],
    ResourceFunction["SetIdleEventHandler"][Cell[StyleData["Input"]], Needs["CodeInspector`" -> None]; CodeInspector`AttachAnalysis[{EvaluationCell[]}]]
    },
   StyleDefinitions -> "PrivateStylesheetFormatting.nb"
   ];
In[12]:=
NotebookPut@Notebook[{}, StyleDefinitions -> stylesheet]
Out[12]=

Possible Issues (2) 

SetIdleEventHandler uses CellDynamicExpression to track changes and will overwrite the existing setting if it exists:

In[13]:=
ResourceFunction["SetIdleEventHandler"][
  Cell["test", "Text", CellDynamicExpression -> Dynamic[expr]], code] // InputForm
Out[13]=

SetIdleEventHandler will not work if notebook history tracking is disabled:

In[14]:=
ResourceFunction["SetIdleEventHandler"][
  Cell["Edit this text", "Text", TrackCellChangeTimes -> False], Print["evaluated"]] // CellPrint

Edit this text

Version History

  • 1.0.0 – 28 June 2022

Related Resources

License Information