|
20 | 20 | pathname
|
21 | 21 | data
|
22 | 22 | index
|
23 |
| - edit-string) |
| 23 | + edit-string |
| 24 | + limit) |
24 | 25 |
|
25 | 26 | (defun history-data-list (history)
|
26 | 27 | "Return the history data as a list (and not a vector)."
|
|
31 | 32 | (and (not (equal input last-input))
|
32 | 33 | (not (equal input ""))))
|
33 | 34 |
|
34 |
| -(defun make-history (&key pathname) |
| 35 | +(defun make-history (&key pathname limit) |
35 | 36 | (let* ((initial-contents
|
36 | 37 | (when (and pathname (uiop:file-exists-p pathname))
|
37 | 38 | (uiop:read-file-form pathname)))
|
38 | 39 | (num-contents (length initial-contents)))
|
39 | 40 | (%make-history
|
40 | 41 | :pathname pathname
|
41 | 42 | :data (make-array num-contents :fill-pointer num-contents :adjustable t :initial-contents initial-contents)
|
42 |
| - :index num-contents))) |
| 43 | + :index num-contents |
| 44 | + :limit limit))) |
43 | 45 |
|
44 | 46 | (defun save-file (history)
|
45 | 47 | (when (history-pathname history)
|
|
59 | 61 | "Add this input to the history.
|
60 | 62 |
|
61 | 63 | Don't add the same input as the previous one.
|
62 |
| - If allow-duplicates is non t, don't add duplicates at all." |
63 |
| - (cond |
64 |
| - ((not allow-duplicates) |
65 |
| - (when (not (find input (history-data history) :test test)) |
66 |
| - (vector-push-extend input (history-data history)))) |
67 |
| - ((require-additions-to-history-p input |
68 |
| - (last-history history)) |
69 |
| - (vector-push-extend input (history-data history))) |
70 |
| - (t |
71 |
| - nil)) |
72 |
| - |
73 |
| - (setf (history-index history) |
74 |
| - (length (history-data history))) |
| 64 | + If allow-duplicates is non t, don't add duplicates at all. |
| 65 | + If limit is set, overwrite oldest entry when reached." |
| 66 | + (when (or allow-duplicates |
| 67 | + (not (find input (history-data history) :test test))) |
| 68 | + (when (require-additions-to-history-p input (last-history history)) |
| 69 | + (let ((limit (history-limit history))) |
| 70 | + (cond |
| 71 | + ((and limit (>= (length (history-data history)) limit)) |
| 72 | + ;; Shift by 1, overwriting the oldest |
| 73 | + (replace (history-data history) |
| 74 | + (history-data history) |
| 75 | + :start1 0 |
| 76 | + :start2 1 |
| 77 | + :end2 limit) |
| 78 | + (setf (aref (history-data history) (1- limit)) input)) |
| 79 | + (t |
| 80 | + ;; Add new element normally |
| 81 | + (vector-push-extend input (history-data history))))) |
| 82 | + (setf (history-index history) |
| 83 | + (length (history-data history))))) |
75 | 84 | input)
|
76 | 85 |
|
77 | 86 | (defun remove-history (history input)
|
|
0 commit comments