From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jim Porter Newsgroups: gmane.emacs.devel Subject: [Updated Patch] Re: [WIP PATCH] Adding keys to keymaps in alphabetical order (for use with `mode-line-mode-menu') Date: Wed, 23 Jun 2021 09:50:17 -0700 Message-ID: <13f2a2b1-626d-4dcb-2737-00c5d6bc4a2e@gmail.com> References: <87tulu11f1.fsf@gnus.org> <87bl7ztn4p.fsf@gnus.org> <44969496-4f6c-e299-9fee-7bad01c17815@gmail.com> <87a6ngn31s.fsf@gnus.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------3BF611C07046C344372508B8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19681"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Stefan Monnier , Lars Ingebrigtsen Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Jun 23 18:50:58 2021 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 1lw65Z-0004y8-MB for ged-emacs-devel@m.gmane-mx.org; Wed, 23 Jun 2021 18:50:57 +0200 Original-Received: from localhost ([::1]:54628 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lw65Y-0001FX-Gf for ged-emacs-devel@m.gmane-mx.org; Wed, 23 Jun 2021 12:50:56 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:52920) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lw64z-0000XK-4s for emacs-devel@gnu.org; Wed, 23 Jun 2021 12:50:21 -0400 Original-Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]:44742) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lw64w-0007Hk-El for emacs-devel@gnu.org; Wed, 23 Jun 2021 12:50:20 -0400 Original-Received: by mail-pf1-x42c.google.com with SMTP id g21so1071976pfc.11 for ; Wed, 23 Jun 2021 09:50:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-language; bh=1XjbkQna/uFvuKbQ3K7J8bDTOKhlg50IvAgRAPkFeYA=; b=fTKgVWLtvkwR27wA0zVGP7EtA9lj8TCJQD8w7VM43g+gHNCDTz0xGPpVir7/mtjzoR GBoLJ+GPNyzoQiumG2TtMatIZroYC6kfXra4KAtpKZUFdh72vsHvGrT2MsxRnyzWLP7a 9TUsXeSY+avVdXhWkYyD/aLWhWdDOb12k0BeAkMKHHy/9GdgmcaHqihIVoAGzdV7u8aE qeLNQ1uLozU7FB/ri/6vg8W9AX3Nfj2lW12nLPu/tcrss6GBSjrRP0WbLh0dpHlCWwpH U08n8thhvs7XYOubl2bjfmAiO7Ts9VNiuUoQtktWoEluu+ha262gcousLVivW3cI5FEE YtZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :mime-version:in-reply-to:content-language; bh=1XjbkQna/uFvuKbQ3K7J8bDTOKhlg50IvAgRAPkFeYA=; b=SmJebxzGMo5iqWTzjRhC+98EPQne1JE23B23avnpaeceyQ9u7VpkhVkQKkJzzvJ2H/ 44D8MT36UpEYERceJVpfDpfiZp2Q6K3X/8MwiWvg65r2iIHqlEEvCDXEtTlaH3bZZcyl fuj3y2nWGU2NyAL2abHPBe+Ga+DHRKBUfsr6f5oS39g7gkvKJMNH2YMAzsFuAwKpd04w 7NsmxTTuB20YEcSk3AH6Fg7PCDU+5lXsfUPN6UkuE1U1tXAycm/1N9mc6WQuwv63Df9R 0Bj/8BfTL2pDBcEAcL86yMCHft1tz1CaFp27LvJXbaHr+Qemva+a1wddNELQGVURFeWF 4W3g== X-Gm-Message-State: AOAM532FqJM/PfAf0bsuGXKldxI8IrJKR3pvBqiSIhz5v1HkS2Vwy5HX akm3aNT9eNdBldWg6BQNhq9BxcY/vDw= X-Google-Smtp-Source: ABdhPJzLSg+VW7UyaglaxHeeJHRsmV/2d5xf2BAEPMsIkbAS6KF25kSECQjKrx8E/zSe8LrhO6lHHA== X-Received: by 2002:a63:d90b:: with SMTP id r11mr379313pgg.81.1624467017036; Wed, 23 Jun 2021 09:50:17 -0700 (PDT) Original-Received: from [192.168.1.2] (cpe-76-168-148-233.socal.res.rr.com. [76.168.148.233]) by smtp.googlemail.com with ESMTPSA id j16sm403585pgh.69.2021.06.23.09.50.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 23 Jun 2021 09:50:16 -0700 (PDT) In-Reply-To: Content-Language: en-US Received-SPF: pass client-ip=2607:f8b0:4864:20::42c; envelope-from=jporterbugs@gmail.com; helo=mail-pf1-x42c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:270995 Archived-At: This is a multi-part message in MIME format. --------------3BF611C07046C344372508B8 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 6/23/2021 6:58 AM, Stefan Monnier wrote: >>> + (sort (cdr keymap) >>> + (lambda (a b) >>> + (string< (bindings--menu-item-string (cdr-safe a)) >>> + (bindings--menu-item-string (cdr-safe b))))))) >>> + (setcdr keymap menu-items) >> >> I'm slightly worried about this, though -- will altering the keymap >> structure have any adverse side effects? I don't think so, and testing >> a bit doesn't seem to reveal anything obvious, but we should be on the >> lookup. > > Altering the keymap by side-effect is not dangerous in and of itself > (`define-key` does it as well). > But the above code will mess things up when applied to a keymap that has > a parent keymap. > > It will also fail to do its job on menu keymaps which use a vector > rather or on composed keymaps (which can be > created "implicitly", e.g. when looking up `menu-bar` in a keymap which > has a `menu-bar` binding and which additionally inherits from a keymap > that also has a `menu-bar` binding). > > To solve those issues I think one would have to `map-keymap` and return > a brand new keymap rather than modify it in-situ, AFAICT. Attached is a patch that uses `map-keymap'. I've modeled the implementation on `keymap-canonicalize', so hopefully it's sufficiently-correct. The patch doesn't do anything special about char-ranges, but I'm not sure that matters for menus. One difference in this patch is that `mode-line-mode-menu' itself is no longer updated after sorting. It just returns a new keymap. The performance is the same as before, which is good, and this is probably a bit safer too. - Jim --------------3BF611C07046C344372508B8 Content-Type: text/plain; charset=UTF-8; name="0001-Sort-the-items-in-mode-line-mode-menu-before-display.patch" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename*0="0001-Sort-the-items-in-mode-line-mode-menu-before-display.pa"; filename*1="tch" RnJvbSAwMzRmMTBkZDZmMmU0Y2Q1NWM2YjViNmFhMjNmYjkzZGUxN2UxMGM3IE1vbiBTZXAg MTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBKaW0gUG9ydGVyIDxqcG9ydGVyYnVnc0BnbWFpbC5j b20+CkRhdGU6IFdlZCwgMjMgSnVuIDIwMjEgMDk6NDM6MzcgLTA3MDAKU3ViamVjdDogW1BB VENIX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g ICAgICAgICAgICA7IE5vdCBhIG1lbnUtaXRlbS4KKyAgICgoZXEgJ21lbnUtaXRlbSAoY2Fy IGl0ZW0pKQorICAgIChldmFsIChjYWRyIGl0ZW0pKSkKKyAgICgoc3RyaW5ncCAoY2FyIGl0 ZW0pKQorICAgIChjYXIgaXRlbSkpCisgICAodCBuaWwpKSkgICAgICAgICAgICAgICAgICAg ICAgICAgICAgOyBOb3QgYSBtZW51LWl0ZW0gZWl0aGVyLgorCisoZGVmdW4gYmluZGluZ3Mt LXNvcnQta2V5bWFwIChtYXApCisgICJTb3J0IHRoZSBiaW5kaW5ncyBpbiBNQVAgaW4gYWxw aGFiZXRpY2FsIG9yZGVyIGJ5IG1lbnUtaXRlbSBzdHJpbmcuCitUaGUgb3JkZXIgb2YgYmlu ZGluZ3MgaW4gYSBrZXltYXAgbWF0dGVycyBvbmx5IHdoZW4gaXQgaXMgdXNlZCBhcworYSBt ZW51LCBzbyB0aGlzIGZ1bmN0aW9uIGlzIG5vdCB1c2VmdWwgZm9yIG5vbi1tZW51IGtleW1h cHMuIgorICAobGV0ICgoYmluZGluZ3MgbmlsKQorICAgICAgICAocHJvbXB0IChrZXltYXAt cHJvbXB0IG1hcCkpKQorICAgICh3aGlsZSAoa2V5bWFwcCBtYXApCisgICAgICAoc2V0cSBt YXAgKG1hcC1rZXltYXAKKyAgICAgICAgICAgICAgICAgKGxhbWJkYSAoa2V5IGl0ZW0pCisg ICAgICAgICAgICAgICAgICAgOzsgRklYTUU6IEhhbmRsZSBjaGFyLXJhbmdlcyBoZXJlPwor ICAgICAgICAgICAgICAgICAgIChwdXNoIChjb25zIGtleSBpdGVtKSBiaW5kaW5ncykpCisg ICAgICAgICAgICAgICAgIG1hcCkpKQorICAgIDs7IFNvcnQgdGhlIGJpbmRpbmdzIGFuZCBt YWtlIGEgbmV3IGtleW1hcCBmcm9tIHRoZW0uCisgICAgKHNldHEgYmluZGluZ3MKKyAgICAg ICAgICAoc29ydCBiaW5kaW5ncworICAgICAgICAgICAgICAgIChsYW1iZGEgKGEgYikKKyAg ICAgICAgICAgICAgICAgIChzdHJpbmc8IChiaW5kaW5ncy0tbWVudS1pdGVtLXN0cmluZyAo Y2RyLXNhZmUgYSkpCisgICAgICAgICAgICAgICAgICAgICAgICAgICAoYmluZGluZ3MtLW1l bnUtaXRlbS1zdHJpbmcgKGNkci1zYWZlIGIpKSkpKSkKKyAgICAobmNvbmMgKG1ha2Utc3Bh cnNlLWtleW1hcCBwcm9tcHQpIGJpbmRpbmdzKSkpCisKIChkZWZ2YXIgbW9kZS1saW5lLW1h am9yLW1vZGUta2V5bWFwCiAgIChsZXQgKChtYXAgKG1ha2Utc3BhcnNlLWtleW1hcCkpKQog ICAgIChiaW5kaW5ncy0tZGVmaW5lLWtleSBtYXAgW21vZGUtbGluZSBkb3duLW1vdXNlLTFd CiAgICAgICBgKG1lbnUtaXRlbSAiTWVudSBCYXIiIGlnbm9yZQogICAgICAgICA6ZmlsdGVy ICwobGFtYmRhIChfKSAobW91c2UtbWVudS1tYWpvci1tb2RlLW1hcCkpKSkKICAgICAoZGVm aW5lLWtleSBtYXAgW21vZGUtbGluZSBtb3VzZS0yXSAnZGVzY3JpYmUtbW9kZSkKLSAgICAo ZGVmaW5lLWtleSBtYXAgW21vZGUtbGluZSBkb3duLW1vdXNlLTNdIG1vZGUtbGluZS1tb2Rl LW1lbnUpCisgICAgKGJpbmRpbmdzLS1kZWZpbmUta2V5IG1hcCBbbW9kZS1saW5lIGRvd24t bW91c2UtM10KKyAgICAgIGAobWVudS1pdGVtICJNZW51IEJhciIgLG1vZGUtbGluZS1tb2Rl LW1lbnUKKyAgICAgICAgOmZpbHRlciBiaW5kaW5ncy0tc29ydC1rZXltYXApKQogICAgIG1h cCkgIlwKIEtleW1hcCB0byBkaXNwbGF5IG9uIG1ham9yIG1vZGUuIikKIAogKGRlZnZhciBt b2RlLWxpbmUtbWlub3ItbW9kZS1rZXltYXAKLSAgKGxldCAoKG1hcCAobWFrZS1zcGFyc2Ut a2V5bWFwKSkpCisgIChsZXQgKChtYXAgKG1ha2Utc3BhcnNlLWtleW1hcCkpCisgICAgICAg IChtb2RlLW1lbnUtYmluZGluZworICAgICAgICAgYChtZW51LWl0ZW0gIk1lbnUgQmFyIiAs bW9kZS1saW5lLW1vZGUtbWVudQorICAgICAgICAgICA6ZmlsdGVyIGJpbmRpbmdzLS1zb3J0 LWtleW1hcCkpKQogICAgIChkZWZpbmUta2V5IG1hcCBbbW9kZS1saW5lIGRvd24tbW91c2Ut MV0gJ21vdXNlLW1pbm9yLW1vZGUtbWVudSkKICAgICAoZGVmaW5lLWtleSBtYXAgW21vZGUt bGluZSBtb3VzZS0yXSAnbW9kZS1saW5lLW1pbm9yLW1vZGUtaGVscCkKLSAgICAoZGVmaW5l LWtleSBtYXAgW21vZGUtbGluZSBkb3duLW1vdXNlLTNdIG1vZGUtbGluZS1tb2RlLW1lbnUp Ci0gICAgKGRlZmluZS1rZXkgbWFwIFtoZWFkZXItbGluZSBkb3duLW1vdXNlLTNdIG1vZGUt bGluZS1tb2RlLW1lbnUpCisgICAgKGRlZmluZS1rZXkgbWFwIFttb2RlLWxpbmUgZG93bi1t b3VzZS0zXSBtb2RlLW1lbnUtYmluZGluZykKKyAgICAoZGVmaW5lLWtleSBtYXAgW2hlYWRl ci1saW5lIGRvd24tbW91c2UtM10gbW9kZS1tZW51LWJpbmRpbmcpCiAgICAgbWFwKSAiXAog S2V5bWFwIHRvIGRpc3BsYXkgb24gbWlub3IgbW9kZXMuIikKIAotLSAKMi4yNS4xCgo= --------------3BF611C07046C344372508B8--