diff scripts/miscellaneous/edit.m @ 16399:f9a737fd8829

allow command-line edit function to use GUI editor * file-editor-interface.h (file_editor_interface::handle_edit_file_request): New pure virtual function. * file-editor.h, file-editor.cc (file_editor::handle_edit_file_request): New function. * octave-event-listener.h (octave_event_listenter::edit_file): New pure virtual function. * octave-qt-event-listener.h, octave-qt-event-listener.cc (octave_qt_event_listener::edit_file): New function. (octave_qt_event_listener::edit_file_signal): New signal. * main-window.h, main-window.cc (main_window::handle_edit_file_request): New function. (main_window::construct): Connect edit_file_signal to handle_edit_file_request. * __execute_edit_hook__.cc: New file. Provide Fadd_edit_hook, Fremove_edit_hook, and F__execute_edit_hook__ functions for the interpreter. * libinterp/corefcn/module.mk (COREFCN_SRC): Include it in the list. * octave-link.h, octave-link.cc (octave_link::edit_file, octave_link::do_edit_file): New functions. * octave-main-thread.cc (edit_hook_fcn): New function. (octave_main_thread::run): Install it as an edit_hook function. * miscellaneous/edit.m (do_edit): New subfunction. Allow edit request to be intercepted by edit_hook function. (edit): Use it to uniformly handle starting the editor.
author John W. Eaton <jwe@octave.org>
date Sat, 30 Mar 2013 10:57:15 -0400
parents 5b9778df4ef1
children 81ec95768520
line wrap: on
line diff
--- a/scripts/miscellaneous/edit.m	Sat Mar 30 10:56:15 2013 +0100
+++ b/scripts/miscellaneous/edit.m	Sat Mar 30 10:57:15 2013 -0400
@@ -241,13 +241,16 @@
 
   ## Start the editor without a file if no file is given.
   if (nargin < 1)
-    if (exist (FUNCTION.HOME, "dir") == 7 && (isunix () || ! ispc ()))
-      system (cstrcat ("cd \"", FUNCTION.HOME, "\" ; ",
-                      sprintf (undo_string_escapes (FUNCTION.EDITOR), "")),
-              [], FUNCTION.MODE);
+    if (exist (FUNCTION.HOME, "dir") == 7)
+      curr_dir = pwd ();
+      unwind_protect
+        chdir (FUNCTION.HOME);
+        do_edit (FUNCTION.EDITOR, "", FUNCTION.MODE);
+      unwind_protect_cleanup
+        chdir (curr_dir);
+      end_unwind_protect
     else
-      system (sprintf (undo_string_escapes (FUNCTION.EDITOR), ""),
-              [], FUNCTION.MODE);
+      do_edit (FUNCTION.EDITOR, "", FUNCTION.MODE);
     endif
     return;
   endif
@@ -329,9 +332,7 @@
       ## If the file exists, then edit it.
       if (FUNCTION.EDITINPLACE)
         ## Edit in place even if it is protected.
-        system (sprintf (undo_string_escapes (FUNCTION.EDITOR),
-                         cstrcat ("\"", fileandpath, "\"")),
-                [], FUNCTION.MODE);
+        do_edit (FUNCTION.EDITOR, fileandpath, FUNCTION.MODE);
         return;
       else
         ## If the file is modifiable in place then edit it, otherwise make
@@ -347,9 +348,7 @@
         else
           fclose (fid);
         endif
-        system (sprintf (undo_string_escapes (FUNCTION.EDITOR),
-                         cstrcat ("\"", fileandpath, "\"")),
-                [], FUNCTION.MODE);
+        do_edit (FUNCTION.EDITOR, fileandpath, FUNCTION.MODE);
         return;
       endif
     endif
@@ -364,9 +363,7 @@
       case {"cc", "m"}
         0;
       otherwise
-        system (sprintf (undo_string_escapes (FUNCTION.EDITOR),
-                         cstrcat ("\"", fileandpath, "\"")),
-                [], FUNCTION.MODE);
+        do_edit (FUNCTION.EDITOR, fileandpath, FUNCTION.MODE);
         return;
     endswitch
 
@@ -515,10 +512,7 @@
     fputs (fid, text);
     fclose (fid);
 
-    ## Finally we are ready to edit it!
-    system (sprintf (undo_string_escapes (FUNCTION.EDITOR),
-                     cstrcat ("\"", fileandpath, "\"")),
-            [], FUNCTION.MODE);
+    do_edit (FUNCTION.EDITOR, fileandpath, FUNCTION.MODE);
             
   endif
 
@@ -564,6 +558,21 @@
 
 endfunction
 
+function do_edit (editor, file, mode)
+
+  ## Give the hook function a chance.  If that fails, fall back
+  ## on running an editor with the system function.
+
+  status = __execute_edit_hook__ (file);
+
+  if (! status)
+    system (sprintf (undo_string_escapes (editor),
+                     cstrcat ("\"", file, "\"")),
+            [], mode);
+  endif
+
+endfunction
+
 
 %!test
 %! s.editor = edit ("get", "editor");