octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bug in edit.m


From: Ben Abbott
Subject: Re: bug in edit.m
Date: Wed, 16 Jan 2008 22:03:16 -0500


On Jan 15, 2008, at 8:56 PM, John W. Eaton wrote:

On 15-Jan-2008, Ben Abbott wrote:

|
| On Jan 15, 2008, at 7:12 PM, John W. Eaton wrote:
|
| > On 15-Jan-2008, Ben Abbott wrote:
| >
| > | Actually, I guess I'd prefer all cases be left to the editor to  
| > decide
| > | what to do.
| >
| > In that case, you would just always pass the filename (whatever it is,
| > presumably after looking for it in the path if it is not an absolute
| > file name) and let the editor have its way.  Then we wouldn't need to
| > worry about FUNCTION.HOME or opening in the current directory, or
| > permissions or anything else.
| >
| > jwe
|
| Something like the sequence below?
|
| (1) Search the path for the explicit file (do not append .m)
| (2) If no file, the append the ".m" and search again.
| (3) if file exists in the path, append its path to the filename.
| (4) Pass the result to the editor.

OK, how about the following (here, F is the given file name):

 files = {f};
 if (isempty (regexp (f, "\\.m$")))
   files{2} = strcat (f, ".m");
 endif
 file_to_edit = file_in_loadpath (files);
 if (isempty (file_to_edit))
   file_to_edit = f;
 endif

If we are searching for foo and foo.m, the above will only search the
path once, looking in each directory for foo first, then for foo.m,
returning the first one found.

Except that you might want to deal with absolute or rooted relative
file names first.  There are internal functions in Octave that decide
whether a file name is absolute or rooted relative (declared in
liboctave/oct-env.h), but they are not exported to the scripting
language.  Maybe they should be.  Or maybe it would be better to fix
file_in_loadpath to properly handle absolute and rooted relative
names.

Also, if a user asks to edit foo.dat, should we avoid looking for
foo.dat.m? In that case, I guess the pattern match should just be to
see if there is a "." anywhere in the name.

jwe

Please review and comment.

2008-01-16  Ben Abbott <address@hidden>

* miscellaneous/edit.m: Changed behavior to be consistent with Matlab.
Open all existing files in place. New files are opened in the pwd.


Index: edit.m
===================================================================
RCS file: /cvs/octave/scripts/miscellaneous/edit.m,v
retrieving revision 1.3
diff -u -r1.3 edit.m
--- edit.m 29 Dec 2007 00:30:20 -0000 1.3
+++ edit.m 17 Jan 2008 02:56:11 -0000
@@ -27,28 +27,25 @@
 ## 
 ## @itemize @bullet
 ## @item
-## If the function @var{name} is available in a file on your path and
-## that file is modifiable, then it will be edited in place.  If it 
-## is a system function, then it will first be copied to the directory
-## @code{HOME} (see further down) and then edited.  
+## If the function @var{name} is available in a file on your path it 
+## will be edited in place.  
 ##
 ## @item
-## If @var{name} is the name of a function defined in the interpreter but 
-## not in an m-file, then an m-file will be created in @code{HOME}
+## If @var{name} is the name of a function defined in the interpreter 
+## but not in an m-file, then an m-file will be created in @code{PWD}
 ## to contain that function along with its current definition.  
 ##
 ## @item
 ## If @code{name.cc} is specified, then it will search for @code{name.cc}
 ## in the path and try to modify it, otherwise it will create a new
-## @file{.cc} file in @code{HOME}.  If @var{name} happens to be an
+## @file{.cc} file in @code{PWD}.  If @var{name} happens to be an
 ## m-file or interpreter defined function, then the text of that
 ## function will be inserted into the .cc file as a comment.
 ##
 ## @item
-## If @var{name.ext} is on your path then it will be editted, otherwise
-## the editor will be started with @file{HOME/name.ext} as the
-## filename.  If @file{name.ext} is not modifiable, it will be copied to
-## @code{HOME} before editing.
+## If @var{name.ext} is on your path then it will be edited, otherwise
+## the editor will be started with @file{./name.ext} as the
+## filename.
 ##
 ## @strong{WARNING!} You may need to clear name before the new definition
 ## is available.  If you are editing a .cc file, you will need
@@ -185,47 +182,31 @@
       error ("unable to edit a built-in or compiled function");
   endswitch
 
-  ## Find file in path.
-  idx = rindex (file, ".");
-  if (idx != 0)
-    ## If file has an extension, use it.
-    path = file_in_loadpath (file);
-  else
-    ## Otherwise try file.cc, and if that fails, default to file.m.
-    path = file_in_loadpath (strcat (file, ".cc"));
-    if (isempty (path))
-      file = strcat (file, ".m");
-      path = file_in_loadpath (file);
+  ## If no path info, and no ".m" look for both the file and file+".m"
+  if !(any (strfind (file, filesep)) || \
+      (any (strfind (file, ".")) && isempty (regexp (file, "\\.m$"))))
+    files = {file};
+    if (isempty (regexp (file, "\\.m$")))
+      ## If the name is not explicity an m-file, create a list with each.
+      files{2} = strcat (file, ".m");
+    endif
+    ## File without the ".m" exists, it has precidence over the ".m" version.
+    file_to_edit = file_in_loadpath (files);
+    if (numel (file_to_edit))
+      file = file_to_edit;
     endif
   endif
 
-  ## If the file exists and is modifiable in place then edit it,
-  ## otherwise copy it and then edit it.
-  if (! isempty (path))
-    fid = fopen (path, "r+t");
-    if (fid < 0)
-      from = path;
-      path = strcat (FUNCTION.HOME, from (rindex (from, filesep):end))
-      [status, msg] = copyfile (from, path, 1);
-      if (status == 0)
-        error (msg);
-      endif
-    else
-      fclose(fid);
-    endif
-    system (sprintf (FUNCTION.EDITOR, strcat ("\"", path, "\"")),
-    [], FUNCTION.MODE);
+  ## If the file exists, edit it.
+  if exist (file)
+    system (sprintf (FUNCTION.EDITOR, strcat ("\"", file, "\"")),
+            [], FUNCTION.MODE);
     return;
   endif
 
   ## If editing something other than a m-file or an oct-file, just
   ## edit it.
-  idx = rindex (file, filesep);
-  if (idx != 0)
-    path = file;
-  else
-    path = fullfile (FUNCTION.HOME, file);
-  endif
+  path = file;
   idx = rindex (file, ".");
   name = file(1:idx-1);
   ext = file(idx+1:end);
@@ -373,6 +354,8 @@
       text = strcat (comment, body);
   endswitch
 
+path
+file
   ## Write the initial file (if there is anything to write)
   fid = fopen (path, "wt");
   if (fid < 0)



Attachment: edit.patch
Description: Binary data

Attachment: edit.ChangeLog
Description: Binary data



reply via email to

[Prev in Thread] Current Thread [Next in Thread]