Mercurial Hosting > editor
changeset 56:6059b4e22d47 default tip
almost fix to front
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 28 May 2025 16:02:27 -0600 |
parents | 77ee3a37475a |
children | |
files | src/luan_editor/Window.luan src/luan_editor/editor.luan src/luan_editor/menu.luan src/luan_editor/window.luan |
diffstat | 4 files changed, 402 insertions(+), 387 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan_editor/Window.luan Wed May 28 16:02:27 2025 -0600 @@ -0,0 +1,388 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local stringify = Luan.stringify or error() +local ipairs = Luan.ipairs or error() +local Parsers = require "luan:Parsers.luan" +local json_string = Parsers.json_string or error() +local json_parse = Parsers.json_parse or error() +local Math = require "luan:Math.luan" +local min = Math.min 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 Io = require "luan:Io.luan" +local new_file = Io.schemes.file or error() +local new_text_area = require("luan:swing/Text_area.luan").new or error() +local new_frame = require("luan:swing/Frame.luan").new 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_list = require("luan:swing/List.luan").new or error() +local Layout = require "luan:swing/Layout.luan" +local new_mig_layout = Layout.new_mig_layout 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 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 new_label = require("luan:swing/Label.luan").new or error() +local make_find_panel = require "classpath:luan_editor/find.luan" +local Swing = require "luan:swing/Swing.luan" +local run_later = Swing.run_later or error() +local File_chooser = require "luan:swing/File_chooser.luan" +local choose_file = File_chooser.awt_choose_file or error() +local Option_pane = require "luan:swing/Option_pane.luan" +local show_message_dialog = Option_pane.show_message_dialog or error() +local Clipboard = require "luan:swing/Clipboard.luan" +local Java = require "classpath:luan_editor/Java.luan" +local Logging = require "luan:logging/Logging.luan" +local logger = Logging.logger "editor/Window" + + +local Window = {} + +local n_windows = 0 +local documents = {} + +local function bool(val,default) + if val ~= nil then + return val + else + return default + end +end + +local config_file = Io.uri("file:"..Java.home_dir.."/.luan_editor/config.json") +local config = {} +if config_file.exists() then + try + config = json_parse(config_file.read_text()) + catch e + logger.error(e) + end +end +config.size = config.size or { width=700, height=700 } +config.line_wrap = bool( config.line_wrap, true ) +config.whitespace_visible = bool( config.whitespace_visible, false ) +config.tab_size = config.tab_size or 4 +config.list_window = config.list_window or {} +config.list_window.size = config.list_window.size or { width=200, height=400 } + +local function save_config() + config_file.write_text( json_string(config).."\n" ) +end + +local list_view = new_list{ + --layout = new_mig_layout("insets 0,wrap,fill") + --layout = new_mig_layout("wrap","[grow]") + horizontal_alignment = "right" + hover_background = int_to_color(0xEEEEEE) +} +local list_scroll_pane = new_scroll_pane{ + view = list_view +} +local list_window = new_dialog{ + size = config.list_window.size + content_pane = list_scroll_pane + focusable_window_state = false +} +list_window.add_resize_stopped_listener( 200, function() + --logger.info(stringify(list_window.size)) + config.list_window.size = list_window.size + save_config() +end) +list_window.add_move_stopped_listener( 200, function() + --logger.info(stringify(list_window.location)) + config.list_window.location = list_window.location + save_config() +end) +function Window.show_list_window() + local location = config.list_window.location + if location ~= nil then + list_window.location = location + end + list_window.visible = true + list_scroll_pane.scroll_to_right() +end + +local function add_list_window_item(item) + list_view.add_element(item) + list_scroll_pane.scroll_to_right() +end +local function remove_list_window_item(item) + list_view.remove_element(item) --or error() +end +list_view.add_list_selection_listener( function(item) + if item ~= nil then + item.window.text_area.request_focus() + end +end ) + +local black = int_to_color(0x000000) +local dark_blue = int_to_color(0x0000C0) +local grey = int_to_color(0x888888) + +local function new_window(file,document) + local window = {} + if file == nil or not file.exists() then + window.has_file = false + elseif file.is_file() then + window.has_file = true + else + show_message_dialog(nil,"Not a file") + if n_windows == 0 then + Luan.exit() + else + return + end + end + window.has_file = file~=nil and file.exists() + local text_area = new_text_area{ + wrap_style_word = true + line_wrap = config.line_wrap + whitespace_visible = config.whitespace_visible + tab_size = config.tab_size + font = { family="Monospaced", size=13 } + } + window.text_area = text_area + local title = file and file.canonical().to_string() or "new" + if document ~= nil then + text_area.document = document + elseif file ~= nil then + local document = documents[title] + if document ~= nil then + text_area.document = document + else + documents[title] = text_area.document + if file.exists() then + text_area.text = file.read_text() + text_area.document.clear_unedited() + end + end + end + text_area.set_selection(0) + local list_window_item = { + window = window + } + add_list_window_item(list_window_item) + local status_bar = new_label{ + constraints = "span,growx" + text = " " + border = create_empty_border(2,16,4,16) + } + window.status_bar = status_bar + local find_panel = make_find_panel(window) + local frame = new_frame{ + preferred_size = config.size + 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 = grey + border = create_empty_border(0,8,0,8) + } + } + find_panel + status_bar + } + } + } + window.frame = frame + frame.add_close_listener(function() + n_windows = n_windows - 1 + if n_windows == 0 then + Luan.exit() + end + remove_list_window_item(list_window_item) + end) + frame.add_window_focus_listener(function() + list_view.selected_value = list_window_item + end) + frame.add_resize_stopped_listener( 200, function() + --logger.info(stringify(frame.size)) + config.size = frame.size + save_config() + end) + frame.add_move_stopped_listener( 200, function() + --logger.info(stringify(frame.location)) + config.location = frame.location + save_config() + end) + local function undo_listener() + local is_unedited = text_area.document.is_unedited() + if window.is_unedited == is_unedited then + return + end + window.is_unedited = is_unedited + local s = title + if not is_unedited then + s = s.." *" + end + frame.title = s + list_window_item.text = title + list_window_item.foreground_color = is_unedited and black or dark_blue + list_view.repaint(list_window_item) + end + undo_listener() + --window.undo_listener = undo_listener -- dont gc + text_area.document.add_undo_listener(undo_listener) + window.new = new_window + function window.title() + return title + end + function window.open() + local new_file = choose_file{ + action = "load" + parent = frame + directory = file and file.parent() + } + if new_file ~= nil then + new_window(new_file) + end + end + function window.save() + if file == nil then + file = choose_file{ + action = "save" + parent = frame + } + if file == nil then + return false + end + title = file.canonical().to_string() + documents[title] = text_area.document + window.is_unedited = nil + undo_listener() + end + try + file.write_text(text_area.text) + catch e + show_message_dialog( frame, e.get_message() ) + return false + end + 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() + status_bar.text = "Reverted" + 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 + function window.set_line_wrap(line_wrap) + text_area.line_wrap = line_wrap + config.line_wrap = line_wrap + save_config() + end + function window.set_whitespace_visible(whitespace_visible) + text_area.whitespace_visible = whitespace_visible + config.whitespace_visible = whitespace_visible + save_config() + end + function window.set_tab_size(tab_size) + text_area.tab_size = tab_size + config.tab_size = tab_size + save_config() + end + function window.duplicate() + local new = new_window(file,text_area.document) + new.text_area.set_selection( text_area.get_selection() ) + end + function window.paste_files() + --logger.info("paste_files "..stringify(Clipboard.get_files())) + local files = Clipboard.get_files() + if files == nil then + return false + end + for _, file in ipairs(files) do + new_window(file) + end + return true + end + local add_menu_bar = require "classpath:luan_editor/menu.luan" + add_menu_bar(window) + frame.pack() + local location = config.location + if location ~= nil then + frame.location = location + end + frame.visible = true + text_area.request_focus_in_window() + n_windows = n_windows + 1 + return window +end +Window.new_window = new_window + +function Window.open_windows(file_paths) + list_window.to_front() + for _, file_path in ipairs(file_paths) do + local file = new_file(file_path) + new_window(file) + end + if #file_paths == 0 then + local window = list_view.selected_value.window + window.frame.to_front() + window.text_area.request_focus_in_window() + end +end + +return Window
--- a/src/luan_editor/editor.luan Tue May 27 22:31:32 2025 -0600 +++ b/src/luan_editor/editor.luan Wed May 28 16:02:27 2025 -0600 @@ -9,8 +9,10 @@ local Rpc = require "luan:Rpc.luan" local Swing = require "luan:swing/Swing.luan" local swing_run = Swing.run or error() -local to_front = Swing.to_front or error() -local new_window = require "classpath:luan_editor/window.luan" +local swing_run_later = Swing.run_later or error() +local Window = require "classpath:luan_editor/Window.luan" +local new_window = Window.new_window or error() +local open_windows = Window.open_windows or error() local Java = require "classpath:luan_editor/Java.luan" local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "editor/editor" @@ -34,25 +36,18 @@ end local host = Rpc.remote("localhost",port) local args = Luan.arg + local file_paths = {} for _, arg in ipairs(args) do local file = new_file(arg) file = file.canonical().to_string() - host.open(file) + file_paths[#file_paths+1] = file end - host.to_front() + host.open_windows(file_paths) host.close() return end -function Rpc.functions.open(file_path) - swing_run(function() - local file = new_file(file_path) - new_window(file) - end) -end -function Rpc.functions.to_front() - swing_run(to_front) -end +Rpc.functions.open_windows = open_windows swing_run(function() local args = Luan.arg @@ -66,4 +61,6 @@ end end) -Rpc.serve_socket(server_socket,nil,false) +Rpc.serve_socket(server_socket,nil,function(socket,fns) + swing_run_later(Rpc.new_server_fn(socket,fns)) +end)
--- a/src/luan_editor/menu.luan Tue May 27 22:31:32 2025 -0600 +++ b/src/luan_editor/menu.luan Wed May 28 16:02:27 2025 -0600 @@ -16,6 +16,8 @@ local get_all_frames = Frame.get_all_frames or error() local Spell_checker = require "classpath:luan_editor/Spell_checker.luan" local spell_check = Spell_checker.spell_check or error() +local Window = require "classpath:luan_editor/Window.luan" +local show_list_window = Window.show_list_window or error() local function action_listener(fn) @@ -265,7 +267,7 @@ new_menu_item{ text = "List Windows" accelerator = "meta L" - action_listener = action_listener(window.show_list_window) + action_listener = action_listener(show_list_window) } } }
--- a/src/luan_editor/window.luan Tue May 27 22:31:32 2025 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,372 +0,0 @@ -local Luan = require "luan:Luan.luan" -local error = Luan.error -local stringify = Luan.stringify or error() -local ipairs = Luan.ipairs or error() -local Parsers = require "luan:Parsers.luan" -local json_string = Parsers.json_string or error() -local json_parse = Parsers.json_parse or error() -local Math = require "luan:Math.luan" -local min = Math.min 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 Io = require "luan:Io.luan" -local new_text_area = require("luan:swing/Text_area.luan").new or error() -local new_frame = require("luan:swing/Frame.luan").new 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_list = require("luan:swing/List.luan").new or error() -local Layout = require "luan:swing/Layout.luan" -local new_mig_layout = Layout.new_mig_layout 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 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 new_label = require("luan:swing/Label.luan").new or error() -local make_find_panel = require "classpath:luan_editor/find.luan" -local add_menu_bar = require "classpath:luan_editor/menu.luan" -local Swing = require "luan:swing/Swing.luan" -local run_later = Swing.run_later or error() -local File_chooser = require "luan:swing/File_chooser.luan" -local choose_file = File_chooser.awt_choose_file or error() -local Option_pane = require "luan:swing/Option_pane.luan" -local show_message_dialog = Option_pane.show_message_dialog or error() -local Clipboard = require "luan:swing/Clipboard.luan" -local Java = require "classpath:luan_editor/Java.luan" -local Logging = require "luan:logging/Logging.luan" -local logger = Logging.logger "editor/window" - - -local n_windows = 0 -local documents = {} - -local function bool(val,default) - if val ~= nil then - return val - else - return default - end -end - -local config_file = Io.uri("file:"..Java.home_dir.."/.luan_editor/config.json") -local config = {} -if config_file.exists() then - try - config = json_parse(config_file.read_text()) - catch e - logger.error(e) - end -end -config.size = config.size or { width=700, height=700 } -config.line_wrap = bool( config.line_wrap, true ) -config.whitespace_visible = bool( config.whitespace_visible, false ) -config.tab_size = config.tab_size or 4 -config.list_window = config.list_window or {} -config.list_window.size = config.list_window.size or { width=200, height=400 } - -local function save_config() - config_file.write_text( json_string(config).."\n" ) -end - -local list_view = new_list{ - --layout = new_mig_layout("insets 0,wrap,fill") - --layout = new_mig_layout("wrap","[grow]") - horizontal_alignment = "right" - hover_background = int_to_color(0xEEEEEE) -} -local list_scroll_pane = new_scroll_pane{ - view = list_view -} -local list_window = new_dialog{ - size = config.list_window.size - content_pane = list_scroll_pane - focusable_window_state = false -} -list_window.add_resize_stopped_listener( 200, function() - --logger.info(stringify(list_window.size)) - config.list_window.size = list_window.size - save_config() -end) -list_window.add_move_stopped_listener( 200, function() - --logger.info(stringify(list_window.location)) - config.list_window.location = list_window.location - save_config() -end) -local function show_list_window() - local location = config.list_window.location - if location ~= nil then - list_window.location = location - end - list_window.visible = true - list_scroll_pane.scroll_to_right() -end - -local function add_list_window_item(item) - list_view.add_element(item) - list_scroll_pane.scroll_to_right() -end -local function remove_list_window_item(item) - list_view.remove_element(item) --or error() -end -list_view.add_list_selection_listener( function(item) - if item ~= nil then - item.window.text_area.request_focus() - end -end ) - -local black = int_to_color(0x000000) -local dark_blue = int_to_color(0x0000C0) -local grey = int_to_color(0x888888) - -local function new_window(file,document) - local window = {} - if file == nil or not file.exists() then - window.has_file = false - elseif file.is_file() then - window.has_file = true - else - show_message_dialog(nil,"Not a file") - if n_windows == 0 then - Luan.exit() - else - return - end - end - window.has_file = file~=nil and file.exists() - local text_area = new_text_area{ - wrap_style_word = true - line_wrap = config.line_wrap - whitespace_visible = config.whitespace_visible - tab_size = config.tab_size - font = { family="Monospaced", size=13 } - } - window.text_area = text_area - local title = file and file.canonical().to_string() or "new" - if document ~= nil then - text_area.document = document - elseif file ~= nil then - local document = documents[title] - if document ~= nil then - text_area.document = document - else - documents[title] = text_area.document - if file.exists() then - text_area.text = file.read_text() - text_area.document.clear_unedited() - end - end - end - text_area.set_selection(0) - local list_window_item = { - window = window - } - add_list_window_item(list_window_item) - local status_bar = new_label{ - constraints = "span,growx" - text = " " - border = create_empty_border(2,16,4,16) - } - window.status_bar = status_bar - local find_panel = make_find_panel(window) - local frame = new_frame{ - preferred_size = config.size - 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 = grey - border = create_empty_border(0,8,0,8) - } - } - find_panel - status_bar - } - } - } - window.frame = frame - frame.add_close_listener(function() - n_windows = n_windows - 1 - if n_windows == 0 then - Luan.exit() - end - remove_list_window_item(list_window_item) - end) - frame.add_window_focus_listener(function() - list_view.set_selected_value(list_window_item) - end) - frame.add_resize_stopped_listener( 200, function() - --logger.info(stringify(frame.size)) - config.size = frame.size - save_config() - end) - frame.add_move_stopped_listener( 200, function() - --logger.info(stringify(frame.location)) - config.location = frame.location - save_config() - end) - local function undo_listener() - local is_unedited = text_area.document.is_unedited() - if window.is_unedited == is_unedited then - return - end - window.is_unedited = is_unedited - local s = title - if not is_unedited then - s = s.." *" - end - frame.title = s - list_window_item.text = title - list_window_item.foreground_color = is_unedited and black or dark_blue - list_view.repaint(list_window_item) - end - undo_listener() - --window.undo_listener = undo_listener -- dont gc - text_area.document.add_undo_listener(undo_listener) - window.new = new_window - function window.title() - return title - end - function window.open() - local new_file = choose_file{ - action = "load" - parent = frame - directory = file and file.parent() - } - if new_file ~= nil then - new_window(new_file) - end - end - function window.save() - if file == nil then - file = choose_file{ - action = "save" - parent = frame - } - if file == nil then - return false - end - title = file.canonical().to_string() - documents[title] = text_area.document - window.is_unedited = nil - undo_listener() - end - try - file.write_text(text_area.text) - catch e - show_message_dialog( frame, e.get_message() ) - return false - end - 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() - status_bar.text = "Reverted" - 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 - function window.set_line_wrap(line_wrap) - text_area.line_wrap = line_wrap - config.line_wrap = line_wrap - save_config() - end - function window.set_whitespace_visible(whitespace_visible) - text_area.whitespace_visible = whitespace_visible - config.whitespace_visible = whitespace_visible - save_config() - end - function window.set_tab_size(tab_size) - text_area.tab_size = tab_size - config.tab_size = tab_size - save_config() - end - function window.duplicate() - local new = new_window(file,text_area.document) - new.text_area.set_selection( text_area.get_selection() ) - end - window.show_list_window = show_list_window - function window.paste_files() - --logger.info("paste_files "..stringify(Clipboard.get_files())) - local files = Clipboard.get_files() - if files == nil then - return false - end - for _, file in ipairs(files) do - new_window(file) - end - return true - end - add_menu_bar(window) - frame.pack() - local location = config.location - if location ~= nil then - frame.location = location - end - frame.visible = true - text_area.request_focus_in_window() - n_windows = n_windows + 1 - return window -end - -return new_window