# HG changeset patch
# User Daniel J Sebald <daniel.sebald@ieee.org>
# Date 1348132133 18000
# Node ID 903cee3872e8863db1bf899571dc322e1d68ccae
# Parent  7b69cd89868cbbc31b7d8cccf0e8757837470d26
Retain QsciAPIs lexer_api as part of lexer_octave_gui object so that it may be deleted upon deconstruction and not cause segmentation fault at startup (bug #37359)

* file-editor-tab.cc, file-editor-tab.h (file_editor_tab::update_tracked_file): Delete. (file_editor_tab::set_file_name): Rather than clear whole list, just subtract out the old file name then add the new name. (file_editor_tab::load_file): Remove update_tracked_file, it's part of set_file_name. (file_editor_tab::file_has_changed): Remove update_tracked_file, it's part of set_file_name. (file_editor_tab::save_file): Move file close before set_file_name so watcher doesn't notice.  Remove inelegant code that solved this by clearing watcher files.

* lexer-octave-gui.cc, file-editor-tab.cc (file_editor_tab::update_lexer, lexer_octave_gui::lexer_octave_gui, lexer_octave_gui : public QsciLexer): Move all the lexer preparatory code to the constructor so that lexer_api can be retained as part of object. (lexer_octave_gui::~lexer_octave_gui): Make destructor non-virtual and delete lexer_gui when done.

* octave-gui.cc, main-window-h, main-window.cc, file-editor.cc (octave_start_gui, file_editor::construct): Move read_settings from constructor to after construction in octave_start_gui so that no signal occur referencing a partially constructed object.

* file-editor.cc (file_editor::construct): Tidy code.

diff -r 7b69cd89868c -r 903cee3872e8 libgui/src/m-editor/file-editor-tab.cc
--- a/libgui/src/m-editor/file-editor-tab.cc	Tue Sep 18 00:40:16 2012 -0400
+++ b/libgui/src/m-editor/file-editor-tab.cc	Thu Sep 20 04:08:53 2012 -0500
@@ -145,9 +145,15 @@
 void
 file_editor_tab::set_file_name (const QString& fileName)
 {
+  // update tracked file
+  QStringList trackedFiles = _file_system_watcher.files ();
+  if (!trackedFiles.isEmpty ())
+    _file_system_watcher.removePath (_file_name);
+  _file_system_watcher.addPath (fileName);
   _file_name = fileName;
+
+  // update lexer after _file_name change
   update_lexer ();
-  update_tracked_file ();
 }
 
 void
@@ -188,31 +194,6 @@
   if (_file_name.endsWith (".m") || _file_name.endsWith (".M"))
     {
       lexer = new lexer_octave_gui ();
-
-      // The API info that is used for auto completion
-      // TODO: Where to store a file with API info (raw or prepared?)?
-      // TODO: Also provide infos on octave-forge functions?
-      // TODO: Also provide infos on function parameters?
-      // By now, use the keywords-list from syntax highlighting
-
-      QsciAPIs *lexer_api = new QsciAPIs (lexer);
-
-      QString keyword;
-      QStringList keywordList;
-
-      // get whole string with all keywords
-      keyword = lexer->keywords (1);
-      // split into single strings
-      keywordList = keyword.split (QRegExp ("\\s+"));
-
-      int i;
-      for (i = 0; i < keywordList.size (); i++)
-        {
-          // add single strings to the API
-          lexer_api->add (keywordList.at (i));
-        }
-      // prepare API info ... this make take some time
-      lexer_api->prepare ();
     }
   else if (_file_name.endsWith (".c")
            || _file_name.endsWith (".cc")
@@ -365,24 +346,13 @@
   emit editor_state_changed ();
 }
 
-void
-file_editor_tab::update_tracked_file ()
-{
-  QStringList trackedFiles = _file_system_watcher.files ();
-  if (!trackedFiles.isEmpty ())
-    _file_system_watcher.removePaths (trackedFiles);
-
-  if (_file_name != UNNAMED_FILE)
-    _file_system_watcher.addPath (_file_name);
-}
-
 int
 file_editor_tab::check_file_modified (const QString& msg, int cancelButton)
 {
   int decision = QMessageBox::Yes;
   if (_edit_area->isModified ())
     {
-      // file is modified but not saved, aks user what to do
+      // file is modified but not saved, ask user what to do
       decision = QMessageBox::warning (this,
                                        msg,
                                        tr ("The file %1\n"
@@ -590,9 +560,6 @@
   QApplication::restoreOverrideCursor ();
 
   set_file_name (fileName);
-  update_tracked_file ();
-
-
   update_window_title (false); // window title (no modification)
   _edit_area->setModified (false); // loaded file is not modified yet
 
@@ -627,10 +594,6 @@
       return save_file_as();
     }
 
-  QStringList watched_files = _file_system_watcher.files();
-  if (!watched_files.isEmpty ())
-    _file_system_watcher.removePaths(watched_files);
-
   // open the file for writing
   QFile file (saveFileName);
   if (!file.open (QFile::WriteOnly))
@@ -638,7 +601,6 @@
       QMessageBox::warning (this, tr ("Octave Editor"),
                             tr ("Could not open file %1 for write:\n%2.").
                             arg (saveFileName).arg (file.errorString ()));
-      _file_system_watcher.addPaths (watched_files);
       return false;
     }
 
@@ -647,17 +609,15 @@
   QApplication::setOverrideCursor (Qt::WaitCursor);
   out << _edit_area->text ();
   QApplication::restoreOverrideCursor ();
+  file.close();
 
-  // save file name for later use
-  _file_name = saveFileName;
+  // save file name after closing file otherwise tracker will notice file change
+  set_file_name (saveFileName);
   // set the window title to actual file name (not modified)
   update_window_title (false);
   // files is save -> not modified
   _edit_area->setModified (false);
-  file.close();
 
-  if (!watched_files.isEmpty ())
-    _file_system_watcher.addPaths (watched_files);
   return true;
 }
 
@@ -760,7 +720,6 @@
               set_file_name (UNNAMED_FILE);
               update_window_title (true); // window title (no modification)
               set_modified (true);
-              update_tracked_file ();
             }
         }
       else
diff -r 7b69cd89868c -r 903cee3872e8 libgui/src/m-editor/file-editor-tab.h
--- a/libgui/src/m-editor/file-editor-tab.h	Tue Sep 18 00:40:16 2012 -0400
+++ b/libgui/src/m-editor/file-editor-tab.h	Thu Sep 20 04:08:53 2012 -0500
@@ -100,7 +100,6 @@
   void request_add_breakpoint (int line);
   void request_remove_breakpoint (int line);
 
-  void update_tracked_file ();
   int check_file_modified (const QString& msg, int cancelButton);
   void do_comment_selected_text (bool comment);
 
diff -r 7b69cd89868c -r 903cee3872e8 libgui/src/m-editor/file-editor.cc
--- a/libgui/src/m-editor/file-editor.cc	Tue Sep 18 00:40:16 2012 -0400
+++ b/libgui/src/m-editor/file-editor.cc	Thu Sep 20 04:08:53 2012 -0500
@@ -594,8 +594,8 @@
     {
       QStringList sessionFileNames = settings->value("editor/savedSessionTabs", QStringList()).toStringList ();
 
-      for (int n=0;n<sessionFileNames.count();++n)
-        request_open_file(sessionFileNames.at(n), true);
+      for (int n=0; n < sessionFileNames.count (); ++n)
+        request_open_file (sessionFileNames.at (n), true);
     }
 }
 
diff -r 7b69cd89868c -r 903cee3872e8 libgui/src/m-editor/lexer-octave-gui.cc
--- a/libgui/src/m-editor/lexer-octave-gui.cc	Tue Sep 18 00:40:16 2012 -0400
+++ b/libgui/src/m-editor/lexer-octave-gui.cc	Thu Sep 20 04:08:53 2012 -0500
@@ -34,10 +34,37 @@
 lexer_octave_gui::lexer_octave_gui (QObject *p)
   : QsciLexer (p)
 {
+  // The API info that is used for auto completion
+  // TODO: Where to store a file with API info (raw or prepared?)?
+  // TODO: Also provide infos on octave-forge functions?
+  // TODO: Also provide infos on function parameters?
+  // By now, use the keywords-list from syntax highlighting
+
+  QString keyword;
+  QStringList keywordList;
+
+  // get whole string with all keywords
+  keyword = this->keywords (1);
+  // split into single strings
+  keywordList = keyword.split (QRegExp ("\\s+"));
+
+  lexer_api = new QsciAPIs (this);
+  if (lexer_api)
+    {
+      for (int i = 0; i < keywordList.size (); i++)
+        {
+          // add single strings to the API
+          lexer_api->add (keywordList.at (i));
+        }
+      // prepare API info ... this may take some time
+      lexer_api->prepare ();
+    }
 }
 
 lexer_octave_gui::~lexer_octave_gui()
 {
+  if (lexer_api)
+    delete lexer_api;
 }
 
 const char *lexer_octave_gui::language() const
diff -r 7b69cd89868c -r 903cee3872e8 libgui/src/m-editor/lexer-octave-gui.h
--- a/libgui/src/m-editor/lexer-octave-gui.h	Tue Sep 18 00:40:16 2012 -0400
+++ b/libgui/src/m-editor/lexer-octave-gui.h	Thu Sep 20 04:08:53 2012 -0500
@@ -27,7 +27,7 @@
 #include <QObject>
 #include <Qsci/qsciglobal.h>
 #include <Qsci/qscilexer.h>
-
+#include <Qsci/qsciapis.h>
 
 class lexer_octave_gui : public QsciLexer
 {
@@ -49,7 +49,7 @@
     };
 
   lexer_octave_gui (QObject *parent = 0);
-  virtual ~lexer_octave_gui ();
+  ~lexer_octave_gui ();
   const char *language () const;
   const char *lexer () const;
   QColor defaultColor (int style) const;
@@ -60,6 +60,7 @@
 private:
   lexer_octave_gui (const lexer_octave_gui &);
   lexer_octave_gui &operator= (const lexer_octave_gui &);
+  QsciAPIs *lexer_api;
 };
 
 #endif
diff -r 7b69cd89868c -r 903cee3872e8 libgui/src/main-window.cc
--- a/libgui/src/main-window.cc	Tue Sep 18 00:40:16 2012 -0400
+++ b/libgui/src/main-window.cc	Thu Sep 20 04:08:53 2012 -0500
@@ -860,7 +860,6 @@
   addDockWidget (Qt::BottomDockWidgetArea, _terminal_dock_widget);
   addDockWidget (Qt::RightDockWidgetArea, _documentation_dock_widget);
   setStatusBar (_status_bar);
-  read_settings ();
 
   _octave_qt_event_listener = new octave_qt_event_listener ();
   octave_link::register_event_listener (_octave_qt_event_listener);
diff -r 7b69cd89868c -r 903cee3872e8 libgui/src/main-window.h
--- a/libgui/src/main-window.h	Tue Sep 18 00:40:16 2012 -0400
+++ b/libgui/src/main-window.h	Thu Sep 20 04:08:53 2012 -0500
@@ -114,10 +114,11 @@
   void debug_step_out ();
   void debug_quit ();
 
+  void read_settings ();
+  void write_settings ();
+
 protected:
   void closeEvent (QCloseEvent * closeEvent);
-  void read_settings ();
-  void write_settings ();
 
 private:
   void construct ();
diff -r 7b69cd89868c -r 903cee3872e8 libgui/src/octave-gui.cc
--- a/libgui/src/octave-gui.cc	Tue Sep 18 00:40:16 2012 -0400
+++ b/libgui/src/octave-gui.cc	Thu Sep 20 04:08:53 2012 -0500
@@ -110,6 +110,7 @@
           resource_manager::update_network_settings ();
 
           main_window w;
+          w.read_settings ();  // Get the widget settings after construction and before showing
           w.show ();
           w.focus_command_window ();
 
