Skip to content

Commit a5201ac

Browse files
wraithmgracjan
authored andcommitted
Stylish-haskell runs on buffer instead of file on disk
1 parent f9dbbe5 commit a5201ac

File tree

3 files changed

+38
-45
lines changed

3 files changed

+38
-45
lines changed

haskell-commands.el

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -789,47 +789,43 @@ inferior GHCi process."
789789
Use buffer as input and replace the whole buffer with the
790790
output. If CMD fails the buffer remains unchanged."
791791
(set-buffer-modified-p t)
792-
(let* ((chomp (lambda (str)
793-
(while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'" str)
794-
(setq str (replace-match "" t t str)))
795-
str))
796-
(_errout (lambda (fmt &rest args)
797-
(let* ((warning-fill-prefix " "))
798-
(display-warning cmd (apply 'format fmt args) :warning))))
799-
(filename (buffer-file-name (current-buffer)))
800-
(cmd-prefix (replace-regexp-in-string " .*" "" cmd))
801-
(tmp-file (make-temp-file cmd-prefix))
802-
(err-file (make-temp-file cmd-prefix))
792+
(let* ((tmp-buf (generate-new-buffer "stylish-output"))
793+
(err-file (make-temp-file "stylish-error"))
803794
(default-directory (if (and (boundp 'haskell-session)
804795
haskell-session)
805796
(haskell-session-cabal-dir haskell-session)
806-
default-directory))
807-
(_errcode (with-temp-file tmp-file
808-
(call-process cmd filename
809-
(list (current-buffer) err-file) nil)))
810-
(stderr-output
811-
(with-temp-buffer
812-
(insert-file-contents err-file)
813-
(funcall chomp (buffer-substring-no-properties (point-min) (point-max)))))
814-
(stdout-output
815-
(with-temp-buffer
816-
(insert-file-contents tmp-file)
817-
(buffer-substring-no-properties (point-min) (point-max)))))
818-
(if (string= "" stderr-output)
819-
(if (string= "" stdout-output)
820-
(message "Error: %s produced no output, leaving buffer alone" cmd)
821-
(save-restriction
822-
(widen)
823-
;; command successful, insert file with replacement to preserve
824-
;; markers.
825-
(insert-file-contents tmp-file nil nil nil t)))
826-
(progn
827-
;; non-null stderr, command must have failed
828-
(message "Error: %s ended with errors, leaving buffer alone" cmd)
829-
;; use (warning-minimum-level :debug) to see this
830-
(display-warning cmd stderr-output :debug)))
831-
(delete-file tmp-file)
832-
(delete-file err-file)))
797+
default-directory)))
798+
(unwind-protect
799+
(let* ((_errcode
800+
(call-process-region (point-min) (point-max) cmd nil
801+
(list (buffer-name tmp-buf) err-file)
802+
nil))
803+
(stderr-output
804+
(with-temp-buffer
805+
(insert-file-contents err-file)
806+
(buffer-substring-no-properties (point-min) (point-max))))
807+
(stdout-output
808+
(with-temp-buffer
809+
(insert-buffer-substring tmp-buf)
810+
(buffer-substring-no-properties (point-min) (point-max)))))
811+
(if (string= "" stderr-output)
812+
(if (string= "" stdout-output)
813+
(message "Error: %s produced no output, leaving buffer alone" cmd)
814+
(save-restriction
815+
(widen)
816+
;; command successful, insert file with replacement to preserve
817+
;; markers.
818+
(erase-buffer)
819+
(insert-buffer-substring tmp-buf)))
820+
(progn
821+
;; non-null stderr, command must have failed
822+
(message "Error: %s ended with errors, leaving buffer alone" cmd)
823+
;; use (warning-minimum-level :debug) to see this
824+
(display-warning cmd stderr-output :debug))))
825+
(ignore-errors
826+
(delete-file err-file))
827+
(ignore-errors
828+
(kill-buffer tmp-buf)))))
833829

834830
;;;###autoload
835831
(defun haskell-mode-find-uses ()

haskell-mode.el

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,8 @@ To be added to `flymake-init-create-temp-buffer-copy'."
10551055

10561056
(defun haskell-mode-before-save-handler ()
10571057
"Function that will be called before buffer's saving."
1058-
)
1058+
(when haskell-stylish-on-save
1059+
(ignore-errors (haskell-mode-stylish-buffer))))
10591060

10601061
;; From Bryan O'Sullivan's blog:
10611062
;; http://www.serpentine.com/blog/2007/10/09/using-emacs-to-insert-scc-annotations-in-haskell-code/

haskell.el

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,8 @@ Give optional NEXT-P parameter to override value of
361361
;;;###autoload
362362
(defun haskell-mode-after-save-handler ()
363363
"Function that will be called after buffer's saving."
364-
(when haskell-tags-on-save (ignore-errors (haskell-mode-generate-tags)))
365-
(when haskell-stylish-on-save
366-
(ignore-errors (haskell-mode-stylish-buffer))
367-
(let ((before-save-hook '())
368-
(after-save-hook '()))
369-
(basic-save-buffer))))
364+
(when haskell-tags-on-save
365+
(ignore-errors (haskell-mode-generate-tags))))
370366

371367
;;;###autoload
372368
(defun haskell-mode-tag-find (&optional _next-p)

0 commit comments

Comments
 (0)