>From 6619bb2ace89143b5194af755928bac2157fcd70 Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Wed, 18 Apr 2018 18:13:26 +0100 Subject: [PATCH 1/2] Improve simple.el history and ring pruning * lisp/simple.el (command-execute): Fix off-by-one error in command-history pruning. (bug#31211) (kill-new, push-mark): Prune kill-ring, mark-ring and global-mark-ring in a single pass, as add-to-history does. --- lisp/simple.el | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lisp/simple.el b/lisp/simple.el index b51be3a8f8..f7f8da87ad 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -1894,8 +1894,8 @@ command-execute (push `(execute-kbd-macro ,final ,prefixarg) command-history) ;; Don't keep command history around forever. (when (and (numberp history-length) (> history-length 0)) - (let ((cell (nthcdr history-length command-history))) - (if (consp cell) (setcdr cell nil))))) + (let ((tail (nthcdr (1- history-length) command-history))) + (when (cdr tail) (setcdr tail ()))))) (execute-kbd-macro final prefixarg)) (t ;; Pass `cmd' rather than `final', for the backtrace's sake. @@ -4396,8 +4396,8 @@ kill-new (if (and replace kill-ring) (setcar kill-ring string) (push string kill-ring) - (if (> (length kill-ring) kill-ring-max) - (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))) + (let ((tail (nthcdr (1- kill-ring-max) kill-ring))) + (when (cdr tail) (setcdr tail ()))))) (setq kill-ring-yank-pointer kill-ring) (if interprogram-cut-function (funcall interprogram-cut-function string))) @@ -5705,9 +5705,10 @@ push-mark In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil." (unless (null (mark t)) (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring)) - (when (> (length mark-ring) mark-ring-max) - (move-marker (car (nthcdr mark-ring-max mark-ring)) nil) - (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))) + (let ((tail (nthcdr (1- mark-ring-max) mark-ring))) + (when (cdr tail) + (set-marker (cadr tail) nil) + (setcdr tail ())))) (set-marker (mark-marker) (or location (point)) (current-buffer)) ;; Now push the mark on the global mark ring. (if (and global-mark-ring @@ -5716,9 +5717,10 @@ push-mark ;; Don't push another one. nil (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring)) - (when (> (length global-mark-ring) global-mark-ring-max) - (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil) - (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil))) + (let ((tail (nthcdr (1- global-mark-ring-max) global-mark-ring))) + (when (cdr tail) + (set-marker (cadr tail) nil) + (setcdr tail ())))) (or nomsg executing-kbd-macro (> (minibuffer-depth) 0) (message "Mark set")) (if (or activate (not transient-mark-mode)) -- 2.17.0