emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Calling (package-initialize) sooner during initialization


From: Artur Malabarba
Subject: Re: Calling (package-initialize) sooner during initialization
Date: Tue, 31 Mar 2015 11:52:34 +0100

Something like this?

diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index 526c0b4..cf92557 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -1628,6 +1628,28 @@ PACKAGES are satisfied, i.e. that PACKAGES is computed
 using `package-compute-transaction'."
   (mapc #'package-install-from-archive packages))

+(defun package--ensure-init-file ()
+  "Ensure that the user's init file calls `package-initialize'."
+  (let ((buffer (find-buffer-visiting user-init-file)))
+    (with-current-buffer (or buffer (find-file-noselect user-init-file))
+      (save-excursion
+        (save-restriction
+          (widen)
+          (goto-char (point-min))
+          (unless (search-forward "(package-initialize)" nil 'noerror)
+            (goto-char (point-min))
+            (insert
+             ";; Added by Package.el.  This must come before
configurations of\n"
+             ";; installed packages.  Don't delete this line.  If you
don't want it,\n"
+             ";; just comment it out by adding a semicolon to the
start of the line.\n"
+             "(package-initialize)\n")
+            (unless (looking-at-p "$")
+              (insert "\n"))
+            (let ((file-precious-flag t))
+              (save-buffer)))
+          (unless buffer
+            (kill-buffer (current-buffer))))))))
+
 ;;;###autoload
 (defun package-install (pkg &optional dont-select)
   "Install the package PKG.
@@ -1656,6 +1678,7 @@ to install it but still mark it as selected."
                                   package-archive-contents))
                     nil t))
            nil)))
+  (package--ensure-init-file)
   (let ((name (if (package-desc-p pkg)
                   (package-desc-name pkg)
                 pkg)))
@@ -1699,6 +1722,7 @@ is derived from the main .el file in the directory.

 Downloads and installs required packages as needed."
   (interactive)
+  (package--ensure-init-file)
   (let* ((pkg-desc
           (cond
             ((derived-mode-p 'dired-mode)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]