Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Extend to work in sh-mode #13

Closed
nverno opened this Issue Nov 9, 2016 · 9 comments

Comments

Projects

Notable Changes for Re... in 2.1 Release

3 participants

nverno commented Nov 9, 2016 edited

I love this package! It seems fairly easy to extend it to work in sh-mode with the following
redefinition of comint-line-beginning-position

(defun bash-completion-sh-complete ()
  (cl-letf (((symbol-function 'comint-line-beginning-position)
             #'(lambda ()
                 (save-excursion
                   (sh-beginning-of-command)
                   (point)))))
    (bash-completion-dynamic-complete)))

(add-hook 'completion-at-point-functions
          'bash-completion-sh-complete nil 'local)

This works well for me so far, but haven't tested it thoroughly. Is this necessary, or am
I doing something unneeded with this?

Same thing for eshell, with (save-excursion (eshell-bol) (point)) instead of comint-line-beginning-position.

Would it be possible to refactor the function parameters so that it takes either a string or 2 positions?

szermatt added a commit that referenced this issue Aug 12, 2017

Add bash-completion-dynamic-complete-nocomint for issue #13
This commit introduces a function that can safely be called outside of a
comint buffer. It is passed the region to complete and returns the same
result as bash-completion-dynamic-complete without accessing any
comint-specific function.

The refactoring removes bash-completion-dynamic-complete-0 and
bash-completion-dynamic-try-wordbreak-complete. Obsolete wrappers are
left for now, to avoid making a breaking change, for now.
Owner

szermatt commented Aug 19, 2017

f639154 introduces
bash-completion-dynamic-complete-nocomint (start pos)

@szermatt szermatt closed this Aug 19, 2017

Ambrevar commented Sep 1, 2017

Thanks for this addition.

I've just tested it and I don't seem to be able to complete anything but files.
ls --<TAB> completes into

--foo
--bar

assuming foo and bar are in the current directory.

Owner

szermatt commented Sep 1, 2017

Where did you set the start point you passed to the function? It must be set at the beginning of the command (on the "l" of "ls) From your description, it sounds like you set start and end at (point).

Ambrevar commented Sep 1, 2017

Forgot to mention the call:

(defun eshell-bash-completion ()
  (while (pcomplete-here
          (nth 2 (bash-completion-dynamic-complete-nocomint (save-excursion (eshell-bol) (point)) (point))))))

The former code was

(defun eshell-bash-completion ()
  (while (pcomplete-here
          (cl-letf (((symbol-function 'comint-line-beginning-position)
                     #'(lambda () (save-excursion (eshell-bol) (point)))))
            (nth 2 (bash-completion-dynamic-complete))))))

used to work when I first posted on this issue, it does not anymore. Something got wrong somewhere else. Could be on my end too.

Owner

szermatt commented Sep 1, 2017 edited

I can reproduce this behavior if there is no programmable completion registered for "ls", even when using bash-completion-dynamic-complete, then what follows -- gets expanded as file completion. Something's not right. I'll have a look.

However, when there is programmable completion registered for "ls", I get the expected argument completion when I use your code (I didn't use pcomplete, just printed out the result)
Something's wrong with the latest version, but it doesn't look like it's specific to the -nocomint function.

Ambrevar commented Sep 1, 2017

Indeed, as you can see from my last code snippet, bash-completion-dynamic-complete is also affected.

szermatt added a commit that referenced this issue Sep 2, 2017

Fixes strange behavior when completing --.
Completing -- in commands where no completion has been defined used to
default to file completion, so for example:
ls --<complete>
would complete to
ls --bar
if there was a file bar in the current directory.

This is because the string to be completed was passed to compgen,
and when compgen sees --, it just disables parsing of further
parameters. It didn't happen when using custom completion, because
bash-completion.el already passed -- in this case.

This change adds the missing -- for default and command completion, to
avoid issue when completing -- or files starting with -.

Mentioned in issue #13
Owner

szermatt commented Sep 2, 2017

It should work now. Thank you for the report! It was a silly old bug that must have caused quite some confusion.

Any luck getting full completion to work in eshell?

@szermatt szermatt added this to Notable Changes for Release in 2.1 Release Sep 2, 2017

Ambrevar commented Sep 3, 2017 edited

Thank you, all good now.

Completion is a wonder in Eshell thanks to those simple lines:

(when (require 'bash-completion nil t)
  (setq eshell-default-completion-function 'eshell-bash-completion))

(defun eshell-bash-completion ()
  (while (pcomplete-here
          (nth 2 (bash-completion-dynamic-complete-nocomint (save-excursion (eshell-bol) (point)) (point))))))

I recommend using Helm as a selection menu. (Don't know if it works with Ivy.)

Bash completion is sometimes lacking. The fish shell arguably provides better completion.
I hook it into Eshell with the a similar function. See my dotfiles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment