📄 highlight.m
字号:
str = strrep(str,'>','>'); str = strrep(str,'"','"');%===============================================================================% XML FORMAT %%===============================================================================function xml_file_start(fid,mfile) fprintf(fid,[ ... '<?xml version="1.0" encoding="iso-8859-1" ?>\n' ... '<!DOCTYPE mfile SYSTEM "matlab.dtd">\n' ... '<mfile name="%s">\n'],mfile);%-------------------------------------------------------------------------------function xml_file_end(fid) fprintf(fid,'</mfile>');%-------------------------------------------------------------------------------function format = xml_format format.string = '<string>%s</string>'; format.comment = '<comment>%s</comment>'; format.code = '%s'; %'<code>%s</code>'; format.keyword = '<keyword>%s</keyword>'; format.cont = '<cont>%s</cont>'; format.pre_start = ''; format.pre_end = ''; format.nb_line = '<line nb="%04d ">'; format.line = '%s</line>\n';%-------------------------------------------------------------------------------function str = xml_special_char(str) %- See http://www.w3.org/TR/REC-xml#sec-predefined-ent str = strrep(str,'&','&'); str = strrep(str,'<','<'); str = strrep(str,'>','>'); str = strrep(str,'"','"'); %str = strrep(str,'''','''); %===============================================================================% LaTeX FORMAT %%===============================================================================function tex_file_start(fid,mfile) fprintf(fid,['\\documentclass[a4paper,10pt]{article}\n' ... ' \\usepackage{alltt}\n' ... ' \\usepackage{color}\n' ... ' \\usepackage{fullpage}\n' ... ' \\definecolor{string}{rgb}{0.7,0.0,0.0}\n' ... ' \\definecolor{comment}{rgb}{0.13,0.54,0.13}\n' ... ' \\definecolor{keyword}{rgb}{0.0,0.0,1.0}\n' ... ' \\title{%s}\n' ... ' \\author{\\textsc{Matlab}, The Mathworks, Inc.}\n' ... '\\begin{document}\n' ... '\\maketitle\n'],mfile);%-------------------------------------------------------------------------------function tex_file_end(fid) fprintf(fid,'\\end{document}');%-------------------------------------------------------------------------------function format = tex_format format.string = '\\textcolor{string}{%s}'; format.comment = 'textcolor{comment}{%s}'; % '%' has been replaced by '\%' format.code = '%s'; format.keyword = '\\textcolor{keyword}{%s}'; format.cont = '\\textcolor{keyword}{\\underline{%s}}'; format.pre_start = '\\begin{alltt}\n'; format.pre_end = '\\end{alltt}\n'; format.nb_line = '%04d '; format.line = '%s\n';%-------------------------------------------------------------------------------function str = tex_special_char(str) % Special characters: # $ % & ~ _ ^ \ { } str = strrep(str,'\','\(\backslash\)'); % $\backslash$ or \textbackslash or \verb+\+ str = strrep(str,'#','\#'); str = strrep(str,'$','\$'); str = strrep(str,'%','\%'); str = strrep(str,'&','\&'); str = strrep(str,'_','\_'); str = strrep(str,'{','\{'); str = strrep(str,'}','\}'); str = strrep(str,'^','\^{}'); str = strrep(str,'~','\~{}'); % or \textasciitilde%===============================================================================% RTF FORMAT %%===============================================================================function rtf_file_start(fid,mfile) fprintf(fid,['{\\rtf1\\ansi\n\n' ... '{\\fonttbl{\\f0\\fmodern\\fcharset0\\fprq1 Courier New;}}\n\n' ... '{\\colortbl;\n' ... '\\red0\\green0\\blue0;\n' ... '\\red0\\green0\\blue255;\n' ... '\\red33\\green138\\blue33;\n' ... '\\red178\\green0\\blue0;}\n\n' ... '{\\info{\\title %s}\n' ... '{\\author HighLight.m Copyright 2003}\n' ... '{\\creatim\\yr%s\\mo%s\\dy%s}' ... '{\\*\\manager Guillaume Flandin}\n' ... '{\\*\\company Artefact.tk}\n' ... '{\\*\\hlinkbase http://www.madic.org/download/' ... 'matlab/highlight/}}\n\n'],mfile,... datestr(date,'yyyy'),datestr(date,'mm'),datestr(date,'dd'));%-------------------------------------------------------------------------------function rtf_file_end(fid) fprintf(fid,'}');%-------------------------------------------------------------------------------function format = rtf_format format.string = '{\\cf4 %s}'; format.comment = '{\\cf3 %s}'; format.code = '{%s}'; format.keyword = '{\\cf2 %s}'; format.cont = '{\\cf2 %s}'; format.pre_start = '{\\f0\\fs16{'; format.pre_end = '}}'; format.nb_line = '{%04d }'; format.line = '%s\n\\par ';%-------------------------------------------------------------------------------function str = rtf_special_char(str) str = strrep(str,'\','\\'); str = strrep(str,'{','\{'); str = strrep(str,'}','\}');%===============================================================================% MATLAB KEYWORDS %%===============================================================================function matlabKeywords = getMatlabKeywords %matlabKeywords = iskeyword; % Matlab R13 matlabKeywords = {'break', 'case', 'catch', 'continue', 'elseif', 'else',... 'end', 'for', 'function', 'global', 'if', 'otherwise', ... 'persistent', 'return', 'switch', 'try', 'while'}; %-------------------------------------------------------------------------------function mKeySort = getMatlabKeywordsSorted mKeySort.nextincr = {'for', 'while', 'if', 'else', 'elseif', ... 'case', 'otherwise', 'try', 'catch'}; mKeySort.nextincr2 = {'switch'}; mKeySort.currdecr = {'else', 'elseif', 'case', 'otherwise', 'catch'}; mKeySort.nextdecr = {'end'}; mKeySort.other = {'break', 'continue', 'function', 'global', ... 'persistent', 'return'};%===============================================================================% HANDLE TAB CHARACTER %%===============================================================================function str = horztab(str,n) %- For browsers, the horizontal tab character is the smallest non-zero %- number of spaces necessary to line characters up along tab stops that are %- every 8 characters: behaviour obtained when n = 0. if n > 0 str = strrep(str,sprintf('\t'),blanks(n)); end%===============================================================================% MATLAB CODE PARSER %%===============================================================================function splitc = splitcode(code) %Split a line of Matlab code in string, comment and other %- Label quotes in {'transpose', 'beginstring', 'midstring', 'endstring'} iquote = findstr(code,''''); quotetransp = [double('_''.)}]') ... double('A'):double('Z') ... double('0'):double('9') ... double('a'):double('z')]; flagstring = 0; flagdoublequote = 0; jquote = []; for i=1:length(iquote) if ~flagstring if iquote(i) > 1 & any(quotetransp == double(code(iquote(i)-1))) % => 'transpose'; else % => 'beginstring'; jquote(size(jquote,1)+1,:) = [iquote(i) length(code)]; flagstring = 1; end else % if flagstring if flagdoublequote | ... (iquote(i) < length(code) & strcmp(code(iquote(i)+1),'''')) % => 'midstring'; flagdoublequote = ~flagdoublequote; else % => 'endstring'; jquote(size(jquote,1),2) = iquote(i); flagstring = 0; end end end %- Find if a portion of code is a comment ipercent = findstr(code,'%'); jpercent = []; for i=1:length(ipercent) if isempty(jquote) | ... ~any((ipercent(i) > jquote(:,1)) & (ipercent(i) < jquote(:,2))) jpercent = [ipercent(i) length(code)]; break; end end %- Find continuation punctuation '...' icont = findstr(code,'...'); for i=1:length(icont) if (isempty(jquote) | ... ~any((icont(i) > jquote(:,1)) & (icont(i) < jquote(:,2)))) & ... (isempty(jpercent) | ... icont(i) < jpercent(1)) jpercent = [icont(i) length(code)]; break; end end %- Remove strings inside comments if ~isempty(jpercent) & ~isempty(jquote) jquote(find(jquote(:,1) > jpercent(1)),:) = []; end %- Split code in a cell array of strings icode = [jquote ; jpercent]; splitc = {}; if isempty(icode) splitc{1} = code; elseif icode(1,1) > 1 splitc{1} = code(1:icode(1,1)-1); end for i=1:size(icode,1) splitc{end+1} = code(icode(i,1):icode(i,2)); if i < size(icode,1) & icode(i+1,1) > icode(i,2) + 1 splitc{end+1} = code((icode(i,2)+1):(icode(i+1,1)-1)); elseif i == size(icode,1) & icode(i,2) < length(code) splitc{end+1} = code(icode(i,2)+1:end); end end %===============================================================================% MODIFIED VERSION OF STRTOK %%===============================================================================function [token, remainder, quotient] = strtok(string, delimiters)% Modified version of STRTOK to also return the quotient% string = [quotient token remainder]%STRTOK Find token in string.% STRTOK(S) returns the first token in the string S delimited% by "white space". Any leading white space characters are ignored.%% STRTOK(S,D) returns the first token delimited by one of the % characters in D. Any leading delimiter characters are ignored.%% [T,R] = STRTOK(...) also returns the remainder of the original% string.% If the token is not found in S then R is an empty string and T% is same as S. %% Copyright 1984-2002 The MathWorks, Inc. % $Revision: 5.14 $ $Date: 2002/04/09 00:33:38 $token = []; remainder = []; quotient = string;len = length(string);if len == 0 returnendif (nargin == 1) delimiters = [9:13 32]; % White space charactersendi = 1;while (any(string(i) == delimiters)) i = i + 1; if (i > len), return, endendstart = i;while (~any(string(i) == delimiters)) i = i + 1; if (i > len), break, endendfinish = i - 1;token = string(start:finish);if (nargout >= 2) remainder = string(finish + 1:length(string));endif (nargout == 3 & start > 1) quotient = string(1:start-1);else quotient = [];end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -