Permalink
Browse files

use {before/after}-save-hook instead of separate functions

before-save-hook will call stylish before save
after-save-hook will call hasktags after file was saved

both hooks are made file-local, to be called only in the haskell-mode
  • Loading branch information...
1 parent 1de57e4 commit 1ceb0eb03cc9fdaa2570f33ddf761c9060d20532 @alexott alexott committed with chrisdone Nov 7, 2012
Showing with 34 additions and 22 deletions.
  1. +0 −4 examples/init.el
  2. +18 −18 haskell-mode.el
  3. +16 −0 haskell-process.el
View
4 examples/init.el
@@ -78,10 +78,6 @@
;; Jump to the definition of the current symbol.
(define-key haskell-mode-map (kbd "M-.") 'haskell-mode-tag-find)
- ;; Save the current buffer and generate etags (a TAGS file) for the
- ;; whole project.
- (define-key haskell-mode-map (kbd "C-x C-s") 'haskell-mode-save-buffer)
-
;; Indent the below lines on columns after the current column.
(define-key haskell-mode-map (kbd "C-<right>")
(lambda ()
View
36 haskell-mode.el
@@ -176,8 +176,7 @@
;; Set load-path
;;;###autoload
-(add-to-list 'load-path
- (or (file-name-directory load-file-name) (car load-path)))
+(when load-file-name (add-to-list 'load-path (file-name-directory load-file-name)))
;; Set up autoloads for the modules we supply
(autoload 'turn-on-haskell-decl-scan "haskell-decl-scan"
@@ -530,7 +529,12 @@ Invokes `haskell-mode-hook'."
(set (make-local-variable 'dabbrev-case-distinction) nil)
(set (make-local-variable 'dabbrev-case-replace) nil)
(set (make-local-variable 'dabbrev-abbrev-char-regexp) "\\sw\\|[.]")
- (setq haskell-literate nil))
+ (setq haskell-literate nil)
+ (make-local-variable 'before-save-hook)
+ (add-hook 'before-save-hook 'haskell-mode-before-save-handler)
+ (make-local-variable 'after-save-hook)
+ (add-hook 'after-save-hook 'haskell-mode-after-save-handler)
+ )
(defun haskell-fill-paragraph (justify)
(save-excursion
@@ -659,14 +663,12 @@ If nil, use the Hoogle web-site."
(string :tag "Other command")))
(defcustom haskell-stylish-on-save nil
- "Whether to run stylish-haskell on the buffer before
-saving. Needs 'haskell-mode-save-buffer to be bound for C-x C-s."
+ "Whether to run stylish-haskell on the buffer before saving."
:group 'haskell
:type 'boolean)
(defcustom haskell-tags-on-save nil
- "Generate tags via hasktags on save. Needs
-'haskell-mode-save-buffer to be bound for C-x C-s."
+ "Generate tags via hasktags after saving."
:group 'haskell
:type 'boolean)
@@ -753,17 +755,15 @@ This function will be called with no arguments.")
(haskell-process-do-try-info ident)))
(t (insert " "))))
-(defun haskell-mode-save-buffer ()
- "Save the current buffer."
- (interactive)
- (let ((modified (buffer-modified-p)))
- (save-buffer)
- (when haskell-stylish-on-save
- (haskell-mode-stylish-buffer))
- (save-buffer)
- (when modified
- (when haskell-tags-on-save
- (haskell-process-generate-tags)))))
+(defun haskell-mode-before-save-handler ()
+ "Function that will be called before buffer's saving."
+ (when haskell-stylish-on-save
+ (haskell-mode-stylish-buffer)))
@gregorycollins
gregorycollins Dec 11, 2012

This is absolutely the wrong thing to do here! Calling haskell-mode-stylish-buffer causes stylish-haskell to overwrite the buffer contents in-place, but the new changes haven't been saved yet, so the buffer contents get blasted away. This needs to be moved to the after-save hook (which I'll do shortly).

@sheyll
sheyll Dec 16, 2012

of course... you are right

+
+(defun haskell-mode-after-save-handler ()
+ "Function that will be called after buffer's saving."
+ (when haskell-tags-on-save
+ (haskell-process-generate-tags)))
(defun haskell-mode-buffer-apply-command (cmd)
"Execute shell command CMD with current buffer as input and
View
16 haskell-process.el
@@ -115,6 +115,7 @@ has changed?"
(haskell-process-reset (haskell-process))
(haskell-process-set (haskell-process) 'command-queue nil))
+;;;###autoload
(defun haskell-process-generate-tags (&optional and-then-find-this-tag)
"Regenerate the TAGS table."
(interactive)
@@ -833,3 +834,18 @@ to be loaded by ghci."
(funcall live-func
(haskell-command-state command)
response))))
+
@sheyll
sheyll Nov 20, 2012

careful! haskell-command-get/set will most definitely fail.

+(defun haskell-command-get (s key)
+ "Get the command `key'."
+ (let ((x (assoc key s)))
+ (when x
+ (cdr x))))
+
+(defun haskell-command-set (s key value)
+ "Set the command's `key'."
+ (delete-if (lambda (prop) (equal (car prop) key)) s)
+ (setf (cdr s) (cons (cons key value)
+ (cdr s)))
+ s)
+
+(provide 'haskell-process)

0 comments on commit 1ceb0eb

Please sign in to comment.