do not polute history in ipython, fixes #5

This commit is contained in:
Valentin Boettcher 2025-01-21 14:47:32 -05:00
parent 9c3a1feeac
commit 6cce934b0a
No known key found for this signature in database
GPG key ID: E034E12B7AF56ACE
2 changed files with 57 additions and 17 deletions

View file

@ -148,6 +148,19 @@ If SESSION-NAME is not given, the default session name `main' is assumed."
"A timer that is used to determine the interpreter upon launch.") "A timer that is used to determine the interpreter upon launch.")
(defun py-vterm-interaction--ipython-delete-history-string (num)
"Return python code to remove the last NUM ipython history entries.
If the interpreter is not ipython an empty string is returned."
(with-current-buffer (py-vterm-interaction-fellow-repl-buffer)
(if (eq py-vterm-interaction-repl-interpreter :ipython)
(format (concat "hismgr = get_ipython().history_manager;"
"session_id = hismgr.get_last_session_id();"
"hismgr.db.execute('DELETE from history where rowid in"
" (Select rowid FROM history WHERE session={0} order by "
"line DESC LIMIT %i)'.format(hismgr.get_last_session_id()))"
";del hismgr") num)
"")))
(defun py-vterm-interaction--launch (ses-name env context) (defun py-vterm-interaction--launch (ses-name env context)
"Launch a new Python REPL buffer with SES-NAME and ENV. "Launch a new Python REPL buffer with SES-NAME and ENV.
@ -166,20 +179,24 @@ python interpreter is ipython. This times out after
(setq py-vterm-interaction-repl-script-buffer (plist-get context :script-buffer))) (setq py-vterm-interaction-repl-script-buffer (plist-get context :script-buffer)))
(py-vterm-interaction-repl-mode) (py-vterm-interaction-repl-mode)
(push (cons id (push
(run-with-timer .1 1 (cons id
(lambda (buffer) (run-with-timer .1 1
(let ((timer (alist-get id py-vterm-interaction-repl--launch-timers))) (lambda (buffer)
(if (and buffer (buffer-live-p buffer)) (let ((timer (alist-get id py-vterm-interaction-repl--launch-timers)))
(if (py-vterm-interaction-repl-prompt-status) (if (and buffer (buffer-live-p buffer))
(progn (if (py-vterm-interaction-repl-prompt-status)
(setq py-vterm-interaction-repl-interpreter (progn
(if (eq (py-vterm-interaction--execute-script "is_ipython") :false) (setq py-vterm-interaction-repl-interpreter
:python :ipython)) (if (eq (py-vterm-interaction--execute-script "is_ipython") :false)
(cancel-timer timer))) :python :ipython))
(cancel-timer timer)))) (cancel-timer timer)
new-buffer)) (if (eq py-vterm-interaction-repl-interpreter :ipython)
py-vterm-interaction-repl--launch-timers) (progn
(py-vterm-interaction--execute-script "delete_history" 1)))))
(cancel-timer timer))))
new-buffer))
py-vterm-interaction-repl--launch-timers)
(add-function :filter-args (process-filter vterm--process) (add-function :filter-args (process-filter vterm--process)
(py-vterm-interaction-repl-run-filter-functions-func ses-name)) (py-vterm-interaction-repl-run-filter-functions-func ses-name))
(setq-local py-vterm-interaction-session ses-name)) (setq-local py-vterm-interaction-session ses-name))
@ -336,6 +353,10 @@ will be cleaned up afterwards."
(py-vterm-interaction-paste-string (format "del %s" name)) (py-vterm-interaction-paste-string (format "del %s" name))
(py-vterm-interaction-send-return-key) (py-vterm-interaction-send-return-key)
(py-vterm-interaction-paste-string (py-vterm-interaction--ipython-delete-history-string 2))
(py-vterm-interaction-send-return-key)
(with-timeout (py-vterm-interaction-repl-script-timeout (with-timeout (py-vterm-interaction-repl-script-timeout
(progn (display-warning 'py-vterm-interaction "Python script did not finish in time.") (progn (display-warning 'py-vterm-interaction "Python script did not finish in time.")
(delete-file tmpfile) (delete-file tmpfile)
@ -520,13 +541,15 @@ Optional argument COMMENT will be appended as a comment in the repl."
If PY-VTERM-INTERACTION--SEND-MAYBE-SILENT is non-nil, uses If PY-VTERM-INTERACTION--SEND-MAYBE-SILENT is non-nil, uses
`%run -i' with a temp file. Optional argument COMMENT will be `%run -i' with a temp file. Optional argument COMMENT will be
appended as a comment in the repl." appended as a comment in the repl."
(with-current-buffer (py-vterm-interaction-fellow-repl-buffer) (with-current-buffer (py-vterm-interaction-fellow-repl-buffer)
(if py-vterm-interaction-silent-cells (if py-vterm-interaction-silent-cells
(let ((tmpfile (make-temp-file "py-vterm-interaction" nil ".py"))) (let ((tmpfile (make-temp-file "py-vterm-interaction" nil ".py")))
(with-temp-file tmpfile (with-temp-file tmpfile
(insert string)) (insert string)
(py-vterm-interaction-paste-string (py-vterm-interaction--load-file tmpfile comment)) (insert "\n")
(insert (py-vterm-interaction--ipython-delete-history-string 1)))
(py-vterm-interaction-paste-string
(py-vterm-interaction--load-file tmpfile comment))
(run-with-timer 10 nil (run-with-timer 10 nil
(lambda (tmpfile) (lambda (tmpfile)
(delete-file tmpfile)) (delete-file tmpfile))

17
scripts/delete_history.py Normal file
View file

@ -0,0 +1,17 @@
def delete_history(tmpfile, num):
try:
if not __IPYTHON__:
dump_json(tmpfile, False)
return
hismgr = get_ipython().history_manager
hismgr.db.execute(
"DELETE from history where rowid in (Select rowid FROM history WHERE session={0} order by line DESC LIMIT {1})".format(
hismgr.get_last_session_id(), num
)
)
del hismgr
dump_json(tmpfile, True)
except NameError:
dump_json(tmpfile, True)