1
1
/* Yash: yet another shell */
2
2
/* complete.c: command line completion */
3
- /* (C) 2007-2022 magicant */
3
+ /* (C) 2007-2024 magicant */
4
4
5
5
/* This program is free software: you can redistribute it and/or modify
6
6
* it under the terms of the GNU General Public License as published by
@@ -486,10 +486,12 @@ void print_context_info(const le_context_T *ctxt)
486
486
{
487
487
const char * s DUMMY_INIT (NULL );
488
488
switch (ctxt -> quote ) {
489
- case QUOTE_NONE : s = "none" ; break ;
490
- case QUOTE_NORMAL : s = "normal" ; break ;
491
- case QUOTE_SINGLE : s = "single" ; break ;
492
- case QUOTE_DOUBLE : s = "double" ; break ;
489
+ case QUOTE_NONE : s = "none" ; break ;
490
+ case QUOTE_NORMAL : s = "normal" ; break ;
491
+ case QUOTE_NORMAL_ESCAPED : s = "normal escaped" ; break ;
492
+ case QUOTE_SINGLE : s = "single" ; break ;
493
+ case QUOTE_DOUBLE : s = "double" ; break ;
494
+ case QUOTE_DOUBLE_ESCAPED : s = "double escaped" ; break ;
493
495
}
494
496
le_compdebug ("quote type: %s" , s );
495
497
switch (ctxt -> type & CTXT_MASK ) {
@@ -1177,13 +1179,9 @@ size_t get_common_prefix_length(void)
1177
1179
* up after this function. */
1178
1180
void update_main_buffer (bool subst , bool finish )
1179
1181
{
1180
- const le_candidate_T * cand ;
1181
- xwcsbuf_T buf ;
1182
1182
size_t srclen ;
1183
1183
size_t substindex ;
1184
1184
le_quote_T quotetype ;
1185
-
1186
- wb_init (& buf );
1187
1185
if (subst ) {
1188
1186
srclen = 0 ;
1189
1187
substindex = ctxt -> srcindex ;
@@ -1192,8 +1190,29 @@ void update_main_buffer(bool subst, bool finish)
1192
1190
srclen = wcslen (ctxt -> src );
1193
1191
substindex = ctxt -> origindex ;
1194
1192
quotetype = ctxt -> quote ;
1193
+
1194
+ switch (quotetype ) {
1195
+ case QUOTE_NONE :
1196
+ case QUOTE_NORMAL :
1197
+ case QUOTE_SINGLE :
1198
+ case QUOTE_DOUBLE :
1199
+ break ;
1200
+ case QUOTE_NORMAL_ESCAPED :
1201
+ case QUOTE_DOUBLE_ESCAPED :
1202
+ // Get the backslash preceding the cursor removed
1203
+ assert (substindex > 0 );
1204
+ substindex -- ;
1205
+ assert (le_main_buffer .contents [substindex ] == '\\' );
1206
+ break ;
1207
+ }
1195
1208
}
1209
+
1210
+ // Quote the substring of the candidate to be inserted
1211
+ const le_candidate_T * cand ;
1212
+ xwcsbuf_T quoted ;
1213
+ wb_init (& quoted );
1196
1214
if (le_selected_candidate_index >= le_candidates .length ) {
1215
+ // No candidate selected. Quote the longest common prefix.
1197
1216
size_t cpl = get_common_prefix_length ();
1198
1217
assert (srclen <= cpl );
1199
1218
cand = le_candidates .contents [0 ];
@@ -1202,21 +1221,24 @@ void update_main_buffer(bool subst, bool finish)
1202
1221
wchar_t value [valuelen + 1 ];
1203
1222
wcsncpy (value , cand -> origvalue + srclen , valuelen );
1204
1223
value [valuelen ] = L'\0' ;
1205
- quote (& buf , value , quotetype );
1224
+ quote (& quoted , value , quotetype );
1206
1225
} else {
1226
+ // Quote the selected candidate.
1207
1227
cand = le_candidates .contents [le_selected_candidate_index ];
1208
1228
assert (srclen <= wcslen (cand -> origvalue ));
1209
1229
if (cand -> origvalue [0 ] == L'\0' && quotetype == QUOTE_NORMAL )
1210
- wb_cat (& buf , L"\"\"" );
1230
+ wb_cat (& quoted , L"\"\"" );
1211
1231
else
1212
- quote (& buf , cand -> origvalue + srclen , quotetype );
1232
+ quote (& quoted , cand -> origvalue + srclen , quotetype );
1213
1233
}
1234
+
1235
+ // Insert the quoted candidate to the main buffer
1214
1236
assert (le_main_index >= substindex );
1215
1237
wb_replace_force (& le_main_buffer ,
1216
1238
substindex , le_main_index - substindex ,
1217
- buf .contents , buf .length );
1218
- le_main_index = substindex + buf .length ;
1219
- wb_destroy (& buf );
1239
+ quoted .contents , quoted .length );
1240
+ le_main_index = substindex + quoted .length ;
1241
+ wb_destroy (& quoted );
1220
1242
1221
1243
if (le_selected_candidate_index >= le_candidates .length )
1222
1244
return ;
@@ -1227,11 +1249,13 @@ void update_main_buffer(bool subst, bool finish)
1227
1249
switch (quotetype ) {
1228
1250
case QUOTE_NONE :
1229
1251
case QUOTE_NORMAL :
1252
+ case QUOTE_NORMAL_ESCAPED :
1230
1253
break ;
1231
1254
case QUOTE_SINGLE :
1232
1255
insert_to_main_buffer (L'\'' );
1233
1256
break ;
1234
1257
case QUOTE_DOUBLE :
1258
+ case QUOTE_DOUBLE_ESCAPED :
1235
1259
insert_to_main_buffer (L'"' );
1236
1260
break ;
1237
1261
}
@@ -1331,6 +1355,7 @@ void quote(xwcsbuf_T *restrict buf,
1331
1355
wb_cat (buf , s );
1332
1356
return ;
1333
1357
case QUOTE_NORMAL :
1358
+ case QUOTE_NORMAL_ESCAPED :
1334
1359
for (size_t i = 0 ; s [i ] != L'\0' ; i ++ ) {
1335
1360
if (s [i ] == L'\n' ) {
1336
1361
wb_ncat_force (buf , L"'\n'" , 3 );
@@ -1350,6 +1375,7 @@ void quote(xwcsbuf_T *restrict buf,
1350
1375
}
1351
1376
return ;
1352
1377
case QUOTE_DOUBLE :
1378
+ case QUOTE_DOUBLE_ESCAPED :
1353
1379
for (size_t i = 0 ; s [i ] != L'\0' ; i ++ ) {
1354
1380
if (wcschr (CHARS_ESCAPABLE , s [i ]) != NULL )
1355
1381
wb_wccat (buf , L'\\' );
0 commit comments