Mercurial Hosting > editor
view editor.luan @ 29:01b8a25b38aa
work
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 13 Apr 2025 12:25:57 -0600 |
parents | bdb8754f1211 |
children | 8e32ad89c2a1 |
line wrap: on
line source
local Luan = require "luan:Luan.luan" local error = Luan.error local ipairs = Luan.ipairs or error() local range = Luan.range or error() local stringify = Luan.stringify or error() local String = require "luan:String.luan" local sub_string = String.sub or error() local replace = String.replace or error() local starts_with = String.starts_with or error() local to_number = String.to_number or error() local find = String.find or error() local Io = require "luan:Io.luan" local print = Io.print or error() local new_file = Io.schemes.file or error() local Math = require "luan:Math.luan" local min = Math.min or error() local Swing = require "luan:swing/Swing.luan" local new_frame = require("luan:swing/Frame.luan").new or error() local new_label = require("luan:swing/Label.luan").new or error() local new_text_area = require("luan:swing/Text_area.luan").new or error() local new_scroll_pane = require("luan:swing/Scroll_pane.luan").new or error() local new_text_area_line_numbers = require("luan:swing/Text_area_line_numbers.luan").new or error() local new_menu_bar = require("luan:swing/Menu_bar.luan").new or error() local Menu = require "luan:swing/Menu.luan" local new_menu = Menu.new or error() local separator = Menu.separator or error() local new_menu_item = require("luan:swing/Menu_item.luan").new or error() local new_check_box_menu_item = require("luan:swing/Check_box_menu_item.luan").new or error() local int_to_color = require("luan:swing/Color.luan").int_to_color or error() local Border = require "luan:swing/Border.luan" local create_empty_border = Border.create_empty_border or error() local create_line_border = Border.create_line_border or error() local no_border = Border.no_border or error() local Layout = require "luan:swing/Layout.luan" local new_mig_layout = Layout.new_mig_layout or error() local Option_pane = require "luan:swing/Option_pane.luan" local show_message_dialog = Option_pane.show_message_dialog or error() local show_input_dialog = Option_pane.show_input_dialog or error() local new_dialog = require("luan:swing/Dialog.luan").new or error() local new_panel = require("luan:swing/Component.luan").new_panel or error() local new_button = require("luan:swing/Button.luan").new or error() local new_text_field = require("luan:swing/Text_field.luan").new or error() local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "editor" local new_window local function action_listener(fn) return function(_) fn() end end local function add_menu_bar(window) local document = window.text_area.document local revert = new_menu_item{ text = "Revert" enabled = window.has_file action_listener = action_listener(window.revert) } local undo = new_menu_item{ text = "Undo" accelerator = "meta Z" action_listener = action_listener(document.undo) } local redo = new_menu_item{ text = "Redo" accelerator = "meta shift Z" action_listener = action_listener(document.redo) } local function update_undo_redo() undo.set_enabled(document.can_undo()) redo.set_enabled(document.can_redo()) end window.update_undo_redo = update_undo_redo -- dont gc update_undo_redo() document.add_undo_listener(update_undo_redo) local menu_bar = new_menu_bar{ menus = { new_menu{ text = "File" menu_items = { new_menu_item{ text = "New File" accelerator = "meta N" action_listener = function(_) new_window() end } new_menu_item{ text = "Open..." accelerator = "meta O" action_listener = action_listener(window.open) } new_menu_item{ text = "Save" accelerator = "meta S" action_listener = function(_) if window.save() then revert.set_enabled(true) end end } revert } } new_menu{ text = "Edit" menu_items = { undo redo separator new_menu_item{ text = "Cut" accelerator = "meta X" action_listener = action_listener(window.text_area.cut) } new_menu_item{ text = "Copy" accelerator = "meta C" action_listener = action_listener(window.text_area.copy) } new_menu_item{ text = "Paste" accelerator = "meta V" action_listener = action_listener(window.text_area.paste) } separator new_menu_item{ text = "Indent" accelerator = "meta CLOSE_BRACKET" action_listener = action_listener(window.indent) } new_menu_item{ text = "Unindent" accelerator = "meta OPEN_BRACKET" action_listener = action_listener(window.unindent) } separator new_menu_item{ text = "Select All" accelerator = "meta A" action_listener = action_listener(window.text_area.select_all) } separator new_check_box_menu_item{ text = "Find and Replace" accelerator = "meta F" action_listener = function(event) window.show_find_panel(event.source.state) end } } } new_menu{ text = "View" menu_items = { new_check_box_menu_item{ text = "Word Wrap" state = window.text_area.line_wrap action_listener = function(event) window.text_area.line_wrap = event.source.state end } new_check_box_menu_item{ text = "Show Whitespace" action_listener = function(event) window.text_area.show_whitespace(event.source.state) end } new_menu_item{ text = "Show Cursor Column" action_listener = function(_) show_message_dialog( window.frame, "Cursor Column: "..window.cursor_column() ) end } new_menu_item{ text = "Goto Line" accelerator = "meta G" action_listener = function(_) local input = show_input_dialog( window.frame, "Goto line" ) local line = input and to_number(input) if line ~= nil then window.goto(line) end end } } } } } window.frame.set_menu_bar(menu_bar) end local function get_matches(text,s) local n = #s if n == 0 then return nil end local matches = {} local i = 0 while(true) do local j = find(text,s,i) if j == nil then break end matches[#matches+1] = { start=j, end_=j+n } i = j + n end return matches end local function make_find_panel(window) local text_area = window.text_area local find_field, output local function find_match(event) --logger.info("action "..event.action) local s = find_field.text local matches = get_matches( text_area.text, s ) if matches == nil then output.text = "" return end local n_matches = #matches if n_matches == 0 then output.text = "0 matches" return end local action = event.action if action == "next" then local _, pos = text_area.get_selection() for i, match in ipairs(matches) do if match.start >= pos then text_area.set_selection( match.start, match.end_ ) output.text = i.." of "..n_matches.." matches" return end end local match = matches[1] text_area.set_selection( match.start, match.end_ ) output.text = "1 of "..n_matches.." matches; wrapped past end" elseif action == "previous" then local pos = text_area.get_selection() for i in range(n_matches,1,-1) do local match = matches[i] if match.end_ <= pos then text_area.set_selection( match.start, match.end_ ) output.text = i.." of "..n_matches.." matches" return end end local match = matches[n_matches] text_area.set_selection( match.start, match.end_ ) output.text = n_matches.." of "..n_matches.." matches; wrapped past end" else error(action) end end find_field = new_text_field{ constraints = "growx" columns = 20 action = "next" action_listener = find_match } output = new_label{ constraints = "span" text = "testing" } local find_panel = new_panel{ constraints = "growy 0,growx" layout = new_mig_layout("","[][grow][grow 0]") visible = false children = { new_label{ constraints = "right" text = "Find:" } find_field new_panel{ constraints = "wrap" layout = new_mig_layout("insets 0") children = { new_button{ text = "Find Next" action = "next" action_listener = find_match } new_button{ text = "Find Previous" action = "previous" action_listener = find_match } } } new_label{ constraints = "right" text = "Replace:" } new_text_field{ constraints = "growx" columns = 20 } new_button{ constraints = "wrap" text = "Replace" } output } } function window.show_find_panel(visible) find_panel.visible = visible if visible then find_field.request_focus_in_window() end end return find_panel end local n_windows = 0 local documents = {} function new_window(file) local window = {} window.has_file = file~=nil and file.is_file() local text_area = new_text_area{ wrap_style_word = true line_wrap = true tab_size = 4 font = { family="Monospaced", size=13 } } window.text_area = text_area local title = file and file.canonical().to_string() or "new" if file ~= nil then local document = documents[title] if document == nil then documents[title] = text_area.document else text_area.document = document end if file.is_file() then text_area.text = file.read_text() text_area.document.clear_unedited() end end text_area.set_selection(0) local find_panel = make_find_panel(window) local frame = new_frame{ preferred_size = { width=700, height=700 } content_pane = new_panel{ layout = new_mig_layout("insets 0,wrap,fill,hidemode 3","","[][grow 0]") children = { new_scroll_pane{ constraints = "grow" view = text_area row_header_view = new_text_area_line_numbers{ text_area = text_area foreground_color = int_to_color(0x888888) border = create_empty_border(0,8,0,8) } } find_panel } } } window.frame = frame frame.add_close_listener(function() n_windows = n_windows - 1 if n_windows == 0 then Luan.exit() end end) local function set_title() local s = title if not text_area.document.is_unedited() then s = s.." *" end frame.title = s end set_title() window.set_title = set_title -- dont gc text_area.document.add_undo_listener(set_title) function window.open() local file_chooser = frame.file_chooser_load() if file ~= nil then file_chooser.directory = file.parent() end file_chooser.visible = true local new_file = file_chooser.file if new_file ~= nil then new_window(new_file) end end function window.save() if file == nil then local file_chooser = frame.file_chooser_save() file_chooser.visible = true file = file_chooser.file if file == nil then return false end title = file.canonical().to_string() frame.title = title documents[title] = text_area.document end file.write_text(text_area.text) text_area.document.set_unedited() return true end function window.revert() local selection = text_area.get_selection() local text = file.read_text() text_area.text = text text_area.set_selection(min(selection,#text+1)) text_area.document.set_unedited() end local function selection_lines() local start_seletion, end_selection = text_area.get_selection() local end_ = end_selection == start_seletion and end_selection or end_selection - 1 local start_line = text_area.get_line_from_position(start_seletion) local end_line = text_area.get_line_from_position(end_) local start_pos = text_area.get_line_start_position(start_line) local end_pos = text_area.get_line_end_position(end_line) local text = text_area.text text = sub_string(text,start_pos,end_pos-2) return { text = text start_pos = start_pos length = #text lines = end_line - start_line + 1 start_seletion = start_seletion end_selection = end_selection } end function window.indent() local r = selection_lines() local text = r.text local start_pos = r.start_pos text = "\t"..replace(text,"\n","\n\t") text_area.replace(start_pos,r.length,text) --logger.info(stringify{text_area.get_selection()}) text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines ) end function window.unindent() local r = selection_lines() local text = r.text text = "\n"..text local start_seletion = r.start_seletion if starts_with(text,"\n\t") then start_seletion = start_seletion - 1 end local len1 = #text text = replace(text,"\n\t","\n") local len2 = #text local end_selection = r.end_selection - (len1 - len2) text = sub_string(text,2) text_area.replace(r.start_pos,r.length,text) text_area.set_selection(start_seletion,end_selection) end function window.cursor_column() local cursor_pos = text_area.get_selection() local line = text_area.get_line_from_position(cursor_pos) local start_line_pos = text_area.get_line_start_position(line) return cursor_pos - start_line_pos + 1 end function window.goto(line) local pos = text_area.get_line_start_position(line) text_area.set_selection(pos) end add_menu_bar(window) frame.pack() frame.visible = true text_area.request_focus_in_window() n_windows = n_windows + 1 end Swing.run(function() local args = Luan.arg if #args == 0 then new_window() else for _, arg in ipairs(args) do local file = new_file(arg) new_window(file) end end end)