changeset 18318:770c525a1a2b gui-release

Warn when saving/running a script whose name is not a valid identifier. * file-editor-tab.h: rename save_file_check_spaces * file-editor-tab.cc (check_valid_identifier): renamed save_file_check_spaces, check if valid identifier and adapt message; (handle_save_file_as_answer, handle_save_file_as_answer_close): use this function * main-window.cc (run_file_in_terminal): use the function
author Julien Bect <julien.bect@supelec.fr>
date Tue, 21 Jan 2014 18:11:50 +0100
parents 6e8188effddf
children 1ecfcfa4858e
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h libgui/src/main-window.cc
diffstat 3 files changed, 19 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Sun Jan 19 08:25:58 2014 +0100
+++ b/libgui/src/m-editor/file-editor-tab.cc	Tue Jan 21 18:11:50 2014 +0100
@@ -57,6 +57,7 @@
 #include "debug.h"
 #include "octave-qt-link.h"
 #include "version.h"
+#include "utils.h"
 
 // Make parent null for the file editor tab so that warning
 // WindowModal messages don't affect grandparents.
@@ -1228,16 +1229,20 @@
 }
 
 bool
-file_editor_tab::save_file_check_spaces (QString file_name)
+file_editor_tab::check_valid_identifier (QString file_name)
 {
-  QFileInfo file = QFileInfo(file_name);
+  QFileInfo file = QFileInfo (file_name);
+  QString base_name = file.baseName ();
 
-  if (file.suffix () == "m" && file.baseName ().contains (' '))
+  if ((file.suffix () == "m")
+      && (! valid_identifier (base_name.toStdString ())))
     {
       int ans = QMessageBox::question (0, tr ("Octave Editor"),
-         tr ("It is not advisable to save an Octave script\n"
-              "in a file with a name containing spaces.\n\n"
-              "Do you want to choose another name?"),
+         tr ("\"%1\"\n"
+             "is not a valid identifier.\n\n"
+             "If you keep this file name, you will not be able to\n"
+             "call your script using its name as an Octave command.\n\n"
+             "Do you want to choose another name?").arg (base_name),
           QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
 
       if (ans == QMessageBox::Yes)
@@ -1258,7 +1263,7 @@
   else
     {
       // Have editor check for conflict, do not delete tab after save.
-      if (save_file_check_spaces (saveFileName))
+      if (check_valid_identifier (saveFileName))
         save_file_as (false);
       else
         emit editor_check_conflict_save (saveFileName, false);
@@ -1272,7 +1277,7 @@
   // when we close a tab and _file_name is not a valid file name yet
 
   // Have editor check for conflict, delete tab after save.
-  if (save_file_check_spaces (saveFileName))
+  if (check_valid_identifier (saveFileName))
     save_file_as (true);
   else
     emit editor_check_conflict_save (saveFileName, true);
--- a/libgui/src/m-editor/file-editor-tab.h	Sun Jan 19 08:25:58 2014 +0100
+++ b/libgui/src/m-editor/file-editor-tab.h	Tue Jan 21 18:11:50 2014 +0100
@@ -180,7 +180,7 @@
   bool valid_file_name (const QString& file=QString ());
   void save_file (const QString& saveFileName, bool remove_on_success = false);
   void save_file_as (bool remove_on_success = false);
-  bool save_file_check_spaces (QString file_name);
+  bool check_valid_identifier (QString file_name);
 
   void update_lexer ();
   void request_add_breakpoint (int line);
--- a/libgui/src/main-window.cc	Sun Jan 19 08:25:58 2014 +0100
+++ b/libgui/src/main-window.cc	Tue Jan 21 18:11:50 2014 +0100
@@ -60,6 +60,7 @@
 #include "defaults.h"
 #include "symtab.h"
 #include "version.h"
+#include "utils.h"
 
 static file_editor_interface *
 create_default_editor (QWidget *p)
@@ -263,16 +264,17 @@
 main_window::run_file_in_terminal (const QFileInfo& info)
 {
   QString file_name = info.canonicalFilePath ();
-  QString command = "run \""+file_name+"\"";
+  QString command = "run \"" + file_name + "\"";
 
   QString function_name = info.fileName ();
   function_name.chop (info.suffix ().length () + 1);
 
-  if (function_name.contains (' '))
+  if (! valid_identifier (function_name.toStdString ()))
     {
       int ans = QMessageBox::question (0, tr ("Octave"),
          tr ("The file %1\n"
-             "contains spaces and can not be executed.\n\n"
+             "can not be executed because its name\n"
+             "is not a valid identifier.\n\n"
              "Do you want to execute\n%2\n"
              "instead?").
           arg (file_name).arg (command),