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)
      (set-buffer "*Messages*")
        (forward-line (- 1 num))
        (let ((end (point)))
          (forward-line 0)
          (buffer-substring-no-properties (point) end))))))

(global-set-key [C-tab]
                (lambda ()
                  (if (string= "No completions" (last-message))
                      (progn (completion-help-at-point)
  • Why the nested save-excursions? (They only became noticeable after fixing your unhelpful indentation, BTW.) – Drew Jun 30 '17 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 Jun 30 '17 at 13:40
  • Perhaps the source code contained TAB chars. That can throw off indentation when it is pasted. – Drew Jun 30 '17 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 Jun 30 '17 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 Jun 30 '17 at 14:44

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 ()
  (when completion-in-region-mode

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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