Skip to content

Commit

Permalink
haskell-mode-buffer-apply-command: capture error output and display a…
Browse files Browse the repository at this point in the history
…s warning
  • Loading branch information
gregorycollins committed Jan 19, 2013
1 parent ed31b18 commit 11b531c
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions haskell-mode.el
Expand Up @@ -780,21 +780,50 @@ This function will be called with no arguments.")
replace the whole buffer with the output. If CMD fails the replace the whole buffer with the output. If CMD fails the
buffer remains unchanged." buffer remains unchanged."
(set-buffer-modified-p t) (set-buffer-modified-p t)
(let* ((filename (buffer-file-name (current-buffer))) (flet
(tmp-file (make-temp-file (replace-regexp-in-string " .*" "" cmd))) ((chomp (str)
(while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'"
str)
(setq str (replace-match "" t t str)))
str)
(errout
(fmt &rest args)
(let* ((warning-fill-prefix " "))
(display-warning cmd (apply 'format fmt args) :warning))))
(let*
((filename (buffer-file-name (current-buffer)))
(cmd-prefix (replace-regexp-in-string " .*" "" cmd))
(tmp-file (make-temp-file cmd-prefix))
(err-file (make-temp-file cmd-prefix))
(default-directory (if (and (boundp 'haskell-session) (default-directory (if (and (boundp 'haskell-session)
haskell-session) haskell-session)
(haskell-session-cabal-dir haskell-session) (haskell-session-cabal-dir haskell-session)
default-directory)) default-directory))
(nbytes (with-temp-file tmp-file (errcode (call-process cmd filename
(call-process cmd filename t nil) (list (list :file tmp-file) err-file) nil))
(point-max)))) (stderr-output
(unless (= 0 nbytes) (with-temp-buffer
(save-restriction (insert-file-contents err-file)
(widen) (chomp (buffer-substring-no-properties (point-min) (point-max)))))
; insert file with replacement to preserve markers. (stdout-output
(insert-file-contents tmp-file nil nil nil t))) (with-temp-buffer
(delete-file tmp-file))) (insert-file-contents tmp-file)
(buffer-substring-no-properties (point-min) (point-max)))))
(if (string= "" stderr-output)
(if (string= "" stdout-output)
(errout
"Error: %s produced no output, leaving buffer alone" cmd)
(save-restriction
(widen)
;; command successful, insert file with replacement to preserve
;; markers.
(insert-file-contents tmp-file nil nil nil t)))
;; non-null stderr, command must have failed
(errout "%s failed: %s" cmd stderr-output)
)
(delete-file tmp-file)
(delete-file err-file)
)))


(defun haskell-mode-stylish-buffer () (defun haskell-mode-stylish-buffer ()
"Apply stylish-haskell to the current buffer." "Apply stylish-haskell to the current buffer."
Expand Down

0 comments on commit 11b531c

Please sign in to comment.