history.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "Implement Idle Shell history mechanism with History class"
  2. from idlelib.config import idleConf
  3. class History:
  4. ''' Implement Idle Shell history mechanism.
  5. store - Store source statement (called from pyshell.resetoutput).
  6. fetch - Fetch stored statement matching prefix already entered.
  7. history_next - Bound to <<history-next>> event (default Alt-N).
  8. history_prev - Bound to <<history-prev>> event (default Alt-P).
  9. '''
  10. def __init__(self, text):
  11. '''Initialize data attributes and bind event methods.
  12. .text - Idle wrapper of tk Text widget, with .bell().
  13. .history - source statements, possibly with multiple lines.
  14. .prefix - source already entered at prompt; filters history list.
  15. .pointer - index into history.
  16. .cyclic - wrap around history list (or not).
  17. '''
  18. self.text = text
  19. self.history = []
  20. self.prefix = None
  21. self.pointer = None
  22. self.cyclic = idleConf.GetOption("main", "History", "cyclic", 1, "bool")
  23. text.bind("<<history-previous>>", self.history_prev)
  24. text.bind("<<history-next>>", self.history_next)
  25. def history_next(self, event):
  26. "Fetch later statement; start with earliest if cyclic."
  27. self.fetch(reverse=False)
  28. return "break"
  29. def history_prev(self, event):
  30. "Fetch earlier statement; start with most recent."
  31. self.fetch(reverse=True)
  32. return "break"
  33. def fetch(self, reverse):
  34. '''Fetch statement and replace current line in text widget.
  35. Set prefix and pointer as needed for successive fetches.
  36. Reset them to None, None when returning to the start line.
  37. Sound bell when return to start line or cannot leave a line
  38. because cyclic is False.
  39. '''
  40. nhist = len(self.history)
  41. pointer = self.pointer
  42. prefix = self.prefix
  43. if pointer is not None and prefix is not None:
  44. if self.text.compare("insert", "!=", "end-1c") or \
  45. self.text.get("iomark", "end-1c") != self.history[pointer]:
  46. pointer = prefix = None
  47. self.text.mark_set("insert", "end-1c") # != after cursor move
  48. if pointer is None or prefix is None:
  49. prefix = self.text.get("iomark", "end-1c")
  50. if reverse:
  51. pointer = nhist # will be decremented
  52. else:
  53. if self.cyclic:
  54. pointer = -1 # will be incremented
  55. else: # abort history_next
  56. self.text.bell()
  57. return
  58. nprefix = len(prefix)
  59. while True:
  60. pointer += -1 if reverse else 1
  61. if pointer < 0 or pointer >= nhist:
  62. self.text.bell()
  63. if not self.cyclic and pointer < 0: # abort history_prev
  64. return
  65. else:
  66. if self.text.get("iomark", "end-1c") != prefix:
  67. self.text.delete("iomark", "end-1c")
  68. self.text.insert("iomark", prefix, "stdin")
  69. pointer = prefix = None
  70. break
  71. item = self.history[pointer]
  72. if item[:nprefix] == prefix and len(item) > nprefix:
  73. self.text.delete("iomark", "end-1c")
  74. self.text.insert("iomark", item, "stdin")
  75. break
  76. self.text.see("insert")
  77. self.text.tag_remove("sel", "1.0", "end")
  78. self.pointer = pointer
  79. self.prefix = prefix
  80. def store(self, source):
  81. "Store Shell input statement into history list."
  82. source = source.strip()
  83. if len(source) > 2:
  84. # avoid duplicates
  85. try:
  86. self.history.remove(source)
  87. except ValueError:
  88. pass
  89. self.history.append(source)
  90. self.pointer = None
  91. self.prefix = None
  92. if __name__ == "__main__":
  93. from unittest import main
  94. main('idlelib.idle_test.test_history', verbosity=2, exit=False)