changeset 26914:b09941690b65

edit.m: allow HOME to be empty, only use if set and exists (bug #41278) * edit.m: Allow HOME to be set to empty to disable using it. Only use HOME if it is not empty, it exists, and EDITINPLACE is false. Prefer "chdir" instead of "cd" for clarity. Defer tilde expansion until HOME directory is needed.
author Mike Miller <mtmiller@octave.org>
date Thu, 14 Mar 2019 16:36:41 -0700
parents 8efea0dcccd4
children 5ef01ab5c9e5
files scripts/miscellaneous/edit.m
diffstat 1 files changed, 14 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/miscellaneous/edit.m	Wed Mar 13 01:13:53 2019 -0400
+++ b/scripts/miscellaneous/edit.m	Thu Mar 14 16:36:41 2019 -0700
@@ -165,9 +165,6 @@
         error ("Octave:deprecated-function",
                "The EDITOR option of edit has been removed.  Use EDITOR() directly.");
       case "HOME"
-        if (! isempty (stateval) && stateval(1) == "~")
-          stateval = [ get_home_directory, stateval(2:end) ];
-        endif
         FUNCTION.HOME = stateval;
         return;
       case "AUTHOR"
@@ -218,15 +215,23 @@
     endif
   endif
 
+  ## Only use the legacy "HOME" directory if the user explicitly configured
+  ## it and if the directory exists.  In previous versions of Octave, HOME
+  ## was ~/octave by default and edited functions were copied into ~/octave.
+  ## Now 'edit_file_in_place' should be true by default unless the user
+  ## opts in by setting "EDITINPLACE" to false and "HOME" to a directory.
+  edit_file_in_place = (FUNCTION.EDITINPLACE || isempty (FUNCTION.HOME)
+                        || ! isfolder (FUNCTION.HOME));
+
   ## Start the editor without a file if no file is given.
   if (nargin == 0)
-    if (isfolder (FUNCTION.HOME))
+    if (! edit_file_in_place && ! strcmp (FUNCTION.HOME, "."))
       curr_dir = pwd ();
       unwind_protect
-        cd (FUNCTION.HOME);
+        chdir (FUNCTION.HOME);
         do_edit (FUNCTION.EDITOR, "", FUNCTION.MODE);
       unwind_protect_cleanup
-        cd (curr_dir);
+        chdir (curr_dir);
       end_unwind_protect
     else
       do_edit (FUNCTION.EDITOR, "", FUNCTION.MODE);
@@ -307,7 +312,7 @@
 
     if (! isempty (fileandpath))
       ## If the file exists, then edit it.
-      if (FUNCTION.EDITINPLACE)
+      if (edit_file_in_place)
         ## Edit in place even if it is protected.
         do_edit (FUNCTION.EDITOR, fileandpath, FUNCTION.MODE);
         return;
@@ -317,7 +322,8 @@
         fid = fopen (fileandpath, "r+t");
         if (fid < 0)
           from = fileandpath;
-          fileandpath = [FUNCTION.HOME, from(rindex(from, filesep):end)];
+          [~, fname, ext] = fileparts (from);
+          fileandpath = fullfile (tilde_expand (FUNCTION.HOME), [fname, ext]);
           [status, msg] = copyfile (from, fileandpath, 1);
           if (status == 0)
             error (msg);