~pyreadline/pyreadline/trunk

« back to all changes in this revision

Viewing changes to pyreadline/lineeditor/history.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:
140
140
    def reverse_search_history(self, searchfor, startpos=None):
141
141
        if startpos is None:
142
142
            startpos = self.history_cursor
 
143
        origpos = startpos
143
144
 
144
145
        result =  lineobj.ReadLineTextBuffer("")
145
146
 
150
151
 
151
152
        #If we get a new search without change in search term it means
152
153
        #someone pushed ctrl-r and we should find the next match
153
 
        if self.last_search_for == searchfor:
 
154
        if self.last_search_for == searchfor and startpos > 0:
154
155
            startpos -= 1
155
156
            for idx, line in list(enumerate(self.history))[startpos:0:-1]:
156
157
                if searchfor in line:
157
158
                    startpos = idx
158
159
                    break
159
160
 
160
 
        result = self.history[startpos]
 
161
        if self.history:                    
 
162
            result = self.history[startpos].get_line_text()
 
163
        else:
 
164
            result = u""
161
165
        self.history_cursor = startpos
162
166
        self.last_search_for = searchfor
163
 
        return result.get_line_text()
 
167
        log(u"reverse_search_history: old:%d new:%d result:%r"%(origpos, self.history_cursor, result))
 
168
        return result
164
169
        
165
170
    def forward_search_history(self, searchfor, startpos=None):
166
171
        if startpos is None:
167
 
            startpos = self.history_cursor
 
172
            startpos = min(self.history_cursor, max(0, self.get_current_history_length()-1))
168
173
        origpos = startpos
169
174
        
170
175
        result =  lineobj.ReadLineTextBuffer("")
176
181
 
177
182
        #If we get a new search without change in search term it means
178
183
        #someone pushed ctrl-r and we should find the next match
179
 
        if self.last_search_for == searchfor:
 
184
        if self.last_search_for == searchfor and startpos < self.get_current_history_length()-1:
180
185
            startpos += 1
181
186
            for idx, line in list(enumerate(self.history))[startpos:]:
182
187
                if searchfor in line:
183
188
                    startpos = idx
184
189
                    break
185
 
        if len(self.history) == startpos:
186
 
            if origpos == len(self.history):
187
 
                return u""
188
 
            else:
189
 
                return self.history[origpos]
 
190
 
 
191
        if self.history:                    
 
192
            result = self.history[startpos].get_line_text()
190
193
        else:
191
 
            result = self.history[startpos]
 
194
            result = u""
192
195
        self.history_cursor = startpos
193
196
        self.last_search_for = searchfor
194
 
        return result.get_line_text()
 
197
        return result
195
198
 
196
199
    def _search(self, direction, partial):
197
200
        try:
247
250
        return q
248
251
 
249
252
if __name__==u"__main__":
 
253
    import pdb
250
254
    q = LineHistory()
 
255
    r = LineHistory()
 
256
    s = LineHistory()
251
257
    RL = lineobj.ReadLineTextBuffer
252
258
    q.add_history(RL(u"aaaa"))
253
259
    q.add_history(RL(u"aaba"))
255
261
    q.add_history(RL(u"akca"))
256
262
    q.add_history(RL(u"bbb"))
257
263
    q.add_history(RL(u"ako"))
 
264
    r.add_history(RL(u"ako"))