使用一键同时使用flymake和flyspell

时间:2009-12-09 13:15:35

标签: emacs flymake flyspell

我正在使用带有pyflakes的flymake检查我的python代码和flyspell来检查我的字符串和注释。我想有一个函数将转到下一个错误,或者如果当前出错,将显示有关错误的信息。我该怎么写这个函数?

1 个答案:

答案 0 :(得分:4)

此代码提供的功能会将您跳转到下一个错误,如果是flymake错误,则显示该信息,如果是flyspell错误,则会为您更正错误。如果您不想进行自动更正,请取消注释调用'my-flyspell-message的行并删除调用'flyspell-auto-correct-word之前的行 - 然后您将收到有关拼写单词错误的消息

第一行将其绑定到键绑定 C-c n 。有关绑定密钥的更多信息,请参阅信息页面Key Bindings

(global-set-key (kbd "C-c n") 'my-display-error-or-next-error)
(defun my-display-error-or-next-error ()
  "display information for current error, or go to next one"
  (interactive)
  (when (or (not (my-at-flymake-error))
            (not (my-at-flyspell-error)))
    ;; jump to error if not at one
    (my-goto-next-error))

  (cond ((my-at-flymake-error)
         ;; if at flymake error, display menu
         (flymake-display-err-menu-for-current-line))
        ((my-at-flyspell-error)
         ;; if at flyspell error, fix it
         (call-interactively 'flyspell-auto-correct-word)
         ;; or, uncomment the next line to just get a message
         ;; (my-flyspell-message)
         )))

(defun my-at-flyspell-error ()
  "return non-nill if at flyspell error"
  (some 'flyspell-overlay-p (overlays-at (point))))

(defun my-at-flymake-error ()
  "return non-nil if at flymake error"
  (let* ((line-no             (flymake-current-line-no))
         (line-err-info-list  (nth 0 (flymake-find-err-info flymake-err-info line-no))))
    line-err-info-list))

(defun my-goto-next-error ()
  "jump to next flyspell or flymake error"
  (interactive)
  (let* ((p (point))
         (spell-next-error-function '(lambda ()
                                 (forward-word) (forward-char)
                                 (flyspell-goto-next-error)))
         (spell-pos (save-excursion
                      (funcall spell-next-error-function)
                      (point)))
         (make-pos (save-excursion
                     (flymake-goto-next-error)
                     (point))))
    (cond ((or (and (< p make-pos) (< p spell-pos))
               (and (> p make-pos) (> p spell-pos)))
           (funcall (if (< make-pos spell-pos)
                        'flymake-goto-next-error
                      spell-next-error-function)))
          ((< p make-pos)
           (flymake-goto-next-error))

          ((< p spell-pos)
           (funcall spell-next-error-function)))))

(defun my-flyspell-message ()
  (interactive)
  (let ((word (thing-at-point 'word)))
    (set-text-properties 0 (length word) nil word)
    (message "Missspelled word: %s" word)))