From: Lars Ingebrigtsen Date: Fri, 10 Jun 2022 08:51:34 +0000 (+0200) Subject: Don't put trailing optional nil values into `command-history' X-Git-Tag: emacs-29.0.90~1910^2~105 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=07fb8d284f8d08f79bb65e764b39180e2b974761;p=emacs.git Don't put trailing optional nil values into `command-history' * src/callint.c (fix_command): Don't put trailing optional nil values into `command-history' (bug#45333). --- diff --git a/src/callint.c b/src/callint.c index 92bfaf8d397..8283c61da67 100644 --- a/src/callint.c +++ b/src/callint.c @@ -170,7 +170,7 @@ check_mark (bool for_region) of VALUES to do its job. */ static void -fix_command (Lisp_Object input, Lisp_Object values) +fix_command (Lisp_Object input, Lisp_Object function, Lisp_Object values) { /* FIXME: Instead of this ugly hack, we should provide a way for an interactive spec to return an expression/function that will re-build the @@ -230,6 +230,37 @@ fix_command (Lisp_Object input, Lisp_Object values) } } } + + /* If the list contains a bunch of trailing nil values, and they are + optional, remove them from the list. This makes navigating the + history less confusing, since it doesn't contain a lot of + parameters that aren't used. */ + if (CONSP (values)) + { + Lisp_Object arity = Ffunc_arity (function); + /* We don't want to do this simplification if we have an &rest + function, because (cl-defun foo (a &optional (b 'zot)) ..) + etc. */ + if (FIXNUMP (XCAR (arity)) && FIXNUMP (XCDR (arity))) + { + Lisp_Object final = Qnil; + ptrdiff_t final_i = 0, i = 0; + for (Lisp_Object tail = values; + CONSP (tail); + tail = XCDR (tail), ++i) + { + if (!NILP (XCAR (tail))) + { + final = tail; + final_i = i; + } + } + + /* Chop the trailing optional values. */ + if (final_i > 0 && final_i >= XFIXNUM (XCAR (arity)) - 1) + XSETCDR (final, Qnil); + } + } } /* Helper function to call `read-file-name' from C. */ @@ -340,7 +371,7 @@ invoke it (via an `interactive' spec that contains, for instance, an Make a copy of the list of values, for the command history, and turn them into things we can eval. */ Lisp_Object values = quotify_args (Fcopy_sequence (specs)); - fix_command (input, values); + fix_command (input, function, values); call4 (intern ("add-to-history"), intern ("command-history"), Fcons (function, values), Qnil, Qt); }