function [] = mfiles(pth)
%MFILES(DIR) Lists the Matlab M-Files in directory DIR, with hypertext.
% If DIR is not specified, the current directory will be used.
% When MFILES displays the M-File names, they are hyperlinked so that
% clicking on a filename in the list will open it for editing.
% MFILES does not return a value.
%
% Examples:
% To see what M-Files are in the current directory, and gain easy
% access to editing them, simply type:
%
% mfiles
%
% To see what files are in the directory on path PTH, and gain easy
% access to editing them, simply type:
%
% mfiles(PTH)
%
% See also what, dir, matfiles, figfiles, whats(on the file exchange)
%
% Author: Matt Fig
% Contact: popkenai@yahoo.com
% 7/8/2008
if nargin ~= 1
pth = pwd; % User wants the the current directory.
crnt = 'current '; % This is for display purposes later.
else
crnt = ''; % For display purposes when user passed alt directory.
end
try
W = what(pth); % Use WHAT to get the files we want.
W = W(1);
catch
error('Invalid path, make sure the path entered is correct.')
end
if isempty(W.m), return, end % No M-Files in the directory.
var = sort(W.m); % Alphabetical order.
lngth = length(var); % Start to calculate how many lines to display.
mx = max(cellfun('length',var)); % Find the maximum string length.
cwsz = get(0,'commandwindowsize'); % Find out how many will fit.
vct = 1:75; % Try several different numbers to find how many lines.
vct = floor(cwsz(1)./(vct*mx + (vct-1)*3)); % ~3 spaces between strings.
nmprln = find(vct==1); % Last index is the num per line, see next comment.
nmprln = nmprln(end); % For BC to ver 6.5: no 'last' arg to find func. :(
if isempty(nmprln), nmprln=1; end % User has very small command window!
nmlns = ceil(lngth/nmprln); % This is how many lines to display.
ch = char(var); % Get a character array from the cell.
chlnk = cell(size(ch,1),1); % Build a cell with hypertext.
for mm = 1:lngth
db = deblank(ch(mm,:)); % For the html label.
db = db(1:end-2); % Drop the extention on the label.
chlnk{mm} =['<a href="matlab:','edit' ,'(deblank(',' ''',W.path,...
filesep,ch(mm,:),'''','))','">',db,'</a>',' '];
end
chlnk = char(chlnk); % Need the padding to get display spacing correct.
fprintf('\n%s\n\n',['M-Files',' in the ',crnt,'directory ', W.path,...
' (click to ','edit','):']) % Announce what is printing.
for ii = 1:nmlns
fprintf('%s\n',chlnk(ii:nmlns:lngth,:)') % Print the lines.
end
fprintf('\n') % Provide a space between list and command prompt.