2020-11-13 17:27:12 +03:00
|
|
|
// Copyright (c) 2020 Lars Pontoppidan. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license distributed with this software.
|
|
|
|
// Don't use this editor for any serious work.
|
2022-01-11 18:33:17 +03:00
|
|
|
// A lot of functionality is missing compared to your favourite editor :)
|
2020-11-13 17:27:12 +03:00
|
|
|
import strings
|
|
|
|
import os
|
2022-01-05 19:02:20 +03:00
|
|
|
import math
|
2020-11-14 10:21:46 +03:00
|
|
|
import term.ui as tui
|
2022-01-25 16:39:35 +03:00
|
|
|
import encoding.utf8
|
2022-01-26 18:25:29 +03:00
|
|
|
import encoding.utf8.east_asian
|
|
|
|
|
|
|
|
const (
|
|
|
|
rune_digits = [`0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`]
|
|
|
|
|
|
|
|
zero_width_unicode = [
|
|
|
|
`\u034f`, // U+034F COMBINING GRAPHEME JOINER
|
|
|
|
`\u061c`, // U+061C ARABIC LETTER MARK
|
|
|
|
`\u17b4`, // U+17B4 KHMER VOWEL INHERENT AQ
|
|
|
|
`\u17b5`, // U+17B5 KHMER VOWEL INHERENT AA
|
|
|
|
`\u200a`, // U+200A HAIR SPACE
|
|
|
|
`\u200b`, // U+200B ZERO WIDTH SPACE
|
|
|
|
`\u200c`, // U+200C ZERO WIDTH NON-JOINER
|
|
|
|
`\u200d`, // U+200D ZERO WIDTH JOINER
|
|
|
|
`\u200e`, // U+200E LEFT-TO-RIGHT MARK
|
|
|
|
`\u200f`, // U+200F RIGHT-TO-LEFT MARK
|
|
|
|
`\u2060`, // U+2060 WORD JOINER
|
|
|
|
`\u2061`, // U+2061 FUNCTION APPLICATION
|
|
|
|
`\u2062`, // U+2062 INVISIBLE TIMES
|
|
|
|
`\u2063`, // U+2063 INVISIBLE SEPARATOR
|
|
|
|
`\u2064`, // U+2064 INVISIBLE PLUS
|
|
|
|
`\u206a`, // U+206A INHIBIT SYMMETRIC SWAPPING
|
|
|
|
`\u206b`, // U+206B ACTIVATE SYMMETRIC SWAPPING
|
|
|
|
`\u206c`, // U+206C INHIBIT ARABIC FORM SHAPING
|
|
|
|
`\u206d`, // U+206D ACTIVATE ARABIC FORM SHAPING
|
|
|
|
`\u206e`, // U+206E NATIONAL DIGIT SHAPES
|
|
|
|
`\u206f`, // U+206F NOMINAL DIGIT SHAPES
|
|
|
|
`\ufeff`, // U+FEFF ZERO WIDTH NO-BREAK SPACE
|
|
|
|
]
|
|
|
|
)
|
2020-11-13 17:27:12 +03:00
|
|
|
|
|
|
|
enum Movement {
|
|
|
|
up
|
|
|
|
down
|
|
|
|
left
|
|
|
|
right
|
|
|
|
home
|
|
|
|
end
|
2020-11-14 20:39:59 +03:00
|
|
|
page_up
|
|
|
|
page_down
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct View {
|
|
|
|
pub:
|
|
|
|
raw string
|
|
|
|
cursor Cursor
|
|
|
|
}
|
|
|
|
|
|
|
|
struct App {
|
|
|
|
mut:
|
2020-11-14 20:39:59 +03:00
|
|
|
tui &tui.Context = 0
|
2021-01-26 17:43:10 +03:00
|
|
|
ed &Buffer = 0
|
2020-11-14 20:39:59 +03:00
|
|
|
current_file int
|
|
|
|
files []string
|
|
|
|
status string
|
|
|
|
t int
|
2020-11-16 15:51:39 +03:00
|
|
|
magnet_x int
|
2020-11-13 20:18:05 +03:00
|
|
|
footer_height int = 2
|
2020-11-14 20:39:59 +03:00
|
|
|
viewport int
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut a App) set_status(msg string, duration_ms int) {
|
|
|
|
a.status = msg
|
|
|
|
a.t = duration_ms
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut a App) save() {
|
2020-11-14 20:39:59 +03:00
|
|
|
if a.cfile().len > 0 {
|
2020-11-13 17:27:12 +03:00
|
|
|
b := a.ed
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file(a.cfile(), b.raw()) or { panic(err) }
|
2020-11-13 20:18:05 +03:00
|
|
|
a.set_status('Saved', 2000)
|
2020-11-13 17:27:12 +03:00
|
|
|
} else {
|
|
|
|
a.set_status('No file loaded', 4000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 20:39:59 +03:00
|
|
|
fn (mut a App) cfile() string {
|
|
|
|
if a.files.len == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
if a.current_file >= a.files.len {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return a.files[a.current_file]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut a App) visit_prev_file() {
|
|
|
|
if a.files.len == 0 {
|
|
|
|
a.current_file = 0
|
|
|
|
} else {
|
|
|
|
a.current_file = (a.current_file + a.files.len - 1) % a.files.len
|
|
|
|
}
|
|
|
|
a.init_file()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut a App) visit_next_file() {
|
|
|
|
if a.files.len == 0 {
|
|
|
|
a.current_file = 0
|
|
|
|
} else {
|
|
|
|
a.current_file = (a.current_file + a.files.len + 1) % a.files.len
|
|
|
|
}
|
|
|
|
a.init_file()
|
|
|
|
}
|
|
|
|
|
2020-11-13 17:27:12 +03:00
|
|
|
fn (mut a App) footer() {
|
2020-11-13 20:18:05 +03:00
|
|
|
w, h := a.tui.window_width, a.tui.window_height
|
2020-11-13 17:27:12 +03:00
|
|
|
mut b := a.ed
|
2020-11-14 10:21:46 +03:00
|
|
|
// flat := b.flat()
|
|
|
|
// snip := if flat.len > 19 { flat[..20] } else { flat }
|
2020-11-14 20:39:59 +03:00
|
|
|
finfo := if a.cfile().len > 0 { ' (' + os.file_name(a.cfile()) + ')' } else { '' }
|
2020-11-13 17:27:12 +03:00
|
|
|
mut status := a.status
|
2020-11-14 10:21:46 +03:00
|
|
|
a.tui.draw_text(0, h - 1, '─'.repeat(w))
|
|
|
|
footer := '$finfo Line ${b.cursor.pos_y + 1:4}/${b.lines.len:-4}, Column ${b.cursor.pos_x + 1:3}/${b.cur_line().len:-3} index: ${b.cursor_index():5} (ESC = quit, Ctrl+s = save)'
|
|
|
|
if footer.len < w {
|
|
|
|
a.tui.draw_text((w - footer.len) / 2, h, footer)
|
|
|
|
} else if footer.len == w {
|
|
|
|
a.tui.draw_text(0, h, footer)
|
|
|
|
} else {
|
|
|
|
a.tui.draw_text(0, h, footer[..w])
|
|
|
|
}
|
2020-11-13 17:27:12 +03:00
|
|
|
if a.t <= 0 {
|
|
|
|
status = ''
|
2020-11-14 10:21:46 +03:00
|
|
|
} else {
|
2021-01-26 17:43:10 +03:00
|
|
|
a.tui.set_bg_color(
|
2020-11-14 20:39:59 +03:00
|
|
|
r: 200
|
|
|
|
g: 200
|
|
|
|
b: 200
|
2021-01-26 17:43:10 +03:00
|
|
|
)
|
|
|
|
a.tui.set_color(
|
2020-11-14 20:39:59 +03:00
|
|
|
r: 0
|
|
|
|
g: 0
|
|
|
|
b: 0
|
2021-01-26 17:43:10 +03:00
|
|
|
)
|
2020-11-14 10:21:46 +03:00
|
|
|
a.tui.draw_text((w + 4 - status.len) / 2, h - 1, ' $status ')
|
|
|
|
a.tui.reset()
|
|
|
|
a.t -= 33
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Buffer {
|
2020-11-14 20:39:59 +03:00
|
|
|
tab_width int = 4
|
2020-11-13 17:27:12 +03:00
|
|
|
pub mut:
|
2021-01-26 17:43:10 +03:00
|
|
|
lines []string
|
|
|
|
cursor Cursor
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (b Buffer) flat() string {
|
2020-11-13 20:18:05 +03:00
|
|
|
return b.raw().replace_each(['\n', r'\n', '\t', r'\t'])
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (b Buffer) raw() string {
|
2020-11-13 20:18:05 +03:00
|
|
|
return b.lines.join('\n')
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 20:18:05 +03:00
|
|
|
fn (b Buffer) view(from int, to int) View {
|
2022-01-25 16:39:35 +03:00
|
|
|
l := b.cur_line().runes()
|
2020-11-13 17:27:12 +03:00
|
|
|
mut x := 0
|
2020-11-13 20:18:05 +03:00
|
|
|
for i := 0; i < b.cursor.pos_x && i < l.len; i++ {
|
2020-11-13 17:27:12 +03:00
|
|
|
if l[i] == `\t` {
|
|
|
|
x += b.tab_width
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
x++
|
|
|
|
}
|
2020-11-13 20:18:05 +03:00
|
|
|
mut lines := []string{}
|
|
|
|
for i, line in b.lines {
|
|
|
|
if i >= from && i <= to {
|
|
|
|
lines << line
|
|
|
|
}
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
2020-11-13 20:18:05 +03:00
|
|
|
raw := lines.join('\n')
|
2021-07-20 11:17:08 +03:00
|
|
|
return View{
|
2020-11-13 20:18:05 +03:00
|
|
|
raw: raw.replace('\t', strings.repeat(` `, b.tab_width))
|
2021-07-20 11:17:08 +03:00
|
|
|
cursor: Cursor{
|
2020-11-13 20:18:05 +03:00
|
|
|
pos_x: x
|
|
|
|
pos_y: b.cursor.pos_y
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 20:39:59 +03:00
|
|
|
fn (b Buffer) line(i int) string {
|
|
|
|
if i < 0 || i >= b.lines.len {
|
2020-11-13 17:27:12 +03:00
|
|
|
return ''
|
|
|
|
}
|
2020-11-14 20:39:59 +03:00
|
|
|
return b.lines[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (b Buffer) cur_line() string {
|
|
|
|
return b.line(b.cursor.pos_y)
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
|
2022-01-26 18:25:29 +03:00
|
|
|
fn (b Buffer) cur_slice() string {
|
|
|
|
line := b.line(b.cursor.pos_y).runes()
|
|
|
|
if b.cursor.pos_x == 0 || b.cursor.pos_x > line.len {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return line[..b.cursor.pos_x].string()
|
|
|
|
}
|
|
|
|
|
2020-11-13 17:27:12 +03:00
|
|
|
fn (b Buffer) cursor_index() int {
|
|
|
|
mut i := 0
|
|
|
|
for y, line in b.lines {
|
2020-11-13 20:18:05 +03:00
|
|
|
if b.cursor.pos_y == y {
|
|
|
|
i += b.cursor.pos_x
|
2020-11-13 17:27:12 +03:00
|
|
|
break
|
|
|
|
}
|
2022-01-25 16:39:35 +03:00
|
|
|
i += line.runes().len + 1
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2020-11-13 20:18:05 +03:00
|
|
|
fn (mut b Buffer) put(s string) {
|
|
|
|
has_line_ending := s.contains('\n')
|
2020-11-13 17:27:12 +03:00
|
|
|
x, y := b.cursor.xy()
|
|
|
|
if b.lines.len == 0 {
|
|
|
|
b.lines.prepend('')
|
|
|
|
}
|
2022-01-25 16:39:35 +03:00
|
|
|
line := b.lines[y].runes()
|
|
|
|
l, r := line[..x].string(), line[x..].string()
|
2020-11-13 17:27:12 +03:00
|
|
|
if has_line_ending {
|
2020-11-13 20:18:05 +03:00
|
|
|
mut lines := s.split('\n')
|
2020-11-13 17:27:12 +03:00
|
|
|
lines[0] = l + lines[0]
|
|
|
|
lines[lines.len - 1] += r
|
|
|
|
b.lines.delete(y)
|
|
|
|
b.lines.insert(y, lines)
|
2022-01-25 16:39:35 +03:00
|
|
|
last := lines[lines.len - 1].runes()
|
2020-11-13 17:27:12 +03:00
|
|
|
b.cursor.set(last.len, y + lines.len - 1)
|
2020-11-13 20:18:05 +03:00
|
|
|
if s == '\n' {
|
|
|
|
b.cursor.set(0, b.cursor.pos_y)
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b.lines[y] = l + s + r
|
2022-01-25 16:39:35 +03:00
|
|
|
b.cursor.set(x + s.runes().len, y)
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
$if debug {
|
2020-11-13 20:18:05 +03:00
|
|
|
flat := s.replace('\n', r'\n')
|
2020-11-13 17:27:12 +03:00
|
|
|
eprintln(@MOD + '.' + @STRUCT + '::' + @FN + ' "$flat"')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut b Buffer) del(amount int) string {
|
|
|
|
if amount == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
x, y := b.cursor.xy()
|
|
|
|
if amount < 0 { // don't delete left if we're at 0,0
|
|
|
|
if x == 0 && y == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2022-01-25 16:39:35 +03:00
|
|
|
} else if x >= b.cur_line().runes().len && y >= b.lines.len - 1 {
|
2020-11-13 20:18:05 +03:00
|
|
|
return ''
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
mut removed := ''
|
|
|
|
if amount < 0 { // backspace (backward)
|
|
|
|
i := b.cursor_index()
|
2022-01-25 16:39:35 +03:00
|
|
|
raw_runes := b.raw().runes()
|
|
|
|
removed = raw_runes[i + amount..i].string()
|
2020-11-13 17:27:12 +03:00
|
|
|
mut left := amount * -1
|
|
|
|
for li := y; li >= 0 && left > 0; li-- {
|
2022-01-25 16:39:35 +03:00
|
|
|
ln := b.lines[li].runes()
|
|
|
|
if left == ln.len + 1 { // All of the line + 1 - since we're going backwards the "+1" is the line break delimiter.
|
|
|
|
b.lines.delete(li)
|
|
|
|
left = 0
|
|
|
|
if y == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
line_above := b.lines[li - 1].runes()
|
|
|
|
b.cursor.pos_x = line_above.len
|
|
|
|
b.cursor.pos_y--
|
|
|
|
break
|
|
|
|
} else if left > ln.len {
|
2020-11-13 17:27:12 +03:00
|
|
|
b.lines.delete(li)
|
|
|
|
if ln.len == 0 { // line break delimiter
|
|
|
|
left--
|
|
|
|
if y == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2022-01-25 16:39:35 +03:00
|
|
|
line_above := b.lines[li - 1].runes()
|
2020-11-13 20:18:05 +03:00
|
|
|
b.cursor.pos_x = line_above.len
|
2020-11-13 17:27:12 +03:00
|
|
|
} else {
|
|
|
|
left -= ln.len
|
|
|
|
}
|
2020-11-13 20:18:05 +03:00
|
|
|
b.cursor.pos_y--
|
2020-11-13 17:27:12 +03:00
|
|
|
} else {
|
|
|
|
if x == 0 {
|
|
|
|
if y == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2022-01-25 16:39:35 +03:00
|
|
|
line_above := b.lines[li - 1].runes()
|
2020-11-13 17:27:12 +03:00
|
|
|
if ln.len == 0 { // at line break
|
|
|
|
b.lines.delete(li)
|
2020-11-13 20:18:05 +03:00
|
|
|
b.cursor.pos_y--
|
|
|
|
b.cursor.pos_x = line_above.len
|
2020-11-13 17:27:12 +03:00
|
|
|
} else {
|
2022-01-25 16:39:35 +03:00
|
|
|
b.lines[li - 1] = line_above.string() + ln.string()
|
2020-11-13 17:27:12 +03:00
|
|
|
b.lines.delete(li)
|
2020-11-13 20:18:05 +03:00
|
|
|
b.cursor.pos_y--
|
|
|
|
b.cursor.pos_x = line_above.len
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
} else if x == 1 {
|
2022-01-25 16:39:35 +03:00
|
|
|
runes := b.lines[li].runes()
|
|
|
|
b.lines[li] = runes[left..].string()
|
2020-11-13 20:18:05 +03:00
|
|
|
b.cursor.pos_x = 0
|
2020-11-13 17:27:12 +03:00
|
|
|
} else {
|
2022-01-25 16:39:35 +03:00
|
|
|
b.lines[li] = ln[..x - left].string() + ln[x..].string()
|
2020-11-13 20:18:05 +03:00
|
|
|
b.cursor.pos_x -= left
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
left = 0
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // delete (forward)
|
|
|
|
i := b.cursor_index() + 1
|
2022-01-25 16:39:35 +03:00
|
|
|
raw_buffer := b.raw().runes()
|
|
|
|
from_i := i
|
|
|
|
mut to_i := i + amount
|
|
|
|
|
|
|
|
if to_i > raw_buffer.len {
|
|
|
|
to_i = raw_buffer.len
|
|
|
|
}
|
|
|
|
removed = raw_buffer[from_i..to_i].string()
|
2020-11-13 17:27:12 +03:00
|
|
|
mut left := amount
|
|
|
|
for li := y; li >= 0 && left > 0; li++ {
|
2022-01-25 16:39:35 +03:00
|
|
|
ln := b.lines[li].runes()
|
2020-11-13 17:27:12 +03:00
|
|
|
if x == ln.len { // at line end
|
|
|
|
if y + 1 <= b.lines.len {
|
2022-01-25 16:39:35 +03:00
|
|
|
b.lines[li] = ln.string() + b.lines[y + 1]
|
2020-11-13 17:27:12 +03:00
|
|
|
b.lines.delete(y + 1)
|
|
|
|
left--
|
|
|
|
b.del(left)
|
|
|
|
}
|
|
|
|
} else if left > ln.len {
|
|
|
|
b.lines.delete(li)
|
|
|
|
left -= ln.len
|
|
|
|
} else {
|
2022-01-25 16:39:35 +03:00
|
|
|
b.lines[li] = ln[..x].string() + ln[x + left..].string()
|
2020-11-13 17:27:12 +03:00
|
|
|
left = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$if debug {
|
2020-11-13 20:18:05 +03:00
|
|
|
flat := removed.replace('\n', r'\n')
|
2020-11-13 17:27:12 +03:00
|
|
|
eprintln(@MOD + '.' + @STRUCT + '::' + @FN + ' "$flat"')
|
|
|
|
}
|
|
|
|
return removed
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut b Buffer) free() {
|
|
|
|
$if debug {
|
|
|
|
eprintln(@MOD + '.' + @STRUCT + '::' + @FN)
|
|
|
|
}
|
|
|
|
for line in b.lines {
|
2021-04-20 17:16:35 +03:00
|
|
|
unsafe { line.free() }
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
unsafe { b.lines.free() }
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
|
2020-11-14 20:39:59 +03:00
|
|
|
fn (mut b Buffer) move_updown(amount int) {
|
|
|
|
b.cursor.move(0, amount)
|
|
|
|
// Check the move
|
2022-01-25 16:39:35 +03:00
|
|
|
line := b.cur_line().runes()
|
2020-11-14 20:39:59 +03:00
|
|
|
if b.cursor.pos_x > line.len {
|
|
|
|
b.cursor.set(line.len, b.cursor.pos_y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 17:27:12 +03:00
|
|
|
// move_cursor will navigate the cursor within the buffer bounds
|
|
|
|
fn (mut b Buffer) move_cursor(amount int, movement Movement) {
|
2022-01-25 16:39:35 +03:00
|
|
|
cur_line := b.cur_line().runes()
|
2020-11-13 17:27:12 +03:00
|
|
|
match movement {
|
|
|
|
.up {
|
2020-11-13 20:18:05 +03:00
|
|
|
if b.cursor.pos_y - amount >= 0 {
|
2020-11-14 20:39:59 +03:00
|
|
|
b.move_updown(-amount)
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.down {
|
2020-11-13 20:18:05 +03:00
|
|
|
if b.cursor.pos_y + amount < b.lines.len {
|
2020-11-14 20:39:59 +03:00
|
|
|
b.move_updown(amount)
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-14 20:39:59 +03:00
|
|
|
.page_up {
|
2022-01-05 19:02:20 +03:00
|
|
|
dlines := math.min(b.cursor.pos_y, amount)
|
2020-11-14 20:39:59 +03:00
|
|
|
b.move_updown(-dlines)
|
|
|
|
}
|
|
|
|
.page_down {
|
2022-01-05 19:02:20 +03:00
|
|
|
dlines := math.min(b.lines.len - 1, b.cursor.pos_y + amount) - b.cursor.pos_y
|
2020-11-14 20:39:59 +03:00
|
|
|
b.move_updown(dlines)
|
|
|
|
}
|
2020-11-13 17:27:12 +03:00
|
|
|
.left {
|
2020-11-13 20:18:05 +03:00
|
|
|
if b.cursor.pos_x - amount >= 0 {
|
2020-11-13 17:27:12 +03:00
|
|
|
b.cursor.move(-amount, 0)
|
2020-11-14 20:39:59 +03:00
|
|
|
} else if b.cursor.pos_y > 0 {
|
2022-01-25 16:39:35 +03:00
|
|
|
b.cursor.set(b.line(b.cursor.pos_y - 1).runes().len, b.cursor.pos_y - 1)
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.right {
|
2020-11-13 20:18:05 +03:00
|
|
|
if b.cursor.pos_x + amount <= cur_line.len {
|
2020-11-13 17:27:12 +03:00
|
|
|
b.cursor.move(amount, 0)
|
2020-11-14 20:39:59 +03:00
|
|
|
} else if b.cursor.pos_y + 1 < b.lines.len {
|
|
|
|
b.cursor.set(0, b.cursor.pos_y + 1)
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.home {
|
2020-11-13 20:18:05 +03:00
|
|
|
b.cursor.set(0, b.cursor.pos_y)
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
.end {
|
2020-11-13 20:18:05 +03:00
|
|
|
b.cursor.set(cur_line.len, b.cursor.pos_y)
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 15:33:08 +03:00
|
|
|
fn (mut b Buffer) move_to_word(movement Movement) {
|
|
|
|
a := if movement == .left { -1 } else { 1 }
|
2022-01-25 16:39:35 +03:00
|
|
|
|
|
|
|
mut line := b.cur_line().runes()
|
2020-11-15 15:33:08 +03:00
|
|
|
mut x, mut y := b.cursor.pos_x, b.cursor.pos_y
|
|
|
|
if x + a < 0 && y > 0 {
|
|
|
|
y--
|
2022-01-25 16:39:35 +03:00
|
|
|
line = b.line(b.cursor.pos_y - 1).runes()
|
2020-11-15 15:33:08 +03:00
|
|
|
x = line.len
|
|
|
|
} else if x + a >= line.len && y + 1 < b.lines.len {
|
|
|
|
y++
|
2022-01-25 16:39:35 +03:00
|
|
|
line = b.line(b.cursor.pos_y + 1).runes()
|
2020-11-15 15:33:08 +03:00
|
|
|
x = 0
|
|
|
|
}
|
|
|
|
// first, move past all non-`a-zA-Z0-9_` characters
|
2022-01-25 16:39:35 +03:00
|
|
|
for x + a >= 0 && x + a < line.len && !(utf8.is_letter(line[x + a])
|
|
|
|
|| line[x + a] in rune_digits || line[x + a] == `_`) {
|
2021-01-26 17:43:10 +03:00
|
|
|
x += a
|
|
|
|
}
|
2020-11-15 15:33:08 +03:00
|
|
|
// then, move past all the letters and numbers
|
2022-01-25 16:39:35 +03:00
|
|
|
for x + a >= 0 && x + a < line.len && (utf8.is_letter(line[x + a])
|
|
|
|
|| line[x + a] in rune_digits || line[x + a] == `_`) {
|
2021-01-26 17:43:10 +03:00
|
|
|
x += a
|
|
|
|
}
|
2020-11-15 15:33:08 +03:00
|
|
|
// if the cursor is out of bounds, move it to the next/previous line
|
2020-11-15 15:43:06 +03:00
|
|
|
if x + a >= 0 && x + a <= line.len {
|
2020-11-15 15:33:08 +03:00
|
|
|
x += a
|
2021-01-26 17:43:10 +03:00
|
|
|
} else if a < 0 && y + 1 > b.lines.len && y - 1 >= 0 {
|
2020-11-15 15:33:08 +03:00
|
|
|
y += a
|
|
|
|
x = 0
|
|
|
|
}
|
|
|
|
b.cursor.set(x, y)
|
|
|
|
}
|
|
|
|
|
2020-11-13 17:27:12 +03:00
|
|
|
struct Cursor {
|
|
|
|
pub mut:
|
2020-11-13 20:18:05 +03:00
|
|
|
pos_x int
|
|
|
|
pos_y int
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut c Cursor) set(x int, y int) {
|
2020-11-13 20:18:05 +03:00
|
|
|
c.pos_x = x
|
|
|
|
c.pos_y = y
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut c Cursor) move(x int, y int) {
|
2020-11-13 20:18:05 +03:00
|
|
|
c.pos_x += x
|
|
|
|
c.pos_y += y
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (c Cursor) xy() (int, int) {
|
2020-11-13 20:18:05 +03:00
|
|
|
return c.pos_x, c.pos_y
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// App callbacks
|
|
|
|
fn init(x voidptr) {
|
2020-11-14 20:39:59 +03:00
|
|
|
mut a := &App(x)
|
|
|
|
a.init_file()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut a App) init_file() {
|
|
|
|
a.ed = &Buffer{}
|
2020-11-13 20:18:05 +03:00
|
|
|
mut init_y := 0
|
|
|
|
mut init_x := 0
|
2020-11-14 20:39:59 +03:00
|
|
|
if a.files.len > 0 && a.current_file < a.files.len && a.files[a.current_file].len > 0 {
|
|
|
|
if !os.is_file(a.files[a.current_file]) && a.files[a.current_file].contains(':') {
|
2020-11-13 20:18:05 +03:00
|
|
|
// support the file:line:col: format
|
2020-11-14 20:39:59 +03:00
|
|
|
fparts := a.files[a.current_file].split(':')
|
2020-11-13 20:18:05 +03:00
|
|
|
if fparts.len > 0 {
|
2020-11-14 20:39:59 +03:00
|
|
|
a.files[a.current_file] = fparts[0]
|
2020-11-13 20:18:05 +03:00
|
|
|
}
|
|
|
|
if fparts.len > 1 {
|
|
|
|
init_y = fparts[1].int() - 1
|
|
|
|
}
|
|
|
|
if fparts.len > 2 {
|
|
|
|
init_x = fparts[2].int() - 1
|
|
|
|
}
|
|
|
|
}
|
2020-11-14 20:39:59 +03:00
|
|
|
if os.is_file(a.files[a.current_file]) {
|
|
|
|
// 'vico: ' +
|
|
|
|
a.tui.set_window_title(a.files[a.current_file])
|
|
|
|
mut b := a.ed
|
2021-03-01 02:18:14 +03:00
|
|
|
content := os.read_file(a.files[a.current_file]) or { panic(err) }
|
2020-11-13 17:27:12 +03:00
|
|
|
b.put(content)
|
2020-11-14 20:39:59 +03:00
|
|
|
a.ed.cursor.pos_x = init_x
|
|
|
|
a.ed.cursor.pos_y = init_y
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 20:39:59 +03:00
|
|
|
fn (a &App) view_height() int {
|
|
|
|
return a.tui.window_height - a.footer_height - 1
|
|
|
|
}
|
2020-11-14 10:21:46 +03:00
|
|
|
|
2020-11-16 15:51:39 +03:00
|
|
|
// magnet_cursor_x will place the cursor as close to it's last move left or right as possible
|
|
|
|
fn (mut a App) magnet_cursor_x() {
|
|
|
|
mut buffer := a.ed
|
|
|
|
if buffer.cursor.pos_x < a.magnet_x {
|
2022-01-25 16:39:35 +03:00
|
|
|
if a.magnet_x < buffer.cur_line().runes().len {
|
2020-11-16 15:51:39 +03:00
|
|
|
move_x := a.magnet_x - buffer.cursor.pos_x
|
|
|
|
buffer.move_cursor(move_x, .right)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 20:39:59 +03:00
|
|
|
fn frame(x voidptr) {
|
|
|
|
mut a := &App(x)
|
|
|
|
mut ed := a.ed
|
|
|
|
a.tui.clear()
|
|
|
|
scroll_limit := a.view_height()
|
2020-11-14 10:21:46 +03:00
|
|
|
// scroll down
|
2020-11-14 20:39:59 +03:00
|
|
|
if ed.cursor.pos_y > a.viewport + scroll_limit { // scroll down
|
|
|
|
a.viewport = ed.cursor.pos_y - scroll_limit
|
|
|
|
} else if ed.cursor.pos_y < a.viewport { // scroll up
|
|
|
|
a.viewport = ed.cursor.pos_y
|
2020-11-13 20:18:05 +03:00
|
|
|
}
|
2020-11-14 20:39:59 +03:00
|
|
|
view := ed.view(a.viewport, scroll_limit + a.viewport)
|
|
|
|
a.tui.draw_text(0, 0, view.raw)
|
|
|
|
a.footer()
|
2022-01-26 18:25:29 +03:00
|
|
|
|
|
|
|
// Unicode: Handle correct mapping of cursor X position in terminal.
|
|
|
|
mut ch_x := view.cursor.pos_x
|
|
|
|
mut sl := ed.cur_slice().replace('\t', ' '.repeat(ed.tab_width))
|
|
|
|
if sl.len > 0 {
|
|
|
|
// Strip out any zero-width codepoints.
|
|
|
|
sl = sl.runes().filter(it !in zero_width_unicode).string()
|
|
|
|
ch_x = east_asian.display_width(sl, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.tui.set_cursor_position(ch_x + 1, ed.cursor.pos_y + 1 - a.viewport)
|
2020-11-14 20:39:59 +03:00
|
|
|
a.tui.flush()
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
|
2020-11-14 10:21:46 +03:00
|
|
|
fn event(e &tui.Event, x voidptr) {
|
2020-11-14 20:39:59 +03:00
|
|
|
mut a := &App(x)
|
|
|
|
mut buffer := a.ed
|
2020-11-13 17:27:12 +03:00
|
|
|
if e.typ == .key_down {
|
|
|
|
match e.code {
|
|
|
|
.escape {
|
|
|
|
exit(0)
|
|
|
|
}
|
2021-01-27 15:52:39 +03:00
|
|
|
.enter {
|
|
|
|
buffer.put('\n')
|
|
|
|
}
|
2020-11-13 17:27:12 +03:00
|
|
|
.backspace {
|
|
|
|
buffer.del(-1)
|
|
|
|
}
|
|
|
|
.delete {
|
|
|
|
buffer.del(1)
|
|
|
|
}
|
|
|
|
.left {
|
2021-02-21 17:07:49 +03:00
|
|
|
if e.modifiers == .ctrl {
|
2020-11-15 15:33:08 +03:00
|
|
|
buffer.move_to_word(.left)
|
2021-02-21 17:07:49 +03:00
|
|
|
} else if e.modifiers.is_empty() {
|
2020-11-15 15:33:08 +03:00
|
|
|
buffer.move_cursor(1, .left)
|
|
|
|
}
|
2020-11-16 15:51:39 +03:00
|
|
|
a.magnet_x = buffer.cursor.pos_x
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
.right {
|
2021-02-21 17:07:49 +03:00
|
|
|
if e.modifiers == .ctrl {
|
2020-11-15 15:33:08 +03:00
|
|
|
buffer.move_to_word(.right)
|
2021-02-21 17:07:49 +03:00
|
|
|
} else if e.modifiers.is_empty() {
|
2020-11-15 15:33:08 +03:00
|
|
|
buffer.move_cursor(1, .right)
|
|
|
|
}
|
2020-11-16 15:51:39 +03:00
|
|
|
a.magnet_x = buffer.cursor.pos_x
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
.up {
|
|
|
|
buffer.move_cursor(1, .up)
|
2020-11-16 15:51:39 +03:00
|
|
|
a.magnet_cursor_x()
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
.down {
|
|
|
|
buffer.move_cursor(1, .down)
|
2020-11-16 15:51:39 +03:00
|
|
|
a.magnet_cursor_x()
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
2020-11-14 20:39:59 +03:00
|
|
|
.page_up {
|
|
|
|
buffer.move_cursor(a.view_height(), .page_up)
|
|
|
|
}
|
|
|
|
.page_down {
|
|
|
|
buffer.move_cursor(a.view_height(), .page_down)
|
|
|
|
}
|
2020-11-13 17:27:12 +03:00
|
|
|
.home {
|
|
|
|
buffer.move_cursor(1, .home)
|
|
|
|
}
|
|
|
|
.end {
|
|
|
|
buffer.move_cursor(1, .end)
|
|
|
|
}
|
2020-11-13 20:18:05 +03:00
|
|
|
48...57, 97...122 { // 0-9a-zA-Z
|
2021-02-21 17:07:49 +03:00
|
|
|
if e.modifiers == .ctrl {
|
2020-11-13 20:18:05 +03:00
|
|
|
if e.code == .s {
|
2020-11-14 20:39:59 +03:00
|
|
|
a.save()
|
2020-11-13 20:18:05 +03:00
|
|
|
}
|
2021-02-21 17:07:49 +03:00
|
|
|
} else if !(e.modifiers.has(.ctrl | .alt) || e.code == .null) {
|
2021-01-27 15:52:39 +03:00
|
|
|
buffer.put(e.ascii.ascii_str())
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2021-02-21 17:07:49 +03:00
|
|
|
if e.modifiers == .alt {
|
2020-11-14 20:39:59 +03:00
|
|
|
if e.code == .comma {
|
|
|
|
a.visit_prev_file()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if e.code == .period {
|
|
|
|
a.visit_next_file()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 16:39:35 +03:00
|
|
|
|
|
|
|
buffer.put(e.utf8)
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-13 20:18:05 +03:00
|
|
|
} else if e.typ == .mouse_scroll {
|
|
|
|
direction := if e.direction == .up { Movement.down } else { Movement.up }
|
|
|
|
buffer.move_cursor(1, direction)
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 20:39:59 +03:00
|
|
|
fn main() {
|
|
|
|
mut files := []string{}
|
|
|
|
if os.args.len > 1 {
|
|
|
|
files << os.args[1..]
|
|
|
|
}
|
|
|
|
mut a := &App{
|
|
|
|
files: files
|
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
a.tui = tui.init(
|
2020-11-14 20:39:59 +03:00
|
|
|
user_data: a
|
|
|
|
init_fn: init
|
|
|
|
frame_fn: frame
|
|
|
|
event_fn: event
|
|
|
|
capture_events: true
|
2021-01-26 17:43:10 +03:00
|
|
|
)
|
2022-05-13 06:56:21 +03:00
|
|
|
a.tui.run()?
|
2020-11-13 17:27:12 +03:00
|
|
|
}
|