(require 'package) (require 'cl) ;; load misc files (dolist (file '("elisp.el" "os.el")) (load-file (concat *lisp-dir* "/" file))) ;; load some customization ;; (defun load-customize () ;; do not load custom.el ;; because we load themes ;; (when (file-exists-p custom-file) ;; (load-file custom-file)) (let ((fonts (first-n-elem '("Inconsolata LGC" "Inconsolata" "Consolas" "Monaco") 1 #'(lambda (fnt) (member fnt (font-family-list)))))) (when fonts (set-face-attribute 'default nil :font (car fonts)))) (put 'set-goal-column 'disabled nil) (put 'narrow-to-region 'disabled nil)) ;; configure EMACS package system (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/")) (when (< emacs-major-version 24) (add-tolist 'package-archives '("gnu" . "http://elpa.gnu.org/packages/"))) ;; functions for pkg load (defvar *my-pkgs* nil "List of pkg which should be enabled") (defvar *mypkgs-dir* (concat *custom-dir* "/pkgs")) (defvar *idle-works* nil "Idle configuration") (defvar *idle-timer* "Timer for idle works") (defvar *idle-active-interval* 1.0 "Interval of idle active") (defun mypkg-enabled (pkginfo) (message "checking package %s" (cl-getf pkginfo :name)) (cl-getf pkginfo :enabled)) (defun register-mypkg (pkginfo) (when (mypkg-enabled pkginfo) (push pkginfo *my-pkgs*))) (defun mypkg-call (pkginfo tag) (let ((func (cl-getf pkginfo tag))) (when func (let ((tagname (symbol-name tag)) (pkgname (cl-getf pkginfo :name)) (before (current-time))) (progn (funcall func) (let ((after (current-time))) (message "CALL %s%s %f" pkgname tagname (float-time (time-subtract after before))))))))) (defun mypkg-do-install (pkginfo) (let ((pkgs (cl-getf pkginfo :packages))) (dolist (pkg pkgs) (progn (package-install pkg) (package-activate pkg))))) (defun mypkg-install-all () (dolist (pkginfo *my-pkgs*) (mypkg-do-install pkginfo))) ;; worker for idle time ;; when no work need to do cancel ;; idle timer (defun idle-worker () (if *idle-works* (let ((pkginfo (car *idle-works*)) (remain (cdr *idle-works*))) (mypkg-call pkginfo :idle-conf) (setq *idle-works* remain)) (progn (when *idle-timer* (cancel-timer *idle-timer*) (setq *idle-timer* nil)) t))) ;; install idle handler (defun install-idle-handler () (dolist (pkginfo *my-pkgs*) (let ((func (cl-getf pkginfo :idle-conf))) (when func (add-to-list '*idle-works* pkginfo)))) (when *idle-works* (setq *idle-timer* (run-with-idle-timer *idle-active-interval* t 'idle-worker)))) (defun my-load-file (file) (let ((disabled (cl-map 'list #'(lambda (x) (symbol-name x)) '(asy guile))) (xfile (my-replace-string (file-name-base file) "-init" ""))) (unless (member xfile disabled) (load-file file)))) ;; load pkg list (defun load-mypkgs-info () (let ((files (file-expand-wildcards (concat *mypkgs-dir* "/*.el")))) (dolist (file files) (my-load-file file)) (setq *my-pkgs* (reverse *my-pkgs*)))) ;; config packages after active (defun mypkg-after-init () (dolist (pkginfo *my-pkgs*) (mypkg-call pkginfo :config)) ;; load custom.el (load-customize) (install-idle-handler)) ;; check whether "--update-archives" is in command line ;; args (defun need-update-archives () (let ((need-update (cl-some #'(lambda (x) (string= x "--update-archives")) command-line-args))) (when need-update (setq command-line-args (cl-remove-if #'(lambda (x) (string= x '--update-archives)) command-line-args))) need-update)) ;; function to prepare to install/active ;; packages (defun mypkgs-pre-active (update-archives) (load-mypkgs-info) ;; initialize EMACS package system but ;; without active any packages (package-initialize t) ;; update package archive when option ;; --update-archives in command line ;; or there is no archive contents (when (or update-archives (not package-archive-contents)) (package-refresh-contents)) ;; prepare to install/active packages (dolist (pkginfo *my-pkgs*) (mypkg-call pkginfo :init)) ;; install packages (mypkg-install-all) ;; install functions to configure ;; package after package actived (add-hook 'after-init-hook 'mypkg-after-init)) ;; go (mypkgs-pre-active (need-update-archives))