summaryrefslogtreecommitdiff
blob: c54f27600e34036b7de07a9045e4714191ae8a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
--- libedit-20170329-3.1/src/filecomplete.c	2017-03-29 21:15:04.000000000 +0300
+++ libedit-20170329-3.1/src/filecomplete.c	2017-05-21 02:23:16.000000000 +0300
@@ -51,6 +51,7 @@
 #include "filecomplete.h"
 
 static const wchar_t break_chars[] = L" \t\n\"\\'`@$><=;|&{(";
+static const wchar_t extra_quote_chars[] = L")}*?[$\0";
 
 /********************************/
 /* completion functions */
@@ -405,10 +406,14 @@
 	char **(*attempted_completion_function)(const char *, int, int),
 	const wchar_t *word_break, const wchar_t *special_prefixes,
 	const char *(*app_func)(const char *), size_t query_items,
-	int *completion_type, int *over, int *point, int *end)
+	int *completion_type, int *over, int *point, int *end,
+	const char *(*find_word_start_func)(const char *, const char *),
+	char *(*dequoting_func)(const char *),
+	char *(*quoting_func)(const char *))
 {
 	const LineInfoW *li;
 	wchar_t *temp;
+	char *dequoted_temp;
         char **matches;
 	const wchar_t *ctemp;
 	size_t len;
@@ -429,17 +434,28 @@
 
 	/* We now look backwards for the start of a filename/variable word */
 	li = el_wline(el);
+	if (find_word_start_func)
+		ctemp = ct_decode_string(find_word_start_func(ct_encode_string(li->buffer,&el->el_scratch), ct_encode_string(li->cursor,&el->el_scratch)),&el->el_scratch);
+	else {	
 	ctemp = li->cursor;
 	while (ctemp > li->buffer
 	    && !wcschr(word_break, ctemp[-1])
 	    && (!special_prefixes || !wcschr(special_prefixes, ctemp[-1]) ) )
 		ctemp--;
+	}
 
 	len = (size_t)(li->cursor - ctemp);
 	temp = el_malloc((len + 1) * sizeof(*temp));
 	(void)wcsncpy(temp, ctemp, len);
 	temp[len] = '\0';
 
+	if (dequoting_func) {
+		dequoted_temp = dequoting_func(ct_encode_string(temp,&el->el_scratch));
+		if (dequoted_temp == NULL)
+			return retval;
+	} else
+		dequoted_temp = NULL;
+	
 	/* these can be used by function called in completion_matches() */
 	/* or (*attempted_completion_function)() */
 	if (point != NULL)
@@ -450,14 +466,14 @@
 	if (attempted_completion_function) {
 		int cur_off = (int)(li->cursor - li->buffer);
 		matches = (*attempted_completion_function)(
-		    ct_encode_string(temp, &el->el_scratch),
+		    dequoted_temp? dequoted_temp : ct_encode_string(temp, &el->el_scratch),
 		    cur_off - (int)len, cur_off);
 	} else
 		matches = NULL;
 	if (!attempted_completion_function ||
 	    (over != NULL && !*over && !matches))
 		matches = completion_matches(
-		    ct_encode_string(temp, &el->el_scratch), complet_func);
+		    dequoted_temp? dequoted_temp : ct_encode_string(temp, &el->el_scratch), complet_func);
 
 	if (over != NULL)
 		*over = 0;
@@ -472,9 +488,18 @@
 		 * possible matches if there is possible completion.
 		 */
 		if (matches[0][0] != '\0') {
+			char *quoted_match;
+			if (quoting_func) {
+				quoted_match = quoting_func(matches[0]);
+				if (quoted_match == NULL)
+					goto free_matches;
+			} else
+				quoted_match = NULL;
+		  
 			el_deletestr(el, (int) len);
 			el_winsertstr(el,
-			    ct_decode_string(matches[0], &el->el_scratch));
+			    ct_decode_string(quoted_match? quoted_match : matches[0], &el->el_scratch));
+			free(quoted_match);
 		}
 
 
@@ -545,12 +570,14 @@
 			retval = CC_NORM;
 		}
 
+free_matches:
 		/* free elements of array and the array itself */
 		for (i = 0; matches[i]; i++)
 			el_free(matches[i]);
 		el_free(matches);
 		matches = NULL;
 	}
+	el_free(dequoted_temp);
 	el_free(temp);
 	return retval;
 }
@@ -564,5 +591,102 @@
 {
 	return (unsigned char)fn_complete(el, NULL, NULL,
 	    break_chars, NULL, NULL, (size_t)100,
-	    NULL, NULL, NULL, NULL);
+	    NULL, NULL, NULL, NULL,
+	    NULL, NULL, NULL);
+}
+
+static const char *
+sh_find_word_start(const char *buffer, const char *cursor)
+{
+	const char *word_start = buffer;
+
+	while (buffer < cursor) {
+		if (*buffer == '\\')
+			buffer++;
+		else if (strchr(break_chars, *buffer))
+			word_start = buffer + 1;
+
+		buffer++;
+	}
+
+	return word_start;
+}
+
+
+static char *
+sh_quote(const char *str)
+{
+	const char *src;
+	int extra_len = 0;
+	char *quoted_str, *dst;
+
+	if (*str == '-' || *str == '+')
+		extra_len += 2;
+	for (src = str; *src != '\0'; src++)
+		if (strchr(break_chars, *src) ||
+		    strchr(extra_quote_chars, *src))
+			extra_len++;
+
+	quoted_str = malloc(sizeof(*quoted_str) *
+	    (strlen(str) + extra_len + 1));
+	if (quoted_str == NULL)
+		return NULL;
+
+	dst = quoted_str;
+	if (*str == '-' || *str == '+')
+		*dst++ = '.', *dst++ = '/';
+	for (src = str; *src != '\0'; src++) {
+		if (strchr(break_chars, *src) ||
+		    strchr(extra_quote_chars, *src))
+			*dst++ = '\\';
+		*dst++ = *src;
+	}
+	*dst = '\0';
+
+	return quoted_str;
+}
+
+
+static char *
+sh_dequote(const char *str)
+{
+	char *dequoted_str, *dst;
+
+	/* save extra space to replace \~ with ./~ */
+	dequoted_str = malloc(sizeof(*dequoted_str) * (strlen(str) + 1 + 1));
+	if (dequoted_str == NULL)
+		return NULL;
+
+	dst = dequoted_str;
+
+	/* dequote \~ at start as ./~ */
+	if (*str == '\\' && str[1] == '~') {
+		str++;
+		*dst++ = '.';
+		*dst++ = '/';
+	}
+
+	while (*str) {
+		if (*str == '\\')
+			str++;
+		if (*str)
+			*dst++ = *str++;
+	}
+	*dst = '\0';
+
+	return dequoted_str;
+}
+
+
+/*
+ * completion function using sh quoting rules; for key binding
+ */
+/* ARGSUSED */
+unsigned char
+_el_fn_sh_complete(EditLine *el, int ch __attribute__((__unused__)))
+{
+	return (unsigned char)fn_complete(el, NULL, NULL,
+	    break_chars, NULL, NULL, 100,
+	    NULL, NULL, NULL, NULL,
+	    sh_find_word_start, sh_dequote, sh_quote);
 }

--- libedit-20170329-3.1/src/readline.c	2017-03-29 21:15:04.000000000 +0300
+++ libedit-20170329-3.1/src/readline.c	2017-05-21 02:24:57.000000000 +0300
@@ -1869,7 +1869,7 @@
 	    _rl_completion_append_character_function,
 	    (size_t)rl_completion_query_items,
 	    &rl_completion_type, &rl_attempted_completion_over,
-	    &rl_point, &rl_end);
+	    &rl_point, &rl_end, NULL, NULL, NULL);
 
 
 }

--- libedit-20170329-3.1/src/histedit.h	2017-03-29 21:08:21.000000000 +0300
+++ libedit-20170329-3.1/src/histedit.h	2017-05-21 02:32:04.000000000 +0300
@@ -113,6 +113,7 @@
 int		 el_set(EditLine *, int, ...);
 int		 el_get(EditLine *, int, ...);
 unsigned char	_el_fn_complete(EditLine *, int);
+unsigned char	_el_fn_sh_complete(EditLine *, int);  
 
 /*
  * el_set/el_get parameters

--- libedit-20170329-3.1/src/filecomplete.h	2017-03-29 21:08:21.000000000 +0300
+++ libedit-20170329-3.1/src/filecomplete.h	2017-05-21 02:32:27.000000000 +0300
@@ -35,7 +35,10 @@
     char *(*)(const char *, int),
     char **(*)(const char *, int, int),
     const wchar_t *, const wchar_t *, const char *(*)(const char *), size_t,
-    int *, int *, int *, int *);
+    int *, int *, int *, int *,
+    const char *(*)(const char *, const char *),
+    char *(*)(const char *),
+    char *(*)(const char *));
 
 void fn_display_match_list(EditLine *, char **, size_t, size_t);
 char *fn_tilde_expand(const char *);