all repos — minline @ 4668fd3c1bc99d7d519b4ee9091c5f364f67d995

A minimalist but highly-customizable line editing library.

minline.nim

 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
## This module provides a simple, limited but fully-functional line editing library written in pure Nim.
##
## To use this library, you must first initialize a **LineEditor** object using the **initEditor** method,
## and then use the **readLine** method to capture standard input instead of **stdout.readLine**:
##
## .. code-block:: nim
##    var ed = initEditor(historyFile = "history.txt")
##    while true:
##      let str = ed.readLine("-> ")
##      echo "You typed: ", str
##
## Optionally, you can also configure custom key bindings for keys and key sequences:
##
## .. code-block:: nim
##    KEYMAP["ctrl+k"] = proc(ed: var LineEditor) =
##      ed.clearLine()
##
## Additionally, you can also configure a **completionCallback** proc to trigger auto-completion by pressing TAB:
##
## .. code-block:: nim
##    ed.completionCallback = proc(ed: LineEditor): seq[string] =
##      return @["copy", "list", "delete", "move", "remove"]
##
## **Note** When compared to the readline or linenoise libraries, this module has the following limitations:
##
## * It is only possible to edit one line of text at a time. When using the **readLine** method, it will not be possible to physically go to the next line (this simplifies things a bit...).
## * No UTF8 support, only ASCII characters are supported.
## * No support for colorized output.
## * Only limited support for Emacs keybindings, no support for Vi mode and Vi keybindings.

import
  critbits,
  terminal,
  deques,
  sequtils,
  strutils,
  std/exitprocs,
  os

if isatty(stdin):
  addExitProc(resetAttributes)

when defined(windows):
  proc putchr*(c: cint): cint {.discardable, header: "<conio.h>",
      importc: "_putch".}
    ## Prints an ASCII character to stdout.
  proc getchr*(): cint {.header: "<conio.h>", importc: "_getch".}
    ## Retrieves an ASCII character from stdin.
else:
  proc putchr*(c: cint) {.header: "stdio.h", importc: "putchar".} =
    ## Prints an ASCII character to stdout.
    stdout.write(c.chr)
    stdout.flushFile()

  proc getchr*(): cint =
    ## Retrieves an ASCII character from stdin.
    stdout.flushFile()
    return getch().ord.cint

# Types

type
  Key* = int ## The ASCII code of a keyboard key.
  KeySeq* = seq[Key] ## A sequence of one or more Keys.
  KeyCallback* = proc(ed: var Editor) {.closure,
      gcsafe.} ## A proc that can be bound to a key or a key sequence to access line editing functionalities.


  ### TO REMOVE
  LineError* = ref Exception ## A generic nimline error.
  LineEditorError* = ref Exception ## An error occured in the LineEditor.
  LineEditorMode* = enum ## The *mode* a LineEditor operates in (insert or replace).
    mdInsert
    mdReplace
  Line* = object ## An object representing a line of text.
    text: string
    position: int
  LineHistory* = object ## An object representing the history of all commands typed in a LineEditor.
    file: string
    tainted: bool
    position: int
    queue: Deque[string]
    max: int
  LineEditor* = object ## An object representing a line editor, used to process text typed in the terminal.
    completionCallback*: proc(ed: LineEditor): seq[string] {.closure, gcsafe.}
    history: LineHistory
    line: Line
    mode: LineEditorMode
  ###

  MinlineError* = ref Exception ## A generic nimline error.
  EditorError* = ref Exception ## An error occured in the Editor.
  EditorMode* = enum ## The *mode* a Editor operates in (insert or replace).
    modeInsert ## Insert mode.
    modeReplace ## Replace mode.
  Entry* = object ## An object representing text entered in a prompt, potentially including multiple lines.
    text: string ## The full text of the entry, including newlines.
    offset: int ## The number of characters reserved for the line prompt.
    position: int ## The current position of the cursor within the entry text.
  Editor* = object ## An object representing a command line editor, used to process text typed in the terminal.
    completionCallback*: proc(ed: Editor): seq[string] {.closure, gcsafe.} ## Callback executed when completion key is pressed (e.g. TAB)
    newLineCallback*: proc(ed: var Editor, prompt: string, c: int): string {.closure, gcsafe.}
    prompt: string ## Editor prompt
    history: History ## Editor history
    index: int ## Current history index
    entry: Entry ## Current entry
    mode: EditorMode ## Editor more
  History* = object ## An object representing the history of all entries typed in an Editor.
    file: string ## Path to a file containing the editor history
    tainted: bool
    position: int
    queue: Deque[Entry]
    max: int

# Internal Methods

proc bol(ed: var Editor): int =
  ## Returns the beginning of line index.
  return 0

proc eol(ed: var Editor): int =
  ## Returns the end of line index based on terminal width.
  return terminalWidth() - ed.prompt.len

proc boh(ed: var Editor): int =
  ## Return the beginning of history index.
  return 0

proc eoh(ed: var Editor): int =
  ## Returns the end of history index.
  return ed.history.queue.len-1

proc boe(ed: var Editor): int =
  return 0

proc eoe(en: Entry): int

proc eoe(ed: var Editor): int =
  return ed.entry.eoe()

proc lines(en: Entry): seq[string] =
  ## Returns a sequence of all lines in an entry.
  return en.text.split("\n")

proc index(en: Entry): int =
  ## Returns the index to the current line, based on cursor position
  result = 0
  var cursor = en.position
  for line in en.lines:
    if cursor <= line.len:
      return
    cursor.dec(line.len)

proc line(en: Entry): string =
  return en.lines[en.index]

proc linepos(en: Entry): int =
  result = en.position
  for line in en.lines:
    if result <= line.len:
      return
    result.dec(line.len)

proc empty(en: Entry): bool =
  return en.text.len == 0

proc position(ed: var Editor): int = 
  return ed.entry.position

proc `position=`(ed: var Editor, value: int) =
  ed.position = value

proc boe(en: Entry): int =
  return 0

proc eoe(en: Entry): int =
  return en.line.len-1

proc changeLine*(ed: var Editor, entry: Entry)

proc add(h: var History, entry: Entry, force = false) 

proc full(entry: Entry): bool =
  return entry.position >= entry.text.len

# Reviewed
proc back*(ed: var Editor, n = 1) =
  ## Move the cursor back by **n** characters on the current line (unless the beginning of the line is reached).
  if ed.entry.position <= 0:
    return
  stdout.cursorBackward(n)
  ed.entry.position = ed.entry.position - n

# Reviewed
proc forward*(ed: var Editor, n = 1) =
  ## Move the cursor forward by **n** characters on the current line (unless the beginning of the line is reached).
  if ed.entry.full:
    return
  stdout.cursorForward(n)
  ed.entry.position += n

# Arrow Keys

proc up*(ed: var Editor) =
  if ed.entry.index <= ed.boe:
    # beginning of entry
    if ed.index <= ed.boh:
      # beginning of history
      return
    ed.history.add(ed.entry)
    ed.index -= 1
    ed.changeLine(ed.history.queue[ed.index])
    return
  let prevline = ed.entry.lines[ed.entry.index]
  if ed.entry.linepos <= prevline.len:
    # previous wline is shorter than current line
    ed.back(ed.entry.linepos)
    return
  ed.back(prevline.len)

proc down*(ed: var Editor) =
  if ed.entry.index >= ed.eoe:
    if ed.index >= ed.eoh:
      return
    ed.index += 1
    ed.changeLine(ed.history.queue[ed.index])
    return
  let nextline = ed.entry.lines[ed.entry.index+1]
  if ed.entry.linepos >= nextline.len:
    # current wline is longer than next line
    ed.forward(ed.entry.line.len - ed.entry.linepos + nextline.len)
    return
  ed.forward(ed.entry.line.len)

proc left*(ed: var Editor) =
  if ed.entry.linepos <= ed.bol:
    if ed.entry.index <= ed.boe:
      return
    let prevPos = ed.position
    ed.back(prevPos)
    return
  ed.back()

proc right*(ed: var Editor) = 
  if ed.entry.linepos >= ed.eol:
    if ed.entry.index >= ed.eoe:
      return
    let prevPos = ed.position
    ed.forward(prevPos)
    return
  ed.forward()

proc initEntry*(text = "", offset = 0, position = 0): Entry =
  result.text = text
  result.offset = offset
  result.position = position

proc `[]`(q: Deque[Entry], pos: int): Entry =
  var c = 0
  for e in q.items:
    if c == pos:
      result = e
      break
    c.inc

proc `[]=`(q: var Deque[Entry], pos: int, entry: Entry) =
  var c = 0
  for e in q.mitems:
    if c == pos:
      e = entry
      break
    c.inc

proc add(h: var History, entry: Entry, force = false) =
  if entry.text.len == 0 and not force:
    return
  if h.queue.len >= h.max:
    discard h.queue.popFirst
  if h.tainted:
    h.queue[h.queue.len-1] = entry
  else:
    h.queue.addLast entry

proc toEnd(entry: Entry): string =
  if entry.empty:
    return ""
  return entry.text[entry.position..entry.eoe]

proc historyAdd*(ed: var Editor, force = false) =
  ## Adds the current editor line to the history. If **force** is set to **true**, the line will be added even if it's blank.
  ed.history.add ed.entry, force
  if ed.history.file == "":
    return
  ed.history.file.writeFile(toSeq(ed.history.queue.items).join("\f"))

proc historyFlush*(ed: var Editor) =
  ## If there is at least one entry in the history, it sets the position of the cursor to the last element and sets the **tainted** flag to **false**.
  if ed.history.queue.len > 0:
    ed.history.position = ed.history.queue.len
    ed.history.tainted = false

# TO REVIEW:

# Reviewed
proc empty(line: Line): bool =
  return line.text.len <= 0

# Reviewed
proc full(line: Line): bool =
  return line.position >= line.text.len

proc first(line: Line): int =
  if line.empty:
    raise LineError(msg: "Line is empty!")
  return 0

proc last(line: Line): int =
  if line.empty:
    raise LineError(msg: "Line is empty!")
  return line.text.len-1

proc fromStart(line: Line): string =
  if line.empty:
    return ""
  return line.text[line.first..line.position-1]

proc toEnd(line: Line): string =
  if line.empty:
    return ""
  return line.text[line.position..line.last]

# Reviewed
proc back*(ed: var LineEditor, n = 1) =
  ## Move the cursor back by **n** characters on the current line (unless the beginning of the line is reached).
  if ed.line.position <= 0:
    return
  stdout.cursorBackward(n)
  ed.line.position = ed.line.position - n

# Reviewed
proc forward*(ed: var LineEditor, n = 1) =
  ## Move the cursor forward by **n** characters on the current line (unless the beginning of the line is reached).
  if ed.line.full:
    return
  stdout.cursorForward(n)
  ed.line.position += n

# Reviewed
proc `[]`(q: Deque[string], pos: int): string =
  var c = 0
  for e in q.items:
    if c == pos:
      result = e
      break
    c.inc

# Reviewed
proc `[]=`(q: var Deque[string], pos: int, s: string) =
  var c = 0
  for e in q.mitems:
    if c == pos:
      e = s
      break
    c.inc

# Reviewed (add)
proc add(h: var LineHistory, s: string, force = false) =
  if s == "" and not force:
    return
  if h.queue.len >= h.max:
    discard h.queue.popFirst
  if h.tainted:
    h.queue[h.queue.len-1] = s
  else:
    h.queue.addLast s

# Reviewed (up)
proc previous(h: var LineHistory): string =
  if h.queue.len == 0 or h.position <= 0:
    return ""
  h.position.dec
  result = h.queue[h.position]

# Reviewed (down)
proc next(h: var LineHistory): string =
  if h.queue.len == 0 or h.position >= h.queue.len-1:
    return ""
  h.position.inc
  result = h.queue[h.position]

# Public API

proc deletePrevious*(ed: var LineEditor) =
  ## Move the cursor to the left by one character (unless at the beginning of the line) and delete the existing character, if any.
  if ed.line.position <= 0:
    return
  if not ed.line.empty:
    if ed.line.full:
      stdout.cursorBackward
      putchr(32)
      stdout.cursorBackward
      ed.line.position.dec
      ed.line.text = ed.line.text[0..ed.line.last-1]
    else:
      let rest = ed.line.toEnd & " "
      ed.back
      for i in rest:
        putchr i.ord.cint
      ed.line.text = ed.line.fromStart & ed.line.text[
          ed.line.position+1..ed.line.last]
      stdout.cursorBackward(rest.len)

proc deleteNext*(ed: var LineEditor) =
  ## Move the cursor to the right by one character (unless at the end of the line) and delete the existing character, if any.
  if not ed.line.empty:
    if not ed.line.full:
      let rest = ed.line.toEnd[1..^1] & " "
      for c in rest:
        putchr c.ord.cint
      stdout.cursorBackward(rest.len)
      ed.line.text = ed.line.fromStart & ed.line.toEnd[1..^1]

# Reviewed
proc printChar*(ed: var LineEditor, c: int) =
  ## Prints the character **c** to the current line. If in the middle of the line, the following characters are shifted right or replaced depending on the editor mode.
  if ed.line.full:
    putchr(c.cint)
    ed.line.text &= c.chr
    ed.line.position += 1
  else:
    if ed.mode == mdInsert:
      putchr(c.cint)
      let rest = ed.line.toEnd
      ed.line.text.insert($c.chr, ed.line.position)
      ed.line.position += 1
      for j in rest:
        putchr(j.ord.cint)
        ed.line.position += 1
      ed.back(rest.len)
    else:
      putchr(c.cint)
      ed.line.text[ed.line.position] = c.chr
      ed.line.position += 1

# Reviewed
proc changeLine*(ed: var LineEditor, s: string) =
  ## Replaces the contents of the current line with the string **s**.
  let text = ed.line.text
  let diff = text.len - s.len
  let position = ed.line.position
  if position > 0:
    stdout.cursorBackward(position)
  for c in s:
    putchr(c.ord.cint)
  ed.line.position = s.len
  ed.line.text = s
  if diff > 0:
    for i in 0.countup(diff-1):
      putchr(32)
    stdout.cursorBackward(diff)

proc addToLineAtPosition(ed: var LineEditor, s: string) =
  for c in s:
    ed.printChar(c.ord.cint)

proc clearLine*(ed: var LineEditor) =
  ## Clears the contents of the current line and reset the cursor position to the beginning of the line.
  stdout.cursorBackward(ed.line.position+1)
  for i in ed.line.text:
    putchr(32)
  putchr(32)
  putchr(32)
  stdout.cursorBackward(ed.line.text.len+1)
  ed.line.position = 0
  ed.line.text = ""

proc goToStart*(ed: var LineEditor) =
  ## Move the cursor to the beginning of the line.
  if ed.line.position <= 0:
    return
  try:
    stdout.cursorBackward(ed.line.position)
    ed.line.position = 0
  except:
    discard

proc goToEnd*(ed: var LineEditor) =
  ## Move the cursor to the end of the line.
  if ed.line.full:
    return
  let diff = ed.line.text.len - ed.line.position
  stdout.cursorForward(diff)
  ed.line.position = ed.line.text.len

# Reviewed (initHistory)
proc historyInit*(size = 256, file: string = ""): LineHistory =
  ## Creates a new **LineHistory** object with the specified **size** and **file**.
  result.file = file
  result.queue = initDeque[string](size)
  result.position = 0
  result.tainted = false
  result.max = size
  if file == "":
    return
  if result.file.fileExists:
    let lines = result.file.readFile.split("\n")
    for line in lines:
      if line != "":
        result.add line
    result.position = lines.len
  else:
    result.file.writeFile("")

# Reviewed
proc historyAdd*(ed: var LineEditor, force = false) =
  ## Adds the current editor line to the history. If **force** is set to **true**, the line will be added even if it's blank.
  ed.history.add ed.line.text, force
  if ed.history.file == "":
    return
  ed.history.file.writeFile(toSeq(ed.history.queue.items).join("\n"))

# Reviewed (up)
proc historyPrevious*(ed: var LineEditor) =
  ## Replaces the contents of the current line with the previous line stored in the history (if any).
  ## The current line will be added to the history and the hisory will be marked as *tainted*.
  let s = ed.history.previous
  if s == "":
    return
  let pos = ed.history.position
  var current: int
  if ed.history.tainted:
    current = ed.history.queue.len-2
  else:
    current = ed.history.queue.len-1
  if pos == current and ed.history.queue[current] != ed.line.text:
    ed.historyAdd(force = true)
    ed.history.tainted = true
  if s != "":
    ed.changeLine(s)

# Reviewed (down)
proc historyNext*(ed: var LineEditor) =
  ## Replaces the contents of the current line with the following line stored in the history (if any).
  let s = ed.history.next
  if s == "":
    return
  ed.changeLine(s)

proc historyFlush*(ed: var LineEditor) =
  ## If there is at least one entry in the history, it sets the position of the cursor to the last element and sets the **tainted** flag to **false**.
  if ed.history.queue.len > 0:
    ed.history.position = ed.history.queue.len
    ed.history.tainted = false

proc completeLine*(ed: var LineEditor): int =
  ## If a **completionCallback** proc has been specified for the current editor, attempts to auto-complete the current line by running **completionProc**
  ## to return a list of possible values. It is possible to cycle through the matches by pressing the same key that triggered this proc.
  ##
  ## The matches provided will be filtered based on the contents of the line when this proc was first triggered. If a match starts with the contents of the line, it
  ## will be displayed.
  ##
  ## The following is a real-world example of a **completionCallback** used to complete the last word on the line with valid file paths.
  ##
  ## .. code-block:: nim
  ##   import sequtils, strutils, ospath
  ##
  ##   ed.completionCallback = proc(ed: LineEditor): seq[string] =
  ##     var words = ed.lineText.split(" ")
  ##     var word: string
  ##     if words.len == 0:
  ##       word = ed.lineText
  ##     else:
  ##       word = words[words.len-1]
  ##     var f = word[1..^1]
  ##     if f == "":
  ##       f = getCurrentDir().replace("\\", "/")
  ##       return toSeq(walkDir(f, true))
  ##         .mapIt("\"$1" % it.path.replace("\\", "/"))
  ##     elif f.dirExists:
  ##       f = f.replace("\\", "/")
  ##       if f[f.len-1] != '/':
  ##         f = f & "/"
  ##       return toSeq(walkDir(f, true))
  ##         .mapIt("\"$1$2" % [f, it.path.replace("\\", "/")])
  ##     else:
  ##       var dir: string
  ##       if f.contains("/") or dir.contains("\\"):
  ##         dir = f.parentDir
  ##         let file = f.extractFileName
  ##         return toSeq(walkDir(dir, true))
  ##           .filterIt(it.path.toLowerAscii.startsWith(file.toLowerAscii))
  ##           .mapIt("\"$1/$2" % [dir, it.path.replace("\\", "/")])
  ##       else:
  ##         dir = getCurrentDir()
  ##         return toSeq(walkDir(dir, true))
  ##           .filterIt(it.path.toLowerAscii.startsWith(f.toLowerAscii))
  ##           .mapIt("\"$1" % [it.path.replace("\\", "/")])
  ##
  if ed.completionCallback.isNil:
    return
  let compl = ed.completionCallback(ed)
  let position = ed.line.position
  let words = ed.line.fromStart.split(" ")
  var word: string
  if words.len > 0:
    word = words[words.len-1]
  else:
    word = ed.line.fromStart
  var matches = compl.filterIt(it.toLowerAscii.startsWith(word.toLowerAscii))
  if ed.line.fromStart.len > 0 and matches.len > 0:
    for i in 0..word.len-1:
      ed.deletePrevious
  var n = 0
  if matches.len > 0:
    ed.addToLineAtPosition(matches[0])
  else:
    return -1
  var ch = getchr()
  while ch == 9:
    n.inc
    if n < matches.len:
      let diff = ed.line.position - position
      for i in 0.countup(diff-1 + word.len):
        ed.deletePrevious
      ed.addToLineAtPosition(matches[n])
      ch = getchr()
    else:
      n = -1
  return ch

proc lineText*(ed: LineEditor): string =
  ## Returns the contents of the current line.
  return ed.line.text

# Reviewed (initEditor)
proc initLineEditor*(mode = mdInsert, historySize = 256,
    historyFile: string = ""): LineEditor =
  ## Creates a **LineEditor** object.
  result.mode = mode
  result.history = historyInit(historySize, historyFile)

# Character sets
const
  CTRL* = {0 .. 31}          ## Control characters.
  DIGIT* = {48 .. 57}        ## Digits.
  LETTER* = {65 .. 122}      ## Letters.
  UPPERLETTER* = {65 .. 90}  ## Uppercase letters.
  LOWERLETTER* = {97 .. 122} ## Lowercase letters.
  PRINTABLE* = {32 .. 126}   ## Printable characters.
when defined(windows):
  const
    ESCAPES* = {0, 22, 224} ## Escape characters.
else:
  const
    ESCAPES* = {27} ## Escape characters.

# Key Names
var KEYNAMES* {.threadvar.}: array[0..31,
    string] ## The following strings can be used in keymaps instead of the correspinding ASCII codes:
 ##
 ## .. code-block:: nim
 ##    KEYNAMES[1]    =    "ctrl+a"
 ##    KEYNAMES[2]    =    "ctrl+b"
 ##    KEYNAMES[3]    =    "ctrl+c"
 ##    KEYNAMES[4]    =    "ctrl+d"
 ##    KEYNAMES[5]    =    "ctrl+e"
 ##    KEYNAMES[6]    =    "ctrl+f"
 ##    KEYNAMES[7]    =    "ctrl+g"
 ##    KEYNAMES[8]    =    "ctrl+h"
 ##    KEYNAMES[9]    =    "ctrl+i"
 ##    KEYNAMES[9]    =    "tab"
 ##    KEYNAMES[10]   =    "ctrl+j"
 ##    KEYNAMES[11]   =    "ctrl+k"
 ##    KEYNAMES[12]   =    "ctrl+l"
 ##    KEYNAMES[13]   =    "ctrl+m"
 ##    KEYNAMES[14]   =    "ctrl+n"
 ##    KEYNAMES[15]   =    "ctrl+o"
 ##    KEYNAMES[16]   =    "ctrl+p"
 ##    KEYNAMES[17]   =    "ctrl+q"
 ##    KEYNAMES[18]   =    "ctrl+r"
 ##    KEYNAMES[19]   =    "ctrl+s"
 ##    KEYNAMES[20]   =    "ctrl+t"
 ##    KEYNAMES[21]   =    "ctrl+u"
 ##    KEYNAMES[22]   =    "ctrl+v"
 ##    KEYNAMES[23]   =    "ctrl+w"
 ##    KEYNAMES[24]   =    "ctrl+x"
 ##    KEYNAMES[25]   =    "ctrl+y"
 ##    KEYNAMES[26]   =    "ctrl+z"

KEYNAMES[1] = "ctrl+a"
KEYNAMES[2] = "ctrl+b"
KEYNAMES[3] = "ctrl+c"
KEYNAMES[4] = "ctrl+d"
KEYNAMES[5] = "ctrl+e"
KEYNAMES[6] = "ctrl+f"
KEYNAMES[7] = "ctrl+g"
KEYNAMES[8] = "ctrl+h"
KEYNAMES[9] = "ctrl+i"
KEYNAMES[9] = "tab"
KEYNAMES[10] = "ctrl+j"
KEYNAMES[11] = "ctrl+k"
KEYNAMES[12] = "ctrl+l"
KEYNAMES[13] = "ctrl+m"
KEYNAMES[14] = "ctrl+n"
KEYNAMES[15] = "ctrl+o"
KEYNAMES[16] = "ctrl+p"
KEYNAMES[17] = "ctrl+q"
KEYNAMES[18] = "ctrl+r"
KEYNAMES[19] = "ctrl+s"
KEYNAMES[20] = "ctrl+t"
KEYNAMES[21] = "ctrl+u"
KEYNAMES[22] = "ctrl+v"
KEYNAMES[23] = "ctrl+w"
KEYNAMES[24] = "ctrl+x"
KEYNAMES[25] = "ctrl+y"
KEYNAMES[26] = "ctrl+z"

# Key Sequences
var KEYSEQS* {.threadvar.}: CritBitTree[
    KeySeq] ## The following key sequences are defined and are used internally by **LineEditor**:
 ##
 ## .. code-block:: nim
 ##    KEYSEQS["up"]         = @[27, 91, 65]      # Windows: @[224, 72]
 ##    KEYSEQS["down"]       = @[27, 91, 66]      # Windows: @[224, 80]
 ##    KEYSEQS["right"]      = @[27, 91, 67]      # Windows: @[224, 77]
 ##    KEYSEQS["left"]       = @[27, 91, 68]      # Windows: @[224, 75]
 ##    KEYSEQS["home"]       = @[27, 91, 72]      # Windows: @[224, 71]
 ##    KEYSEQS["end"]        = @[27, 91, 70]      # Windows: @[224, 79]
 ##    KEYSEQS["insert"]     = @[27, 91, 50, 126] # Windows: @[224, 82]
 ##    KEYSEQS["delete"]     = @[27, 91, 51, 126] # Windows: @[224, 83]

when defined(windows):
  KEYSEQS["up"] = @[224, 72]
  KEYSEQS["down"] = @[224, 80]
  KEYSEQS["right"] = @[224, 77]
  KEYSEQS["left"] = @[224, 75]
  KEYSEQS["home"] = @[224, 71]
  KEYSEQS["end"] = @[224, 79]
  KEYSEQS["insert"] = @[224, 82]
  KEYSEQS["delete"] = @[224, 83]
else:
  KEYSEQS["up"] = @[27, 91, 65]
  KEYSEQS["down"] = @[27, 91, 66]
  KEYSEQS["right"] = @[27, 91, 67]
  KEYSEQS["left"] = @[27, 91, 68]
  KEYSEQS["home"] = @[27, 91, 72]
  KEYSEQS["end"] = @[27, 91, 70]
  KEYSEQS["insert"] = @[27, 91, 50, 126]
  KEYSEQS["delete"] = @[27, 91, 51, 126]

# Key Mappings
var KEYMAP* {.threadvar.}: CritBitTree[KeyCallBack] ## The following key mappings are configured by default:
 ##
 ## * backspace: **deletePrevious**
 ## * delete: **deleteNext**
 ## * insert: *toggle editor mode*
 ## * down: **historyNext**
 ## * up: **historyPrevious**
 ## * ctrl+n: **historyNext**
 ## * ctrl+p: **historyPrevious**
 ## * left: **back**
 ## * right: **forward**
 ## * ctrl+b: **back**
 ## * ctrl+f: **forward**
 ## * ctrl+c: *quits the program*
 ## * ctrl+d: *quits the program*
 ## * ctrl+u: **clearLine**
 ## * ctrl+a: **goToStart**
 ## * ctrl+e: **goToEnd**
 ## * home: **goToStart**
 ## * end: **goToEnd**

# KEYMAP["backspace"] = proc(ed: var Editor) {.gcsafe.} =
#   ed.deletePrevious()
# KEYMAP["delete"] = proc(ed: var Editor) {.gcsafe.} =
#   ed.deleteNext()
KEYMAP["insert"] = proc(ed: var Editor) {.gcsafe.} =
  if ed.mode == modeInsert:
    ed.mode = modeReplace
  else:
    ed.mode = modeInsert
KEYMAP["down"] = proc(ed: var Editor) {.gcsafe.} =
  ed.down()
KEYMAP["up"] = proc(ed: var Editor) {.gcsafe.} =
  ed.up()
# KEYMAP["ctrl+n"] = proc(ed: var Editor) {.gcsafe.} =
#   ed.historyNext()
# KEYMAP["ctrl+p"] = proc(ed: var Editor) {.gcsafe.} =
#   ed.historyPrevious()
KEYMAP["left"] = proc(ed: var Editor) {.gcsafe.} =
  ed.back()
KEYMAP["right"] = proc(ed: var Editor) {.gcsafe.} =
  ed.forward()
KEYMAP["ctrl+b"] = proc(ed: var Editor) {.gcsafe.} =
  ed.back()
KEYMAP["ctrl+f"] = proc(ed: var Editor) {.gcsafe.} =
  ed.forward()
KEYMAP["ctrl+c"] = proc(ed: var Editor) {.gcsafe.} =
  quit(0)
KEYMAP["ctrl+d"] = proc(ed: var Editor) {.gcsafe.} =
  quit(0)
# KEYMAP["ctrl+u"] = proc(ed: var Editor) {.gcsafe.} =
#   ed.clearLine()
# KEYMAP["ctrl+a"] = proc(ed: var Editor) {.gcsafe.} =
#   ed.goToStart()
# KEYMAP["ctrl+e"] = proc(ed: var Editor) {.gcsafe.} =
#   ed.goToEnd()
# KEYMAP["home"] = proc(ed: var Editor) {.gcsafe.} =
#   ed.goToStart()
# KEYMAP["end"] = proc(ed: var Editor) {.gcsafe.} =
#   ed.goToEnd()

var keyMapProc {.threadvar.}: proc(ed: var Editor) {.gcsafe.}

# ---------------- NEW METHODS ---------------- #

proc initHistory*(size = 256, file: string = ""): History =
  ## Creates a new **History** object with the specified **size** and **file**.
  result.file = file
  result.queue = initDeque[Entry](size)
  result.position = 0
  result.tainted = false
  result.max = size
  if file == "":
    return
  if result.file.fileExists:
    let lines = result.file.readFile.split("\f")
    for line in lines:
      if line != "":
        result.add initEntry(line)
    result.position = lines.len
  else:
    result.file.writeFile("")

proc initEditor*(mode = modeInsert, historySize = 256, historyFile: string = ""): Editor =
  ## Creates a **Editor** object.
  result.mode = mode
  result.index = 0
  result.entry = initEntry()
  result.history = initHistory(historySize, historyFile)

proc changeLine*(ed: var Editor, entry: Entry) =
  ## Replaces the contents of the current line with the string **s**.
  let text = ed.entry.text
  let diff = text.len - entry.text.len
  let position = ed.position
  if position > 0:
    stdout.cursorBackward(position)
  for c in entry.text:
    putchr(c.ord.cint)
  ed.position = entry.text.len
  ed.entry.text = entry.text
  if diff > 0:
    for i in 0.countup(diff-1):
      putchr(32)
    stdout.cursorBackward(diff)

proc printChar*(ed: var Editor, c: int) =
  ## Prints the character **c** to the current line. If in the middle of the line, the following characters are shifted right or replaced depending on the editor mode.
  if ed.entry.full:
    putchr(c.cint)
    ed.entry.text &= c.chr
    ed.entry.position += 1
  else:
    if ed.mode == modeInsert:
      putchr(c.cint)
      let rest = ed.entry.toEnd
      ed.entry.text.insert($c.chr, ed.entry.position)
      ed.entry.position += 1
      for j in rest:
        putchr(j.ord.cint)
        ed.entry.position += 1
      ed.back(rest.len)
    else:
      putchr(c.cint)
      ed.entry.text[ed.entry.position] = c.chr
      ed.entry.position += 1

proc readLine*(ed: var Editor, prompt = "", hidechars = false): string {.gcsafe.} =
  ## High-level proc to be used instead of **stdin.readLine** to read a line from standard input using the specified **LineEditor** object.
  ##
  ## Note that:
  ## * **prompt** is a string (that *cannot* contain escape codes, so it cannot be colored) that will be prepended at the start of the line and
  ##   not included in the contents of the line itself.
  ## * If **hidechars** is set to **true**, asterisks will be printed to stdout instead of the characters entered by the user.
  stdout.write(prompt)
  stdout.flushFile()
  var c = -1 # Used to manage completions
  var esc = false
  while true:
    var c1: int
    if c > 0:
      c1 = c
      c = -1
    else:
      c1 = getchr()
    if esc:
      esc = false
      continue
    elif c1 in {10, 13}:
      if not ed.newLineCallback.isNil:
        let line = ed.newLineCallback(ed, prompt, c1)
        ed.entry.text &= c1.chr
        ed.entry.position.inc
        if line != "":
          return line
      else:
        ed.historyAdd()
        ed.historyFlush()
        let text = ed.entry.text
        ed.entry = initEntry()
        return text
    # TODO
    #elif c1 in {8, 127}:
    #  KEYMAP["backspace"](ed)
    elif c1 in PRINTABLE:
      if hidechars:
        putchr('*'.ord.cint)
        ed.entry.text &= c1.chr
        ed.entry.position.inc
      else:
        ed.printChar(c1)
    # TODO
    #elif c1 == 9: # TAB
    #  c = ed.completeLine()
    elif c1 in ESCAPES:
      var s = newSeq[Key](0)
      s.add(c1)
      let c2 = getchr()
      s.add(c2)
      if s == KEYSEQS["left"]:
        KEYMAP["left"](ed)
      elif s == KEYSEQS["right"]:
        KEYMAP["right"](ed)
      elif s == KEYSEQS["up"]:
        KEYMAP["up"](ed)
      elif s == KEYSEQS["down"]:
        KEYMAP["down"](ed)
      #elif s == KEYSEQS["home"]:
      #  KEYMAP["home"](ed)
      #elif s == KEYSEQS["end"]:
      #  KEYMAP["end"](ed)
      #elif s == KEYSEQS["delete"]:
      #  KEYMAP["delete"](ed)
      #elif s == KEYSEQS["insert"]:
      #  KEYMAP["insert"](ed)
      elif c2 == 91:
        let c3 = getchr()
        s.add(c3)
        if s == KEYSEQS["right"]:
          KEYMAP["right"](ed)
        elif s == KEYSEQS["left"]:
          KEYMAP["left"](ed)
        elif s == KEYSEQS["up"]:
          KEYMAP["up"](ed)
        elif s == KEYSEQS["down"]:
          KEYMAP["down"](ed)
        #elif s == KEYSEQS["home"]:
        #  KEYMAP["home"](ed)
        #elif s == KEYSEQS["end"]:
        #  KEYMAP["end"](ed)
        elif c3 in {50, 51}:
          let c4 = getchr()
          s.add(c4)
          #if c4 == 126 and c3 == 50:
          #  KEYMAP["insert"](ed)
          #elif c4 == 126 and c3 == 51:
          #  KEYMAP["delete"](ed)
    elif c1 in CTRL and KEYMAP.hasKey(KEYNAMES[c1]):
      keyMapProc = KEYMAP[KEYNAMES[c1]]
      keyMapProc(ed)
    else:
      # Assuming unhandled two-values escape sequence; do nothing.
      if esc:
        esc = false
        continue
      else:
        esc = true
        continue

proc password*(ed: var Editor, prompt = ""): string =
  ## Convenience method to use instead of **readLine** to hide the characters inputed by the user.
  return ed.readLine(prompt, true)

when isMainModule:
  #proc testChar() =
  #  while true:
  #    let a = getchr()
  #    echo "\n->", a
  #    if a == 3:
  #      quit(0)
  #
  #testChar()

  type InfoError = object of MinlineError

  KEYMAP["ctrl+o"] = proc(ed: var Editor) {.gcsafe.} =
    #echo "\nindex: $1 pos: $2 row: $3 col: $4\ntext: $5 " % [$ed.index, $ed.position, $ed.entry.index, $ed.entry.linepos, $ed.entry.text]
    #echo "\n\n\n\n---" & $ed.entry.lines.len & "---"
    raise newException(InfoError, "")
  proc testLineEditor() =
    var ed = initEditor(historyFile = "")
    ed.newLineCallback = proc(ed: var Editor, prompt: string, c: int): string =
      let s = " ".repeat(prompt.len)
      let lpar = ed.entry.text.count("(")
      let rpar = ed.entry.text.count(")")
      if (lpar != rpar):
        stdout.write("\n"&s)
        return ""
      else:
        # TODO: Not working, not printing full text when 
        stdout.flushFile()
        ed.historyAdd()
        ed.historyFlush()
        let text = ed.entry.text
        ed.entry = initEntry()
        return text
    while true:
      try:
        echo "\n=> ----", ed.readLine("-> "), "----"
      except InfoError:
        discard

  testLineEditor()