- Undocumented Matlab - https://undocumentedmatlab.com -

Non-textual editor actions

Posted By Yair Altman On July 17, 2009 | 10 Comments

Following my EditorMacro post [1] a couple of weeks ago, which showed how to assign a keyboard macro to the integrated Matlab Editor, several people have asked me whether it is possible to assign a macro to non-textual actions, in addition to the text insertion/replacement which EditorMacro supports.
The quick answer is yes, with some careful programming. Instead of specifying the end result, I will use this opportunity to illustrate how Java objects (not just the editor) can be inspected for their supported actions/properties.
Our first step is to get the requested Java reference handle. This can be done via the Matlab Command Window (interested readers can look at the EditorMacro.m source code, specifically at its getJEditor() function). However, a much easier way is to assign some macro using EditorMacro and then simply place a breakpoint in EditorMacro’s keyPressedCallback() callback function. Then press an arrow key (or any other key) in the editor, and wait for the breakpoint focus to arrive (don’t forget to clear the breakpoint…). From here on, all our actions will be done in the Command Window.
We now have a variable called jEditorPane, which is a reference to a Java object of type javahandle_withcallbacks. com.mathworks.mde.editor.EditorSyntaxTextPane (in Matlab 7 – it’s something similar in Matlab 6). This is a Matlab wrapper for the basic Java object, used for accessing the callback hooks, as explained in a previous post. [2] In our case we are not interested in this wrapper but in its wrapped object, which is retrieved via Matlab’s built-in java function (java(jEditorPane) or jEditorPane.java). The inspection itself is done using Matlab’s standard tools (inspect, methodsview etc.) or via my UIINSPECT utility [3]. I suggest using UIINSPECT, which displays all the information of the standard tools and lots extra, but I’m of course biased…

uiinspect(jEditorPane.java);

jEditorPane inspection using UIINSPECT (click to see details) [4]
jEditorPane inspection using UIINSPECT (click to see details)

Without diving into all the UIINSPECT options (I shall do this in a dedicated post), we see the supported methods/actions on the left, and properties on the right. It is true that none of them are documented, but many are self-explanatory. For example, the cut()/copy()/paste() methods or the caretPosition/caretColor properties. Any combination of these methods and properties can be used in a user-defined macro.
Let’s do a simple example, setting the <Ctrl-E> combination to a macro moving to the end-of-line (unix-style – equivalent to <End> on Windows), and <Ctrl-Shift-E> to a similar macro doing the same while also selecting the text (like <Shift-End> on Windows). We shall even use the same macro code, by simply checking in the eventData whether the <Shift> key is depressed:

function EOL_Macro(hDocument,eventData)
  % Find the position of the next EOL mark
  currentPos = hDocument.getCaretPosition;
  docLength = hDocument.getLength;
  textToEOF = char(hDocument.getTextStartEnd(currentPos,docLength));
  nextEOLPos = currentPos+find(textToEOF<=13,1)-1;  % next CR/LF pos
  if isempty(nextEOLPos)
      % no EOL found (=> move to end-of-file)
      nextEOLPos = docLength;
  end
  % Do action based on whether  was pressed or not
  %get(eventData);
  if eventData.isShiftDown
      % Select to EOL
      hDocument.moveCaretPosition(nextEOLPos);
  else
      % Move to EOL (without selection)
      hDocument.setCaretPosition(nextEOLPos);
  end
end  % EOL_Macro

…and now let’s activate this macro in the Matlab Command Window:

>> macros = EditorMacro('ctrl-e',@EOL_Macro,'run');
>> macros = EditorMacro('ctrl-shift-e',@EOL_Macro,'run')
macros =
    'ctrl alt pressed T'      @(a,b)datestr(now)    'text'
    'ctrl pressed E'          @EOL_Macro            'run'
    'shift ctrl pressed E'    @EOL_Macro            'run'

Please do explore all the possible actions/properties exposed by the jEditorPane object. Probably the worst that could happen (and very rarely) is that you’ll crash Matlab and need to restart it – no biggy. If you find an interesting macro combination, please post it to the File Excahnge, and/or let us all know by placing a comment below.

Categories: Desktop, High risk of breaking in future versions, Java


10 Comments (Open | Close)

10 Comments To "Non-textual editor actions"

#1 Comment By Dan On July 21, 2009 @ 07:06

Have you ever considered assigning Matlab shortcuts to an X-keys? They were originally designed for CAD apps and other shortcut intensive apps, but they are easy to set up, and could make an interesting code editing tool, especially when coupled with the macros you describe in this post.

#2 Comment By Yair Altman On July 21, 2009 @ 08:22

@Dan – I am not sure how the X-keys are mapped within Java, and so I’m not sure what combination should be specified (‘meta-x’ ?). It would be helpful if you could answer this, for Matlab X-keys users. If you need help debugging, email me offline.

#3 Pingback By EditorMacro v2 – setting Command Window key-bindings | Undocumented Matlab On August 21, 2009 @ 01:40

[…] The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details. […]

#4 Pingback By R2009b keyboard bindings | Undocumented Matlab On September 6, 2009 @ 08:56

[…] this need has been addressed in the past by my EditorMacro utility, its expansions, and Perttu Ranta-aho’s KeyBindings derivative […]

#5 Comment By vvml On March 24, 2013 @ 14:21

Key binding seems NOT to work in debug mode (like when you set breakpoint in m-function and stop when execution reaches it).
Is it possible in Matlab to register bindings for debug-mode?
I used
>> EditorMacro(‘F4′, @myCallbackFunction,’run’);
to register new key binding.


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/non-textual-editor-actions

URLs in this post:

[1] EditorMacro post: http://undocumentedmatlab.com/blog/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/

[2] accessing the callback hooks, as explained in a previous post.: http://undocumentedmatlab.com/blog/uicontrol-callbacks/

[3] UIINSPECT utility: http://www.mathworks.com/matlabcentral/fileexchange/17935

[4] Image: http://undocumentedmatlab.com/images/uiinspect_jeditor.png

[5] EditorMacro – assign a keyboard macro in the Matlab editor : https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor

[6] Accessing the Matlab Editor : https://undocumentedmatlab.com/articles/accessing-the-matlab-editor

[7] Spicing up the Matlab Editor : https://undocumentedmatlab.com/articles/spicing-up-the-matlab-editor

[8] EditorMacro v2 – setting Command Window key-bindings : https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings

[9] Setting listbox mouse actions : https://undocumentedmatlab.com/articles/setting-listbox-mouse-actions

[10] Variables Editor scrolling : https://undocumentedmatlab.com/articles/variables-editor-scrolling

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.