Wok*_*Wok 8 matlab editor save trailing removing-whitespace
我没有在Matlab 2012b中找到一个基本功能:
Remove trailing whitespaces on save.
Run Code Online (Sandbox Code Playgroud)
有关:
我没有足够的声誉来评论,但我在github上创建了一个Gist,以更新Sam Roberts的答案:
它将记住您的最后一个光标位置/选择,并在删除尾随空格后选择返回.
我还删除了编辑器末尾的所有尾随空白行.
我觉得它对于较长的文件非常有用
https://gist.github.com/hmaarrfk/8462415
% To remove a Matlab trailing whitespace in the editor
% Original Author: Sam Roberts
% http://stackoverflow.com/questions/19770347/how-to-auto-remove-trailing-whitespaces-on-save-in-matlab
% Modified by Mark Harfouche to remember cursor location
%
%
% Temp variable for shortcut. Give it an unusual name so it's unlikely to
% conflict with anything in the workspace.
shtcutwh__ = struct;
% Check that the editor is available.
if ~matlab.desktop.editor.isEditorAvailable
return
end
% Check that a document exists.
shtcutwh__.activeDoc = matlab.desktop.editor.getActive;
if isempty(shtcutwh__.activeDoc)
return
end
% save the old cursor location
shtcutwh__.Selection = shtcutwh__.activeDoc.Selection;
% Get the current text.
shtcutwh__.txt = shtcutwh__.activeDoc.Text;
% Remove trailing whitespace from each line.
shtcutwh__.lines = deblank(regexp(shtcutwh__.txt,'[^\n]*(\n)|[^\n]*$', 'match'));
% remove the trailing blank lines
for n = length(shtcutwh__.lines):-1:1
if length(shtcutwh__.lines{n}) == 0
shtcutwh__.lines(n) = [];
else
break
end
end
% Reconcatenate lines.
shtcutwh__.addNewline = @(x)sprintf('%s\n',x);
shtcutwh__.lines = cellfun(shtcutwh__.addNewline, shtcutwh__.lines, 'UniformOutput', false);
% If you always want to add a newline at the end of the file, comment this line out
% Remove the last newline character
shtcutwh__.lines{end}(end) = '';
shtcutwh__.newtxt = horzcat(shtcutwh__.lines{:});
% Set the current text.
shtcutwh__.activeDoc.Text = shtcutwh__.newtxt;
% Place the cursor back
shtcutwh__.activeDoc.Selection = shtcutwh__.Selection;
% Delete temp variable.
clear shtcutwh__
Run Code Online (Sandbox Code Playgroud)
我有同样的需要,写了一个小脚本来做一些接近的事情.将以下内容放在MATLAB桌面快捷方式中.每当您单击快捷按钮时,它将从编辑器中的活动文件中删除尾随空格.不如在保存时自动执行它 - 你需要记住在保存之前按下按钮 - 但差不多.测试了11b,12a和13b,但在12b也应该没问题.
希望有所帮助!
% Temp variable for shortcut. Give it an unusual name so it's unlikely to
% conflict with anything in the workspace.
shtcutwh__ = struct;
% Check that the editor is available.
if ~matlab.desktop.editor.isEditorAvailable
return
end
% Check that a document exists.
shtcutwh__.activeDoc = matlab.desktop.editor.getActive;
if isempty(shtcutwh__.activeDoc)
return
end
% Get the current text.
shtcutwh__.txt = shtcutwh__.activeDoc.Text;
% Remove trailing whitespace from each line.
shtcutwh__.lines = deblank(regexp(shtcutwh__.txt,'[^\n]*(\n)|[^\n]*$', 'match'));
% Reconcatenate lines.
shtcutwh__.addNewline = @(x)sprintf('%s\n',x);
shtcutwh__.lines = cellfun(shtcutwh__.addNewline, shtcutwh__.lines, 'UniformOutput', false);
shtcutwh__.newtxt = horzcat(shtcutwh__.lines{:});
% Set the current text.
shtcutwh__.activeDoc.Text = shtcutwh__.newtxt;
% Delete temp variable.
clear shtcutwh__
Run Code Online (Sandbox Code Playgroud)