|
|
|
|
@ -2,16 +2,13 @@ package main
|
|
|
|
|
|
|
|
|
|
import "core:fmt"
|
|
|
|
|
import "core:log"
|
|
|
|
|
import "core:math"
|
|
|
|
|
import "core:os"
|
|
|
|
|
import "core:slice"
|
|
|
|
|
import "core:strings"
|
|
|
|
|
import "core:thread"
|
|
|
|
|
import "core:time"
|
|
|
|
|
|
|
|
|
|
import "core:math"
|
|
|
|
|
import rl "vendor:raylib"
|
|
|
|
|
|
|
|
|
|
import "file_dialog"
|
|
|
|
|
|
|
|
|
|
main :: proc() {
|
|
|
|
|
context.logger = log.create_console_logger(.Debug)
|
|
|
|
|
@ -19,6 +16,11 @@ main :: proc() {
|
|
|
|
|
|
|
|
|
|
args := os.args[1:]
|
|
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
fmt.println("Usage: gui_cat <file1> [file2] [file3] ...")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config, ok := parse_config("config.ini")
|
|
|
|
|
if !ok {
|
|
|
|
|
log.fatal("failed to parse config file")
|
|
|
|
|
@ -27,7 +29,7 @@ main :: proc() {
|
|
|
|
|
|
|
|
|
|
// Initialize application
|
|
|
|
|
app := App{
|
|
|
|
|
files = make([dynamic]File_Buffer),
|
|
|
|
|
files = make([dynamic]FileBuffer),
|
|
|
|
|
fonts = make([dynamic]FontConfig),
|
|
|
|
|
active_tab = 0,
|
|
|
|
|
current_font_index = 0,
|
|
|
|
|
@ -53,16 +55,12 @@ main :: proc() {
|
|
|
|
|
load_fonts(&app)
|
|
|
|
|
|
|
|
|
|
// Load files from command line arguments
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
|
for filename in args {
|
|
|
|
|
create_file_buffer(&app, filename)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
create_file_buffer(&app)
|
|
|
|
|
load_file(&app, filename)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(app.files) == 0 {
|
|
|
|
|
log.error("no files could be loaded")
|
|
|
|
|
fmt.println("No files could be loaded")
|
|
|
|
|
rl.CloseWindow()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
@ -75,15 +73,141 @@ main :: proc() {
|
|
|
|
|
|
|
|
|
|
// Main loop
|
|
|
|
|
for !rl.WindowShouldClose() {
|
|
|
|
|
rl.BeginDrawing()
|
|
|
|
|
rl.ClearBackground(app.config.colours.background)
|
|
|
|
|
update(&app)
|
|
|
|
|
draw(&app)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
load_fonts :: proc(app: ^App) {
|
|
|
|
|
// TODO: Support loading multiple fonts
|
|
|
|
|
// TODO: Allow searching for a font based on it's name and search fc-list database
|
|
|
|
|
font := FontConfig{
|
|
|
|
|
font = rl.LoadFont(strings.clone_to_cstring(app.config.font)),
|
|
|
|
|
size = f32(app.config.font_size),
|
|
|
|
|
name = "Default",
|
|
|
|
|
}
|
|
|
|
|
append(&app.fonts, font)
|
|
|
|
|
|
|
|
|
|
app.line_height = app.fonts[app.current_font_index].size + 4
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
load_file :: proc(app: ^App, filename: string) {
|
|
|
|
|
data, ok := os.read_entire_file(filename)
|
|
|
|
|
if !ok {
|
|
|
|
|
fmt.printf("Failed to read file: %s\n", filename)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer := FileBuffer{
|
|
|
|
|
filename = strings.clone(filename),
|
|
|
|
|
content = string(data),
|
|
|
|
|
modified = false,
|
|
|
|
|
cursor_pos = 0,
|
|
|
|
|
scroll_y = 0,
|
|
|
|
|
selection = Selection{start = 0, end = 0, active = false},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
append(&app.files, buffer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get_wrapped_lines :: proc(content: string, font: ^FontConfig, max_width: f32) -> []WrappedLine {
|
|
|
|
|
lines := strings.split(content, "\n")
|
|
|
|
|
defer delete(lines)
|
|
|
|
|
|
|
|
|
|
wrapped_lines := make([dynamic]WrappedLine)
|
|
|
|
|
character_offset := 0
|
|
|
|
|
|
|
|
|
|
for line, line_index in lines {
|
|
|
|
|
if len(line) == 0 {
|
|
|
|
|
// Empty line
|
|
|
|
|
append(&wrapped_lines, WrappedLine{
|
|
|
|
|
text = "",
|
|
|
|
|
original_line = line_index,
|
|
|
|
|
character_offset = character_offset,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
// Wrap long lines
|
|
|
|
|
current_pos := 0
|
|
|
|
|
for current_pos < len(line) {
|
|
|
|
|
// Find how many characters fit in the width
|
|
|
|
|
fit_characters := 0
|
|
|
|
|
current_width: f32 = 0
|
|
|
|
|
|
|
|
|
|
for file in app.files {
|
|
|
|
|
if app.thread_finished && thread.is_done(app.thread) {
|
|
|
|
|
thread.destroy(app.thread)
|
|
|
|
|
for i in current_pos..<len(line) {
|
|
|
|
|
character_width := rl.MeasureTextEx(font.font, strings.clone_to_cstring(fmt.tprint(rune(line[i]))), font.size, 1).x
|
|
|
|
|
if current_width + character_width > max_width && fit_characters > 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
current_width += character_width
|
|
|
|
|
fit_characters += 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we couldn't fit even one character, force at least one
|
|
|
|
|
if fit_characters == 0 {
|
|
|
|
|
fit_characters = 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract the text that fits
|
|
|
|
|
end_pos := min(current_pos + fit_characters, len(line))
|
|
|
|
|
wrapped_text := line[current_pos:end_pos]
|
|
|
|
|
|
|
|
|
|
append(&wrapped_lines, WrappedLine{
|
|
|
|
|
text = wrapped_text,
|
|
|
|
|
original_line = line_index,
|
|
|
|
|
character_offset = character_offset + current_pos,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
current_pos = end_pos
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
character_offset += len(line) + 1 // +1 for newline
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return wrapped_lines[:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get_character_at_position :: proc(app: ^App, mouse_pos: rl.Vector2) -> int {
|
|
|
|
|
if len(app.files) == 0 || app.active_tab >= len(app.files) do return 0
|
|
|
|
|
|
|
|
|
|
file := &app.files[app.active_tab]
|
|
|
|
|
font := &app.fonts[app.current_font_index]
|
|
|
|
|
|
|
|
|
|
content_area_x := 10 + app.line_number_width
|
|
|
|
|
content_area_y := app.text_area_y
|
|
|
|
|
content_width := f32(app.window_width - 20) - app.line_number_width - 10
|
|
|
|
|
|
|
|
|
|
if mouse_pos.x < content_area_x || mouse_pos.y < content_area_y do return 0
|
|
|
|
|
|
|
|
|
|
wrapped_lines := get_wrapped_lines(file.content, font, content_width)
|
|
|
|
|
defer delete(wrapped_lines)
|
|
|
|
|
|
|
|
|
|
y_pos := content_area_y - file.scroll_y + 5
|
|
|
|
|
|
|
|
|
|
for wrapped_line, i in wrapped_lines {
|
|
|
|
|
if mouse_pos.y >= y_pos && mouse_pos.y < y_pos + app.line_height {
|
|
|
|
|
// Found the line, now find the character
|
|
|
|
|
x_offset := mouse_pos.x - content_area_x - 5
|
|
|
|
|
|
|
|
|
|
if x_offset <= 0 {
|
|
|
|
|
return wrapped_line.character_offset
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for j in 0..=len(wrapped_line.text) {
|
|
|
|
|
text_slice := wrapped_line.text[:j]
|
|
|
|
|
text_width := rl.MeasureTextEx(font.font, strings.clone_to_cstring(text_slice), font.size, 1).x
|
|
|
|
|
if x_offset <= text_width {
|
|
|
|
|
return wrapped_line.character_offset + j
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return wrapped_line.character_offset + len(wrapped_line.text)
|
|
|
|
|
}
|
|
|
|
|
y_pos += app.line_height
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return len(file.content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update :: proc(app: ^App) {
|
|
|
|
|
// Handle window resize
|
|
|
|
|
if rl.IsWindowResized() {
|
|
|
|
|
app.window_width = rl.GetScreenWidth()
|
|
|
|
|
@ -102,7 +226,6 @@ main :: proc() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !app.file_dialog_open {
|
|
|
|
|
// Handle text selection with mouse
|
|
|
|
|
if len(app.files) > 0 && app.active_tab < len(app.files) {
|
|
|
|
|
current_file := &app.files[app.active_tab]
|
|
|
|
|
@ -110,7 +233,7 @@ main :: proc() {
|
|
|
|
|
|
|
|
|
|
if rl.IsMouseButtonPressed(.LEFT) {
|
|
|
|
|
if mouse_pos.y > app.text_area_y {
|
|
|
|
|
character_pos := get_character_at_position(&app, mouse_pos)
|
|
|
|
|
character_pos := get_character_at_position(app, mouse_pos)
|
|
|
|
|
current_file.cursor_pos = character_pos
|
|
|
|
|
current_file.selection.start = character_pos
|
|
|
|
|
current_file.selection.end = character_pos
|
|
|
|
|
@ -122,7 +245,7 @@ main :: proc() {
|
|
|
|
|
|
|
|
|
|
if app.is_dragging && rl.IsMouseButtonDown(.LEFT) {
|
|
|
|
|
if mouse_pos.y > app.text_area_y {
|
|
|
|
|
character_pos := get_character_at_position(&app, mouse_pos)
|
|
|
|
|
character_pos := get_character_at_position(app, mouse_pos)
|
|
|
|
|
current_file.cursor_pos = character_pos
|
|
|
|
|
if character_pos != app.mouse_drag_start {
|
|
|
|
|
current_file.selection.start = min(app.mouse_drag_start, character_pos)
|
|
|
|
|
@ -263,16 +386,13 @@ main :: proc() {
|
|
|
|
|
// Handle keyboard shortcuts
|
|
|
|
|
if rl.IsKeyDown(.LEFT_CONTROL) || rl.IsKeyDown(.RIGHT_CONTROL) {
|
|
|
|
|
if rl.IsKeyPressed(.S) {
|
|
|
|
|
app.thread = thread.create_and_start_with_poly_data2(&app, current_file, save_file)
|
|
|
|
|
}
|
|
|
|
|
if rl.IsKeyPressed(.O) {
|
|
|
|
|
app.thread = thread.create_and_start_with_poly_data(&app, open_file)
|
|
|
|
|
save_file(current_file)
|
|
|
|
|
}
|
|
|
|
|
if rl.IsKeyPressed(.A) {
|
|
|
|
|
select_all(current_file)
|
|
|
|
|
}
|
|
|
|
|
if rl.IsKeyPressed(.C) {
|
|
|
|
|
copy_selection(&app, current_file)
|
|
|
|
|
copy_selection(app, current_file)
|
|
|
|
|
}
|
|
|
|
|
if rl.IsKeyPressed(.V) {
|
|
|
|
|
paste_text(current_file, app.copied_text)
|
|
|
|
|
@ -294,193 +414,24 @@ main :: proc() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
draw :: proc(app: ^App) {
|
|
|
|
|
rl.BeginDrawing()
|
|
|
|
|
rl.ClearBackground(app.config.colours.background)
|
|
|
|
|
|
|
|
|
|
// Draw tabs
|
|
|
|
|
draw_tabs(&app)
|
|
|
|
|
draw_tabs(app)
|
|
|
|
|
|
|
|
|
|
// Draw active file content
|
|
|
|
|
if len(app.files) > 0 && app.active_tab < len(app.files) {
|
|
|
|
|
draw_file_content(&app, &app.files[app.active_tab])
|
|
|
|
|
draw_file_content(app, &app.files[app.active_tab])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Draw status bar
|
|
|
|
|
draw_status_bar(&app)
|
|
|
|
|
draw_status_bar(app)
|
|
|
|
|
|
|
|
|
|
rl.EndDrawing()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
load_fonts :: proc(app: ^App) {
|
|
|
|
|
// TODO: Support loading multiple fonts
|
|
|
|
|
// TODO: Allow searching for a font based on it's name and search fc-list database
|
|
|
|
|
font := FontConfig{
|
|
|
|
|
font = rl.LoadFont(strings.clone_to_cstring(app.config.font)),
|
|
|
|
|
size = f32(app.config.font_size),
|
|
|
|
|
name = "Default",
|
|
|
|
|
}
|
|
|
|
|
append(&app.fonts, font)
|
|
|
|
|
|
|
|
|
|
app.line_height = app.fonts[app.current_font_index].size + 4
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
create_file_buffer :: proc(app: ^App, filename: string = "") -> (ok: bool) {
|
|
|
|
|
ok = false
|
|
|
|
|
|
|
|
|
|
buffer: File_Buffer
|
|
|
|
|
if len(filename) == 0 {
|
|
|
|
|
buffer = File_Buffer{
|
|
|
|
|
filename = "",
|
|
|
|
|
content = "",
|
|
|
|
|
modified = false,
|
|
|
|
|
cursor_pos = 0,
|
|
|
|
|
scroll_y = 0,
|
|
|
|
|
selection = Selection{start = 0, end = 0, active = false},
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
data, ok := os.read_entire_file(filename)
|
|
|
|
|
if !ok {
|
|
|
|
|
fmt.printf("Failed to read file: %s\n", filename)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer = File_Buffer{
|
|
|
|
|
filename = strings.clone(filename),
|
|
|
|
|
content = string(data),
|
|
|
|
|
modified = false,
|
|
|
|
|
cursor_pos = 0,
|
|
|
|
|
scroll_y = 0,
|
|
|
|
|
selection = Selection{start = 0, end = 0, active = false},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
append(&app.files, buffer)
|
|
|
|
|
|
|
|
|
|
ok = true
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
open_file :: proc(app: ^App) {
|
|
|
|
|
app.file_dialog_open = true
|
|
|
|
|
result: file_dialog.Result
|
|
|
|
|
result = file_dialog.show_open_file_dialog()
|
|
|
|
|
app.file_dialog_open = false
|
|
|
|
|
if !result.success {
|
|
|
|
|
log.error(result.error_message)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer file_dialog.destroy_result(&result)
|
|
|
|
|
|
|
|
|
|
ok := create_file_buffer(app, result.file_path)
|
|
|
|
|
if !ok {
|
|
|
|
|
log.error("failed ot create file buffer")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
app.active_tab+=1
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get_wrapped_lines :: proc(content: string, font: ^FontConfig, max_width: f32) -> []WrappedLine {
|
|
|
|
|
lines := strings.split(content, "\n")
|
|
|
|
|
defer delete(lines)
|
|
|
|
|
|
|
|
|
|
wrapped_lines := make([dynamic]WrappedLine)
|
|
|
|
|
character_offset := 0
|
|
|
|
|
|
|
|
|
|
for line, line_index in lines {
|
|
|
|
|
if len(line) == 0 {
|
|
|
|
|
// Empty line
|
|
|
|
|
append(&wrapped_lines, WrappedLine{
|
|
|
|
|
text = "",
|
|
|
|
|
original_line = line_index,
|
|
|
|
|
character_offset = character_offset,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
// Wrap long lines
|
|
|
|
|
current_pos := 0
|
|
|
|
|
for current_pos < len(line) {
|
|
|
|
|
// Find how many characters fit in the width
|
|
|
|
|
fit_characters := 0
|
|
|
|
|
current_width: f32 = 0
|
|
|
|
|
|
|
|
|
|
for i in current_pos..<len(line) {
|
|
|
|
|
character_width := rl.MeasureTextEx(font.font, strings.clone_to_cstring(fmt.tprint(rune(line[i]))), font.size, 1).x
|
|
|
|
|
if current_width + character_width > max_width && fit_characters > 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
current_width += character_width
|
|
|
|
|
fit_characters += 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we couldn't fit even one character, force at least one
|
|
|
|
|
if fit_characters == 0 {
|
|
|
|
|
fit_characters = 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract the text that fits
|
|
|
|
|
end_pos := min(current_pos + fit_characters, len(line))
|
|
|
|
|
wrapped_text := line[current_pos:end_pos]
|
|
|
|
|
|
|
|
|
|
append(&wrapped_lines, WrappedLine{
|
|
|
|
|
text = wrapped_text,
|
|
|
|
|
original_line = line_index,
|
|
|
|
|
character_offset = character_offset + current_pos,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
current_pos = end_pos
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
character_offset += len(line) + 1 // +1 for newline
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return wrapped_lines[:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get_character_at_position :: proc(app: ^App, mouse_pos: rl.Vector2) -> int {
|
|
|
|
|
if len(app.files) == 0 || app.active_tab >= len(app.files) do return 0
|
|
|
|
|
|
|
|
|
|
file := &app.files[app.active_tab]
|
|
|
|
|
font := &app.fonts[app.current_font_index]
|
|
|
|
|
|
|
|
|
|
content_area_x := 10 + app.line_number_width
|
|
|
|
|
content_area_y := app.text_area_y
|
|
|
|
|
content_width := f32(app.window_width - 20) - app.line_number_width - 10
|
|
|
|
|
|
|
|
|
|
if mouse_pos.x < content_area_x || mouse_pos.y < content_area_y do return 0
|
|
|
|
|
|
|
|
|
|
wrapped_lines := get_wrapped_lines(file.content, font, content_width)
|
|
|
|
|
defer delete(wrapped_lines)
|
|
|
|
|
|
|
|
|
|
y_pos := content_area_y - file.scroll_y + 5
|
|
|
|
|
|
|
|
|
|
for wrapped_line, i in wrapped_lines {
|
|
|
|
|
if mouse_pos.y >= y_pos && mouse_pos.y < y_pos + app.line_height {
|
|
|
|
|
// Found the line, now find the character
|
|
|
|
|
x_offset := mouse_pos.x - content_area_x - 5
|
|
|
|
|
|
|
|
|
|
if x_offset <= 0 {
|
|
|
|
|
return wrapped_line.character_offset
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for j in 0..=len(wrapped_line.text) {
|
|
|
|
|
text_slice := wrapped_line.text[:j]
|
|
|
|
|
text_width := rl.MeasureTextEx(font.font, strings.clone_to_cstring(text_slice), font.size, 1).x
|
|
|
|
|
if x_offset <= text_width {
|
|
|
|
|
return wrapped_line.character_offset + j
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return wrapped_line.character_offset + len(wrapped_line.text)
|
|
|
|
|
}
|
|
|
|
|
y_pos += app.line_height
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return len(file.content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw :: proc(app: ^App) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw_tabs :: proc(app: ^App) {
|
|
|
|
|
@ -528,7 +479,7 @@ draw_tabs :: proc(app: ^App) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw_file_content :: proc(app: ^App, file: ^File_Buffer) {
|
|
|
|
|
draw_file_content :: proc(app: ^App, file: ^FileBuffer) {
|
|
|
|
|
font := &app.fonts[app.current_font_index]
|
|
|
|
|
|
|
|
|
|
// Content area (excluding line numbers)
|
|
|
|
|
@ -706,7 +657,7 @@ draw_status_bar :: proc(app: ^App) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
insert_character :: proc(file: ^File_Buffer, character: rune) {
|
|
|
|
|
insert_character :: proc(file: ^FileBuffer, character: rune) {
|
|
|
|
|
if file.cursor_pos <= len(file.content) {
|
|
|
|
|
before := file.content[:file.cursor_pos]
|
|
|
|
|
after := file.content[file.cursor_pos:]
|
|
|
|
|
@ -717,7 +668,7 @@ insert_character :: proc(file: ^File_Buffer, character: rune) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete_character :: proc(file: ^File_Buffer, delete_backwards: bool = false) {
|
|
|
|
|
delete_character :: proc(file: ^FileBuffer, delete_backwards: bool = false) {
|
|
|
|
|
if file.cursor_pos > 0 && !delete_backwards {
|
|
|
|
|
before := file.content[:file.cursor_pos-1]
|
|
|
|
|
after := file.content[file.cursor_pos:]
|
|
|
|
|
@ -734,7 +685,7 @@ delete_character :: proc(file: ^File_Buffer, delete_backwards: bool = false) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete_selection :: proc(file: ^File_Buffer) {
|
|
|
|
|
delete_selection :: proc(file: ^FileBuffer) {
|
|
|
|
|
if !file.selection.active do return
|
|
|
|
|
|
|
|
|
|
start := min(file.selection.start, file.selection.end)
|
|
|
|
|
@ -748,20 +699,20 @@ delete_selection :: proc(file: ^File_Buffer) {
|
|
|
|
|
clear_selection(file)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear_selection :: proc(file: ^File_Buffer) {
|
|
|
|
|
clear_selection :: proc(file: ^FileBuffer) {
|
|
|
|
|
file.selection.active = false
|
|
|
|
|
file.selection.start = 0
|
|
|
|
|
file.selection.end = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
select_all :: proc(file: ^File_Buffer) {
|
|
|
|
|
select_all :: proc(file: ^FileBuffer) {
|
|
|
|
|
file.selection.start = 0
|
|
|
|
|
file.selection.end = len(file.content)
|
|
|
|
|
file.selection.active = true
|
|
|
|
|
file.cursor_pos = len(file.content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
copy_selection :: proc(app: ^App, file: ^File_Buffer) {
|
|
|
|
|
copy_selection :: proc(app: ^App, file: ^FileBuffer) {
|
|
|
|
|
if !file.selection.active do return
|
|
|
|
|
|
|
|
|
|
start := min(file.selection.start, file.selection.end)
|
|
|
|
|
@ -772,7 +723,7 @@ copy_selection :: proc(app: ^App, file: ^File_Buffer) {
|
|
|
|
|
rl.SetClipboardText(strings.clone_to_cstring(app.copied_text))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
paste_text :: proc(file: ^File_Buffer, text: string) {
|
|
|
|
|
paste_text :: proc(file: ^FileBuffer, text: string) {
|
|
|
|
|
if file.selection.active {
|
|
|
|
|
delete_selection(file)
|
|
|
|
|
}
|
|
|
|
|
@ -789,7 +740,7 @@ paste_text :: proc(file: ^File_Buffer, text: string) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
move_cursor_up :: proc(file: ^File_Buffer) {
|
|
|
|
|
move_cursor_up :: proc(file: ^FileBuffer) {
|
|
|
|
|
// For wrapped text, we need to handle cursor movement differently
|
|
|
|
|
if file.cursor_pos > 0 {
|
|
|
|
|
// Find the previous newline
|
|
|
|
|
@ -816,7 +767,7 @@ move_cursor_up :: proc(file: ^File_Buffer) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
move_cursor_down :: proc(file: ^File_Buffer) {
|
|
|
|
|
move_cursor_down :: proc(file: ^FileBuffer) {
|
|
|
|
|
// For wrapped text, we need to handle cursor movement differently
|
|
|
|
|
if file.cursor_pos < len(file.content) {
|
|
|
|
|
// Find the current line start
|
|
|
|
|
@ -852,7 +803,7 @@ move_cursor_down :: proc(file: ^File_Buffer) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
move_cursor_to_start_of_line :: proc(file: ^File_Buffer) {
|
|
|
|
|
move_cursor_to_start_of_line :: proc(file: ^FileBuffer) {
|
|
|
|
|
// For wrapped text, we need to handle cursor movement differently
|
|
|
|
|
if file.cursor_pos < len(file.content) {
|
|
|
|
|
// Find the current line start
|
|
|
|
|
@ -868,7 +819,7 @@ move_cursor_to_start_of_line :: proc(file: ^File_Buffer) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
move_cursor_to_end_of_line :: proc(file: ^File_Buffer) {
|
|
|
|
|
move_cursor_to_end_of_line :: proc(file: ^FileBuffer) {
|
|
|
|
|
// For wrapped text, we need to handle cursor movement differently
|
|
|
|
|
if file.cursor_pos < len(file.content) {
|
|
|
|
|
// Find the next newline
|
|
|
|
|
@ -884,26 +835,13 @@ move_cursor_to_end_of_line :: proc(file: ^File_Buffer) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
save_file :: proc(app: ^App, file: ^File_Buffer) {
|
|
|
|
|
app.thread_finished = false
|
|
|
|
|
if len(file.filename) == 0 {
|
|
|
|
|
app.file_dialog_open = true
|
|
|
|
|
result := file_dialog.show_save_file_dialog()
|
|
|
|
|
app.file_dialog_open = false
|
|
|
|
|
if !result.success {
|
|
|
|
|
log.error(result.error_message)
|
|
|
|
|
}
|
|
|
|
|
defer file_dialog.destroy_result(&result)
|
|
|
|
|
|
|
|
|
|
file.filename = strings.clone(result.file_path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ok := os.write_entire_file(file.filename, transmute([]u8)file.content)
|
|
|
|
|
file.modified = !ok
|
|
|
|
|
if !ok {
|
|
|
|
|
log.error("failed to save file, \"", file.filename, "\"", sep="") // TODO: Show popup window that displays this to the user
|
|
|
|
|
return
|
|
|
|
|
save_file :: proc(file: ^FileBuffer) {
|
|
|
|
|
success := os.write_entire_file(file.filename, transmute([]u8)file.content)
|
|
|
|
|
if success {
|
|
|
|
|
file.modified = false
|
|
|
|
|
fmt.printf("Saved: %s\n", file.filename)
|
|
|
|
|
} else {
|
|
|
|
|
fmt.printf("Failed to save: %s\n", file.filename)
|
|
|
|
|
}
|
|
|
|
|
app.thread_finished = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|