Skip to content

Commit

Permalink
Add new verb-set-var features
Browse files Browse the repository at this point in the history
  • Loading branch information
federicotdn committed Nov 10, 2023
1 parent e1e551f commit d931cd2
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
test/server.pid

# linting packages installation directory
/vendor
/packages

# autoloads
verb-autoloads.el
Expand Down
24 changes: 13 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ SHELL = bash
EMACS ?= emacs
PORT ?= 8000
NOOUTPUT = grep -v '^Loading' | { ! grep '^'; }
VENDOR = vendor
PACKAGES = packages
FONT_SIZE ?= 180
ENV ?= env
ACTIVATE = source $(ENV)/bin/activate
MAX_LINE_LEN = 80
WAIT_TIME ?= 0.5
SELECTOR ?= t
GIT_CLONE = git clone --depth 1

.PHONY: test

Expand Down Expand Up @@ -46,16 +45,15 @@ server-bg:
server-kill:
kill $$(cat test/server.pid)

clean: ## Clean up all temporary files created during testing/linting.
clean: ## Clean up all temporary files created during testing/runtime.
rm -f verb-autoloads.el test/server.pid
find . -name "*.elc" -type f -delete

setup-check: ## Install everything required for linting (package-lint and relint).
rm -rf $(VENDOR)
mkdir $(VENDOR)
$(GIT_CLONE) https://github.com/purcell/package-lint.git $(VENDOR)/package-lint
$(GIT_CLONE) https://github.com/mattiase/xr.git $(VENDOR)/xr
$(GIT_CLONE) https://github.com/mattiase/relint.git $(RELINT) $(VENDOR)/relint
setup-check: ## Install packages required for linting.
rm -rf $(PACKAGES)
$(EMACS) --batch \
--eval "(setq package-user-dir \"$$PWD/$(PACKAGES)\")" \
-l test/init-check.el

lint-file:
@printf "\n<<<------------ Lint file: $(filename) ------------>>>\n"
Expand All @@ -68,10 +66,14 @@ lint-file:
--eval '(setq sentence-end-double-space nil)' \
--eval '(checkdoc-current-buffer)' 2>&1 | $(NOOUTPUT)
@printf "\n--> Step: Run package-lint\n\n"
$(EMACS) --batch -l $(VENDOR)/package-lint/package-lint.el \
$(EMACS) --batch --eval "(setq package-user-dir \"$$PWD/$(PACKAGES)\")" \
--eval "(package-initialize)" \
--eval "(require 'package-lint)" \
-f package-lint-batch-and-exit "$(filename)"
@printf "\n--> Step: Run relint\n\n"
$(EMACS) --batch -l $(VENDOR)/xr/xr.el -l $(VENDOR)/relint/relint.el \
$(EMACS) --batch --eval "(setq package-user-dir \"$$PWD/$(PACKAGES)\")" \
--eval "(package-initialize)" \
--eval "(require 'relint)" \
-f relint-batch "$(filename)"
@printf "\n--> Step: Ensure maximum line length\n\n"
! grep -n '.\{$(MAX_LINE_LEN)\}' "$(filename)"
Expand Down
1 change: 1 addition & 0 deletions extra/verb-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,4 @@ All hooks, variables, functions and classes starting with `verb-` but not starti
- User Option: **verb-base-headers**
- User Option: **verb-trim-body-end**
- User Option: **verb-enable-elisp-completion**
- User Option: **verb-enable-var-preview**
11 changes: 11 additions & 0 deletions test/init-check.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
;; init-check.el --- Init for Verb linting/checks -*- lexical-binding: t; -*-

(require 'package)

(push '("melpa" . "https://melpa.org/packages/") package-archives)
(package-initialize)
(unless package-archive-contents
(package-refresh-contents))
(dolist (pkg '(cl-lib let-alist compat package-lint xr relint))
(unless (package-installed-p pkg)
(package-install pkg)))
20 changes: 20 additions & 0 deletions test/verb-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,15 @@
(with-temp-buffer
(should-not verb--vars)))

(ert-deftest test-verb-set-var-copy-kill-ring ()
(with-temp-buffer
(org-mode)
(verb-mode)
(verb--eval-string "(verb-var test-var-foo \"foo\")" (current-buffer))
(let ((current-prefix-arg '(4)))
(verb-set-var 'test-var-foo 123)
(should (string= (car kill-ring) "foo")))))

(ert-deftest test-verb-set-var-empty-name ()
(with-temp-buffer
(org-mode)
Expand Down Expand Up @@ -1779,6 +1788,17 @@
(verb-mode -1)
(should (equal font-lock-keywords '(t nil)))))

(ert-deftest test-verb-var-preview ()
(let ((code "(verb-var foobar 123123)"))
(with-temp-buffer
(org-mode)
(verb-mode)
(insert "{{" code "}}")
(verb--eval-string code (current-buffer))
(backward-char 4)
(message nil)
(should (string= "Current value for foobar: 123123" (verb--var-preview))))))

(ert-deftest test-disable-verb-mode-completion-at-point ()
(with-temp-buffer
(should-not (member #'verb-elisp-completion-at-point completion-at-point-functions))
Expand Down
58 changes: 52 additions & 6 deletions verb.el
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ Note the the point must be between the two code tag delimiters
\(e.g. \"{{\" and \"}}\") for the completion function to work."
:type 'boolean)

(defcustom verb-enable-var-preview t
"When set to a non-nil value, enable preview of Verb variables.
A preview of the value of a Verb variable will be shown in the
minibuffer, when the point is moved over a code tag containing a
call to `verb-var'."
:type 'boolean)

(defface verb-http-keyword '((t :inherit font-lock-constant-face
:weight bold))
"Face for highlighting HTTP methods.")
Expand Down Expand Up @@ -381,6 +388,9 @@ here under its value.")
(defvar-local verb--vars nil
"List of values set with `verb-var', with their corresponding names.")

(defvar verb--set-var-hist nil
"Input history for `verb-set-var'.")

(defvar verb--requests-count 0
"Number of HTTP requests sent in the past.")

Expand Down Expand Up @@ -409,6 +419,7 @@ other buffers without actually expanding the embedded code tags.")
(define-key map (kbd "C-b") #'verb-export-request-on-point-verb)
(define-key map (kbd "C-w") #'verb-export-request-on-point-eww)
(define-key map (kbd "C-v") #'verb-set-var)
(define-key map (kbd "C-x") #'verb-show-vars)
map)
"Keymap for `verb-mode' commands.
Bind this to an easy-to-reach key in Org mode in order to use Verb
Expand Down Expand Up @@ -487,6 +498,7 @@ more details on how to use it."
(add-hook 'completion-at-point-functions
#'verb-elisp-completion-at-point
nil 'local))
(add-hook 'post-command-hook #'verb--var-preview nil t)
(when (buffer-file-name)
(verb--log nil 'I
"Verb mode enabled in buffer: %s"
Expand Down Expand Up @@ -671,6 +683,27 @@ KEY and VALUE must be strings. KEY must not be the empty string."
(interactive)
(customize-group "verb"))

(defun verb--var-preview ()
"Preview the value of a Verb variable in the minibuffer."
(when-let* (((not (current-message)))
(verb-enable-var-preview)
;; Get the contents inside the code tag: {{<content>}}
(beg (save-excursion
(when (search-backward (car verb-code-tag-delimiters)
(line-beginning-position) t)
(match-end 0))))
(end (save-excursion
(when (search-forward (cdr verb-code-tag-delimiters)
(line-end-position) t)
(match-beginning 0))))
(code (buffer-substring-no-properties beg end))
(form (car (ignore-errors (read-from-string code))))
((eq (car form) 'verb-var))
(var (cadr form))
(val (assq var verb--vars)))
(let ((message-log-max nil))
(message "Current value for %s: %s" (car val) (cdr val)))))

(defun verb-elisp-completion-at-point ()
"Completion at point function for Lisp code tags."
(when-let (;; Get the contents inside the code tag: {{<content>}}
Expand Down Expand Up @@ -1124,23 +1157,36 @@ unless DEFAULT is non-nil, in which case that value is used instead."
"Set new value for variable VAR previously set with `verb-var'.
When called interactively, prompt the user for a variable that has
been set once with `verb-var', and then prompt for VALUE. Otherwise,
use string VAR and value VALUE."
use string VAR and value VALUE.

When called with a non-nil prefix argument, copy the current variable
value to the kill ring instead of setting it, and ignore VALUE."
(interactive)
(verb--ensure-verb-mode)
(let* ((name (or (and (stringp var) var)
(and (symbolp var) var (symbol-name var))
(completing-read "Variable: "
(mapcar (lambda (e)
(symbol-name (car e)))
verb--vars))))
verb--vars)
nil nil nil
'verb--set-var-hist)))
(key (intern name))
(val (or value (read-string (format "Set value for %s: " name))))
(val (or current-prefix-arg
value
(read-string (format "Set value for %s: " name))))
(elem (assq key verb--vars)))
(when (string-empty-p name)
(user-error "%s" "Variable name can't be empty"))
(if elem
(setcdr elem val)
(push (cons key val) verb--vars))))
(if current-prefix-arg
(progn
(kill-new (format "%s" (cdr elem)))
(message "Variable value copied to the kill ring"))
(setcdr elem val))
(if current-prefix-arg
(user-error "%s" "Variable has no value set")
(push (cons key val) verb--vars)))))

(defun verb-unset-vars ()
"Unset all variables set with `verb-var' or `verb-set-var'.
Expand Down Expand Up @@ -1357,7 +1403,7 @@ After the user has finished modifying the buffer, they can press
;; Don't require tagging for this temp buffer
(set (make-local-variable 'verb-tag) t)

;; Copy over verb variables
;; Copy over Verb variables
(setq verb--vars verb-variables)

;; Insert the request spec
Expand Down

0 comments on commit d931cd2

Please sign in to comment.