From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Please add a lossage-limit option for `view-lossage' Date: Mon, 06 Apr 2020 11:54:55 -0400 Message-ID: References: <7d79be75-058d-4678-a9b7-f9db23abe460@default> <87pnck4xci.fsf@oitofelix.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="21975"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: Drew Adams , Emacs developers To: Bruno =?windows-1252?Q?F=E9lix?= Rezende Ribeiro Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Apr 06 17:55:39 2020 Return-path: Envelope-to: ged-emacs-devel@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 1jLU65-0005au-Kx for ged-emacs-devel@m.gmane-mx.org; Mon, 06 Apr 2020 17:55:37 +0200 Original-Received: from localhost ([::1]:34286 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jLU64-0002sC-N2 for ged-emacs-devel@m.gmane-mx.org; Mon, 06 Apr 2020 11:55:36 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46589) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jLU5W-0002QQ-7v for emacs-devel@gnu.org; Mon, 06 Apr 2020 11:55:03 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jLU5U-000608-1X for emacs-devel@gnu.org; Mon, 06 Apr 2020 11:55:01 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:2088) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jLU5T-0005zg-QT; Mon, 06 Apr 2020 11:54:59 -0400 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id D6E3944FD0B; Mon, 6 Apr 2020 11:54:58 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 7BA9644FCFE; Mon, 6 Apr 2020 11:54:56 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1586188496; bh=Tjobu8jo3+5yDF2o9k01yaMSqHE0Vncqdx8fduTwTTU=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=Qc+G8jWyPbf0nEuIiwL2pbGGYVHNT3KxoJYHMybDCCv3zMlhF14fqz7XatorhMjMY mTe4Y5rHMrgCqu0WhRH8SpsOifmW6xtJdxEd5iZOAS3kG2p6SJIzKKKx66R+thODMr 7YBup/raKmilkASLnbUlEwxeofmlhMImC2mS8ZQgLUhtqM929BNeT2dYPUxK+Ikq3s kPEnIuKpKtZczPRQzW8DtD6hq1A5grdJKKssUqNJ0Clkny4k+jjSO9jteW4AvHPDts hdn1tVZY8E4EHKluPuvFf4h+6B8w/fmKfQOBMNpkzFnvg9UJe5WvWNjiEyjEkIojEd 5bZZN7AjV7Tcw== Original-Received: from alfajor (unknown [104.247.241.114]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 0312612076C; Mon, 6 Apr 2020 11:54:55 -0400 (EDT) In-Reply-To: <87pnck4xci.fsf@oitofelix.com> ("Bruno =?windows-1252?Q?F=E9l?= =?windows-1252?Q?ix?= Rezende Ribeiro"'s message of "Mon, 06 Apr 2020 09:23:09 -0300") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 132.204.25.50 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:246537 Archived-At: Hi, > I took this task as a goal in my first attempt to write code to Emacs C c= ore. Very good choice! > In the Lisp reference documentation, I was unable to find a way to do > that. I thought that if that mechanism existed it=E2=80=99d be important= enough > to be documented. I couldn=E2=80=99t find it in a quick look around the = code > neither. How is the interception of change of variables from the Lisp > world canonically handled by Emacs C source code? Indeed, there's no such canonical thing. One way this kind of problem is solved sometimes is by not exporting the variable to Lisp, and instead only providing access to the variable via functions to read and/or set it. But here's how I'd recommend you solve this specific case: 1- consolidate the (apprently 3) places in the current code that add an element to the vector into its own function. That's a good change in itself, will make the code shorter (by reducing duplication) and more readable. 2- Replace the NUM_RECENT_KEYS constant with a num_recent_keys variable (tho maybe we can just replace the var with `ASIZE (recent_keys)` instead!). 3- Add a new Lisp variable which we could call `Vnum_recent_keys`, tho maybe I'd rather make it use the `recent-key` prefix, so it could be `Vrecent_key_size`? [ Notice that, by convention, Lisp variables in the C world are prefixed with a `V`. ] 4- Synchronize this new var with `num_recent_keys` at just one place: inside the function introduced at step 1. This means that calling `recent-keys` just after setting the var will not reflect the new value of the variable, but nobody cares about that, I think. Stefan > Footnotes:=20 > > [1] This is the requested user option. > > [2] Actually creating another vector of the right size and copying the > data over as needed. > > > From f87911cf8f6d12046f25e2964bbbdbcd3ca0581d Mon Sep 17 00:00:00 2001 > From: =3D?UTF-8?q?Bruno=3D20F=3DC3=3DA9lix=3D20Rezende=3D20Ribeiro?=3D > Date: Mon, 6 Apr 2020 08:30:16 -0300 > Subject: [PATCH] =3D?UTF-8?q?Transform=3D20C=3D20constant=3D20=3DE2=3D80= =3D98NUM=3D5FRECEN?=3D > =3D?UTF-8?q?T=3D5FKEYS=3DE2=3D80=3D99=3D20into=3D20=3DE2=3D80=3D98num-re= cent-keys=3DE2=3D80=3D99=3D20?=3D > =3D?UTF-8?q?Lisp=3D20variable?=3D > MIME-Version: 1.0 > Content-Type: text/plain; charset=3DUTF-8 > Content-Transfer-Encoding: 8bit > > * src/keyboard.c: remove NUM_RECENT_KEYS constant. > (ensure_recent_keys_size): new function. > (command_loop_1, record_char, recent-keys): add calls to > =E2=80=98ensure_recent_key_size=E2=80=99. > (syms_of_keyboard): add =E2=80=98num-recent-keys=E2=80=99 Lisp integer = variable. > * lisp/cus-start.el: add =E2=80=98num-recent-keys=E2=80=99. > --- > lisp/cus-start.el | 1 + > src/keyboard.c | 62 ++++++++++++++++++++++++++++++++++++++-----------= ------ > 2 files changed, 44 insertions(+), 19 deletions(-) > > diff --git a/lisp/cus-start.el b/lisp/cus-start.el > index dff4d9a..001f79b 100644 > --- a/lisp/cus-start.el > +++ b/lisp/cus-start.el > @@ -353,6 +353,7 @@ minibuffer-prompt-properties--setter > (indent-tabs-mode indent boolean) > ;; keyboard.c > (meta-prefix-char keyboard character) > + (num-recent-keys keyboard integer) > (auto-save-interval auto-save integer) > (auto-save-no-message auto-save boolean "27.1") > (auto-save-timeout auto-save (choice (const :tag "off" nil) > diff --git a/src/keyboard.c b/src/keyboard.c > index 9ce168c..cbbb572 100644 > --- a/src/keyboard.c > +++ b/src/keyboard.c > @@ -103,15 +103,13 @@ 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; >=20=20 > -#define NUM_RECENT_KEYS (300) > - > /* Index for storing next element into recent_keys. */ > static int recent_keys_index; >=20=20 > /* Total number of elements stored into recent_keys. */ > static int total_keys; >=20=20 > -/* This vector holds the last NUM_RECENT_KEYS keystrokes. */ > +/* This vector holds the last num_recent_keys keystrokes. */ > static Lisp_Object recent_keys; >=20=20 > /* Vector holding the key sequence that invoked the current command. > @@ -371,6 +369,7 @@ #define READABLE_EVENTS_IGNORE_SQUEEZABLES (1 << 2) > static void deliver_user_signal (int); > static char *find_user_signal_name (int); > static void store_user_signal_events (void); > +static void ensure_recent_keys_size (void); >=20=20 > /* Advance or retreat a buffered input event pointer. */ >=20=20 > @@ -448,6 +447,20 @@ kset_system_key_syms (struct kboard *kb, Lisp_Object= val) > } >=20=20 > > +void > +ensure_recent_keys_size (void) > +{ > + if (ASIZE (recent_keys) !=3D num_recent_keys) > + { > + Lisp_Object new_recent_keys =3D make_nil_vector (num_recent_keys); > + memcpy (XVECTOR (new_recent_keys)->contents, XVECTOR (recent_keys)= ->contents, > + min (ASIZE (recent_keys), num_recent_keys) > + * sizeof *XVECTOR (recent_keys)->contents); > + recent_keys =3D new_recent_keys; > + } > +} > + > + > static bool > echo_keystrokes_p (void) > { > @@ -1421,10 +1434,11 @@ command_loop_1 (void) > /* Execute the command. */ >=20=20 > { > - total_keys +=3D total_keys < NUM_RECENT_KEYS; > + ensure_recent_keys_size (); > + total_keys +=3D total_keys < num_recent_keys; > ASET (recent_keys, recent_keys_index, > Fcons (Qnil, cmd)); > - if (++recent_keys_index >=3D NUM_RECENT_KEYS) > + if (++recent_keys_index >=3D num_recent_keys) > recent_keys_index =3D 0; > } > Vthis_command =3D cmd; > @@ -3249,16 +3263,18 @@ record_char (Lisp_Object c) > Lisp_Object ev1, ev2, ev3; > int ix1, ix2, ix3; >=20=20 > + ensure_recent_keys_size (); > + > if ((ix1 =3D recent_keys_index - 1) < 0) > - ix1 =3D NUM_RECENT_KEYS - 1; > + ix1 =3D num_recent_keys - 1; > ev1 =3D AREF (recent_keys, ix1); >=20=20 > if ((ix2 =3D ix1 - 1) < 0) > - ix2 =3D NUM_RECENT_KEYS - 1; > + ix2 =3D num_recent_keys - 1; > ev2 =3D AREF (recent_keys, ix2); >=20=20 > if ((ix3 =3D ix2 - 1) < 0) > - ix3 =3D NUM_RECENT_KEYS - 1; > + ix3 =3D num_recent_keys - 1; > ev3 =3D AREF (recent_keys, ix3); >=20=20 > if (EQ (XCAR (c), Qhelp_echo)) > @@ -3309,12 +3325,13 @@ record_char (Lisp_Object c) > { > if (!recorded) > { > - total_keys +=3D total_keys < NUM_RECENT_KEYS; > + ensure_recent_keys_size (); > + total_keys +=3D total_keys < num_recent_keys; > ASET (recent_keys, recent_keys_index, > /* Copy the event, in case it gets modified by side-effe= ct > by some remapping function (bug#30955). */ > CONSP (c) ? Fcopy_sequence (c) : c); > - if (++recent_keys_index >=3D NUM_RECENT_KEYS) > + if (++recent_keys_index >=3D num_recent_keys) > recent_keys_index =3D 0; > } > else if (recorded < 0) > @@ -3328,10 +3345,11 @@ record_char (Lisp_Object c) >=20=20 > while (recorded++ < 0 && total_keys > 0) > { > - if (total_keys < NUM_RECENT_KEYS) > + ensure_recent_keys_size (); > + if (total_keys < num_recent_keys) > total_keys--; > if (--recent_keys_index < 0) > - recent_keys_index =3D NUM_RECENT_KEYS - 1; > + recent_keys_index =3D num_recent_keys - 1; > ASET (recent_keys, recent_keys_index, Qnil); > } > } > @@ -10420,22 +10438,24 @@ DEFUN ("recent-keys", Frecent_keys, Srecent_key= s, 0, 1, 0, > { > bool cmds =3D !NILP (include_cmds); >=20=20 > + ensure_recent_keys_size (); > + > if (!total_keys > - || (cmds && total_keys < NUM_RECENT_KEYS)) > + || (cmds && total_keys < num_recent_keys)) > return Fvector (total_keys, > XVECTOR (recent_keys)->contents); > else > { > Lisp_Object es =3D Qnil; > - int i =3D (total_keys < NUM_RECENT_KEYS > + int i =3D (total_keys < num_recent_keys > ? 0 : recent_keys_index); > - eassert (recent_keys_index < NUM_RECENT_KEYS); > + eassert (recent_keys_index < num_recent_keys); > do > { > Lisp_Object e =3D AREF (recent_keys, i); > if (cmds || !CONSP (e) || !NILP (XCAR (e))) > es =3D Fcons (e, es); > - if (++i >=3D NUM_RECENT_KEYS) > + if (++i >=3D num_recent_keys) > i =3D 0; > } while (i !=3D recent_keys_index); > es =3D Fnreverse (es); > @@ -11690,9 +11710,6 @@ syms_of_keyboard (void) > staticpro (&modifier_symbols); > } >=20=20 > - recent_keys =3D make_nil_vector (NUM_RECENT_KEYS); > - staticpro (&recent_keys); > - > this_command_keys =3D make_nil_vector (40); > staticpro (&this_command_keys); >=20=20 > @@ -11858,6 +11875,13 @@ syms_of_keyboard (void) > result of looking up the original command in the active keymaps. */); > Vthis_original_command =3D Qnil; >=20=20 > + DEFVAR_INT ("num-recent-keys", num_recent_keys, > + doc: /* Number of input events to consider. */); > + num_recent_keys =3D 300; > + > + recent_keys =3D make_nil_vector (num_recent_keys); > + staticpro (&recent_keys); > + > DEFVAR_INT ("auto-save-interval", auto_save_interval, > doc: /* Number of input events between auto-saves. > Zero means disable autosaving due to number of characters typed. */); > --=20 > 2.7.4