~pyreadline/pyreadline/trunk

« back to all changes in this revision

Viewing changes to pyreadline/modes/emacs.py

  • Committer: Jorgen Stenarson
  • Date: 2011-04-14 18:36:47 UTC
  • Revision ID: jorgen.stenarson@bostream.nu-20110414183647-4ki7dybb2mbb6z8g
Finalizing incremental search

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
        pass
32
32
 
33
33
    def _process_incremental_search_keyevent(self, keyinfo):
 
34
        log("_process_incremental_search_keyevent")
34
35
        keytuple = keyinfo.tuple()
 
36
        #dispatch_func = self.key_dispatch.get(keytuple, default)
 
37
        revtuples = []
 
38
        fwdtuples = []
 
39
        for ktuple, func in self.key_dispatch.iteritems():
 
40
            if func == self.reverse_search_history:
 
41
                revtuples.append(ktuple)
 
42
            elif func == self.forward_search_history:
 
43
                fwdtuples.append(ktuple)
 
44
        
 
45
        
35
46
        log(u"IncrementalSearchPromptMode %s %s"%(keyinfo, keytuple))
36
47
        if keyinfo.keyname == u'backspace':
37
48
            self.subsearch_query = self.subsearch_query[:-1]
47
58
            self._history.history_cursor = len(self._history.history)
48
59
            if keyinfo.keyname == u'escape':
49
60
                self.l_buffer.set_line(self.subsearch_old_line)
50
 
            return False
 
61
            return True
51
62
        elif keyinfo.keyname:
52
63
            pass
53
 
        elif keytuple == self.subsearch_init_event:
54
 
            self._history.history_cursor += self.subsearch_direction
 
64
        elif keytuple in revtuples:
 
65
            self.subsearch_fun = self._history.reverse_search_history
 
66
            self.subsearch_prompt = u"reverse-i-search%d`%s': "
 
67
            self.line = self.subsearch_fun(self.subsearch_query)
 
68
        elif keytuple in fwdtuples:
 
69
            self.subsearch_fun = self._history.forward_search_history
 
70
            self.subsearch_prompt = u"forward-i-search%d`%s': "
55
71
            self.line = self.subsearch_fun(self.subsearch_query)
56
72
        elif keyinfo.control == False and keyinfo.meta == False:
57
73
            self.subsearch_query += keyinfo.char
58
74
            self.line = self.subsearch_fun(self.subsearch_query)
59
75
        else:
60
76
            pass
61
 
        self.prompt = self.subsearch_prompt%self.subsearch_query
 
77
        self.prompt = self.subsearch_prompt%(self._history.history_cursor, self.subsearch_query)
62
78
        self.l_buffer.set_line(self.line)
63
79
 
64
 
    def _init_incremental_search(self, searchfun, direction, init_event):
 
80
    def _init_incremental_search(self, searchfun, init_event):
65
81
        u"""Initialize search prompt
66
82
        """
67
 
        self.subsearch_init_event = init_event.tuple()
68
 
        self.subsearch_direction = direction
 
83
        log("init_incremental_search")
69
84
        self.subsearch_query = u''
70
85
        self.subsearch_fun = searchfun
71
86
        self.subsearch_old_line = self.l_buffer.get_line_text()
79
94
            self.previous_func != self.forward_search_history):
80
95
            self.subsearch_query = self.l_buffer[0:Point].get_line_text()
81
96
 
82
 
        if self.subsearch_direction < 0:
83
 
            self.subsearch_prompt = u"reverse-i-search`%s': "
 
97
        if self.subsearch_fun == self.reverse_search_history:
 
98
            self.subsearch_prompt = u"reverse-i-search%d`%s': "
84
99
        else:
85
 
            self.subsearch_prompt = u"forward-i-search`%s': "
 
100
            self.subsearch_prompt = u"forward-i-search%d`%s': "
86
101
 
87
 
        self.prompt = self.subsearch_prompt%""
 
102
        self.prompt = self.subsearch_prompt%(self._history.history_cursor, "")
88
103
 
89
104
        if self.subsearch_query:
90
105
            self.line = self._process_incremental_search_keyevent(init_event)
302
317
    def reverse_search_history(self, e):  # (C-r)
303
318
        u'''Search backward starting at the current line and moving up
304
319
        through the history as necessary. This is an incremental search.'''
305
 
        self._init_incremental_search(self._history.reverse_search_history,
306
 
                                      -1, e)
 
320
        log("rev_search_history")
 
321
        self._init_incremental_search(self._history.reverse_search_history, e)
307
322
        self.finalize()
308
323
 
309
324
    def forward_search_history(self, e):  # (C-s)
310
325
        u'''Search forward starting at the current line and moving down
311
326
        through the the history as necessary. This is an incremental
312
327
        search.'''
313
 
        self._init_incremental_search(self._history.forward_search_history,
314
 
                                      1, e)
 
328
        log("fwd_search_history")
 
329
        self._init_incremental_search(self._history.forward_search_history, e)
315
330
        self.finalize()
316
331
 
317
332
    def history_search_forward(self, e):  # ()
644
659
        self._bind_key(u'Alt->',             self.end_of_history)
645
660
        self._bind_key(u'Control-r',         self.reverse_search_history)
646
661
        self._bind_key(u'Control-s',         self.forward_search_history)
 
662
        self._bind_key(u'Control-Shift-r',         self.forward_search_history)
647
663
        self._bind_key(u'Alt-p',
648
664
                       self.non_incremental_reverse_search_history)
649
665
        self._bind_key(u'Alt-n',