~registry/texmacs/trunk

« back to all changes in this revision

Viewing changes to src/src/Edit/Replace/edit_spell.cpp

  • Committer: mgubi
  • Date: 2009-06-04 15:13:41 UTC
  • Revision ID: svn-v4:64cb5145-927a-446d-8aed-2fb7b4773692:trunk:2717
Support for X11 TeXmacs.app on Mac

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
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
 
******************************************************************************/
11
 
 
12
 
#include "analyze.hpp"
13
 
#include "Replace/edit_replace.hpp"
14
 
#include "Interface/edit_interface.hpp"
15
 
 
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
23
 
#else
24
 
#include "Ispell/ispell.hpp"
25
 
#endif
26
 
 
27
 
 
28
 
/******************************************************************************
29
 
* Start and end spell checking
30
 
******************************************************************************/
31
 
 
32
 
void
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);
41
 
  }
42
 
 
43
 
  /********** initialize spell checker ***********/
44
 
  search_mode = copy (as_string (get_env_value (MODE, spell_p)));
45
 
  search_lan  =
46
 
    copy (as_string (get_env_value (MODE_LANGUAGE (search_mode), spell_p)));
47
 
 
48
 
  string message= ispell_start (search_lan);
49
 
  if (starts (message, "Error: ")) {
50
 
    spell_end ();
51
 
    set_message (message, "correct text");
52
 
    return;
53
 
  }
54
 
 
55
 
  /********** start spell checking ***********/
56
 
  set_input_mode (INPUT_SPELL);
57
 
  forward      = true;
58
 
  nr_replaced  = 0;
59
 
  spell_dicmod = false;
60
 
 
61
 
  spell_next ();
62
 
  if (search_at == rp)
63
 
    set_message ("no spelling errors found in text", "correct text");
64
 
}
65
 
 
66
 
void
67
 
edit_replace_rep::spell_end () {
68
 
  if (spell_dicmod) {
69
 
    ispell_done (search_lan);
70
 
    set_message ("personal dictionary has been modified", "correct text");
71
 
  }
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");
78
 
  beep ();
79
 
  set_input_normal ();
80
 
}
81
 
 
82
 
/******************************************************************************
83
 
* Find next word to be spelled
84
 
******************************************************************************/
85
 
 
86
 
path
87
 
edit_replace_rep::test_spellable (path p) {
88
 
  tree st= subtree (et, path_up (p));
89
 
  if (is_compound (st)) return p;
90
 
  string s= st->label;
91
 
  int    b= last_item (p);
92
 
  int    e= b;
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;
96
 
  if (e == b) return p;
97
 
  spell_s= s (b, e);
98
 
  return path_add (p, e - b);
99
 
}
100
 
 
101
 
static string
102
 
message_ispell (tree t) {
103
 
  int i;
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 << ", ...";
108
 
  }
109
 
  return s;
110
 
}
111
 
 
112
 
void
113
 
edit_replace_rep::spell_next () {
114
 
  while (true) {
115
 
    if (path_inf (spell_end_p, search_at))
116
 
      search_at= rp;
117
 
    if (search_at == rp) {
118
 
      spell_end ();
119
 
      return;
120
 
    }
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: ")) {
125
 
        spell_end ();
126
 
        set_message (spell_t->label, "spelling text");
127
 
        return;
128
 
      }
129
 
      if (spell_t != "ok") {
130
 
        string mode= as_string (get_env_value (MODE, search_at));
131
 
        string lan =
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");
138
 
          return;
139
 
        }
140
 
      }
141
 
    }
142
 
    step_horizontal (forward);
143
 
  }
144
 
}
145
 
 
146
 
/******************************************************************************
147
 
* Spell checking commands
148
 
******************************************************************************/
149
 
 
150
 
void
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));
155
 
  nr_replaced++;
156
 
  spell_next ();
157
 
}
158
 
 
159
 
bool
160
 
edit_replace_rep::spell_keypress (string s) {
161
 
  if ((s == "C-c") || (s == "C-g") || (s == "escape"))
162
 
    spell_end ();
163
 
  else if ((s == "a") || (s == "A")) {
164
 
    ispell_accept (search_lan, spell_s);
165
 
    step_horizontal (forward);
166
 
    spell_next ();
167
 
  }
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);
172
 
    spell_dicmod= true;
173
 
    step_horizontal (forward);
174
 
    spell_next ();
175
 
  }
176
 
  else if ((N(s)==1) && (is_digit (s[0])) && (s != "0")) {
177
 
    int i= as_int (s);
178
 
    int r= as_int (spell_t[0]);
179
 
    if (i <= r) {
180
 
      go_to (copy (search_end));
181
 
      cut (search_at, search_end);
182
 
      insert_tree (copy (spell_t[i]));
183
 
      search_at= copy (tp);
184
 
      nr_replaced++;
185
 
      spell_next ();
186
 
    }
187
 
    else if (i < N (spell_t)) {
188
 
      ispell_accept (search_lan, spell_s);
189
 
      step_horizontal (forward);
190
 
      spell_next ();
191
 
    }
192
 
    else set_message (message_ispell (spell_t), "spelling error");
193
 
  }
194
 
  else set_message (message_ispell (spell_t), "spelling error");
195
 
  return true;
196
 
}