Subject: Re: bug#66022: 30.0.50; kmacro overwriting global keybindings

From: Gerd Möllmann <[email-masked]>
Date: Sun, 17 Sep 2023 11:50:16 +0200

Eli Zaretskii <[email-masked]> writes:

>> From: Gerd Möllmann <[email-masked]>
>> Cc: [email-masked],  [email-masked]
>> Date: Sun, 17 Sep 2023 10:58:11 +0200
>> 
>> Eli Zaretskii <[email-masked]> writes:
>> 
>> >> > What does use-package do when the "package" is "emacs"?  IOW, at what
>> >> > point in time will the above binding be executed, and what event will
>> >> > trigger that?
>> >> 
>> >> Nothing.  It's sort of an idiom I found on the net for general
>> >> customizations and so on that don't belong to a particular package.
>> >> Some people apparently name it "general".
>> >
>> > So the above binding of F2 is never executed in your case?  Or else,
>> > how should I interpret your "Nothing" response to my question about
>> > the point in time when this binding is executed?
>> 
>> No, sorry for not being clear.  I meant use-package doesn't load a
>> package, or something like that.  It executes normally, and in
>> particular, it uses bind-key for the :bind clause, so that F2 gets
>> bound.
>
> Does it do that immediately as that particular use-package form is
> read and interpreted, or does it do it later (e.g., after the init
> file was loaded or something)?
>
> I'm trying to figure out when could the autoloaded bindings become in
> effect so as to countermand your use-package bindings (if this is what
> happens; does it?).

Hm, I must confess I'm not a great use-package expert, but maybe we can
figure this out.  I used pp-macro-expand-last-sexp on the use-package in
my .init.el.  The result is at the end.

I'd say it looks like the bind-key is done like so

  (condition-case-unless-debug err
      (progn
        ...
	(unless (fboundp 'mode-line-other-buffer)
	  (autoload #'mode-line-other-buffer "emacs" nil t))
        ...
        (bind-key ...)

This is the full macro expansion:

(progn
  (defvar use-package--warning47
    #'(lambda (keyword err)
	(let
	    ((msg
	      (format "%s/%s: %s" 'emacs keyword
		      (error-message-string err))))
	  (display-warning 'use-package msg :error))))
  (condition-case-unless-debug err
      (progn
	(let ((custom--inhibit-theme-enable nil))
	  (unless (memq 'use-package custom-known-themes)
	    (deftheme use-package) (enable-theme 'use-package)
	    (setq custom-enabled-themes
		  (remq 'use-package custom-enabled-themes)))
	  (custom-theme-set-variables 'use-package
				      '(cursor-type 'bar nil nil
						    "Customized with use-package emacs")
				      '(scroll-bar-mode nil nil nil
							"Customized with use-package emacs")
				      '(custom-enabled-themes
					'(modus-vivendi) nil nil
					"Customized with use-package emacs")
				      '(default-frame-alist
					'((height . 82) (width . 120))
					nil nil
					"Customized with use-package emacs")
				      '(ns-alternate-modifier 'meta
							      nil nil
							      "Customized with use-package emacs")
				      '(ns-left-command-modifier
					'super nil nil
					"Customized with use-package emacs")
				      '(ns-right-alternate-modifier
					'none nil nil
					"Customized with use-package emacs")
				      '(ns-right-command-modifier
					'meta nil nil
					"Customized with use-package emacs")
				      '(duplicate-line-final-position
					1 nil nil
					"Customized with use-package emacs")
				      '(enable-local-eval t nil nil
							  "Customized with use-package emacs")
				      '(enable-local-variables :all
							       nil nil
							       "Customized with use-package emacs")
				      '(help-window-select t nil nil
							   "Customized with use-package emacs")
				      '(server-mode t nil nil
						    "Customized with use-package emacs")
				      '(send-mail-function
					'smtpmail-send-it nil nil
					"Customized with use-package emacs")
				      '(vc-follow-symlinks t nil nil
							   "Customized with use-package emacs")
				      '(disabled-command-function nil
								  nil
								  nil
								  "Customized with use-package emacs")
				      '(ring-bell-function 'ignore nil
							   nil
							   "Customized with use-package emacs")
				      '(initial-scratch-message nil
								nil
								nil
								"Customized with use-package emacs")))
	(unless (fboundp 'undo) (autoload #'undo "emacs" nil t))
	(unless (fboundp 'undo-redo)
	  (autoload #'undo-redo "emacs" nil t))
	(unless (fboundp 'ibuffer) (autoload #'ibuffer "emacs" nil t))
	(unless (fboundp 'zap-up-to-char)
	  (autoload #'zap-up-to-char "emacs" nil t))
	(unless (fboundp 'mode-line-other-buffer)
	  (autoload #'mode-line-other-buffer "emacs" nil t))
	(unless (fboundp 'duplicate-dwim)
	  (autoload #'duplicate-dwim "emacs" nil t))
	(unless (fboundp 'switch-to-buffer)
	  (autoload #'switch-to-buffer "emacs" nil t))
	(unless (fboundp 'beginning-of-buffer)
	  (autoload #'beginning-of-buffer "emacs" nil t))
	(unless (fboundp 'end-of-buffer)
	  (autoload #'end-of-buffer "emacs" nil t))
	(unless (fboundp 'previous-buffer)
	  (autoload #'previous-buffer "emacs" nil t))
	(unless (fboundp 'next-buffer)
	  (autoload #'next-buffer "emacs" nil t))
	(unless (fboundp 'execute-extended-command)
	  (autoload #'execute-extended-command "emacs" nil t))
	(unless (fboundp 'delete-other-windows)
	  (autoload #'delete-other-windows "emacs" nil t))
	(unless (fboundp 'global-text-scale-adjust)
	  (autoload #'global-text-scale-adjust "emacs" nil t))
	(eval-after-load 'emacs
	  '(condition-case-unless-debug err
	       (progn
		 (defun my-startup-time nil
		   (message "Start time %s." (emacs-init-time)))
		 (add-hook 'emacs-startup-hook #'my-startup-time) t)
	     (error (funcall use-package--warning47 :config err))))
	(bind-keys :package emacs ("C-z" . undo) ("C-S-z" . undo-redo)
		   ("C-x C-b" . ibuffer) ("C-x 6") ("C-x C-k")
		   ("M-z" . zap-up-to-char)
		   ("<f2>" . mode-line-other-buffer)
		   ("C-c o" . mode-line-other-buffer)
		   ("C-c C-o" . mode-line-other-buffer)
		   ("<f4>" lambda nil (interactive)
		    (kill-buffer (current-buffer)))
		   ("C-c k" lambda nil (interactive)
		    (kill-buffer (current-buffer)))
		   ("C-c C-k" lambda nil (interactive)
		    (kill-buffer (current-buffer)))
		   ("C-<up>" lambda nil (interactive) (scroll-up 1))
		   ("C-<down>" lambda nil (interactive)
		    (scroll-down 1))
		   ("C-<tab>" . other-window)
		   ("C-c d" . duplicate-dwim) (" " . switch-to-buffer)
		   ("s-<up>" . beginning-of-buffer)
		   ("s-<down>" . end-of-buffer)
		   ("s-<left>" . previous-buffer)
		   ("s-<right>" . next-buffer)
		   ("s-x" . execute-extended-command)
		   ("s-o" . delete-other-windows)
		   ("s-+" . global-text-scale-adjust)
		   ("s--" . global-text-scale-adjust) ("s-h") ("s-m")
		   ("s-p") ("s-q")))
    (error (funcall use-package--warning47 :catch err))))