all repos — min @ ea9b697606d17ed5c850e805be78e21362a157f7

A small but practical concatenative programming language.

core/line.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
import
  critbits,
  terminal,
  queues

# getch/putch implementations
when defined(windows):
   proc getchar(): cint {.header: "<conio.h>", importc: "_getch".}
   proc putchar(c: cint): cint {.discardable, header: "<conio.h>", importc: "_putch".}

   proc termSetup*() = 
     discard

   proc termSave*(): string = 
     return ""

   proc termRestore*(c: string) =
     discard
else:
  import osproc

  proc termSetup*() =
    discard execCmd "stty </dev/tty -icanon -echo -isig -iexten"

  proc termSave*(): string =
    let res = execCmdEx "stty </dev/tty -g"
    return res[0]

  proc termRestore*(c: string) =
    discard execCmd "stty </dev/tty " & c

  proc getchar(): cint =
    return stdin.readChar().ord.cint

  proc putchar(c: cint) =
    stdout.write(c.chr)


# Types

type
  Key* = int
  KeySeq* = seq[Key]
  LineError* = ref Exception
  LineEditorMode* = enum
    mdInsert
    mdReplace
  Line* = object
    text*: string
    position*: int
  KeyCallback* = proc(ed: var LineEditor)
  LineHistory* = object
    position*: int
    queue*: Queue[string]
    max*: int
  LineEditor* = object
    history: LineHistory
    line*: Line
    mode*: LineEditorMode

proc len*(line: Line): int =
  return line.text.len

proc empty*(line: Line): bool =
  return line.text.len == 0

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 fromFirst*(line: var Line): string =
  if line.empty:
    raise LineError(msg: "Line is empty!")
  return line.text[line.first..line.position-1]

proc toLast*(line: var Line): string =
  if line.empty:
    raise LineError(msg: "Line is empty!")
  return line.text[line.position..line.last]

proc back*(ed: var LineEditor, n=1) =
  if ed.line.empty:
    return
  stdout.cursorBackward(n)
  ed.line.position = ed.line.position - n

proc forward*(ed: var LineEditor, n=1) = 
  if ed.line.full:
    return
  stdout.cursorForward(n)
  ed.line.position += n

proc deletePrevious*(ed: var LineEditor) =
  if not ed.line.empty:
    if ed.line.full:
      stdout.cursorBackward
      putchar(32)
      stdout.cursorBackward
      ed.line.text = ed.line.text[0..ed.line.last-1]
    else:
      let rest = ed.line.toLast & " "
      ed.back
      for i in rest:
        putchar i.ord
      ed.line.text = ed.line.fromFirst & ed.line.text[ed.line.position+1..ed.line.last]
      stdout.cursorBackward(rest.len)
  
proc deleteNext*(ed: var LineEditor) =
  if not ed.line.empty:
    if not ed.line.full:
      let rest = ed.line.toLast[1..^1] & " "
      for c in rest:
        putchar c.ord
      stdout.cursorBackward(rest.len)
      ed.line.text = ed.line.fromFirst & ed.line.toLast[1..^1]

proc printChar*(ed: var LineEditor, c: int) =  
  if ed.line.full:
    putchar(c.cint)
    ed.line.text &= c.chr
    ed.line.position += 1
  else:
    if ed.mode == mdInsert:
      putchar(c.cint)
      let rest = ed.line.toLast
      ed.line.text.insert($c.chr, ed.line.position)
      ed.line.position += 1
      for j in rest:
        putchar(j.ord)
        ed.line.position += 1
      ed.back(rest.len)
    else: 
      putchar(c.cint)
      ed.line.text[ed.line.position] = c.chr
      ed.line.position += 1

proc initHistory(size = 256): LineHistory =
  result.queue = initQueue[string](size)
  result.position = 0
  result.max = size

proc add(h: var LineHistory, s: string) =
  if s == "":
    return
  if h.queue.len >= h.max:
    discard h.queue.dequeue
  h.queue.enqueue s

proc addToHistory(ed: var LineEditor) =
  ed.history.add ed.line.text

proc initEditor*(mode = mdInsert, historySize = 256): LineEditor =
  result.mode =mode
  result.history = initHistory(historySize)


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

let TERMSETTINGS* = termSave()

# Key Mappings
var KEYMAP*: CritBitTree[KeyCallBack]

KEYMAP["backspace"] = proc(ed: var LineEditor) =
  ed.deletePrevious()
KEYMAP["delete"] = proc(ed: var LineEditor) =
  ed.deleteNext()
KEYMAP["insert"] = proc(ed: var LineEditor) =
  if ed.mode == mdInsert:
    ed.mode = mdReplace
  else:
    ed.mode = mdInsert
KEYMAP["down"] = proc(ed: var LineEditor) =
  discard #TODO
KEYMAP["up"] = proc(ed: var LineEditor) =
  discard #TODO
KEYMAP["left"] = proc(ed: var LineEditor) =
  ed.back()
KEYMAP["right"] = proc(ed: var LineEditor) =
  ed.forward()
KEYMAP["ctrl+c"] = proc(ed: var LineEditor) =
  termRestore(TERMSETTINGS)
  quit(0)

# Key Names
var KEYNAMES*: array[0..31, string]
KEYNAMES[3] = "ctrl+c"


# Key Sequences
var KEYSEQS*: CritBitTree[KeySeq]

when defined(windows):
  KEYSEQS["up"]         = @[224, 72]
  KEYSEQS["down"]       = @[224, 80]
  KEYSEQS["right"]      = @[224, 77]
  KEYSEQS["left"]       = @[224, 75]
  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["insert"]     = @[27, 91, 50, 126]
  KEYSEQS["delete"]     = @[27, 91, 51, 126]


proc readLine*(ed: var LineEditor, prompt=""): string =
  termSetup()
  stdout.write(prompt)
  ed.line = Line(text: "", position: 0)
  while true:
    let c1 = getchar()
    if c1 in {10, 13}:
      termRestore(TERMSETTINGS)
      ed.addToHistory()
      return ed.line.text
    elif c1 in {8, 127}:
      KEYMAP["backspace"](ed)
    elif c1 in PRINTABLE:
      ed.printChar(c1)
    elif c1 in ESCAPES:
      var s = newSeq[Key](0)
      s.add(c1)
      let c2 = getchar()
      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["delete"]:
        KEYMAP["delete"](ed)
      elif s == KEYSEQS["insert"]:
        KEYMAP["insert"](ed)
      elif c2 == 91:
        let c3 = getchar()
        s.add(c3)
        if s == KEYSEQS["right"]:
          KEYMAP["right"](ed)
        elif s == KEYSEQS["left"]:
          KEYMAP["left"](ed)
        elif c3 in {50, 51}:
          let c4 = getchar()
          s.add(c4)
          if c4 == 126 and c3 == 50:
            KEYMAP["insert"](ed)
          elif c4 == 126 and c3 == 51:
            KEYMAP["delete"](ed)
    elif KEYMAP.hasKey(KEYNAMES[c1]):
      KEYMAP[KEYNAMES[c1]](ed)

 
when isMainModule:
  var ed = initEditor()
  while true:
    echo "\n---", ed.readLine("-> "), "---"