Update auto-complete candidates dynamically

前面对auto-complete的配置有问题,即使用F1开启自动补全功能后,候选列表不会随着输入自动更新,要连续按F1才行。使用如下配置,可以使候选列表在一次补全期间自动更新。

(setq ac-auto-start nil)
(defun wl-ac-start ()
  (interactive)
  (setq ac-auto-start 6)
  (ac-start))
(defadvice ac-cleanup (after wl-ac-cleanup ())
  (setq ac-auto-start nil))
(ad-activate 'ac-cleanup)
(define-key global-map (kbd "<f1>") 'wl-ac-start)

自动补全功能入口改为自定义的函数wl-ac-start,在这个函数里,ac-auto-start变量的值被设置为6,即当输入字符达到6个时显示候选列表。当本次自动补全完成后,ac-cleanup函数会清理现场,而自定义的advice wl-ac-cleanup会在它运行后将ac-auto-start设置nil,这样自动补全功能就被关闭了。