Skip to content
Browse files

haskell-mode-buffer-apply-command: use a temporary file

Prior behavior clobbered markers, registers, and the undo stack. Using a
temporary file here allows us to switch to insert-file-contents, which has a
"replace" mode that replaces the buffer contents while attempting to maintain
marker positions.

Also twiddled the buffer-modified-p settings to make the undo stack set itself
properly.
  • Loading branch information...
1 parent d593f03 commit b5e8f40a181369e6949d64f34b42e74c12261780 @gregorycollins gregorycollins committed Jan 18, 2013
Showing with 19 additions and 15 deletions.
  1. +19 −15 haskell-mode.el
View
34 haskell-mode.el
@@ -770,27 +770,31 @@ This function will be called with no arguments.")
(ignore-errors (haskell-process-generate-tags)))
(when haskell-stylish-on-save
(ignore-errors (haskell-mode-stylish-buffer)))
- (set-buffer-modified-p nil)
+ (let ((before-save-hook '())
+ (after-save-hook '()))
+ (basic-save-buffer))
)
(defun haskell-mode-buffer-apply-command (cmd)
"Execute shell command CMD with current buffer as input and
replace the whole buffer with the output. If CMD fails the
buffer remains unchanged."
- (let* ((file (buffer-file-name (current-buffer)))
- (output (with-temp-buffer
- (let ((default-directory (if (and (boundp 'haskell-session)
- haskell-session)
- (haskell-session-cabal-dir haskell-session)
- default-directory)))
- (call-process cmd
- file
- (list t nil)
- nil))
- (buffer-substring-no-properties (point-min) (point-max)))))
- (unless (string= "" output)
- (erase-buffer)
- (insert output))))
+ (set-buffer-modified-p t)
+ (let* ((filename (buffer-file-name (current-buffer)))
+ (tmp-file (make-temp-file (replace-regexp-in-string " .*" "" cmd)))
+ (default-directory (if (and (boundp 'haskell-session)
+ haskell-session)
+ (haskell-session-cabal-dir haskell-session)
+ default-directory))
+ (nbytes (with-temp-file tmp-file
+ (call-process cmd filename t nil)
+ (point-max))))
+ (unless (= 0 nbytes)
+ (save-restriction
+ (widen)
+ ; insert file with replacement to preserve markers.
+ (insert-file-contents tmp-file nil nil nil t)))
+ (delete-file tmp-file)))
(defun haskell-mode-stylish-buffer ()
"Apply stylish-haskell to the current buffer."

0 comments on commit b5e8f40

Please sign in to comment.
Something went wrong with that request. Please try again.