2

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.

itsjeyd
  • 5,070
  • 2
  • 30
  • 49
stardiviner
  • 1,090
  • 1
  • 22
  • 33
  • 1
    Keymaps are (by default) global variables. If you `(define-key ac-menu-map ...)` *anywhere* it affects that keymap *everywhere*. You can either do what juanleon suggested and bind the key (everywhere) to a function which checks the current major mode and then acts accordingly; or you could use org-mode-hook to make `ac-menu-map` buffer-local for org-mode buffers, and modify it that way. – phils Apr 03 '14 at 21:29
  • 1
    Probably a duplicate of [Buffer-locally overriding minor-mode key bindings in Emacs](http://stackoverflow.com/q/13102494/324105). – phils Apr 03 '14 at 21:32

2 Answers2

5

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.

juanleon
  • 9,220
  • 30
  • 41
  • I use code like this: (add-hook 'org-mode-hook (lambda () (eval-after-load 'auto-complete (define-key ac-menu-map (kbd "SPC") 'self-insert-command)) )) But this does not work. – stardiviner Apr 03 '14 at 16:26
0
(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)))
stardiviner
  • 1,090
  • 1
  • 22
  • 33