summary refs log tree commit diff stats
path: root/doc/aerc-config.5.scd
blob: 58eaa0f33f69ddaa60eeb1db4d54f05484e66a56 (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
aerc-config(5)

# NAME

aerc-config - configuration file formats for *aerc*(1)

# CONFIGURATION

There are three aerc config files: *aerc.conf*, *binds.conf*, and
*accounts.conf*. The last one must be kept secret, as it may include your
account credentials. We look for these files in your XDG config home plus
"aerc", which defaults to ~/.config/aerc.

Examples of these config files are typically included with your installation of
aerc and are usually installed in /usr/share/aerc.

Each file uses the _ini_ format, and consists of sections with keys and values.
A line beginning with # is considered a comment and ignored, as are empty lines.
New sections begin with [section-name] on a single line, and keys and values are
separated with "=".

# AERC.CONF

This file is used for configuring the general appearance and behavior of aerc.

## GENERAL OPTIONS

These options are configured in the *[general]* section of aerc.conf.

*default-save-path*
	Used as a default path for save operations if no other path is specified.

## UI OPTIONS

These options are configured in the *[ui]* section of aerc.conf.

*index-format*
	Describes the format for each row in a mailbox view. This field is
	compatible with mutt's printf-like syntax.

	Default: %D %-17.17n %s

[- *Format specifier*
:[ *Description*
|  %%
:  literal %
|  %a
:  sender address
|  %A
:  reply-to address, or sender address if none
|  %C
:  message number
|  %d
:  formatted message timestamp
|  %D
:  formatted message timestamp converted to local timezone
|  %f
:  sender name and address
|  %F
:  author name, or recipient name if the message is from you.
   The adderss is shown if no name part.
|  %g
:  message labels (for example notmuch tags)
|  %i
:  message id
|  %n
:  sender name, or sender address if none
|  %r
:  comma-separated list of formatted recipient names and addresses
|  %R
:  comma-separated list of formatted CC names and addresses
|  %s
:  subject
|  %t
:  the (first) address the new email was sent to
|  %T
:  the account name which received the email
|  %u
:  sender mailbox name (e.g. "smith" in "smith@example.net")
|  %v
:  sender first name (e.g. "Alex" in "Alex Smith <smith@example.net>")
|  %Z
:  flags (O=old, N=new, r=answered, D=deleted, !=flagged, \*=marked)

*timestamp-format*
	See time.Time#Format at https://godoc.org/time#Time.Format

	Default: 2006-01-02 03:04 PM (ISO 8601 + 12 hour time)

*sidebar-width*
	Width of the sidebar, including the border. Set to zero to disable the
	sidebar.

	Default: 20

*empty-message*
	Message to display when viewing an empty folder.

	Default: (no messages)

*empty-dirlist*
	Message to display when no folders exists or are all filtered.

	Default: (no folders)

*mouse-enabled*
	Enable mouse events in the ui, e.g. clicking and scrolling with the mousewheel

	Default: false

*new-message-bell*
	Ring the bell when a new message is received.

	Default: true

*pinned-tab-marker*
	Marker to show before a pinned tab's name.

	Default: `

*spinner*
	Animation shown while loading, split by spinner-delimiter (below)

	Examples:
	- spinner = "\-\_-,\_-\_"
	- spinner = '. , .'
	- spinner = "\,|,/,-"

	Default: "[..]    , [..]   ,  [..]  ,   [..] ,    [..],   [..] ,  [..]  , [..]   "

*spinner-delimiter*
	Spinner delimiter to split string into a animation

	Default: ","

*sort*
	List of space-separated criteria to sort the messages by, see *sort*
	command in *aerc*(1) for reference. Prefixing a criterion with "-r "
	reverses that criterion.

	Example: "from -r date"

	Default: ""

*dirlist-format*
	Describes the format string to use for the directory list

	Default: %n %>r

[- *Format specifier*
:[ *Description*
|  %%
:  literal %
|  %n
:  directory name
|  %r
:  recent/unseen/total message count
|  %>X
:  make format specifier 'X' be right justified

*next-message-on-delete*
	Moves to next message when the current message is deleted

	Default: true

*completion-popovers*
	Shows potential auto-completions for text inputs in popovers.

	Default: true

*completion-delay*
	How long to wait after the last input before auto-completion is triggered.

	Default: 250ms

## Contextual UI Configuration

The UI configuration can be specialized for accounts, specific mail
directories and message subjects. The specializations are added using
contextual config sections based on the context.

The contextual UI configuration is merged to the base UiConfig in the
following order:
*Base UIConfig > Account Context > Folder Context > Subject Context.*

*[ui:account=<AccountName>]*
	Adds account specific configuration with the account name.

*[ui:folder=<FolderName>]*
	Add folder specific configuration with the folder name.

*[ui:folder~<Regex>]*
	Add folder specific configuration for folders whose names match the regular
	expression.

*[ui:subject~<Regex>]*
	Add specialized ui configuration for messages that match a given regular
	expression.

Example:
```
[ui:account=Work]
sidebar-width=...

[ui:folder=Sent]
index-format=...

[ui:folder~Archive/\d+/.*]
index-format=...

[ui:subject~^\[PATCH]
index-format=...
```


## VIEWER

These options are configured in the *[viewer]* section of aerc.conf.

*pager*
	Specifies the pager to use when displaying emails. Note that some filters
	may add ANSI escape sequences to add color to rendered emails, so you may
	want to use a pager which supports ANSI.

	Default: less -R

*alternatives*
	If an email offers several versions (multipart), you can configure which
	mimetype to prefer. For example, this can be used to prefer plaintext over
	HTML emails.

	Default: text/plain,text/html

*header-layout*
	Defines the default headers to display when viewing a message. To display
	multiple headers in the same row, separate them with a pipe, e.g. "From|To".
	Rows will be hidden if none of their specified headers are present in the
	message.

	Default: From|To,Cc|Bcc,Date,Subject

*show-headers*
	Default setting to determine whether to show full headers or only parsed
	ones in message viewer.

	Default: false

*always-show-mime*
	Whether to always show the mimetype of an email, even when it is just a single part.

	Default: false

## COMPOSE

These options are configured in the *[compose]* section of aerc.conf.

*editor*
	Specifies the command to run the editor with. It will be shown in an
	embedded terminal, though it may also launch a graphical window if the
	environment supports it. Defaults to *$EDITOR*, or *vi*(1).

*header-layout*
	Defines the default headers to display when composing a message. To display
	multiple headers in the same row, separate them with a pipe, e.g. "To|From".

	Default: To|From,Subject

*address-book-cmd*
	Specifies the command to be used to tab-complete email addresses. Any
	occurrence of "%s" in the address-book-cmd will be replaced with what the
	user has typed so far.

	The command must output the completions to standard output, one completion
	per line. Each line must be tab-delimited, with an email address occurring as
	the first field. Only the email address field is required. The second field,
	if present, will be treated as the contact name. Additional fields are
	ignored.

	Example:
		khard email --remove-first-line --parsable '%s'

	Default: none

## FILTERS

Filters allow you to pipe an email body through a shell command to render
certain emails differently, e.g. highlighting them with ANSI escape codes.
They are configured in the *[filters]* section of aerc.conf.

The first filter which matches the email's mimetype will be used, so order
them from most to least specific.

You can also match on non-mimetypes, by prefixing with the header to match
against (non-case-sensitive) and a comma, e.g. subject,text will match a
subject which contains "text". Use header,~regex to match against a regex.

aerc ships with some default filters installed in the share directory (usually
_/usr/share/aerc/filters_). Note that these may have additional dependencies
that aerc does not have alone.

## TRIGGERS

Triggers specify commands to execute when certain events occur.

They are configured in the *[triggers]* section of aerc.conf.

*new-email*
	Executed when a new email arrives in the selected folder.

	e.g. new-email=exec notify-send "New email from %n" "%s"

	Default: ""

	Format specifiers from *index-format* are expanded with respect to the new
	message.

## TEMPLATES

Templates are used to populate the body of an email. The compose, reply
and forward commands can be called with the -T flag with the name of the
template name.

aerc ships with some default templates installed in the share directory (usually
_/usr/share/aerc/templates_).

These options are configured in the *[templates]* section of aerc.conf.

*template-dirs*
	The directory where the templates are stored. The config takes a
	colon-separated list of dirs.

	Default: "/usr/share/aerc/templates"

*quoted-reply*
	The template to be used for quoted replies.

	Default: "quoted_reply"

*forwards*
	The template to be used for forward as body.

	Default: "forward_as_body"

# ACCOUNTS.CONF

This file is used for configuring each mail account used for aerc. Each section
is the name of an account you want to configure, and the keys & values in that
section specify details of that account's configuration. In addition to the
options documented here, specific transports for incoming and outgoing emails
may have additional configuration parameters, documented on their respective man
pages.

Note that many of these configuration options are written for you, such as
*source* and *outgoing*, when you run the account configuration wizard
(*:new-account*).

*archive*
	Specifies a folder to use as the destination of the *:archive* command.

	Default: Archive

*copy-to*
	Specifies a folder to copy sent mails to, usually "Sent".

	Default: none

*default*
	Specifies the default folder to open in the message list when aerc
	configures this account.

	Default: INBOX

*folders*
	Specifies the comma separated list of folders to display in the sidebar.
	Names prefixed with ~ are interpreted as regular expressions.

	Default: all folders

*folders-sort*
	Specifies a comma separated list of folders to be shown at the top of the
	list in the provided order. Remaining folders will be sorted alphabetically.

	Default: none

*from*
	The default value to use for the From header in new emails. This should be
	an RFC 5322-compatible string, such as "Your Name <you@example.org>".

	Default: none

*outgoing*
	Specifies the transport for sending outgoing emails on this account.  It
	should be a connection string, and the specific meaning of each component
	varies depending on the protocol in use.  See each protocol's man page for
	more details:

	- *aerc-smtp*(5)

*outgoing-cred-cmd*
	Specifies an optional command that is run to get the outgoing account's
	password. See each protocol's man page for more details.

	Default: none

*postpone*
	Specifies the folder to save postponed messages to.

	Default: Drafts

*source*
	Specifies the source for reading incoming emails on this account. This key
	is required for all accounts. It should be a connection string, and the
	specific meaning of each component varies depending on the protocol in use.
	See each protocol's man page for more details:

	- *aerc-imap*(5)
	- *aerc-maildir*(5)
	- *aerc-notmuch*(5)

	Default: none

*source-cred-cmd*
	Specifies an optional command that is run to get the source account's
	password. See each protocol's man page for more details.

*signature-file*
	Specifies the file to read in order to obtain the signature to be added
	to emails sent from this account.

*signature-cmd*
	Specifies the command to execute in *sh* in order to obtain the
	signature to be added to emails sent from this account. If the command
	fails then *signature-file* is used instead.

# BINDS.CONF

This file is used for configuring keybindings used in the aerc interactive
client. You may configure different keybindings for different contexts by
writing them into different *[sections]* of the ini file. The available contexts
are:

*[messages]*
	keybindings for the message list

*[view]*
	keybindings for the message viewer

*[compose]*
	keybindings for the message composer

*[compose::editor]*
	keybindings for the composer, when the editor is focused

*[compose::review]*
	keybindings for the composer, when reviewing the email before it's sent

*[terminal]*
	keybindings for terminal tabs

You may also configure global keybindings by placing them at the beginning of
the file, before specifying any context-specific sections. For each *key=value*
option specified, the _key_ is the keystrokes pressed (in order) to invoke this
keybinding, and _value_ specifies keystrokes that aerc will simulate when the
keybinding is invoked. Generally this is used to execute commands, for example:

	rq = :reply -q<Enter>

Pressing r, then q, will simulate typing in ":reply -q<Enter>", and execute
:reply -q accordingly. It is also possible to invoke keybindings recursively in
a similar fashion. Additionally, the following special options are available in
each binding context:

*$noinherit*
	If set to "true", global keybindings will not be effective in this context.

	Default: false

*$ex*
	This can be set to a keystroke which will bring up the command input in this
	context.
	
	Default: <semicolon>

In addition to letters, special keys may be specified in <angle brackets>. The
following special keys are supported:

[[ *Name*
:- *Description*
|  space
:  " "
|  semicolon
:  ;
|  tab
:  
|  enter
:  
|  up
:  
|  down
:  
|  right
:  
|  left
:  
|  pgup
:  
|  pgdn
:  
|  home
:  
|  end
:  
|  insert
:  
|  delete
:  
|  exit
:  
|  cancel
:  
|  print
:  
|  pause
:  
|  backtab
: 
|  c-space
:  Ctrl+Space
|  c-a
:  Ctrl+a
|  c-b
:  Ctrl+b
|  c-c
:  Ctrl+c
|  c-d
:  Ctrl+d
|  c-e
:  Ctrl+e
|  c-f
:  Ctrl+f
|  c-g
:  Ctrl+g
|  c-h
:  Ctrl+h
|  c-i
:  Ctrl+i
|  c-j
:  Ctrl+j
|  c-k
:  Ctrl+k
|  c-l
:  Ctrl+l
|  c-m
:  Ctrl+m
|  c-n
:  Ctrl+n
|  c-o
:  Ctrl+o
|  c-p
:  Ctrl+p
|  c-q
:  Ctrl+q
|  c-r
:  Ctrl+r
|  c-s
:  Ctrl+s
|  c-t
:  Ctrl+t
|  c-u
:  Ctrl+u
|  c-v
:  Ctrl+v
|  c-w
:  Ctrl+w
|  c-x
:  Ctrl+x
|  c-y
:  Ctrl+y
|  c-z
:  Ctrl+z
|  c-]
:  Ctrl+]
|  c-[
:  Ctrl+[
|  c-^
:  Ctrl+^
|  c-_
:  Ctrl+_

# SEE ALSO

*aerc*(1) *aerc-imap*(5) *aerc-smtp*(5) *aerc-maildir*(5) *aerc-sendmail*(5)
*aerc-notmuch*(5)

# AUTHORS

Maintained by Drew DeVault <sir@cmpwn.com>, who is assisted by other open
source contributors. For more information about aerc development, see
https://git.sr.ht/~sircmpwn/aerc.
e previous revision' href='/akkartik/mu/blame/edit/002-typing.mu?h=main&id=9f78ec2d5e2a74ec24cc20cd3b1d60c3bdb9d605'>^
3d8b137c ^
a90faae9 ^



ada5eb55 ^
a90faae9 ^
741ef43e ^

a90faae9 ^




3d8b137c ^




a90faae9 ^
ea5e7fd4 ^





a90faae9 ^
a90faae9 ^

a90faae9 ^

c76b0066 ^
a90faae9 ^
4a48bedc ^
c76b0066 ^
3d8b137c ^








a90faae9 ^

3d8b137c ^
7bf9212f ^
a90faae9 ^



3d8b137c ^
c76b0066 ^
a90faae9 ^
736b26a7 ^
c76b0066 ^
736b26a7 ^
1ead3562 ^
a90faae9 ^

3d8b137c ^
7bf9212f ^
a90faae9 ^

c76b0066 ^
a90faae9 ^

c76b0066 ^
a90faae9 ^
4a48bedc ^
3d8b137c ^
ea5e7fd4 ^


3d8b137c ^







a90faae9 ^


ea5e7fd4 ^

3d8b137c ^
a90faae9 ^

3d8b137c ^
a90faae9 ^
3d8b137c ^



a90faae9 ^

136412d2 ^
c76b0066 ^
a90faae9 ^



3d8b137c ^
a90faae9 ^
3d8b137c ^
a90faae9 ^
3d8b137c ^
a90faae9 ^

3d8b137c ^
c76b0066 ^
a90faae9 ^

3d8b137c ^

a90faae9 ^
136412d2 ^
a90faae9 ^
741ef43e ^
a90faae9 ^

c76b0066 ^
a90faae9 ^
c76b0066 ^
a90faae9 ^


2d91279b ^
a90faae9 ^
4a48bedc ^
ee1a18f0 ^
3d8b137c ^


1521e4e3 ^
a90faae9 ^
71056d61 ^
a90faae9 ^

ee1a18f0 ^
a90faae9 ^


6f65d591 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^

55479bc2 ^
a90faae9 ^



71056d61 ^
a90faae9 ^




6f65d591 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^




55479bc2 ^
6f65d591 ^

a90faae9 ^



71056d61 ^
a90faae9 ^









6f65d591 ^
a90faae9 ^
9baf76ec ^
a90faae9 ^




55479bc2 ^
6f65d591 ^

a90faae9 ^








6f65d591 ^
a90faae9 ^
6f65d591 ^
a90faae9 ^
9baf76ec ^
a90faae9 ^




55479bc2 ^
6f65d591 ^

a90faae9 ^








6f65d591 ^
a90faae9 ^
6f65d591 ^
a90faae9 ^
9baf76ec ^
a90faae9 ^




55479bc2 ^
6f65d591 ^

a90faae9 ^








6f65d591 ^
a90faae9 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^





55479bc2 ^
6f65d591 ^

a90faae9 ^



71056d61 ^
a90faae9 ^









6f65d591 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^





55479bc2 ^
6f65d591 ^

a90faae9 ^








6f65d591 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^




55479bc2 ^
a90faae9 ^



71056d61 ^
a90faae9 ^





6f65d591 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^







55479bc2 ^
a90faae9 ^



71056d61 ^
a90faae9 ^





6f65d591 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^





55479bc2 ^
a90faae9 ^



71056d61 ^
a90faae9 ^





6f65d591 ^
a90faae9 ^
6f65d591 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^





55479bc2 ^
a90faae9 ^




71056d61 ^
a90faae9 ^





6f65d591 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^





55479bc2 ^
a90faae9 ^



71056d61 ^
a90faae9 ^





6f65d591 ^
a90faae9 ^
6f65d591 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^





55479bc2 ^
a90faae9 ^




71056d61 ^
a90faae9 ^





6f65d591 ^
a90faae9 ^
6f65d591 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^





55479bc2 ^
a90faae9 ^




71056d61 ^
a90faae9 ^





6f65d591 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^



55479bc2 ^
a90faae9 ^



71056d61 ^
a90faae9 ^






6f65d591 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^




55479bc2 ^
a90faae9 ^




71056d61 ^
a90faae9 ^







55479bc2 ^
a90faae9 ^





71056d61 ^
a90faae9 ^




6f65d591 ^
a90faae9 ^

6f65d591 ^
a90faae9 ^
9baf76ec ^
6f65d591 ^
a90faae9 ^





55479bc2 ^
6f65d591 ^

a90faae9 ^


















46824f3e ^


3d8b137c ^




46824f3e ^
3d8b137c ^
46824f3e ^

3d8b137c ^
46824f3e ^


3d8b137c ^
46824f3e ^




3d8b137c ^
46824f3e ^






3d8b137c ^
46824f3e ^


ea5e7fd4 ^


a90faae9 ^

3d8b137c ^
a90faae9 ^


c76b0066 ^
a90faae9 ^


46824f3e ^
6f65d591 ^
a90faae9 ^
9baf76ec ^
a90faae9 ^
46824f3e ^
a90faae9 ^


55479bc2 ^
6f65d591 ^

a90faae9 ^


46824f3e ^

71056d61 ^
a90faae9 ^



46824f3e ^
a90faae9 ^


46824f3e ^

a90faae9 ^
46824f3e ^
6f65d591 ^
46824f3e ^
9baf76ec ^

46824f3e ^



9baf76ec ^
46824f3e ^

a90faae9 ^
46824f3e ^

a90faae9 ^

55479bc2 ^
a90faae9 ^


46824f3e ^


71056d61 ^
a90faae9 ^


b14ac6dc ^
6f65d591 ^
b14ac6dc ^
9baf76ec ^
b14ac6dc ^




55479bc2 ^
6f65d591 ^

b14ac6dc ^




71056d61 ^
b14ac6dc ^







a90faae9 ^

2d91279b ^
3d8b137c ^
a90faae9 ^


ea5e7fd4 ^
a90faae9 ^


6f65d591 ^
a90faae9 ^
9baf76ec ^
a90faae9 ^




55479bc2 ^
a90faae9 ^




71056d61 ^
a90faae9 ^





3d8b137c ^
a90faae9 ^
736b26a7 ^
c76b0066 ^
736b26a7 ^
c76b0066 ^
a90faae9 ^


c76b0066 ^
a90faae9 ^
4a48bedc ^
3d8b137c ^





a59f20f8 ^
b7556315 ^
b7556315 ^




ea5e7fd4 ^

a90faae9 ^

3d8b137c ^
a90faae9 ^

ea5e7fd4 ^

a90faae9 ^
a59f20f8 ^



a90faae9 ^
3d8b137c ^
1ead3562 ^
3d8b137c ^



a90faae9 ^
3d8b137c ^
a90faae9 ^
c76b0066 ^
a90faae9 ^


a90faae9 ^

b7556315 ^

4a48bedc ^
b7556315 ^














a90faae9 ^

3d8b137c ^
a90faae9 ^
4a48bedc ^
3d8b137c ^
1ead3562 ^
3d8b137c ^
1ead3562 ^
a90faae9 ^
741ef43e ^
a90faae9 ^
3d8b137c ^
a90faae9 ^
3d8b137c ^

a90faae9 ^

3d8b137c ^
a90faae9 ^










a90faae9 ^


6f65d591 ^
a90faae9 ^
9baf76ec ^
a90faae9 ^




55479bc2 ^
a90faae9 ^




71056d61 ^
a90faae9 ^




6f65d591 ^
a90faae9 ^
9baf76ec ^

a90faae9 ^



9baf76ec ^
a90faae9 ^
9baf76ec ^


a90faae9 ^

55479bc2 ^
a90faae9 ^






71056d61 ^
a90faae9 ^


b7556315 ^
































a90faae9 ^
6f65d591 ^
a90faae9 ^
6f65d591 ^
a90faae9 ^

9baf76ec ^
a90faae9 ^






55479bc2 ^
6f65d591 ^

a90faae9 ^








6f65d591 ^
a90faae9 ^
6f65d591 ^
a90faae9 ^

9baf76ec ^
a90faae9 ^







55479bc2 ^
6f65d591 ^

a90faae9 ^









3d8b137c ^
a90faae9 ^
ea5e7fd4 ^
c76b0066 ^
a90faae9 ^




3d8b137c ^
a90faae9 ^
ea5e7fd4 ^
c76b0066 ^
a90faae9 ^




3d8b137c ^
a90faae9 ^
4a48bedc ^
ee1a18f0 ^


4a48bedc ^
a90faae9 ^



4a48bedc ^
a90faae9 ^




4a48bedc ^
a90faae9 ^





2d91279b ^
a90faae9 ^
136412d2 ^
a90faae9 ^



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146

                                                              

                                    
                     
             
             
              
                                            
                                                      
                                



                                               
                                                                                                                                  
             
             


                                                              

                                                          
                                                           
                                                                   



                                   
                                                                   
     
                            
                                   
                      


                     
                        
                                                                

                               
                                              






                                                   
                                                                                                    
             
             
                               
                                    
                                                             


                                                      
                                  

                                                         
                                   
                   
                     
                                                               
                                       
                   
              
               

 


                                                                              
                                                                                                        
             
             
                      


                                           
                                   

                                                                                                  
                   

                         
                                                       
                                   
                                                             

                                                                       


                     
                                                           
                        
                                 


                                                                              
                                                  
                                 
                                                    
                             

                                                                 
     
                                     

                                           
                                          


                                  
                                                    
                                   
                                                                 
                                    



                                                                   



                         

                       
                          



                                                                          
                                           



                                 
                          
     

                     




                                                        




                                                                                
                               





                                                               
   

 

                                                                                    
                                                                                                                          
             
             
                                      








                                                                       

             
                                                             
                            



                                                               
                                                           
                                                    
                          
                            
                                                    
                          
          

                                                     
                                                                   
                                                                             

                                              
                    

 
                                                                                                                    
             
             
                                                                       


                                                             







                                                        


                                                   

                                                             
                                               

                                                                               
                                      
                        



                                               

                                             
                   
                         



                                                               
                                                                  
                      
                                                 
                                             
                                       

                                                       
                                                       
                                      

                       

                                                 
                          
                         
                                       
                       

          
                         
   
                    


                  
                                                                                        
             
             
                                        


                                              
                                
                   
                                                                  

                                                  
                                      


                                           
             
                                  
                                                  
                         

                   
                                        



                         
                                    




                                      
             
                                  
                                                  
                         




                                 
                                        

                                             



                         
                                    









                                                   
             
                                  
                                                  




                                                      
                                        

                                             








                                                     
             
                                  
                    
    
                                              




                                                          
                                        

                                             








                                                     
             
                                  
                    
    
                                              




                                 
                                        

                                             








                                                     
             
                                  
                                            
                                                 
                         





                                   
                                        

                                             



                         
                          









                                                   
             
                                  
                                                 
                         





                                
                                        

                                             








                                                      
             
                                  
                                              
                         




                  
                                        



                         
                          





                                                  
             
                                  
                                                  
                         







                                        
                                        



                         
                                    





                                                                                      
             
                                  
                                                  
                         





                                         
                                        



                         
                                    





                                                  
             
                                  
                    
  
                                              
                         





                                             
                                        




                         
                                    





                                                  
             
                                  
                                                  
                         





                                     
                                        



                         
                                    





                                                  
             
                                  
                    
  
                                              
                         





                                     
                                        




                         
                                    





                                                  
             
                                  
                    
  
                                              
                         





                                     
                                        




                         
                                    





                                                         
             
                                  
                                                
                         



                  
                                        



                         
                          






                                                       
             
                                 
                                                 
                         




                  
                                        




                         
                     







                        
                                        





                         
                     




                                        
             

                                   
                        
     
                                             
                         





                               
                                        

                                             


















                                                                     


                                                                                  




                                                                                
                                                           
                                         

                              
                                                    


                                                                         
                                                  




                                                                           
                                                  






                                                                              
                                                                                 


                                                     


                                                               

                                             
                                                                      


                                
                      


   
                                                                           
             
                                  
                                                   
                  
                                                 


            
                                        

                                             


                         

                  
                          



                         
                           


   

                                                                        
                                  
                                         
                    
    

                                             



                         
                          

                
                  

                                           

       
                                        


                         


                  
                          


   
                                             
             
                                  
                                                   




                                                     
                                        

                                             




                         
                          







                           

                                                                                          
                  
              


                               
                                                


                                                           
             
                                  
                                                  




                  
                                        




                         
                                    





                                  
                                        
                         
                        
                                             
                      
                      


   
                                                                                                     
             
             





                                                                       
                             
                                                                    




                                                         

                                                             

                
                                                                                                    

                              

                                                                        
   



                                                             
                       
                                             
                       



                                                               
   
                                                   
                         
                                             


                 

 

                                                              
             














                                                                                          

                                                                             
                                                                                 
             
             
                      
                    
                                     
                     
   
                     
                     
                                       
                      

                                           

                                       
                                       










                                       


                                                             
             
                                  
                                                  




                  
                                        




                         
                                  




                                                                         
             
                                  

                                                   



                         
                          
                


                  

       
                                        






                                
                          


   
































                                                             
                                              
             
                                   
                   

    
                                              






                                                
                                        

                                             








                                                 
             
                                   
                   

    
                                              







                                                                            
                                        

                                             









                                                 
                                                   
                             
                                                   
                      




                            
                                               
                           
                                                  
                      




          
                                                                                   
             
             


                                                   
                                             



                                 
                                            




                            
                                                  





                                      
                                                                                               
                          
                                        



                 
## handling events from the keyboard, mouse, touch screen, ...

# temporary main: interactive editor
# hit ctrl-c to exit
def! main text:text [
  local-scope
  load-inputs
  open-console
  clear-screen 0/screen  # non-scrolling app
  editor:&:editor <- new-editor text, 5/left, 45/right
  editor-render 0/screen, editor
  editor-event-loop 0/screen, 0/console, editor
  close-console
]

def editor-event-loop screen:&:screen, console:&:console, editor:&:editor -> screen:&:screen, console:&:console, editor:&:editor [
  local-scope
  load-inputs
  {
    # looping over each (keyboard or touch) event as it occurs
    +next-event
    cursor-row:num <- get *editor, cursor-row:offset
    cursor-column:num <- get *editor, cursor-column:offset
    screen <- move-cursor screen, cursor-row, cursor-column
    e:event, found?:bool, quit?:bool, console <- read-event console
    loop-unless found?
    break-if quit?  # only in tests
    trace 10, [app], [next-event]
    # 'touch' event
    t:touch-event, is-touch?:bool <- maybe-convert e, touch:variant
    {
      break-unless is-touch?
      move-cursor editor, screen, t
      loop +next-event
    }
    # keyboard events
    {
      break-if is-touch?
      go-render?:bool <- handle-keyboard-event screen, editor, e
      {
        break-unless go-render?
        screen <- editor-render screen, editor
      }
    }
    loop
  }
]

# process click, return if it was on current editor
def move-cursor editor:&:editor, screen:&:screen, t:touch-event -> in-focus?:bool, editor:&:editor [
  local-scope
  load-inputs
  return-unless editor, 0/false
  click-row:num <- get t, row:offset
  return-unless click-row, 0/false  # ignore clicks on 'menu'
  click-column:num <- get t, column:offset
  left:num <- get *editor, left:offset
  too-far-left?:bool <- lesser-than click-column, left
  return-if too-far-left?, 0/false
  right:num <- get *editor, right:offset
  too-far-right?:bool <- greater-than click-column, right
  return-if too-far-right?, 0/false
  # position cursor
  <begin-move-cursor>
  editor <- snap-cursor editor, screen, click-row, click-column
  undo-coalesce-tag:num <- copy 0/never
  <end-move-cursor>
  # gain focus
  return 1/true
]

# Variant of 'render' that only moves the cursor (coordinates and
# before-cursor). If it's past the end of a line, it 'slides' it left. If it's
# past the last line it positions at end of last line.
def snap-cursor editor:&:editor, screen:&:screen, target-row:num, target-column:num -> editor:&:editor [
  local-scope
  load-inputs
  return-unless editor
  left:num <- get *editor, left:offset
  right:num <- get *editor, right:offset
  screen-height:num <- screen-height screen
  # count newlines until screen row
  curr:&:duplex-list:char <- get *editor, top-of-screen:offset
  prev:&:duplex-list:char <- copy curr  # just in case curr becomes null and we can't compute prev
  curr <- next curr
  row:num <- copy 1/top
  column:num <- copy left
  *editor <- put *editor, cursor-row:offset, target-row
  cursor-row:num <- copy target-row
  *editor <- put *editor, cursor-column:offset, target-column
  cursor-column:num <- copy target-column
  before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset
  {
    +next-character
    break-unless curr
    off-screen?:bool <- greater-or-equal row, screen-height
    break-if off-screen?
    # update editor.before-cursor
    # Doing so at the start of each iteration ensures it stays one step behind
    # the current character.
    {
      at-cursor-row?:bool <- equal row, cursor-row
      break-unless at-cursor-row?
      at-cursor?:bool <- equal column, cursor-column
      break-unless at-cursor?
      before-cursor <- copy prev
      *editor <- put *editor, before-cursor:offset, before-cursor
    }
    c:char <- get *curr, value:offset
    {
      # newline? move to left rather than 0
      newline?:bool <- equal c, 10/newline
      break-unless newline?
      # adjust cursor if necessary
      {
        at-cursor-row?:bool <- equal row, cursor-row
        break-unless at-cursor-row?
        left-of-cursor?:bool <- lesser-than column, cursor-column
        break-unless left-of-cursor?
        cursor-column <- copy column
        *editor <- put *editor, cursor-column:offset, cursor-column
        before-cursor <- copy prev
        *editor <- put *editor, before-cursor:offset, before-cursor
      }
      # skip to next line
      row <- add row, 1
      column <- copy left
      curr <- next curr
      prev <- next prev
      loop +next-character
    }
    {
      # at right? wrap. even if there's only one more letter left; we need
      # room for clicking on the cursor after it.
      at-right?:bool <- equal column, right
      break-unless at-right?
      column <- copy left
      row <- add row, 1
      # don't increment curr/prev
      loop +next-character
    }
    curr <- next curr
    prev <- next prev
    column <- add column, 1
    loop
  }
  # is cursor to the right of the last line? move to end
  {
    at-cursor-row?:bool <- equal row, cursor-row
    cursor-outside-line?:bool <- lesser-or-equal column, cursor-column
    before-cursor-on-same-line?:bool <- and at-cursor-row?, cursor-outside-line?
    above-cursor-row?:bool <- lesser-than row, cursor-row
    before-cursor?:bool <- or before-cursor-on-same-line?, above-cursor-row?
    break-unless before-cursor?
    cursor-row <- copy row
    *editor <- put *editor, cursor-row:offset, cursor-row
    cursor-column <- copy column
    *editor <- put *editor, cursor-column:offset, cursor-column
    before-cursor <- copy prev
    *editor <- put *editor, before-cursor:offset, before-cursor
  }
]

# Process an event 'e' and try to minimally update the screen.
# Set 'go-render?' to true to indicate the caller must perform a non-minimal update.
def handle-keyboard-event screen:&:screen, editor:&:editor, e:event -> go-render?:bool, screen:&:screen, editor:&:editor [
  local-scope
  load-inputs
  return-unless editor, 0/don't-render
  screen-width:num <- screen-width screen
  screen-height:num <- screen-height screen
  left:num <- get *editor, left:offset
  right:num <- get *editor, right:offset
  before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset
  cursor-row:num <- get *editor, cursor-row:offset
  cursor-column:num <- get *editor, cursor-column:offset
  save-row:num <- copy cursor-row
  save-column:num <- copy cursor-column
  # character
  {
    c:char, is-unicode?:bool <- maybe-convert e, text:variant
    break-unless is-unicode?
    trace 10, [app], [handle-keyboard-event: special character]
    # exceptions for special characters go here
    <handle-special-character>
    # ignore any other special characters
    regular-character?:bool <- greater-or-equal c, 32/space
    return-unless regular-character?, 0/don't-render
    # otherwise type it in
    <begin-insert-character>
    go-render? <- insert-at-cursor editor, c, screen
    <end-insert-character>
    return
  }
  # special key to modify the text or move the cursor
  k:num, is-keycode?:bool <- maybe-convert e:event, keycode:variant
  assert is-keycode?, [event was of unknown type; neither keyboard nor mouse]
  # handlers for each special key will go here
  <handle-special-key>
  return 1/go-render
]

def insert-at-cursor editor:&:editor, c:char, screen:&:screen -> go-render?:bool, editor:&:editor, screen:&:screen [
  local-scope
  load-inputs
  before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset
  insert c, before-cursor
  before-cursor <- next before-cursor
  *editor <- put *editor, before-cursor:offset, before-cursor
  cursor-row:num <- get *editor, cursor-row:offset
  cursor-column:num <- get *editor, cursor-column:offset
  left:num <- get *editor, left:offset
  right:num <- get *editor, right:offset
  save-row:num <- copy cursor-row
  save-column:num <- copy cursor-column
  screen-width:num <- screen-width screen
  screen-height:num <- screen-height screen
  # occasionally we'll need to mess with the cursor
  <insert-character-special-case>
  # but mostly we'll just move the cursor right
  cursor-column <- add cursor-column, 1
  *editor <- put *editor, cursor-column:offset, cursor-column
  next:&:duplex-list:char <- next before-cursor
  {
    # at end of all text? no need to scroll? just print the character and leave
    at-end?:bool <- equal next, 0/null
    break-unless at-end?
    bottom:num <- subtract screen-height, 1
    at-bottom?:bool <- equal save-row, bottom
    at-right?:bool <- equal save-column, right
    overflow?:bool <- and at-bottom?, at-right?
    break-if overflow?
    move-cursor screen, save-row, save-column
    print screen, c
    return 0/don't-render
  }
  {
    # not at right margin? print the character and rest of line
    break-unless next
    at-right?:bool <- greater-or-equal cursor-column, screen-width
    break-if at-right?
    curr:&:duplex-list:char <- copy before-cursor
    move-cursor screen, save-row, save-column
    curr-column:num <- copy save-column
    {
      # hit right margin? give up and let caller render
      at-right?:bool <- greater-than curr-column, right
      return-if at-right?, 1/go-render
      break-unless curr
      # newline? done.
      currc:char <- get *curr, value:offset
      at-newline?:bool <- equal currc, 10/newline
      break-if at-newline?
      print screen, currc
      curr-column <- add curr-column, 1
      curr <- next curr
      loop
    }
    return 0/don't-render
  }
  return 1/go-render
]

# helper for tests
def editor-render screen:&:screen, editor:&:editor -> screen:&:screen, editor:&:editor [
  local-scope
  load-inputs
  old-top-idx:num <- save-top-idx screen
  left:num <- get *editor, left:offset
  right:num <- get *editor, right:offset
  row:num, column:num <- render screen, editor
  clear-line-until screen, right
  row <- add row, 1
  draw-horizontal screen, row, left, right, 9480/horizontal-dotted
  row <- add row, 1
  clear-screen-from screen, row, left, left, right
  assert-no-scroll screen, old-top-idx
]

scenario editor-handles-empty-event-queue [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abc], 0/left, 10/right
  editor-render screen, e
  assume-console []
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    .abc       .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
]

scenario editor-handles-mouse-clicks [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abc], 0/left, 10/right
  editor-render screen, e
  $clear-trace
  assume-console [
    left-click 1, 1  # on the 'b'
  ]
  run [
    editor-event-loop screen, console, e
    3:num/raw <- get *e, cursor-row:offset
    4:num/raw <- get *e, cursor-column:offset
  ]
  screen-should-contain [
    .          .
    .abc       .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
  memory-should-contain [
    3 <- 1  # cursor is at row 0..
    4 <- 1  # ..and column 1
  ]
  check-trace-count-for-label 0, [print-character]
]

scenario editor-handles-mouse-clicks-outside-text [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abc], 0/left, 10/right
  $clear-trace
  assume-console [
    left-click 1, 7  # last line, to the right of text
  ]
  run [
    editor-event-loop screen, console, e
    3:num/raw <- get *e, cursor-row:offset
    4:num/raw <- get *e, cursor-column:offset
  ]
  memory-should-contain [
    3 <- 1  # cursor row
    4 <- 3  # cursor column
  ]
  check-trace-count-for-label 0, [print-character]
]

scenario editor-handles-mouse-clicks-outside-text-2 [
  local-scope
  assume-screen 10/width, 5/height
  s:text <- new [abc
def]
  e:&:editor <- new-editor s, 0/left, 10/right
  $clear-trace
  assume-console [
    left-click 1, 7  # interior line, to the right of text
  ]
  run [
    editor-event-loop screen, console, e
    3:num/raw <- get *e, cursor-row:offset
    4:num/raw <- get *e, cursor-column:offset
  ]
  memory-should-contain [
    3 <- 1  # cursor row
    4 <- 3  # cursor column
  ]
  check-trace-count-for-label 0, [print-character]
]

scenario editor-handles-mouse-clicks-outside-text-3 [
  local-scope
  assume-screen 10/width, 5/height
  s:text <- new [abc
def]
  e:&:editor <- new-editor s, 0/left, 10/right
  $clear-trace
  assume-console [
    left-click 3, 7  # below text
  ]
  run [
    editor-event-loop screen, console, e
    3:num/raw <- get *e, cursor-row:offset
    4:num/raw <- get *e, cursor-column:offset
  ]
  memory-should-contain [
    3 <- 2  # cursor row
    4 <- 3  # cursor column
  ]
  check-trace-count-for-label 0, [print-character]
]

scenario editor-handles-mouse-clicks-outside-column [
  local-scope
  assume-screen 10/width, 5/height
  # editor occupies only left half of screen
  e:&:editor <- new-editor [abc], 0/left, 5/right
  editor-render screen, e
  $clear-trace
  assume-console [
    # click on right half of screen
    left-click 3, 8
  ]
  run [
    editor-event-loop screen, console, e
    3:num/raw <- get *e, cursor-row:offset
    4:num/raw <- get *e, cursor-column:offset
  ]
  screen-should-contain [
    .          .
    .abc       .
    .┈┈┈┈┈     .
    .          .
  ]
  memory-should-contain [
    3 <- 1  # no change to cursor row
    4 <- 0  # ..or column
  ]
  check-trace-count-for-label 0, [print-character]
]

scenario editor-handles-mouse-clicks-in-menu-area [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abc], 0/left, 5/right
  editor-render screen, e
  $clear-trace
  assume-console [
    # click on first, 'menu' row
    left-click 0, 3
  ]
  run [
    editor-event-loop screen, console, e
    3:num/raw <- get *e, cursor-row:offset
    4:num/raw <- get *e, cursor-column:offset
  ]
  # no change to cursor
  memory-should-contain [
    3 <- 1
    4 <- 0
  ]
]

scenario editor-inserts-characters-into-empty-editor [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [], 0/left, 5/right
  editor-render screen, e
  $clear-trace
  assume-console [
    type [abc]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    .abc       .
    .┈┈┈┈┈     .
    .          .
  ]
  check-trace-count-for-label 3, [print-character]
]

scenario editor-inserts-characters-at-cursor [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abc], 0/left, 10/right
  editor-render screen, e
  $clear-trace
  # type two letters at different places
  assume-console [
    type [0]
    left-click 1, 2
    type [d]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    .0adbc     .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
  check-trace-count-for-label 7, [print-character]  # 4 for first letter, 3 for second
]

scenario editor-inserts-characters-at-cursor-2 [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abc], 0/left, 10/right
  editor-render screen, e
  $clear-trace
  assume-console [
    left-click 1, 5  # right of last line
    type [d]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    .abcd      .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
  check-trace-count-for-label 1, [print-character]
]

scenario editor-inserts-characters-at-cursor-5 [
  local-scope
  assume-screen 10/width, 5/height
  s:text <- new [abc
d]
  e:&:editor <- new-editor s, 0/left, 10/right
  editor-render screen, e
  $clear-trace
  assume-console [
    left-click 1, 5  # right of non-last line
    type [e]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    .abce      .
    .d         .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
  check-trace-count-for-label 1, [print-character]
]

scenario editor-inserts-characters-at-cursor-3 [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abc], 0/left, 10/right
  editor-render screen, e
  $clear-trace
  assume-console [
    left-click 3, 5  # below all text
    type [d]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    .abcd      .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
  check-trace-count-for-label 1, [print-character]
]

scenario editor-inserts-characters-at-cursor-4 [
  local-scope
  assume-screen 10/width, 5/height
  s:text <- new [abc
d]
  e:&:editor <- new-editor s, 0/left, 10/right
  editor-render screen, e
  $clear-trace
  assume-console [
    left-click 3, 5  # below all text
    type [e]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    .abc       .
    .de        .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
  check-trace-count-for-label 1, [print-character]
]

scenario editor-inserts-characters-at-cursor-6 [
  local-scope
  assume-screen 10/width, 5/height
  s:text <- new [abc
d]
  e:&:editor <- new-editor s, 0/left, 10/right
  editor-render screen, e
  $clear-trace
  assume-console [
    left-click 3, 5  # below all text
    type [ef]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    .abc       .
    .def       .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
  check-trace-count-for-label 2, [print-character]
]

scenario editor-moves-cursor-after-inserting-characters [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [ab], 0/left, 5/right
  editor-render screen, e
  assume-console [
    type [01]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    .01ab      .
    .┈┈┈┈┈     .
    .          .
  ]
]

# if the cursor reaches the right margin, wrap the line

scenario editor-wraps-line-on-insert [
  local-scope
  assume-screen 5/width, 5/height
  e:&:editor <- new-editor [abc], 0/left, 5/right
  editor-render screen, e
  # type a letter
  assume-console [
    type [e]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  # no wrap yet
  screen-should-contain [
    .     .
    .eabc .
    .┈┈┈┈┈.
    .     .
    .     .
  ]
  # type a second letter
  assume-console [
    type [f]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  # now wrap
  screen-should-contain [
    .     .
    .efab.
    .c    .
    .┈┈┈┈┈.
    .     .
  ]
]

scenario editor-wraps-line-on-insert-2 [
  local-scope
  # create an editor with some text
  assume-screen 10/width, 5/height
  s:text <- new [abcdefg
defg]
  e:&:editor <- new-editor s, 0/left, 5/right
  editor-render screen, e
  # type more text at the start
  assume-console [
    left-click 3, 0
    type [abc]
  ]
  run [
    editor-event-loop screen, console, e
    3:num/raw <- get *e, cursor-row:offset
    4:num/raw <- get *e, cursor-column:offset
  ]
  # cursor is not wrapped
  memory-should-contain [
    3 <- 3
    4 <- 3
  ]
  # but line is wrapped
  screen-should-contain [
    .          .
    .abcd     .
    .efg       .
    .abcd     .
    .efg       .
  ]
]

after <insert-character-special-case> [
  # if the line wraps at the cursor, move cursor to start of next row
  {
    # if either:
    # a) we're at the end of the line and at the column of the wrap indicator, or
    # b) we're not at end of line and just before the column of the wrap indicator
    wrap-column:num <- copy right
    before-wrap-column:num <- subtract wrap-column, 1
    at-wrap?:bool <- greater-or-equal cursor-column, wrap-column
    just-before-wrap?:bool <- greater-or-equal cursor-column, before-wrap-column
    next:&:duplex-list:char <- next before-cursor
    # at end of line? next == 0 || next.value == 10/newline
    at-end-of-line?:bool <- equal next, 0
    {
      break-if at-end-of-line?
      next-character:char <- get *next, value:offset
      at-end-of-line? <- equal next-character, 10/newline
    }
    # break unless ((eol? and at-wrap?) or (~eol? and just-before-wrap?))
    move-cursor-to-next-line?:bool <- copy 0/false
    {
      break-if at-end-of-line?
      move-cursor-to-next-line? <- copy just-before-wrap?
      # if we're moving the cursor because it's in the middle of a wrapping
      # line, adjust it to left-most column
      potential-new-cursor-column:num <- copy left
    }
    {
      break-unless at-end-of-line?
      move-cursor-to-next-line? <- copy at-wrap?
      # if we're moving the cursor because it's at the end of a wrapping line,
      # adjust it to one past the left-most column to make room for the
      # newly-inserted wrap-indicator
      potential-new-cursor-column:num <- add left, 1/make-room-for-wrap-indicator
    }
    break-unless move-cursor-to-next-line?
    cursor-column <- copy potential-new-cursor-column
    *editor <- put *editor, cursor-column:offset, cursor-column
    cursor-row <- add cursor-row, 1
    *editor <- put *editor, cursor-row:offset, cursor-row
    # if we're out of the screen, scroll down
    {
      below-screen?:bool <- greater-or-equal cursor-row, screen-height
      break-unless below-screen?
      <scroll-down>
    }
    return 1/go-render
  }
]

scenario editor-wraps-cursor-after-inserting-characters-in-middle-of-line [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abcde], 0/left, 5/right
  assume-console [
    left-click 1, 3  # right before the wrap icon
    type [f]
  ]
  run [
    editor-event-loop screen, console, e
    3:num/raw <- get *e, cursor-row:offset
    4:num/raw <- get *e, cursor-column:offset
  ]
  screen-should-contain [
    .          .
    .abcf     .
    .de        .
    .┈┈┈┈┈     .
    .          .
  ]
  memory-should-contain [
    3 <- 2  # cursor row
    4 <- 0  # cursor column
  ]
]

scenario editor-wraps-cursor-after-inserting-characters-at-end-of-line [
  local-scope
  assume-screen 10/width, 5/height
  # create an editor containing two lines
  s:text <- new [abc
xyz]
  e:&:editor <- new-editor s, 0/left, 5/right
  editor-render screen, e
  screen-should-contain [
    .          .
    .abc       .
    .xyz       .
    .┈┈┈┈┈     .
    .          .
  ]
  assume-console [
    left-click 1, 4  # at end of first line
    type [de]  # trigger wrap
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    .abcd     .
    .e         .
    .xyz       .
    .┈┈┈┈┈     .
  ]
]

scenario editor-wraps-cursor-to-left-margin [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abcde], 2/left, 7/right
  assume-console [
    left-click 1, 5  # line is full; no wrap icon yet
    type [01]
  ]
  run [
    editor-event-loop screen, console, e
    3:num/raw <- get *e, cursor-row:offset
    4:num/raw <- get *e, cursor-column:offset
  ]
  screen-should-contain [
    .          .
    .  abc0   .
    .  1de     .
    .  ┈┈┈┈┈   .
    .          .
  ]
  memory-should-contain [
    3 <- 2  # cursor row
    4 <- 3  # cursor column
  ]
]

# if newline, move cursor to start of next line, and maybe align indent with previous line

container editor [
  indent?:bool
]

after <editor-initialization> [
  *result <- put *result, indent?:offset, 1/true
]

scenario editor-moves-cursor-down-after-inserting-newline [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abc], 0/left, 10/right
  assume-console [
    type [0
1]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    .0         .
    .1abc      .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
]

after <handle-special-character> [
  {
    newline?:bool <- equal c, 10/newline
    break-unless newline?
    <begin-insert-enter>
    insert-new-line-and-indent editor, screen
    <end-insert-enter>
    return 1/go-render
  }
]

def insert-new-line-and-indent editor:&:editor, screen:&:screen -> editor:&:editor, screen:&:screen [
  local-scope
  load-inputs
  cursor-row:num <- get *editor, cursor-row:offset
  cursor-column:num <- get *editor, cursor-column:offset
  before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset
  left:num <- get *editor, left:offset
  right:num <- get *editor, right:offset
  screen-height:num <- screen-height screen
  # update cursor coordinates
  at-start-of-wrapped-line?:bool <- at-start-of-wrapped-line? editor
  {
    break-if at-start-of-wrapped-line?
    cursor-row <- add cursor-row, 1
    *editor <- put *editor, cursor-row:offset, cursor-row
  }
  cursor-column <- copy left
  *editor <- put *editor, cursor-column:offset, cursor-column
  # maybe scroll
  {
    below-screen?:bool <- greater-or-equal cursor-row, screen-height  # must be equal, never greater
    break-unless below-screen?
    <scroll-down>
    cursor-row <- subtract cursor-row, 1  # bring back into screen range
    *editor <- put *editor, cursor-row:offset, cursor-row
  }
  # insert newline
  insert 10/newline, before-cursor
  before-cursor <- next before-cursor
  *editor <- put *editor, before-cursor:offset, before-cursor
  # indent if necessary
  indent?:bool <- get *editor, indent?:offset
  return-unless indent?
  d:&:duplex-list:char <- get *editor, data:offset
  end-of-previous-line:&:duplex-list:char <- prev before-cursor
  indent:num <- line-indent end-of-previous-line, d
  i:num <- copy 0
  {
    indent-done?:bool <- greater-or-equal i, indent
    break-if indent-done?
    insert-at-cursor editor, 32/space, screen
    i <- add i, 1
    loop
  }
]

def at-start-of-wrapped-line? editor:&:editor -> result:bool [
  local-scope
  load-inputs
  left:num <- get *editor, left:offset
  cursor-column:num <- get *editor, cursor-column:offset
  cursor-at-left?:bool <- equal cursor-column, left
  return-unless cursor-at-left?, 0/false
  before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset
  before-before-cursor:&:duplex-list:char <- prev before-cursor
  return-unless before-before-cursor, 0/false  # cursor is at start of editor
  char-before-cursor:char <- get *before-cursor, value:offset
  cursor-after-newline?:bool <- equal char-before-cursor, 10/newline
  return-if cursor-after-newline?, 0/false
  # if cursor is at left margin and not at start, but previous character is not a newline,
  # then we're at start of a wrapped line
  return 1/true
]

# takes a pointer 'curr' into the doubly-linked list and its sentinel, counts
# the number of spaces at the start of the line containing 'curr'.
def line-indent curr:&:duplex-list:char, start:&:duplex-list:char -> result:num [
  local-scope
  load-inputs
  result:num <- copy 0
  return-unless curr
  at-start?:bool <- equal curr, start
  return-if at-start?
  {
    curr <- prev curr
    break-unless curr
    at-start?:bool <- equal curr, start
    break-if at-start?
    c:char <- get *curr, value:offset
    at-newline?:bool <- equal c, 10/newline
    break-if at-newline?
    # if c is a space, increment result
    is-space?:bool <- equal c, 32/space
    {
      break-unless is-space?
      result <- add result, 1
    }
    # if c is not a space, reset result
    {
      break-if is-space?
      result <- copy 0
    }
    loop
  }
]

scenario editor-moves-cursor-down-after-inserting-newline-2 [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abc], 1/left, 10/right
  assume-console [
    type [0
1]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    . 0        .
    . 1abc     .
    . ┈┈┈┈┈┈┈┈┈.
    .          .
  ]
]

scenario editor-clears-previous-line-completely-after-inserting-newline [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abcde], 0/left, 5/right
  editor-render screen, e
  screen-should-contain [
    .          .
    .abcd     .
    .e         .
    .┈┈┈┈┈     .
    .          .
  ]
  assume-console [
    press enter
  ]
  run [
    editor-event-loop screen, console, e
  ]
  # line should be fully cleared
  screen-should-contain [
    .          .
    .          .
    .abcd     .
    .e         .
    .┈┈┈┈┈     .
  ]
]

scenario editor-splits-wrapped-line-after-inserting-newline [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abcdef], 0/left, 5/right
  editor-render screen, e
  screen-should-contain [
    .          .
    .abcd     .
    .ef        .
    .┈┈┈┈┈     .
    .          .
  ]
  assume-console [
    left-click 2, 0
    press enter
  ]
  run [
    editor-event-loop screen, console, e
    10:num/raw <- get *e, cursor-row:offset
    11:num/raw <- get *e, cursor-column:offset
  ]
  screen-should-contain [
    .          .
    .abcd      .
    .ef        .
    .┈┈┈┈┈     .
  ]
  memory-should-contain [
    10 <- 2  # cursor-row
    11 <- 0  # cursor-column
  ]
]

scenario editor-inserts-indent-after-newline [
  local-scope
  assume-screen 10/width, 10/height
  s:text <- new [ab
  cd
ef]
  e:&:editor <- new-editor s, 0/left, 10/right
  # position cursor after 'cd' and hit 'newline'
  assume-console [
    left-click 2, 8
    type [
]
  ]
  run [
    editor-event-loop screen, console, e
    3:num/raw <- get *e, cursor-row:offset
    4:num/raw <- get *e, cursor-column:offset
  ]
  # cursor should be below start of previous line
  memory-should-contain [
    3 <- 3  # cursor row
    4 <- 2  # cursor column (indented)
  ]
]

scenario editor-skips-indent-around-paste [
  local-scope
  assume-screen 10/width, 10/height
  s:text <- new [ab
  cd
ef]
  e:&:editor <- new-editor s, 0/left, 10/right
  # position cursor after 'cd' and hit 'newline' surrounded by paste markers
  assume-console [
    left-click 2, 8
    press 65507  # start paste
    press enter
    press 65506  # end paste
  ]
  run [
    editor-event-loop screen, console, e
    3:num/raw <- get *e, cursor-row:offset
    4:num/raw <- get *e, cursor-column:offset
  ]
  # cursor should be below start of previous line
  memory-should-contain [
    3 <- 3  # cursor row
    4 <- 0  # cursor column (not indented)
  ]
]

after <handle-special-key> [
  {
    paste-start?:bool <- equal k, 65507/paste-start
    break-unless paste-start?
    *editor <- put *editor, indent?:offset, 0/false
    return 1/go-render
  }
]

after <handle-special-key> [
  {
    paste-end?:bool <- equal k, 65506/paste-end
    break-unless paste-end?
    *editor <- put *editor, indent?:offset, 1/true
    return 1/go-render
  }
]

## helpers

def draw-horizontal screen:&:screen, row:num, x:num, right:num -> screen:&:screen [
  local-scope
  load-inputs
  height:num <- screen-height screen
  past-bottom?:bool <- greater-or-equal row, height
  return-if past-bottom?
  style:char, style-found?:bool <- next-input
  {
    break-if style-found?
    style <- copy 9472/horizontal
  }
  color:num, color-found?:bool <- next-input
  {
    # default color to white
    break-if color-found?
    color <- copy 245/grey
  }
  bg-color:num, bg-color-found?:bool <- next-input
  {
    break-if bg-color-found?
    bg-color <- copy 0/black
  }
  screen <- move-cursor screen, row, x
  {
    continue?:bool <- lesser-or-equal x, right  # right is inclusive, to match editor semantics
    break-unless continue?
    print screen, style, color, bg-color
    x <- add x, 1
    loop
  }
]