To borrow a phrase from Mr. Jinks[i], “I hate meeses to pieces”. Before you call the SPCA, let me be clear that I’m talking about computer mice and not Mus musculus, the common household mouse. Further, I should narrow my context to that of programming because in many other areas of computing the mouse is indispensable.

In programming, however, the mouse can be one of the greatest productivity sinkholes. Time is wasted each time you lift your hand from the keyboard to grab the mouse, and more time is wasted when you move your hand back to home row in preparation for more typing.

So what’s the solution? Keep your hands on the keyboard! To do this use the SlickEdit® command line and bind commonly used commands to keys.

Launching Operations
SlickEdit provides three ways to launch operations: key bindings, commands, and menus. OK, there are actually four, if you include icons. Since the focus of this article is how to keep your hands on the keyboard, I was going to skip that one.

Key Bindings
Key bindings are the fastest way to launch an operation. They can be pressed at any time while editing. A key binding associates a key sequence with a command. A key sequence is a series of key strokes. Anything you do frequently should be bound to an easily remembered key sequence.

For example the key sequence Ctrl+A means hold down the Control key, typically labeled “Ctrl” on most keyboards, and press the A key. The letter A is represented as an uppercase letter, but this does not mean that you are to press the Shift key. If you are supposed to press the Shift key, the sequence will be written as Ctrl+Shift+A.  We use the uppercase A to match the letter on the keyboard.

Commands
Commands are useful for less frequently used operations or operations that take arguments. They are executed from the SlickEdit command line and are nearly as fast as key bindings. Press the Escape key (in most emulations) to activate the command line, which is displayed at the bottom of the SlickEdit application window. 

image001.jpg 

  Figure 1, SlickEdit with Command Line

It offers a full command history and completions for command names and arguments. Use the arrow keys or the mouse to scroll through previous commands. As you type, a list of possible completions is displayed. Use the arrow keys or mouse to select a completion. SlickEdit stores recorded macros as commands. You can also use Slick-C to write new commands. Use the _command keyword to identify a macro as a command. Any command can be bound to a key sequence for faster launching. 

Menus
Menus are accessed via the mouse or a keyboard shortcut. Menus are great for infrequently used items that may not be worth a key binding. They are also helpful to learn about new capabilities. But if you use an operation frequently, you will be more productive by binding it to a key sequence or launching it from the command line. Many menus list a keyboard shortcut to launch the operation. This is just the key binding for the associated command. So when you use a keyboard shortcut, you are really just using a key binding.

You can also operate the main menu through hotkeys. Enable the use of hotkeys by selecting Tools > Options > General, selecting the General tab, and putting a check in “Alt menu hotkeys”. This will be selected by default in many emulations. Once enabled, pressing the Alt key will display an underscore beneath each menu option, indicating the letter to press to activate that option. This is not as fast as key bindings, but it does keep your hands on the keyboard and is much faster than using the mouse. In some cases the menu hot key sequence is a good mnemonic for the command. For example, Alt+P, E (to bring up Project > Properties) may be easier to remember and type than binding project-edit to Ctrl plus some function key. 

Icons
SlickEdit presents icons in a series of toolbars related to different tasks. Toolbars can be customized with an extensive set of additional icons. Icons are useful for die-hard mouse users or for setting up infrequently used operations for which you have a hard time remembering the associated command or key binding. 

The Golden Triangle
The three methods (excluding icons) to launch an operation are related, forming the Golden Triangle of operation launching. If you know how to execute an operation with one of these, you can easily configure SlickEdit to launch the operation using the other two methods. Commands form the base for the other two…which is why it is shown as the apex of the triangle (Damn it, Jim, I’m an engineer, not an artist!). Maybe I should have used a Golden U or something. 
 

image003.jpg

Figure 2 the Golden Triangle; behold its majesty!

From Key Binding to Command
Sometimes you may want to change the key sequence to which an operation is bound. You first need to find the command associated with that key sequence. Use the what-is command from the SlickEdit command line or select Help > What Is Key from the main menu. The command line will be replaced with the prompt, “What is key:” Enter the key sequence in question, like Ctrl+A (hold down the Control key and press A), and SlickEdit displays the associated command. In CUA emulation, SlickEdit will output, “Ctrl+A runs the command select-all”. You can use the same approach to determine the commands associated with mouse events. After you run what-is, click any mouse button and you will be prompted with a list of mouse events. Select one from the list and SlickEdit displays the associated command. For example, if you select mbutton-down in CUA emulation, SlickEdit will output, “MButtonDn runs the command mou-paste”. 

From Command to Key Binding
To find the key binding for a command, use the where-is command or select Tools > Options > Key Bindings from the main menu. The where-is command works just like what-is, except you type in the command name and it outputs the key sequence. The Key Bindings dialog displays a list of the commands. You can scroll through the list or search for a command by typing the command in the text box at the top. The key binding for the selected command is displayed in the dialog.  

image005.png

Figure 3, Key Bindings Dialog   

Modifying Key Bindings
The Key Bindings dialog can also be used to set a key binding. Type the command in the Command field, or select one from the list. Then click the “Add Key or Mouse Click” button. Now type the key sequence you want to use. For example, to bind a command to Ctrl+A, hold down the Control key and press A. To bind a command to a mouse event, click any mouse button and then select the appropriate event from the list. When you are finished, click the Bind button. To exit the dialog, click Done.

From Menu to Command
If the menu entry displays a keyboard shortcut, you can quickly find the associated command using what-is and entering the key sequence. If the menu entry does not display a shortcut, then finding the command for an entry involves a little more work.

SlickEdit provides a convenient UI under Macro > Menus that allows you to view and modify menus in SlickEdit. To find commands for the items on the main menu, select “_mdi_menu” from the list and then click the Open button. You are presented with a menu hierarchy that you can expand to find the menu entry in question. Once selected, information about the entry is displayed including the command executed. You can use this menu to add or change the shortcut for a menu item. Doing so has the same effect as using the Key Bindings dialog to bind the menu entry’s command to a key sequence.   

image007.jpg

Figure 4, Menu Editor

Some menu entries are added dynamically by SlickEdit, so you may have to search the code to find the associated command. One example of this is Build > Java Options, which is only present when a Java project is active. Unfortunately, finding the associated commands for these entries can be very difficult and not easily covered in an article like this.

Context Menus
It is also easy to find the commands in the context menu for the editor window. The context menu, itself, contains an entry to “Edit This Menu”. Selecting this option brings up a dialog that lists the menu entries and associated information, including the command. Use this dialog to modify the context menus. Note that there are two context menus for the editor window: one when a selection is made and one when no selection is made. You can view or change the function that is used for these menus by selecting Tools > Options > File Extension Setup and selecting the Advanced tab.

From Key Binding to Menu and Back
Menu entries map to key bindings through commands, so key bindings are not directly associated with menu entries. To map from a key binding to a menu or vice versa, you need to look up the associated command. That Golden U is looking better all the time!

Customizing Toolbar Icons
The focus of this article has been to help you keep your hands on the keyboard, so icons weren’t included in the Golden Triangle. Besides, then I would have needed a Golden Square or some other form of rhombus, and that just doesn’t sound cool. You may find it helpful to change the set of displayed toolbars, add or remove icons, or change the behavior for an icon. SlickEdit uses the term “toolbar” to refer to both icon bars and tool windows. To change the set of displayed toolbars, select View > Toolbars and check/uncheck one of the listed toolbars.

The list is divided into two groups. The top group is comprised of tool windows, like the Symbol view and the Projects view. The bottom group is made up of icon bars.

Additional options are available on the Toolbar Customization dialog by selecting View > Toolbars > Customize. The Toolbars tab provides a way to control the visibility of the toolbars. The Toolbars tab allows you to set options for which toolbars are visible, whether they are dockable, etc.

The Categories tab displays the complete set of available icons. These can be added to any icon toolbar by dragging them to the desired location. All of the icons have pre-defined behaviors, except the set in the “User Definable Tools” category. Use these to add your own functionality.

Each icon has an associated command that defines what happens when it is clicked. To view or change that command, right-click on the icon and select Properties. The icon needs to be located in a toolbar to do this. You cannot access the properties for an icon in the Toolbar Customizations dialog.  

“Exit, Stage Left”
OK, that one is a Snagglepuss reference[ii]. Whatever angst I previously expressed for mice has now been transferred to the Golden Polygon of Unspecified Dimensions. Regardless of the shape, you now have the tools you need to configure SlickEdit to work the way YOU think it should, using the key bindings you find natural. This is one of the essential steps in being a true power programmer.
 


    

[i] Mr. Jinks, sometimes called “Jinks the cat”, appeared in the “Pixie and Dixie” cartoons as part of “The Huckleberry Hound Show”, by Hanna and Barbera.

[ii] Snagglepus is another of Hanna-Barbera’s cartoon characters.

In Part 1, we went over how to create a macro from scratch, load it and bind it to a key.  We also went over some basic macro functionality like inserting a new line of text and determining which language we are working in.  I thought it would be useful to stick with the theme of writing a macro that could insert some useful code.  In this blog, we’ll write a macro that surrounds the currently selected block of code with some simple code that reports how long the block takes to execute.  It’s sort of a poor-man’s performance tuning, and everyone needs to do it from time to time when they need to determine how long various sections of code take to execute, but don’t want to instrument the whole code base.

Working with selections

The very first thing I realized when I sat down to do this was that I’d need to get the bounds of the current selection, and I’d never worked with selections before.  Whenever I hit an area that I’m unfamiliar with, the first place I go is the API documentation.  To get there, you can click Help > Contents, and then click API Documentation at the bottom of the contents list.  Next, select Macro Functions By Category and you will be taken to a categorized list of the documented Slick-C functions.  I scrolled down and found the Selection Functions category in the list, clicked it, and started looking through that list for potentially useful ways to get the bounds of the current selection.

Right away, I noticed _get_selinfo() and it looked like a perfect fit.  The problem that I discovered though, is that _get_selinfo() doesn’t provide line numbers for the current selection, only column numbers, which made it useless for the information I needed.  I then decided to look at how other macro functions were using _get_selinfo(), so I right clicked it and selected “Goto reference to _get_selinfo” which lists all of the places in the macro code where it’s called.  You can also use the find-refs command for this.  This led me to other code that used _begin_select() and _end_select(), which I had passed over in the help originally because they sounded more like actions than a way of getting selection information. My strategy for getting the line range was to call _begin_select(), which places the cursor on the first line of the selection, and insert a line of code to get a timestamp.  I would then do the same thing with _end_select() and insert the code to get a second timestamp and report the difference.

Writing the macro

We’ll start by beginning a new macro file the same way we did in Part 1, and we’ll call it “insert_debug_timer.e”.  Using our selection strategy, we can write the following macro code:

After loading this, you can select some text in your editor, run insert-debug-timer() and your selection will be surrounded by the text “This is the beginning” and “This is the end”.  Success!  Now we can move on to inserting the actual code that will do the timing.  There will be two parts to this, the code that gets inserted before the selection, and the code that gets inserted after the selection.  I first made a throw-away command to test what the code might look like (in Slick-C):

Let’s start with the code that gets inserted before the selection.  We know that we’re going to have to detect which language we’re in and insert something different based on that, just like we did in Part 1.  I didn’t want to pollute the code in the insert_debug_timing() command, so I decided that I’d make a separate function to insert the “before” code.  This function will take the indentation text as a parameter and directly insert the text at the current line.

Next we can write the second function, which is basically the same as this one, except that it inserts the code to take a second timestamp, diff the two timestamps and report the time difference.  We now replace the lines in insert_debug_timer() with calls to these two functions.  We now have a command that takes the current selection (or the current line if there’s no selection) and wraps it with timing code.

A final improvement

This is a good start, but there are a small improvements I had to include.  The problem I ran into after about 10 seconds of using the macro is that if you have more than one section you want to time, you get a variable name collision.  Since these debug timing code inserts are really just temporary, we can name the variables anything we want.  I decided to make a global int variable called g_debugVarCounter that we could append to the variable names every time the insert_debug_timer() command is called, and then increment that value at the end of the command.  Here is the declaration of the global counter:

and here’s how we can use it to make the variable names:

Now we can insert debug timing commands wherever we want and there are no variable name collisions. The insert-debug-timer() macro is ready to be bound to a key combination by clicking Tools > Options, then go to Keyboard and Mouse > Key Bindings in the options tree, just like we did in part 1.

The macro file can be downloaded by clicking the this link.

Removing debug timing sections

Another important improvement would be a way to remove any of the code we’ve inserted by running this command.  We’ll cover how to do that in the next blog post… check back soon!

One of the things that we think sets SlickEdit apart from other editors is the amount of customization you can do to it.  If you aren’t happy with the way something behaves, chances are there’s an option to change it. In addition to the standard settings available in the Options dialog, there’s the fact that much of SlickEdit is written in macro code.  This allows for an entirely new level of customization and functionality tailored to the way you work.

Unfortunately, only the most advanced of the advanced users ever consider getting so deep into an application that they learn to write their own macros for it.  There’s a few reasons for this.  Many people don’t even know that so much functionality is available in macro form.  Even if a user did look into making their own macros, finding out what functionality is available in macro code is an enormous challenge.  We all have deadlines and important things to do, and learning a new code base like the Slick-C macro language just doesn’t realistically fit into most people’s 24 hour day.

I personally love making little knock-off gizmo utilities for things that fit my workflow.  One of the things that I do often is putting debug statements at various points in my code to report the value of some variable.  This is very helpful when you want a history of values, not just a one stop breakpoint in the debugger.  I decided to make a macro that adds these statements automatically given the word under my cursor (assuming it’s a variable).  It’s a simple, first-step macro, and instead of just posting the code, I’m going to walk through the steps to make it.

Starting a macro from scratch

To start, click File > New Item From Template.  This will bring up the “Add New Item” dialog.  Select Slick-C in the “Categories” tree, select a location where you’d like this file (a scratch development folder is best), and enter insert_debug_value as the name.

Click the Add button.  This creates a new file called insert_debug_value.e in the specified directory.  This file will be opened in a new buffer with the following code included:

This is the basis for all macros started from scratch.  You can load it hitting the escape button to go to the command line, then type “load” and hit enter.

SlickEdit will respond by reporting “Module(s) loaded” in the status area at the bottom of the application.  You can now type “insert-debug-value” on the command line and your new command will execute.  Of course, it doesn’t do anything yet…

Implementing the macro

A general strategy for this function will be the following:

  1. Get the current word that the cursor is in.
  2. Determine which language is being edited.
  3. Insert a new line beneath that to output it’s value.

That seems simple enough.  To start, we’ll use the cur_word() function to determine the full word beneath the cursor.  This function has a byref parameter, which returns the beginning column of the current word, so we have to pass a temp variable for that.  We can also use the say() function to report the value of curWord to the stdout window.  The code currently looks like this:

Again, you can reload this by hitting the escape button to go to the command line, then type “load” and hit enter.  If you run it from the command line, it will show the stdout window titled vsapi.dll, and the value of curWord will be shown.

Now we can start to build the text that we’ll insert.  Since we want this inserted line to be indented to match the current line, we’ll get the current indentation using the first_non_blank() function.  This puts the cursor on the first non-blank column on the line.  Next, we’ll get the column number using the p_col property.  We can build an indentation string using the indent_string() function, passing the starting column value.  The code now looks like this:

For this example, we’ll support three languages; Slick-C, C/C++ and Java.  To tell which language we’re in, we can use the p_LangId property, which returns the language ID of the current editor.  We can switch on that value and append the code to the variable containing the text to insert.  Finally we can call insert_line() to insert the text we’ve built.

Go to the command line and reload the file by typing “load” and hitting enter.  Now put the cursor on a variable in the editor, go back to the command line and type “insert-debug-value” and hit enter.  A say() statement will be inserted into the file on the line after the cursor that reports the value of the variable your cursor was on.  Now you have a way to quickly add a debug statement to your code to report the value of the variable without having to write anything.  A final improvement is to store the original line number and column so they can be restored after the new line is inserted.

The macro file can be downloaded by clicking the this link.

Binding a key combination to your macro

A macro like the one we just made works best when bound to a key, since we don’t want to have to go to the command line every time we want to invoke it.  To bind a key combination to this macro, bring up the options by clicking Tools > Options, then go to Keyboard and Mouse > Key Bindings in the options tree.  Type the name of the macro in the “Search by command:” text box to find it.  Double click its entry to bring up the “Bind Key” dialog.  We’ll bind it to “Ctrl+Shift+/”.  Press that key combination and it will appear in the “Key Sequence:” dialog.  Click the “Bind” button to bind it.  The key bindings entry should look like this:

Click OK on the options dialog.  Now, whenever you press “Ctrl+Shift+/” your macro will execute.

Is it perfect?

Absolutely not!  There are many potential problems here.  The cursor may not be on a variable.  The buffer may be read only.  There are also many other languages that this macro could support.  The list is potentially huge.  However, this macro is a starting point from which you can start to tinker around with writing your own macros.  Soon, you’ll be amazed at how much can be done with Slick-C that you’ll start writing lots of little tools and utilities that fit your own unique workflow.  Check back for more blog posts in this series, which will build on what we’ve started here.

Next Page »