# HG changeset patch # User Julien Bect # Date 1390324310 -3600 # Node ID 770c525a1a2b26a7254ec5859ed3d2dc60798f33 # Parent 6e8188effddff8773d770f21dd11f26332720004 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 diff -r 6e8188effddf -r 770c525a1a2b libgui/src/m-editor/file-editor-tab.cc --- 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); diff -r 6e8188effddf -r 770c525a1a2b libgui/src/m-editor/file-editor-tab.h --- 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); diff -r 6e8188effddf -r 770c525a1a2b libgui/src/main-window.cc --- 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),