changeset 16716:23b5dde25367

make octave lexer the default and dynamically set margin width for line numbers * file-editor-tab.cc(update_lexer): octave lexer is the default lexer, remove check for a lexer before setting its apis, reset the width of the margin displaying the line numbers because char size may have changed, (notice_settings): call update_lexer only if there is a file name to prevent the function being called just after tab creation before the file is loaded, do not explicitly use the font metrics for the margin width since they are already respected by setMarginWidth, (dis)connect new slot auto_margin_width to signal linesChanged in order to atuomatically set width for line numbers, (auto_margin_width): set width of margin ahich displays the line numbers depending on max. line number * file-editor-tab.h: new slot auto_margin_width
author Torsten <ttl@justmail.de>
date Sat, 01 Jun 2013 19:40:24 +0200
parents 96ed7ab44e2e
children 9ad6e7d4fa42
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h
diffstat 2 files changed, 54 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc
+++ b/libgui/src/m-editor/file-editor-tab.cc
@@ -254,48 +254,50 @@
         }
       else
         {
-          // FIXME -- why should the bash lexer be the default?
+#if defined (HAVE_LEXER_OCTAVE)
+          lexer = new QsciLexerOctave ();
+#elif defined (HAVE_LEXER_MATLAB)
+          lexer = new QsciLexerMatlab ();
+#else
           lexer = new QsciLexerBash ();
+#endif
         }
     }
 
-  if (lexer)
+  _lexer_apis = new QsciAPIs(lexer);
+  if (_lexer_apis)
     {
-      _lexer_apis = new QsciAPIs(lexer);
-      if (_lexer_apis)
+      // create raw apis info
+      QString keyword;
+      QStringList keyword_list;
+      int i;
+      for (i=1; i<=3; i++) // load the first 3 keyword sets
         {
-          // create raw apis info
-          QString keyword;
-          QStringList keyword_list;
-          int i;
-          for (i=1; i<=3; i++) // load the first 3 keyword sets
-            {
-              keyword = QString(lexer->keywords (i));           // get list
-              keyword_list = keyword.split (QRegExp ("\\s+"));  // split
-              for (i = 0; i < keyword_list.size (); i++)        // add to API
-                _lexer_apis->add (keyword_list.at (i));
-            }
+          keyword = QString(lexer->keywords (i));           // get list
+          keyword_list = keyword.split (QRegExp ("\\s+"));  // split
+          for (i = 0; i < keyword_list.size (); i++)        // add to API
+            _lexer_apis->add (keyword_list.at (i));
+        }
 
-          // get path where to store prepared api info
-          QDesktopServices desktopServices;
-          QString prep_apis_path
-            = desktopServices.storageLocation (QDesktopServices::HomeLocation)
-                + "/.config/octave/"  + QString(OCTAVE_VERSION) + "/qsci/";
+     // get path where to store prepared api info
+     QDesktopServices desktopServices;
+     QString prep_apis_path
+          = desktopServices.storageLocation (QDesktopServices::HomeLocation)
+            + "/.config/octave/"  + QString(OCTAVE_VERSION) + "/qsci/";
 
-          // check whether path exists or can be created
-          if (QDir("/").mkpath (prep_apis_path))
-            { // path exists, apis info can be saved there
-              _prep_apis_file = prep_apis_path + lexer->lexer () + ".pap";
-              if (!_lexer_apis->loadPrepared (_prep_apis_file))
-                { // no prepared info loaded, prepare and save
-                  connect (_lexer_apis, SIGNAL (apiPreparationFinished ()),
-                           this, SLOT (save_apis_info ()));
-                  _lexer_apis->prepare ();  // prepare apis info and save
-                }
+      // check whether path exists or can be created
+      if (QDir("/").mkpath (prep_apis_path))
+        { // path exists, apis info can be saved there
+          _prep_apis_file = prep_apis_path + lexer->lexer () + ".pap";
+          if (!_lexer_apis->loadPrepared (_prep_apis_file))
+            { // no prepared info loaded, prepare and save
+              connect (_lexer_apis, SIGNAL (apiPreparationFinished ()),
+                       this, SLOT (save_apis_info ()));
+              _lexer_apis->prepare ();  // prepare apis info and save
             }
-          else
-            _lexer_apis->prepare ();  // prepare apis info wihtout saving
         }
+      else
+        _lexer_apis->prepare ();  // prepare apis info wihtout saving
     }
 
   QSettings *settings = resource_manager::get_settings ();
@@ -304,6 +306,11 @@
 
   _edit_area->setLexer (lexer);
 
+  // adapt line number width to the font size of the lexer
+  if (settings->value ("editor/showLineNumbers", true).toBool ())
+    auto_margin_width ();
+  else
+    _edit_area->setMarginWidth (2,0);
 }
 
 void
@@ -1112,9 +1119,8 @@
 {
   // QSettings pointer is checked before emitting.
 
-  update_lexer ();
-
-  QFontMetrics lexer_font_metrics (_edit_area->lexer ()->defaultFont (0));
+  if (!_file_name.isEmpty ())
+    update_lexer (); // do not update lexer when tab is just created
 
   //highlight current line color
   QVariant default_var = QColor (240, 240, 240);
@@ -1163,12 +1169,14 @@
   if (settings->value ("editor/showLineNumbers", true).toBool ())
     {
       _edit_area->setMarginLineNumbers (2, true);
-      _edit_area->setMarginWidth (2, lexer_font_metrics.width ("9999"));
+      auto_margin_width ();
+      connect (_edit_area, SIGNAL (linesChanged ()),
+               this, SLOT (auto_margin_width ()));
     }
   else
     {
       _edit_area->setMarginLineNumbers (2, false);
-      _edit_area->setMarginWidth (2, 0);
+      disconnect (_edit_area, SIGNAL (linesChanged ()), 0, 0);
     }
 
   _edit_area->setAutoIndent
@@ -1189,6 +1197,12 @@
 }
 
 void
+file_editor_tab::auto_margin_width ()
+{
+  _edit_area->setMarginWidth (2, "1"+QString::number (_edit_area->lines ()));
+}
+
+void
 file_editor_tab::conditional_close (const QWidget *ID, bool app_closing)
 {
   if (ID != this)
--- a/libgui/src/m-editor/file-editor-tab.h
+++ b/libgui/src/m-editor/file-editor-tab.h
@@ -141,6 +141,9 @@
   // When apis preparation has finished and is ready to save
   void save_apis_info ();
 
+  // When the numer of lines changes -> adapt width of margin
+  void auto_margin_width ();
+
 private:
 
   enum editor_markers