define a new function for keyboard-quit and highlight clearup

巧了我就是萌 提交于 2020-01-05 10:18:10

问题


I am pretty new to elisp, and i was trying to define a function to set highlight behavior while searching. The goal is: after i-search, I want to be able to clear the highlight with C-g, but I want the highlight to remain if I press enter.

So i defined a function in my init.el as:

(defun keyboard-quit-cleanup ()
  "clean up highligh after keyboard quit"
  (keyboard-quit)
  (lazy-highlight-cleanup))
(global-set-key (kbd "C-g") '(keyboard-quit-cleanup))

how-ever, it doesn't work. What's wrong here?

thanks!


回答1:


Lots of things, I'm afraid :)

  1. Your global-set-key is broken:

    (global-set-key (kbd "C-g") 'keyboard-quit-cleanup)
    

    not:

    (global-set-key (kbd "C-g") '(keyboard-quit-cleanup))
    

    (where did you see that??)

  2. Your custom function is not an interactive command, so you cannot bind it to a key. It needs to be:

    (defun keyboard-quit-cleanup ()
      "Clean up highlights after keyboard quit."
      (interactive)
      ...)
    
  3. You probably want to use the FORCE argument to lazy-highlight-cleanup

  4. You're binding the wrong thing.

    C-g is bound to isearch-abort in the isearch-mode-map keymap.

    (edit: and as per tripleee's comment, messing with the global binding is surely a bad idea. There's definitely some special behaviour associated with it, and you don't want to risk breaking it.)

  5. As you're looking to add an additional behaviour to a standard behaviour, you probably want to use a hook (by preference) or some advice. In this case there's a convenient hook we can use.

    See C-hv isearch-mode-end-hook RET

    (add-hook 'isearch-mode-end-hook 'my-isearch-end)
    (defun my-isearch-end ()
      "Custom behaviours for `isearch-mode-end-hook'."
      (when isearch-mode-end-hook-quit
        (lazy-highlight-cleanup t)))
    

(I assume you have lazy-highlight-cleanup set to nil normally, as otherwise the clean-up happens by default.)



来源:https://stackoverflow.com/questions/16804131/define-a-new-function-for-keyboard-quit-and-highlight-clearup

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!