0

Using the helper command last-message from another StackExchange question, I wrote and bound an anonymous function to switch to the completion buffer if it exists, and if it doesn't exist but would be created by TAB TAB, create that buffer. The function works as intended, except for one problem... It's really slow at displaying the new buffer when it's created. I was first running it in a Python inferior shell for testing, but it has the same behavior in M-x shell.

The function appears to be as fast as you'd expect (basically instant). If I press C-TAB and hit the down arrow, I immediately see the buffer. However, if I press C-TAB and do nothing, it takes 2-3 seconds for the window to pop up.

(defun last-message (&optional num)
  (or num (setq num 1))
  (if (= num 0)
      (current-message)
    (save-excursion
      (set-buffer "*Messages*")
      (save-excursion
        (forward-line (- 1 num))
        (backward-char)
        (let ((end (point)))
          (forward-line 0)
          (buffer-substring-no-properties (point) end))))))

(global-set-key [C-tab]
                (lambda ()
                  (interactive)
                  (switch-to-completions)
                  (if (string= "No completions" (last-message))
                      (progn (completion-help-at-point)
                             (switch-to-completions)))))
5
  • Why the nested save-excursions? (They only became noticeable after fixing your unhelpful indentation, BTW.)
    – Drew
    Commented Jun 30, 2017 at 13:36
  • I didn't write that function, so I don't know. I copy-pasted it from another StackExchange question, which is linked in the question.
    – Zorgoth
    Commented Jun 30, 2017 at 13:40
  • Perhaps the source code contained TAB chars. That can throw off indentation when it is pasted.
    – Drew
    Commented Jun 30, 2017 at 13:48
  • If you're NOT calling the command from within the minibuffer, switch-to-completions probably makes no sense and will build completions from the current buffer-string, which may explain the delay.
    – mutbuerger
    Commented Jun 30, 2017 at 14:36
  • The intention is to build completions from the current buffer-string, if I understand your meaning. For example, I can use this function in an inferior Python shell.
    – Zorgoth
    Commented Jun 30, 2017 at 14:44

1 Answer 1

1

Scanning a command's messages is not a good way to determine its result. Maybe this is what you want.

(defun completion-help-at-point-switch ()
  (interactive)
  (completion-help-at-point)
  (when completion-in-region-mode
    (switch-to-completions)))

(global-set-key [C-tab] #'completion-help-at-point-switch)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.