how to set matlab built in Editors Callback

9 views (last 30 days)
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
  3 Comments
Andreas Justin
Andreas Justin on 25 Mar 2013
Edited: Andreas Justin on 25 Mar 2013
following code allows to override the edit function. I have it in the startup file
if overloadEDIT
Matlab_extendEditorFunctionality(true)
addpath(matlabroot);
fid = fopen(fullfile(matlabroot,'edit.m'),'w');
fprintf(fid,['function edit(str)',char(13), 'if nargin < 1 || isempty(str)',char(13),...
'str='''';',char(13),'end',char(13), 'rmpath(matlabroot);',char(13),...
'edit(str);',char(13), 'addpath(matlabroot);',char(13),'Matlab_extendEditorFunctionality(true);']);
fclose(fid);
else
warning('off')
rmpath(matlabroot);
delete(fullfile(matlabroot,'edit.m'))
warning('on')
end

Sign in to comment.

Accepted Answer

Andreas Justin
Andreas Justin on 22 Mar 2013
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

More Answers (1)

Andreas Justin
Andreas Justin on 12 Mar 2013
Edited: Andreas Justin on 12 Mar 2013
it's nearly that what i want. it works with this 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/
ToDo:
  • apply on all opened Editor
  • run at startup -> solution startup doc
  • apply on newly opened edtiors -> solution? edit edit function
  • EDIT: { on active editor (file) }
function hEditorPane = getEditor
jEditor = com.mathworks.mde.desk.MLDesktop.getInstance.getGroupContainer('Editor');
jMainPane = [];
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
hEditorPane = [];
jSyntaxTextPaneView = getDescendent(jMainPane.getComponent(0),[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
function child = getDescendent(child,listOfChildrenIdx)
if ~isempty(listOfChildrenIdx)
child = getDescendent(child.getComponent(listOfChildrenIdx(1)),listOfChildrenIdx(2:end));
end
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set(hEditorPane ,'KeyTypedCallback',@(src,evnt)doSth_FUNCTION(src,evnt))
  1 Comment
Andreas Justin
Andreas Justin on 21 Mar 2013
function hEditorPaneArray = getEditor
% desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
% jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor;
% jEditor.uiinspect
% clientTitles = desktop.getClientTitles;
% for ii = 1:numel(clientTitles)
% editorHandles{ii,1} = char(clientTitles(ii));
% end
% editorHandles(~Aid_SearchCell(editorHandles,'D\:\\')) = [];
% for ii = 1:numel(editorHandles)
% ed = desktop.getClient(editorHandles{ii});
% xEdView = ed.getComponent(0).getComponent(0)% getViewport.getComponent(0)
% h_ed = handle(xEdView,'CallbackProperties');
% set(h_ed, 'MouseEnteredCallback', {@disp,1});
% % set(h_cw, 'KeyPressedCallback', {@MyMatlabFunction,extraParam1}); %alternative
% end
%
% % You were fairly close:
% mde = com.mathworks.mde.desk.MLDesktop.getInstance;
% cw = mde.getClient('Command Window');
% xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
% h_cw = handle(xCmdWndView,'CallbackProperties');
% set(h_cw, 'KeyPressedCallback', @test1);
% set(h_cw, 'KeyPressedCallback', '');
% set(h_cw, 'KeyPressedCallback', {@MyMatlabFunction,extraParam1}...}); %alternative
% aE = matlab.desktop.editor.getAll;
% filenames = {aE.Filename}';
% aE.close
% for ii = 1:numel(filenames)
% edit(filenames{ii})
% end
jMainPane = [];
edit(aE(ii).Filename)
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
%set(hEditorPane ,'KeyTypedCallback',@(src,evnt)disp(1))
hEditorPaneArray(jj) = hEditorPane;
end
function child = getDescendent(child,listOfChildrenIdx)
if ~isempty(listOfChildrenIdx)
child = getDescendent(child.getComponent(listOfChildrenIdx(1)),listOfChildrenIdx(2:end));
end

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!