ghc-mod/elisp/ghc-flymake.el

106 lines
3.1 KiB
EmacsLisp
Raw Normal View History

2010-03-12 04:39:38 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; ghc-flymake.el
;;;
;; Author: Kazu Yamamoto <Kazu@Mew.org>
;; Created: Mar 12, 2010
;;; Code:
2010-03-11 10:03:17 +00:00
(require 'flymake)
(defvar ghc-error-buffer-name "*GHC Errors*")
2010-03-11 10:03:17 +00:00
(defvar ghc-flymake-allowed-file-name-masks
'("\\.l?hs$" ghc-flymake-init flymake-simple-cleanup flymake-get-real-file-name))
(defvar ghc-flymake-err-line-patterns
'("^\\(.*\\.l?hs\\):\\([0-9]+\\):\\([0-9]+\\):[ ]*\\(.+\\)" 1 2 3 4))
2010-03-11 10:03:17 +00:00
(add-to-list 'flymake-allowed-file-name-masks
ghc-flymake-allowed-file-name-masks)
(add-to-list 'flymake-err-line-patterns
ghc-flymake-err-line-patterns)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2010-03-11 10:03:17 +00:00
(defun ghc-flymake-init ()
2010-03-12 02:55:11 +00:00
(let ((after-save-hook nil))
(save-buffer))
2010-03-11 15:20:02 +00:00
(let ((file (file-name-nondirectory (buffer-file-name))))
2010-05-06 06:29:55 +00:00
(list ghc-module-command (ghc-flymake-command file))))
(defvar ghc-flymake-command nil) ;; nil: check, t: lint
(defun ghc-flymake-command (file)
(if ghc-flymake-command
2010-06-08 02:23:58 +00:00
(list "lint" file)
2010-05-06 06:29:55 +00:00
(list "check" file)))
(defun ghc-flymake-toggle-command ()
(interactive)
2010-06-08 02:23:58 +00:00
(setq ghc-flymake-command (not ghc-flymake-command))
(if ghc-flymake-command
(message "Syntax check with hlint")
(message "Syntax check with GHC")))
2010-03-11 15:20:02 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ghc-flymake-display-errors ()
(interactive)
(if (not (ghc-flymake-have-errs-p))
(message "No errors or warnings")
(let ((buf (get-buffer-create ghc-error-buffer-name))
(title (ghc-flymake-err-title))
(errs (ghc-flymake-err-list)))
(with-current-buffer buf
(erase-buffer)
(ghc-flymake-insert-errors title errs))
(display-buffer buf))))
(defun ghc-flymake-insert-errors (title errs)
(save-excursion
(insert title "\n")
2010-05-06 06:29:55 +00:00
(mapc (lambda (x) (insert (ghc-replace-character x ghc-null ghc-newline) "\n")) errs)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ghc-flymake-insert-from-warning ()
2010-03-14 13:39:45 +00:00
(interactive)
(dolist (data (ghc-flymake-err-list))
(save-excursion
(cond
((string-match "Inferred type: \\([^:]+ :: \\)\\(forall [^.]+\\. \\)?\\([^\0]*\\)" data)
2010-03-14 13:39:45 +00:00
(beginning-of-line)
(insert (match-string 1 data)
(replace-regexp-in-string "\\[Char\\]" "String" (match-string 3 data))
"\n"))
((string-match "Not in scope: `\\([^']+\\)'" data)
(save-match-data
(unless (re-search-forward "^$" nil t)
(goto-char (point-max))
(insert "\n")))
(insert "\n" (match-string 1 data) " = undefined\n"))))))
2010-03-14 13:39:45 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ghc-flymake-err-get-title (x) (nth 0 x))
(defun ghc-flymake-err-get-errs (x) (nth 1 x))
(defalias 'ghc-flymake-have-errs-p 'ghc-flymake-data)
2010-03-14 13:39:45 +00:00
(defun ghc-flymake-data ()
(let* ((line-no (flymake-current-line-no))
(info (nth 0 (flymake-find-err-info flymake-err-info line-no))))
(flymake-make-err-menu-data line-no info)))
(defun ghc-flymake-err-title ()
(ghc-flymake-err-get-title (ghc-flymake-data)))
(defun ghc-flymake-err-list ()
(mapcar 'car (ghc-flymake-err-get-errs (ghc-flymake-data))))
2010-03-14 13:39:45 +00:00
(provide 'ghc-flymake)