Skip to content

Commit 789f2a1

Browse files
committed
Watch changes using special flag
Defined buffer local flag `hs-utils/async-post-command-flag` Defined flag related functions: + hs-utils/async-update-post-command-flag + hs-utils/async-watch-changes + hs-utils/async-stop-watching-changes
1 parent 0b463f3 commit 789f2a1

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

haskell-commands.el

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,10 @@ command from GHCi."
614614
(string-match "^<interactive>" response))
615615
(haskell-mode-message-line response)))))))
616616

617+
(defvar hs-utils/async-post-command-flag nil
618+
"Non-nil means some commands were triggered during async function execution.")
619+
(make-variable-buffer-local 'hs-utils/async-post-command-flag)
620+
617621
;;;###autoload
618622
(defun haskell-mode-show-type-at (&optional insert-value)
619623
"Show the type of the thing at point."
@@ -921,4 +925,35 @@ Optinal NAME will be used as presentation mode buffer name."
921925
(haskell-present bufname session msg))
922926
(let (m (hs-utils/reduce-string msg))
923927
(message m))))
928+
929+
(defun hs-utils/async-update-post-command-flag ()
930+
"A special hook which collects triggered commands during async execution.
931+
This hook pushes value of variable `this-command' to flag variable
932+
`hs-utils/async-post-command-flag'."
933+
(let* ((cmd this-command)
934+
(updated-flag (cons cmd hs-utils/async-post-command-flag)))
935+
(setq hs-utils/async-post-command-flag updated-flag)))
936+
937+
(defun hs-utils/async-watch-changes ()
938+
"Watch for triggered commands during async operation execution.
939+
Resets flag variable
940+
`hs-utils/async-update-post-command-flag' to NIL. By chanhges it is
941+
assumed that nothing happened, e.g. nothing was inserted in
942+
buffer, point was not moved, etc. To collect data `post-command-hook' is used."
943+
(setq hs-utils/async-post-command-flag nil)
944+
(add-hook
945+
'post-command-hook #'hs-utils/async-update-post-command-flag nil t))
946+
947+
(defun hs-utils/async-stop-watching-changes (buffer)
948+
"Clean up after async operation finished.
949+
This function takes care about cleaning up things made by
950+
`hs-utils/async-watch-changes'. The BUFFER argument is a buffer where
951+
`post-command-hook' should be disabled. This is neccessary, because
952+
it is possible that user will change buffer during async function
953+
execusion."
954+
(with-current-buffer buffer
955+
(setq hs-utils/async-post-command-flag nil)
956+
(remove-hook
957+
'post-command-hook #'hs-utils/async-update-post-command-flag t)))
958+
924959
(provide 'haskell-commands)

0 commit comments

Comments
 (0)