Handle negative numbers in jupyter-repl-history-(next|previous)

This commit is contained in:
Nathaniel Nicandro 2018-01-14 00:04:05 -06:00
parent 133be4aa92
commit 4d82867ab4

View file

@ -826,54 +826,58 @@ lines then truncate it to something less than
(jupyter-repl-newline) (jupyter-repl-newline)
(jupyter-repl-insert (concat prompt value))))) (jupyter-repl-insert (concat prompt value)))))
(defun jupyter-repl-history-next (n) (defun jupyter-repl-history-next (&optional n)
"Go to the next history element. "Go to the next history element.
Navigate through the REPL history to the next (newer) history Navigate through the REPL history to the next (newer) history
element and insert it as the last code cell. For N positive move element and insert it as the last code cell. For N positive move
forward in history that many times. If N is negative, move to forward in history that many times. If N is negative, move to
older history elements." older history elements."
(interactive "p") (interactive "p")
(goto-char (point-max)) (or n (setq n 1))
(if (cl-loop (if (< n 0) (jupyter-repl-history-previous (- n))
repeat (or n 1) (goto-char (point-max))
thereis (eq (ring-ref jupyter-repl-history -1) 'jupyter-repl-history) (if (cl-loop
do (ring-insert repeat n
jupyter-repl-history (ring-remove jupyter-repl-history -1))) thereis (eq (ring-ref jupyter-repl-history -1) 'jupyter-repl-history)
(cond do (ring-insert
((equal (jupyter-repl-cell-code) jupyter-repl-history (ring-remove jupyter-repl-history -1)))
(ring-ref jupyter-repl-history 0)) (cond
(jupyter-repl-replace-cell-code "")) ((equal (jupyter-repl-cell-code)
((equal (jupyter-repl-cell-code) "") (ring-ref jupyter-repl-history 0))
(message "Beginning of history")) (jupyter-repl-replace-cell-code ""))
(t)) ((equal (jupyter-repl-cell-code) "")
(jupyter-repl-replace-cell-code (message "Beginning of history"))
(ring-ref jupyter-repl-history 0)))) (t))
(jupyter-repl-replace-cell-code
(ring-ref jupyter-repl-history 0)))))
(defun jupyter-repl-history-previous (n) (defun jupyter-repl-history-previous (&optional n)
"Go to the previous history element. "Go to the previous history element.
Similar to `jupyter-repl-history-next' but for older history Similar to `jupyter-repl-history-next' but for older history
elements. If N is negative in this case, move to newer history elements. If N is negative in this case, move to newer history
elements." elements."
(interactive "p") (interactive "p")
(goto-char (point-max)) (or n (setq n 1))
(if (not (equal (jupyter-repl-cell-code) (if (< n 0) (jupyter-repl-history-next (- n))
(ring-ref jupyter-repl-history 0))) (goto-char (point-max))
(jupyter-repl-replace-cell-code (ring-ref jupyter-repl-history 0)) (if (not (equal (jupyter-repl-cell-code)
(if (cl-loop (ring-ref jupyter-repl-history 0)))
repeat (or n 1) (jupyter-repl-replace-cell-code (ring-ref jupyter-repl-history 0))
thereis (eq (ring-ref jupyter-repl-history 1) 'jupyter-repl-history) (if (cl-loop
do (ring-insert-at-beginning repeat n
jupyter-repl-history (ring-remove jupyter-repl-history 0))) thereis (eq (ring-ref jupyter-repl-history 1) 'jupyter-repl-history)
(message "End of history") do (ring-insert-at-beginning
(jupyter-repl-replace-cell-code jupyter-repl-history (ring-remove jupyter-repl-history 0)))
(ring-ref jupyter-repl-history 0))))) (message "End of history")
(jupyter-repl-replace-cell-code
(ring-ref jupyter-repl-history 0))))))
(cl-defmethod jupyter-handle-history-reply ((client jupyter-repl-client) req history) (cl-defmethod jupyter-handle-history-reply ((client jupyter-repl-client) req history)
(with-jupyter-repl-buffer client (with-jupyter-repl-buffer client
(cl-loop for (session line-number input-output) in history (cl-loop for (_session _line-number input-output) in history
do (ring-insert jupyter-repl-history input-output)))) do (ring-remove+insert+extend jupyter-repl-history input-output))))
(cl-defmethod jupyter-handle-is-complete-reply ((client jupyter-repl-client) req status indent) (cl-defmethod jupyter-handle-is-complete-reply ((client jupyter-repl-client) _req status indent)
(with-jupyter-repl-buffer client (with-jupyter-repl-buffer client
(pcase status (pcase status
("complete" ("complete"