#!/usr/bin/env fish # fin - Fish Extensible Text Editor Written In Fish (String-based version) # Copyright (C) 2025 Digit # Licensed under GNU GPL v3 - see http://www.gnu.org/licenses/ # # NOTE: [This] fishsticks branch uses Fish string operations # [other] onix (original master/main) branch uses external tools. # [other] artictern branch is more AWK heavy. # Fish interprets escape sequences (\n, \t, etc.) in string literals, which # may differ from awk-based text processing that treats them more literally. # # This is another candidate for 0.019, which: # # Mends: # 1a. wrap-hides-top-lines bug. # # The viewport's now budgeted in SCREEN ROWS, not logical lines, # so wrapped lines wont irretrievably push a file's top lines off screen. # # 1b. status bar wraps bug. # # The status bar is clipped to the terminal width, # so it can never wrap on the bottom row and scroll everything up. # (... except, the user can still overload the status bar, if they try) # ... yay. that bug was kicking my ass longer than the undo/redo. # ... i got un-fussy. it can be cleaned up more. # putting it through some further real world testing and stress testing # ... then we might actualy have a solid candidate to push # ... then we can clean up the repos, move main to onix, merge(? pull?) fishsticks to main. # ... and then continue dev. :) # ... ... (maybe port fishsticks features to onix and artictern and frenetic) # ... ... ... (maybe leave onix and artictern to rott in museum ) set -g cursor_line 1 set -g cursor_col 1 set -g running true set -g current_file "" set -g temp_file (mktemp) set -g viewport_start 1 set -g file_lines # Cache file content in array set -g undo_count 0 set -g undo_cursor_stack set -g redo_count 0 set -g redo_cursor_stack set -g status_left 0 #set -g finversion "0.019" set -g finversion "0.019 tlwfix002" #testline # string length --visual needs fish 3.3+; fall back to plain length otherwise if string length --visual -- "x" >/dev/null 2>&1 set -g fin_visual_ok true else set -g fin_visual_ok false end function fin if contains -- --help $argv; or contains -- -h $argv fin_help return end if contains -- --version $argv; or contains -- -v $argv echo "fin" $finversion "- fish editor" return end if test (count $argv) -gt 0 open_file $argv[1] else set -g file_lines "" # Start with empty content save_lines_to_temp end edit rm -f $temp_file end function fin_help echo "fin - fish editor" echo "" echo "Usage: fin [file]" echo "" echo "Keys:" echo " ^Q quit" echo " ^S save" echo " ^O open" echo " ^V paste clipboard" echo " ^Z undo" echo " ^Y redo" echo " arrows move" echo " pgup/pgdn scroll" echo " bksp delete" echo " mid-click paste selection" end # Save file_lines array to temp file function save_lines_to_temp printf '%s\n' $file_lines > $temp_file end # Load temp file into file_lines array function load_lines_from_temp set -g file_lines if test -s $temp_file while read -l line set -ga file_lines $line end < $temp_file end end # Save state for undo function save_state set undo_count (math $undo_count + 1) cp $temp_file /tmp/fin_undo_$undo_count set -ga undo_cursor_stack "$cursor_line $cursor_col" end # Undo function undo if test $undo_count -gt 0 # Save current for redo set redo_count (math $redo_count + 1) cp $temp_file /tmp/fin_redo_$redo_count set -ga redo_cursor_stack "$cursor_line $cursor_col" # Then restore cp /tmp/fin_undo_$undo_count $temp_file load_lines_from_temp set cursor_parts (string split ' ' -- $undo_cursor_stack[-1]) set -g cursor_line $cursor_parts[1] set -g cursor_col $cursor_parts[2] clamp_cursor_col rm -f /tmp/fin_undo_$undo_count set -e undo_cursor_stack[-1] set undo_count (math $undo_count - 1) end end # Redo function redo if test $redo_count -gt 0 cp /tmp/fin_redo_$redo_count $temp_file load_lines_from_temp set cursor_parts (string split ' ' -- $undo_cursor_stack[-1]) set -g cursor_line $cursor_parts[1] set -g cursor_col $cursor_parts[2] clamp_cursor_col rm -f /tmp/fin_redo_$redo_count set -e redo_cursor_stack[-1] set redo_count (math $redo_count - 1) end end # Utility functions for handling escape sequences if needed function escape_for_storage # Use string escape to handle special characters for storage string escape --style=script -- $argv[1] end function unescape_for_display # Use string unescape to restore characters for display string unescape --style=script -- $argv[1] end # Get line by number using array indexing (escape dashes) function get_line set line_num $argv[1] if test $line_num -le (count $file_lines) echo -- $file_lines[$line_num] else echo "" end end # Get total line count function get_line_count count $file_lines end # How many SCREEN ROWS a logical line occupies once the terminal wraps it. # old buggy code wrongly assumed 1 line = 1 row, # so any line wider than the terminal secretly consumed extra rows and # pushed the top of the buffer off screen. function wrapped_rows --argument-names line_num width if test $line_num -gt (count $file_lines) echo 1 return end if test $fin_visual_ok = true set len (string length --visual -- "$file_lines[$line_num]") else set len (string length -- "$file_lines[$line_num]") end if test -z "$len" set len 0 end set rows 1 while test $len -gt $width set rows (math $rows + 1) set len (math $len - $width) end echo $rows end # Last logical line that fits within $avail screen rows, starting at $start. # Always returns at least $start, even if that single line alone is too tall # (a line taller than the whole screen still overflows; the real cure for # that is horizontal scrolling, someday). function last_fitting_line --argument-names start avail width set total (count $file_lines) set rows 0 set last (math $start - 1) set n $start while test $n -le $total set need (wrapped_rows $n $width) if test (math $rows + $need) -gt $avail break end set rows (math $rows + $need) set last $n set n (math $n + 1) end if test $last -lt $start set last $start end echo $last end # hey digit, remember that idea about yanking the status line code out to # its own function? this is a start. status_seg prints one coloured segment # clipped to the remaining status width, so the status bar can NEVER reach # the last column of the bottom row. writing that column arms the terminal's # auto-margin, and the wrap that follows scrolls the whole screen up, which # is exactly how the wrap-hides-top-lines bug ate your top lines. function status_seg --argument-names seg if test $status_left -le 0 return end set clipped (string sub --length $status_left -- "$seg") # printf '%s' "$clipped" "dafuqiziz" #testline # printf '%s' "dafuq" "$clipped" #testline printf '%s' "$clipped" set used (string length -- "$clipped") if test -z "$used" set used 0 end set -g status_left (math $status_left - $used) end # Render - row-aware viewport: budget screen rows, not logical lines function render clear set term_height (tput lines) set term_width (tput cols) set status_row (math $term_height - 1) set avail_rows (math $term_height - 1) # every row above the status bar set total_lines (get_line_count) # Cursor above the window: snap to it if test $cursor_line -lt $viewport_start set -g viewport_start $cursor_line end if test $viewport_start -lt 1 set -g viewport_start 1 end # Scroll down until the cursor's line fits inside the ROW budget. # This replaces the old logical-line clamp: a cursor sitting on a tall # wrapped line now scrolls by as many lines as it really takes. while test (last_fitting_line $viewport_start $avail_rows $term_width) -lt $cursor_line set -g viewport_start (math $viewport_start + 1) end set end_line (last_fitting_line $viewport_start $avail_rows $term_width) # Render visible lines for line_num in (seq $viewport_start $end_line) if test $line_num -le $total_lines set current_line (get_line $line_num) else set current_line "" end if test $line_num -eq $cursor_line # Show cursor line with visual cursor (handle dash escaping) set before_cursor (string sub --length (math $cursor_col - 1) -- "$current_line") set at_cursor (string sub --start $cursor_col --length 1 -- "$current_line") set after_cursor (string sub --start (math $cursor_col + 1) -- "$current_line") if test -z "$at_cursor" printf "%s\033[7m \033[0m%s\r\n" "$before_cursor" "$after_cursor" else printf "%s\033[7m%s\033[0m%s\r\n" "$before_cursor" "$at_cursor" "$after_cursor" end else printf "%s\r\n" "$current_line" end end # Status line - built from clipped segments, so an over-wide status # (long path, big file name) now clips at the right edge instead of # wrapping off the bottom row. # # whoami@hostname:pwd could come back now, the status can't wrap anymore, # but it'd still crowd out the filename. your call. set file_state edited if test -n "$current_file"; and diff $temp_file $current_file >/dev/null 2>&1 set file_state safe end set -g status_left (math $term_width - 1) tput cup $status_row 0 set_color -b "#8356AF" status_seg "$cursor_line" set_color -b "#D07771" set_color "#8356AF" status_seg "$cursor_col" set_color -b "#8356AF" set_color white status_seg " | " set_color "#D07771" status_seg "$current_file" set_color white status_seg " | $file_state | " set_color "#D07771" status_seg "fin " set_color white status_seg "$finversion" set_color -b normal # status_seg "dafuqizat" #testline end # Simple main loop with mouse reporting function edit stty raw -echo 2>/dev/null printf '\e[?1000h' # Enable mouse reporting while $running render set key (head -c1) handle_key $key end printf '\e[?1000l' # Disable mouse reporting stty sane 2>/dev/null end # Key handling with mouse support function handle_key set key_code (printf '%s' $argv[1] | od -An -tx1 | string trim | string replace -a ' ' '') switch $key_code case '11' # Ctrl-Q set -g running false case '13' # Ctrl-S save_file case '0f' # Ctrl-O open_file_dialog case '16' # Ctrl-V paste_clipboard case '19' # Ctrl-Y redo case '1a' # Ctrl-Z undo case '1b' # Escape sequences handle_escape_sequences case '7f' '08' # Backspace backspace_char case '0a' '0d' # Enter insert_newline case '*' if test -n "$argv[1]" insert_char $argv[1] end end end # Handle escape sequences including mouse events function handle_escape_sequences set seq1 (head -c1) # Mouse sequences start with [M if test "$seq1" = "[" set seq2 (head -c1) if test "$seq2" = "M" handle_mouse_event return end # Regular arrow/page keys switch $seq1$seq2 case '[A' 'OA' # Up move_up case '[B' 'OB' # Down move_down case '[C' 'OC' # Right move_right case '[D' 'OD' # Left move_left case '[5' # Page Up (ESC [5~) set seq3 (head -c1) # Read the ~ page_up case '[6' # Page Down (ESC [6~) set seq3 (head -c1) # Read the ~ page_down end else if test "$seq1" = "O" set seq2 (head -c1) switch O$seq2 case 'OA' # Up move_up case 'OB' # Down move_down case 'OC' # Right move_right case 'OD' # Left move_left end end end # Handle mouse events - detect middle-click and paste function handle_mouse_event # Read mouse data: button, x, y (3 bytes) set button_byte (head -c1) set x_byte (head -c1) set y_byte (head -c1) # Convert button byte to decimal set button_code (printf '%s' $button_byte | od -An -tu1 | string trim) # Middle-click button press is typically 33 (0x21) if test "$button_code" = "33" paste_selection end end # Cross-platform paste functions function paste_selection set temp_paste (mktemp) # Try clipboard tools in order of preference if xsel -p 2>/dev/null > $temp_paste # Linux xsel worked else if xclip -o -selection primary 2>/dev/null > $temp_paste # Linux xclip worked else if pbpaste 2>/dev/null > $temp_paste # macOS worked else if wl-paste -p 2>/dev/null > $temp_paste # Wayland worked else rm $temp_paste return end if test ! -s $temp_paste rm $temp_paste return end paste_from_file $temp_paste rm $temp_paste end function paste_clipboard set temp_paste (mktemp) # Try clipboard tools in order of preference if xsel -b 2>/dev/null > $temp_paste # Linux xsel worked else if xclip -o -selection clipboard 2>/dev/null > $temp_paste # Linux xclip worked else if pbpaste 2>/dev/null > $temp_paste # macOS worked else if wl-paste -p 2>/dev/null > $temp_paste # Wayland worked else rm $temp_paste return end if test ! -s $temp_paste rm $temp_paste return end paste_from_file $temp_paste rm $temp_paste end # Paste logic using Fish string operations and arrays function paste_from_file save_state set temp_paste $argv[1] # Read paste content into array using Fish set paste_lines while read -l line set -a paste_lines $line end < $temp_paste set paste_line_count (count $paste_lines) if test $paste_line_count -eq 0 return end # Get current line set current_line (get_line $cursor_line) # Split current line at cursor position using string sub (escape dashes) set before_cursor (string sub --length (math $cursor_col - 1) -- "$current_line") set after_cursor (string sub --start $cursor_col -- "$current_line") if test $paste_line_count -eq 1 # Single line paste - use string join (escape dashes) set new_line (string join '' -- "$before_cursor" "$paste_lines[1]" "$after_cursor") set file_lines[$cursor_line] "$new_line" # Update cursor position set -g cursor_col (math $cursor_col + (string length -- "$paste_lines[1]")) else # Multi-line paste - rebuild file_lines array set new_file_lines # Add lines before cursor line for i in (seq 1 (math $cursor_line - 1)) if test $i -le (count $file_lines) set -a new_file_lines $file_lines[$i] end end # Add first paste line with before text (escape dashes) set -a new_file_lines (string join '' -- "$before_cursor" "$paste_lines[1]") # Add middle paste lines if test $paste_line_count -gt 2 for i in (seq 2 (math $paste_line_count - 1)) set -a new_file_lines "$paste_lines[$i]" end end # Add last paste line with after text (escape dashes) set -a new_file_lines (string join '' -- "$paste_lines[$paste_line_count]" "$after_cursor") # Add lines after cursor line set old_line_count (count $file_lines) if test $cursor_line -lt $old_line_count for i in (seq (math $cursor_line + 1) $old_line_count) set -a new_file_lines $file_lines[$i] end end # Replace file_lines with new content set -g file_lines $new_file_lines # Update cursor position to end of paste set -g cursor_line (math $cursor_line + $paste_line_count - 1) set -g cursor_col (math (string length -- "$paste_lines[$paste_line_count]") + 1) end save_lines_to_temp end # Movement functions using Fish operations function move_up if test $cursor_line -gt 1 set -g cursor_line (math $cursor_line - 1) clamp_cursor_col else set -g cursor_col 1 end end function move_down set total_lines (get_line_count) if test $cursor_line -lt $total_lines set -g cursor_line (math $cursor_line + 1) clamp_cursor_col else set line (get_line $cursor_line) set -g cursor_col (math (string length -- "$line") + 1) end end function move_left if test $cursor_col -eq 1 if test $cursor_line -gt 1 set -g cursor_line (math $cursor_line - 1) set prev_line (get_line $cursor_line) set -g cursor_col (math (string length -- "$prev_line") + 1) end else set -g cursor_col (math $cursor_col - 1) end end function move_right set line (get_line $cursor_line) set line_len (string length -- "$line") if test $cursor_col -eq (math $line_len + 1) set total_lines (get_line_count) if test $cursor_line -lt $total_lines set -g cursor_line (math $cursor_line + 1) set -g cursor_col 1 end else set -g cursor_col (math $cursor_col + 1) end end function clamp_cursor_col set line (get_line $cursor_line) set line_len (string length -- "$line") if test $cursor_col -gt (math $line_len + 1) set -g cursor_col (math $line_len + 1) end end # Page movement functions (logical pages; render clamps the cursor into the # row budget afterwards, so paging still lands sanely with wrapped lines) function page_up set term_height (tput lines) set content_lines (math $term_height - 2) set page_jump (math $content_lines - 1) if test $viewport_start -eq 1 set -g cursor_line 1 set -g cursor_col 1 return end set cursor_offset_in_view (math $cursor_line - $viewport_start) set new_viewport_start (math $viewport_start - $page_jump) if test $new_viewport_start -lt 1 set new_viewport_start 1 end set -g viewport_start $new_viewport_start set new_cursor_line (math $viewport_start + $cursor_offset_in_view) if test $new_cursor_line -lt 1 set new_cursor_line 1 end set -g cursor_line $new_cursor_line clamp_cursor_col end function page_down set term_height (tput lines) set content_lines (math $term_height - 2) set page_jump (math $content_lines - 1) set total_lines (get_line_count) set viewport_end (math $viewport_start + $content_lines - 1) if test $viewport_end -ge $total_lines set -g cursor_line $total_lines set line (get_line $cursor_line) set -g cursor_col (math (string length -- "$line") + 1) return end set cursor_offset_in_view (math $cursor_line - $viewport_start) set new_viewport_start (math $viewport_start + $page_jump) set -g viewport_start $new_viewport_start set new_cursor_line (math $viewport_start + $cursor_offset_in_view) if test $new_cursor_line -gt $total_lines set new_cursor_line $total_lines end set -g cursor_line $new_cursor_line clamp_cursor_col end # Character insertion using Fish string operations (escape dashes) function insert_char set char $argv[1] # Skip control characters (except tab, newline, carriage return) set char_code (printf '%s' $char | od -An -tu1 | string trim) if test $char_code -lt 32 -a $char_code -ne 9 -a $char_code -ne 10 -a $char_code -ne 13 return end save_state set current_line (get_line $cursor_line) # Use string sub to split line at cursor position (escape dashes) set before_cursor (string sub --length (math $cursor_col - 1) -- "$current_line") set after_cursor (string sub --start $cursor_col -- "$current_line") # Join the parts with the new character using string join (escape dashes) set new_line (string join '' -- "$before_cursor" "$char" "$after_cursor") # Update the line in our array if test $cursor_line -le (count $file_lines) set file_lines[$cursor_line] "$new_line" else # Extend file_lines if needed while test (count $file_lines) -lt $cursor_line set -a file_lines "" end set file_lines[$cursor_line] "$new_line" end set -g cursor_col (math $cursor_col + 1) save_lines_to_temp end # Newline insertion using Fish array operations (escape dashes) function insert_newline save_state set current_line (get_line $cursor_line) # Split current line at cursor position (escape dashes) set before_cursor (string sub --length (math $cursor_col - 1) -- "$current_line") set after_cursor (string sub --start $cursor_col -- "$current_line") # Rebuild file_lines array with new line inserted set new_file_lines # Add lines before cursor for i in (seq 1 (math $cursor_line - 1)) if test $i -le (count $file_lines) set -a new_file_lines "$file_lines[$i]" end end # Add the split lines set -a new_file_lines "$before_cursor" set -a new_file_lines "$after_cursor" # Add lines after cursor set old_line_count (count $file_lines) if test $cursor_line -lt $old_line_count for i in (seq (math $cursor_line + 1) $old_line_count) set -a new_file_lines "$file_lines[$i]" end end set -g file_lines $new_file_lines # Move cursor to start of new line set -g cursor_line (math $cursor_line + 1) set -g cursor_col 1 save_lines_to_temp end # Backspace using Fish string operations (escape dashes) function backspace_char save_state if test $cursor_col -gt 1 set current_line (get_line $cursor_line) # Use string sub to remove character before cursor (escape dashes) set before_cursor (string sub --length (math $cursor_col - 2) -- "$current_line") set after_cursor (string sub --start $cursor_col -- "$current_line") # Join the parts (escape dashes) set new_line (string join '' -- "$before_cursor" "$after_cursor") set file_lines[$cursor_line] "$new_line" set -g cursor_col (math $cursor_col - 1) save_lines_to_temp else if test $cursor_line -gt 1 # Backspace at start of line - join with previous line set current_line (get_line $cursor_line) set prev_line (get_line (math $cursor_line - 1)) # Calculate new cursor position set new_cursor_col (math (string length -- "$prev_line") + 1) # Join lines using string join (escape dashes) set joined_line (string join '' -- "$prev_line" "$current_line") # Rebuild file_lines array without current line set new_file_lines # Add lines before previous line for i in (seq 1 (math $cursor_line - 2)) if test $i -le (count $file_lines) set -a new_file_lines "$file_lines[$i]" end end # Add joined line set -a new_file_lines "$joined_line" # Add lines after current line set old_line_count (count $file_lines) if test $cursor_line -lt $old_line_count for i in (seq (math $cursor_line + 1) $old_line_count) set -a new_file_lines "$file_lines[$i]" end end set -g file_lines $new_file_lines set -g cursor_line (math $cursor_line - 1) set -g cursor_col $new_cursor_col save_lines_to_temp end end # File operations function save_file if test -n "$current_file" save_lines_to_temp cp $temp_file $current_file else stty sane 2>/dev/null read -P "save:" filename if test -n "$filename" set -g current_file $filename save_lines_to_temp cp $temp_file $current_file end stty raw -echo 2>/dev/null end end function open_file_dialog stty sane 2>/dev/null read -P "open:" filename if test -n "$filename" open_file $filename end stty raw -echo 2>/dev/null end function open_file set -g current_file $argv[1] if test -f $current_file cp $current_file $temp_file load_lines_from_temp else set -g file_lines "" save_lines_to_temp end set -g cursor_line 1 set -g cursor_col 1 set -g viewport_start 1 # Clear undo/redo stacks for new file set -g undo_count 0 set -g undo_cursor_stack set -g redo_count 0 set -g redo_cursor_stack rm -f /tmp/fin_undo_* /tmp/fin_redo_* end if not status is-interactive fin $argv end