File: README.md

package info (click to toggle)
with-simulated-input-el 2.4%2Bgit20200216.29173588-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 148 kB
  • sloc: lisp: 441; sh: 11; makefile: 9
file content (63 lines) | stat: -rw-r--r-- 2,191 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# with-simulated-input

This package provides an Emacs Lisp macro, `with-simulated-input`,
which evaluates one or more forms while simulating a sequence of input
events for those forms to read. The result is the same as if you had
evaluated the forms and then manually typed in the same input. This
macro is useful for non-interactive testing of normally interactive
commands and functions, such as `completing-read`.

For example:

```elisp
(with-simulated-input
    "hello SPC world RET"
  (read-string "Say hello: "))
```

This would return the string `"hello world"`.

## Running code during input

Sometimes you need to simulate an interaction that is difficult or
impossible to express as a sequence of keys. In this case, you can
pass a list of "inputs" instead of a single string. Any string in this
list will be treated as a key sequence, and any other form will be
treated as Emacs Lisp code and evaluated at that point during the
simulated interaction. For example, we can use Emacs Lisp code to
enter "world" after entering "hello" via key sequence:

```elisp
(with-simulated-input
    '("hello SPC" (insert "world") "RET")
  (read-string "Say hello: "))
```

## Simulating idleness

Some interactive functions rely on idle timers to do their work, so
you might need a way to simulate idleness. For that, there is the
`wsi-simulate-idle-time` function. For example, the following code
will return `"hello world"`.

```elisp
;; Insert "world" after 500 seconds
(run-with-idle-timer 500 nil 'insert "world")
(with-simulated-input
    ;; Type "hello ", then "wait" 501 seconds, then type "RET"
    '("hello SPC" (wsi-simulate-idle-time 501) "RET")
  (read-string "Enter a string: "))
```

Note that the example code above only *pretends* to be idle for 501
seconds. It actually runs immediately.

Get it from MELPA: https://stable.melpa.org/#/with-simulated-input

## Running the tests

This package comes with a test suite. If you want to run it yourself,
first install the [Eldev](https://github.com/doublep/eldev), then use
`eldev test` to run the tests. Please run this test suite before
submitting any pull requests, and note in the pull request whether any
of the tests fail.