@@ -8,7 +8,9 @@ use crate::{
8
8
ui:: { self , style:: SharedTheme } ,
9
9
} ;
10
10
use anyhow:: { anyhow, bail, Result } ;
11
- use asyncgit:: { sync:: utils:: repo_work_dir, CWD } ;
11
+ use asyncgit:: {
12
+ sync:: utils:: get_config_string, sync:: utils:: repo_work_dir, CWD ,
13
+ } ;
12
14
use crossterm:: {
13
15
event:: Event ,
14
16
terminal:: { EnterAlternateScreen , LeaveAlternateScreen } ,
@@ -66,26 +68,43 @@ impl ExternalEditorComponent {
66
68
67
69
let editor = env:: var ( "GIT_EDITOR" )
68
70
. ok ( )
71
+ . or_else ( || get_config_string ( CWD , "core.editor" ) . ok ( ) ?)
69
72
. or_else ( || env:: var ( "VISUAL" ) . ok ( ) )
70
73
. or_else ( || env:: var ( "EDITOR" ) . ok ( ) )
71
74
. unwrap_or_else ( || String :: from ( "vi" ) ) ;
72
75
73
76
// TODO: proper handling arguments containing whitespaces
74
77
// This does not do the right thing if the input is `editor --something "with spaces"`
75
- let mut editor = editor. split_whitespace ( ) ;
76
78
77
- let command = editor. next ( ) . ok_or_else ( || {
78
- anyhow ! ( "unable to read editor command" )
79
- } ) ?;
79
+ // deal with "editor name with spaces" p1 p2 p3
80
+ // and with "editor_no_spaces" p1 p2 p3
81
+ // does not address spaces in pn
82
+ let mut echars = editor. chars ( ) . peekable ( ) ;
83
+
84
+ let command: String = if * echars. peek ( ) . ok_or_else ( || {
85
+ anyhow ! ( "editor configuration set to empty string" )
86
+ } ) ? == '\"'
87
+ {
88
+ echars
89
+ . by_ref ( )
90
+ . skip ( 1 )
91
+ . take_while ( |c| * c != '\"' )
92
+ . collect ( )
93
+ } else {
94
+ echars. by_ref ( ) . take_while ( |c| * c != ' ' ) . collect ( )
95
+ } ;
96
+
97
+ let remainder_str = echars. collect :: < String > ( ) ;
98
+ let remainder = remainder_str. split_whitespace ( ) ;
80
99
81
- let mut editor : Vec < & OsStr > =
82
- editor . map ( |s| OsStr :: new ( s) ) . collect ( ) ;
100
+ let mut args : Vec < & OsStr > =
101
+ remainder . map ( |s| OsStr :: new ( s) ) . collect ( ) ;
83
102
84
- editor . push ( path. as_os_str ( ) ) ;
103
+ args . push ( path. as_os_str ( ) ) ;
85
104
86
- Command :: new ( command)
105
+ Command :: new ( command. clone ( ) )
87
106
. current_dir ( work_dir)
88
- . args ( editor )
107
+ . args ( args )
89
108
. status ( )
90
109
. map_err ( |e| anyhow ! ( "\" {}\" : {}" , command, e) ) ?;
91
110
0 commit comments