Coverage for C:\Repos\leo-editor\leo\commands\spellCommands.py: 17%

549 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-24 10:21 -0500

1# -*- coding: utf-8 -*- 

2#@+leo-ver=5-thin 

3#@+node:ekr.20150514040239.1: * @file ../commands/spellCommands.py 

4#@@first 

5"""Leo's spell-checking commands.""" 

6#@+<< imports >> 

7#@+node:ekr.20150514050530.1: ** << imports >> (spellCommands.py) 

8import re 

9try: 

10 # pylint: disable=import-error 

11 # We can't assume the user has this. 

12 # pip install pyenchant 

13 import enchant 

14except Exception: # May throw WinError(!) 

15 enchant = None 

16from leo.commands.baseCommands import BaseEditCommandsClass 

17from leo.core import leoGlobals as g 

18#@-<< imports >> 

19 

20def cmd(name): 

21 """Command decorator for the SpellCommandsClass class.""" 

22 return g.new_cmd_decorator(name, ['c', 'spellCommands',]) 

23 

24#@+others 

25#@+node:ekr.20180207071908.1: ** class BaseSpellWrapper 

26class BaseSpellWrapper: 

27 """Code common to EnchantWrapper and DefaultWrapper""" 

28 # pylint: disable=no-member 

29 # Subclasses set self.c and self.d 

30 #@+others 

31 #@+node:ekr.20180207071114.3: *3* spell.add 

32 def add(self, word): 

33 """Add a word to the user dictionary.""" 

34 self.d.add(word) 

35 #@+node:ekr.20150514063305.513: *3* spell.clean_dict 

36 def clean_dict(self, fn): 

37 if g.os_path_exists(fn): 

38 f = open(fn, mode='rb') 

39 s = f.read() 

40 f.close() 

41 # Blanks lines cause troubles. 

42 s2 = s.replace(b'\r', b'').replace(b'\n\n', b'\n') 

43 if s2.startswith(b'\n'): 

44 s2 = s2[1:] 

45 if s != s2: 

46 g.es_print('cleaning', fn) 

47 f = open(fn, mode='wb') # type:ignore 

48 f.write(s2) 

49 f.close() 

50 #@+node:ekr.20180207071114.5: *3* spell.create 

51 def create(self, fn): 

52 """Create the given file with empty contents.""" 

53 # Make the directories as needed. 

54 theDir = g.os_path_dirname(fn) 

55 if theDir: 

56 ok = g.makeAllNonExistentDirectories(theDir) 

57 # #1453: Don't assume the directory exists. 

58 if not ok: 

59 g.error(f"did not create directory: {theDir}") 

60 return 

61 # Create the file. 

62 try: 

63 f = open(fn, mode='wb') 

64 f.close() 

65 g.note(f"created: {fn}") 

66 except IOError: 

67 g.error(f"can not create: {fn}") 

68 except Exception: 

69 g.error(f"unexpected error creating: {fn}") 

70 g.es_exception() 

71 #@+node:ekr.20180207072351.1: *3* spell.find_user_dict 

72 def find_user_dict(self): 

73 """Return the full path to the local dictionary.""" 

74 c = self.c 

75 join = g.os_path_finalize_join 

76 table = ( 

77 # Settings first. 

78 c.config.getString('enchant-local-dictionary'), 

79 # #108: then the .leo directory. 

80 join(g.app.homeDir, '.leo', 'spellpyx.txt'), 

81 # The plugins directory as a last resort. 

82 join(g.app.loadDir, "..", "plugins", 'spellpyx.txt'), 

83 ) 

84 for path in table: 

85 if g.os_path_exists(path): 

86 return path 

87 g.es_print('Creating ~/.leo/spellpyx.txt') 

88 # #1453: Return the default path. 

89 return join(g.app.homeDir, '.leo', 'spellpyx.txt') 

90 #@+node:ekr.20150514063305.515: *3* spell.ignore 

91 def ignore(self, word): 

92 

93 self.d.add_to_session(word) 

94 #@+node:ekr.20150514063305.517: *3* spell.process_word 

95 def process_word(self, word): 

96 """ 

97 Check the word. Return None if the word is properly spelled. 

98 Otherwise, return a list of alternatives. 

99 """ 

100 d = self.d 

101 if not d: 

102 return None 

103 if d.check(word): 

104 return None 

105 # Speed doesn't matter here. The more we find, the more convenient. 

106 # Remove all digits. 

107 word = ''.join([i for i in word if not i.isdigit()]) 

108 if d.check(word) or d.check(word.lower()): 

109 return None 

110 if word.find('_') > -1: 

111 # Snake case. 

112 words = word.split('_') 

113 for word2 in words: 

114 if not d.check(word2) and not d.check(word2.lower()): 

115 return d.suggest(word) 

116 return None 

117 words = g.unCamel(word) 

118 if words: 

119 for word2 in words: 

120 if not d.check(word2) and not d.check(word2.lower()): 

121 return d.suggest(word) 

122 return None 

123 return d.suggest(word) 

124 #@-others 

125#@+node:ekr.20180207075606.1: ** class DefaultDict 

126class DefaultDict: 

127 """A class with the same interface as the enchant dict class.""" 

128 

129 def __init__(self, words=None): 

130 self.added_words = set() 

131 self.ignored_words = set() 

132 self.words = set() if words is None else set(words) 

133 #@+others 

134 #@+node:ekr.20180207075740.1: *3* dict.add 

135 def add(self, word): 

136 """Add a word to the dictionary.""" 

137 self.words.add(word) 

138 self.added_words.add(word) 

139 #@+node:ekr.20180207101513.1: *3* dict.add_words_from_dict 

140 def add_words_from_dict(self, kind, fn, words): 

141 """For use by DefaultWrapper.""" 

142 for word in words or []: 

143 self.words.add(word) 

144 self.words.add(word.lower()) 

145 #@+node:ekr.20180207075751.1: *3* dict.add_to_session 

146 def add_to_session(self, word): 

147 

148 self.ignored_words.add(word) 

149 #@+node:ekr.20180207080007.1: *3* dict.check 

150 def check(self, word): 

151 """Return True if the word is in the dict.""" 

152 for s in (word, word.lower(), word.capitalize()): 

153 if s in self.words or s in self.ignored_words: 

154 return True 

155 return False 

156 #@+node:ekr.20180207081634.1: *3* dict.suggest & helpers 

157 def suggest(self, word): 

158 

159 def known(words): 

160 """Return the words that are in the dictionary.""" 

161 return [z for z in list(set(words)) if z in self.words] 

162 

163 assert not known([word]), repr(word) 

164 suggestions = ( 

165 known(self.edits1(word)) or 

166 known(self.edits2(word)) 

167 # [word] # Fall back to the unknown word itself. 

168 ) 

169 return suggestions 

170 #@+node:ekr.20180207085717.1: *4* dict.edits1 & edits2 

171 #@@nobeautify 

172 

173 def edits1(self, word): 

174 "All edits that are one edit away from `word`." 

175 letters = 'abcdefghijklmnopqrstuvwxyz' 

176 splits = [(word[:i], word[i:]) for i in range(len(word) + 1)] 

177 deletes = [L + R[1:] for L, R in splits if R] 

178 transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R)>1] 

179 replaces = [L + c + R[1:] for L, R in splits if R for c in letters] 

180 inserts = [L + c + R for L, R in splits for c in letters] 

181 return list(set(deletes + transposes + replaces + inserts)) 

182 

183 def edits2(self, word): 

184 "All edits that are two edits away from `word`." 

185 return [e2 for e1 in self.edits1(word) for e2 in self.edits1(e1)] 

186 #@-others 

187#@+node:ekr.20180207071114.1: ** class DefaultWrapper (BaseSpellWrapper) 

188class DefaultWrapper(BaseSpellWrapper): 

189 """ 

190 A default spell checker for when pyenchant is not available. 

191 

192 Based on http://norvig.com/spell-correct.html 

193 

194 Main dictionary: ~/.leo/main_spelling_dict.txt 

195 User dictionary: 

196 - @string enchant_local_dictionary or 

197 - leo/plugins/spellpyx.txt or 

198 - ~/.leo/spellpyx.txt 

199 """ 

200 #@+others 

201 #@+node:ekr.20180207071114.2: *3* default. __init__ 

202 def __init__(self, c): 

203 """Ctor for DefaultWrapper class.""" 

204 # pylint: disable=super-init-not-called 

205 self.c = c 

206 if not g.app.spellDict: 

207 g.app.spellDict = DefaultDict() 

208 self.d = g.app.spellDict 

209 self.user_fn = self.find_user_dict() 

210 if not g.os_path_exists(self.user_fn): 

211 # Fix bug 1175013: leo/plugins/spellpyx.txt is 

212 # both source controlled and customized. 

213 self.create(self.user_fn) 

214 self.main_fn = self.find_main_dict() 

215 table = ( 

216 ('user', self.user_fn), 

217 ('main', self.main_fn), 

218 ) 

219 for kind, fn in table: 

220 if fn: 

221 words = self.read_words(kind, fn) 

222 self.d.add_words_from_dict(kind, fn, words) 

223 #@+node:ekr.20180207110701.1: *3* default.add 

224 def add(self, word): 

225 """Add a word to the user dictionary.""" 

226 self.d.add(word) 

227 self.d.add(word.lower()) 

228 self.save_user_dict() 

229 #@+node:ekr.20180207100238.1: *3* default.find_main_dict 

230 def find_main_dict(self): 

231 """Return the full path to the global dictionary.""" 

232 c = self.c 

233 fn = c.config.getString('main-spelling-dictionary') 

234 if fn and g.os_path_exists(fn): 

235 return fn 

236 # Default to ~/.leo/main_spelling_dict.txt 

237 fn = g.os_path_finalize_join( 

238 g.app.homeDir, '.leo', 'main_spelling_dict.txt') 

239 return fn if g.os_path_exists(fn) else None 

240 #@+node:ekr.20180207073815.1: *3* default.read_words 

241 def read_words(self, kind, fn): 

242 """Return all the words from the dictionary file.""" 

243 words = set() 

244 try: 

245 with open(fn, 'rb') as f: 

246 s = g.toUnicode(f.read()) 

247 # #1688: Do this in place. 

248 for line in g.splitLines(s): 

249 line = line.strip() 

250 if line and not line.startswith('#'): 

251 words.add(line) 

252 except Exception: 

253 g.es_print(f"can not open {kind} dictionary: {fn}") 

254 return words 

255 #@+node:ekr.20180207110718.1: *3* default.save_dict 

256 def save_dict(self, kind, fn, trace=False): 

257 """ 

258 Save the dictionary whose name is given, alphabetizing the file. 

259 Write added words to the file if kind is 'user'. 

260 """ 

261 if not fn: 

262 return 

263 words = self.read_words(kind, fn) 

264 if not words: 

265 return 

266 words = set(words) 

267 if kind == 'user': 

268 for word in self.d.added_words: 

269 words.add(word) 

270 aList = sorted(words, key=lambda s: s.lower()) 

271 f = open(fn, mode='wb') 

272 s = '\n'.join(aList) + '\n' 

273 f.write(g.toEncodedString(s)) 

274 f.close() 

275 #@+node:ekr.20180211104628.1: *3* default.save_main/user_dict 

276 def save_main_dict(self, trace=False): 

277 

278 self.save_dict('main', self.main_fn, trace=trace) 

279 

280 def save_user_dict(self, trace=False): 

281 

282 self.save_dict('user', self.user_fn, trace=trace) 

283 #@+node:ekr.20180209141933.1: *3* default.show_info 

284 def show_info(self): 

285 

286 if self.main_fn: 

287 g.es_print('Default spell checker') 

288 table = ( 

289 ('main', self.main_fn), 

290 ('user', self.user_fn), 

291 ) 

292 else: 

293 g.es_print('\nSpell checking has been disabled.') 

294 g.es_print('To enable, put a main dictionary at:') 

295 g.es_print('~/.leo/main_spelling_dict.txt') 

296 table = ( # type:ignore 

297 ('user', self.user_fn), 

298 ) 

299 for kind, fn in table: 

300 g.es_print( 

301 f"{kind} dictionary: {(g.os_path_normpath(fn) if fn else 'None')}") 

302 #@-others 

303#@+node:ekr.20150514063305.510: ** class EnchantWrapper (BaseSpellWrapper) 

304class EnchantWrapper(BaseSpellWrapper): 

305 """A wrapper class for PyEnchant spell checker""" 

306 #@+others 

307 #@+node:ekr.20150514063305.511: *3* enchant. __init__ 

308 def __init__(self, c): 

309 """Ctor for EnchantWrapper class.""" 

310 # pylint: disable=super-init-not-called 

311 self.c = c 

312 self.init_language() 

313 fn = self.find_user_dict() 

314 g.app.spellDict = self.d = self.open_dict_file(fn) 

315 #@+node:ekr.20180207073536.1: *3* enchant.create_dict_from_file 

316 def create_dict_from_file(self, fn, language): 

317 

318 return enchant.DictWithPWL(language, fn) 

319 #@+node:ekr.20180207074613.1: *3* enchant.default_dict 

320 def default_dict(self, language): 

321 

322 return enchant.Dict(language) 

323 #@+node:ekr.20180207072846.1: *3* enchant.init_language 

324 def init_language(self): 

325 """Init self.language.""" 

326 c = self.c 

327 language = g.checkUnicode(c.config.getString('enchant-language')) 

328 if language: 

329 try: 

330 ok = enchant.dict_exists(language) 

331 except Exception: 

332 ok = False 

333 if not ok: 

334 g.warning('Invalid language code for Enchant', repr(language)) 

335 g.es_print('Using "en_US" instead') 

336 g.es_print('Use @string enchant_language to specify your language') 

337 language = 'en_US' 

338 self.language = language 

339 #@+node:ekr.20180207102856.1: *3* enchant.open_dict_file 

340 def open_dict_file(self, fn): 

341 """Open or create the dict with the given fn.""" 

342 language = self.language 

343 if not fn or not language: 

344 return None 

345 if g.app.spellDict: 

346 return g.app.spellDict 

347 if not g.os_path_exists(fn): 

348 # Fix bug 1175013: leo/plugins/spellpyx.txt is 

349 # both source controlled and customized. 

350 self.create(fn) 

351 if g.os_path_exists(fn): 

352 # Merge the local and global dictionaries. 

353 try: 

354 self.clean_dict(fn) 

355 d = enchant.DictWithPWL(language, fn) 

356 except Exception: 

357 # This is off-putting, and not necessary. 

358 # g.es('Error reading dictionary file', fn) 

359 # g.es_exception() 

360 d = enchant.Dict(language) 

361 else: 

362 # A fallback. Unlikely to happen. 

363 d = enchant.Dict(language) 

364 return d 

365 #@+node:ekr.20150514063305.515: *3* spell.ignore 

366 def ignore(self, word): 

367 

368 self.d.add_to_session(word) 

369 #@+node:ekr.20150514063305.517: *3* spell.process_word 

370 def process_word(self, word): 

371 """ 

372 Check the word. Return None if the word is properly spelled. 

373 Otherwise, return a list of alternatives. 

374 """ 

375 d = self.d 

376 if not d: 

377 return None 

378 if d.check(word): 

379 return None 

380 # Speed doesn't matter here. The more we find, the more convenient. 

381 # Remove all digits. 

382 word = ''.join([i for i in word if not i.isdigit()]) 

383 if d.check(word) or d.check(word.lower()): 

384 return None 

385 if word.find('_') > -1: 

386 # Snake case. 

387 words = word.split('_') 

388 for word2 in words: 

389 if not d.check(word2) and not d.check(word2.lower()): 

390 return d.suggest(word) 

391 return None 

392 words = g.unCamel(word) 

393 if words: 

394 for word2 in words: 

395 if not d.check(word2) and not d.check(word2.lower()): 

396 return d.suggest(word) 

397 return None 

398 return d.suggest(word) 

399 #@+node:ekr.20180209142310.1: *3* spell.show_info 

400 def show_info(self): 

401 

402 g.es_print('pyenchant spell checker') 

403 g.es_print(f"user dictionary: {self.find_user_dict()}") 

404 try: 

405 aList = enchant.list_dicts() 

406 aList2 = [a for a, b in aList] 

407 g.es_print(f"main dictionaries: {', '.join(aList2)}") 

408 except Exception: 

409 g.es_exception() 

410 #@-others 

411#@+node:ekr.20150514063305.481: ** class SpellCommandsClass 

412class SpellCommandsClass(BaseEditCommandsClass): 

413 """Commands to support the Spell Tab.""" 

414 #@+others 

415 #@+node:ekr.20150514063305.482: *3* ctor & reloadSettings(SpellCommandsClass) 

416 def __init__(self, c): 

417 """ 

418 Ctor for SpellCommandsClass class. 

419 Inits happen when the first frame opens. 

420 """ 

421 # pylint: disable=super-init-not-called 

422 self.c = c 

423 self.handler = None 

424 self.reloadSettings() 

425 

426 def reloadSettings(self): 

427 """SpellCommandsClass.reloadSettings.""" 

428 c = self.c 

429 self.page_width = c.config.getInt("page-width") # for wrapping 

430 #@+node:ekr.20150514063305.484: *3* openSpellTab 

431 @cmd('spell-tab-open') 

432 def openSpellTab(self, event=None): 

433 """Open the Spell Checker tab in the log pane.""" 

434 if g.unitTesting: 

435 return 

436 c = self.c 

437 log = c.frame.log 

438 tabName = 'Spell' 

439 if log.frameDict.get(tabName): 

440 log.selectTab(tabName) 

441 else: 

442 log.selectTab(tabName) 

443 self.handler = SpellTabHandler(c, tabName) 

444 # Bug fix: 2013/05/22. 

445 if not self.handler.loaded: 

446 log.deleteTab(tabName) 

447 # spell as you type stuff 

448 self.suggestions = [] 

449 self.suggestions_idx = None 

450 self.word = None 

451 self.spell_as_you_type = False 

452 self.wrap_as_you_type = False 

453 #@+node:ekr.20150514063305.485: *3* commands...(SpellCommandsClass) 

454 #@+node:ekr.20171205043931.1: *4* add 

455 @cmd('spell-add') 

456 def add(self, event=None): 

457 """ 

458 Simulate pressing the 'add' button in the Spell tab. 

459 

460 Just open the Spell tab if it has never been opened. 

461 For minibuffer commands, we must also force the Spell tab to be visible. 

462 """ 

463 # self.handler is a SpellTabHandler object (inited by openSpellTab) 

464 if self.handler: 

465 self.openSpellTab() 

466 self.handler.add() 

467 else: 

468 self.openSpellTab() 

469 #@+node:ekr.20150514063305.486: *4* find 

470 @cmd('spell-find') 

471 def find(self, event=None): 

472 """ 

473 Simulate pressing the 'Find' button in the Spell tab. 

474 

475 Just open the Spell tab if it has never been opened. 

476 For minibuffer commands, we must also force the Spell tab to be visible. 

477 """ 

478 # self.handler is a SpellTabHandler object (inited by openSpellTab) 

479 if self.handler: 

480 self.openSpellTab() 

481 self.handler.find() 

482 else: 

483 self.openSpellTab() 

484 #@+node:ekr.20150514063305.487: *4* change 

485 @cmd('spell-change') 

486 def change(self, event=None): 

487 """Simulate pressing the 'Change' button in the Spell tab.""" 

488 if self.handler: 

489 self.openSpellTab() 

490 self.handler.change() 

491 else: 

492 self.openSpellTab() 

493 #@+node:ekr.20150514063305.488: *4* changeThenFind 

494 @cmd('spell-change-then-find') 

495 def changeThenFind(self, event=None): 

496 """Simulate pressing the 'Change, Find' button in the Spell tab.""" 

497 if self.handler: 

498 self.openSpellTab() 

499 # A workaround for a pylint warning: 

500 # self.handler.changeThenFind() 

501 f = getattr(self.handler, 'changeThenFind') 

502 f() 

503 else: 

504 self.openSpellTab() 

505 #@+node:ekr.20150514063305.489: *4* hide 

506 @cmd('spell-tab-hide') 

507 def hide(self, event=None): 

508 """Hide the Spell tab.""" 

509 if self.handler: 

510 self.c.frame.log.selectTab('Log') 

511 self.c.bodyWantsFocus() 

512 #@+node:ekr.20150514063305.490: *4* ignore 

513 @cmd('spell-ignore') 

514 def ignore(self, event=None): 

515 """Simulate pressing the 'Ignore' button in the Spell tab.""" 

516 if self.handler: 

517 self.openSpellTab() 

518 self.handler.ignore() 

519 else: 

520 self.openSpellTab() 

521 #@+node:ekr.20150514063305.491: *4* focusToSpell 

522 @cmd('focus-to-spell-tab') 

523 def focusToSpell(self, event=None): 

524 """Put focus in the spell tab.""" 

525 self.openSpellTab() # Makes Spell tab visible. 

526 # This is not a great idea. There is no indication of focus. 

527 # if self.handler and self.handler.tab: 

528 # self.handler.tab.setFocus() 

529 #@+node:ekr.20150514063305.492: *3* as_you_type_* commands 

530 #@+node:ekr.20150514063305.493: *4* as_you_type_toggle 

531 @cmd('spell-as-you-type-toggle') 

532 def as_you_type_toggle(self, event): 

533 """as_you_type_toggle - toggle spell as you type.""" 

534 # c = self.c 

535 if self.spell_as_you_type: 

536 self.spell_as_you_type = False 

537 if not self.wrap_as_you_type: 

538 g.unregisterHandler('bodykey2', self.as_you_type_onkey) 

539 g.es("Spell as you type disabled") 

540 return 

541 self.spell_as_you_type = True 

542 if not self.wrap_as_you_type: 

543 g.registerHandler('bodykey2', self.as_you_type_onkey) 

544 g.es("Spell as you type enabled") 

545 #@+node:ekr.20150514063305.494: *4* as_you_type_wrap 

546 @cmd('spell-as-you-type-wrap') 

547 def as_you_type_wrap(self, event): 

548 """as_you_type_wrap - toggle wrap as you type.""" 

549 # c = self.c 

550 if self.wrap_as_you_type: 

551 self.wrap_as_you_type = False 

552 if not self.spell_as_you_type: 

553 g.unregisterHandler('bodykey2', self.as_you_type_onkey) 

554 g.es("Wrap as you type disabled") 

555 return 

556 self.wrap_as_you_type = True 

557 if not self.spell_as_you_type: 

558 g.registerHandler('bodykey2', self.as_you_type_onkey) 

559 g.es("Wrap as you type enabled") 

560 #@+node:ekr.20150514063305.495: *4* as_you_type_next 

561 @cmd('spell-as-you-type-next') 

562 def as_you_type_next(self, event): 

563 """as_you_type_next - cycle word behind cursor to next suggestion.""" 

564 if not self.suggestions: 

565 g.es('[no suggestions]') 

566 return 

567 word = self.suggestions[self.suggestion_idx] # type:ignore 

568 self.suggestion_idx = (self.suggestion_idx + 1) % len(self.suggestions) # type:ignore 

569 self.as_you_type_replace(word) 

570 #@+node:ekr.20150514063305.496: *4* as_you_type_undo 

571 @cmd('spell-as-you-type-undo') 

572 def as_you_type_undo(self, event): 

573 """as_you_type_undo - replace word behind cursor with word 

574 user typed before it started cycling suggestions. 

575 """ 

576 if not self.word: 

577 g.es('[no previous word]') 

578 return 

579 self.as_you_type_replace(self.word) 

580 #@+node:ekr.20150514063305.497: *4* as_you_type_onkey 

581 def as_you_type_onkey(self, tag, kwargs): 

582 """as_you_type_onkey - handle a keystroke in the body when 

583 spell as you type is active 

584 

585 :Parameters: 

586 - `tag`: hook tag 

587 - `kwargs`: hook arguments 

588 """ 

589 if kwargs['c'] != self.c: 

590 return 

591 if kwargs['ch'] not in '\'",.:) \n\t': 

592 return 

593 c = self.c 

594 spell_ok = True 

595 if self.spell_as_you_type: # might just be for wrapping 

596 w = c.frame.body.wrapper 

597 txt = w.getAllText() 

598 i = w.getInsertPoint() 

599 word = txt[:i].rsplit(None, 1)[-1] 

600 word = ''.join(i if i.isalpha() else ' ' for i in word).split() 

601 if word: 

602 word = word[-1] 

603 ec = c.spellCommands.handler.spellController 

604 suggests = ec.process_word(word) 

605 if suggests: 

606 spell_ok = False 

607 g.es(' '.join(suggests[:5]) + 

608 ('...' if len(suggests) > 5 else ''), 

609 color='red') 

610 elif suggests is not None: 

611 spell_ok = False 

612 g.es('[no suggestions]') 

613 self.suggestions = suggests 

614 self.suggestion_idx = 0 

615 self.word = word 

616 if spell_ok and self.wrap_as_you_type and kwargs['ch'] == ' ': 

617 w = c.frame.body.wrapper 

618 txt = w.getAllText() 

619 i = w.getInsertPoint() 

620 # calculate the current column 

621 parts = txt.split('\n') 

622 popped = 0 # chars on previous lines 

623 while len(parts[0]) + popped < i: 

624 popped += len(parts.pop(0)) + 1 # +1 for the \n that's gone 

625 col = i - popped 

626 if col > self.page_width: 

627 txt = txt[:i] + '\n' + txt[i:] # replace space with \n 

628 w.setAllText(txt) 

629 c.p.b = txt 

630 w.setInsertPoint(i + 1) # must come after c.p.b assignment 

631 #@+node:ekr.20150514063305.498: *4* as_you_type_replace 

632 def as_you_type_replace(self, word): 

633 """as_you_type_replace - replace the word behind the cursor 

634 with `word` 

635 

636 :Parameters: 

637 - `word`: word to use as replacement 

638 """ 

639 c = self.c 

640 w = c.frame.body.wrapper 

641 txt = w.getAllText() 

642 j = i = w.getInsertPoint() 

643 i -= 1 

644 while i and not txt[i].isalpha(): 

645 i -= 1 

646 xtra = j - i 

647 j = i + 1 

648 while i and txt[i].isalpha(): 

649 i -= 1 

650 if i or (txt and not txt[0].isalpha()): 

651 i += 1 

652 txt = txt[:i] + word + txt[j:] 

653 w.setAllText(txt) 

654 c.p.b = txt 

655 w.setInsertPoint(i + len(word) + xtra - 1) 

656 c.bodyWantsFocusNow() 

657 #@-others 

658#@+node:ekr.20150514063305.499: ** class SpellTabHandler 

659class SpellTabHandler: 

660 """A class to create and manage Leo's Spell Check dialog.""" 

661 #@+others 

662 #@+node:ekr.20150514063305.501: *3* SpellTabHandler.__init__ 

663 def __init__(self, c, tabName): 

664 """Ctor for SpellTabHandler class.""" 

665 if g.app.gui.isNullGui: 

666 return 

667 self.c = c 

668 self.body = c.frame.body 

669 self.currentWord = None 

670 # Don't include underscores in words. It just complicates things. 

671 # [^\W\d_] means any unicode char except underscore or digit. 

672 self.re_word = re.compile(r"([^\W\d_]+)(['`][^\W\d_]+)?", flags=re.UNICODE) 

673 self.outerScrolledFrame = None 

674 self.seen = set() # Adding a word to seen will ignore it until restart. 

675 # A text widget for scanning. Must have a parent frame. 

676 self.workCtrl = g.app.gui.plainTextWidget(c.frame.top) 

677 if enchant: 

678 self.spellController = EnchantWrapper(c) 

679 self.tab = g.app.gui.createSpellTab(c, self, tabName) 

680 self.loaded = True 

681 return 

682 # Create the spellController for the show-spell-info command. 

683 self.spellController = DefaultWrapper(c) # type:ignore 

684 self.loaded = bool(self.spellController.main_fn) 

685 if self.loaded: 

686 # Create the spell tab only if the main dict exists. 

687 self.tab = g.app.gui.createSpellTab(c, self, tabName) 

688 else: 

689 # g.es_print('No main dictionary') 

690 self.tab = None 

691 #@+node:ekr.20150514063305.502: *3* Commands 

692 #@+node:ekr.20150514063305.503: *4* add (spellTab) 

693 def add(self, event=None): 

694 """Add the selected suggestion to the dictionary.""" 

695 if self.loaded: 

696 w = self.currentWord 

697 if w: 

698 self.spellController.add(w) 

699 self.tab.onFindButton() 

700 #@+node:ekr.20150514063305.504: *4* change (spellTab) 

701 def change(self, event=None): 

702 """Make the selected change to the text""" 

703 if not self.loaded: 

704 return False 

705 c, p, u = self.c, self.c.p, self.c.undoer 

706 w = c.frame.body.wrapper 

707 selection = self.tab.getSuggestion() 

708 if selection: 

709 bunch = u.beforeChangeBody(p) 

710 # Use getattr to keep pylint happy. 

711 i = getattr(self.tab, 'change_i', None) 

712 j = getattr(self.tab, 'change_j', None) 

713 if i is not None: 

714 start, end = i, j 

715 else: 

716 start, end = w.getSelectionRange() 

717 if start is not None: 

718 if start > end: 

719 start, end = end, start 

720 w.delete(start, end) 

721 w.insert(start, selection) 

722 w.setSelectionRange(start, start + len(selection)) 

723 p.v.b = w.getAllText() 

724 u.afterChangeBody(p, 'Change', bunch) 

725 c.invalidateFocus() 

726 c.bodyWantsFocus() 

727 return True 

728 # The focus must never leave the body pane. 

729 c.invalidateFocus() 

730 c.bodyWantsFocus() 

731 return False 

732 #@+node:ekr.20150514063305.505: *4* find & helper 

733 def find(self, event=None): 

734 """Find the next unknown word.""" 

735 if not self.loaded: 

736 return 

737 c, n, p = self.c, 0, self.c.p 

738 sc = self.spellController 

739 w = c.frame.body.wrapper 

740 c.selectPosition(p) 

741 s = w.getAllText().rstrip() 

742 ins = w.getInsertPoint() 

743 # New in Leo 5.3: use regex to find words. 

744 last_p = p.copy() 

745 while True: 

746 for m in self.re_word.finditer(s[ins:]): 

747 start, word = m.start(0), m.group(0) 

748 if word in self.seen: 

749 continue 

750 n += 1 

751 # Ignore the word if numbers precede or follow it. 

752 # Seems difficult to do this in the regex itself. 

753 k1 = ins + start - 1 

754 if k1 >= 0 and s[k1].isdigit(): 

755 continue 

756 k2 = ins + start + len(word) 

757 if k2 < len(s) and s[k2].isdigit(): 

758 continue 

759 alts = sc.process_word(word) 

760 if alts: 

761 self.currentWord = word 

762 i = ins + start 

763 j = i + len(word) 

764 self.showMisspelled(p) 

765 self.tab.fillbox(alts, word) 

766 c.invalidateFocus() 

767 c.bodyWantsFocus() 

768 w.setSelectionRange(i, j, insert=j) 

769 k = g.see_more_lines(s, j, 4) 

770 w.see(k) 

771 return 

772 self.seen.add(word) 

773 # No more misspellings in p 

774 p.moveToThreadNext() 

775 if p: 

776 ins = 0 

777 s = p.b 

778 else: 

779 g.es("no more misspellings") 

780 c.selectPosition(last_p) 

781 self.tab.fillbox([]) 

782 c.invalidateFocus() 

783 c.bodyWantsFocus() 

784 return 

785 #@+node:ekr.20160415033936.1: *5* showMisspelled 

786 def showMisspelled(self, p): 

787 """Show the position p, contracting the tree as needed.""" 

788 c = self.c 

789 redraw = not p.isVisible(c) 

790 # New in Leo 4.4.8: show only the 'sparse' tree when redrawing. 

791 if c.sparse_spell and not c.p.isAncestorOf(p): 

792 for p2 in c.p.self_and_parents(copy=False): 

793 p2.contract() 

794 redraw = True 

795 for p2 in p.parents(copy=False): 

796 if not p2.isExpanded(): 

797 p2.expand() 

798 redraw = True 

799 if redraw: 

800 c.redraw(p) 

801 else: 

802 c.selectPosition(p) 

803 #@+node:ekr.20150514063305.508: *4* hide 

804 def hide(self, event=None): 

805 self.c.frame.log.selectTab('Log') 

806 #@+node:ekr.20150514063305.509: *4* ignore 

807 def ignore(self, event=None): 

808 """Ignore the incorrect word for the duration of this spell check session.""" 

809 if self.loaded: 

810 w = self.currentWord 

811 if w: 

812 self.spellController.ignore(w) 

813 self.tab.onFindButton() 

814 #@-others 

815#@+node:ekr.20180209141207.1: ** @g.command('show-spell-info') 

816@g.command('show-spell-info') 

817def show_spell_info(event=None): 

818 c = event.get('c') 

819 if c: 

820 c.spellCommands.handler.spellController.show_info() 

821#@+node:ekr.20180211104019.1: ** @g.command('clean-main-spell-dict') 

822@g.command('clean-main-spell-dict') 

823def clean_main_spell_dict(event): 

824 """ 

825 Clean the main spelling dictionary used *only* by the default spell 

826 checker. 

827 

828 This command works regardless of the spell checker being used. 

829 """ 

830 c = event and event.get('c') 

831 if c: 

832 DefaultWrapper(c).save_main_dict(trace=True) 

833#@+node:ekr.20180211105748.1: ** @g.command('clean-user-spell-dict') 

834@g.command('clean-user-spell-dict') 

835def clean_user_spell_dict(event): 

836 """ 

837 Clean the user spelling dictionary used *only* by the default spell 

838 checker. Mostly for debugging, because this happens automatically. 

839 

840 This command works regardless of the spell checker being used. 

841 """ 

842 c = event and event.get('c') 

843 if c: 

844 DefaultWrapper(c).save_user_dict(trace=True) 

845#@-others 

846#@@language python 

847#@@tabwidth -4 

848#@-leo