From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Tino Calancha Newsgroups: gmane.emacs.bugs Subject: bug#38796: 26.3; `view-lossage': Use a variable for the lossage limit Date: Fri, 04 Sep 2020 11:31:33 +0200 Message-ID: <87363xj3be.fsf@gmail.com> References: <43aac56d-ecf1-44ed-9be1-ffb8e5f8a7ce@default> <83zhfbm448.fsf@gnu.org> <87zh8pij6e.fsf@calancha-pc.dy.bbexcite.jp> <83v9jc3o3q.fsf@gnu.org> <87wo3qj1s6.fsf@calancha-pc.dy.bbexcite.jp> <87eenygyrx.fsf@gnus.org> <87d03beq3g.fsf@gmail.com> <83zh6fz4q1.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38934"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: larsi@gnus.org, monnier@iro.umontreal.ca, uyennhi.qm@gmail.com, 38796@debbugs.gnu.org, stefankangas@gmail.com To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Sep 04 11:33:06 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kE85h-0009z7-Kg for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 04 Sep 2020 11:33:05 +0200 Original-Received: from localhost ([::1]:39344 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kE85g-0001Yo-Jq for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 04 Sep 2020 05:33:04 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42520) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kE84g-0008Bt-Ab for bug-gnu-emacs@gnu.org; Fri, 04 Sep 2020 05:32:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:54141) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kE84f-0005nf-UC for bug-gnu-emacs@gnu.org; Fri, 04 Sep 2020 05:32:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kE84f-00015E-P6 for bug-gnu-emacs@gnu.org; Fri, 04 Sep 2020 05:32:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Tino Calancha Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 04 Sep 2020 09:32:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 38796 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 38796-submit@debbugs.gnu.org id=B38796.15992119064141 (code B ref 38796); Fri, 04 Sep 2020 09:32:01 +0000 Original-Received: (at 38796) by debbugs.gnu.org; 4 Sep 2020 09:31:46 +0000 Original-Received: from localhost ([127.0.0.1]:37454 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kE84P-00014j-B0 for submit@debbugs.gnu.org; Fri, 04 Sep 2020 05:31:45 -0400 Original-Received: from mail-wm1-f50.google.com ([209.85.128.50]:36772) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kE84M-00014S-On for 38796@debbugs.gnu.org; Fri, 04 Sep 2020 05:31:43 -0400 Original-Received: by mail-wm1-f50.google.com with SMTP id z9so5439025wmk.1 for <38796@debbugs.gnu.org>; Fri, 04 Sep 2020 02:31:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=YEBpsYM5Y66jt+tp7qMunO0Bn6ahLE6qPzmPBgf9iBc=; b=oeVaNVNQuykrcfswPk7uoAmN1h5wG+hNHgyD8/hFyerlkGAUfX7Df+S/qaRi2GLbUF 2SitiIcrVHEUGJ8oWy+5jGArMbzTHxdI3Hv4+LO//huKSeobeCvdjRVQySguT5B//MFJ 03vIyuVesyTskJ/wr9rOsuM1rkWguOerl0LsVfc2XrppQSII6E/9IY81emF7ei5atN6x CCTtKkpviy2dnx5M0XFr1EBY6zVs7+FgktwLaIiTEkJ81W62HafI8mQ4X+S3Dl++o7Pn +NPYky3NE11+H+GGJvNgrmmfT6qCz5xDw3tBoXui6tRfi11qZ2YAr2NuNddvE5YgXFmB fG1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=YEBpsYM5Y66jt+tp7qMunO0Bn6ahLE6qPzmPBgf9iBc=; b=husP88T7rBhcCOd/hz8O8xMmrzlKDPAvXkr0CeuH1s6OtXMoyQLPPVD2sHa/voQvpi RajOvAPJgiIr2//z+m0Q4iRWfYw9YEOAG39oOiifWx9Wm9BDNSeGWFgDZylUpIuM7XzD iIaVogwK6huvWym14e20peo9DoMdm8ONukYThHjUBQVB09A6r7qx+AQKDXV630I0nuz7 Hqg668l2doveZDZuM5yfRVU6mfjtaUu44YjMYJ/1kHK7MdgR5QNuTppsQ6zhNJyleaOZ O6PtCEaT0T2ucWX5ANajFo8EcNd+d+Qy5kh6y2x5sFjJDaLxS8pLNjciBYxAU/gZoGbH w+nw== X-Gm-Message-State: AOAM533PZrhs6qdHqyeWZ5zwL9roIn0gfJSKoOWhIYnu5D1bacSrRdx1 pxmu6ujC+h4fgPGiqYwyBos= X-Google-Smtp-Source: ABdhPJxHWaAB7gImOIxWQMUTAcFKw3UFW8RpRy26lKw27Z01oQ68IGljLFH0cG/er8TD0qud8p5w1g== X-Received: by 2002:a1c:4455:: with SMTP id r82mr6598904wma.60.1599211895647; Fri, 04 Sep 2020 02:31:35 -0700 (PDT) Original-Received: from localhost.example.com (64.red-83-58-61.dynamicip.rima-tde.net. [83.58.61.64]) by smtp.gmail.com with ESMTPSA id o4sm9878486wrv.86.2020.09.04.02.31.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 02:31:34 -0700 (PDT) In-Reply-To: <83zh6fz4q1.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 28 Aug 2020 09:04:06 +0300") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:187097 Archived-At: Eli Zaretskii writes: >> FWIW I have been running Emacs almost 3 months using this branch >> with no issues. >> If Eli is OK with it, I can merge it to master next week. > > Sorry, OK with what? where's the patch which I should agree with? Branch origin/bug#38796-lossage-limit The following is the difference with master branch when I updated the branch (last August 27th): --8<-----------------------------cut here---------------start------------->8--- diff --git a/doc/emacs/help.texi b/doc/emacs/help.texi index 06ad5a583d..34a354891b 100644 --- a/doc/emacs/help.texi +++ b/doc/emacs/help.texi @@ -573,10 +573,13 @@ Misc Help @kindex C-h l @findex view-lossage +@findex lossage-size If something surprising happens, and you are not sure what you typed, use @kbd{C-h l} (@code{view-lossage}). @kbd{C-h l} displays your last -300 input keystrokes and the commands they invoked. If you see -commands that you are not familiar with, you can use @kbd{C-h k} or +input keystrokes and the commands they invoked. By default, Emacs +stores the last 300 events; if you wish, you can change this number with +the command @code{lossage-size}. +If you see commands that you are not familiar with, you can use @kbd{C-h k} or @kbd{C-h f} to find out what they do. @kindex C-h e diff --git a/etc/NEWS b/etc/NEWS index 964b626d7b..8bb3527d85 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -85,6 +85,10 @@ useful on systems such as FreeBSD which ships only with "etc/termcap". * Changes in Emacs 28.1 ++++ +** The new command 'lossage-limit' controls the maximum number +of keystrokes and commands recorded. + +++ ** New variables that hold default buffer names for shell output. The new constants 'shell-command-buffer-name' and @@ -92,6 +96,9 @@ The new constants 'shell-command-buffer-name' and for the output of, respectively, synchronous and async shell commands. +** The new command lossage-size' allow users to set the maximum +number of keystrokes and commands recorded. + ** Support for '(box . SIZE)' 'cursor-type'. By default, 'box' cursor always has a filled box shape. But if you specify 'cursor-type' to be '(box . SIZE)', the cursor becomes a hollow diff --git a/lisp/comint.el b/lisp/comint.el index be0e32b9e0..afc8395b36 100644 --- a/lisp/comint.el +++ b/lisp/comint.el @@ -2393,7 +2393,7 @@ comint-send-invisible Then send it to the process running in the current buffer. The string is sent using `comint-input-sender'. Security bug: your string can still be temporarily recovered with -\\[view-lossage]; `clear-this-command-keys' can fix that." +\\[view-lossage]; `clear-this-command-keys' and `lossage-size' can fix that." (interactive "P") ; Defeat snooping via C-x ESC ESC (let ((proc (get-buffer-process (current-buffer))) (prefix diff --git a/lisp/edmacro.el b/lisp/edmacro.el index 71474c0289..52cbf5b5a6 100644 --- a/lisp/edmacro.el +++ b/lisp/edmacro.el @@ -35,8 +35,8 @@ ;; * `M-x' followed by a command name, to edit a named command ;; whose definition is a keyboard macro. ;; -;; * `C-h l' (view-lossage), to edit the 300 most recent keystrokes -;; and install them as the "current" macro. +;; * `C-h l' (view-lossage), to edit the 300 most recent +;; keystrokes and install them as the "current" macro. ;; ;; * any key sequence whose definition is a keyboard macro. ;; diff --git a/lisp/help.el b/lisp/help.el index 1b0149616f..d3b0e02f08 100644 --- a/lisp/help.el +++ b/lisp/help.el @@ -455,9 +455,10 @@ view-external-packages (info "(efaq)Packages that do not come with Emacs")) (defun view-lossage () - "Display last few input keystrokes and the commands run. + "Display last input keystrokes and the commands run. For convenience this uses the same format as `edit-last-kbd-macro'. +See `lossage-size' to update the number of recorded keystrokes. To record all your input, use `open-dribble-file'." (interactive) diff --git a/lisp/term.el b/lisp/term.el index e77c2c1331..e39d03e8cd 100644 --- a/lisp/term.el +++ b/lisp/term.el @@ -2236,7 +2236,8 @@ term-read-noecho Note that the keystrokes comprising the text can still be recovered \(temporarily) with \\[view-lossage]. This may be a security bug for some -applications." +applications. See `clear-this-command-keys' and `lossage-size' for ways +to fix that." (declare (obsolete read-passwd "27.1")) (let ((ans "") (c 0) diff --git a/src/keyboard.c b/src/keyboard.c index 5fa58abce1..bf8d75b602 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -103,15 +103,16 @@ Copyright (C) 1985-1989, 1993-1997, 1999-2020 Free Software Foundation, /* True in the single-kboard state, false in the any-kboard state. */ static bool single_kboard; -#define NUM_RECENT_KEYS (300) - /* Index for storing next element into recent_keys. */ static int recent_keys_index; /* Total number of elements stored into recent_keys. */ static int total_keys; -/* This vector holds the last NUM_RECENT_KEYS keystrokes. */ +/* Size of the recent_keys vector */ +static int lossage_limit = 300; + +/* This vector holds the last lossage_limit keystrokes. */ static Lisp_Object recent_keys; /* Vector holding the key sequence that invoked the current command. @@ -294,6 +295,7 @@ #define GROW_RAW_KEYBUF \ /* Pointer to next place to store character in kbd_buffer. */ static union buffered_input_event *kbd_store_ptr; + /* The above pair of variables forms a "queue empty" flag. When we enqueue a non-hook event, we increment kbd_store_ptr. When we dequeue a non-hook event, we increment kbd_fetch_ptr. We say that @@ -1421,10 +1423,10 @@ command_loop_1 (void) /* Execute the command. */ { - total_keys += total_keys < NUM_RECENT_KEYS; + total_keys += total_keys < lossage_limit; ASET (recent_keys, recent_keys_index, Fcons (Qnil, cmd)); - if (++recent_keys_index >= NUM_RECENT_KEYS) + if (++recent_keys_index >= lossage_limit) recent_keys_index = 0; } Vthis_command = cmd; @@ -3248,15 +3250,15 @@ record_char (Lisp_Object c) int ix1, ix2, ix3; if ((ix1 = recent_keys_index - 1) < 0) - ix1 = NUM_RECENT_KEYS - 1; + ix1 = lossage_limit - 1; ev1 = AREF (recent_keys, ix1); if ((ix2 = ix1 - 1) < 0) - ix2 = NUM_RECENT_KEYS - 1; + ix2 = lossage_limit - 1; ev2 = AREF (recent_keys, ix2); if ((ix3 = ix2 - 1) < 0) - ix3 = NUM_RECENT_KEYS - 1; + ix3 = lossage_limit - 1; ev3 = AREF (recent_keys, ix3); if (EQ (XCAR (c), Qhelp_echo)) @@ -3307,12 +3309,12 @@ record_char (Lisp_Object c) { if (!recorded) { - total_keys += total_keys < NUM_RECENT_KEYS; + total_keys += total_keys < lossage_limit; ASET (recent_keys, recent_keys_index, /* Copy the event, in case it gets modified by side-effect by some remapping function (bug#30955). */ CONSP (c) ? Fcopy_sequence (c) : c); - if (++recent_keys_index >= NUM_RECENT_KEYS) + if (++recent_keys_index >= lossage_limit) recent_keys_index = 0; } else if (recorded < 0) @@ -3326,10 +3328,10 @@ record_char (Lisp_Object c) while (recorded++ < 0 && total_keys > 0) { - if (total_keys < NUM_RECENT_KEYS) + if (total_keys < lossage_limit) total_keys--; if (--recent_keys_index < 0) - recent_keys_index = NUM_RECENT_KEYS - 1; + recent_keys_index = lossage_limit - 1; ASET (recent_keys, recent_keys_index, Qnil); } } @@ -10410,6 +10412,62 @@ DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 1, 0, ? Qt : Qnil); } +/* Reallocate recent_keys copying the keystrokes in the right order */ +static void +update_recent_keys (int new_size, int kept_keys) +{ + int osize = ASIZE (recent_keys); + eassert (recent_keys_index < osize); + eassert (kept_keys <= min (osize, new_size)); + Lisp_Object v = make_nil_vector (new_size); + int i, idx; + for (i = 0; i < kept_keys; ++i) + { + idx = recent_keys_index - kept_keys + i; + while (idx < 0) + idx += osize; + ASET (v, i, AREF (recent_keys, idx)); + } + recent_keys = v; + total_keys = kept_keys; + recent_keys_index = total_keys % new_size; + lossage_limit = new_size; + +} + +DEFUN ("lossage-size", Flossage_size, Slossage_size, 0, 1, + "(list (read-number \"new-size: \" (lossage-size)))", + doc: /* Return the maximum number of saved keystrokes. +Called with ARG, then set this number to ARG. + +The saved keystrokes are the records shown by `view-lossage'. +If you want to disable the lossage records, then set this maximum to a +small number, e.g. 0. +usage: (lossage-size &optional ARG) */) + (Lisp_Object arg) +{ + if (NILP(arg)) + return make_fixnum(lossage_limit == 1 ? 0 : lossage_limit); + + if (!FIXNATP (arg)) + user_error ("Value must be a non-negative integer"); + int osize = ASIZE (recent_keys); + eassert (lossage_limit == osize); + int new_size = XFIXNAT (arg); + + if (new_size == osize) + return Qnil; + /* Internally, the minimum lossage_limit is 1; users will likely use + 0 to disable the lossage, thus here we change 0 -> 1. */ + if (new_size == 0) + new_size = 1; + + int kept_keys = new_size > osize ? total_keys : min (new_size, total_keys); + update_recent_keys (new_size, kept_keys); + + return Qnil; +} + DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 1, 0, doc: /* Return vector of last few events, not counting those from keyboard macros. If INCLUDE-CMDS is non-nil, include the commands that were run, @@ -10419,21 +10477,21 @@ DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 1, 0, bool cmds = !NILP (include_cmds); if (!total_keys - || (cmds && total_keys < NUM_RECENT_KEYS)) + || (cmds && total_keys < lossage_limit)) return Fvector (total_keys, XVECTOR (recent_keys)->contents); else { Lisp_Object es = Qnil; - int i = (total_keys < NUM_RECENT_KEYS + int i = (total_keys < lossage_limit ? 0 : recent_keys_index); - eassert (recent_keys_index < NUM_RECENT_KEYS); + eassert (recent_keys_index < lossage_limit); do { Lisp_Object e = AREF (recent_keys, i); if (cmds || !CONSP (e) || !NILP (XCAR (e))) es = Fcons (e, es); - if (++i >= NUM_RECENT_KEYS) + if (++i >= lossage_limit) i = 0; } while (i != recent_keys_index); es = Fnreverse (es); @@ -10531,7 +10589,7 @@ DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys, DEFUN ("clear-this-command-keys", Fclear_this_command_keys, Sclear_this_command_keys, 0, 1, 0, doc: /* Clear out the vector that `this-command-keys' returns. -Also clear the record of the last 300 input events, unless optional arg +Also clear the record of the last input events, unless optional arg KEEP-RECORD is non-nil. */) (Lisp_Object keep_record) { @@ -11686,7 +11744,7 @@ syms_of_keyboard (void) staticpro (&modifier_symbols); } - recent_keys = make_nil_vector (NUM_RECENT_KEYS); + recent_keys = make_nil_vector (lossage_limit); staticpro (&recent_keys); this_command_keys = make_nil_vector (40); @@ -11736,6 +11794,7 @@ syms_of_keyboard (void) defsubr (&Srecursive_edit); defsubr (&Sinternal_track_mouse); defsubr (&Sinput_pending_p); + defsubr (&Slossage_size); defsubr (&Srecent_keys); defsubr (&Sthis_command_keys); defsubr (&Sthis_command_keys_vector); diff --git a/test/src/keyboard-tests.el b/test/src/keyboard-tests.el index 1988ba51a7..d4ac4a518d 100644 --- a/test/src/keyboard-tests.el +++ b/test/src/keyboard-tests.el @@ -32,5 +32,23 @@ keyboard-unread-command-events (read-event nil nil 2)) ?\C-b))) +(ert-deftest keyboard-lossage-size () + "Test `lossage-size'." + (dolist (val (list 100 300 400 400 500 1000 700 300)) + (lossage-size val) + (should (= val (lossage-size)))) + (let ((current-size (lossage-size))) + ;; Set lossage size to 1 and 0 are equivalent + (lossage-size 0) + (should (zerop (lossage-size))) + (lossage-size 1) + (should (zerop (lossage-size))) + (lossage-size current-size) + ;; Argument must be a natural number + (should-error (lossage-size -5)) + (should-error (lossage-size "200")) + (should (= (lossage-size) current-size)))) + + (provide 'keyboard-tests) ;;; keyboard-tests.el ends here --8<-----------------------------cut here---------------end--------------->8---