Use more descriptive terminology for preferred text editor.

Also improves execution to allow the preferred editor to include
parameters, such as "/usr/bin/open -e".

Fixes https://gitlab.com/kicad/code/kicad/issues/9131
This commit is contained in:
Jeff Young 2021-09-19 14:45:27 +01:00
parent e7814915db
commit 6e7ce09572
15 changed files with 79 additions and 81 deletions

View File

@ -235,7 +235,7 @@ bool PANEL_COMMON_SETTINGS::TransferDataToWindow()
applySettingsToPanel( *commonSettings ); applySettingsToPanel( *commonSettings );
// TODO(JE) Move these into COMMON_SETTINGS probably // TODO(JE) Move these into COMMON_SETTINGS probably
m_textEditorPath->SetValue( Pgm().GetEditorName( false ) ); m_textEditorPath->SetValue( Pgm().GetTextEditor( false ) );
m_defaultPDFViewer->SetValue( Pgm().UseSystemPdfBrowser() ); m_defaultPDFViewer->SetValue( Pgm().UseSystemPdfBrowser() );
m_otherPDFViewer->SetValue( !Pgm().UseSystemPdfBrowser() ); m_otherPDFViewer->SetValue( !Pgm().UseSystemPdfBrowser() );
m_PDFViewerPath->SetValue( Pgm().GetPdfBrowserName() ); m_PDFViewerPath->SetValue( Pgm().GetPdfBrowserName() );
@ -289,7 +289,7 @@ bool PANEL_COMMON_SETTINGS::TransferDataFromWindow()
commonSettings->m_Session.remember_open_files = m_cbRememberOpenFiles->GetValue(); commonSettings->m_Session.remember_open_files = m_cbRememberOpenFiles->GetValue();
Pgm().SetEditorName( m_textEditorPath->GetValue() ); Pgm().SetTextEditor( m_textEditorPath->GetValue());
Pgm().SetPdfBrowserName( m_PDFViewerPath->GetValue() ); Pgm().SetPdfBrowserName( m_PDFViewerPath->GetValue() );
Pgm().ForceSystemPdfBrowser( m_defaultPDFViewer->GetValue() ); Pgm().ForceSystemPdfBrowser( m_defaultPDFViewer->GetValue() );
@ -310,7 +310,7 @@ void PANEL_COMMON_SETTINGS::ResetPanel()
applySettingsToPanel( defaultSettings ); applySettingsToPanel( defaultSettings );
// TODO(JE) Move these into COMMON_SETTINGS probably // TODO(JE) Move these into COMMON_SETTINGS probably
m_textEditorPath->SetValue( defaultSettings.m_System.editor_name ); m_textEditorPath->SetValue( defaultSettings.m_System.text_editor );
m_defaultPDFViewer->SetValue( defaultSettings.m_System.use_system_pdf_viewer ); m_defaultPDFViewer->SetValue( defaultSettings.m_System.use_system_pdf_viewer );
m_otherPDFViewer->SetValue( !defaultSettings.m_System.use_system_pdf_viewer ); m_otherPDFViewer->SetValue( !defaultSettings.m_System.use_system_pdf_viewer );
m_PDFViewerPath->SetValue( defaultSettings.m_System.pdf_viewer_name ); m_PDFViewerPath->SetValue( defaultSettings.m_System.pdf_viewer_name );
@ -430,7 +430,7 @@ void PANEL_COMMON_SETTINGS::OnTextEditorClick( wxCommandEvent& event )
// Ask the user to select a new editor, but suggest the current one as the default. // Ask the user to select a new editor, but suggest the current one as the default.
wxString editorname = Pgm().AskUserForPreferredEditor( m_textEditorPath->GetValue() ); wxString editorname = Pgm().AskUserForPreferredEditor( m_textEditorPath->GetValue() );
// If we have a new editor name request it to be copied to m_editor_name and saved // If we have a new editor name request it to be copied to m_text_editor and saved
// to the preferences file. If the user cancelled the dialog then the previous // to the preferences file. If the user cancelled the dialog then the previous
// value will be retained. // value will be retained.
if( !editorname.IsEmpty() ) if( !editorname.IsEmpty() )

View File

@ -39,7 +39,7 @@
#include <core/arraydim.h> #include <core/arraydim.h>
#include <gestfich.h> #include <gestfich.h>
void AddDelimiterString( wxString& string ) void QuoteString( wxString& string )
{ {
if( !string.StartsWith( wxT( "\"" ) ) ) if( !string.StartsWith( wxT( "\"" ) ) )
{ {
@ -112,22 +112,34 @@ wxString FindKicadFile( const wxString& shortname )
} }
int ExecuteFile( wxWindow* frame, const wxString& ExecFile, const wxString& param, int ExecuteFile( const wxString& ExecFile, const wxString& param, wxProcess *callback )
wxProcess *callback )
{ {
wxString fullFileName = FindKicadFile( ExecFile ); wxString fullFileName;
wxString fullParams;
int space = ExecFile.Find( ' ' );
if( space > 0 && !ExecFile.Contains( "\"" ) && !ExecFile.Contains( "\'" ) )
{
fullFileName = FindKicadFile( ExecFile.Mid( 0, space ) );
fullParams = ExecFile.Mid( space + 1 ) + wxS( " " ) + param;
}
else
{
fullFileName = FindKicadFile( ExecFile );
fullParams = param;
}
if( wxFileExists( fullFileName ) ) if( wxFileExists( fullFileName ) )
{ {
if( !param.IsEmpty() ) if( !fullParams.IsEmpty() )
fullFileName += wxT( " " ) + param; fullFileName += wxS( " " ) + fullParams;
return ProcessExecute( fullFileName, wxEXEC_ASYNC, callback ); return ProcessExecute( fullFileName, wxEXEC_ASYNC, callback );
} }
#ifdef __WXMAC__ #ifdef __WXMAC__
else else
{ {
AddDelimiterString( fullFileName ); QuoteString( fullFileName );
if( !param.IsEmpty() ) if( !param.IsEmpty() )
fullFileName += wxT( " " ) + param; fullFileName += wxT( " " ) + param;
@ -135,11 +147,6 @@ int ExecuteFile( wxWindow* frame, const wxString& ExecFile, const wxString& para
return ProcessExecute( wxT( "/usr/bin/open -a " ) + fullFileName, wxEXEC_ASYNC, callback ); return ProcessExecute( wxT( "/usr/bin/open -a " ) + fullFileName, wxEXEC_ASYNC, callback );
} }
#endif #endif
wxString msg;
msg.Printf( _( "Command '%s' could not be found." ), fullFileName );
DisplayError( frame, msg, 20 );
return -1;
} }

View File

@ -135,17 +135,16 @@ wxApp& PGM_BASE::App()
} }
void PGM_BASE::SetEditorName( const wxString& aFileName ) void PGM_BASE::SetTextEditor( const wxString& aFileName )
{ {
m_editor_name = aFileName; m_text_editor = aFileName;
wxASSERT( GetCommonSettings() ); GetCommonSettings()->m_System.text_editor = aFileName;
GetCommonSettings()->m_System.editor_name = aFileName;
} }
const wxString& PGM_BASE::GetEditorName( bool aCanShowFileChooser ) const wxString& PGM_BASE::GetTextEditor( bool aCanShowFileChooser )
{ {
wxString editorname = m_editor_name; wxString editorname = m_text_editor;
if( !editorname ) if( !editorname )
{ {
@ -153,7 +152,7 @@ const wxString& PGM_BASE::GetEditorName( bool aCanShowFileChooser )
{ {
// If there is no EDITOR variable set, try the desktop default // If there is no EDITOR variable set, try the desktop default
#ifdef __WXMAC__ #ifdef __WXMAC__
editorname = "/usr/bin/open"; editorname = "/usr/bin/open -e";
#elif __WXX11__ #elif __WXX11__
editorname = "/usr/bin/xdg-open"; editorname = "/usr/bin/xdg-open";
#endif #endif
@ -163,19 +162,19 @@ const wxString& PGM_BASE::GetEditorName( bool aCanShowFileChooser )
// If we still don't have an editor name show a dialog asking the user to select one // If we still don't have an editor name show a dialog asking the user to select one
if( !editorname && aCanShowFileChooser ) if( !editorname && aCanShowFileChooser )
{ {
DisplayInfoMessage( nullptr, _( "No default editor found, you must choose it" ) ); DisplayInfoMessage( nullptr, _( "No default editor found, you must choose one." ) );
editorname = AskUserForPreferredEditor(); editorname = AskUserForPreferredEditor();
} }
// If we finally have a new editor name request it to be copied to m_editor_name and // If we finally have a new editor name request it to be copied to m_text_editor and
// saved to the preferences file. // saved to the preferences file.
if( !editorname.IsEmpty() ) if( !editorname.IsEmpty() )
SetEditorName( editorname ); SetTextEditor( editorname );
// m_editor_name already has the same value that editorname, or empty if no editor was // m_text_editor already has the same value that editorname, or empty if no editor was
// found/chosen. // found/chosen.
return m_editor_name; return m_text_editor;
} }
@ -344,7 +343,7 @@ bool PGM_BASE::setExecutablePath()
void PGM_BASE::loadCommonSettings() void PGM_BASE::loadCommonSettings()
{ {
m_editor_name = GetCommonSettings()->m_System.editor_name; m_text_editor = GetCommonSettings()->m_System.text_editor;
for( const std::pair<wxString, ENV_VAR_ITEM> it : GetCommonSettings()->m_Env.vars ) for( const std::pair<wxString, ENV_VAR_ITEM> it : GetCommonSettings()->m_Env.vars )
{ {

View File

@ -280,8 +280,13 @@ COMMON_SETTINGS::COMMON_SETTINGS() :
m_params.emplace_back( new PARAM<int>( "system.autosave_interval", m_params.emplace_back( new PARAM<int>( "system.autosave_interval",
&m_System.autosave_interval, 600 ) ); &m_System.autosave_interval, 600 ) );
m_params.emplace_back( new PARAM<wxString>( "system.editor_name", #ifdef __WXMAC__
&m_System.editor_name, "" ) ); m_params.emplace_back( new PARAM<wxString>( "system.text_editor",
&m_System.text_editor, "/usr/bin/open -e" ) );
#else
m_params.emplace_back( new PARAM<wxString>( "system.text_editor",
&m_System.text_editor, "" ) );
#endif
m_params.emplace_back( new PARAM<int>( "system.file_history_size", m_params.emplace_back( new PARAM<int>( "system.file_history_size",
&m_System.file_history_size, 9 ) ); &m_System.file_history_size, 9 ) );

View File

@ -96,11 +96,11 @@ void DIALOG_CONFIG_EQUFILES::Init()
void DIALOG_CONFIG_EQUFILES::OnEditEquFile( wxCommandEvent& event ) void DIALOG_CONFIG_EQUFILES::OnEditEquFile( wxCommandEvent& event )
{ {
wxString editorname = Pgm().GetEditorName(); wxString editorname = Pgm().GetTextEditor();
if( editorname.IsEmpty() ) if( editorname.IsEmpty() )
{ {
wxMessageBox( _( "No editor defined in KiCad. Please choose it." ) ); wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) );
return; return;
} }
@ -116,7 +116,7 @@ void DIALOG_CONFIG_EQUFILES::OnEditEquFile( wxCommandEvent& event )
m_ListChanged = true; m_ListChanged = true;
} }
ExecuteFile( this, editorname, fullFileNames ); ExecuteFile( editorname, fullFileNames );
} }

View File

@ -440,11 +440,11 @@ void DIALOG_BOM::OnEditGenerator( wxCommandEvent& event )
return; return;
} }
AddDelimiterString( pluginFile ); QuoteString( pluginFile );
wxString editorname = Pgm().GetEditorName(); wxString editorname = Pgm().GetTextEditor();
if( !editorname.IsEmpty() ) if( !editorname.IsEmpty() )
ExecuteFile( this, editorname, pluginFile ); ExecuteFile( editorname, pluginFile );
else else
wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) ); wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) );
} }

View File

@ -980,7 +980,7 @@ void SCH_EDIT_FRAME::OnOpenPcbnew( wxCommandEvent& event )
if( Kiface().IsSingle() ) if( Kiface().IsSingle() )
{ {
wxString filename = QuoteFullPath( boardfn ); wxString filename = QuoteFullPath( boardfn );
ExecuteFile( this, PCBNEW_EXE, filename ); ExecuteFile( PCBNEW_EXE, filename );
} }
else else
{ {
@ -1006,7 +1006,7 @@ void SCH_EDIT_FRAME::OnOpenPcbnew( wxCommandEvent& event )
{ {
// If we are running inside a project, it should be impossible for this case to happen // If we are running inside a project, it should be impossible for this case to happen
wxASSERT( Kiface().IsSingle() ); wxASSERT( Kiface().IsSingle() );
ExecuteFile( this, PCBNEW_EXE ); ExecuteFile( PCBNEW_EXE );
} }
} }

View File

@ -160,7 +160,7 @@ int GERBVIEW_INSPECTION_TOOL::ShowSource( const TOOL_EVENT& aEvent )
if( gerber_layer ) if( gerber_layer )
{ {
wxString editorname = Pgm().GetEditorName(); wxString editorname = Pgm().GetTextEditor();
if( !editorname.IsEmpty() ) if( !editorname.IsEmpty() )
{ {
@ -177,12 +177,12 @@ int GERBVIEW_INSPECTION_TOOL::ShowSource( const TOOL_EVENT& aEvent )
} }
else else
{ {
ExecuteFile( m_frame, editorname, QuoteFullPath( fn ) ); ExecuteFile( editorname, QuoteFullPath( fn ) );
} }
} }
else else
{ {
wxMessageBox( _( "No editor defined. Please select one." ) ); wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) );
} }
} }
else else

View File

@ -22,16 +22,8 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/ */
/** #ifndef GESTFICH_H
* This file is part of the common library #define GESTFICH_H
* TODO brief description
* @file gestfich.h
* @see common.h
*/
#ifndef __INCLUDE__GESTFICH_H__
#define __INCLUDE__GESTFICH_H__ 1
#include <wx/filename.h> #include <wx/filename.h>
#include <wx/process.h> #include <wx/process.h>
@ -69,15 +61,15 @@ void KiCopyFile( const wxString& aSrcPath, const wxString& aDestPath, wxString&
/** /**
* Call the executable file \a ExecFile with the command line parameters \a param. * Call the executable file \a ExecFile with the command line parameters \a param.
*/ */
int ExecuteFile( wxWindow* frame, const wxString& ExecFile, int ExecuteFile( const wxString& ExecFile, const wxString& param = wxEmptyString,
const wxString& param = wxEmptyString, wxProcess* callback = nullptr ); wxProcess* callback = nullptr );
/** /**
* Add un " to the start and the end of string (if not already done). * Add un " to the start and the end of string (if not already done).
* *
* @param string string to modify. * @param string string to modify.
*/ */
void AddDelimiterString( wxString& string ); void QuoteString( wxString& string );
/** /**
* Search the executable file shortname in KiCad binary path and return full file * Search the executable file shortname in KiCad binary path and return full file
@ -103,4 +95,4 @@ wxString FindKicadFile( const wxString& shortname );
*/ */
extern wxString QuoteFullPath( wxFileName& fn, wxPathFormat format = wxPATH_NATIVE ); extern wxString QuoteFullPath( wxFileName& fn, wxPathFormat format = wxPATH_NATIVE );
#endif /* __INCLUDE__GESTFICH_H__ */ #endif /* GESTFICH_H */

View File

@ -130,10 +130,10 @@ public:
virtual COMMON_SETTINGS* GetCommonSettings() const; virtual COMMON_SETTINGS* GetCommonSettings() const;
virtual void SetEditorName( const wxString& aFileName ); virtual void SetTextEditor( const wxString& aFileName );
/** /**
* Return the preferred editor name. * Return the path to the preferred text editor application.
* *
* @param aCanShowFileChooser If no editor is currently set and this argument is * @param aCanShowFileChooser If no editor is currently set and this argument is
* 'true' then this method will show a file chooser dialog asking for the * 'true' then this method will show a file chooser dialog asking for the
@ -141,7 +141,7 @@ public:
* @return Returns the full path of the editor, or an empty string if no editor has * @return Returns the full path of the editor, or an empty string if no editor has
* been set. * been set.
*/ */
virtual const wxString& GetEditorName( bool aCanShowFileChooser = true ); virtual const wxString& GetTextEditor( bool aCanShowFileChooser = true );
/** /**
* Shows a dialog that instructs the user to select a new preferred editor. * Shows a dialog that instructs the user to select a new preferred editor.
@ -306,29 +306,21 @@ protected:
*/ */
bool setExecutablePath(); bool setExecutablePath();
protected:
std::unique_ptr<SETTINGS_MANAGER> m_settings_manager; std::unique_ptr<SETTINGS_MANAGER> m_settings_manager;
std::unique_ptr<SCRIPTING> m_python_scripting; std::unique_ptr<SCRIPTING> m_python_scripting;
/// full path to this program wxString m_bin_dir; /// full path to this program
wxString m_bin_dir; wxString m_kicad_env; /// The KICAD system environment variable.
/// The KICAD system environment variable.
wxString m_kicad_env;
/// The current locale.
wxLocale* m_locale; wxLocale* m_locale;
/// The current language setting.
int m_language_id; int m_language_id;
/// true to use the selected PDF browser, if exists, or false to use the default
bool m_use_system_pdf_browser; bool m_use_system_pdf_browser;
wxString m_pdf_browser; /// Filename of the app selected for browsing PDFs
/// The file name of the the program selected for browsing pdf files. wxString m_text_editor;
wxString m_pdf_browser;
wxString m_editor_name;
}; };

View File

@ -109,7 +109,7 @@ public:
struct SYSTEM struct SYSTEM
{ {
int autosave_interval; int autosave_interval;
wxString editor_name; wxString text_editor;
int file_history_size; int file_history_size;
wxString language; wxString language;
wxString pdf_viewer_name; wxString pdf_viewer_name;

View File

@ -577,7 +577,7 @@ void KICAD_MANAGER_FRAME::OnOpenFileInTextEditor( wxCommandEvent& event )
wxString filename = wxT( "\"" ); wxString filename = wxT( "\"" );
filename += dlg.GetPath() + wxT( "\"" ); filename += dlg.GetPath() + wxT( "\"" );
if( !dlg.GetPath().IsEmpty() && !Pgm().GetEditorName().IsEmpty() ) if( !dlg.GetPath().IsEmpty() && !Pgm().GetTextEditor().IsEmpty() )
m_toolManager->RunAction( KICAD_MANAGER_ACTIONS::openTextEditor, true, &filename ); m_toolManager->RunAction( KICAD_MANAGER_ACTIONS::openTextEditor, true, &filename );
} }

View File

@ -817,10 +817,13 @@ void PROJECT_TREE_PANE::onRight( wxTreeEvent& Event )
void PROJECT_TREE_PANE::onOpenSelectedFileWithTextEditor( wxCommandEvent& event ) void PROJECT_TREE_PANE::onOpenSelectedFileWithTextEditor( wxCommandEvent& event )
{ {
wxString editorname = Pgm().GetEditorName(); wxString editorname = Pgm().GetTextEditor();
if( editorname.IsEmpty() ) if( editorname.IsEmpty() )
{
wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) );
return; return;
}
std::vector<PROJECT_TREE_ITEM*> tree_data = GetSelectedData(); std::vector<PROJECT_TREE_ITEM*> tree_data = GetSelectedData();
@ -829,7 +832,7 @@ void PROJECT_TREE_PANE::onOpenSelectedFileWithTextEditor( wxCommandEvent& event
for( PROJECT_TREE_ITEM* item_data : tree_data ) for( PROJECT_TREE_ITEM* item_data : tree_data )
{ {
wxString fullFileName = item_data->GetFileName(); wxString fullFileName = item_data->GetFileName();
AddDelimiterString( fullFileName ); QuoteString( fullFileName );
if( !files.IsEmpty() ) if( !files.IsEmpty() )
files += " "; files += " ";
@ -837,7 +840,7 @@ void PROJECT_TREE_PANE::onOpenSelectedFileWithTextEditor( wxCommandEvent& event
files += fullFileName; files += fullFileName;
} }
ExecuteFile( this, editorname, files ); ExecuteFile( editorname, files );
} }

View File

@ -751,7 +751,7 @@ int KICAD_MANAGER_CONTROL::Execute( const TOOL_EVENT& aEvent )
else if( aEvent.IsAction( &KICAD_MANAGER_ACTIONS::editDrawingSheet ) ) else if( aEvent.IsAction( &KICAD_MANAGER_ACTIONS::editDrawingSheet ) )
execFile = PL_EDITOR_EXE; execFile = PL_EDITOR_EXE;
else if( aEvent.IsAction( &KICAD_MANAGER_ACTIONS::openTextEditor ) ) else if( aEvent.IsAction( &KICAD_MANAGER_ACTIONS::openTextEditor ) )
execFile = Pgm().GetEditorName(); execFile = Pgm().GetTextEditor();
else if( aEvent.IsAction( &KICAD_MANAGER_ACTIONS::editOtherSch ) ) else if( aEvent.IsAction( &KICAD_MANAGER_ACTIONS::editOtherSch ) )
execFile = EESCHEMA_EXE; execFile = EESCHEMA_EXE;
else if( aEvent.IsAction( &KICAD_MANAGER_ACTIONS::editOtherPCB ) ) else if( aEvent.IsAction( &KICAD_MANAGER_ACTIONS::editOtherPCB ) )
@ -777,11 +777,11 @@ int KICAD_MANAGER_CONTROL::Execute( const TOOL_EVENT& aEvent )
params = m_frame->Prj().GetProjectPath(); params = m_frame->Prj().GetProjectPath();
if( !params.empty() ) if( !params.empty() )
AddDelimiterString( params ); QuoteString( params );
TERMINATE_HANDLER* callback = new TERMINATE_HANDLER( execFile ); TERMINATE_HANDLER* callback = new TERMINATE_HANDLER( execFile );
long pid = ExecuteFile( m_frame, execFile, params, callback ); long pid = ExecuteFile( execFile, params, callback );
if( pid > 0 ) if( pid > 0 )
{ {

View File

@ -1506,7 +1506,7 @@ void PCB_EDIT_FRAME::RunEeschema()
if( Kiface().IsSingle() ) if( Kiface().IsSingle() )
{ {
wxString filename = wxT( "\"" ) + schematic.GetFullPath( wxPATH_NATIVE ) + wxT( "\"" ); wxString filename = wxT( "\"" ) + schematic.GetFullPath( wxPATH_NATIVE ) + wxT( "\"" );
ExecuteFile( this, EESCHEMA_EXE, filename ); ExecuteFile( EESCHEMA_EXE, filename );
} }
else else
{ {