Skip to content

Commit 1fa3f7c

Browse files
Zzullbbatsov
authored andcommitted
New value per-project for cider-repl-history-file
To save the history on a per-project basis.
1 parent 430cfd4 commit 1fa3f7c

File tree

4 files changed

+53
-22
lines changed

4 files changed

+53
-22
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### Changes
6+
7+
- [#3574](https://github.com/clojure-emacs/cider/issues/3574): New value `per-project` for `cider-repl-history-file` to save the history on a per-project basis.
8+
59
### Bugs fixed
610

711
- [#3763](https://github.com/clojure-emacs/cider/issues/3763): Fix `cider-docview-render` completion popup error when symbol being completed does not have a docstring.

cider-repl-history.el

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ call `cider-repl-history' again.")
220220
(defvar cider-repl-history-previous-overlay nil
221221
"Previous overlay within *cider-repl-history* buffer.")
222222

223-
224223
(defun cider-repl-history-get-history ()
225224
"Function to retrieve history from the REPL buffer."
226225
(if cider-repl-history-repl-buffer

cider-repl.el

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,6 @@ CIDER 1.7."
194194
This property value must be unique to avoid having adjacent inputs be
195195
joined together.")
196196

197-
(defvar-local cider-repl-input-history '()
198-
"History list of strings read from the REPL buffer.")
199-
200197
(defvar-local cider-repl-input-history-items-added 0
201198
"Variable counting the items added in the current session.")
202199

@@ -1468,6 +1465,9 @@ WIN, BUFFER and POS are the window, buffer and point under mouse position."
14681465
(defvar cider-repl-history-pattern nil
14691466
"The regexp most recently used for finding input history.")
14701467

1468+
(defvar cider-repl-input-history '()
1469+
"History list of strings read from the REPL buffer.")
1470+
14711471
(defun cider-repl--add-to-input-history (string)
14721472
"Add STRING to the input history.
14731473
Empty strings and duplicates are ignored."
@@ -1593,9 +1593,11 @@ If USE-CURRENT-INPUT is non-nil, use the current input."
15931593
:safe #'integerp)
15941594

15951595
(defcustom cider-repl-history-file nil
1596-
"File to save the persistent REPL history to."
1597-
:type 'string
1598-
:safe #'stringp)
1596+
"File to save the persistent REPL history to.
1597+
If this is set to a path the history will be global to all projects. If this is
1598+
set to `per-project', the history will be stored in a file (.cider-history) at
1599+
the root of each project."
1600+
:type '(choice string symbol))
15991601

16001602
(defun cider-repl--history-read-filename ()
16011603
"Ask the user which file to use, defaulting `cider-repl-history-file'."
@@ -1619,11 +1621,27 @@ defined filenames can be used to read special history files.
16191621
16201622
The value of `cider-repl-input-history' is set by this function."
16211623
(interactive (list (cider-repl--history-read-filename)))
1622-
(let ((f (or filename cider-repl-history-file)))
1623-
;; TODO: probably need to set cider-repl-input-history-position as well.
1624-
;; in a fresh connection the newest item in the list is currently
1625-
;; not available. After sending one input, everything seems to work.
1626-
(setq cider-repl-input-history (cider-repl--history-read f))))
1624+
(cond
1625+
(filename (setq cider-repl-history-file filename))
1626+
((equal 'per-project cider-repl-history-file)
1627+
(make-local-variable 'cider-repl-input-history)
1628+
(when-let ((dir (clojure-project-dir)))
1629+
(setq-local
1630+
cider-repl-history-file (expand-file-name ".cider-history" dir)))))
1631+
(when cider-repl-history-file
1632+
(condition-case nil
1633+
;; TODO: probably need to set cider-repl-input-history-position as
1634+
;; well. In a fresh connection the newest item in the list is
1635+
;; currently not available. After sending one input, everything
1636+
;; seems to work.
1637+
(setq
1638+
cider-repl-input-history
1639+
(cider-repl--history-read cider-repl-history-file))
1640+
(error
1641+
(message
1642+
"Malformed cider-repl-history-file: %s" cider-repl-history-file)))
1643+
(add-hook 'kill-buffer-hook #'cider-repl-history-just-save t t)
1644+
(add-hook 'kill-emacs-hook #'cider-repl-history-save-all)))
16271645

16281646
(defun cider-repl--history-write (filename)
16291647
"Write history to FILENAME.
@@ -1658,6 +1676,13 @@ This function is meant to be used in hooks to avoid lambda
16581676
constructs."
16591677
(cider-repl-history-save cider-repl-history-file))
16601678

1679+
(defun cider-repl-history-save-all ()
1680+
"Save all histories."
1681+
(dolist (buffer (buffer-list))
1682+
(with-current-buffer buffer
1683+
(when (equal major-mode 'cider-repl-mode)
1684+
(cider-repl-history-just-save)))))
1685+
16611686
;; SLIME has different semantics and will not save any duplicates.
16621687
;; we keep track of how many items were added to the history in the
16631688
;; current session in `cider-repl--add-to-input-history' and merge only the
@@ -2051,13 +2076,7 @@ in an unexpected place."
20512076
(setq-local prettify-symbols-alist clojure--prettify-symbols-alist)
20522077
;; apply dir-local variables to REPL buffers
20532078
(hack-dir-local-variables-non-file-buffer)
2054-
(when cider-repl-history-file
2055-
(condition-case nil
2056-
(cider-repl-history-load cider-repl-history-file)
2057-
(error
2058-
(message "Malformed cider-repl-history-file: %s" cider-repl-history-file)))
2059-
(add-hook 'kill-buffer-hook #'cider-repl-history-just-save t t)
2060-
(add-hook 'kill-emacs-hook #'cider-repl-history-just-save))
2079+
(cider-repl-history-load)
20612080
(add-hook 'completion-at-point-functions #'cider-complete-at-point nil t)
20622081
(add-hook 'paredit-mode-hook (lambda () (clojure-paredit-setup cider-repl-mode-map)))
20632082
(cider-repl-setup-paredit))

doc/modules/ROOT/pages/repl/configuration.adoc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,21 @@ reset automatically by the `track-state` middleware.
340340
(setq cider-repl-history-size 1000) ; the default is 500
341341
----
342342

343-
* To store the REPL history in a file:
343+
* To store the REPL history of all projects in a single file:
344344

345345
[source,lisp]
346346
----
347347
(setq cider-repl-history-file "path/to/file")
348348
----
349349

350-
Note that CIDER writes the history to the file when you kill the REPL
351-
buffer, which includes invoking `cider-quit`, or when you quit Emacs.
350+
* To store the REPL history per project (by creating a
351+
`.cider-history` file at the root of each):
352+
353+
[source,lisp]
354+
----
355+
(setq cider-repl-history-file 'per-project)
356+
----
357+
358+
Note that CIDER writes the history to the file(s) when you kill the
359+
REPL buffer, which includes invoking `cider-quit`, or when you quit
360+
Emacs.

0 commit comments

Comments
 (0)