# HG changeset patch # User Torsten # Date 1412625168 -7200 # Node ID c766a1f63c4059c2386212f7bab0a7340a15d87e # Parent 0279c601b49ce5d539b1ae938e8ec562b63f1ced 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 diff -r 0279c601b49c -r c766a1f63c40 libgui/src/m-editor/file-editor-tab.cc --- a/libgui/src/m-editor/file-editor-tab.cc Sun Oct 05 10:41:51 2014 -0400 +++ b/libgui/src/m-editor/file-editor-tab.cc Mon Oct 06 21:52:48 2014 +0200 @@ -87,9 +87,15 @@ this, SLOT (handle_cursor_moved (int,int))); - // 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); @@ -1013,6 +1019,7 @@ QTextStream in (&file); QApplication::setOverrideCursor (Qt::WaitCursor); _edit_area->setText (in.readAll ()); + _edit_area->setEolMode (detect_eol_mode ()); QApplication::restoreOverrideCursor (); _copy_available = false; // no selection yet available @@ -1020,15 +1027,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 diff -r 0279c601b49c -r c766a1f63c40 libgui/src/m-editor/file-editor-tab.h --- a/libgui/src/m-editor/file-editor-tab.h Sun Oct 05 10:41:51 2014 -0400 +++ b/libgui/src/m-editor/file-editor-tab.h Mon Oct 06 21:52:48 2014 +0200 @@ -191,11 +191,15 @@ void remove_all_breakpoints_callback (const bp_info& info); void center_current_line (); + 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;