# HG changeset patch # User Torsten # Date 1402933253 -7200 # Node ID 0e6f7b5f65561cb97f0dae82260051b9abf98359 # Parent 6504a1932637c9b79e6b70d5b6a625a590d4ee39 propose function name as file name when saving a new file (bug #42568) * file-editor-tab.cc (get_function_name): new function, get a possible function name from the contents of the file; (save_file_as): when saving a new file, try to detect the function name by get_function_name and propose this name as file name * file-editor-tab.h (get_function_name): new function diff -r 6504a1932637 -r 0e6f7b5f6556 libgui/src/m-editor/file-editor-tab.cc --- a/libgui/src/m-editor/file-editor-tab.cc Mon Jun 16 12:11:21 2014 +0200 +++ b/libgui/src/m-editor/file-editor-tab.cc Mon Jun 16 17:40:53 2014 +0200 @@ -1324,6 +1324,11 @@ // constructor argument. fileDialog->setDirectory (_file_name); } + + // propose a name corresponding to the function name + QString fname = get_function_name (); + if (! fname.isEmpty ()) + fileDialog->selectFile (fname + ".m"); } fileDialog->setNameFilter (tr ("Octave Files (*.m);;All Files (*)")); @@ -1739,4 +1744,26 @@ emit set_global_edit_shortcuts_signal (! focus); } +QString +file_editor_tab::get_function_name () +{ + QRegExp rxfun1 ("^([\t ]*)function([^=]+)=([^\\(]+)\\(([^\\)]*)\\)"); + QRegExp rxfun2 ("^([\t ]*)function([^\\(]+)\\(([^\\)]*)\\)"); + QRegExp rxfun3 ("^([\t ]*)function([\t ]*)([^\t ]+)"); + + QStringList lines = _edit_area->text ().split ("\n"); + + for (int i = 0; i < lines.count (); i++) + { + if (rxfun1.indexIn (lines.at (i)) != -1) + return rxfun1.cap (3).remove (QRegExp("[ \t]*")); + else if (rxfun2.indexIn (lines.at (i)) != -1) + return rxfun2.cap (2).remove (QRegExp("[ \t]*")); + else if (rxfun3.indexIn (lines.at (i)) != -1) + return rxfun3.cap (3).remove (QRegExp("[ \t]*")); + } + + return QString (); +} + #endif diff -r 6504a1932637 -r 0e6f7b5f6556 libgui/src/m-editor/file-editor-tab.h --- a/libgui/src/m-editor/file-editor-tab.h Mon Jun 16 12:11:21 2014 +0200 +++ b/libgui/src/m-editor/file-editor-tab.h Mon Jun 16 17:40:53 2014 +0200 @@ -208,6 +208,7 @@ void center_current_line (); void add_octave_apis (octave_value_list key_ovl); + QString get_function_name (); octave_qscintilla *_edit_area;