Mercurial Hosting > luan
changeset 1902:9f07d69551d6 default tip
split up editor code
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 15 Apr 2025 13:31:51 -0600 |
parents | 80ca91007f15 |
children | |
files | src/luan/modules/editor/editor.luan src/luan/modules/editor/find.luan src/luan/modules/editor/menu.luan src/luan/modules/editor/window.luan |
diffstat | 4 files changed, 602 insertions(+), 589 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/modules/editor/editor.luan Tue Apr 15 12:45:26 2025 -0600 +++ b/src/luan/modules/editor/editor.luan Tue Apr 15 13:31:51 2025 -0600 @@ -1,599 +1,11 @@ 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 regex = String.regex or error() -local regex_quote = String.regex_quote or error() -local regex_quote_replacement = String.regex_quote_replacement or error() -local repeated = String.repeated 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_check_box = require("luan:swing/Check_box.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 find_menu_item = new_check_box_menu_item{ - text = "Find and Replace" - accelerator = "meta F" - action_listener = function(event) - window.show_find_panel(event.source.state) - end - } - window.find_menu_item = find_menu_item - - 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) - } - } - } - new_menu{ - text = "Find" - menu_items = { - find_menu_item - new_menu_item{ - text = "Find Case Insensitive" - action_listener = window.find_case_insensitive - } - new_menu_item{ - text = "Convert Leading Tabs to Spaces" - action_listener = window.tabs_to_spaces - } - new_menu_item{ - text = "Convert Leading Spaces to Tabs" - action_listener = window.spaces_to_tabs - } - } - } - 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 r = regex(s) - local matches = {} - local i = 1 - local n = #text - while i <= n do - local j1, j2 = r.find(text,i) - if j1 == nil then - break - end - j2 = j2 + 1 - if j1 == j2 then - i = j2 + 1 - else - matches[#matches+1] = { start=j1, end_=j2 } - i = j2 - end - end - return matches -end +local new_window = require "luan:editor/window.luan" -local function make_find_panel(window) - local text_area = window.text_area - local find_field, replace_field, regex_check_box, output - local function find_match(event) - --logger.info("action "..event.action) - local s = find_field.text - if #s == 0 then - output.text = "" - return - end - if not regex_check_box.is_selected then - s = regex_quote(s) - end - local matches - try - matches = get_matches( text_area.text, s ) - catch e - output.text = "Regex error: "..e.get_message() - 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 - local function replace_match(event) - local find = find_field.text - if #find == 0 then - output.text = "" - return - end - local replace = replace_field.text - if not regex_check_box.is_selected then - find = regex_quote(find) - replace = regex_quote_replacement(replace) - end - local r - try - r = regex(find) - catch e - output.text = "Regex error: "..e.get_message() - return - end - local new, n - local action = event.action - if action == "replace" then - local selection = text_area.selected_text - new, n = r.gsub(selection,replace) - if n > 0 then - text_area.selected_text = new - end - elseif action == "replace_all" then - local text = text_area.text - new, n = r.gsub(text,replace) - if n > 0 then - text_area.text = new - end - else - error(action) - end - output.text = n.." replacements" - end - find_field = new_text_field{ - constraints = "growx" - show_whitespace = true - action = "next" - action_listener = find_match - } - replace_field = new_text_field{ - constraints = "growx" - show_whitespace = true - action = "replace" - action_listener = replace_match - } - regex_check_box = new_check_box{ - text = "Use Regex" - } - output = new_label{ - constraints = "span" - } - 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_button{ - constraints = "grow" - text = "Find Next" - action = "next" - action_listener = find_match - } - new_button{ - constraints = "grow,wrap" - text = "Find Previous" - action = "previous" - action_listener = find_match - } - new_label{ - constraints = "right" - text = "Replace:" - } - replace_field - new_button{ - constraints = "grow" - text = "Replace" - tool_tip_text = "Replace matches in selected text" - action = "replace" - action_listener = replace_match - } - new_button{ - constraints = "grow,wrap" - text = "Replace All" - action = "replace_all" - action_listener = replace_match - } - new_panel{ - constraints = "span,wrap" - layout = new_mig_layout("insets 0,gap 16px") - children = { - regex_check_box - new_button{ - text = "Learn About Regular Expressions" - } - } - } - output - } - } - function window.show_find_panel(visible) - find_panel.visible = visible - if visible then - find_field.request_focus_in_window() - end - end - function window.find_case_insensitive(_) - find_panel.visible = true - window.find_menu_item.is_selected = true - regex_check_box.is_selected = true - find_field.text = "(?i)\Q\E" - find_field.set_selection(7) - find_field.request_focus_in_window() - output.text = [[Put search text between "\Q" and "\E"]] - end - function window.tabs_to_spaces(_) - find_panel.visible = true - window.find_menu_item.is_selected = true - regex_check_box.is_selected = true - find_field.text = [[(?m)^(\t*)\t]] - local spaces = repeated( " ", text_area.tab_size ) - replace_field.text = "$1"..spaces - output.text = [[Do "Replace All" until 0 replacements]] - end - function window.spaces_to_tabs(_) - find_panel.visible = true - window.find_menu_item.is_selected = true - regex_check_box.is_selected = true - local tab_size = text_area.tab_size - find_field.text = `%>(?m)^(( {<%=tab_size%>})*) {<%=tab_size%>}<%` - replace_field.text = "$1\t" - output.text = [[Do "Replace All" until 0 replacements]] - 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/editor/find.luan Tue Apr 15 13:31:51 2025 -0600 @@ -0,0 +1,239 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local ipairs = Luan.ipairs or error() +local range = Luan.range or error() +local String = require "luan:String.luan" +local regex = String.regex or error() +local regex_quote = String.regex_quote or error() +local regex_quote_replacement = String.regex_quote_replacement or error() +local repeated = String.repeated or error() +local new_text_field = require("luan:swing/Text_field.luan").new or error() +local new_check_box = require("luan:swing/Check_box.luan").new or error() +local new_label = require("luan:swing/Label.luan").new or error() +local new_panel = require("luan:swing/Component.luan").new_panel or error() +local Layout = require "luan:swing/Layout.luan" +local new_mig_layout = Layout.new_mig_layout or error() +local new_button = require("luan:swing/Button.luan").new or error() + + +local function get_matches(text,s) + local r = regex(s) + local matches = {} + local i = 1 + local n = #text + while i <= n do + local j1, j2 = r.find(text,i) + if j1 == nil then + break + end + j2 = j2 + 1 + if j1 == j2 then + i = j2 + 1 + else + matches[#matches+1] = { start=j1, end_=j2 } + i = j2 + end + end + return matches +end + +local function make_find_panel(window) + local text_area = window.text_area + local find_field, replace_field, regex_check_box, output + local function find_match(event) + --logger.info("action "..event.action) + local s = find_field.text + if #s == 0 then + output.text = "" + return + end + if not regex_check_box.is_selected then + s = regex_quote(s) + end + local matches + try + matches = get_matches( text_area.text, s ) + catch e + output.text = "Regex error: "..e.get_message() + 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 + local function replace_match(event) + local find = find_field.text + if #find == 0 then + output.text = "" + return + end + local replace = replace_field.text + if not regex_check_box.is_selected then + find = regex_quote(find) + replace = regex_quote_replacement(replace) + end + local r + try + r = regex(find) + catch e + output.text = "Regex error: "..e.get_message() + return + end + local new, n + local action = event.action + if action == "replace" then + local selection = text_area.selected_text + new, n = r.gsub(selection,replace) + if n > 0 then + text_area.selected_text = new + end + elseif action == "replace_all" then + local text = text_area.text + new, n = r.gsub(text,replace) + if n > 0 then + text_area.text = new + end + else + error(action) + end + output.text = n.." replacements" + end + find_field = new_text_field{ + constraints = "growx" + show_whitespace = true + action = "next" + action_listener = find_match + } + replace_field = new_text_field{ + constraints = "growx" + show_whitespace = true + action = "replace" + action_listener = replace_match + } + regex_check_box = new_check_box{ + text = "Use Regex" + } + output = new_label{ + constraints = "span" + } + 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_button{ + constraints = "grow" + text = "Find Next" + action = "next" + action_listener = find_match + } + new_button{ + constraints = "grow,wrap" + text = "Find Previous" + action = "previous" + action_listener = find_match + } + new_label{ + constraints = "right" + text = "Replace:" + } + replace_field + new_button{ + constraints = "grow" + text = "Replace" + tool_tip_text = "Replace matches in selected text" + action = "replace" + action_listener = replace_match + } + new_button{ + constraints = "grow,wrap" + text = "Replace All" + action = "replace_all" + action_listener = replace_match + } + new_panel{ + constraints = "span,wrap" + layout = new_mig_layout("insets 0,gap 16px") + children = { + regex_check_box + new_button{ + text = "Learn About Regular Expressions" + } + } + } + output + } + } + function window.show_find_panel(visible) + find_panel.visible = visible + if visible then + find_field.request_focus_in_window() + end + end + function window.find_case_insensitive(_) + find_panel.visible = true + window.find_menu_item.is_selected = true + regex_check_box.is_selected = true + find_field.text = "(?i)\Q\E" + find_field.set_selection(7) + find_field.request_focus_in_window() + output.text = [[Put search text between "\Q" and "\E"]] + end + function window.tabs_to_spaces(_) + find_panel.visible = true + window.find_menu_item.is_selected = true + regex_check_box.is_selected = true + find_field.text = [[(?m)^(\t*)\t]] + local spaces = repeated( " ", text_area.tab_size ) + replace_field.text = "$1"..spaces + output.text = [[Do "Replace All" until 0 replacements]] + end + function window.spaces_to_tabs(_) + find_panel.visible = true + window.find_menu_item.is_selected = true + regex_check_box.is_selected = true + local tab_size = text_area.tab_size + find_field.text = `%>(?m)^(( {<%=tab_size%>})*) {<%=tab_size%>}<%` + replace_field.text = "$1\t" + output.text = [[Do "Replace All" until 0 replacements]] + end + return find_panel +end + +return make_find_panel
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/editor/menu.luan Tue Apr 15 13:31:51 2025 -0600 @@ -0,0 +1,181 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local String = require "luan:String.luan" +local to_number = String.to_number 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 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 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 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 find_menu_item = new_check_box_menu_item{ + text = "Find and Replace" + accelerator = "meta F" + action_listener = function(event) + window.show_find_panel(event.source.state) + end + } + window.find_menu_item = find_menu_item + + local menu_bar = new_menu_bar{ + menus = { + new_menu{ + text = "File" + menu_items = { + new_menu_item{ + text = "New File" + accelerator = "meta N" + action_listener = action_listener(window.new) + } + 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) + } + } + } + new_menu{ + text = "Find" + menu_items = { + find_menu_item + new_menu_item{ + text = "Find Case Insensitive" + action_listener = window.find_case_insensitive + } + new_menu_item{ + text = "Convert Leading Tabs to Spaces" + action_listener = window.tabs_to_spaces + } + new_menu_item{ + text = "Convert Leading Spaces to Tabs" + action_listener = window.spaces_to_tabs + } + } + } + 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 + +return add_menu_bar
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/editor/window.luan Tue Apr 15 13:31:51 2025 -0600 @@ -0,0 +1,181 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.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 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_panel = require("luan:swing/Component.luan").new_panel 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 make_find_panel = require "luan:editor/find.luan" +local add_menu_bar = require "luan:editor/menu.luan" + + +local n_windows = 0 +local documents = {} + +local 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) + window.new = new_window + 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 + +return new_window