GNU Octave  6.2.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-2021 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 
38 namespace octave
39 {
41  base_qobject& oct_qobj)
42  : QWidget (p), m_octave_qobj (oct_qobj)
43  { }
44 
45  // Calling the external editor
46  bool
48  {
49  if (line > -1) // check for a specific line (debugging)
50  return true; // yes: do not open a file in external editor
51  else
52  line = 0; // no: start external editor at beginning of file
53 
54  QString editor = external_editor ();
55  if (editor.isEmpty ())
56  return true;
57 
58  // replace macros
59  editor.replace ("%f", file);
60  editor.replace ("%l", QString::number (line));
61 
62  QStringList arguments;
63 
64  // start the process and check for success
65  bool started_ok = QProcess::startDetached (editor, arguments);
66 
67  if (started_ok != true)
68  {
69  QMessageBox *msgBox = new QMessageBox (QMessageBox::Critical,
70  tr ("Octave Editor"),
71  tr ("Could not start custom file editor\n%1").
72  arg (editor),
73  QMessageBox::Ok);
74 
75  msgBox->setWindowModality (Qt::NonModal);
76  msgBox->setAttribute (Qt::WA_DeleteOnClose);
77  msgBox->show ();
78  }
79 
80  return started_ok;
81  }
82 
83 
84  // Slots for the several signals for invoking the editor
85 
87  {
89  }
90 
91  void external_editor_interface::request_open_file (const QString& file_name,
92  const QString&, int line,
93  bool, bool, bool,
94  const QString&)
95  {
96  call_custom_editor (file_name, line);
97  }
98 
100  {
101  call_custom_editor (file);
102  }
103 
104  // Get and verify the settings of the external editor program
106  {
109  QString editor = settings->value (global_custom_editor.key,
110  global_custom_editor.def).toString ();
111 
112  // check the settings (avoid an empty string)
113  if (editor.trimmed ().isEmpty ())
114  {
115  QMessageBox *msgBox
116  = new QMessageBox (QMessageBox::Warning,
117  tr ("Octave Editor"),
118  tr ("There is no custom editor configured yet.\n"
119  "Do you want to open the preferences?"),
120  QMessageBox::No | QMessageBox::Yes);
121  msgBox->setDefaultButton (QMessageBox::Yes);
122  msgBox->setAttribute (Qt::WA_DeleteOnClose);
123 
124  int button = msgBox->exec ();
125 
126  if (button == QMessageBox::Yes)
127  emit request_settings_dialog ("editor");
128  }
129 
130  return editor;
131  }
132 }
Base class for Octave interfaces that use Qt.
resource_manager & get_resource_manager(void)
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="")
bool call_custom_editor(const QString &file=QString(), int line=-1)
external_editor_interface(QWidget *main_win, base_qobject &oct_qobj)
void handle_edit_file_request(const QString &file)
void request_settings_dialog(const QString &)
gui_settings * get_settings(void) const
const gui_pref global_custom_editor("customFileEditor", QVariant("emacs +%l %f"))
const QString key
const QVariant def