changeset 24475:8cb985e362f3

Allow uncommenting with any kind of comment (bug #52695): * file-editor-tab.cc (do_comment_selected_text): get list of possible strings for uncommenting and create an according regular expression; (handle_char_added): comment string is snow a string list * octave-qscintilla.cc (comment_string), octave-qscintilla.h: boolean parameter for commenting or uncommenting string, now returns a string list * module.mk: new file octave-settings.h * octave-settings.h: new files with constants for the preferences used for the comment strings * settings-dialog.cc (settings_dialog): add the required radio buttons for the comment strings programmatically, read preferences from settings file; (write_changed_settings): read values from radio buttons and write into the settings file * settings-dialog.h: declare the required radio butons * settings-dialog.ui: add horizontal layouts for the radio buttons
author Torsten <mttl@mailbox.org>
date Wed, 27 Dec 2017 20:46:11 +0100
parents 0b65949870e3
children bb8ae4e9e09d
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/octave-qscintilla.cc libgui/src/m-editor/octave-qscintilla.h libgui/src/module.mk libgui/src/octave-settings.h libgui/src/settings-dialog.cc libgui/src/settings-dialog.h libgui/src/settings-dialog.ui
diffstat 8 files changed, 254 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Wed Dec 27 11:22:26 2017 -0800
+++ b/libgui/src/m-editor/file-editor-tab.cc	Wed Dec 27 20:46:11 2017 +0100
@@ -1462,11 +1462,52 @@
 void
 file_editor_tab::do_comment_selected_text (bool comment)
 {
-  QString comment_str = _edit_area->comment_string ();
-  QRegExp rxc = QRegExp ("^([ \\t]*)" + comment_str);
-  int len = 0;
-  int lenc = comment_str.length ();
-
+  QStringList comment_str = _edit_area->comment_string (comment);
+  QRegExp rxc;
+  QString ws = "^([ \\t]*)";
+
+  if (comment)
+    {
+      // Commenting (only one string possible)
+      rxc = QRegExp (ws + comment_str.at (0));
+    }
+  else
+    {
+      // Uncommenting (several strings possible)
+
+      // Sort strings according their length
+      QStringList comment_str_sorted (comment_str.at (0));
+      bool inserted;
+
+      for (int i = 1; i < comment_str.length (); i++)
+        {
+          inserted = false;
+          for (int j = 0; j < comment_str_sorted.length (); j++)
+            {
+              if (comment_str.at (i).length () > comment_str_sorted.at (j).length ())
+                {
+                  comment_str_sorted.insert (j, comment_str.at (i));
+                  inserted = true;
+                  break;
+                }
+            }
+          if (! inserted)
+            comment_str_sorted << comment_str.at (i);
+        }
+
+      // Create regular expression
+      QString regexp;
+      for (int i = 0; i < comment_str_sorted.length (); i++)
+        {
+          if (i > 0)
+            regexp = regexp + QString ("|");
+          regexp = regexp + comment_str_sorted.at (i);
+        }
+      rxc = QRegExp (ws + "(" + regexp + ")");
+    }
+
+  // Do the commenting/uncommenting
+  int len = 0, lenc = 0;
   _edit_area->beginUndoAction ();
 
   if (_edit_area->hasSelectedText ())
@@ -1485,15 +1526,16 @@
         {
           if (comment)
             {
-              _edit_area->insertAt (comment_str, i, 0);
-
+              _edit_area->insertAt (comment_str.at (0), i, 0);
             }
           else
             {
               QString line (_edit_area->text (i));
               if ((removed = line.contains (rxc)))
                 {
-                  len = rxc.matchedLength ();
+                  len = rxc.matchedLength ();   // complete length
+                  QString matched_text = rxc.capturedTexts ().at (0);
+                  lenc = matched_text.remove (QRegExp (ws)).length ();  // only comment string
                   _edit_area->setSelection (i, len-lenc, i, len);
                   _edit_area->removeSelectedText ();
                 }
@@ -1531,13 +1573,15 @@
       int cpline, col;
       _edit_area->getCursorPosition (&cpline, &col);
       if (comment)
-        _edit_area->insertAt (comment_str, cpline, 0);
+        _edit_area->insertAt (comment_str.at (0), cpline, 0);
       else
         {
           QString line (_edit_area->text (cpline));
           if (line.contains (rxc))
             {
-              len = rxc.matchedLength ();
+              len = rxc.matchedLength ();   // complete length
+              QString matched_text = rxc.capturedTexts ().at (0);
+              lenc = matched_text.remove (QRegExp (ws)).length ();  // only comment string
               _edit_area->setSelection (cpline, len-lenc, cpline, len);
               _edit_area->removeSelectedText ();
             }
@@ -2894,7 +2938,7 @@
     QString newline = QString ("\n");
     style_comment = _edit_area->is_style_comment ();
     if (style_comment == octave_qscintilla::ST_LINE_COMMENT)
-      newline = newline + _edit_area->comment_string ();
+      newline = newline + _edit_area->comment_string ().at (0);
     _edit_area->insertAt (newline, line, col_newline);
 
     // Automatically indent the new line to the indentation of previous line
--- a/libgui/src/m-editor/octave-qscintilla.cc	Wed Dec 27 11:22:26 2017 -0800
+++ b/libgui/src/m-editor/octave-qscintilla.cc	Wed Dec 27 20:46:11 2017 +0100
@@ -57,6 +57,8 @@
 #include "file-editor-tab.h"
 #include "shortcut-manager.h"
 #include "resource-manager.h"
+#include "octave-settings.h"
+
 
 // Return true if CANDIDATE is a "closing" that matches OPENING,
 // such as "end" or "endif" for "if", or "catch" for "try".
@@ -376,8 +378,8 @@
 }
 
 // Function returning the comment string of the current lexer
-QString
-octave_qscintilla::comment_string (void)
+QStringList
+octave_qscintilla::comment_string (bool comment)
 {
   int lexer = SendScintilla (SCI_GETLEXER);
 
@@ -389,34 +391,58 @@
 #else
       case SCLEX_MATLAB:
 #endif
-       {
+        {
           QSettings *settings = resource_manager::get_settings ();
-          int comment_index
-                = settings->value ("editor/octave_comment_string", 0).toInt ();
-          if (comment_index == 1)
-            return QString ("#");
-          else if (comment_index == 2)
-            return QString ("%");
+          int comment_string;
+
+          if (comment)
+            {
+              // The commenting string is requested
+              if (settings->contains (oct_comment_str))   // new version (radio buttons)
+                comment_string = settings->value (oct_comment_str,
+                                                  oct_comment_str_d).toInt ();
+              else                                         // old version (combo box)
+                comment_string = settings->value (oct_comment_str_old,
+                                                  oct_comment_str_d).toInt ();
+
+              return (QStringList (oct_comment_strings.at (comment_string)));
+            }
           else
-            return QString ("##");  // default and for index 0
+            {
+              QStringList c_str;
+
+              // The possible uncommenting string(s) are requested
+              comment_string = settings->value (oct_uncomment_str,
+                                                oct_uncomment_str_d).toInt ();
+
+              for (int i = 0; i < oct_comment_strings_count; i++)
+                {
+                  if (1 << i & comment_string)
+                    c_str.append (oct_comment_strings.at (i));
+                }
+
+              return c_str;
+            }
+
         }
 #endif
 
       case SCLEX_PERL:
       case SCLEX_BASH:
       case SCLEX_DIFF:
-        return QString ("#");
+        return QStringList ("#");
 
       case SCLEX_CPP:
-        return QString ("//");
+        return QStringList ("//");
 
       case SCLEX_BATCH:
-        return QString ("REM ");
+        return QStringList ("REM ");
     }
 
-    return QString ("%");  // should never happen
+    return QStringList ("%");  // should never happen
 }
 
+
 // provide the style at a specific position
 int
 octave_qscintilla::get_style (int pos)
--- a/libgui/src/m-editor/octave-qscintilla.h	Wed Dec 27 11:22:26 2017 -0800
+++ b/libgui/src/m-editor/octave-qscintilla.h	Wed Dec 27 20:46:11 2017 +0100
@@ -58,7 +58,7 @@
   bool get_actual_word (void);
   void clear_selection_markers (void);
   void get_current_position (int *pos, int *line, int *col);
-  QString comment_string (void);
+  QStringList comment_string (bool comment = true);
   int get_style (int pos = -1);
   int is_style_comment (int pos = -1);
   void smart_indent (bool do_smart_indent, int do_auto_close, int line);
--- a/libgui/src/module.mk	Wed Dec 27 11:22:26 2017 -0800
+++ b/libgui/src/module.mk	Wed Dec 27 20:46:11 2017 +0100
@@ -161,6 +161,7 @@
   %reldir%/octave-gui.h \
   %reldir%/octave-cmd.h \
   %reldir%/octave-qt-link.h \
+  %reldir%/octave-settings.h \
   %reldir%/qtinfo/texinfo-parser.h \
   %reldir%/qtinfo/webinfo.h \
   %reldir%/resource-manager.h \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgui/src/octave-settings.h	Wed Dec 27 20:46:11 2017 +0100
@@ -0,0 +1,43 @@
+/*
+
+Copyright (C) 2017 Torsten <mttl@mailbox.de>
+
+This file is part of Octave.
+
+Octave is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+Octave is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Octave; see the file COPYING.  If not, see
+<http://www.gnu.org/licenses/>.
+
+*/
+
+#if ! defined (octave_settings_h)
+#define octave_settings_h 1
+
+//#if defined (HAVE_CONFIG_H)
+//#  include "config.h"
+//#endif
+
+#include <QStringList>
+
+// Octave comment strings
+const QString oct_comment_str_old ("editor/octave_comment_string");
+const int oct_comment_str_old_d = 0;
+
+const QString oct_comment_str ("editor/oct_comment_str");
+const QString oct_uncomment_str ("editor/oct_uncomment_str");
+const QStringList oct_comment_strings (QStringList () << "##" << "#" << "%"<< "%%" << "%!");
+const int oct_comment_strings_count = 5;
+const int oct_comment_str_d = 0;
+const int oct_uncomment_str_d = 1 + 2 + 4 + 8;
+
+#endif
--- a/libgui/src/settings-dialog.cc	Wed Dec 27 11:22:26 2017 -0800
+++ b/libgui/src/settings-dialog.cc	Wed Dec 27 20:46:11 2017 +0100
@@ -445,7 +445,39 @@
   ui->editor_ws_indent_checkbox->setChecked (settings->value ("editor/show_white_space_indent", false).toBool ());
   ui->cb_show_eol->setChecked (settings->value ("editor/show_eol_chars", false).toBool ());
   ui->cb_show_hscrollbar->setChecked (settings->value ("editor/show_hscroll_bar", true).toBool ());
-  ui->combo_oct_comment_str->setCurrentIndex (settings->value ("editor/octave_comment_string", 0).toInt ());
+
+  int selected_comment_string, selected_uncomment_string;
+
+  if (settings->contains (oct_comment_str))   // new version (radio buttons)
+    selected_comment_string = settings->value (oct_comment_str,
+                                               oct_comment_str_d).toInt ();
+  else                                         // old version (combo box)
+    selected_comment_string = settings->value (oct_comment_str_old,
+                                               oct_comment_str_d).toInt ();
+
+  selected_uncomment_string = settings->value (oct_uncomment_str,
+                                               oct_uncomment_str_d).toInt ();
+
+  for (int i = 0; i < oct_comment_strings_count; i++)
+    {
+      m_rb_comment_strings[i] = new QRadioButton ();
+      m_rb_uncomment_strings[i] = new QRadioButton ();
+
+      connect (m_rb_comment_strings[i], SIGNAL (clicked (bool)),
+               m_rb_uncomment_strings[i], SLOT (setChecked (bool)));
+      connect (m_rb_comment_strings[i], SIGNAL (toggled (bool)),
+               m_rb_uncomment_strings[i], SLOT (setDisabled (bool)));
+
+      m_rb_comment_strings[i]->setText (oct_comment_strings.at(i));
+      m_rb_comment_strings[i]->setChecked (i == selected_comment_string);
+      ui->layout_comment_strings->addWidget (m_rb_comment_strings[i]);
+
+      m_rb_uncomment_strings[i]->setText (oct_comment_strings.at(i));
+      m_rb_uncomment_strings[i]->setAutoExclusive (false);
+      m_rb_uncomment_strings[i]->setChecked ( 1 << i & selected_uncomment_string);
+      ui->layout_uncomment_strings->addWidget (m_rb_uncomment_strings[i]);
+    }
+
 
 #if defined (HAVE_QSCINTILLA)
 #  if defined (Q_OS_WIN32)
@@ -799,7 +831,24 @@
   settings->setValue ("editor/show_eol_chars", ui->cb_show_eol->isChecked ());
   settings->setValue ("editor/show_hscroll_bar", ui->cb_show_hscrollbar->isChecked ());
   settings->setValue ("editor/default_eol_mode", ui->combo_eol_mode->currentIndex ());
-  settings->setValue ("editor/octave_comment_string", ui->combo_oct_comment_str->currentIndex ());
+
+  // Comment strings
+  int rb_uncomment = 0;
+  for (int i = 0; i < oct_comment_strings_count; i++)
+    {
+      if (m_rb_comment_strings[i]->isChecked ())
+        {
+          settings->setValue (oct_comment_str, i);
+          if (i < 3)
+            settings->setValue (oct_comment_str_old, i);
+          else
+            settings->setValue (oct_comment_str_old, oct_comment_str_d);
+        }
+      if (m_rb_uncomment_strings[i]->isChecked ())
+        rb_uncomment = rb_uncomment + (1 << i);
+    }
+  settings->setValue (oct_uncomment_str, rb_uncomment);
+
   settings->setValue ("editor/default_encoding", ui->editor_combo_encoding->currentText ());
   settings->setValue ("editor/auto_indent", ui->editor_auto_ind_checkbox->isChecked ());
   settings->setValue ("editor/tab_indents_line", ui->editor_tab_ind_checkbox->isChecked ());
--- a/libgui/src/settings-dialog.h	Wed Dec 27 11:22:26 2017 -0800
+++ b/libgui/src/settings-dialog.h	Wed Dec 27 20:46:11 2017 +0100
@@ -26,8 +26,10 @@
 #include <QDialog>
 #include <QSettings>
 #include <QLineEdit>
+#include <QRadioButton>
 
 #include "color-picker.h"
+#include "octave-settings.h"
 
 namespace Ui
 {
@@ -83,6 +85,9 @@
   color_picker *m_widget_title_fg_color;
   color_picker *m_widget_title_fg_color_active;
   color_picker *m_editor_current_line_color;
+
+  QRadioButton *m_rb_comment_strings[oct_comment_strings_count];
+  QRadioButton *m_rb_uncomment_strings[oct_comment_strings_count];
 };
 
 #endif
--- a/libgui/src/settings-dialog.ui	Wed Dec 27 11:22:26 2017 -0800
+++ b/libgui/src/settings-dialog.ui	Wed Dec 27 20:46:11 2017 +0100
@@ -32,7 +32,7 @@
       </size>
      </property>
      <property name="currentIndex">
-      <number>8</number>
+      <number>1</number>
      </property>
      <widget class="QWidget" name="tab_general">
       <property name="enabled">
@@ -510,7 +510,7 @@
             <x>0</x>
             <y>0</y>
             <width>642</width>
-            <height>968</height>
+            <height>1012</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_16">
@@ -932,58 +932,65 @@
                 </item>
                </layout>
               </item>
+             </layout>
+            </widget>
+           </item>
+           <item>
+            <widget class="QGroupBox" name="groupBox_9">
+             <property name="title">
+              <string>Comments (Octave)</string>
+             </property>
+             <property name="checked">
+              <bool>false</bool>
+             </property>
+             <layout class="QVBoxLayout" name="verticalLayout_24">
               <item>
-               <layout class="QHBoxLayout" name="horizontalLayout_17">
-                <property name="topMargin">
-                 <number>0</number>
-                </property>
-                <item>
-                 <widget class="QLabel" name="label_23">
+               <layout class="QGridLayout" name="gridLayout_18">
+                <item row="1" column="0">
+                 <widget class="QLabel" name="label_28">
                   <property name="text">
-                   <string>Character used to comment selected text (Ctrl+R)</string>
+                   <string>Strings considered for uncommenting text</string>
                   </property>
                  </widget>
                 </item>
-                <item>
-                 <widget class="QComboBox" name="combo_oct_comment_str">
-                  <property name="sizePolicy">
-                   <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
-                    <horstretch>0</horstretch>
-                    <verstretch>0</verstretch>
-                   </sizepolicy>
+                <item row="0" column="1">
+                 <spacer name="horizontalSpacer_35">
+                  <property name="orientation">
+                   <enum>Qt::Horizontal</enum>
                   </property>
-                  <property name="sizeAdjustPolicy">
-                   <enum>QComboBox::AdjustToContents</enum>
-                  </property>
-                  <property name="minimumContentsLength">
-                   <number>3</number>
+                  <property name="sizeType">
+                   <enum>QSizePolicy::Fixed</enum>
                   </property>
-                  <item>
-                   <property name="text">
-                    <string>##</string>
-                   </property>
-                  </item>
-                  <item>
-                   <property name="text">
-                    <string>#</string>
-                   </property>
-                  </item>
-                  <item>
-                   <property name="text">
-                    <string>%</string>
-                   </property>
-                  </item>
+                  <property name="sizeHint" stdset="0">
+                   <size>
+                    <width>10</width>
+                    <height>10</height>
+                   </size>
+                  </property>
+                 </spacer>
+                </item>
+                <item row="0" column="0">
+                 <widget class="QLabel" name="label_24">
+                  <property name="text">
+                   <string>String used for commenting selected text</string>
+                  </property>
                  </widget>
                 </item>
-                <item>
-                 <spacer name="horizontalSpacer_33">
+                <item row="0" column="2">
+                 <layout class="QHBoxLayout" name="layout_comment_strings"/>
+                </item>
+                <item row="1" column="2">
+                 <layout class="QHBoxLayout" name="layout_uncomment_strings"/>
+                </item>
+                <item row="0" column="8">
+                 <spacer name="horizontalSpacer_34">
                   <property name="orientation">
                    <enum>Qt::Horizontal</enum>
                   </property>
                   <property name="sizeHint" stdset="0">
                    <size>
                     <width>40</width>
-                    <height>20</height>
+                    <height>10</height>
                    </size>
                   </property>
                  </spacer>
@@ -1816,8 +1823,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>573</height>
+            <width>481</width>
+            <height>230</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_7">
@@ -2097,8 +2104,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>573</height>
+            <width>474</width>
+            <height>196</height>
            </rect>
           </property>
           <layout class="QGridLayout" name="gridLayout_8">
@@ -2241,8 +2248,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>573</height>
+            <width>200</width>
+            <height>77</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_19">
@@ -2310,8 +2317,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>573</height>
+            <width>364</width>
+            <height>210</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_25">
@@ -2509,8 +2516,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>573</height>
+            <width>529</width>
+            <height>199</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_20">
@@ -2668,8 +2675,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>573</height>
+            <width>615</width>
+            <height>169</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_vesc_manual">