Contextual Commands

We've discussed how to create "global" commands (which are always enabled) as well as application-specific commands (which are enabled only when a particular application is active). In addition, you can create contextual commands, which are enabled only when a particular window has the focus.

By specifying all or part of the window title in a context statement you can create commands specific to that window. For example, here are three groups of commands for Microsoft Outlook (from the application-specific command file outlook.vcl):

Microsoft Outlook:
  New Message = {Ctrl+n};
  Reply to Message = {Ctrl+r};
  Reply to All = {Ctrl+Shift+r};
  Forward Message = {Ctrl+f};

Message (Plain Text):
  Send That = {Alt+s};
  Reply Here = "{Home}{Shift+End}{Del}{Enter 3}{Left 2}";

Calendar:
  View Month = {Alt+m};
  View Work Week = {Alt+r};

The context statement Microsoft Outlook: specifies that subsequent commands should be active only when "Microsoft Outlook" is part of the title of the active window. When reading mail using Outlook the main window title is "Inbox - Microsoft Outlook", so the first group of commands above (to create, reply to, or forward a message) will be enabled.

Similarly, when editing a mail message the window title is "Message (Plain Text)", so the second group of commands above will be enabled. Finally, the third group of commands above will be enabled when the Outlook calendar is displayed.

Here is another example. The text editor application Emacs can be customized so that the window title includes the name of the currently-edited file. For example, when editing iexplore.vcl (the file of Vocola commands for Internet Explorer) the Emacs window title is Emacs - iexplore.vcl - 01.05.02. This allows defining a set of contextual commands for editing Vocola files. The application-specific command file emacs.vcl would include:

### Editing Vocola files
.vcl:
  Insert             <key> = {$1       Wait(0) }; 
  Insert       <mod> <key> = {$1+$2    Wait(0) }; 
  Insert <mod> <mod> <key> = {$1+$2+$3 Wait(0) }; 

### Editing Perl files
.pl:
  New Statement = {Ctrl+e}{Ctrl+j};
  New Statement Above = {Ctrl+a}{Ctrl+b}{Ctrl+j}; 
  Insert Hash Reference = ->{}{Ctrl+b};

So for example when the Emacs window title contains .vcl, saying "Insert Shift Tab" inserts the text {Shift+Tab} into the file. These commands allow speaking keystroke sequences when writing Vocola code. (The variable definitions for <mod> and <key> are omitted here, but available with the Vocola distribution.)

Similarly, the second group of commands above is active only when editing Perl files.

The context command ":" can be used to specify that subsequent commands are not restricted to particular window titles.

Global Contextual Commands and Multiple Context Choices

Contextual commands don't need to be application-specific. You can also create global contextual commands, as in this example from the global command file _vocola.vcl:

Open:
  Go Up = ..{Enter};
  Folder List = {Shift+Tab_2}{Down}{PgUp};

This defines two commands for use in a "File Open" dialog box, regardless of the current application. Whenever the window title includes the word Open, you can say "Go Up" to move to the parent folder or "Folder List" to open the folder hierarchy drop down at the top of the dialog.

As it happens, "File Chooser" dialog boxes appear with a wide range of titles, such as "Open File", "Save As", "Browse for Folder", etc. Ideally we want the same set of context-specific commands to be available for all such dialogs. Vocola supports this by allowing the | character in context statements. Here is the full set of context titles and commands I use:

Open | New | Save | File | Attachment | Browse | Directory:
  Folder <folder> = {Ctrl+c}$1\{Enter};
  Go Up = ..{Enter};
  Go Up <n> = Repeat($1, ..\) {Enter};
  Folder List = {Shift+Tab_2}{Down}{PgUp};
  Choose <n> = {Down_$1}{Enter}{Esc};
 
Copyright © 2002-2023 Rick Mohr