Is it possible to programmatically add a file extension for syntax highlighting?

2 次查看(过去 30 天)
Hi,
Is it possible to programmatically add a file extension for syntax highlighting?
Just for the sake of argument, suppose I have a file with extension "xtn". It is possible to go into
preference-->editor/Debugger-->language
and from there add extension xtn under the block "file extensions". Then matlab will apply the same syntax coloring it uses for files with extension .m to all files with extension xtn.
Is there any way to do this programmatically instead of manually?
Thanks

回答(1 个)

Jan
Jan 2015-10-24
编辑:Jan 2015-10-24
You can do it the hard way:
Open the file prefdir\matlab.prf. Search for the variable "Editor.Language.MATLAB.Extensions" and append 'xtn'.
Ext = 'xtn';
PrefFile = fullfile(prefdir, 'matlab.prf');
copyfile(PrefFile, [PrefFile, '.bak']); % To be secure
fid = fopen(PrefFile, 'r');
if fid == -1
error('Cannot open file for reading: %s', PrefFile);
end
C = textscan(fid, '%s', 'Delimiter', '\n');
Data = C{1};
fclose(fid);
key = 'Editor.Language.MATLAB.Extensions';
index = strncmp(Data, key, length(key));
if sum(index) ~= 1
error('Cannot identify key line.');
end
keyLine = Data{index};
keyData = keyLine(length(key) + 3:end); % <key>=S...
keyValue = regexp(keyData, ';', 'split');
if ~any(strcmpi(keyValue, Ext))
keyData = [keyData, ';', Ext];
end
Data{index} = [key, '=S', keyData];
% Save the file:
fid = fopen(PrefFile, 'w');
if fid == -1
error('Cannot open file for writing: %s', PrefFile);
end
fprintf(fid, '%s\n', Data{:});
fclose(fid);
Afterwards restart Matlab:
!matlab
quit
Or you use the smart, but undocumented methods:
key = 'Editor.Language.MATLAB.Extensions';
keyData = com.mathworks.services.Prefs.getPref(key);
keyValue = regexp(keyData, ';', 'split');
if ~any(strcmpi(keyValue, Ext))
keyData = [keyData, ';', Ext];
com.mathworks.services.Prefs.setStringPref(key, keyData);
end
This worked 2007 (see http://mathworks.com/matlabcentral/newsreader/view_thread/154608) and in R2011b. But as all undocumented methods this might change in the future.
Finally, let me mention, that this does not look like a useful method to me. I guess that it is easier to change the file extension from the concerned files.
  2 个评论
Patrick Mboma
Patrick Mboma 2015-10-24
Dear Jan,
Thanks for the answer. What I am hoping for is something simple that I can turn on and off straight from the command window.
It could be something like
add_extension('xtn')
remove_extension('xtn')
Of course if it is too complicated, which would be a pity, I would simply have to revert to the textbook way of doing it.
Jan
Jan 2015-10-24
编辑:Jan 2015-10-24
Too complicated? I've posted 2 solutions already. So simply copy&paste one of them into a function called "add_extension". All you have to modify for "remove_extension" is:
match = strcmpi(keyValue, Ext);
if any(match)
keyValue(match) = [];
keyData = sprintf('%s;', keyValue{:});
keyData(end) = [];
com.mathworks.services.Prefs.setStringPref(key, keyData);
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by