5.0

5.0 | 5 ratings Rate this file 134 downloads (last 30 days) File Size: 11.27 KB File ID: #24615

EditorMacro - assign a macro to a keyboard key-stroke in the Matlab Editor and Command Window

by Yair Altman

 

01 Jul 2009 (Updated 19 Aug 2009)

Code covered by BSD License  

EditorMacro assigns a macro or action to requested key-binding in the Matlab Editor & Command Window

Download Now | Watch this File

File Information
Description

Syntax:
     [bindingsList, actionsList] = EditorMacro(keystroke, macro, macroType)
     [bindingsList, actionsList] = EditorMacro(bindingsList)
 
Description:
     EditorMacro assigns the specified MACRO to the requested keyboard KEYSTROKE, within the context of the Matlab Editor and Command Window.
 
     KEYSTROKE is a string representation of the keyboard combination. Special modifiers (Alt, Ctrl or Control, Shift, Meta, AltGraph) are recognized and should be separated with a space, dash (-), plus (+) or comma (,). If KEYSTROKE was already defined, then it will be updated (overridden).
 
     MACRO should be in one of Matlab's standard callback formats: 'string', @FunctionHandle or {@FunctionHandle,arg1,...}, or any of several hundred built-in editor action-names - read MACROTYPE below for a full description. To remove a KEYSTROKE-MACRO definition, simply enter an empty MACRO ([], {} or '').
 
     MACROTYPE is an optional input argument specifying the type of action that MACRO is expected to do:
 
      - 'text' (=default value) indicates that if the MACRO is a:
         1. 'string': this string will be inserted as-is into the current
              editor caret position (or replace selected editor text).
              Multi-line strings can be set using embedded \n's.
         2. @FunctionHandle - the specified function will be invoked
              and is expected to return a string which will then be inserted
              into the editor document as expained above.
         3. {@FunctionHandle,arg1,...} - like #2, but the function will
              be called with the specified args.
 
      - 'run' indicates that MACRO should be invoked as a Matlab
              command, just like any regular Matlab callback. The
              accepted MACRO formats and function input args are
              exactly like for 'text' above, except that no output
              string is expected and no text insertion/replacement
              will be done, useful for non-textual actions.

              In addition, this MACROTYPE accepts all available
              (built-in) editor action names. Valid action names can
              be listed by requesting the ACTIONSLIST output arg.
 
     BINDINGSLIST = EditorMacro returns the list of currently-defined KEYSTROKE bindings as a 4-columned cell array:{keystroke,macro,type,class}. The class information indicates a built-in action ('editor menu action', 'editor native action', 'cmdwin native action' or 'cmdwin menu action') or a user-defined action ('text' or 'user-defined macro').
 
     BINDINGSLIST = EditorMacro(KEYSTROKE) returns the bindings list for the specified KEYSTROKE as a 4-columned cell array: {keystroke, macro, type, class}.
 
     BINDINGSLIST = EditorMacro(KEYSTROKE,MACRO) returns the bindings list after defining a specific KEYSTROKE-MACRO binding.
 
     EditorMacro(BINDINGSLIST) can be used to set a bunch of key bindings using a single command. BINDINGSLIST is the cell array returned from a previous invocation of EditorMacro, or by manual construction.

     [BINDINGSLIST, ACTIONSLIST] = EditorMacro(...) returns in ACTIONSLIST a 3-columned cell array of all available built-in actions and currently-associated key-biding(s): {actionName, keyBinding(s), class}.
 
Usage Examples:
     bindingsList = EditorMacro; % get list of current key-bindings
     bindingsList = EditorMacro('ctrl r'); % get list of bindings for <Ctrl>-R
     [bindings,actions] = EditorMacro; % get list of available built-in action-names
     EditorMacro('Ctrl Shift C', '%%% Main comment %%%\n% \n% \n% \n');
     EditorMacro('Alt-x', 'try\n % Main code here\ncatch\n % Exception handling here\nend');
     EditorMacro('Ctrl-Alt C', @myCallbackFunction); % myCallbackFunction returns a string to insert
     EditorMacro('Alt control t', @(a,b)datestr(now), 'text'); % insert current timestamp
     EditorMacro('Shift-Control d', {@computeDiameter,3.14159}, 'run');
     EditorMacro('Alt L', 'to-lower-case', 'run') % Built-in action: convert text to lowercase
     EditorMacro('ctrl D','open-selection','run') % Override default Command-Window action (=delete) to behave as in the Editor (=open selected file)
 
A few known limitations (=TODO for future versions) are listed within the file's help section.
 
Bugs and suggestions:
     EditorMacro was tested on Matlab 6.0 (R12) through 7.7(R2008b).

     Note: Unfortunately, my Matlab 6 computer crashed since the first version so I can no longer test EditorMacro on Matlab 6, so if you find problems please email me directly.

     Please send bugs to Yair Altman (altmany at gmail dot com)
 
Warning:
     This code heavily relies on undocumented and unsupported
     Matlab functionality. It works on Matlab 6 & 7+, but use at
     your own risk!
 
A technical description of the implementation can be found at: http://UndocumentedMatlab.com/blog/EditorMacro/

Acknowledgements
This submission has inspired the following:
KeyBindings - Alter Matlab Editor key bindings, TextTools
MATLAB release MATLAB 6.0 (R12)
Zip File Content  
Other Files EditorMacro.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
02 Jul 2009 Jackson Shen

Exellent Work. It works with R2009A (Trial).
Brillian Features! (not for me at moment).
Unfornuately, it does not work with my R2008A.

03 Jul 2009 Jackson Shen

I have tried again today. It "magically" works.
Yes, it does with with the Matlab R2008A.
I must say I love this feature.

04 Jul 2009 Qun HAN

WOW!

20 Jul 2009 Julien

Excellent work indeed, thank you very much for sharing this very useful and powerful function.
For users which would be interested in modifying selected text and not inserting new text at caret position, you can use the jEditorPane methods "getSelectedText" and "replaceSelection".
For example, if you want to transform selected text in current editor to upper case characters, you can do as follow :
---------------------------------------------------------------------------
function Upper_Macro(hDocument,eventData)
% Get Selected text in current editor
selectedText = char(hDocument.getSelectedText);
if isempty(selectedText)
    return;
end
% Modify selected text to upper case character
modifiedText = upper(selectedText);
% Update selection in current editor
hDocument.replaceSelection(modifiedText);
---------------------------------------------------------------------------
And simply run in the command window :
>>macros = EditorMacro('ctrl-u',@Upper_Macro,'run');
---------------------------------------------------------------------------
Thank you again Yair !

22 Aug 2009 Ustun Ozgur

EditorMacro('Alt-x', 'try\n % Main code here\ncatch\n % Exception handling here\nend');

does not work in OS X with Matlab R2008b.

EditorMacro('Meta x', 'try\n % Main code here\ncatch\n % Exception handling here\nend');

works however.

26 Aug 2009 Yair Altman

For those interested, a set of EditorMacro macro functions has been submitted by Perttu Ranta-aho. The submission is called "TextTools" and can be found here: http://www.mathworks.com/matlabcentral/fileexchange/25122

06 Oct 2009 Andrey Pudeyev  
06 Oct 2009 Andrey Pudeyev

Great work, just the very thing I missed in a matlab interface - a customizable macro.

Summarizing the examples of posted here, i propose the following solution for exception management:
-----------
function trycatch(hDocument,eventData)
% Get Selected text in current editor
selectedText = char(hDocument.getSelectedText);

modifiedText = ['try' char(13) selectedText char(13) 'catch' char(13) 'keyboard' char(13) 'end'];

% Update selection in current editor
hDocument.replaceSelection(modifiedText);
--------------

This will allow quickly put the selected text block inside try-catch construction

Please login to add a comment or rating.
Updates
19 Aug 2009

Many fixes; Automatically detect macro functions that do not accept the expected two input args; Support for native/menu actions (idea by Perttu Ranta-aho); Support for command-window actions; Use EDT for text replacement; Added screenshot

Tag Activity for this File
Tag Applied By Date/Time
editor Yair Altman 01 Jul 2009 14:22:29
java Yair Altman 01 Jul 2009 14:22:30
undocumented Yair Altman 01 Jul 2009 14:22:30
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com