changeset 19274:536dadff0226 gui-release

maint: Periodic merge of stable to gui-release.
author John W. Eaton <jwe@octave.org>
date Thu, 09 Oct 2014 16:07:29 -0400
parents b8ffcb88d77c (current diff) c766a1f63c40 (diff)
children 05d8e71d20cb
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h
diffstat 2 files changed, 82 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Thu Oct 09 16:06:02 2014 -0400
+++ b/libgui/src/m-editor/file-editor-tab.cc	Thu Oct 09 16:07:29 2014 -0400
@@ -99,9 +99,15 @@
   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);
+  _eol_indicator->setMinimumSize (35,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);
@@ -1304,6 +1310,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 +1318,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 ("CRLF");
+        break;
+      case QsciScintilla::EolMac:
+        _eol_indicator->setText ("CR");
+        break;
+      case QsciScintilla::EolUnix:
+        _eol_indicator->setText ("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	Thu Oct 09 16:06:02 2014 -0400
+++ b/libgui/src/m-editor/file-editor-tab.h	Thu Oct 09 16:07:29 2014 -0400
@@ -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;