Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add idle autorun feature to quickrun-autorun-mode #154

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ Replace region of code with its output.

### `quickrun-autorun-mode`

Minor mode which executes `quickrun` after saving buffer.
Minor mode which automatically executes `quickrun` after saving the buffer.
If you set `quickrun-autorun-idle-delay` to a number, `quickrun` will also run automatically after the buffer has been idle for that many seconds, even without saving.

#### `helm-quickrun`

Expand Down Expand Up @@ -172,6 +173,11 @@ If this value is `nil`, quickrun.el does not move focus to output buffer.

The `truncate-lines' value for `*quickrun*` buffer.

### `quickrun-autorun-idle-delay` (Default: `nil`)

Idle time in seconds before automatically running `quickrun` without saving.
If `nil`, automatic execution without saving is disabled.

## User Defined Command

You can add your own command or override existsing command by `quickrun-add-command` as below.
Expand Down
49 changes: 46 additions & 3 deletions quickrun.el
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@
:type 'boolean
:group 'quickrun)

(defcustom quickrun-autorun-idle-delay nil
"Idle time in seconds before automatically running `quickrun' without saving.
If NIL, automatic execution without saving is disabled."
:type '(choice (const :tag "Disable automatic run without save" nil)
(number :tag "Idle time in seconds"))
:group 'quickrun)

(defconst quickrun--buffer-name "*quickrun*")
(defvar quickrun--executed-file nil)
(defvar quickrun--remove-files nil)
Expand Down Expand Up @@ -1586,15 +1593,51 @@ With double prefix argument(C-u C-u), run in compile-only-mode."
(let ((quickrun-focus-p nil))
(quickrun)))

(defvar-local quickrun-autorun-file nil)
(defvar-local quickrun-autorun-buffer-hash nil)
(defvar-local quickrun-autorun-timer nil)

(defun quickrun--autorun-if-buffer-changed ()
"Run `quickrun--without-focus' if the buffer has changed since the lastcheck.

This function is intended to be called during idle time when
`quickrun-autorun-mode' is active with `quickrun-autorun-idle-delay' set.
It checks if the current buffer's content has changed by comparing its hash
with the previously stored hash. If the buffer has changed, it updates the
stored hash and executes `quickrun--without-focus` without saving the buffer.

This ensures that `quickrun` is automatically executed whenever the buffer
content changes, without the need to save the file."
(when (and quickrun-autorun-file (equal quickrun-autorun-file buffer-file-name))
(let ((hash (buffer-hash (current-buffer))))
(when (not (equal quickrun-autorun-buffer-hash hash))
(setq quickrun-autorun-buffer-hash hash)
(quickrun--without-focus)))))

;;;###autoload
(define-minor-mode quickrun-autorun-mode
"`quickrun' after saving buffer."
"Automatically `quickrun` the buffer after saving or after an idle period.

If `quickrun-autorun-no-save-run-idle-time' is non-NIL, will run `quickrun'
without saving the buffer when it has been idle for that many seconds.

If `quickrun-autorun-no-save-run-idle-time' is NIL, `quickrun' will run after the buffer is saved."
:init-value nil
:global nil
:lighter " QAR"
(if quickrun-autorun-mode
(add-hook 'after-save-hook 'quickrun--without-focus nil t)
(remove-hook 'after-save-hook 'quickrun--without-focus t)))
(if quickrun-autorun-idle-delay
(progn
(setq quickrun-autorun-timer
(run-with-idle-timer quickrun-autorun-idle-delay t #'quickrun--autorun-if-buffer-changed))
(setq quickrun-autorun-file buffer-file-name))
(add-hook 'after-save-hook 'quickrun--without-focus nil t))
(setq quickrun-autorun-file nil)
(setq quickrun-autorun-buffer-hash nil)
(remove-hook 'after-save-hook 'quickrun--without-focus t)
(when quickrun-autorun-timer
(cancel-timer quickrun-autorun-timer)
(setq quickrun-autorun-timer nil))))

;;
;; helm/anything interface
Expand Down