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.")
(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)
"Launch a new Python REPL buffer with SES-NAME and ENV.
@ -166,7 +179,8 @@ python interpreter is ipython. This times out after
(setq py-vterm-interaction-repl-script-buffer (plist-get context :script-buffer)))
(py-vterm-interaction-repl-mode)
(push (cons id
(push
(cons id
(run-with-timer .1 1
(lambda (buffer)
(let ((timer (alist-get id py-vterm-interaction-repl--launch-timers)))
@ -176,7 +190,10 @@ python interpreter is ipython. This times out after
(setq py-vterm-interaction-repl-interpreter
(if (eq (py-vterm-interaction--execute-script "is_ipython") :false)
:python :ipython))
(cancel-timer timer)))
(cancel-timer timer)
(if (eq py-vterm-interaction-repl-interpreter :ipython)
(progn
(py-vterm-interaction--execute-script "delete_history" 1)))))
(cancel-timer timer))))
new-buffer))
py-vterm-interaction-repl--launch-timers)
@ -336,6 +353,10 @@ will be cleaned up afterwards."
(py-vterm-interaction-paste-string (format "del %s" name))
(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
(progn (display-warning 'py-vterm-interaction "Python script did not finish in time.")
(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
`%run -i' with a temp file. Optional argument COMMENT will be
appended as a comment in the repl."
(with-current-buffer (py-vterm-interaction-fellow-repl-buffer)
(if py-vterm-interaction-silent-cells
(let ((tmpfile (make-temp-file "py-vterm-interaction" nil ".py")))
(with-temp-file tmpfile
(insert string))
(py-vterm-interaction-paste-string (py-vterm-interaction--load-file tmpfile comment))
(insert string)
(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
(lambda (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)