1
\$\begingroup\$

I decided to create a portable Emacs config and install packages I use if it's necessary on a fresh computer.

Here is what I've done:

; loop through names of packages and install them if needed
(let ((packages '('use-package 'yasnippet 'expand-region 'bm 'undo-tree))
 (content-not-refreshed t)
 )
 (mapc
 (lambda (name)
 (unless (package-installed-p (eval name))
 (when content-not-refreshed
 (package-refresh-contents)
 (setq content-not-refreshed nil))
 (package-install (eval name))))
 packages
 ))
; load packages
(require 'use-package)
(require 'yasnippet)
(require 'expand-region)
(require 'bm)
(require 'undo-tree)

Is this code good? Can it be improved?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jan 19, 2022 at 9:40
\$\endgroup\$
4
  • \$\begingroup\$ If you use use-package - then you don't need to list the other packages in this list - just use-package to load the packages \$\endgroup\$ Commented Jan 19, 2022 at 14:28
  • \$\begingroup\$ emacs.stackexchange.com/tags/elisp/info \$\endgroup\$ Commented Jan 19, 2022 at 15:08
  • \$\begingroup\$ @mmmmmm use-package won't install it from melpa if it's missing. And I think, we won't need use-package for this case: we know the package is installed. \$\endgroup\$ Commented Jan 19, 2022 at 15:25
  • 2
    \$\begingroup\$ @user4035 use-package does see the :ensure keyword in the manual jwiegley.github.io/use-package/keywords It even explicitl;y says " This is particularly useful if you share your .emacs among several machines; the relevant packages are downloaded automatically once declared in your .emacs. " \$\endgroup\$ Commented Jan 19, 2022 at 15:58

1 Answer 1

4
\$\begingroup\$

When quoting a list of symbols, it's redundant to quote each symbol within the list.


There's no need to eval a symbol.


This sequence looks strange:

(require 'use-package)
(require 'yasnippet)
(require 'expand-region)
(require 'bm)
(require 'undo-tree)

Since we're looping over those same names in the preceding loop, why not just (require name) as the last form within the lambda?


Addressing these issues gives somewhat improved code:

; loop through names of packages: install if needed, then load
(let ((packages '(use-package yasnippet expand-region bm undo-tree))
 (content-not-refreshed t)
 )
 (mapc
 (lambda (name)
 (unless (package-installed-p name)
 (when content-not-refreshed
 (package-refresh-contents)
 (setq content-not-refreshed nil))
 (package-install name))
 (require name))
 packages
 ))

Further improvement would maintain the list of packages as a customisable variable.

answered Jan 19, 2022 at 20:22
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.