AutoExpand.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. '''Complete the current word before the cursor with words in the editor.
  2. Each menu selection or shortcut key selection replaces the word with a
  3. different word with the same prefix. The search for matches begins
  4. before the target and moves toward the top of the editor. It then starts
  5. after the cursor and moves down. It then returns to the original word and
  6. the cycle starts again.
  7. Changing the current text line or leaving the cursor in a different
  8. place before requesting the next selection causes AutoExpand to reset
  9. its state.
  10. This is an extension file and there is only one instance of AutoExpand.
  11. '''
  12. import string
  13. import re
  14. ###$ event <<expand-word>>
  15. ###$ win <Alt-slash>
  16. ###$ unix <Alt-slash>
  17. class AutoExpand:
  18. menudefs = [
  19. ('edit', [
  20. ('E_xpand Word', '<<expand-word>>'),
  21. ]),
  22. ]
  23. wordchars = string.ascii_letters + string.digits + "_"
  24. def __init__(self, editwin):
  25. self.text = editwin.text
  26. self.state = None
  27. def expand_word_event(self, event):
  28. "Replace the current word with the next expansion."
  29. curinsert = self.text.index("insert")
  30. curline = self.text.get("insert linestart", "insert lineend")
  31. if not self.state:
  32. words = self.getwords()
  33. index = 0
  34. else:
  35. words, index, insert, line = self.state
  36. if insert != curinsert or line != curline:
  37. words = self.getwords()
  38. index = 0
  39. if not words:
  40. self.text.bell()
  41. return "break"
  42. word = self.getprevword()
  43. self.text.delete("insert - %d chars" % len(word), "insert")
  44. newword = words[index]
  45. index = (index + 1) % len(words)
  46. if index == 0:
  47. self.text.bell() # Warn we cycled around
  48. self.text.insert("insert", newword)
  49. curinsert = self.text.index("insert")
  50. curline = self.text.get("insert linestart", "insert lineend")
  51. self.state = words, index, curinsert, curline
  52. return "break"
  53. def getwords(self):
  54. "Return a list of words that match the prefix before the cursor."
  55. word = self.getprevword()
  56. if not word:
  57. return []
  58. before = self.text.get("1.0", "insert wordstart")
  59. wbefore = re.findall(r"\b" + word + r"\w+\b", before)
  60. del before
  61. after = self.text.get("insert wordend", "end")
  62. wafter = re.findall(r"\b" + word + r"\w+\b", after)
  63. del after
  64. if not wbefore and not wafter:
  65. return []
  66. words = []
  67. dict = {}
  68. # search backwards through words before
  69. wbefore.reverse()
  70. for w in wbefore:
  71. if dict.get(w):
  72. continue
  73. words.append(w)
  74. dict[w] = w
  75. # search onwards through words after
  76. for w in wafter:
  77. if dict.get(w):
  78. continue
  79. words.append(w)
  80. dict[w] = w
  81. words.append(word)
  82. return words
  83. def getprevword(self):
  84. "Return the word prefix before the cursor."
  85. line = self.text.get("insert linestart", "insert")
  86. i = len(line)
  87. while i > 0 and line[i-1] in self.wordchars:
  88. i = i-1
  89. return line[i:]
  90. if __name__ == '__main__':
  91. import unittest
  92. unittest.main('idlelib.idle_test.test_autoexpand', verbosity=2)