Skip to content

Commit 8f877dc

Browse files
committed
Let bash compute COMP_POINT, always at the end of COMP_LINE.
This change simplifies the code and also avoids issues in situations where Emacs and Bash have a different idea of what the character count should be, when using multibyte encodings.
1 parent 4c216c4 commit 8f877dc

File tree

2 files changed

+3
-24
lines changed

2 files changed

+3
-24
lines changed

bash-completion.el

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ Bash processes.")
276276
(:conc-name bash-completion--)
277277
(:copier nil))
278278
line ; the relevant command (string)
279-
point ; 0-based position of the cursor in line (number)
280279
words ; line split into words, unescaped (list of strings)
281280
cword ; 0-based index of the word to be completed in words (number)
282281
unparsed-stub ; unparsed version of the thing we are completing,
@@ -629,7 +628,6 @@ Returns a completion struct."
629628
(cdr (assq 'str (car (last line-tokens)))))))
630629
(bash-completion--make
631630
:line rebuilt-line
632-
:point (length rebuilt-line)
633631
:cword (- (length words) 1)
634632
:words words
635633
:stub-start stub-start
@@ -1336,9 +1334,8 @@ completion candidates."
13361334
(setcar (cdr function) "__emacs_complete_wrapper")
13371335
(format "__EMACS_COMPLETE_WRAPPER=%s compgen %s -- %s"
13381336
(bash-completion-quote
1339-
(format "COMP_LINE=%s; COMP_POINT=%s; COMP_CWORD=%s; COMP_WORDS=( %s ); %s %s %s %s"
1337+
(format "COMP_LINE=%s; COMP_POINT=$(( 1 + ${#COMP_LINE} )); COMP_CWORD=%s; COMP_WORDS=( %s ); %s %s %s %s"
13401338
(bash-completion-quote (bash-completion--line comp))
1341-
(bash-completion--point comp)
13421339
(bash-completion--cword comp)
13431340
(bash-completion-join (bash-completion--words comp))
13441341
(bash-completion-quote function-name)

test/bash-completion-test.el

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ The return value is the one returned by BODY."
145145
(should (equal
146146
(bash-completion--make
147147
:line "a hello world"
148-
:point 13
149148
:cword 2
150149
:words '("a" "hello" "world")
151150
:stub-start 9
@@ -160,7 +159,6 @@ The return value is the one returned by BODY."
160159
(should (equal
161160
(bash-completion--make
162161
:line "a hello "
163-
:point 8
164162
:cword 2
165163
:words '("a" "hello" "")
166164
:stub-start 9
@@ -175,7 +173,6 @@ The return value is the one returned by BODY."
175173
(should (equal
176174
(bash-completion--make
177175
:line "make -"
178-
:point 6
179176
:cword 1
180177
:words '("make" "-")
181178
:stub-start 27
@@ -190,7 +187,6 @@ The return value is the one returned by BODY."
190187
(should (equal
191188
(bash-completion--make
192189
:line "sort -"
193-
:point 6
194190
:cword 1
195191
:words '("sort" "-")
196192
:stub-start 20
@@ -205,7 +201,6 @@ The return value is the one returned by BODY."
205201
(should (equal
206202
(bash-completion--make
207203
:line "find -name '*.txt' -exec echo {} ';' -"
208-
:point 38
209204
:cword 7
210205
:words '("find" "-name" "*.txt" "-exec" "echo" "{}" ";" "-")
211206
:stub-start 38
@@ -220,7 +215,6 @@ The return value is the one returned by BODY."
220215
(should (equal
221216
(bash-completion--make
222217
:line "ZORG=t"
223-
:point 6
224218
:cword 0
225219
:words '("ZORG=t")
226220
:stub-start 24
@@ -235,7 +229,6 @@ The return value is the one returned by BODY."
235229
(should (equal
236230
(bash-completion--make
237231
:line "export PATH=/bin:/usr/bi"
238-
:point 24
239232
:cword 1
240233
:words '("export" "PATH=/bin:/usr/bi")
241234
:stub-start 18
@@ -250,7 +243,6 @@ The return value is the one returned by BODY."
250243
(should (equal
251244
(bash-completion--make
252245
:line "export PATH=/bin:/usr/bi"
253-
:point 24
254246
:cword 5
255247
:words '("export" "PATH" "=" "/bin" ":" "/usr/bi")
256248
:stub-start 18
@@ -265,7 +257,6 @@ The return value is the one returned by BODY."
265257
(should (equal
266258
(bash-completion--make
267259
:line "cd /vcr/shows/Dexter's"
268-
:point 22
269260
:cword 1
270261
:words '("cd" "/vcr/shows/Dexter's")
271262
:stub-start 4
@@ -280,7 +271,6 @@ The return value is the one returned by BODY."
280271
(should (equal
281272
(bash-completion--make
282273
:line "cd /vcr/shows/Dexter's"
283-
:point 22
284274
:cword 1
285275
:words '("cd" "/vcr/shows/Dexter's")
286276
:stub-start 4
@@ -295,7 +285,6 @@ The return value is the one returned by BODY."
295285
(should (equal
296286
(bash-completion--make
297287
:line "cd /vcr/shows/Dexter's"
298-
:point 22
299288
:cword 1
300289
:words '("cd" "/vcr/shows/Dexter's")
301290
:stub-start 4
@@ -311,7 +300,6 @@ The return value is the one returned by BODY."
311300
(should (equal
312301
(bash-completion--make
313302
:line "cd /vcr/shows/Dexter's"
314-
:point 22
315303
:cword 1
316304
:words '("cd" "/vcr/shows/Dexter's")
317305
:stub-start 4
@@ -327,7 +315,6 @@ The return value is the one returned by BODY."
327315
(should (equal
328316
(bash-completion--make
329317
:line ""
330-
:point 0
331318
:cword 0
332319
:words '("")
333320
:stub-start 2
@@ -380,7 +367,6 @@ garbage
380367
(bash-completion-generate-line
381368
(bash-completion--make
382369
:line "hello worl"
383-
:point 7
384370
:words '("hello" "worl")
385371
:stub "worl"
386372
:unparsed-stub "worl"
@@ -392,7 +378,6 @@ garbage
392378
(bash-completion-generate-line
393379
(bash-completion--make
394380
:line "zorg worl"
395-
:point 7
396381
:words '("zorg" "worl")
397382
:stub "worl"
398383
:unparsed-stub "worl"
@@ -404,14 +389,13 @@ garbage
404389
(concat
405390
"cd >/dev/null 2>&1 /test && "
406391
"__EMACS_COMPLETE_WRAPPER='COMP_LINE='\\''zorg blah worl'\\''; "
407-
"COMP_POINT=12; COMP_CWORD=2; "
392+
"COMP_POINT=$(( 1 + ${#COMP_LINE} )); COMP_CWORD=2; "
408393
"COMP_WORDS=( zorg blah worl ); "
409394
"__zorg zorg worl blah' "
410395
"compgen -F __emacs_complete_wrapper -- worl 2>/dev/null")
411396
(bash-completion-generate-line
412397
(bash-completion--make
413398
:line "zorg blah worl"
414-
:point 12
415399
:words '("zorg" "blah" "worl")
416400
:cword 2
417401
:stub "worl"
@@ -423,15 +407,14 @@ garbage
423407
(concat
424408
"cd >/dev/null 2>&1 /test && "
425409
"__EMACS_COMPLETE_WRAPPER='COMP_LINE='\\''zorg worl'\\''; "
426-
"COMP_POINT=7; "
410+
"COMP_POINT=$(( 1 + ${#COMP_LINE} )); "
427411
"COMP_CWORD=1; "
428412
"COMP_WORDS=( zorg worl ); "
429413
"__zorg zorg worl zorg' "
430414
"compgen -F __emacs_complete_wrapper -- worl 2>/dev/null")
431415
(bash-completion-generate-line
432416
(bash-completion--make
433417
:line "zorg worl"
434-
:point 7
435418
:words '("zorg" "worl")
436419
:cword 1
437420
:stub "worl"
@@ -444,7 +427,6 @@ garbage
444427
(bash-completion-generate-line
445428
(bash-completion--make
446429
:line "worl"
447-
:point 7
448430
:words '("worl")
449431
:cword 0
450432
:stub "worl"

0 commit comments

Comments
 (0)