From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Felician Nemeth Newsgroups: gmane.emacs.bugs Subject: bug#47215: 28.0.50; Let M-x switch between M-x and M-X Date: Sat, 10 Apr 2021 19:56:49 +0200 Message-ID: <87r1ji3twe.fsf@betli.tmit.bme.hu> References: <87tup91x6b.fsf@betli.tmit.bme.hu> <87tup9t5f6.fsf@gnus.org> <87k0pi546i.fsf@betli.tmit.bme.hu> <87czv9g74a.fsf@gnus.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32999"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) Cc: 47215@debbugs.gnu.org To: Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Apr 10 19:57:16 2021 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 1lVHrA-0008Sp-5g for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 10 Apr 2021 19:57:16 +0200 Original-Received: from localhost ([::1]:35094 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lVHr9-0002Kc-7j for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 10 Apr 2021 13:57:15 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42892) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lVHqx-0002K9-N3 for bug-gnu-emacs@gnu.org; Sat, 10 Apr 2021 13:57:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:41697) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lVHqw-00008C-9T for bug-gnu-emacs@gnu.org; Sat, 10 Apr 2021 13:57:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lVHqw-0006s7-78 for bug-gnu-emacs@gnu.org; Sat, 10 Apr 2021 13:57:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Felician Nemeth Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 10 Apr 2021 17:57:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 47215 X-GNU-PR-Package: emacs Original-Received: via spool by 47215-submit@debbugs.gnu.org id=B47215.161807742026407 (code B ref 47215); Sat, 10 Apr 2021 17:57:02 +0000 Original-Received: (at 47215) by debbugs.gnu.org; 10 Apr 2021 17:57:00 +0000 Original-Received: from localhost ([127.0.0.1]:53243 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lVHqt-0006rq-RE for submit@debbugs.gnu.org; Sat, 10 Apr 2021 13:57:00 -0400 Original-Received: from mail-ed1-f47.google.com ([209.85.208.47]:36409) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lVHqr-0006rb-8q for 47215@debbugs.gnu.org; Sat, 10 Apr 2021 13:56:58 -0400 Original-Received: by mail-ed1-f47.google.com with SMTP id 18so10206764edx.3 for <47215@debbugs.gnu.org>; Sat, 10 Apr 2021 10:56:57 -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:face:mime-version; bh=gWyYRXYKwaq9XsbHaIs4FJ8uxmsiZfOQ8aIQOuX6qXI=; b=rxRGibH6VKqnCscclbXJJGWBN1yTRxTKgrpAmbVpUYYgcgTiZk1G9pkXD2Kza30pjB HaSAeSg5j2ZTa3X2lKH2jvuVTcjFRxUPTOG2RF6X5zGG5XK/fbpyBJIkyUbATIKLMB4w E44HHKI+VmZpGMaZYSdRO48ubV4CapseBnJ9zPNbco0ivdQpeNdOJwPJiM1BF7yfzBFI 6iE1Hmc9woMylaW3CJob+9dvU5vXwetzO4naqSPmeSkMdOtQaQKehb8rm7/uY1sV5zYD K8IO163LmhhfY7MrJZEfXLPBg8zM+2F0I1VDEaPLifQX/6UgXTLCTEHnK40ARw1jZZmG J+0Q== 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:face:mime-version; bh=gWyYRXYKwaq9XsbHaIs4FJ8uxmsiZfOQ8aIQOuX6qXI=; b=rVI1hsdtXyjIOD4uSvqYwjrC+XzCo/lSxDs1uxX1awUfxq6Vn9vr2LqNi2ort5852C cpwle9WU+BVwapzII0qOIX4PBJD287SEHopsSKLfA59WxOYxaAxDZjYswjSSq95e6wku A757qE6h/wqkvZEkLlc8ES9t8LROlzZjmF/K1g/MV29e/yLuuNUV00QHxslZAZM8uT7y yEqubpeQ/CyKgH6Y63FOyG+BYyX1tkdtYgALl5Rc7BRAT418hvsuTfykXTcfBWTmohRX ZT1egREwa+5A4VBn2b8S5UAPnE7cAgKmsOiDANeENdOTfognx288fro3OPFgwRKN0/ob ZESw== X-Gm-Message-State: AOAM530KkmCtk4011tUNgirDoMeKCmTQjJudlr9FdWeOg9hhC9LV3r9E 2ucJjCZhAv8yuRc5XKMa5/Qi0S3sUmA= X-Google-Smtp-Source: ABdhPJxSZbQew2BLL19kf5W34HsWkj85YogFp/Gv/cU6Hd+QR0jK+ODdGiY67aRbTKnafv4sE2wx+Q== X-Received: by 2002:a05:6402:27d4:: with SMTP id c20mr22761816ede.271.1618077411080; Sat, 10 Apr 2021 10:56:51 -0700 (PDT) Original-Received: from betli.gmail.com (catv-89-135-146-28.catv.broadband.hu. [89.135.146.28]) by smtp.gmail.com with ESMTPSA id gb22sm2902780ejc.78.2021.04.10.10.56.49 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 10 Apr 2021 10:56:50 -0700 (PDT) In-Reply-To: <87czv9g74a.fsf@gnus.org> (Lars Ingebrigtsen's message of "Sun, 04 Apr 2021 21:52:21 +0200") Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAFVBMVEUMBwgHAgMFAAGPjY7/ //80MDHq6eqJt3pKAAABr0lEQVQ4jX2UzZKDIAzHqR177q7TPbtx2HMr6guQcrbY9txZ0fd/hA0f onXazcEJ/CD8E4Js8/HS9mwjXtqeMRxHXJkakTEm4b4GPVQW8PU8ov4fQCqeThlF60MBWdo1IXzd 2nEEZE7CEAZLwI0N/gJAhTj7ESQAX4gPgO8lyI+cvgViSVPlNomAj2M9gW40eg7VWY3cATjUcyiO Z+i03cFruGLYoUR7VyU3HihdmCEVhoDN65FXkpbSxkomOzsTQN/gySaodGb9Gdi1oSRXP46gdBWh LcUKJNdeGCWac74GKakmne0aHCFvyqJPYLsCFAVlhRTvGdzMdLHqtgRyUulyXIAH7CYQ3AB0Nody JQhAkq/qtOnbjhxdzYDkXPxlH5y4WdUAeUcX1NVJ6GR7UQEYPGWoAnA36OQNn5lRRp38vHTAp9Br LoTmvlPPDoRCKzpjG1SXT89AaT5l456BamJuMcs+NIOMzJ/s5dI6yUVcrARlruwOebfdv6gunTn4 ww3+QjGBEn5suVyLHoSGvAqREuDLN+iqZ+VcFg+HBbsJUU9+FZthbez9T+bdb+kPv2Ls6ct3hTkA AAAASUVORK5CYII= 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:203814 Archived-At: --=-=-= Content-Type: text/plain Lars Ingebrigtsen writes: > Felician Nemeth writes: > >>> I'm not quite sure how to implement this, though -- we basically end up >>> in `completing-read', and `execute-extended-command-for-buffer' would >>> have to define an `M-x' binding there, I guess... and then somehow call >>> `read-extended-command' with the text already in the minibuffer. >>> >>> Anybody got an idea as to how to implement this without rewriting >>> `read-extended-command' completely? >> >> The execute-extended-commands have two undocumented optional arguments >> (command-name and typed). What are they used for? >> >> Anyway, following the fallback logic of `ido-find-file', I was able to >> rebind `M-x' and save the content of the minibuffer with a non-standard >> exit from `execute-extended-command'. Maybe the ugly code below can >> give ideas to someone more knowledgeable. > > Thanks -- I was wondering more about the situation where you've typed > > M-X foo|bar > > (| for point) > > and then hit `M-x' because you want to switch to the other mode. > Ideally, `M-x' should do that, and also preserve the text the user has > typed, and the cursor position. I don't see an obvious simple way to do > that... I've discovered that the initial-input argument of `completing-read` can be written as (STRING . POSITION). The attached patch makes use of it and shows a simple implementation of my original wish. However, the patch creates code duplication. Also, I don't know how it copes with recursive editing: maybe it's not a good idea to rebind M-x when `enable-recursive-minibuffers' is t. Finally, the docstring says initial-input is deprecated. I'm happy to work on the patch if you guide me to the right direction, but this time I'd rather receive a fish instead of being taught how to fish :) --=-=-= Content-Type: #("text/x-diff" 0 11 (face iswitchb-current-match)) Content-Disposition: attachment; filename=m-x.patch Content-Transfer-Encoding: quoted-printable diff --git a/lisp/simple.el b/lisp/simple.el index c48e644345..1bd8ba5993 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -1949,7 +1949,46 @@ read-extended-command-predicate command-completion-default-include-p) (function :tag "Other function"))) =20 -(defun read-extended-command () +(defun execute-extended-command--cycle () + (interactive) + (throw 'cycle + (cons 'cycle (cons (minibuffer-contents) + (- (point) (minibuffer-prompt-end)))))) + +(defun read-extended-command (&optional prompt) + (let ((minibuffer-local-must-match-map minibuffer-local-must-match-map) + (read-extended-command-predicate read-extended-command-predicate) + initial-input ret) + (define-key minibuffer-local-must-match-map + (kbd "M-x") #'execute-extended-command--cycle) + (while (not ret) + (setq ret (catch 'cycle + (read-extended-command-1 prompt initial-input))) + (when (and (consp ret) (eq 'cycle (car ret))) + ;; Cycle to the next setting. There's only two, so it's easy to d= o. + (if read-extended-command-predicate + (setq prompt "M-x " + read-extended-command-predicate nil) + (let ((keymaps + ;; The major mode's keymap and any active minor modes. + (cons + (current-local-map) + (mapcar + #'cdr + (seq-filter + (lambda (elem) + (symbol-value (car elem))) + minor-mode-map-alist))))) + (setq prompt "M-X ") + (setq read-extended-command-predicate + (lambda (symbol buffer) + (or (command-completion-using-modes-p symbol buffer) + (where-is-internal symbol keymaps)))))) + (setq initial-input (cdr ret)) + (setq ret nil))) + ret)) + +(defun read-extended-command-1 (prompt initial-input) "Read command name to invoke in `execute-extended-command'. This function uses the `read-extended-command-predicate' user option." (let ((buffer (current-buffer))) @@ -1976,8 +2015,8 @@ read-extended-command (cons def (delete def all)) all))))) ;; Read a string, completing from and restricting to the set of - ;; all defined commands. Don't provide any initial input. - ;; Save the command read on the extended-command history list. + ;; all defined commands. Save the command read on the + ;; extended-command history list. (completing-read (concat (cond ((eq current-prefix-arg '-) "- ") @@ -1994,9 +2033,7 @@ read-extended-command ;; but actually a prompt other than "M-x" would be confusing, ;; because "M-x" is a well-known prompt to read a command ;; and it serves as a shorthand for "Extended command: ". - (if (memq 'shift (event-modifiers last-command-event)) - "M-X " - "M-x ")) + (or prompt "M-x ")) (lambda (string pred action) (if (and suggest-key-bindings (eq action 'metadata)) '(metadata @@ -2013,7 +2050,7 @@ read-extended-command (funcall read-extended-command-predicate sym buff= er) (error (message "read-extended-command-predicate: %= s: %s" sym (error-message-string err))))))= )) - t nil 'extended-command-history)))) + t initial-input 'extended-command-history)))) =20 (defun command-completion-using-modes-p (symbol buffer) "Say whether SYMBOL has been marked as a mode-specific command in BUFFER= ." @@ -2241,7 +2278,7 @@ execute-extended-command-for-buffer (or (command-completion-using-modes-p symbol buffer) (where-is-internal symbol keymaps))))) (list current-prefix-arg - (read-extended-command) + (read-extended-command "M-X ") execute-extended-command--last-typed))) (with-suppressed-warnings ((interactive-only execute-extended-command)) (execute-extended-command prefixarg command-name typed))) --=-=-=--