# HG changeset patch # User Torsten # Date 1407770677 -7200 # Node ID e87e65bc71ae08017e2886d750a60a2c24c90898 # Parent 00b1d60500bd76f5bd80b3457b3f6c5e7ed0dad2 improved finding a function file for editing in gui (bug #41509) * octave-qscintilla.h: new signal context_menu_edit_signal * octave-qscintilla.cc (contextmenu_edit): only emit a new signal, handling of this menu action has been moved to file_editor_tab * file-editor-tab.h: new slot handle_context_menu_edit * file-editor-tab.cc (constructor): connect new signal from octave-qscintilla to the related new slot for handling the edit function event; (handle_context_menu_edit): new slot searching for a matching subfunction, a private function or one within the same directory when __which__ returns an empty type; old contents from contextmenu_edit has moved here as well diff -r 00b1d60500bd -r e87e65bc71ae libgui/src/m-editor/file-editor-tab.cc --- a/libgui/src/m-editor/file-editor-tab.cc Sun Aug 10 19:31:42 2014 +0200 +++ b/libgui/src/m-editor/file-editor-tab.cc Mon Aug 11 17:24:37 2014 +0200 @@ -60,6 +60,7 @@ #include "version.h" #include "utils.h" #include "defaults.h" +#include // Make parent null for the file editor tab so that warning // WindowModal messages don't affect grandparents. @@ -95,6 +96,8 @@ connect (_edit_area, SIGNAL (create_context_menu_signal (QMenu*)), this, SLOT (create_context_menu (QMenu*))); + connect (_edit_area, SIGNAL (context_menu_edit_signal (const QString&)), + this, SLOT (handle_context_menu_edit (const QString&))); // create statusbar for row/col indicator _status_bar = new QStatusBar (this); @@ -205,6 +208,108 @@ } void +file_editor_tab::handle_context_menu_edit (const QString& word_at_cursor) +{ + // search for a subfunction in actual file (this is done at first because + // octave finds this function before other with same name in the search path + QRegExp rxfun1 ("^[\t ]*function[^=]+=[\t ]*" + + word_at_cursor + "[\t ]*\\([^\\)]*\\)[\t ]*$"); + QRegExp rxfun2 ("^[\t ]*function[\t ]+" + + word_at_cursor + "[\t ]*\\([^\\)]*\\)[\t ]*$"); + QRegExp rxfun3 ("^[\t ]*function[\t ]+" + + word_at_cursor + "[\t ]*$"); + QRegExp rxfun4 ("^[\t ]*function[^=]+=[\t ]*" + + word_at_cursor + "[\t ]*$"); + + int pos_fct = -1; + QStringList lines = _edit_area->text ().split ("\n"); + + int line; + for (line = 0; line < lines.count (); line++) + { + if ((pos_fct = rxfun1.indexIn (lines.at (line))) != -1) + break; + if ((pos_fct = rxfun2.indexIn (lines.at (line))) != -1) + break; + if ((pos_fct = rxfun3.indexIn (lines.at (line))) != -1) + break; + if ((pos_fct = rxfun4.indexIn (lines.at (line))) != -1) + break; + } + + if (pos_fct > -1) + { // reg expr. found: it is an internal function + _edit_area->setCursorPosition (line, pos_fct); + _edit_area->SendScintilla (2613, line); // SCI_SETFIRSTVISIBLELINE + return; + } + + // Is it a regular function within the search path? (Call __which__) + octave_value_list fct = F__which__ (ovl (word_at_cursor.toStdString ()),0); + octave_map map = fct(0).map_value (); + + QString type = QString::fromStdString ( + map.contents ("type").data ()[0].string_value ()); + QString name = QString::fromStdString ( + map.contents ("name").data ()[0].string_value ()); + + QString message = QString (); + QString filename = QString (); + + if (type == QString("built-in function")) + { // built in function: can't edit + message = tr ("%1 is a built-in function"); + } + else if (type.isEmpty ()) + { + // function not known to octave -> try directory of edited file + QFileInfo file = QFileInfo (_file_name); + file = QFileInfo (QDir (file.canonicalPath ()), word_at_cursor + ".m"); + + if (file.exists ()) + { + filename = file.canonicalFilePath (); // local file exists + } + else + { // local file does not exist -> try private directory + file = QFileInfo (_file_name); + file = QFileInfo (QDir (file.canonicalPath () + "/private"), + word_at_cursor + ".m"); + + if (file.exists ()) + { + filename = file.canonicalFilePath (); // private function exists + } + else + { + message = tr ("Can not find function %1"); // no file found + } + } + } + + if (! message.isEmpty ()) + { + QMessageBox *msgBox + = new QMessageBox (QMessageBox::Critical, + tr ("Octave Editor"), + message.arg (name), + QMessageBox::Ok, this); + + msgBox->setWindowModality (Qt::NonModal); + msgBox->setAttribute (Qt::WA_DeleteOnClose); + msgBox->show (); + return; + } + + if ( filename.isEmpty ()) + filename = QString::fromStdString ( + map.contents ("file").data ()[0].string_value ()); + + emit execute_command_in_terminal_signal (QString("edit ") + + "\""+filename+"\""); +} + +void file_editor_tab::set_file_name (const QString& fileName) { // update tracked file if we really have a file on disk diff -r 00b1d60500bd -r e87e65bc71ae libgui/src/m-editor/file-editor-tab.h --- a/libgui/src/m-editor/file-editor-tab.h Sun Aug 10 19:31:42 2014 +0200 +++ b/libgui/src/m-editor/file-editor-tab.h Mon Aug 11 17:24:37 2014 +0200 @@ -120,6 +120,7 @@ void execute_command_in_terminal (const QString& command); void edit_area_has_focus (bool foucs); void create_context_menu (QMenu *); + void handle_context_menu_edit (const QString&); signals: diff -r 00b1d60500bd -r e87e65bc71ae libgui/src/m-editor/octave-qscintilla.cc --- a/libgui/src/m-editor/octave-qscintilla.cc Sun Aug 10 19:31:42 2014 +0200 +++ b/libgui/src/m-editor/octave-qscintilla.cc Mon Aug 11 17:24:37 2014 +0200 @@ -32,7 +32,6 @@ #include #include #include -#include #include "octave-qscintilla.h" #include "file-editor-tab.h" @@ -267,40 +266,7 @@ void octave_qscintilla::contextmenu_edit (bool) { - octave_value_list fct = F__which__ (ovl (_word_at_cursor.toStdString ()),0); - octave_map map = fct(0).map_value (); - - QString type = QString::fromStdString ( - map.contents ("type").data ()[0].string_value ()); - QString name = QString::fromStdString ( - map.contents ("name").data ()[0].string_value ()); - - QString message = QString (); - - if (type.isEmpty ()) - message = tr ("Can not find function %1"); - else if (type == QString("built-in function")) - message = tr ("%1 is a built-in function"); - - if (! message.isEmpty ()) - { - QMessageBox *msgBox - = new QMessageBox (QMessageBox::Critical, - tr ("Octave Editor"), - message.arg (name), - QMessageBox::Ok, this); - - msgBox->setWindowModality (Qt::NonModal); - msgBox->setAttribute (Qt::WA_DeleteOnClose); - msgBox->show (); - return; - } - - QString filename = QString::fromStdString ( - map.contents ("file").data ()[0].string_value ()); - - emit execute_command_in_terminal_signal (QString("edit ") - + "\""+filename+"\""); + emit context_menu_edit_signal (_word_at_cursor); } void diff -r 00b1d60500bd -r e87e65bc71ae libgui/src/m-editor/octave-qscintilla.h --- a/libgui/src/m-editor/octave-qscintilla.h Sun Aug 10 19:31:42 2014 +0200 +++ b/libgui/src/m-editor/octave-qscintilla.h Mon Aug 11 17:24:37 2014 +0200 @@ -51,6 +51,7 @@ void execute_command_in_terminal_signal (const QString&); void create_context_menu_signal (QMenu*); + void context_menu_edit_signal (const QString&); void qsci_has_focus_signal (bool); void status_update (bool,bool);