Skip to content

Instantly share code, notes, and snippets.

@tequilasunset
Created January 3, 2011 08:11
Show Gist options
  • Save tequilasunset/763245 to your computer and use it in GitHub Desktop.
Save tequilasunset/763245 to your computer and use it in GitHub Desktop.
;;; init-file-maker.el --- Help making your init files
;;; Setup:
;; (require 'init-file-maker)
;; (setq ifm:dir "/path/to/your-init-files-dir")
;;; Usage:
;; To make init file, type M-x ifm:make RET.
;; To insert init contents, type M-x ifm:insert-contents RET.
;;; Code:
(eval-when-compile
(require 'cl))
(defvar ifm:dir "~/.emacs.d/init"
"*Directory in which init files are stored.")
(defvar ifm:contents "\
;;; `!!'.el --- my configuration for `??'.el
;; (eval-when-compile
;; (require 'cl))
\(require '`??')
`pos'
\(provide '`!!')
;;; `!!'.el ends here\n"
"*Initial contents of init files.\n
`!!': init file, `??': package, `pos': position.")
(defvar ifm:prefix "init-"
"*Prefix of init files.")
(defun ifm:package-candidates ()
(append (mapcar 'symbol-name features)
(loop for dir in load-path
if (file-directory-p dir)
append (loop for file in (directory-files dir)
if (string-match "^\\(.+\\)\\.el$" file)
collect (match-string-no-properties 1 file)))))
(defun ifm:read-package+init ()
;; Return (package-name init-file)
(let ((packages (ifm:package-candidates))
package-name)
(list (setq package-name
(completing-read "Package name: " packages))
(read-string "Init file: "
(concat ifm:prefix package-name)))))
(defun ifm:insert-contents (package-name init-file)
"Insert `ifm:contents'."
(interactive (ifm:read-package+init))
(let (mb)
(insert (replace-regexp-in-string
"`\\?\\?'" package-name
(replace-regexp-in-string
"`!!'" init-file ifm:contents)))
(when (setq mb (save-excursion
(re-search-backward "`pos'" nil t)))
(delete-region mb (match-end 0))
(goto-char mb))))
(defun ifm:make-inner (package-name init-file)
(let ((ifm:current-package package-name)
(filename (concat (expand-file-name ifm:dir) "/" init-file ".el"))
(old-init-file (locate-library (concat init-file ".el") t))
(package (locate-library (concat package-name ".el") t)))
(if old-init-file
(find-file old-init-file)
(find-file-other-window package)
(find-file filename)
(ifm:insert-contents package-name init-file))))
(defun ifm:make (package-name init-file)
"Make the init file for PACKAGE-NAME."
(interactive (ifm:read-package+init))
(ifm:make-inner package-name init-file))
(provide 'init-file-maker)
;;; init-file-maker.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment