From cf3ca98e31bcd6527f7787fc4e77f3eb590e537b 2011-10-17 08:46:32
From: Matthias BUSSONNIER <bussonniermatthias@gmail.com>
Date: 2011-10-17 08:46:32
Subject: [PATCH] Improve 'save history' menu (%save)

	Real file chooser menu, get the current session history range by default.
	If the file already exist, the user will be prompted twice if he/she really
	want to be override the existing file ( by the gui, and the magic afterward)
	One might also to add the (-r) %save option into the gui.

---

diff --git a/IPython/frontend/qt/console/frontend_widget.py b/IPython/frontend/qt/console/frontend_widget.py
index 41ef33d..363d554 100644
--- a/IPython/frontend/qt/console/frontend_widget.py
+++ b/IPython/frontend/qt/console/frontend_widget.py
@@ -324,6 +324,8 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
 
             self._show_interpreter_prompt_for_reply(msg)
             self.executed.emit(msg)
+        else:
+            super(FrontendWidget, self)._handle_execute_reply(msg)
 
     def _handle_input_request(self, msg):
         """ Handle requests for raw_input.
diff --git a/IPython/frontend/qt/console/history_console_widget.py b/IPython/frontend/qt/console/history_console_widget.py
index 66e9c35..dcf3c96 100644
--- a/IPython/frontend/qt/console/history_console_widget.py
+++ b/IPython/frontend/qt/console/history_console_widget.py
@@ -219,16 +219,44 @@ class HistoryConsoleWidget(ConsoleWidget):
     def history_magic(self):
         self.pasteMagic("%history")
 
+    def _request_update_session_history_length(self):
+        msg_id = self.kernel_manager.shell_channel.execute('',
+            silent=True,
+            user_expressions={
+                'hlen':'len(get_ipython().history_manager.input_hist_raw)',
+                }
+            )
+        self._request_info['execute'] = self._ExecutionRequest(msg_id, 'save_magic')
+
+    def _handle_execute_reply(self, msg):
+        """ Handles replies for code execution, here only session history length
+        """
+        info = self._request_info.get('execute')
+        if info and info.id == msg['parent_header']['msg_id'] and \
+                info.kind == 'save_magic' and not self._hidden:
+            content = msg['content']
+            status = content['status']
+            if status == 'ok':
+                self._max_session_history=(int(content['user_expressions']['hlen']))
+
     def save_magic(self):
-        file_name, ok = QtGui.QInputDialog.getText(self,
-            'Enter A file Name',
-            'Please enter a filename to wich export the history as python file:',
-            text='untilted.py')
-        if ok:
+        # update the session history length
+        self._request_update_session_history_length()
+
+        file_name,extFilter = QtGui.QFileDialog.getSaveFileName(self,
+            "Enter A filename",
+            filter='Python File (*.py);; All files (*.*)'
+            )
+
+        # let's the user search/type for a file name, while the history length
+        # is fetched
+
+        if file_name:
             hist_range, ok = QtGui.QInputDialog.getText(self,
                 'Please enter an interval of command to save',
                 'Saving commands:',
-                text='1-500')
+                text=str('1-'+str(self._max_session_history))
+                )
             if ok:
                 self.pasteMagic("%save"+" "+file_name+" "+str(hist_range))