Mercurial Hosting > luan
changeset 1918:f3fd0f43e80a
spell check
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 19 Apr 2025 21:23:47 -0600 |
parents | f5f4c79d375f |
children | 4fe0efb14099 |
files | lib/jortho.jar src/luan/modules/editor/menu.luan src/luan/modules/swing/SpellCheckerLuan.java src/luan/modules/swing/Text_component.luan website/src/dictionaries/dictionaries.cnf website/src/dictionaries/dictionary_en.ortho |
diffstat | 6 files changed, 66 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/modules/editor/menu.luan Fri Apr 18 19:17:23 2025 -0600 +++ b/src/luan/modules/editor/menu.luan Sat Apr 19 21:23:47 2025 -0600 @@ -11,6 +11,8 @@ 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 Text_component = require "luan:swing/Text_component.luan" +local has_spell_checker = not not Text_component.has_spell_checker local function action_listener(fn) @@ -166,11 +168,20 @@ } 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" + enabled = has_spell_checker + action_listener = function(event) + text_area.spell_check(event.source.state) + end + } new_menu_item{ text = "Cursor Column" accelerator = "meta B"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/swing/SpellCheckerLuan.java Sat Apr 19 21:23:47 2025 -0600 @@ -0,0 +1,36 @@ +package luan.modules.swing; + +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); + } +}
--- a/src/luan/modules/swing/Text_component.luan Fri Apr 18 19:17:23 2025 -0600 +++ b/src/luan/modules/swing/Text_component.luan Sat Apr 19 21:23:47 2025 -0600 @@ -13,12 +13,21 @@ require "java" local SwingLuan = require "java:luan.modules.swing.SwingLuan" local fixTextComponent = SwingLuan.fixTextComponent +local SpellCheckerLuan = require "java:luan.modules.swing.SpellCheckerLuan" local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "swing/Text_component" local Text_component = {} +try + SpellCheckerLuan.registerDictionaries( "https://www.luan.software/dictionaries/", "en" ) + Text_component.has_spell_checker = true +catch e + logger.error("Spell checker disabled: "..e.get_message()) + Text_component.has_spell_checker = false +end + function Text_component.__index(component,key) local rtn = super__index(component,key) if rtn ~= fail then return rtn end @@ -87,6 +96,14 @@ jcomponent.select(start_pos-1,end_pos-1) end component.print = jcomponent.print + function component.spell_check(spell_check) + Text_component.has_spell_checker or error "spell_checker disabled" + if spell_check then + SpellCheckerLuan.register(jcomponent) + else + SpellCheckerLuan.unregister(jcomponent) + end + end return component end