0

Could you help me with smarter way of handling package-archives? At the moment I have the following in my init.el

;; This is dumb but...
(if (eq system-type 'windows-nt)
    (setq package-archives '(("elpa" .  "http://elpa.gnu.org/packages/")
                             ("melpa" . "http://melpa.org/packages/")))
  (setq package-archives '(("elpa" .  "https://elpa.gnu.org/packages/")
                           ("melpa" . "https://melpa.org/packages/"))))

Can I make it more DRY? Like this one (doesn't work):

(defvar package-protocol "https://")
(if (eq system-type 'windows-nt) (setq package-protocol "http://"))
(setq package-archives '(("elpa" .  (concat package-protocol "elpa.gnu.org/packages/"))
                         ("melpa" . (concat package-protocol "melpa.org/packages/")))))

thx, combined from the answers, using local variable:

;; Set up packaging system
(let ((package-protocol (if (eq system-type 'windows-nt) "http://" "https://")))
  (setq package-archives `(("elpa" .  ,(concat package-protocol "elpa.gnu.org/packages/"))
                           ("melpa" . ,(concat package-protocol "melpa.org/packages/")))))
Maxim Kim
  • 1,516
  • 9
  • 17
  • 4
    Possible duplicate of [How to evaluate the variables before adding them to a list?](http://emacs.stackexchange.com/questions/7481/how-to-evaluate-the-variables-before-adding-them-to-a-list) – Kaushal Modi May 20 '16 at 15:02

2 Answers2

1

This might help you get set up, you can change it afterwards

(let ((package-protocol (if (eq system-type 'windows-nt) "http://" "https://"))
      (package-suffix ".org/packages/")
      (archives-to-use '("elpa.gnu" "melpa")))
  (setq package-archives
    (cl-loop for cur-archive in archives-to-use
    collect (cons (first (split-string cur-archive "\\.")) 
                  (concat package-protocol cur-archive package-suffix)))))

It lets the package protocol, and then the suffix. It then loops through our list of archives and appends the suffix and prefix to each element. It sets that collected list to package-archives

Jules
  • 1,275
  • 7
  • 12
1

Easy:

(setq package-archives 
      (list (cons "elpa" (concat package-protocol "elpa.gnu.org/packages/"))
            (cons "melpa" (concat package-protocol "melpa.org/packages/")))))

or use backquote:

(setq package-archives 
      `(("elpa" .  ,(concat package-protocol "elpa.gnu.org/packages/"))
        ("melpa" . ,(concat package-protocol "melpa.org/packages/")))))
sds
  • 5,928
  • 20
  • 39