I have thought of migrating to use-package: but i dont really dig declarative style of doing configuration, it just feels wrong to me somehow, I created a small package that generates the autoloads of a package, a configuration file and automatically adds to 'load-path the package. All that is required is to add a line calling the configuration file in the emacs init file. I have been able to create seperated modules like this that are very easy to manage.
A list is maintained in plain text that takes cares of adding those packages to load-path, the directory ~/.emacs.d/custom_packages/ is hardcoded because I havent found the necessity to generalise it.
Please provide feedback - or is it really foolish ?
;; packagesv2.el
(defun generate-autoloads-and-create-init-file ()
"Generate autoloads, create an init file for Elisp files in the current buffer's directory.
and add package to 'load-path"
(interactive)
(let* ((buffer-file (or buffer-file-name (dired-get-file-for-visit) (buffer-file-name (other-buffer))))
(directory (file-name-directory buffer-file))
(base-name (file-name-sans-extension (file-name-nondirectory buffer-file)))
(autoloads-file (concat "autoloads_" base-name ".el"))
(placeholder "%s")
(additional-content "
;; pre-init
;; put pre-init contents here
;;; pre-init ends here
;; post-init
(with-eval-after-load '%s
;; put post-init contents here
) ;;; post-init ends here
(provide 'init_%s)")
(resolved-content (replace-regexp-in-string (regexp-quote placeholder) base-name additional-content))
(init-content (format ";; Package initialization for %s\n(require 'autoloads_%s)%s"
base-name (replace-regexp-in-string " " "-" base-name) resolved-content))
(init-file (concat "init_" (replace-regexp-in-string " " "-" base-name) ".el")))
;; Generate autoloads
(make-directory-autoloads directory autoloads-file)
;; Check if init file exists
(unless (file-exists-p init-file)
;; Create init file
(with-temp-buffer
(insert init-content)
(write-region (point-min) (point-max) init-file)
(message "Autoloads generated and saved to %s. Init file %s created with additional content."
autoloads-file init-file)))
;; Append package name to the packages-list file
(append-to-packages-list base-name)
) ; let* scope ends here
) ; func defun ends here
(defun append-to-packages-list (base-name)
"Appends the package name to the packages-list file if it doesn't exist."
(let* ((packages-list "~/.emacs.d/custom_packages/packages-list")
(packages-list-entries (if (file-exists-p packages-list)
packages-list
(make-temp-file "packages-list" nil "packagesv2/"))))
(unless (member base-name (with-temp-buffer
(insert-file-contents packages-list-entries)
(split-string (buffer-string) "\n")))
(with-temp-buffer
(insert (mapconcat 'identity (append (with-temp-buffer
(insert-file-contents packages-list-entries)
(split-string (buffer-string) "\n"))
(list base-name)) "\n"))
(write-region (point-min) (point-max) packages-list-entries)))))
;; The following ensures that entries specified in packages-list is added to load-path
;; --------------------------------------------------
;; helper function to load package to load-path from custom_packages dir
(defun load-package (package)
"Add a package to the load-path and load its directory."
(let ((package-directory (concat "~/.emacs.d/custom_packages/" package)))
(add-to-list 'load-path package-directory)))
;; Read the packages from the file packages_list and add them to load-path
(let* ((packages-file "~/.emacs.d/custom_packages/packages-list")
(packages (with-temp-buffer
(insert-file-contents packages-file)
(split-string (buffer-string) "\n" t))))
(mapc 'load-package packages))
(provide 'packagesv2.el)