Question

I have set up a key binding for a function that modifies the behavior of Emacs auto-complete:

;; ISSUE: when I type the whole candidate string and then press [SPC], 
;; Emacs will insert two spaces.
(define-key ac-menu-map (kbd "SPC")
  (defun ac-complete-with-space ()
    "Select the candidate and append a space. Save your time for typing space."
    (interactive)
    (ac-complete)
    ;; FIXME: this auto-inserts two spaces.
    (insert " ")
    ))

... and I want to disable this key binding in ac-menu-map in org-mode only.

I have tried the following:

;; Avoid always selecting unwanted first candidate with auto-complete 
;; when writing in org-mode.
(add-hook 'org-mode-hook
          (lambda ()
            ;; (define-key ac-menu-map (kbd "SPC") nil)
            ;; (define-key ac-menu-map (kbd "SPC") 'self-insert-command)
            ;; (setq-local ac-menu-map (delq (kbd "SPC") ac-menu-map))
            ))

Unfortunately, this does not unset the key binding locally (i.e., only in org-mode). Instead, it removes the key binding from the ac-menu-map everywhere.

Was it helpful?

Solution 2

(defun ac-complete-with-space ()
  "Select the candidate and append a space. save your time for typing space."
  (interactive)
  (ac-complete)
  (insert " ")
  )

;; NOTE: ac-completing-map is the parent map of ac-menu-map.
(define-key ac-completing-map (kbd "SPC") 'ac-complete-with-space)
(add-hook 'org-mode-hook
          (lambda ()
            (define-key ac-menu-map (kbd "SPC") 'self-insert-command)))

OTHER TIPS

A different approach to solve your problem would be to check in ac-complete-with-space the mode. If org-mode, then call self-insert-command, else follow your current logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top