From 56a5d88324a3d10b8ba52d673f58fc43b01b1b89 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Wed, 14 Feb 2024 02:54:01 +0000
Subject: [PATCH] Move to straight for packages, with easy reversion to package.el

---
 .gitignore         |    4 +
 init.el            |   40 +++++--------
 config.org         |   40 +++++++------
 early-init.el      |    4 +
 package-manager.el |   41 +++++++++++++
 5 files changed, 85 insertions(+), 44 deletions(-)

diff --git a/.gitignore b/.gitignore
index 572e2af..f1ac5e3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,7 @@
 /*
 !config.org
 !init.el
+!early-init.el
+!package-manager.el
 !.gitignore
-!snippets
\ No newline at end of file
+!snippets
diff --git a/config.org b/config.org
index 8d8f8e0..f4b8d99 100644
--- a/config.org
+++ b/config.org
@@ -405,6 +405,14 @@
   (global-set-key (kbd "C-c e i") 'init-visit)
 #+END_SRC
 
+*** Go to early-init.el
+#+BEGIN_SRC emacs-lisp
+  (defun early-init-visit ()
+    (interactive)
+    (find-file "~/.emacs.d/early-init.el"))
+  (global-set-key (kbd "C-c e e") 'early-init-visit)
+#+END_SRC
+
 *** Reload configuration
 #+BEGIN_SRC emacs-lisp
   (defun config-reload ()
@@ -1087,6 +1095,7 @@
 #+BEGIN_SRC emacs-lisp
   (use-package tex
     :ensure auctex
+    :straight auctex
     :config
     (setq TeX-auto-save t
           TeX-parse-self t
@@ -1242,17 +1251,18 @@
 *** MATLAB
 Mode for editing MATLAB m-files.
 #+BEGIN_SRC emacs-lisp
-    (use-package matlab
-      :ensure matlab-mode
-      :config
-      (autoload 'matlab-mode "matlab" "Matlab Editing Mode" t)
-      (add-to-list
-       'auto-mode-alist
-       '("\\.m$" . matlab-mode))
-      (setq matlab-indent-function t)
-      (setq matlab-shell-command "matlab")
-      (matlab-cedet-setup)
-      (add-to-list 'company-backends 'company-matlab-shell))
+  (use-package matlab
+    :ensure matlab-mode
+    :straight matlab-mode
+    :config
+    (autoload 'matlab-mode "matlab" "Matlab Editing Mode" t)
+    (add-to-list
+     'auto-mode-alist
+     '("\\.m$" . matlab-mode))
+    (setq matlab-indent-function t)
+    (setq matlab-shell-command "matlab")
+    (matlab-cedet-setup)
+    (add-to-list 'company-backends 'company-matlab-shell))
 #+END_SRC
 
 *** COMMENT MIPS
@@ -1322,13 +1332,6 @@
     (add-to-list 'lsp-enabled-clients 'jedi))
 #+END_SRC
 * Org mode
-** Up to date org
-Pull the latest org mode from the repository, rather than the org which comes with emacs.
-#+BEGIN_SRC emacs-lisp
-  (use-package org
-    :pin gnu)
-#+END_SRC
-
 ** Small tweaks
 Small quality of life changes to org-mode.
 #+BEGIN_SRC emacs-lisp
@@ -1425,6 +1428,7 @@
 #+BEGIN_SRC emacs-lisp
   (use-package emms-setup
     :ensure emms
+    :straight emms
     :init
     (add-to-list 'load-path "~/elisp/emms/")
     :config
diff --git a/early-init.el b/early-init.el
new file mode 100644
index 0000000..6ed436b
--- /dev/null
+++ b/early-init.el
@@ -0,0 +1,4 @@
+;; Disable package at startup in case of other package managers being used
+(setq package-enable-at-startup nil)
+(makunbound 'package-archives)
+(setq-default package-manager 'package)
diff --git a/init.el b/init.el
index f911bd8..cd71472 100644
--- a/init.el
+++ b/init.el
@@ -2,37 +2,27 @@
 (setq custom-file "~/.emacs.d/custom.el")
 (load custom-file 'noerror)
 
-;; Repos
-(require 'package)
-(setq package-archives '(("gnu"   . "https://elpa.gnu.org/packages/")
-			             ("nongnu"  . "https://elpa.nongnu.org/nongnu/")
-			             ("melpa" . "https://melpa.org/packages/")))
-(package-initialize)
-
-;; use-package
-(unless (package-installed-p 'use-package)
-  (package-refresh-contents)
-  (package-install 'use-package))
-(setq package-install-upgrade-built-in t)
-
-(eval-when-compile
-  (require 'use-package))
-(require 'use-package-ensure)
-(setq use-package-always-ensure t)
+;; Load package manager
+(setq package-manager 'straight)
+(load "~/.emacs.d/package-manager.el" `noerror)
 
 ;; auto-package-update
-(use-package auto-package-update
-  :ensure t
-  :config
-  (setq auto-package-update-delete-old-versions t)
-  (setq auto-package-update-hide-results t)
-  (setq auto-package-update-interval 7)
-  (setq auto-package-update-at-time "03:00")
-  (auto-package-update-maybe))
+(when (eq package-manager 'package)
+  (use-package auto-package-update
+    :ensure t
+    :config
+    (setq auto-package-update-delete-old-versions t)
+    (setq auto-package-update-hide-results t)
+    (setq auto-package-update-interval 7)
+    (setq auto-package-update-at-time "03:00")
+    (auto-package-update-maybe)))
 
 ;; diminish
 (use-package diminish)
 
+;; org-mode
+(use-package org)
+
 ;; redirect to org config file
 (when (file-readable-p "~/.emacs.d/config.org")
   (org-babel-load-file "~/.emacs.d/config.org"))
diff --git a/package-manager.el b/package-manager.el
new file mode 100644
index 0000000..5ba21f7
--- /dev/null
+++ b/package-manager.el
@@ -0,0 +1,41 @@
+;;; Package manager selection
+;; Can chose the package manager from the default package.el or straight.el
+;; There may be problems with some of them in corporate environments
+
+;; Repos
+(cond ((eq package-manager 'package)
+       (require 'package)
+       (when (not (boundp 'package-archives))
+         (setq package-archives '(("gnu"   . "https://elpa.gnu.org/packages/")
+			                      ("nongnu"  . "https://elpa.nongnu.org/nongnu/")
+			                      ("melpa" . "https://melpa.org/packages/"))))
+       (package-initialize)
+
+       ;; use-package
+       (unless (package-installed-p 'use-package)
+         (package-refresh-contents)
+         (package-install 'use-package))
+       (setq package-install-upgrade-built-in t)
+
+       (eval-when-compile
+         (require 'use-package))
+       (require 'use-package-ensure)
+       (setq use-package-always-ensure t))
+      ((eq package-manager 'straight)
+       (defvar bootstrap-version)
+       (let ((bootstrap-file
+              (expand-file-name
+               "straight/repos/straight.el/bootstrap.el"
+               (or (bound-and-true-p straight-base-dir)
+                  user-emacs-directory)))
+             (bootstrap-version 7))
+         (unless (file-exists-p bootstrap-file)
+           (with-current-buffer
+               (url-retrieve-synchronously
+                "https://raw.githubusercontent.com/radian-software/straight.el/develop/install.el"
+                'silent 'inhibit-cookies)
+             (goto-char (point-max))
+             (eval-print-last-sexp)))
+         (load bootstrap-file nil 'nomessage))
+       (straight-use-package 'use-package)
+       (setq straight-use-package-by-default t)))

--
Gitblit v1.9.3