jupyter icon indicating copy to clipboard operation
jupyter copied to clipboard

Control completion in org-mode source blocks

Open fleimgruber opened this issue 3 years ago • 4 comments

In org-mode source blocks, when I insert a new line, completion immediately triggers and eagerly lists all symbols that are available as completion options. Usually this list is very large and so completion prompt takes 1-3 seconds which is a distraction while developing.

How to control when completion should be triggered, e.g. only after 2 or 3 characters have been inserted?

fleimgruber avatar Jun 15 '22 08:06 fleimgruber

I had this issue too, but only when using selectrum or vertico for in-buffer completion. I wrote a fix that doesn't apply the completion-in-region-function if you're on whitespace or newlines (in this case for vertico, which uses consult for in-region completion):

(use-package vertico
  :init
  (vertico-mode)
  :config
  (defun ib-only-with-prompt (orig-fun start end collection predicate)
    (let ((check))
      (setq check (or (= ?  (preceding-char)) (= ?\C-j (preceding-char))))
      (setq check (or check (= 0 (preceding-char))))
      (unless check
        (apply orig-fun (list start end collection predicate)))))
  (advice-add 'consult-completion-in-region :around
              #'ib-only-with-prompt)

timlod avatar Jun 15 '22 08:06 timlod

Thanks for your inputs!

I am using helm for completion so maybe we can find a generic solution that works on completion-at-point level and is agnostic of completion selection backend?

fleimgruber avatar Jun 15 '22 08:06 fleimgruber

Actually, you can change the above from 'consult-completion-in-region to 'completion-in-region, and it will work with whichever backend it uses.

It is still a bandaid though - I'm not 100% sure why some backends cause this to happen at newline level. I think it has to do with tab-always-indent 'complete, where a newline uses tab to get to the correct indentation level when editing code, and then calls complete if no indentation is added.

Did you check if this only happened with emacs-jupyter?

timlod avatar Jun 15 '22 09:06 timlod

Actually, you can change the above from 'consult-completion-in-region to 'completion-in-region, and it will work with whichever backend it uses.

Thanks for the hint, that works indeed.

Did you check if this only happened with emacs-jupyter?

I only noticed the performance hit on org-mode source blocks, did no other checking.

fleimgruber avatar Jun 16 '22 10:06 fleimgruber