changeset 19263:85b2d669c21e gui-release

detect eol mode when opening a file in the editor of the gui (#bug 43334) * file-editor-tab.cc (constructor): add an eol-mode indicator to status bar and change indicator description to lower case; (load_file): detect eol-mode of loaded file, set the current eol-mode of the editor accordingly, and update the indicator in the status bar; (new_file): update eol-indicator in status bar; (detect_eol_mode): new function scanning the actual contents of the editor for detection of the used eol-mode; (update_eol_indicator): new function for updating the eol-indicator in the status bar * file-editor-tab.h: new indicator eol_indicator, new private functions update_eol_indicator, detect_eol_mode
author Torsten <ttl@justmail.de>
date Mon, 06 Oct 2014 21:52:48 +0200
parents 3978a5509f40
children 97eea1e2d9ff
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h
diffstat 2 files changed, 86 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Sun Oct 05 10:46:48 2014 -0400
+++ b/libgui/src/m-editor/file-editor-tab.cc	Mon Oct 06 21:52:48 2014 +0200
@@ -99,15 +99,23 @@
   connect (_edit_area, SIGNAL (context_menu_edit_signal (const QString&)),
            this, SLOT (handle_context_menu_edit (const QString&)));
 
-  // create statusbar for row/col indicator
+  // create statusbar for row/col indicator and eol mode
   _status_bar = new QStatusBar (this);
 
+  // eol mode
+  _eol_indicator = new QLabel ("",this);
+  QLabel *eol_label = new QLabel (tr ("eol:"), this);
+  _eol_indicator->setMinimumSize (35,0);
+  _status_bar->addPermanentWidget (eol_label, 0);
+  _status_bar->addPermanentWidget (_eol_indicator, 0);
+
+  // row- and col-indicator
   _row_indicator = new QLabel ("", this);
   _row_indicator->setMinimumSize (30,0);
-  QLabel *row_label = new QLabel (tr ("Line:"), this);
+  QLabel *row_label = new QLabel (tr ("line:"), this);
   _col_indicator = new QLabel ("", this);
   _col_indicator->setMinimumSize (25,0);
-  QLabel *col_label = new QLabel (tr ("Col:"), this);
+  QLabel *col_label = new QLabel (tr ("col:"), this);
   _status_bar->addPermanentWidget (row_label, 0);
   _status_bar->addPermanentWidget (_row_indicator, 0);
   _status_bar->addPermanentWidget (col_label, 0);
@@ -1304,6 +1312,7 @@
   in.setCodec("UTF-8");
   QApplication::setOverrideCursor (Qt::WaitCursor);
   _edit_area->setText (in.readAll ());
+  _edit_area->setEolMode (detect_eol_mode ());
   QApplication::restoreOverrideCursor ();
 
   _copy_available = false;     // no selection yet available
@@ -1311,15 +1320,85 @@
   update_window_title (false); // window title (no modification)
   _edit_area->setModified (false); // loaded file is not modified yet
 
+  update_eol_indicator ();
+
   return QString ();
 }
 
+QsciScintilla::EolMode
+file_editor_tab::detect_eol_mode ()
+{
+  char *text = _edit_area->text ().toAscii ().data ();
+  int text_size = _edit_area->text ().length ();
+
+  char eol_lf = 0x0a;
+  char eol_cr = 0x0d;
+
+  int count_lf = 0;
+  int count_cr = 0;
+  int count_crlf = 0;
+
+  for (int i = 0; i < text_size; i++)
+    {
+      if (text[i] == eol_lf)
+        {
+          count_lf++;
+        }
+      else
+        {
+          if (text[i] == eol_cr)
+            {
+              if ((i < text_size -1) && text[i+1] == eol_lf)
+                {
+                  count_crlf++;
+                  i++;
+                }
+              else
+                count_cr++;
+            }
+        }
+    }
+
+  QsciScintilla::EolMode eol_mode = QsciScintilla::EolUnix;
+  int count_max = count_lf;
+
+  if (count_cr > count_max)
+    {
+      eol_mode = QsciScintilla::EolMac;
+      count_max = count_cr;
+    }
+  if (count_crlf > count_max)
+    {
+      eol_mode = QsciScintilla::EolWindows;
+    }
+
+  return eol_mode;
+}
+
+void
+file_editor_tab::update_eol_indicator ()
+{
+  switch (_edit_area->eolMode ())
+    {
+      case QsciScintilla::EolWindows:
+        _eol_indicator->setText (tr ("CRLF"));
+        break;
+      case QsciScintilla::EolMac:
+        _eol_indicator->setText (tr ("CR"));
+        break;
+      case QsciScintilla::EolUnix:
+        _eol_indicator->setText (tr ("LF"));
+        break;
+    }
+}
+
 void
 file_editor_tab::new_file (const QString &commands)
 {
   update_window_title (false); // window title (no modification)
   _edit_area->setText (commands);
   _edit_area->setModified (false); // new file is not modified yet
+  update_eol_indicator ();
 }
 
 void
--- a/libgui/src/m-editor/file-editor-tab.h	Sun Oct 05 10:46:48 2014 -0400
+++ b/libgui/src/m-editor/file-editor-tab.h	Mon Oct 06 21:52:48 2014 +0200
@@ -211,11 +211,15 @@
   void add_octave_apis (octave_value_list key_ovl);
   QString get_function_name ();
 
+  QsciScintilla::EolMode detect_eol_mode ();
+  void update_eol_indicator ();
+
   octave_qscintilla *_edit_area;
 
   QStatusBar *_status_bar;
   QLabel *_row_indicator;
   QLabel *_col_indicator;
+  QLabel *_eol_indicator;
 
   QString _file_name;
   QString _file_name_short;