Commit graph

163 commits

Author SHA1 Message Date
Eric Freese
78e4379711 Fix moving cursor to end of buffer when suggestion accepted
$max_cursor_pos in this case was not the correct value to use. It was
calculated based on the old length of the $BUFFER. After the suggestion
is accepted, we need to recalculate the new max cursor length and use it
to set the $CURSOR.

Fixes issue #452. Follow-up to issue #302 (PR #450).
2019-06-23 12:37:08 -06:00
Eric Freese
c80605595c
Merge pull request #451 from zsh-users/develop
v0.6.2
2019-06-22 16:57:19 -06:00
Eric Freese
676aebdf44 Fix deleting last character in vi mode (#302)
Typing `d` and then `l` runs `vi-delete` and then `vi-forward-char`.  However,
by default, `vi-forward-char` is configured to accept the suggestion. So in
that case, the suggestion was being accepted and the cursor set to the end of
the buffer before the deletion was run.

The reason the user doesn't see the suggestion accepted is that `vi-delete`
doesn't finish until the movement widget is run, so we're already inside of a
`modify` when `accept` is called. `modify` unsets `POSTDISPLAY` before calling
the original widget so when we get to the accept function, `POSTDISPLAY` is
empty and thus accepting the suggestion is a no-op.

The fix is to make sure we reset the cursor to the correct place before running
the original widget.

We skip the test for versions of zsh below 5.0.8 since there was a bug in
earlier versions where deleting the last char did not work.

See http://www.zsh.org/mla/workers/2014/msg01316.html
2019-06-22 16:34:42 -06:00
Eric Freese
a437544cc5 Degrade gracefully on systems missing zsh/system module
When using async mode, stale background processes will not be cancelled
when a new one starts. This shouldn't cause any real issues since the
processes should eventually finish and be cleaned up anyway, and
removing the handler with `zle -F` means that stale suggestions should
never be shown.
2019-06-19 13:41:43 -06:00
Eric Freese
adb02c44a2 Prefix custom _complete implementation with "function" keyword
For some reason, when `_complete` is aliased before sourcing the plugin,
zsh was blowing up with a parse error. Adding "function" keyword makes
it parse successfully.

See similar issue: https://github.com/robbyrussell/oh-my-zsh/issues/6723#issuecomment-381220834

Fixes GitHub #442
2019-06-17 22:04:31 -06:00
Eric Freese
f543ba08c3 Fix cr/lf handling in completion strategy 2019-06-15 21:17:58 -06:00
Eric Freese
68343c8de4 Allow suggestions to suggest trailing newlines
Command substitution via $() trims trailing newlines so the old approach
to reading everything from the fd was preventing suggestions from ending
with newlines.

Found the read solution here: https://stackoverflow.com/a/15184414/154703
2019-06-15 21:17:58 -06:00
Eric Freese
d94475ca1b Simplify logic to extract suggestion from between null bytes
Just remove up to and including the first null byte and after and
including the last null byte.

I also looked into using `${${(0)line}[2]}`, but it fails when `$line`
starts with a null byte, since the first split string will be empty and
thus not included in the resulting array.
2019-06-15 21:17:58 -06:00
Eric Freese
c04e015d13 Add to explanation of extra stuff after zpty -r 2019-06-15 21:17:58 -06:00
Eric Freese
e9ce05c4c1 Ensure that kill succeeds even in older buggy versions of zsh
See https://unix.stackexchange.com/a/477647/156673
2019-06-15 21:17:58 -06:00
Eric Freese
fdf4502c5c We need to autoload is-at-least for it to be available
This error was hidden inside the zpty
2019-06-15 21:17:58 -06:00
Eric Freese
7e048c3f53 Fix error that was hidden in zpty
Without the quotes, this was blowing up with:
  _zsh_autosuggest_capture_postcompletion:unset:5: not enough arguments
2019-06-15 21:17:58 -06:00
Eric Freese
2b05f5ed2d Add a comment explaining post-completion hook 2019-06-15 21:17:58 -06:00
Eric Freese
0d3bbaf8e6 Remove hard-coded ignore of zle-* widgets and add to default config
Use case suggested by @romkatv uses zle-line-init to restore buffer
after running a widget to cd up one level (GitHub #431).

As far as I can tell, the ignoring of zle-line-* was added in commit
9788c2e to support some deprecation warnings that were removed some time
ago.

The pattern was then widened in commit 0c940e7 to zle-* to fix problems
encountered when wrapping zle-isearch-update.

This commit removes the hard coded ignore of all zle-* widgets and adds
zle-* to the default list of widgets to be ignored. Users who want the
plugin to wrap zle-line-init or zle-line-finish can override the
default.
2019-06-14 16:38:28 -06:00
Henré Botha
15bcfd7126 Don't fetch suggestions after copy-earlier-word
Like {up,down}-line-or-beginning-search, this widget relies on
`$LASTWIDGET` being set to function correctly on subsequent invocations.

When asynchronous suggestions are enabled, and the widget triggers a
suggestion to be fetched, `autosuggest-suggest` will be called and
$LASTWIDGET will be set to it.
2019-06-14 13:19:07 -06:00
Eric Freese
474c577f3c Allow setting max size to empty string (GitHub #422)
Some people have gotten used to setting ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE
to an empty string rather than leaving it unset. This lets them continue
to do that without getting any warnings (e.g. "bad math expression:
operand expected at end of string").
2019-05-27 14:18:21 -06:00
Eric Freese
c1910348c7 Implement completion suggestion strategy (#111)
Based on https://github.com/Valodim/zsh-capture-completion

`zpty -r` with a pattern seems to have some funky behavior on older
versions, giving unpredictable results

Don't use `-s` option to `zmodload`. It is not available in zsh versions
older than 5.3

If running in sync mode and a completion takes a long time, the user can
^C out of it. We need to use `always` in the strategy function or the
pty will not be destroyed in this case and the next time we go to create
it, it will fail, making the shell unusable.

User can have many different completion styles set that will modify what
they've already typed. These styles will result in suggestions that
don't match what the user has already typed. We try our best to unset
some of the more problematic ones, but add some code to fetch to
invalidate suggestions that don't match what the user's already typed.
2019-04-11 09:53:55 -06:00
Eric Freese
b9fee8a324 Allow disabling of automatic widget re-binding
Addresses github #411
2019-04-10 11:43:21 -06:00
Eric Freese
db290c518b cleanup: Leave max size config unset by default to match other options 2019-04-10 11:43:20 -06:00
Eric Freese
d8ba53678e cleanup: Use + param expansion flag in arithmetic context 2019-04-10 11:42:25 -06:00
Eric Freese
4cd210b70d Fix async suggestions when SH_WORD_SPLIT is set 2019-04-10 09:51:33 -06:00
Eric Freese
e405afab29 Refactor async mode to no longer use zpty
See technique used in `fast-syntax-highlighting`:
- ca2e18bbc9
- http://www.zsh.org/mla/users/2018/msg00424.html

Also see http://www.zsh.org/mla/users/2018/msg00432.html

In async response handler:
- We only want to read data in case of POLLIN or POLLHUP. Not POLLNVAL
  or select error.
- We always want to remove the handler, so it doesn't get called in an
  infinite loop when error is nval or err.

There is an upstream bug that prevents ctrl-c from resetting the prompt
immediately after a suggestion has been fetched asynchronously. A patch
has been submitted, but a workaround for now is to add `command true`
after the exec.

See https://github.com/zsh-users/zsh-autosuggestions/issues/364
2019-04-09 14:45:22 -06:00
romkatv
4a82ff1ead speed up widget rebinding by removing redundant array subscripts 2019-04-01 14:56:53 -06:00
Eric Freese
f76472272e cleanup: Remove unnecessary braces 2019-04-01 14:36:31 -06:00
Eric Freese
50579b3371 Move widget definitions inside anonymous function 2018-12-21 23:20:08 -07:00
dana
aee1b10db6 Avoid warn_create_global warnings 2018-12-19 13:38:06 -06:00
Eric Freese
e937e89267 Respect user's set options when running original widget
Fixes GitHub #379
2018-12-16 21:13:02 -07:00
Eric Freese
41657e3565 Revert async process substitution & completion strategy
They're not quite ready. Keep them on a feature branch for now.
2018-12-16 20:49:06 -07:00
Eric Nielsen
e61442161e Don't overwrite config with default values
otherwise users are obliged to set the config values *after* sourcing
the plugin. They're not able to do it before. Also, re-sourcing the
plugin will reset the values to the defaults again.

See zimfw/zimfw#301

Fixes #335
2018-11-18 12:39:23 -07:00
Eric Freese
681ffc7b28 Reset opts in some functions affected by GLOB_SUBST
Should fix GitHub #334
2018-07-13 22:16:53 -06:00
Eric Freese
b8bf86f6ab
Merge pull request #359 from zsh-users/fixes/async-bad-file-descriptor
Fixes/async bad file descriptor
2018-07-13 20:50:31 -07:00
Eric Freese
7ab2124904 Kill async process by id when job control disabled 2018-07-13 21:48:25 -06:00
Eric Freese
88fe824ddf Add some error handling to async response handler
We only want to read data in case of POLLIN or POLLHUP. Not POLLNVAL or
select error.

We always want to remove the handler, so it doesn't get called in an
infinite loop when error is nval or err.

In zsh source, see main zle event loop in zle_main.c raw_getbyte
function.
2018-07-13 11:26:57 -06:00
Eric Freese
93877f6b76 We also need to remove the handler when cancelling async request
Should fix GitHub #353
2018-07-13 11:25:59 -06:00
Eric Freese
bd1fd97738 Cleanup unused async pty name 2018-07-02 22:38:20 -06:00
Eric Freese
f1c3b98774 Only capture completions at the end of the buffer.
To prevent the suggestion from not starting with the buffer string.

Example:

`ls / /[cursor left][cursor left]b`

Before the patch, suggests `ls /b /ls /bin/ /`

After the patch, suggests `ls /b /bin/`.

https://github.com/zsh-users/zsh-autosuggestions/issues/343#issuecomment-401675712
2018-07-02 12:25:20 -06:00
Eric Freese
4869a757c8 Ensure we always destroy the zpty
If running in sync mode and a completion takes a long time, the user can
^C out of it. Without this patch, the pty will not be destroyed in this
case and the next time we go to create it, it will fail, making the
shell unusable.
2018-07-02 12:25:20 -06:00
Eric Freese
302bd7c059 Setup zshexit hook immediately in both sync/async cases 2018-07-02 12:25:20 -06:00
Eric Freese
245f5d2ba2 Improve completion suggestions
Just insert the first completion directly into the buffer and read the
whole buffer from the zpty.
2018-07-02 12:25:20 -06:00
Martin Väth
8ae0283c90 Do not rely on implicit NULLCMD=cat 2018-07-02 12:21:49 -06:00
Eric Freese
c0315e96d8 Don't use -s option to zmodload
It is not available in zsh versions older than 5.3
2018-06-30 16:54:33 -06:00
Eric Freese
5529102afc zpty module is only needed for completion strategy 2018-06-30 15:09:24 -06:00
Eric Freese
dad6be4d5e Remove unused feature detection
Not needed after move away from zpty for async
2018-06-30 15:05:37 -06:00
Eric Freese
7d968869e3 Return if no completion found 2018-06-30 15:03:14 -06:00
Eric Freese
8593624996
Merge pull request #330 from zsh-users/features/completion-suggestions
Features/completion suggestions
2018-06-30 14:35:50 -06:00
Eric Freese
1ec43c7291 Fix error when single quote entered into buffer
Error looked something like:

```
% echo 'f(zpty):8: unmatched '
_zsh_autosuggest_capture_completion:zpty:9: no such pty command: zsh_autosuggest_completion_pty
_zsh_autosuggest_capture_completion:zpty:14: no such pty command: zsh_autosuggest_completion_pty
_zsh_autosuggest_capture_completion:zpty:21: no such pty command: zsh_autosuggest_completion_pty
```

According to `man zshmodules`, the args to `zpty` are "concatenated with
spaces between, then executed as a command, as if passed to the eval
builtin." So we need to escape the `$` so that `$1` is passed to eval
instead of the value of `$1`.
2018-06-29 22:08:33 -06:00
Martin Väth
43a011026f Do not leak global variables REPLY and strategy
https://github.com/zsh-users/zsh-autosuggestions/issues/341
2018-06-18 16:39:48 -06:00
Eric Freese
cd81522b30 Attempt to kill async worker process when new request comes in
See http://www.zsh.org/mla/users/2018/msg00432.html
2018-06-12 23:45:29 -06:00
Eric Freese
d7171232c3 Merge branch 'develop' into features/completion-suggestions 2018-06-11 02:34:24 -06:00
Eric Freese
9cb0101512 Refactor async mode to no longer use zpty
See technique used in `fast-syntax-highlighting`:
- ca2e18bbc9
- http://www.zsh.org/mla/users/2018/msg00424.html
2018-06-11 02:12:47 -06:00