سؤال

أنا أستخدم flymake مع pyflakes للتحقق من كود بيثون و flyspell للتحقق من سلاسل التعليقات والتعليقات الخاصة بي. أريد أن أحصل على وظيفة واحدة التي ستذهب إلى الخطأ التالي، أو سيعرض معلومات حول الخطأ في حالة خطأ حاليا. كيف أكتب هذه الوظيفة؟

هل كانت مفيدة؟

المحلول

يوفر هذا الرمز وظيفة يقفزك إلى الخطأ التالي، وإذا كان خطأ FlyMake، يعرض المعلومات لذلك، إذا كان خطأ Flyspell، فهذا يصححه لك. إذا كنت لا تريد التصحيح التلقائي، الأمم المتحدة للتعليق على الخط الذي يستدعي 'my-flyspell-message وإزالة الخط قبل أن يستدعي 'flyspell-auto-correct-word - وسوف تحصل فقط على رسالة حول الكلمة التي يتم أخطرها.

يربط السطر الأول هذا إلى Keybinding CC N.. وبعد لمزيد من المعلومات حول مفاتيح الملزمة، راجع صفحة المعلومات الروابط الرئيسية.

(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)))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top