<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>keyboard &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/keyboard/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Sun, 06 Sep 2009 15:56:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.3</generator>
	<item>
		<title>R2009b keyboard bindings</title>
		<link>https://undocumentedmatlab.com/articles/r2009b-keyboard-bindings?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=r2009b-keyboard-bindings</link>
					<comments>https://undocumentedmatlab.com/articles/r2009b-keyboard-bindings#respond</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 06 Sep 2009 15:56:12 +0000</pubDate>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Editor]]></category>
		<category><![CDATA[keyboard]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=578</guid>

					<description><![CDATA[<p>The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its variants.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/r2009b-keyboard-bindings">R2009b keyboard bindings</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings" rel="bookmark" title="EditorMacro v2 &#8211; setting Command Window key-bindings">EditorMacro v2 &#8211; setting Command Window key-bindings </a> <small>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....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor" rel="bookmark" title="EditorMacro &#8211; assign a keyboard macro in the Matlab editor">EditorMacro &#8211; assign a keyboard macro in the Matlab editor </a> <small>EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/non-textual-editor-actions" rel="bookmark" title="Non-textual editor actions">Non-textual editor actions </a> <small>The UIINSPECT utility can be used to expand EditorMacro capabilities to non-text-insertion actions. This is how:...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/spicing-up-the-matlab-editor" rel="bookmark" title="Spicing up the Matlab Editor">Spicing up the Matlab Editor </a> <small>Matlab's Editor and Workspace can be enhanced quite significantly using an open-source utility. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>This weekend, Kenn Orr, Matlab&#8217;s desktop development team leader, announced an important and long-awaited feature that becomes available in the upcoming R2009b release: the ability to <a rel="nofollow" target="_blank" href="http://blogs.mathworks.com/desktop/2009/09/04/r2009b-is-here/">customize keyboard bindings in the system preferences</a>.<br />
This is indeed an important preference customization, which has often been requested. At first look, it appears that the desktop design team has done a good job of enabling easy keyboard shortcuts customization, saving and loading sets of shortcuts etc. &#8211; all in a new easy-to-use preference page.<br />
This need has been addressed in the past by my <a target="_blank" href="/articles/EditorMacro">EditorMacro utility</a>, its expansions (<a target="_blank" href="/articles/non-textual-editor-actions/">here</a> and <a target="_blank" href="/articles/editormacro-v2-setting-command-window-key-bindings/">here</a>), and Perttu Ranta-aho&#8217;s <a target="_blank" href="http://www.mathworks.com/matlabcentral/fileexchange/25089" rel="nofollow">KeyBindings</a> derivative utility.<br />
Kudos for a job apparently well-done for the Matlab dev team aside, there may still be reasons for using EditorMacro and/or KeyBindings:</p>
<ul>
<li><b>Earlier Matlab versions</b> &#8211; those who have a Matlab release earlier than R2009b have no option but to use these utilities. The first version of EditorMacro even supported Matlab 6.0, now almost a decade old!</li>
<li><b>Programmatic access</b> &#8211; some users may wish to have programmatic access to the keyboard bindings. For example, by saving sets of bindings in an m-file and accessing any of these sets via GUI or the desktop shortcuts toolbar. Note that in R2009b, these preferences can probably be accessed programmatically via the <a target="_blank" href="/articles/changing-system-preferences-programmatically/">preferences interface, explained here</a>.</li>
<li><b>Understanding the underlying workings</b> of the Matlab desktop and editor, for those interested in exploring and using these undocumented subjects.</li>
</ul>
<p>User-contributed utilities, especially those relying on undocumented features like EditorMacro and KeyBindings, will always pale next to sleek GUI preferences which are well-integrated by design. They have a place in niche usages, as explained above, but the hope is that all these needs will eventually be addressed by well-documented integrated features, exactly as happened in this particular case. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/r2009b-keyboard-bindings">R2009b keyboard bindings</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings" rel="bookmark" title="EditorMacro v2 &#8211; setting Command Window key-bindings">EditorMacro v2 &#8211; setting Command Window key-bindings </a> <small>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....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor" rel="bookmark" title="EditorMacro &#8211; assign a keyboard macro in the Matlab editor">EditorMacro &#8211; assign a keyboard macro in the Matlab editor </a> <small>EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/non-textual-editor-actions" rel="bookmark" title="Non-textual editor actions">Non-textual editor actions </a> <small>The UIINSPECT utility can be used to expand EditorMacro capabilities to non-text-insertion actions. This is how:...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/spicing-up-the-matlab-editor" rel="bookmark" title="Spicing up the Matlab Editor">Spicing up the Matlab Editor </a> <small>Matlab's Editor and Workspace can be enhanced quite significantly using an open-source utility. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/r2009b-keyboard-bindings/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>EditorMacro v2 &#8211; setting Command Window key-bindings</title>
		<link>https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=editormacro-v2-setting-command-window-key-bindings</link>
					<comments>https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 20 Aug 2009 13:02:54 +0000</pubDate>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Listeners]]></category>
		<category><![CDATA[Editor]]></category>
		<category><![CDATA[keyboard]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=518</guid>

					<description><![CDATA[<p>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.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings">EditorMacro v2 &#8211; setting Command Window key-bindings</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor" rel="bookmark" title="EditorMacro &#8211; assign a keyboard macro in the Matlab editor">EditorMacro &#8211; assign a keyboard macro in the Matlab editor </a> <small>EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/r2009b-keyboard-bindings" rel="bookmark" title="R2009b keyboard bindings">R2009b keyboard bindings </a> <small>The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its variants....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/changing-matlab-command-window-colors" rel="bookmark" title="Changing Matlab&#039;s Command Window colors">Changing Matlab&#039;s Command Window colors </a> <small>Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/bold-color-text-in-the-command-window" rel="bookmark" title="Bold color text in the Command Window">Bold color text in the Command Window </a> <small>Matlab Command Window text can be formatted *bold* since R2011b. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Some weeks ago, I introduced my <a target="_blank" href="/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/">EditorMacro utility</a> as a means of assigning user-defined text strings and runnable macros (callbacks) to keyboard shortcuts in the Matlab Editor. I later showed how <i><b>EditorMacro</b></i> can be used to <a target="_blank" href="/articles/non-textual-editor-actions/"> set non-textual editor macros</a>.<br />
Since then, I was contacted by several users with questions, enhancement requests and improvement suggestions. Of these, the most notable was from Perttu Ranta-aho who suggested (and provided sample code for) using the editor&#8217;s built-in actions. The next logical step was to extend this to support the editor menus, and then extend again to support Command Window key-bindings. We bounced several increasingly-powerful versions of the utility between us, until arriving at the version I uploaded yesterday, which has the following significant improvements over the first version:</p>
<ul>
<li>several fixes: bugs, edge cases, stability (EDT etc.) etc.</li>
<li>support for built-in (native) Editor actions</li>
<li>support for built-in (native) Command-Window actions</li>
</ul>
<h3>Built-in native actions</h3>
<p>It turns out that the Editor and Command-Window both have some ~200 built-in (native) available actions, about half of them common. Actions are identified by name, which is a lowercase dash-separated description like &#8216;selection-up&#8217; (this format is familiar to Emacs users). Altogether, there are over 300 unique built-in actions which can be used, but only ~100 of them are assigned a default key-bindings. A few dozen actions have multiple assigned key-bindings. For example, the &#8216;selection-up&#8217; action is assigned to both &#8216;shift pressed UP&#8217; (=&lt;shift&gt;-&lt;up&gt;) and &#8216;shift pressed KP_UP&#8217; (=&lt;shift&gt;-&lt;Keypad-up&gt;):</p>
<pre lang="matlab">
>> [bindings, actions] = EditorMacro
actions =
...[snip]
 'selection-page-down'     'shift pressed PAGE_DOWN'     'editor native action'
 'selection-page-up'       'shift pressed PAGE_UP'       'editor native action'
 'selection-previous-word'                   {2x1 cell}  'editor native action'
 'selection-up'                              {2x1 cell}  'editor native action'
 'set-read-only'                                     []  'editor native action'
 'set-writable'                                      []  'editor native action'
 'shift-insert-break'      'shift pressed ENTER'         'editor native action'
 'shift-line-left'         'ctrl pressed OPEN_BRACKET'   'editor native action'
 'shift-line-right'        'ctrl pressed CLOSE_BRACKET'  'editor native action'
 'shift-tab-pressed'       'shift pressed TAB'           'editor native action'
...[snip]...
 'toggle-typing-mode'      'pressed INSERT'              'editor native action'
 'uncomment'               'ctrl pressed T'              'editor native action'
 'undo'                    'ctrl pressed Z'              'editor native action'
 'unselect'                'pressed ESCAPE'              'editor native action'
 'adjust-window-bottom'                              []  'cmdwin native action'
 'adjust-window-top'                                 []  'cmdwin native action'
 'beep'                    'ctrl pressed G'              'cmdwin native action'
 'break-interrupt'         'ctrl pressed CANCEL'         'cmdwin native action'
...[snip]...
 'toggle-typing-mode'      'pressed INSERT'              'cmdwin native action'
 'unselect'                'ctrl pressed BACK_SLASH'     'cmdwin native action'
 'new-mfile'               'ctrl pressed N'              'editor menu action'
 'Figure'                                            []  'editor menu action'
...[snip]...
</pre>
<p>Even more interesting, apparently some 200 actions do not have any pre-assigned default key-bindings, such as &#8216;set-read-only&#8217; and &#8216;set-writable&#8217; in the snippet above. Let&#8217;s take the &#8216;match-brace&#8217; action for example. This sounded promising so I assigned it an unused key-binding and indeed found that it can be very useful: if your cursor is placed on a beginning or end of some code, clicking the assigned key-binding will jump the cursor to the other end, and then back again. This works nicely for (..), [..], for..end, try..end, if..end, etc.</p>
<pre lang="matlab">
>> % Ensure that <alt>-M is unassigned
>> bindings = EditorMacro('alt m')
bindings =
   Empty cell array: 0-by-4
>> % Assign the key-binding and verify
>> EditorMacro('alt m','match-brace','run');
>> bindings = EditorMacro('alt m')
b =
  'alt pressed M'  'match-brace'  'run'  'editor native action'
</pre>
<h3>Implementation details</h3>
<p>Interested readers of this post are encouraged to look within <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/24615">EditorMacro&#8217;s source code</a> and see how the native actions and keybindings were retrieved and modified for the editor, Command-Window and menus. In a nutshell, the native action names and key-bindings are stored in a Java Map object. Here&#8217;s a code snippet:</p>
<pre lang="matlab">
%% Get all available actions even those without any key-binding
function actionNames = getNativeActions(hEditorPane)
  try
    actionNames = {};
    actionKeys = hEditorPane.getActionMap.allKeys;
    actionNames = cellfun(@char,cell(actionKeys),'Uniform',0);
    actionNames = sort(actionNames);
  catch
    % never mind...
  end
%% Get all active native shortcuts (key-bindings)
function accelerators = getAccelerators(hEditorPane)
  try
    accelerators = cell(0,2);
    inputMap = hEditorPane.getInputMap;
    inputKeys = inputMap.allKeys;
    accelerators = cell(numel(inputKeys),2);
    for ii = 1 : numel(inputKeys)
      thisKey = inputKeys(ii);
      thisAction = inputMap.get(thisKey);
      accelerators(ii,:) = {char(thisKey), char(thisAction)};
    end
    accelerators = sortrows(accelerators,1);
  catch
    % never mind...
  end
</pre>
<p>The menu retrieval was more difficult: while it is possible to directly access the menubar reference (jMainPane.getRootPane.getMenuBar), the menu items themselves are not visible until their main menu item is clicked (displayed). The only way I know to access menu actions/keybindings is to read them from the individual menu items (if anyone knows a better way please tell me &#8211; perhaps some central key-listener repository?). Therefore, a simulation of the menu-click events is done and the menu hierarchy is traveled recuresively to collect all its actions and key-bindings.<br />
A final note relates to the use of EDT. EDT really deserves a separate post, but in a nutshell it means that any action that affects the GUI needs to be invoked asynchronously (via the EDT) rather than synchronously (on the main Matlab thread). This is no real problem in the editor, but it is indeed an issue in the Command Window: If we do not use EDT there, we get ugly red stack-trace exceptions thrown on the Command Window whenever we run our <i><b>EditorMacro</b></i>-assigned macro. Here&#8217;s the code snippet that solves this:</p>
<pre lang="matlab">
try
   % Matlab 7
   %jEditorPane.insert(caretPosition, macro);  % better to use replaceSelection() than insert()
   try
      % Try to dispatch on EDT
      awtinvoke(jEditorPane.java, 'replaceSelection', macro);
   catch
      % no good - try direct invocation
      jEditorPane.replaceSelection(macro);
   end
catch
   % Matlab 6
   %jEditorPane.insert(macro, caretPosition);  % note the reverse order of input args vs. Matlab 7...
   try
      % Try to dispatch on EDT
      awtinvoke(jEditorPane.java, 'replaceRange', macro, jEditorPane.getSelStart, jEditorPane.getSelEnd);
   catch
      % no good - try direct invocation
      jEditorPane.replaceRange(macro, jEditorPane.getSelStart, jEditorPane.getSelEnd);
   end
end
</pre>
<h3>Known limitations</h3>
<p>Some limitations remain in <i><b>EditorMacro</b></i> &#8211; here are the major ones:</p>
<ul>
<li>Multi-key bindings are still not reported properly, nor can they be assigned. For example, the editor menu action &#8216;to-lower-case&#8217; has a pre-assigned default key-binding of &lt;Alt&gt;-&lt;U&gt;-&lt;L&gt;, but this is reported as unassigned. Of course, you can always add another (single-key) assignment for this action, for example: &lt;Alt&gt;-&lt;Ctrl&gt;-&lt;L&gt;</li>
<li>Menus are dynamically recreated whenever the Editor is docked/undocked, or a separate type of file is edited (e.g., switching from an m-file to a c-file). Similarly, whenever the active desktop window changes from the Command Window to another desktop-docked window (e.g., Command History). In all these cases, the dynamically recreated menus override any conflicting key-binding previously done with <i><b>EditorMacro</b></i>.</li>
<li>Unfortunately, my Matlab 6-installed computer crashed, so while the first version of <i><b>EditorMacro</b></i> works on ML6, the new version might well not. If you have ML6 available, please email me so I can post a working version with your help. This also means I won&#8217;t be able to post much about undocumented ML6 stuff in the future. Perhaps this is Fate&#8217;s way of telling me to progress with the times&#8230;</li>
<li>Key bindings are sometimes lost when switching between a one-document editor and a two-document one (i.e., adding/closing the second doc)</li>
<li>Key bindings are not saved between editor sessions</li>
<li>In split-pane mode, when inserting a text macro on the secondary (right/bottom) editor pane, then both panes (and the actual document) are updated but the secondary pane does not display the inserted macro (the primary pane looks ok).</li>
</ul>
<p>The first couple of limitations have a non-perfect workaround that Perttu came up with. He implemented this in his <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/25089">KeyBindings utility</a> on the Matlab File Exchange. Perhaps one day Perttu or me will find the time to merge these utilities into one.<br />
Have you designed some crafty user-defined macro? or found some important unassigned built-in action? Please share your experience using <i><b>EditorMacro</b></i> in the comments section below. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings">EditorMacro v2 &#8211; setting Command Window key-bindings</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor" rel="bookmark" title="EditorMacro &#8211; assign a keyboard macro in the Matlab editor">EditorMacro &#8211; assign a keyboard macro in the Matlab editor </a> <small>EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/r2009b-keyboard-bindings" rel="bookmark" title="R2009b keyboard bindings">R2009b keyboard bindings </a> <small>The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its variants....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/changing-matlab-command-window-colors" rel="bookmark" title="Changing Matlab&#039;s Command Window colors">Changing Matlab&#039;s Command Window colors </a> <small>Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/bold-color-text-in-the-command-window" rel="bookmark" title="Bold color text in the Command Window">Bold color text in the Command Window </a> <small>Matlab Command Window text can be formatted *bold* since R2011b. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings/feed</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>Non-textual editor actions</title>
		<link>https://undocumentedmatlab.com/articles/non-textual-editor-actions?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=non-textual-editor-actions</link>
					<comments>https://undocumentedmatlab.com/articles/non-textual-editor-actions#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Fri, 17 Jul 2009 13:55:39 +0000</pubDate>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Editor]]></category>
		<category><![CDATA[keyboard]]></category>
		<category><![CDATA[UIInspect]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=454</guid>

					<description><![CDATA[<p>The UIINSPECT utility can be used to expand EditorMacro capabilities to non-text-insertion actions. This is how:</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/non-textual-editor-actions">Non-textual editor actions</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor" rel="bookmark" title="EditorMacro &#8211; assign a keyboard macro in the Matlab editor">EditorMacro &#8211; assign a keyboard macro in the Matlab editor </a> <small>EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/accessing-the-matlab-editor" rel="bookmark" title="Accessing the Matlab Editor">Accessing the Matlab Editor </a> <small>The Matlab Editor can be accessed programmatically, for a wide variety of possible uses - this article shows how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/spicing-up-the-matlab-editor" rel="bookmark" title="Spicing up the Matlab Editor">Spicing up the Matlab Editor </a> <small>Matlab's Editor and Workspace can be enhanced quite significantly using an open-source utility. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings" rel="bookmark" title="EditorMacro v2 &#8211; setting Command Window key-bindings">EditorMacro v2 &#8211; setting Command Window key-bindings </a> <small>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....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Following my <a href="/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/" target="_blank">EditorMacro post</a> 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 <b><i>EditorMacro</i></b> supports.<br />
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.<br />
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 <b><i>EditorMacro</i></b> and then simply place a breakpoint in EditorMacro&#8217;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&#8217;t forget to clear the breakpoint&#8230;). From here on, all our actions will be done in the Command Window.<br />
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 &#8211; it&#8217;s something similar in Matlab 6). This is a Matlab wrapper for the basic Java object, used for <a href="/articles/uicontrol-callbacks/" target="_blank">accessing the callback hooks, as explained in a previous post.</a> In our case we are not interested in this wrapper but in its wrapped object, which is retrieved via Matlab&#8217;s built-in <b><i>java</i></b> function (java(jEditorPane) or jEditorPane.java). The inspection itself is done using Matlab&#8217;s standard tools (<b><i>inspect</i></b>, <b><i>methodsview</i></b> etc.) or via my <a href="http://www.mathworks.com/matlabcentral/fileexchange/17935" target="_blank" rel="nofollow">UIINSPECT utility</a>. I suggest using <b><i>UIINSPECT</i></b>, which displays all the information of the standard tools and lots extra, but I&#8217;m of course biased&#8230;</p>
<blockquote>
<pre lang="matlab">
uiinspect(jEditorPane.java);
</pre>
</blockquote>
<p><center><figure style="width: 450px" class="wp-caption aligncenter"><a href="/images/uiinspect_jeditor.png" target="_blank"><img fetchpriority="high" decoding="async" alt="jEditorPane inspection using UIINSPECT (click to see details)" src="https://undocumentedmatlab.com/images/uiinspect_jeditor.png" title="jEditorPane inspection using UIINSPECT (click to see details)" width="450" height="355" /></a><figcaption class="wp-caption-text">jEditorPane inspection using <b><i>UIINSPECT</i></b> (click to see details)</figcaption></figure></center><br />
Without diving into all the <b><i>UIINSPECT</i></b> 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 <i>cut()/copy()/paste()</i> methods or the <i>caretPosition/caretColor</i> properties. Any combination of these methods and properties can be used in a user-defined macro.<br />
Let&#8217;s do a simple example, setting the &lt;Ctrl-E&gt; combination to a macro moving to the end-of-line (unix-style &#8211; equivalent to &lt;End&gt; on Windows), and &lt;Ctrl-Shift-E&gt; to a similar macro doing the same while also selecting the text (like &lt;Shift-End&gt; on Windows). We shall even use the same macro code, by simply checking in the eventData whether the &lt;Shift&gt; key is depressed:</p>
<pre lang="matlab">
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 <shift> 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
</pre>
<p>&#8230;and now let&#8217;s activate this macro in the Matlab Command Window:</p>
<pre lang="matlab">
>> 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'
</pre>
<p>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&#8217;ll crash Matlab and need to restart it &#8211; 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.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/non-textual-editor-actions">Non-textual editor actions</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor" rel="bookmark" title="EditorMacro &#8211; assign a keyboard macro in the Matlab editor">EditorMacro &#8211; assign a keyboard macro in the Matlab editor </a> <small>EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/accessing-the-matlab-editor" rel="bookmark" title="Accessing the Matlab Editor">Accessing the Matlab Editor </a> <small>The Matlab Editor can be accessed programmatically, for a wide variety of possible uses - this article shows how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/spicing-up-the-matlab-editor" rel="bookmark" title="Spicing up the Matlab Editor">Spicing up the Matlab Editor </a> <small>Matlab's Editor and Workspace can be enhanced quite significantly using an open-source utility. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings" rel="bookmark" title="EditorMacro v2 &#8211; setting Command Window key-bindings">EditorMacro v2 &#8211; setting Command Window key-bindings </a> <small>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....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/non-textual-editor-actions/feed</wfw:commentRss>
			<slash:comments>10</slash:comments>
		
		
			</item>
		<item>
		<title>EditorMacro &#8211; assign a keyboard macro in the Matlab editor</title>
		<link>https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=editormacro-assign-a-keyboard-macro-in-the-matlab-editor</link>
					<comments>https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 01 Jul 2009 19:17:56 +0000</pubDate>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[Hidden property]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[Editor]]></category>
		<category><![CDATA[keyboard]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=437</guid>

					<description><![CDATA[<p>EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor">EditorMacro &#8211; assign a keyboard macro in the Matlab editor</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings" rel="bookmark" title="EditorMacro v2 &#8211; setting Command Window key-bindings">EditorMacro v2 &#8211; setting Command Window key-bindings </a> <small>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....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/accessing-the-matlab-editor" rel="bookmark" title="Accessing the Matlab Editor">Accessing the Matlab Editor </a> <small>The Matlab Editor can be accessed programmatically, for a wide variety of possible uses - this article shows how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/non-textual-editor-actions" rel="bookmark" title="Non-textual editor actions">Non-textual editor actions </a> <small>The UIINSPECT utility can be used to expand EditorMacro capabilities to non-text-insertion actions. This is how:...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/r2009b-keyboard-bindings" rel="bookmark" title="R2009b keyboard bindings">R2009b keyboard bindings </a> <small>The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its variants....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Over the past years there have been quite a few requests to enable keyboard macros and keybinding modifications in the Matlab editor. Some posters have even noted this lack as their main reason to use an external editor. Coincidentally, some weeks ago I was approached by a reader (Grant Roch) to help with assigning some textual editor macros, and with joint work we were able to figure out a basic working mechanism. The <a rel="nofollow" target="_blank" href="http://blogs.mathworks.com/desktop/2009/06/29/what-does-your-matlab-desktop-look-like-again/#comment-6317">latest user comment</a> on this issue was posted on the official Matlab Desktop blog yesterday. All this prompted me to finally post a solution to this need: My <a rel="nofollow" target="_blank" href="http://www.mathworks.com/matlabcentral/fileexchange/24615">EditorMacro utility is now available for download on the Mathworks File Exchange</a>. In this post I will detail some of its inner workings, which rely on undocumented Matlab features.<br />
In a nutshell, <em><strong>EditorMacro</strong></em> sets the KeyPressedCallback property (<a target="_blank" href="/articles/uicontrol-callbacks/">explained in a previous post</a>) for each of the editor&#8217;s document panes, to an internal function. This internal function then checks each keystroke against a list of registered keybindings. The list itself is persisted in the editor object&#8217;s hidden ApplicationData property (accessible via the <em><strong>getappdata</strong></em>/<em><strong>setappdata</strong></em> built-in functions). If a match is found, then the associated macro is invoked. Depending on the macro type, some text can be inserted at the current editor caret position, or replacing the selected editor text, or a non-text Matlab function/command can be invoked.<br />
This enables <em><strong>EditorMacro</strong></em> to be used for quickly inserting code templates (header comments, try-catch blocks etc.) or for automating Matlab unit testing.<br />
Here&#8217;s a typical usage example: start by defining a simple function that returns a dynamic header comment:</p>
<pre lang="matlab">
function comment = createHeaderComment(hDocument, eventData)
  timestamp = datestr(now);
  username = getenv('username');
  %computer = getenv('computername');  % unused
  lineStr = repmat('%',1,35);
  comment = sprintf(...
      ['%s\n' ...
       '%% \n' ...
       '%% Name:    functionName\n' ...
       '%% \n' ...
       '%% Desc:    enter description here\n' ...
       '%% \n' ...
       '%% Inputs:  enter inputs here\n' ...
       '%% \n' ...
       '%% Outputs: enter outputs here\n' ...
       '%% \n' ...
       '%% Created: %s\n' ...
       '%% \n' ...
       '%% Author:  %s\n' ...
       '%% \n' ...
       '%s\n'], ...
       lineStr, timestamp, username, lineStr);
end  % createHeaderComment
</pre>
<p>Now define a macro to use this function, and another simple try-catch template macro:</p>
<pre lang="matlab">
>> EditorMacro('Alt-Control-h', @createHeaderComment);
>> macroStr = 'try\n  % Main code here\ncatch\n  % Exception handling here\nend';
>> b = EditorMacro('Ctrl alt T', macroStr)
b =
    'ctrl alt pressed H'    @createHeaderComment    'text'
    'ctrl alt pressed T'             [1x60 char]    'text'
</pre>
<p>Now start with a blank document and click &lt;Ctrl&gt;-&lt;Alt&gt;-H and &lt;Ctrl&gt;-&lt;Alt&gt;-T. This is the end result:</p>
<blockquote>
<pre lang="matlab">
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Name:    functionName
%
% Desc:    enter description here
%
% Inputs:  enter inputs here
%
% Outputs: enter outputs here
%
% Created: 01-Jul-2009 23:31:46
%
% Author:  Yair Altman
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try
  % Main code here
catch
  % Exception handling here
end
</pre>
</blockquote>
<p>Keybindings are normalized using Java&#8217;s built-in javax.swing.KeyStroke.getKeyStroke() method, to enable the user a very wide range of keystroke naming formats (e.g., &#8216;Alt-Control-T&#8217; or &#8216;ctrl alt t&#8217;).<br />
I have taken great pains to make <em><strong>EditorMacro</strong></em> compatible with all Matlab versions since 6.0 (R12). This was no easy feat: Matlab 7 made some significant changes to the editor layout. Luckily, once I found out how to get a handle to the Matlab 6 editor object (this took some hours of trial-and-error), listing its display hierarchy was simple and the modifications were generally straight-forward, although non-trivial (different quirks due to missing default type-castings, missing eventData in invoked callbacks etc&#8230;). Please feel free to look at <em><strong>EditorMacro</strong></em>&#8216;s source code for the clearly-marked Matlab 6 segments.<br />
For the record, here is a snippet showing how to get the editor object in Matlab 6 and 7. Note that the desktop handle is only useful to get the editor handle in Matlab 6 &#8211; we need a totally different way in Matlab 6:</p>
<pre lang="matlab">
try
   % Matlab 7
   jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
   jEditor = jDesktop.getGroupContainer('Editor');
catch
   % Matlab 6
   try
      %desktop = com.mathworks.ide.desktop.MLDesktop.getMLDesktop;
      % no use: can't get to the editor from the desktop handle in Matlab 6...
      openDocs = com.mathworks.ide.editor.EditorApplication.getOpenDocuments;
      firstDoc = openDocs.elementAt(0);
      jEditor = firstDoc.getParent.getParent.getParent;
   catch
      error('Cannot find Matlab editor handle: possibly no open documents');
   end
end
</pre>
<p>Another complication arose due to the different layout used for floating/maximized/tiled document layout in the editor. Yet another was due to the different behavior (at least on Matlab 6) between a one-document and a multi-document editor view.<br />
Due to the way keyboard events are processed by the Matlab editor, KeyPressedCallback needs to be set separately for all the open document panes and split-panes. Since we also wish newly-opened documents to recognize the macro bindings, we need to set the common container ancestor&#8217;s ComponentAddedCallback to an internal function that will handle the KeyPressedCallback instrumentation for each newly-opened document. Again, this needs to be done somewhat differently for Matlab 6/7.<br />
Note that <em><strong>EditorMacro</strong></em> relies on the editor&#8217;s internal display layout, which is very sensitive to modification between Matlab releases (as it has between Matlab 6 and 7, for example).<br />
All-in-all, I believe <em><strong>EditorMacro</strong></em> provides a useful and long-awaited service, which should in fact be part of the built-in editor.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor">EditorMacro &#8211; assign a keyboard macro in the Matlab editor</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings" rel="bookmark" title="EditorMacro v2 &#8211; setting Command Window key-bindings">EditorMacro v2 &#8211; setting Command Window key-bindings </a> <small>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....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/accessing-the-matlab-editor" rel="bookmark" title="Accessing the Matlab Editor">Accessing the Matlab Editor </a> <small>The Matlab Editor can be accessed programmatically, for a wide variety of possible uses - this article shows how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/non-textual-editor-actions" rel="bookmark" title="Non-textual editor actions">Non-textual editor actions </a> <small>The UIINSPECT utility can be used to expand EditorMacro capabilities to non-text-insertion actions. This is how:...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/r2009b-keyboard-bindings" rel="bookmark" title="R2009b keyboard bindings">R2009b keyboard bindings </a> <small>The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its variants....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/feed</wfw:commentRss>
			<slash:comments>24</slash:comments>
		
		
			</item>
	</channel>
</rss>
