Mercurial Hosting > editor
changeset 37:b7ff52d45b9a default tip
copy from luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 21 Apr 2025 13:07:29 -0600 |
parents | 0a8865de3d53 |
children | |
files | .hgignore classpath.sh copy_me.sh dev.sh dictionaries/dictionaries.cnf dictionaries/dictionary_en.ortho dictionaries/dictionary_es.ortho editor.luan lib/jortho.jar luan_editor.sh src/luan_editor/SpellCheckerLuan$1.class src/luan_editor/SpellCheckerLuan.class src/luan_editor/SpellCheckerLuan.java src/luan_editor/Spell_checker.luan src/luan_editor/editor.luan src/luan_editor/find.luan src/luan_editor/menu.luan src/luan_editor/window.luan |
diffstat | 18 files changed, 885 insertions(+), 609 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgignore Mon Apr 14 21:17:04 2025 -0600 +++ b/.hgignore Mon Apr 21 13:07:29 2025 -0600 @@ -2,3 +2,4 @@ mine/ err +Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/classpath.sh Mon Apr 21 13:07:29 2025 -0600 @@ -0,0 +1,6 @@ +. luan string: + +EDITOR_HOME=`pwd` +CLASSPATH=$CLASSPATH:$EDITOR_HOME/lib/jortho.jar:$EDITOR_HOME/src + +export CLASSPATH
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/copy_me.sh Mon Apr 21 13:07:29 2025 -0600 @@ -0,0 +1,6 @@ +#!/bin/bash +# Copy this to somewhere in your PATH, rename to something reasonable like "le", and change EDITOR_HOME here as needed. + +EDITOR_HOME=~/hg/editor + +$EDITOR_HOME/luan_editor.sh "$@"
--- a/dev.sh Mon Apr 14 21:17:04 2025 -0600 +++ b/dev.sh Mon Apr 21 13:07:29 2025 -0600 @@ -1,1 +1,11 @@ -java -Xdock:name="Luan Editor" -classpath $CLASSPATH luan.Luan editor.luan "$@" 2>&1 | grep --line-buffered -v 'openAndSavePanelService' | tee err +#!/bin/bash +# for development + +EDITOR_HOME=`dirname $0` + +export DICTIONARIES=$EDITOR_HOME/dictionaries/ + +. luan string: +CLASSPATH=$CLASSPATH:$EDITOR_HOME/lib/jortho.jar:$EDITOR_HOME/src + +java -Xdock:name="Luan Editor" -classpath $CLASSPATH luan.Luan classpath:luan_editor/editor.luan "$@" 2>&1 | grep --line-buffered -v 'openAndSavePanelService' | tee err
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dictionaries/dictionaries.cnf Mon Apr 21 13:07:29 2025 -0600 @@ -0,0 +1,2 @@ +extension=.ortho +languages=en,es
--- a/editor.luan Mon Apr 14 21:17:04 2025 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,608 +0,0 @@ -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 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 - if #args == 0 then - new_window() - else - for _, arg in ipairs(args) do - local file = new_file(arg) - new_window(file) - end - end -end)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/luan_editor.sh Mon Apr 21 13:07:29 2025 -0600 @@ -0,0 +1,12 @@ +#!/bin/bash + +EDITOR_HOME=`dirname $0` + +export DICTIONARIES=$EDITOR_HOME/dictionaries/ + +. luan string: +CLASSPATH=$CLASSPATH:$EDITOR_HOME/lib/jortho.jar:$EDITOR_HOME/src + +set +m + +java -Xdock:name="Luan Editor" -classpath $CLASSPATH luan.Luan classpath:luan_editor/editor.luan "$@" 2>&1 | grep --line-buffered -v 'openAndSavePanelService' &
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan_editor/SpellCheckerLuan.java Mon Apr 21 13:07:29 2025 -0600 @@ -0,0 +1,36 @@ +package luan_editor; + +import java.net.URL; +import java.net.MalformedURLException; +import javax.swing.text.JTextComponent; +import com.inet.jortho.SpellChecker; +import com.inet.jortho.DefaultMessageHandler; +import luan.LuanException; +import luan.LuanRuntimeException; + + +public class SpellCheckerLuan { + + public static void registerDictionaries( String baseURL, String activeLocale) throws MalformedURLException, LuanException { + SpellChecker.setMessageHandler(new DefaultMessageHandler(null) { + @Override public void handleException(java.lang.Throwable throwable) { + throw new LuanRuntimeException(new LuanException(throwable)); + } + } ); + URL url = new URL(baseURL); + try { + SpellChecker.registerDictionaries( url, activeLocale ); + } catch(LuanRuntimeException e) { + LuanException luanException = (LuanException)e.getCause(); + throw luanException; + } + } + + public static void register(JTextComponent text) { + SpellChecker.register(text); + } + + public static void unregister(JTextComponent text) { + SpellChecker.unregister(text); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan_editor/Spell_checker.luan Mon Apr 21 13:07:29 2025 -0600 @@ -0,0 +1,22 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +require "java" +local System = require "java:java.lang.System" +local SpellCheckerLuan = require "java:luan_editor.SpellCheckerLuan" + + +local Spell_checker = {} + +local dir = System.getenv("DICTIONARIES") or error() +SpellCheckerLuan.registerDictionaries( "file:"..dir, "en" ) + +function Spell_checker.spell_check(text_component,spell_check) + local jtext_component = text_component.java + if spell_check then + SpellCheckerLuan.register(jtext_component) + else + SpellCheckerLuan.unregister(jtext_component) + end +end + +return Spell_checker
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan_editor/editor.luan Mon Apr 21 13:07:29 2025 -0600 @@ -0,0 +1,62 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local ipairs = Luan.ipairs or error() +local String = require "luan:String.luan" +local contains = String.contains or error() +local Io = require "luan:Io.luan" +local new_file = Io.schemes.file or error() +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 Logging = require "luan:logging/Logging.luan" +local logger = Logging.logger "editor/editor" + + +local port = 56587 + +Rpc.cipher_suites = nil +local server_socket +try + server_socket = Rpc.new_server_socket(port) +catch e + --logger.info(e.get_message()) + if not contains( e.get_message(), "java.net.BindException" ) then + e.throw() + end + local host = Rpc.remote("localhost",port) + local args = Luan.arg + for _, arg in ipairs(args) do + local file = new_file(arg) + file = file.canonical().to_string() + host.open(file) + end + host.to_front() + 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 + + +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) + +Rpc.serve_socket(server_socket,nil,false)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan_editor/find.luan Mon Apr 21 13:07:29 2025 -0600 @@ -0,0 +1,244 @@ +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 Swing = require "luan:swing/Swing.luan" +local browse = Swing.browse 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 status_bar = window.status_bar + local find_field, replace_field, regex_check_box + local function find_match(event) + --logger.info("action "..event.action) + local s = find_field.text + if #s == 0 then + status_bar.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 + status_bar.text = "Regex error: "..e.get_message() + return + end + text_area.set_hightlights(matches) + local n_matches = #matches + if n_matches == 0 then + status_bar.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_ ) + status_bar.text = i.." of "..n_matches.." matches" + return + end + end + local match = matches[1] + text_area.set_selection( match.start, match.end_ ) + status_bar.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_ ) + status_bar.text = i.." of "..n_matches.." matches" + return + end + end + local match = matches[n_matches] + text_area.set_selection( match.start, match.end_ ) + status_bar.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 + status_bar.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 + status_bar.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 + status_bar.text = n.." replacements" + end + find_field = new_text_field{ + constraints = "growx" + whitespace_visible = true + action = "next" + action_listener = find_match + } + replace_field = new_text_field{ + constraints = "growx" + whitespace_visible = true + action = "replace" + action_listener = replace_match + } + regex_check_box = new_check_box{ + text = "Use Regex" + } + local find_panel = new_panel{ + constraints = "growy 0,growx" + layout = new_mig_layout("insets 8 16 0 16","[][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" + action_listener = function(_) + browse("https://www.reactionary.software/learn.html#regex") + end + } + } + } + } + } + function window.show_find_panel(visible) + find_panel.visible = visible + if visible then + find_field.request_focus_in_window() + else + text_area.clear_hightlights() + 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() + status_bar.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 + status_bar.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" + status_bar.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_editor/menu.luan Mon Apr 21 13:07:29 2025 -0600 @@ -0,0 +1,231 @@ +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 Spell_checker = require "classpath:luan_editor/Spell_checker.luan" +local spell_check = Spell_checker.spell_check or error() + + +local function action_listener(fn) + return function(_) + fn() + end +end + +local function add_menu_bar(window) + local text_area = window.text_area + local document = text_area.document + local status_bar = window.status_bar + 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 + } + new_menu_item{ + text = "Print" + action_listener = action_listener(text_area.print) + } + revert +--[[ + new_menu_item{ + text = "Test" + action_listener = function(_) + local location = window.frame.location + location.y = location.y - 20 + window.frame.location = location + end + } +]] + } + } + new_menu{ + text = "Edit" + menu_items = { + undo + redo + separator + new_menu_item{ + text = "Cut" + accelerator = "meta X" + action_listener = action_listener(text_area.cut) + } + new_menu_item{ + text = "Copy" + accelerator = "meta C" + action_listener = action_listener(text_area.copy) + } + new_menu_item{ + text = "Paste" + accelerator = "meta V" + action_listener = action_listener(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(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 = text_area.line_wrap + action_listener = function(event) + window.set_line_wrap(event.source.state) + end + } + new_check_box_menu_item{ + text = "Show Whitespace" + accelerator = "meta W" + state = text_area.whitespace_visible + action_listener = function(event) + window.set_whitespace_visible(event.source.state) + end + } + new_check_box_menu_item{ + text = "Spell Check" + accelerator = "meta SEMICOLON" + action_listener = function(event) + spell_check(text_area,event.source.state) + end + } + new_menu_item{ + text = "Cursor Column" + accelerator = "meta B" + action_listener = function(_) + status_bar.text = "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" ) + if input == nil then + return + end + local line = to_number(input) + try + window.goto(line) + status_bar.text = "Went to line "..line + catch e + status_bar.text = "Invalid line: "..input + end + end + } + new_menu_item{ + text = "Tab Size" + action_listener = function(_) + local input = show_input_dialog( window.frame, "Tab size", text_area.tab_size ) + if input == nil then + return + end + local size = to_number(input) + try + window.set_tab_size(size) + status_bar.text = "Set tab size to "..size + catch e + status_bar.text = "Invalid tab size: "..input + 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_editor/window.luan Mon Apr 21 13:07:29 2025 -0600 @@ -0,0 +1,252 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local stringify = Luan.stringify 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_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 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 File_chooser = require "luan:swing/File_chooser.luan" +local choose_file = File_chooser.awt_choose_file or error() +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:"..Swing.home_dir.."/.luan_editor") +local config = {} +if config_file.exists() then + try + config = Luan.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 + +local function save_config() + config_file.write_text( stringify(config).."\n" ) +end + +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 = 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 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 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 = int_to_color(0x888888) + 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 + 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 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 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() + 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() + 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 + add_menu_bar(window) + frame.pack() + if config.location ~= nil then + frame.location = config.location + end + frame.visible = true + text_area.request_focus_in_window() + n_windows = n_windows + 1 +end + +return new_window