Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AutoExpand.py 2.7KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import re
  2. import string
  3. ###$ event <<expand-word>>
  4. ###$ win <Alt-slash>
  5. ###$ unix <Alt-slash>
  6. class AutoExpand:
  7. keydefs = {
  8. "<<expand-word>>": ["<Alt-slash>"],
  9. }
  10. unix_keydefs = {
  11. "<<expand-word>>": ["<Meta-slash>"],
  12. }
  13. menudefs = [
  14. (
  15. "edit",
  16. [
  17. ("E_xpand word", "<<expand-word>>"),
  18. ],
  19. ),
  20. ]
  21. wordchars = string.ascii_letters + string.digits + "_"
  22. def __init__(self, editwin):
  23. self.text = editwin.text
  24. self.text.wordlist = None # XXX what is this?
  25. self.state = None
  26. def expand_word_event(self, event):
  27. curinsert = self.text.index("insert")
  28. curline = self.text.get("insert linestart", "insert lineend")
  29. if not self.state:
  30. words = self.getwords()
  31. index = 0
  32. else:
  33. words, index, insert, line = self.state
  34. if insert != curinsert or line != curline:
  35. words = self.getwords()
  36. index = 0
  37. if not words:
  38. self.text.bell()
  39. return "break"
  40. word = self.getprevword()
  41. self.text.delete("insert - %d chars" % len(word), "insert")
  42. newword = words[index]
  43. index = (index + 1) % len(words)
  44. if index == 0:
  45. self.text.bell() # Warn we cycled around
  46. self.text.insert("insert", newword)
  47. curinsert = self.text.index("insert")
  48. curline = self.text.get("insert linestart", "insert lineend")
  49. self.state = words, index, curinsert, curline
  50. return "break"
  51. def getwords(self):
  52. word = self.getprevword()
  53. if not word:
  54. return []
  55. before = self.text.get("1.0", "insert wordstart")
  56. wbefore = re.findall(r"\b" + word + r"\w+\b", before)
  57. del before
  58. after = self.text.get("insert wordend", "end")
  59. wafter = re.findall(r"\b" + word + r"\w+\b", after)
  60. del after
  61. if not wbefore and not wafter:
  62. return []
  63. words = []
  64. dict = {}
  65. # search backwards through words before
  66. wbefore.reverse()
  67. for w in wbefore:
  68. if dict.get(w):
  69. continue
  70. words.append(w)
  71. dict[w] = w
  72. # search onwards through words after
  73. for w in wafter:
  74. if dict.get(w):
  75. continue
  76. words.append(w)
  77. dict[w] = w
  78. words.append(word)
  79. return words
  80. def getprevword(self):
  81. line = self.text.get("insert linestart", "insert")
  82. i = len(line)
  83. while i > 0 and line[i - 1] in self.wordchars:
  84. i = i - 1
  85. return line[i:]