MATLAB: How to set matlab built in Editors Callback

callbackeditorkeypressedcallbackkeytypedcallbackMATLAB

I like to extend the matlab editor with a simple function.
If i press "(" the a function is called which automatically inserts ")" and move the cursor one to the left.
I tried to figure it out like it's done with the command window here: http://www.mathworks.com/matlabcentral/newsreader/view_thread/257842View thread
Matlab Version 8
EDIT{ it's nearly that what i want. it works with the code below (at least on Matlab 8)
i used some functios of Yair Altmans editor macro to get the hEditorPane: http://UndocumentedMatlab.com/blog/EditorMacro/
Done:
  • apply on all opened Editor (getAll Active Editors)
  • run at startup -> solution startup doc
  • apply on newly opened edtiors
I'll Test this function and extend it. if it's stable I'll post it in the file exchange. For now feel free to copy 'n' paste it

Best Answer

function Matlab_extendEditorFunctionality(activateFlg)
%#ok<*NASGU>
%#ok<*AGROW>
%%--------------------------------------------------------------------------------------------

if nargin < 1 || isempty(activateFlg)
activateFlg = false;
activateFlg = true;
end
%%--------------------------------------------------------------------------------------------
jMainPane = [];
jEditor = com.mathworks.mde.desk.MLDesktop.getInstance.getGroupContainer('Editor');
for childIdx = 1 : jEditor.getComponentCount
componentClassName = regexprep(jEditor.getComponent(childIdx-1).class,'.*\.','');
if any(strcmp(componentClassName,{'DTMaximizedPane','DTFloatingPane','DTTiledPane'}))
jMainPane = jEditor.getComponent(childIdx-1);
break;
end
end
if isa(jMainPane,'com.mathworks.mwswing.desk.DTFloatingPane')
jMainPane = jMainPane.getComponent(0); % a com.mathworks.mwswing.desk.DTFloatingPane$2 object
end
for jj = 1:jMainPane.getComponentCount
hEditorPane = [];
jSyntaxTextPaneView = getDescendent(jMainPane.getComponent(jj-1),[0,0,1,0,0,0,0]);
if isa(jSyntaxTextPaneView,'com.mathworks.widgets.SyntaxTextPaneMultiView$1')
hEditorPane(1) = handle(getDescendent(jSyntaxTextPaneView.getComponent(1),[1,0,0]),'CallbackProperties');
hEditorPane(2) = handle(getDescendent(jSyntaxTextPaneView.getComponent(2),[1,0,0]),'CallbackProperties');
else
jEditorPane = getDescendent(jSyntaxTextPaneView,[1,0,0]);
hEditorPane = handle(jEditorPane,'CallbackProperties');
end
if activateFlg
set(hEditorPane ,'KeyTypedCallback',@(src,evnt)Editor_KeyTypedCallback(src,evnt))
else
set(hEditorPane ,'KeyTypedCallback','')
end
hEditorPaneArray(jj) = hEditorPane;
end
function child = getDescendent(child,listOfChildrenIdx)
if ~isempty(listOfChildrenIdx)
child = getDescendent(child.getComponent(listOfChildrenIdx(1)),listOfChildrenIdx(2:end));
end
function Editor_KeyTypedCallback(~,evnt, varargin)
keyPress = get(evnt,'keyChar');
if strcmp(keyPress,'(') || strcmp(keyPress,'"') || ...
strcmp(keyPress,'[') || strcmp(keyPress,'''') || ...
strcmp(keyPress,'{')
aE = matlab.desktop.editor.getActive;
pos = aE.Selection;
switch keyPress
case '('
putIn = ')';
case '"'
putIn = '"';
case '['
putIn = ']';
case '{'
putIn = '}';
case ''''
putIn = '''';
end
cursorPos = matlab.desktop.editor.positionInLineToIndex(aE, pos(1),pos(2));
% MyFunction(Input1,input2) if you missed a '(' it disables the auto input otherwise you would have to delete the
% closing bracket.
if any(strcmp(aE.Text(cursorPos-1),{'(','[','{','''','"'})) && ...
~isempty(regexp(aE.Text(cursorPos),['(\s|',char(13),')'],'match'))
else
putIn = '';
end
aE.insertTextAtPositionInLine(putIn,pos(1),pos(2));
aE.goToPositionInLine(pos(1),pos(2));
end