function open(name)
%OPEN	 Open files by extension.
%   OPEN NAME where NAME must contain a string, does different things
%   depending on the type of the object named by that string:
%
%   Type         Action
%   ----         ------
%   variable     open named array in Array Editor
%   .fig file    open figure in Handle Graphics
%   .m   file    open M-file in M-file Editor
%   .mdl file    open model in SIMULINK
%   .p   file    open the matching M-file if there is one
%
%   If NAME does not contain a file extension, then OPEN opens the object
%   returned by WHICH(NAME).  For example, if foo.m and foo.mdl are both
%   present on the search path, then WHICH('foo') will return foo.mdl,
%   since MDL-files take precedence over M-files, so OPEN('foo') will
%   open foo.mdl into SIMULINK.
%
%   OPEN is user-extensible.  To open a file with the extension ".XXX",
%   OPEN calls the helper function OPENXXX, that is, a function
%   named 'OPEN', with the file extension appended.
%
%   For example,
%      open('foo.m')       calls openm('foo.m')
%      open foo.m          calls openm('foo.m')
%      open myfigure.fig   calls openfig('myfigure.fig')
%
%   You can create your own OPENXXX functions to change the way standard
%   file types are handled, or to set up handlers for new file types.
%   OPEN will call whatever OPENXXX function it finds on the path.
%
%   Special case: to handle a variable, OPEN calls the function
%   OPENVAR.
%
%   If there is no matching helper function found, OPEN opens the file in
%   the user's default text editor.
%
%   If the file name is specified without an extension and there
%   is matching file name without an extension, OPEN looks for an
%   M file with the same name on the path.
%
%   For example,
%      if foo does not exist and foo.m exists on the path,
%         open foo          opens foo.m in the editor
%      if foo does exist
%         open foo          opens foo in the editor
%
%   See also SAVEAS.
%

%   Chris Portal 1-23-98
%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.10 $  $Date: 1998/10/20 20:42:31 $

error(nargchk(1,1,nargin));

[m n]=size(name);

if iscell(name) | ~ischar(name) | m~=1,
   error('NAME must contain a single string.');
end

[path,file,ext,ver]=fileparts(name);

if isempty(path) % just a filename, not a full path name
   %  see if we can find the name on the path
   [isfile, name] = LIsFile(name);
   if ~isfile & length(ext)==0,
      % either a variable or an M-file without the extension
      fullpath = evalin('caller', ['which(''' name ''')']);
      if isempty(fullpath)
         % which did not find it
         error(['File ''' name ''' not found.'])
      end
      [name, ext] = LAddExtension(fullpath, name);
   end
%else % there is a path specifier
   % it is a fully qualified name and we shouldn't mess with the
   % filename at all
end 

if length(ext)==1  % ext == '.'
   ext = '';
elseif length(ext)>1
   ext=ext(2:end);
end

% Call helper function to open file appropriately
try
   if ~isempty(ext) & ~isempty(which(['open' ext])),
      feval(['open' ext],name);
   else
      openother(name);
   end   
catch
   error(lasterr)
end



function [name, ext] = LAddExtension(fullpath, name)
switch fullpath
case 'variable'
   ext='.var';
case 'built-in'
   error(['Can''t open the built-in function ''' name '''.']);
otherwise % which has found an M file or MAT file on the path
   name = fullpath;
   [p,f,ext,v]=fileparts(fullpath);
end


function [isfile, name] = LIsFile(name)
fid = fopen(name,'r');
if fid==-1   % unable to open
   isfile = 0;
else
   isfile = 1;
   name = fopen(fid);
   fclose(fid);
   [p,f,ext,v]=fileparts(name);   
   if isempty(p)	% make sure returned name fully qualified
      name = [pwd filesep name];
   end
end


