author | wenzelm |
Wed, 09 Nov 2022 19:42:21 +0100 | |
changeset 76492 | e228be7cd375 |
parent 76391 | 6129e8cb140d |
child 76502 | 08b950ca0313 |
permissions | -rw-r--r-- |
65139 | 1 |
/* Title: Tools/jEdit/src/jedit_spell_checker.scala |
56547 | 2 |
Author: Makarius |
3 |
||
65139 | 4 |
Specific spell-checker support for Isabelle/jEdit. |
56547 | 5 |
*/ |
6 |
||
7 |
package isabelle.jedit |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
58549 | 12 |
import javax.swing.JMenuItem |
56547 | 13 |
|
58549 | 14 |
import org.gjt.sp.jedit.menu.EnhancedMenuItem |
65139 | 15 |
import org.gjt.sp.jedit.jEdit |
58549 | 16 |
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea} |
56574
2b38472a4695
some actions to maintain spell-checker dictionary;
wenzelm
parents:
56573
diff
changeset
|
17 |
|
56547 | 18 |
|
75393 | 19 |
object JEdit_Spell_Checker { |
65139 | 20 |
/* completion */ |
58549 | 21 |
|
66119 | 22 |
def completion( |
75393 | 23 |
rendering: JEdit_Rendering, |
24 |
explicit: Boolean, |
|
25 |
caret: Text.Offset |
|
26 |
): Option[Completion.Result] = { |
|
66119 | 27 |
PIDE.plugin.spell_checker.get match { |
28 |
case Some(spell_checker) if explicit => |
|
29 |
spell_checker.completion(rendering, caret) |
|
30 |
case _ => None |
|
31 |
} |
|
58549 | 32 |
} |
33 |
||
34 |
||
65139 | 35 |
/* context menu */ |
58549 | 36 |
|
75393 | 37 |
def context_menu(text_area: JEditTextArea, offset: Text.Offset): List[JMenuItem] = { |
58549 | 38 |
val result = |
39 |
for { |
|
65239
509a9b0ad02e
avoid global variables with implicit initialization;
wenzelm
parents:
65139
diff
changeset
|
40 |
spell_checker <- PIDE.plugin.spell_checker.get |
64882 | 41 |
doc_view <- Document_View.get(text_area) |
58549 | 42 |
rendering = doc_view.get_rendering() |
43 |
range = JEdit_Lib.point_range(text_area.getBuffer, offset) |
|
66116 | 44 |
Text.Info(_, word) <- Spell_Checker.current_word(rendering, range) |
58549 | 45 |
} yield (spell_checker, word) |
46 |
||
47 |
result match { |
|
48 |
case Some((spell_checker, word)) => |
|
49 |
||
50 |
val context = jEdit.getActionContext() |
|
51 |
def item(name: String): JMenuItem = |
|
52 |
new EnhancedMenuItem(context.getAction(name).getLabel, name, context) |
|
53 |
||
54 |
val complete_items = |
|
66137 | 55 |
if (spell_checker.complete(word).nonEmpty) List(item("isabelle.complete-word")) |
58549 | 56 |
else Nil |
57 |
||
58 |
val update_items = |
|
59 |
if (spell_checker.check(word)) |
|
60 |
List(item("isabelle.exclude-word"), item("isabelle.exclude-word-permanently")) |
|
61 |
else |
|
62 |
List(item("isabelle.include-word"), item("isabelle.include-word-permanently")) |
|
63 |
||
64 |
val reset_items = |
|
65 |
spell_checker.reset_enabled() match { |
|
66 |
case 0 => Nil |
|
67 |
case n => |
|
68 |
val name = "isabelle.reset-words" |
|
69 |
val label = context.getAction(name).getLabel |
|
70 |
List(new EnhancedMenuItem(label + " (" + n + ")", name, context)) |
|
71 |
} |
|
72 |
||
73 |
complete_items ::: update_items ::: reset_items |
|
74 |
||
75 |
case None => Nil |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
||
65139 | 80 |
/* dictionaries */ |
56557
18d921496aa5
updated to jortho-1.0-1: dictionaries from SCOWL 7.1, with parameters like aspell;
wenzelm
parents:
56552
diff
changeset
|
81 |
|
76391 | 82 |
def dictionaries_selector(): JEdit_Options.Entry = { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
56662
diff
changeset
|
83 |
GUI_Thread.require {} |
56559 | 84 |
|
56569 | 85 |
val option_name = "spell_checker_dictionary" |
56559 | 86 |
val opt = PIDE.options.value.check_name(option_name) |
87 |
||
76492
e228be7cd375
clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents:
76391
diff
changeset
|
88 |
new GUI.Selector(Spell_Checker.dictionaries.map(GUI.Selector.Item(_))) with JEdit_Options.Entry { |
56559 | 89 |
name = option_name |
75837 | 90 |
tooltip = GUI.tooltip_lines(opt.print_default) |
56559 | 91 |
val title = opt.title() |
75393 | 92 |
def load(): Unit = { |
56559 | 93 |
val lang = PIDE.options.string(option_name) |
76492
e228be7cd375
clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents:
76391
diff
changeset
|
94 |
Spell_Checker.get_dictionary(lang) match { |
e228be7cd375
clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents:
76391
diff
changeset
|
95 |
case Some(dict) => selection.item = GUI.Selector.Item(dict) |
56559 | 96 |
case None => |
97 |
} |
|
98 |
} |
|
76492
e228be7cd375
clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents:
76391
diff
changeset
|
99 |
def save(): Unit = |
e228be7cd375
clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents:
76391
diff
changeset
|
100 |
for (item <- selection.item.get_item) PIDE.options.string(option_name) = item.lang |
75837 | 101 |
|
102 |
load() |
|
56559 | 103 |
} |
104 |
} |
|
56547 | 105 |
} |