1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
|
;;; cider-repl-history.el --- REPL input history browser
;; Copyright (c) 2017 John Valente and browse-kill-ring authors
;; 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.
;; Based heavily on browse-kill-ring
;; https://github.com/browse-kill-ring/browse-kill-ring
;;; Commentary:
;; REPL input history browser for CIDER.
;; Allows you to browse the full input history for your REPL buffer, and
;; insert previous commands at the prompt.
;;; Code:
(require 'cl-lib)
(require 'cider-compat)
(require 'cider-popup)
(require 'clojure-mode)
(require 'derived)
(require 'pulse)
(defconst cider-repl-history-buffer "*cider-repl-history*")
(defgroup cider-repl-history nil
"A package for browsing and inserting the items in the CIDER command history."
:prefix "cider-repl-history-"
:group 'cider)
(defvar cider-repl-history-display-styles
'((separated . cider-repl-history-insert-as-separated)
(one-line . cider-repl-history-insert-as-one-line)))
(defcustom cider-repl-history-display-style 'separated
"How to display the CIDER command history items.
If `one-line', then replace newlines with \"\\n\" for display.
If `separated', then display `cider-repl-history-separator' between
entries."
:type '(choice (const :tag "One line" one-line)
(const :tag "Separated" separated))
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-quit-action 'quit-window
"What action to take when `cider-repl-history-quit' is called.
If `bury-buffer', then simply bury the *cider-repl-history* buffer, but keep
the window.
If `bury-and-delete-window', then bury the buffer, and (if there is
more than one window) delete the window.
If `delete-and-restore', then restore the window configuration to what it was
before `cider-repl-history' was called, and kill the *cider-repl-history*
buffer.
If `quit-window', then restore the window configuration to what
it was before `cider-repl-history' was called, and bury *cider-repl-history*.
This is the default.
If `kill-and-delete-window', then kill the *cider-repl-history* buffer, and
delete the window on close.
Otherwise, it should be a function to call."
;; Note, if you use one of the non-"delete" options, after you "quit",
;; the *cider-repl-history* buffer is still available. If you are using
;; `cider-repl-history-show-preview', and you switch to *cider-repl-history* (i.e.,
;; with C-x b), it will not give the preview unless and until you "update"
;; the *cider-repl-history* buffer.
;;
;; This really should not be an issue, because there's no reason to "switch"
;; back to the buffer. If you want to get it back, you can just do C-c M-p
;; from the REPL buffer.
;; If you get in this situation and find it annoying, you can either disable
;; the preview, or set `cider-repl-history-quit-action' to 'delete-and-restore.
;; Then you will simply not have the *cider-repl-history* buffer after you quit,
;; and it won't be an issue.
:type '(choice (const :tag "Bury buffer"
:value bury-buffer)
(const :tag "Bury buffer and delete window"
:value bury-and-delete-window)
(const :tag "Delete window"
:value delete-and-restore)
(const :tag "Save and restore"
:value quit-window)
(const :tag "Kill buffer and delete window"
:value kill-and-delete-window)
function)
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-resize-window nil
"Whether to resize the `cider-repl-history' window to fit its contents.
Value is either t, meaning yes, or a cons pair of integers,
(MAXIMUM . MINIMUM) for the size of the window. MAXIMUM defaults to
the window size chosen by `pop-to-buffer'; MINIMUM defaults to
`window-min-height'."
:type '(choice (const :tag "No" nil)
(const :tag "Yes" t)
(cons (integer :tag "Maximum") (integer :tag "Minimum")))
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-separator ";;;;;;;;;;"
"The string separating entries in the `separated' style.
See `cider-repl-history-display-style'."
;; The (default) separator is a Clojure comment, to preserve fontification
;; in the buffer.
:type 'string
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-recenter nil
"If non-nil, then always keep the current entry at the top of the window."
:type 'boolean
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-highlight-current-entry nil
"If non-nil, highlight the currently selected command history entry."
:type 'boolean
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-highlight-inserted-item nil
"If non-nil, then temporarily highlight the inserted command history entry.
The value selected controls how the inserted item is highlighted,
possible values are `solid' (highlight the inserted text for a
fixed period of time), or `pulse' (fade out the highlighting gradually).
Setting this variable to the value t will select the default
highlighting style, which currently `pulse'.
The variable `cider-repl-history-inserted-item-face' contains the
face used for highlighting."
:type '(choice (const nil) (const t) (const solid) (const pulse))
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-separator-face 'bold
"The face in which to highlight the `cider-repl-history-separator'."
:type 'face
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-current-entry-face 'highlight
"The face in which to highlight the command history current entry."
:type 'face
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-inserted-item-face 'highlight
"The face in which to highlight the inserted item."
:type 'face
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-maximum-display-length nil
"Whether or not to limit the length of displayed items.
If this variable is an integer, the display of the command history will be
limited to that many characters.
Setting this variable to nil means no limit."
:type '(choice (const :tag "None" nil)
integer)
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-display-duplicates t
"If non-nil, then display duplicate items in the command history."
:type 'boolean
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-display-duplicate-highest t
"When `cider-repl-history-display-duplicates' is nil, then display highest (most recent) duplicate items in the command history."
:type 'boolean
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-text-properties nil
"If non-nil, maintain text properties of the command history items."
:type 'boolean
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-hook nil
"A list of functions to call after `cider-repl-history'."
:type 'hook
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defcustom cider-repl-history-show-preview nil
"If non-nil, show a preview of the inserted text in the REPL buffer.
The REPL buffer would show a preview of what the buffer would look like
if the item under point were inserted."
:type 'boolean
:group 'cider-repl-history
:package-version '(cider . "0.15.0"))
(defvar cider-repl-history-repl-window nil
"The window in which chosen command history data will be inserted.
It is probably not a good idea to set this variable directly; simply
call `cider-repl-history' again.")
(defvar cider-repl-history-repl-buffer nil
"The buffer in which chosen command history data will be inserted.
It is probably not a good idea to set this variable directly; simply
call `cider-repl-history' again.")
(defvar cider-repl-history-preview-overlay nil
"The overlay used to preview what would happen if the user inserted the given text.")
(defvar cider-repl-history-previous-overlay nil
"Previous overlay within *cider-repl-history* buffer.")
(defun cider-repl-history-get-history ()
"Function to retrieve history from the REPL buffer."
(if cider-repl-history-repl-buffer
(buffer-local-value
'cider-repl-input-history
cider-repl-history-repl-buffer)
(error "Variable `cider-repl-history-repl-buffer' not bound to a buffer")))
(defun cider-repl-history-resize-window ()
"If variable `cider-repl-history-resize-window' is non-nil, resize the *cider-repl-history* window."
(when cider-repl-history-resize-window
(apply #'fit-window-to-buffer (selected-window)
(if (consp cider-repl-history-resize-window)
(list (car cider-repl-history-resize-window)
(or (cdr cider-repl-history-resize-window)
window-min-height))
(list nil window-min-height)))))
(defun cider-repl-history-read-regexp (msg use-default-p)
"Get a regular expression from the user, prompting with MSG; previous entry is default if USE-DEFAULT-P."
(let* ((default (car regexp-history))
(prompt (if (and default use-default-p)
(format "%s for regexp (default `%s'): "
msg
default)
(format "%s (regexp): " msg)))
(input
(read-from-minibuffer prompt nil nil nil 'regexp-history
(if use-default-p nil default))))
(if (equal input "")
(if use-default-p default nil)
input)))
(defun cider-repl-history-clear-preview ()
"Clear the preview, if one is present."
(interactive)
(when cider-repl-history-preview-overlay
(cl-assert (overlayp cider-repl-history-preview-overlay))
(delete-overlay cider-repl-history-preview-overlay)))
(defun cider-repl-history-cleanup-on-exit ()
"Function called when the user is finished with `cider-repl-history'.
This function performs any cleanup that is required when the user
has finished interacting with the *cider-repl-history* buffer. For now
the only cleanup performed is to remove the preview overlay, if
it's turned on."
(cider-repl-history-clear-preview))
(defun cider-repl-history-quit ()
"Take the action specified by `cider-repl-history-quit-action'."
(interactive)
(cider-repl-history-cleanup-on-exit)
(pcase cider-repl-history-quit-action
(`delete-and-restore
(quit-restore-window (selected-window) 'kill))
(`quit-window
(quit-window))
(`kill-and-delete-window
(kill-buffer (current-buffer))
(unless (= (count-windows) 1)
(delete-window)))
(`bury-and-delete-window
(bury-buffer)
(unless (= (count-windows) 1)
(delete-window)))
(_
(funcall cider-repl-history-quit-action))))
(defun cider-repl-history-preview-overlay-setup (orig-buf)
"Setup the preview overlay in ORIG-BUF."
(when cider-repl-history-show-preview
(with-current-buffer orig-buf
(let* ((will-replace (region-active-p))
(start (if will-replace
(min (point) (mark))
(point)))
(end (if will-replace
(max (point) (mark))
(point))))
(cider-repl-history-clear-preview)
(setq cider-repl-history-preview-overlay
(make-overlay start end orig-buf))
(overlay-put cider-repl-history-preview-overlay
'invisible t)))))
(defun cider-repl-history-highlight-inserted (start end)
"Insert the text between START and END."
(pcase cider-repl-history-highlight-inserted-item
((or `pulse `t)
(let ((pulse-delay .05) (pulse-iterations 10))
(with-no-warnings
(pulse-momentary-highlight-region
start end cider-repl-history-inserted-item-face))))
(`solid
(let ((o (make-overlay start end)))
(overlay-put o 'face cider-repl-history-inserted-item-face)
(sit-for 0.5)
(delete-overlay o)))))
(defun cider-repl-history-insert-and-highlight (str)
"Helper function to insert STR at point, highlighting it if appropriate."
(let ((before-insert (point)))
(let (deactivate-mark)
(insert-for-yank str))
(cider-repl-history-highlight-inserted
before-insert
(point))))
(defun cider-repl-history-target-overlay-at (position &optional no-error)
"Return overlay at POSITION that has property `cider-repl-history-target'.
If no such overlay, raise an error unless NO-ERROR is true, in which
case retun nil."
(let ((ovs (overlays-at (point))))
(catch 'cider-repl-history-target-overlay-at
(dolist (ov ovs)
(when (overlay-get ov 'cider-repl-history-target)
(throw 'cider-repl-history-target-overlay-at ov)))
(unless no-error
(error "No CIDER history item here")))))
(defun cider-repl-history-current-string (pt &optional no-error)
"Find the string to insert into the REPL by looking for the overlay at PT; might error unless NO-ERROR set."
(let ((o (cider-repl-history-target-overlay-at pt t)))
(if o
(overlay-get o 'cider-repl-history-target)
(unless no-error
(error "No CIDER history item in this buffer")))))
(defun cider-repl-history-do-insert (buf pt)
"Helper function to insert text from BUF at PT into the REPL buffer and kill *cider-repl-history*."
;; Note: as mentioned at the top, this file is based on browse-kill-ring,
;; which has numerous insertion options. The functionality of
;; browse-kill-ring allows users to insert at point, and move point to the end
;; of the inserted text; or insert at the beginning or end of the buffer,
;; while leaving point alone. And each of these had the option of leaving the
;; history buffer in place, or getting rid of it. That was appropriate for a
;; generic paste tool, but for inserting a previous command into an
;; interpreter, I felt the only useful option would be inserting it at the end
;; and quitting the history buffer, so that is all that's provided.
(let ((str (cider-repl-history-current-string pt)))
(cider-repl-history-quit)
(with-selected-window cider-repl-history-repl-window
(with-current-buffer cider-repl-history-repl-buffer
(let ((max (point-max)))
(if (= max (point))
(cider-repl-history-insert-and-highlight str)
(save-excursion
(goto-char max)
(cider-repl-history-insert-and-highlight str))))))))
(defun cider-repl-history-insert-and-quit ()
"Insert the item into the REPL buffer, and close *cider-repl-history*.
The text is always inserted at the very bottom of the REPL buffer. If your
cursor is already at the bottom, it is advanced to the end of the inserted
text. If your cursor is somewhere else, the cursor is not moved, but the
text is still inserted at the end."
(interactive)
(cider-repl-history-do-insert (current-buffer) (point)))
(defun cider-repl-history-mouse-insert (e)
"Insert the item at E into the REPL buffer, and close *cider-repl-history*.
The text is always inserted at the very bottom of the REPL buffer. If your
cursor is already at the bottom, it is advanced to the end of the inserted
text. If your cursor is somewhere else, the cursor is not moved, but the
text is still inserted at the end."
(interactive "e")
(let* ((data (save-excursion
(mouse-set-point e)
(cons (current-buffer) (point))))
(buf (car data))
(pt (cdr data)))
(cider-repl-history-do-insert buf pt)))
(defun cider-repl-history-clear-highlighed-entry ()
"Clear the highlighted entry, when one exists."
(when cider-repl-history-previous-overlay
(cl-assert (overlayp cider-repl-history-previous-overlay)
nil "not an overlay")
(overlay-put cider-repl-history-previous-overlay 'face nil)))
(defun cider-repl-history-update-highlighed-entry ()
"Update highlighted entry, when feature is turned on."
(when cider-repl-history-highlight-current-entry
(if-let* ((current-overlay (cider-repl-history-target-overlay-at (point) t)))
(unless (equal cider-repl-history-previous-overlay current-overlay)
;; We've changed overlay. Clear current highlighting,
;; and highlight the new overlay.
(cl-assert (overlay-get current-overlay 'cider-repl-history-target) t)
(cider-repl-history-clear-highlighed-entry)
(setq cider-repl-history-previous-overlay current-overlay)
(overlay-put current-overlay 'face
cider-repl-history-current-entry-face))
;; No overlay at point. Just clear all current highlighting.
(cider-repl-history-clear-highlighed-entry))))
(defun cider-repl-history-forward (&optional arg)
"Move forward by ARG command history entries."
(interactive "p")
(beginning-of-line)
(while (not (zerop arg))
(let ((o (cider-repl-history-target-overlay-at (point) t)))
(cond
((>= arg 0)
(setq arg (1- arg))
;; We're on a cider-repl-history overlay, skip to the end of it.
(when o
(goto-char (overlay-end o))
(setq o nil))
(while (not (or o (eobp)))
(goto-char (next-overlay-change (point)))
(setq o (cider-repl-history-target-overlay-at (point) t))))
(t
(setq arg (1+ arg))
(when o
(goto-char (overlay-start o))
(setq o nil))
(while (not (or o (bobp)))
(goto-char (previous-overlay-change (point)))
(setq o (cider-repl-history-target-overlay-at (point) t)))))))
(when cider-repl-history-recenter
(recenter 1)))
(defun cider-repl-history-previous (&optional arg)
"Move backward by ARG command history entries."
(interactive "p")
(cider-repl-history-forward (- arg)))
(defun cider-repl-history-search-forward (regexp &optional backwards)
"Move to the next command history entry matching REGEXP from point.
If optional arg BACKWARDS is non-nil, move to the previous matching
entry."
(interactive
(list (cider-repl-history-read-regexp "Search forward" t)
current-prefix-arg))
(let ((orig (point)))
(cider-repl-history-forward (if backwards -1 1))
(let ((over (cider-repl-history-target-overlay-at (point) t)))
(while (and over
(not (if backwards (bobp) (eobp)))
(not (string-match regexp
(overlay-get over
'cider-repl-history-target))))
(cider-repl-history-forward (if backwards -1 1))
(setq over (cider-repl-history-target-overlay-at (point) t)))
(unless (and over
(string-match regexp
(overlay-get over
'cider-repl-history-target)))
(goto-char orig)
(message "No more command history entries matching %s" regexp)))))
(defun cider-repl-history-search-backward (regexp)
"Move to the previous command history entry matching REGEXP from point."
(interactive
(list (cider-repl-history-read-regexp "Search backward" t)))
(cider-repl-history-search-forward regexp t))
(defun cider-repl-history-elide (str)
"If STR is too long, abbreviate it with an ellipsis; otherwise, return it unchanged."
(if (and cider-repl-history-maximum-display-length
(> (length str)
cider-repl-history-maximum-display-length))
(concat (substring str 0 (- cider-repl-history-maximum-display-length 3))
(propertize "..." 'cider-repl-history-extra t))
str))
(defmacro cider-repl-history-add-overlays-for (item &rest body)
"Add overlays for ITEM, and execute BODY."
(let ((beg (cl-gensym "cider-repl-history-add-overlays-"))
(end (cl-gensym "cider-repl-history-add-overlays-")))
`(let ((,beg (point))
(,end
(progn
,@body
(point))))
(let ((o (make-overlay ,beg ,end)))
(overlay-put o 'cider-repl-history-target ,item)
(overlay-put o 'mouse-face 'highlight)))))
(defun cider-repl-history-insert-as-separated (items)
"Insert ITEMS into the current buffer, with separators between items."
(while items
(let* ((origitem (car items))
(item (cider-repl-history-elide origitem))
(len (length item)))
(cider-repl-history-add-overlays-for origitem (insert item))
;; When the command history has items with read-only text property at
;; **the end of** string, cider-repl-history-setup fails with error
;; `Text is read-only'. So inhibit-read-only here.
;; See http://bugs.debian.org/225082
(let ((inhibit-read-only t))
(insert "\n")
(when (cdr items)
(insert (propertize cider-repl-history-separator
'cider-repl-history-extra t
'cider-repl-history-separator t))
(insert "\n"))))
(setq items (cdr items))))
(defun cider-repl-history-insert-as-one-line (items)
"Insert ITEMS into the current buffer, formatting each item as a single line.
An explicit newline character will replace newlines so that the text retains its
spacing when it's actually inserted into the REPL buffer."
(dolist (item items)
(cider-repl-history-add-overlays-for
item
(let* ((item (cider-repl-history-elide item))
(len (length item))
(start 0)
(newl (propertize "\\n" 'cider-repl-history-extra t)))
(while (and (< start len)
(string-match "\n" item start))
(insert (substring item start (match-beginning 0))
newl)
(setq start (match-end 0)))
(insert (substring item start len))))
(insert "\n")))
(defun cider-repl-history-preview-update-text (preview-text)
"Update `cider-repl-history-preview-overlay' to show `PREVIEW-TEXT`."
;; If preview-text is nil, replacement should be nil too.
(cl-assert (overlayp cider-repl-history-preview-overlay))
(let ((replacement (when preview-text
(propertize preview-text 'face 'highlight))))
(overlay-put cider-repl-history-preview-overlay
'before-string replacement)))
(defun cider-repl-history-preview-update-by-position (&optional pt)
"Update `cider-repl-history-preview-overlay' to match item at PT.
This function is called whenever the selection in the *cider-repl-history*
buffer is adjusted, the `cider-repl-history-preview-overlay'
is udpated to preview the text of the selection at PT (or the
current point if not specified)."
(let ((new-text (cider-repl-history-current-string
(or pt (point)) t)))
(cider-repl-history-preview-update-text new-text)))
(defun cider-repl-history-undo-other-window ()
"Undo the most recent change in the other window's buffer.
You most likely want to use this command for undoing an insertion of
text from the *cider-repl-history* buffer."
(interactive)
(with-current-buffer cider-repl-history-repl-buffer
(undo)))
(defun cider-repl-history-setup (repl-win repl-buf history-buf &optional regexp)
"Setup: REPL-WIN and REPL-BUF are where to insert commands, HISTORY-BUF is the history, and optional arg REGEXP is a filter."
(cider-repl-history-preview-overlay-setup repl-buf)
(with-current-buffer history-buf
(unwind-protect
(progn
(cider-repl-history-mode)
(setq buffer-read-only nil)
(when (eq 'one-line cider-repl-history-display-style)
(setq truncate-lines t))
(let ((inhibit-read-only t))
(erase-buffer))
(setq cider-repl-history-repl-buffer repl-buf)
(setq cider-repl-history-repl-window repl-win)
(let* ((cider-repl-history-maximum-display-length
(if (and cider-repl-history-maximum-display-length
(<= cider-repl-history-maximum-display-length 3))
4
cider-repl-history-maximum-display-length))
(cider-command-history (cider-repl-history-get-history))
(items (mapcar
(if cider-repl-history-text-properties
#'copy-sequence
#'substring-no-properties)
cider-command-history)))
(unless cider-repl-history-display-duplicates
;; display highest or lowest duplicate.
;; if `cider-repl-history-display-duplicate-highest' is t,
;; display highest (most recent) duplicate.
(cl-delete-duplicates
items
:test #'equal
:from-end cider-repl-history-display-duplicate-highest))
(when (stringp regexp)
(setq items (delq nil
(mapcar
#'(lambda (item)
(when (string-match regexp item)
item))
items))))
(funcall (or (cdr (assq cider-repl-history-display-style
cider-repl-history-display-styles))
(error "Invalid `cider-repl-history-display-style': %s"
cider-repl-history-display-style))
items)
(when cider-repl-history-show-preview
(cider-repl-history-preview-update-by-position (point-min))
;; Local post-command-hook, only happens in *cider-repl-history*
(add-hook 'post-command-hook
'cider-repl-history-preview-update-by-position
nil t)
(add-hook 'kill-buffer-hook
'cider-repl-history-cleanup-on-exit
nil t))
(when cider-repl-history-highlight-current-entry
(add-hook 'post-command-hook
'cider-repl-history-update-highlighed-entry
nil t))
(message
(let ((entry (if (= 1 (length cider-command-history))
"entry"
"entries")))
(concat
(if (and (not regexp)
cider-repl-history-display-duplicates)
(format "%s %s in the command history."
(length cider-command-history) entry)
(format "%s (of %s) %s in the command history shown."
(length items) (length cider-command-history) entry))
(substitute-command-keys
(concat " Type \\[cider-repl-history-quit] to quit. "
"\\[describe-mode] for help.")))))
(set-buffer-modified-p nil)
(goto-char (point-min))
(cider-repl-history-forward 0)
(setq mode-name (if regexp
(concat "History [" regexp "]")
"History"))
(run-hooks 'cider-repl-history-hook)))
(setq buffer-read-only t))))
(defun cider-repl-history-update ()
"Update the history buffer to reflect the latest state of the command history."
(interactive)
(cl-assert (eq major-mode 'cider-repl-history-mode))
(cider-repl-history-setup cider-repl-history-repl-window
cider-repl-history-repl-buffer
(current-buffer))
(cider-repl-history-resize-window))
(defun cider-repl-history-occur (regexp)
"Display all command history entries matching REGEXP."
(interactive
(list (cider-repl-history-read-regexp
"Display command history entries matching" nil)))
(cl-assert (eq major-mode 'cider-repl-history-mode))
(cider-repl-history-setup cider-repl-history-repl-window
cider-repl-history-repl-buffer
(current-buffer)
regexp)
(cider-repl-history-resize-window))
(put 'cider-repl-history-mode 'mode-class 'special)
(define-derived-mode cider-repl-history-mode clojure-mode "History"
"Major mode for browsing the entries in the command input history.
\\{cider-repl-history-mode-map}"
(setq-local sesman-system 'CIDER)
(define-key cider-repl-history-mode-map (kbd "n") 'cider-repl-history-forward)
(define-key cider-repl-history-mode-map (kbd "p") 'cider-repl-history-previous)
(define-key cider-repl-history-mode-map (kbd "SPC") 'cider-repl-history-insert-and-quit)
(define-key cider-repl-history-mode-map (kbd "RET") 'cider-repl-history-insert-and-quit)
(define-key cider-repl-history-mode-map [(mouse-2)] 'cider-repl-history-mouse-insert)
(define-key cider-repl-history-mode-map (kbd "l") 'cider-repl-history-occur)
(define-key cider-repl-history-mode-map (kbd "s") 'cider-repl-history-search-forward)
(define-key cider-repl-history-mode-map (kbd "r") 'cider-repl-history-search-backward)
(define-key cider-repl-history-mode-map (kbd "g") 'cider-repl-history-update)
(define-key cider-repl-history-mode-map (kbd "q") 'cider-repl-history-quit)
(define-key cider-repl-history-mode-map (kbd "U") 'cider-repl-history-undo-other-window)
(define-key cider-repl-history-mode-map (kbd "?") 'describe-mode)
(define-key cider-repl-history-mode-map (kbd "h") 'describe-mode))
;;;###autoload
(defun cider-repl-history ()
"Display items in the CIDER command history in another buffer."
(interactive)
(when (eq major-mode 'cider-repl-history-mode)
(user-error "Already viewing the CIDER command history"))
(let* ((repl-win (selected-window))
(repl-buf (window-buffer repl-win))
(buf (get-buffer-create cider-repl-history-buffer)))
(cider-repl-history-setup repl-win repl-buf buf)
(pop-to-buffer buf)
(cider-repl-history-resize-window)))
(provide 'cider-repl-history)
;;; cider-repl-history.el ends here
|