GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
external-editor-interface.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2017-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <QMessageBox>
31 #include <QProcess>
32 
34 #include "gui-settings.h"
35 #include "gui-preferences-global.h"
36 #include "octave-qobject.h"
37 
39 
41  base_qobject& oct_qobj)
42 : QWidget (p), m_octave_qobj (oct_qobj)
43 { }
44 
45 // Calling the external editor
46 bool
47 external_editor_interface::call_custom_editor (const QString& file, int line)
48 {
49  QString editor = external_editor ();
50  if (editor.isEmpty ())
51  return true;
52 
53  if (line < 0)
54  line = 0;
55 
56  // replace macros
57  editor.replace ("%f", file);
58  editor.replace ("%l", QString::number (line));
59 
60  QStringList arguments = editor.split (QRegExp("\\s+"));
61  editor = arguments.takeFirst ();
62 
63  // start the process and check for success
64  bool started_ok = QProcess::startDetached (editor, arguments);
65 
66  if (started_ok != true)
67  {
68  QMessageBox *msgBox = new QMessageBox (QMessageBox::Critical,
69  tr ("Octave Editor"),
70  tr ("Could not start custom file editor\n%1").
71  arg (editor),
72  QMessageBox::Ok);
73 
74  msgBox->setWindowModality (Qt::NonModal);
75  msgBox->setAttribute (Qt::WA_DeleteOnClose);
76  msgBox->show ();
77  }
78 
79  return started_ok;
80 }
81 
82 // Slots for the several signals for invoking the editor
83 
85 {
87 }
88 
89 void external_editor_interface::request_open_file (const QString& file_name,
90  const QString&, int line,
91  bool, bool, bool,
92  const QString&)
93 {
94  call_custom_editor (file_name, line);
95 }
96 
98 {
99  call_custom_editor (file);
100 }
101 
102 // Get and verify the settings of the external editor program
104 {
107  QString editor = settings->value (global_custom_editor.key,
108  global_custom_editor.def).toString ();
109 
110  // check the settings (avoid an empty string)
111  if (editor.trimmed ().isEmpty ())
112  {
113  QMessageBox *msgBox
114  = new QMessageBox (QMessageBox::Warning,
115  tr ("Octave Editor"),
116  tr ("There is no custom editor configured yet.\n"
117  "Do you want to open the preferences?"),
118  QMessageBox::No | QMessageBox::Yes);
119  msgBox->setDefaultButton (QMessageBox::Yes);
120  msgBox->setAttribute (Qt::WA_DeleteOnClose);
121 
122  int button = msgBox->exec ();
123 
124  if (button == QMessageBox::Yes)
125  emit request_settings_dialog ("editor");
126  }
127 
128  return editor;
129 }
130 
OCTAVE_END_NAMESPACE(octave)
Base class for Octave interfaces that use Qt.
resource_manager & get_resource_manager(void)
void handle_edit_file_request(const QString &file)
void request_new_file(const QString &)
void request_open_file(const QString &fileName, const QString &encoding=QString(), int line=-1, bool debug_pointer=false, bool breakpoint_marker=false, bool insert=true, const QString &cond="")
void request_settings_dialog(const QString &)
bool call_custom_editor(const QString &file=QString(), int line=-1)
gui_settings * get_settings(void) const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
const gui_pref global_custom_editor("customFileEditor", QVariant("emacs +%l %f"))
const QString key
const QVariant def