2
/******************************************************************************
3
* MODULE : edit_spell.cpp
4
* DESCRIPTION: spell checker based on ispell
5
* COPYRIGHT : (C) 1999 Joris van der Hoeven
6
*******************************************************************************
7
* This software falls under the GNU general public license version 3 or later.
8
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10
******************************************************************************/
12
#include "analyze.hpp"
13
#include "Replace/edit_replace.hpp"
14
#include "Interface/edit_interface.hpp"
16
#ifdef MACOSX_EXTENSIONS
17
#include "MacOS/mac_spellservice.h"
18
#define ispell_start mac_spell_start
19
#define ispell_check mac_spell_check
20
#define ispell_accept mac_spell_accept
21
#define ispell_insert mac_spell_insert
22
#define ispell_done mac_spell_done
24
#include "Ispell/ispell.hpp"
28
/******************************************************************************
29
* Start and end spell checking
30
******************************************************************************/
33
edit_replace_rep::spell_start () {
34
/********** get paths ***********/
35
search_at = start (et, rp);
36
spell_end_p = end (et, rp);
37
path spell_p= copy (tp);
38
if (selection_active_normal ()) {
39
get_selection (search_at, spell_end_p);
40
spell_p= copy (search_at);
43
/********** initialize spell checker ***********/
44
search_mode = copy (as_string (get_env_value (MODE, spell_p)));
46
copy (as_string (get_env_value (MODE_LANGUAGE (search_mode), spell_p)));
48
string message= ispell_start (search_lan);
49
if (starts (message, "Error: ")) {
51
set_message (message, "correct text");
55
/********** start spell checking ***********/
56
set_input_mode (INPUT_SPELL);
63
set_message ("no spelling errors found in text", "correct text");
67
edit_replace_rep::spell_end () {
69
ispell_done (search_lan);
70
set_message ("personal dictionary has been modified", "correct text");
72
else if (nr_replaced == 1)
73
set_message ("one spelling error has been corrected", "correct text");
74
else if (nr_replaced > 1)
75
set_message (as_string (nr_replaced) *
76
"#spelling errors have been corrected", "correct text");
77
else set_message ("spell checking complete", "correct text");
82
/******************************************************************************
83
* Find next word to be spelled
84
******************************************************************************/
87
edit_replace_rep::test_spellable (path p) {
88
tree st= subtree (et, path_up (p));
89
if (is_compound (st)) return p;
93
if ((e > 0) && ((is_iso_alpha (s[e-1])) || (is_digit (s[e-1])))) return p;
94
while ((e < N(s)) && (is_iso_alpha (s[e]))) e++;
95
if ((e < N(s)) && (is_digit (s[e]))) return p;
98
return path_add (p, e - b);
102
message_ispell (tree t) {
104
string s= "a: accept, r: replace, i: insert";
105
for (i=1; i<N(t); i++) {
106
s << ", " << as_string (i) << ": " << t[i]->label;
107
if (i==9) return s << ", ...";
113
edit_replace_rep::spell_next () {
115
if (path_inf (spell_end_p, search_at))
117
if (search_at == rp) {
121
search_end= test_spellable (search_at);
122
if (search_end != search_at) {
123
spell_t= ispell_check (search_lan, spell_s);
124
if (is_atomic (spell_t) && starts (spell_t->label, "Error: ")) {
126
set_message (spell_t->label, "spelling text");
129
if (spell_t != "ok") {
130
string mode= as_string (get_env_value (MODE, search_at));
132
as_string (get_env_value (MODE_LANGUAGE (mode), search_at));
133
if ((search_mode == mode) && (search_lan == lan)) {
134
set_selection (search_at, search_end);
135
notify_change (THE_SELECTION);
136
go_to (copy (search_end));
137
set_message (message_ispell (spell_t), "spelling error");
142
step_horizontal (forward);
146
/******************************************************************************
147
* Spell checking commands
148
******************************************************************************/
151
edit_replace_rep::spell_replace (string by) {
152
go_to (copy (search_at));
153
cut (search_at, search_end);
154
insert_tree (copy (by));
160
edit_replace_rep::spell_keypress (string s) {
161
if ((s == "C-c") || (s == "C-g") || (s == "escape"))
163
else if ((s == "a") || (s == "A")) {
164
ispell_accept (search_lan, spell_s);
165
step_horizontal (forward);
168
else if ((s == "r") || (s == "R"))
169
(void) eval ("(interactive spell-replace \"Replace by\")");
170
else if ((s == "i") || (s == "I")) {
171
ispell_insert (search_lan, spell_s);
173
step_horizontal (forward);
176
else if ((N(s)==1) && (is_digit (s[0])) && (s != "0")) {
178
int r= as_int (spell_t[0]);
180
go_to (copy (search_end));
181
cut (search_at, search_end);
182
insert_tree (copy (spell_t[i]));
183
search_at= copy (tp);
187
else if (i < N (spell_t)) {
188
ispell_accept (search_lan, spell_s);
189
step_horizontal (forward);
192
else set_message (message_ispell (spell_t), "spelling error");
194
else set_message (message_ispell (spell_t), "spelling error");