Skip to content

Commit

Permalink
Fix python script editor "open in external editor" action
Browse files Browse the repository at this point in the history
When a system has a EDITOR environment variable set to either
an invalid editor, or an editor which requires a terminal (eg
nano/vim), then fallback to the QDesktopServices approach
to opening the script

Avoids this button doing nothing.
  • Loading branch information
nyalldawson committed Jun 5, 2024
1 parent 2621d95 commit e1f51c3
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions python/console/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Some portions of code were taken from https://code.google.com/p/pydee/
"""
import os
import subprocess

from qgis.PyQt.QtCore import Qt, QTimer, QCoreApplication, QSize, QByteArray, QFileInfo, QUrl, QDir
from qgis.PyQt.QtWidgets import QToolBar, QToolButton, QWidget, QSplitter, QTreeWidget, QAction, QFileDialog, QCheckBox, QSizePolicy, QMenu, QGridLayout, QApplication, QShortcut
Expand Down Expand Up @@ -588,10 +589,21 @@ def reformatCode(self):
def openScriptFileExtEditor(self):
tabWidget = self.tabEditorWidget.currentWidget()
path = tabWidget.path
import subprocess
try:
subprocess.Popen([os.environ['EDITOR'], path])
except KeyError:

editor_command = os.environ.get('EDITOR')
if editor_command:
child = subprocess.Popen([os.environ['EDITOR'], path])
try:
# let's see if the EDITOR drops out immediately....
child.wait(0.01)
rc = child.poll()
if rc:
# editor failed, use backup approach
QDesktopServices.openUrl(QUrl.fromLocalFile(path))
except subprocess.TimeoutExpired:
# looks like EDITOR started up successfully, all is good
pass
else:
QDesktopServices.openUrl(QUrl.fromLocalFile(path))

def openScriptFile(self):
Expand Down

0 comments on commit e1f51c3

Please sign in to comment.