home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / IdleHistory.py < prev    next >
Text File  |  2003-12-30  |  3KB  |  87 lines

  1. class History:
  2.  
  3.     def __init__(self, text, output_sep = "\n"):
  4.         self.text = text
  5.         self.history = []
  6.         self.history_prefix = None
  7.         self.history_pointer = None
  8.         self.output_sep = output_sep
  9.         text.bind("<<history-previous>>", self.history_prev)
  10.         text.bind("<<history-next>>", self.history_next)
  11.  
  12.     def history_next(self, event):
  13.         self.history_do(0)
  14.         return "break"
  15.  
  16.     def history_prev(self, event):
  17.         self.history_do(1)
  18.         return "break"
  19.  
  20.     def _get_source(self, start, end):
  21.         # Get source code from start index to end index.  Lines in the
  22.         # text control may be separated by sys.ps2 .
  23.         lines = self.text.get(start, end).split(self.output_sep)
  24.         return "\n".join(lines)
  25.  
  26.     def _put_source(self, where, source):
  27.         output = self.output_sep.join(source.split("\n"))
  28.         self.text.insert(where, output)
  29.  
  30.     def history_do(self, reverse):
  31.         nhist = len(self.history)
  32.         pointer = self.history_pointer
  33.         prefix = self.history_prefix
  34.         if pointer is not None and prefix is not None:
  35.             if self.text.compare("insert", "!=", "end-1c") or \
  36.                self._get_source("iomark", "end-1c") != self.history[pointer]:
  37.                 pointer = prefix = None
  38.         if pointer is None or prefix is None:
  39.             prefix = self._get_source("iomark", "end-1c")
  40.             if reverse:
  41.                 pointer = nhist
  42.             else:
  43.                 pointer = -1
  44.         nprefix = len(prefix)
  45.         while 1:
  46.             if reverse:
  47.                 pointer = pointer - 1
  48.             else:
  49.                 pointer = pointer + 1
  50.             if pointer < 0 or pointer >= nhist:
  51.                 self.text.bell()
  52.                 if self._get_source("iomark", "end-1c") != prefix:
  53.                     self.text.delete("iomark", "end-1c")
  54.                     self._put_source("iomark", prefix)
  55.                 pointer = prefix = None
  56.                 break
  57.             item = self.history[pointer]
  58.             if item[:nprefix] == prefix and len(item) > nprefix:
  59.                 self.text.delete("iomark", "end-1c")
  60.                 self._put_source("iomark", item)
  61.                 break
  62.         self.text.mark_set("insert", "end-1c")
  63.         self.text.see("insert")
  64.         self.text.tag_remove("sel", "1.0", "end")
  65.         self.history_pointer = pointer
  66.         self.history_prefix = prefix
  67.  
  68.     def history_store(self, source):
  69.         source = source.strip()
  70.         if len(source) > 2:
  71.             # avoid duplicates
  72.             try:
  73.                 self.history.remove(source)
  74.             except ValueError:
  75.                 pass
  76.             self.history.append(source)
  77.         self.history_pointer = None
  78.         self.history_prefix = None
  79.  
  80.     def recall(self, s):
  81.         s = s.strip()
  82.         self.text.tag_remove("sel", "1.0", "end")
  83.         self.text.delete("iomark", "end-1c")
  84.         self.text.mark_set("insert", "end-1c")
  85.         self.text.insert("insert", s)
  86.         self.text.see("insert")
  87.