From db122b8847b1c1896f3cf9512fd0ec9f79927715 Mon Sep 17 00:00:00 2001 From: Federico Tedin Date: Tue, 11 Jul 2023 18:36:42 +0200 Subject: [PATCH] Allow integer values for verb-auto-kill-response-buffers --- CHANGELOG.md | 1 + Makefile | 5 ++++- README.md | 2 +- test/init.el | 2 +- test/verb-test.el | 11 +++++++++++ verb.el | 38 ++++++++++++++++++++++++++++---------- 6 files changed, 46 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 909b902..3086dac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 C-c C-r C-. - 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. diff --git a/Makefile b/Makefile index bbabe7d..6e58fb4 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index 8c62f06..777dbbc 100644 --- a/README.md +++ b/README.md @@ -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 C-c C-r C-k. 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 C-c C-r C-k by default. Response headers buffers will also be killed automatically. -As you send more HTTP requests, more response buffers will be created, with `` 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 `` 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 diff --git a/test/init.el b/test/init.el index 4977ee8..34fca58 100644 --- a/test/init.el +++ b/test/init.el @@ -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)) diff --git a/test/verb-test.el b/test/verb-test.el index 6b36808..7c970f8 100644 --- a/test/verb-test.el +++ b/test/verb-test.el @@ -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) diff --git a/verb.el b/verb.el index a4e1155..da971da 100644 --- a/verb.el +++ b/verb.el @@ -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." @@ -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 @@ -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) @@ -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)))