Skip to content

Commit

Permalink
Allow integer values for verb-auto-kill-response-buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
federicotdn committed Jul 11, 2023
1 parent 00ae076 commit db122b8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- The `verb-json-get` function now accepts negative integer arguments for accessing list elements.
- Changed default binding of `verb-send-request-on-point-no-window` to <kbd>C-c C-r C-<return></kbd>.
- Allow using single- or multi-line lambda expressions for `Verb-Map-Request`.
- The `verb-auto-kill-response-buffers` customizable variable can now be set to an integer. This will cause all response buffers to be killed when a request is sent, except the N most recent ones.

## **2.15.0** - 2021-11-03 (MELPA Stable)
- Fixed font locking on indented Babel source blocks.
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,8 @@ update:

run: ## Run emacs -Q with the working version of verb.el loaded.
run: clean server-bg
FONT_SIZE=$(FONT_SIZE) $(EMACS) -Q -L . --load test/init.el; \
make run-internal; \
make server-kill

run-internal:
FONT_SIZE=$(FONT_SIZE) $(EMACS) -Q -L . --load test/init.el
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ There's two recommended ways of closing response buffers:
- If the response buffer is the current buffer, you can use the `verb-kill-response-buffer-and-window` command, which is bound by default to <kbd>C-c C-r C-k</kbd>. This command will also kill the associated response headers buffer (see the [Response Headers Buffer](https://github.com/federicotdn/verb#the-response-headers-buffer) section).
- If the response buffer is not the current buffer (e.g. you are still on your `guide.org` buffer), you can kill **all** response buffers by using the `verb-kill-all-response-buffers`, which is bound to <kbd>C-c C-r C-k</kbd> by default. Response headers buffers will also be killed automatically.

As you send more HTTP requests, more response buffers will be created, with `<N>` at the end of their name to distinguish between them. If you wish to automatically have old response buffers killed when making a new request, set the `verb-auto-kill-response-buffers` variable to `t`.
As you send more HTTP requests, more response buffers will be created, with `<N>` at the end of their name to distinguish between them. If you wish to automatically have old response buffers killed when making a new request, set the `verb-auto-kill-response-buffers` variable to `t`. If wish for old response buffers to be killed, with the exception of the N most recent ones, then set `verb-auto-kill-response-buffers` to that integer number. This is useful for keeping track of the history of responses received, without creating too many buffers.

### Re-sending Requests

Expand Down
2 changes: 1 addition & 1 deletion test/init.el
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
(setq org-confirm-babel-evaluate nil)

;; Set up Verb
(setq verb-auto-kill-response-buffers t)
(setq verb-auto-kill-response-buffers 3)

;; Keybindings
(with-eval-after-load 'org (define-key org-mode-map (kbd "C-c C-r") verb-command-map))
Expand Down
11 changes: 11 additions & 0 deletions test/verb-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,17 @@
(verb-kill-all-response-buffers))
(should (zerop (length (get-response-buffers))))))

(ert-deftest test-kill-all-response-buffers-keep-n ()
(let ((verb-auto-kill-response-buffers 3))
(server-test "basic")
(server-test "basic")
(server-test "basic")
(server-test "basic")
(server-test "basic")
;; We should have the 3 most recent response buffers, plus
;; the one of the last request sent
(should (= (length (get-response-buffers)) 4))))

(ert-deftest test-re-send-request ()
(server-test "basic"
(erase-buffer)
Expand Down
38 changes: 28 additions & 10 deletions verb.el
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,17 @@ argument."
:type '(alist :key-type string :value-type function))

(defcustom verb-auto-kill-response-buffers nil
"If non-nil, kill all response buffers before sending a request.
Set this variable to t if you wish to have old response buffers (named
"Whether to kill existing response buffers before sending a request.
Set this variable to t if you wish to have all old response buffers (named
*HTTP Response*) automatically killed when sending a new HTTP
request."
:type 'boolean)
request.
Set this variable to an integer number if you wish to have all old response
buffers killed, except the N most recent ones.
Set this variable to nil if you do not wish to have any old response buffers
killed before sending a request."
:type '(choice (const :tag "Never" nil)
(integer :tag "Kill all but keep N most recent")
(const :tag "Kill all" t)))

(defcustom verb-inhibit-cookies nil
"If non-nil, do not send or receive cookies when sending requests."
Expand Down Expand Up @@ -378,6 +384,10 @@ here under its value.")
(defvar verb--requests-count 0
"Number of HTTP requests sent in the past.")

(defvar-local verb--response-number nil
"The number of this particular HTTP response buffer.")
(put 'verb--response-number 'permanent-local t)

(defvar verb--inhibit-code-tags-evaluation nil
"When non-nil, do not evaluate code tags in requests specs.
This variable is used mostly to parse and then copy request specs to
Expand Down Expand Up @@ -1388,13 +1398,20 @@ in."
;;;###autoload
(defun verb-kill-all-response-buffers (&optional keep-windows)
"Kill all response buffers, and delete their windows.
If KEEP-WINDOWS is non-nil, do not delete their respective windows."
If KEEP-WINDOWS is non-nil, do not delete their respective windows.
If the value of `verb-auto-kill-response-buffers' is an integer,
kill all response buffers but keep the N most recent ones."
(interactive)
(verb--ensure-verb-mode)
(dolist (buf (buffer-list))
(with-current-buffer buf
(when verb-http-response
(verb-kill-response-buffer-and-window keep-windows)))))
(let ((keep (if (integerp verb-auto-kill-response-buffers)
(max 0 verb-auto-kill-response-buffers)
0)))
(dolist (buf (buffer-list))
(with-current-buffer buf
(when (and verb-http-response
(> (1+ (- verb--requests-count verb--response-number))
keep))
(verb-kill-response-buffer-and-window keep-windows))))))

;;;###autoload
(defun verb-export-request-on-point (&optional name)
Expand Down Expand Up @@ -1869,13 +1886,14 @@ If a validation does not pass, signal `user-error'."
NUM is the request's identification number."
(with-current-buffer (generate-new-buffer
(format "*HTTP Response%s*"
(if verb-auto-kill-response-buffers
(if (eq verb-auto-kill-response-buffers t)
""
(format " %s" num))))
;; Set `verb-http-response's value to something other than nil
;; so that `verb-kill-all-response-buffers' can find it even if
;; no response was ever received.
(setq verb-http-response t)
(setq verb--response-number num)
(setq header-line-format "Waiting for HTTP response...")
(current-buffer)))

Expand Down

0 comments on commit db122b8

Please sign in to comment.