2009-12-09 56 views
2

我使用flymake pyflakes來檢查我的python代碼和flyspell來檢查我的字符串和註釋。我希望有一個函數可以進入下一個錯誤,或者在出現錯誤時顯示有關錯誤的信息。我會如何編寫這個函數?對flymake和flyspell使用一個密鑰

回答

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))) 
+0

謝謝,這正是我想要的。 – Nikwin 2009-12-23 07:05:59

相關問題