Skip to content

Commit

Permalink
Add cider-history command.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnv02139 committed Oct 7, 2016
1 parent 587bbe3 commit 764b529
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
121 changes: 121 additions & 0 deletions cider-history.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
;;; cider-history.el --- Basic REPL input history browser

;; Copyright (c) 2016 John Valente, Bozhidar Batsov and CIDER contributors

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.

;; This file is not part of GNU Emacs.

;;; Commentary:

;; Basic REPL input history browser for CIDER.

;;; Code:

(require 'cider-client)
(require 'cider-popup)
(require 'cider-compat)

(defvar cider-history-buffer "*cider-history*")

(push cider-history-buffer cider-ancillary-buffers)

(defvar cider-history-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map cider-popup-buffer-mode-map)
(define-key map (kbd "RET") #'cider-history-execute-cmd-at-point)
(define-key map (kbd "SPC") #'cider-history-insert-cmd-at-point)
(define-key map "n" #'next-line)
(define-key map "p" #'previous-line)
map))

(defvar cider-history-mouse-map
(let ((map (make-sparse-keymap)))
(define-key map [mouse-1] #'cider-history-handle-mouse)
map))

(define-derived-mode cider-history-mode clojure-mode "history"
"Major mode for browsing the entries in CIDER's input history.
\\{cider-history-mode-map}"
(setq buffer-read-only t)
(setq-local electric-indent-chars nil)
(setq-local truncate-lines t))


;; Everything works great as long as the commands are a single line.
;; When that's true, selecting the current command is simply a matter
;; of the text from the beginning of the line to the end. Unfortunately,
;; Clojure commands often comprise more than one line.

;; There are a number of ways to try to handle this, but none of them
;; seem great. As a first pass, I'm simply going to make sure that all
;; commands *do* fit on a single line, by replacing newlines with spaces.

;; That hack renders it not ready for "master", but ok for a branch.

(defun cider-history-command-text ()
"Extract and return the command at point."
(let ((bol (line-beginning-position))
(eol (line-end-position)))
(buffer-substring-no-properties bol eol)))

(defun format-command-for-cider-history (command)
"Format the command so that `cider-history-command-text' can extract it."
(concat
(replace-regexp-in-string "\n" " " command)
"\n"))

(defun cider-history-list (buffer commands)
"Populate BUFFER with COMMANDS."
(with-current-buffer buffer
(cider-history-mode)
(let ((inhibit-read-only t))
(erase-buffer)
(dolist (command commands)
(insert (format-command-for-cider-history command)))
(goto-char (point-min)))))

(defun cider-history-insert-cmd-at-point ()
"Insert the command at point into the REPL."
(interactive)
(cider-insert-in-repl
(cider-history-command-text)
nil))

(defun cider-history-execute-cmd-at-point ()
"Insert the command at point into the REPL and execute it."
(interactive)
(cider-insert-in-repl
(cider-history-command-text)
t))

(defun cider-history-handle-mouse (event)
"Handle mouse click EVENT."
(interactive "e")
(cider-history-insert-cmd-at-point))

;;;###autoload
(defun cider-history ()
"Insert all history entries into history buffer."
(interactive)
(with-current-buffer (cider-popup-buffer cider-history-buffer t)
(cider-history-list (current-buffer)
(buffer-local-value
'cider-repl-input-history
(cider-current-repl-buffer)))))

(provide 'cider-history)

;;; cider-history.el ends here
3 changes: 2 additions & 1 deletion cider-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ Configure `cider-cljs-lein-repl' to change the ClojureScript REPL to use."]
["Browse namespace" cider-browse-ns]
["Browse all namespaces" cider-browse-ns-all]
["Browse classpath" cider-classpath]
["Browse classpath entry" cider-open-classpath-entry]))
["Browse classpath entry" cider-open-classpath-entry]
["Browse REPL input history" cider-history]))
"Menu for CIDER interactions.")

(defconst cider-mode-map
Expand Down
2 changes: 2 additions & 0 deletions cider-repl.el
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,7 @@ constructs."
(declare-function cider-undef "cider-interaction")
(declare-function cider-browse-ns "cider-browse-ns")
(declare-function cider-classpath "cider-classpath")
(declare-function cider-history "cider-history")
(declare-function cider-run "cider-interaction")
(declare-function cider-refresh "cider-interaction")
(cider-repl-add-shortcut "clear-output" #'cider-repl-clear-output)
Expand All @@ -1156,6 +1157,7 @@ constructs."
(cider-repl-add-shortcut "toggle-pretty" #'cider-repl-toggle-pretty-printing)
(cider-repl-add-shortcut "browse-ns" (lambda () (cider-browse-ns (cider-current-ns))))
(cider-repl-add-shortcut "classpath" #'cider-classpath)
(cider-repl-add-shortcut "history" #'cider-history)
(cider-repl-add-shortcut "trace-ns" #'cider-toggle-trace-ns)
(cider-repl-add-shortcut "undef" #'cider-undef)
(cider-repl-add-shortcut "refresh" #'cider-refresh)
Expand Down
1 change: 1 addition & 0 deletions cider-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ through a stack of help buffers. Variables `help-back-label' and
"Press <\\[cider-drink-a-sip]> to get more CIDER tips."
"Press <\\[cider-browse-ns-all]> to start CIDER's namespace browser."
"Press <\\[cider-classpath]> to start CIDER's classpath browser."
"Press <\\[cider-history]> to start CIDER's REPL input history browser."
"Press <\\[cider-macroexpand-1]> to expand the preceding macro."
"Press <\\[cider-inspect]> to inspect the preceding expression's result."
"Press <C-u \\[cider-inspect]> to inspect the defun at point's result."
Expand Down

0 comments on commit 764b529

Please sign in to comment.