Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A Useful Recipe: Interactively inspect what form use-package expands to under various scenarios -- minimal expansion, compilation etc. #1056

Open
emacksnotes opened this issue Jun 11, 2023 · 2 comments
Labels

Comments

@emacksnotes
Copy link

emacksnotes commented Jun 11, 2023

Not a issue but a recipe ...

I wonder what variables apart from byte-compile-current-file affects the macroexpansion of use-package. If there are other such variables, they can be prompted for and plugged in to the env bindings below.

Slightly formalized the goings on in #1032

(advice-add 'pp-macroexpand-last-sexp :around
	    (defun pp-macroexpand-last-sexp--around
		(orig-fun &rest orig-args)
	      (pcase-let*
		  ((`(,arg)
		    orig-args)
		   (sexp (pp-last-sexp))
		   (env (when (eq 'use-package (car sexp))
			  `((byte-compile-current-file ,(yes-or-no-p "Byte compilation"))
			    (use-package-expand-minimally ,(yes-or-no-p "Minimal"))))))
		(funcall `(lambda ()
			    ,(macroexp-let* `(,@env)
					    `(progn
					       (if ',arg
						   (save-excursion
						     (insert "\n\n")
						     (apply ,orig-fun ',orig-args))
						 (apply ,orig-fun ',orig-args)))))))))

;; (advice-remove 'pp-macroexpand-last-sexp 'pp-macroexpand-last-sexp--around)

Create a quick binding for the above command

(local-set-key (kbd "C-c C-c") #'pp-macroexpand-last-sexp)

Insert the following in to *scratch* buffer

(use-package rainbow-mode
  :functions (rainbow-x-color-luminance)
  :config
  (rainbow-x-color-luminance "cyan"))

(dolist (elt list value)
  (setq value (cons elt value)))

Put the cursor after any of the above two forms and do C-u C-c C-c and see what happens ...

For example, for byte compilation and minimal scenario I get

(progn
  (eval-and-compile
    (declare-function rainbow-x-color-luminance #1="rainbow-mode")
    (eval-when-compile
      (with-demoted-errors "Cannot load rainbow-mode: %S" nil
			   (unless
			       (featurep 'rainbow-mode)
			     (load #1# nil t)))))
  (require 'rainbow-mode nil nil)
  (rainbow-x-color-luminance "cyan")
  t)

For no byte-compiled, and minimal scenario, I get

(progn
  (require 'rainbow-mode nil nil)
  (rainbow-x-color-luminance "cyan")
  t)
@emacksnotes emacksnotes changed the title Command to inspect what form use-package expands to under various scenarios -- minimal expansion, compilation etc. A Useful Recipe: Interactively inspect what form use-package expands to under various scenarios -- minimal expansion, compilation etc. Jun 11, 2023
@emacksnotes
Copy link
Author

emacksnotes commented Jun 11, 2023

Here is another variation of the above advice ... This uses eval.

(use-package use-package
  :config (advice-add 'pp-macroexpand-last-sexp :around
		      (defun pp-macroexpand-last-sexp--around
			  (orig-fun &rest orig-args)
			(pcase-let*
			    ((`(,arg)
			      orig-args)
			     (sexp (pp-last-sexp))
			     (env (append
				   (when (eq 'use-package (car sexp))
				     `((byte-compile-current-file ,(yes-or-no-p "Byte compilation"))
				       (use-package-expand-minimally ,(yes-or-no-p "Minimal")))))))
			  (eval `(let ,env
				   (if ',arg
				       (save-excursion
					 (insert "\n\n")
					 (apply ',orig-fun ',orig-args))
				     (apply ',orig-fun ',orig-args))))))))

@emacksnotes emacksnotes changed the title A Useful Recipe: Interactively inspect what form use-package expands to under various scenarios -- minimal expansion, compilation etc. A Useful Recipe: Interactively inspect what form use-package expands to under various scenarios -- minimal expansion, compilation etc. Jun 11, 2023
@emacksnotes emacksnotes changed the title A Useful Recipe: Interactively inspect what form use-package expands to under various scenarios -- minimal expansion, compilation etc. A Useful Recipe: Interactively inspect what form use-package expands to under various scenarios -- minimal expansion, compilation etc. Jun 11, 2023
@skangas skangas added the docs label Aug 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants