Skip to content
This repository

Qt Console history search #627

Closed
wants to merge 1 commit into from

4 participants

Benjamin Thyreau Thomas Kluyver Evan Patterson Fernando Perez
Benjamin Thyreau

Using Qt console from IPython dev repo, i found out i'm missing the "Ctrl-R" feature from readline (search in
history), a feature difficult to emulate exactly.

This suggested patch make use of the shift modifier to implement the same behavior in an easier way.

Thomas Kluyver
Collaborator

Thanks, Benjamin. We're focussing on getting 0.11 out now, so it might be a few days before someone goes over this.

Evan Patterson
Collaborator

The patch looks good to me. It would be nice to add a note to guiref so this has a hope of being discovered.

But like Thomas said, with 0.11 so near it is probably best to hold off on the merge for now. Thanks again for the patch.

Fernando Perez
Owner
fperez commented July 29, 2011

Benjamin, if you have a chance to update this PR with a small note about the feature in %guiref, located in /zmq/zmqshell.py, that would be awesome. We won't have a chance to merge this for a few more days (trying to actually release 0.11 :) so there's no rush.

Very happy to have this feature, it's been requested a lot already. Many thanks for the work!!

Evan Patterson
Collaborator

I cherry picked the commit, added a note to %guiref, and pushed to master. Thanks for the patch!

Evan Patterson epatters closed this August 04, 2011
Fernando Perez
Owner

@epatters, much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
28  IPython/frontend/qt/console/history_console_widget.py
@@ -82,7 +82,8 @@ def _up_pressed(self, shift_modifier):
82 82
                 self._history_prefix = input_buffer[:col]
83 83
 
84 84
             # Perform the search.
85  
-            self.history_previous(self._history_prefix)
  85
+            self.history_previous(self._history_prefix,
  86
+                                    as_prefix=not shift_modifier)
86 87
 
87 88
             # Go to the first line of the prompt for seemless history scrolling.
88 89
             # Emulate readline: keep the cursor position fixed for a prefix
@@ -110,7 +111,8 @@ def _down_pressed(self, shift_modifier):
110 111
                 return False
111 112
 
112 113
             # Perform the search.
113  
-            replaced = self.history_next(self._history_prefix)
  114
+            replaced = self.history_next(self._history_prefix, 
  115
+                                            as_prefix=not shift_modifier)
114 116
 
115 117
             # Emulate readline: keep the cursor position fixed for a prefix
116 118
             # search. (We don't need to move the cursor to the end of the buffer
@@ -130,13 +132,15 @@ def _down_pressed(self, shift_modifier):
130 132
     # 'HistoryConsoleWidget' public interface
131 133
     #---------------------------------------------------------------------------
132 134
 
133  
-    def history_previous(self, prefix=''):
  135
+    def history_previous(self, substring='', as_prefix=True):
134 136
         """ If possible, set the input buffer to a previous history item.
135 137
 
136 138
         Parameters:
137 139
         -----------
138  
-        prefix : str, optional
139  
-            If specified, search for an item with this prefix.
  140
+        substring : str, optional
  141
+            If specified, search for an item with this substring.
  142
+        as_prefix : bool, optional
  143
+            If True, the substring must match at the beginning (default).
140 144
 
141 145
         Returns:
142 146
         --------
@@ -147,7 +151,8 @@ def history_previous(self, prefix=''):
147 151
         while index > 0:
148 152
             index -= 1
149 153
             history = self._get_edited_history(index)
150  
-            if history.startswith(prefix):
  154
+            if (as_prefix and history.startswith(substring)) \
  155
+                or (not as_prefix and substring in history):
151 156
                 replace = True
152 157
                 break
153 158
         
@@ -158,13 +163,15 @@ def history_previous(self, prefix=''):
158 163
 
159 164
         return replace
160 165
 
161  
-    def history_next(self, prefix=''):
  166
+    def history_next(self, substring='', as_prefix=True):
162 167
         """ If possible, set the input buffer to a subsequent history item.
163 168
 
164 169
         Parameters:
165 170
         -----------
166  
-        prefix : str, optional
167  
-            If specified, search for an item with this prefix.
  171
+        substring : str, optional
  172
+            If specified, search for an item with this substring.
  173
+        as_prefix : bool, optional
  174
+            If True, the substring must match at the beginning (default).
168 175
 
169 176
         Returns:
170 177
         --------
@@ -175,7 +182,8 @@ def history_next(self, prefix=''):
175 182
         while self._history_index < len(self._history):
176 183
             index += 1
177 184
             history = self._get_edited_history(index)
178  
-            if history.startswith(prefix):
  185
+            if (as_prefix and history.startswith(substring)) \
  186
+                or (not as_prefix and substring in history):
179 187
                 replace = True
180 188
                 break
181 189
             
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.