== Loading multiple files ==

Here's one way to load multiple files from a specified directory.

  ;; default xemacs configuration directory
  (defconst toc:xemacs-config-dir "~/.xemacs/xemacs_configs/" "")

  ;; utility function to auto-load my package configurations
  (defun toc:load-config-file (filelist)
    (dolist (file filelist)
      (load (expand-file-name 
             (concat toc:xemacs-config-dir file)))
       (message "Loaded config file:%s" file)
       ))

  ;; load my configuration files
  (toc:load-config-file '("mswindows_config"
                          "frames_config"
                          "info_config"
                          "desktop_config"
                          "efs_config"
                          "backup_config"
                          "modemaps_config"
                         ))

This loads the files mswindows_config.el, frames_config.el… etc from the directory toc:xemacs-config-dir. I find it helps me to split my (X)Emacs config into manageable chunks. The toc: symbol prefix (my initials) is just to make sure it does not clash with anything else.

==My site start==
EraEriksson created a simple library for keeping your DotEmacs simple
by allowing you to divide it into a modular collection of files.
It's called <tt>my-site-start</tt> and can be downloaded from https://github.com/tripleee/my-site-start/

By default it will recursively load all files and directories starting from <tt>%%~/.emacs.d/site-start.d/%%</tt> but this can easily be customized to use a different directory, not recurse, etc.

The library is just a simple framework for loading a modularized set of startup files.
It does not include any functionality to actually create this modularized set of files.

As of 2009-02-05 this should be considered beta software.
The author would appreciate any comments from users;
please email ee are aye plus ee el eye es pea at eye kay eye dot ef eye.

As of 2015-02-25, please note the new download location and a new version with (gasp) one line of code changed.  I have also expanded the documentation a bit. (And yes, the startup file naming scheme was copied from Debian intentionally.)

[new:TomBreton:2010-02-19 20:11 UTC]

Works well in my experience.  It echoes Debian's design for <tt>%%/etc/emacs/%%</tt> - not sure which came first.

It's theoretically neat to be able to activate a package for personal use just by symlinking its startup file into <tt>%%~/.emacs.d/site-start.d/%%</tt>, but I don't know of any package that uses this method of installing or
which supports soft-linking correctly in its startup file.  In order to set the load-path correctly even when visited thru a soft symlink, you can write:
{{{
(if load-file-name
   (add-to-list 
      'load-path 
      (file-name-directory (file-truename load-file-name))))
}}}
For multi-file packages, there is unfortunately no convention to indicate to the user which file is the Right File to symlink.

[new:ThomasKoch:2011-06-18 15:05 UTC]

Debian already included a method like the mentioned <tt>my-site-start</tt>: It's called <tt>debian-run-directories</tt> defined in file /usr/share/emacs/site-lisp/debian-startup.el in package emacsen-common. So I put in my .emacs.d/init.el

{{{
(setq dotfiles-dir (file-name-directory (or load-file-name (buffer-file-name))))

(let ((user-site-start-dir (concat dotfiles-dir "/site-start.d")))
    (debian-run-directories user-site-start-dir))
}}}

[:RecursiveSubdirs]
== Recursive subdirs ==
The follow defun returns a list of all directories under a specific directory tree:
{{{
(defun recursive-subdirs (directory &optional withroot)
  "Return a unsorted list of names of directories in DIRECTORY recursively.
If WITHROOT is non-nil, also DIRECTORY will be include."
  (let (subdirs)
    (dolist (element (directory-files-and-attributes directory nil nil nil))
      (let* ((path (car element))
             (isdir (car (cdr element)))
             (ignore (or (string= path ".") (string= path ".."))))
        (if (and (eq isdir t) (not ignore))
            (let ((dir (concat directory "/" path)))
              (setq subdirs (append (cons dir subdirs)
                                    (recursive-subdirs dir)))))))
    (if (not (eq withroot nil))
        (add-to-list 'subdirs directory))
    subdirs))
}}}
You can use this function in your InitFile file for reading all ".el" files like in this snippet:
{{{
(dolist (dir (recursive-subdirs "~/.emacs.d/config" t))
  (dolist (file (directory-files dir t "\.el$" nil))
    (load (file-name-sans-extension file))))
}}}

[[targzeta]]

[:LoadDirectory]
== Load directory ==
A faster alternative to [[#RecursiveSubdirs]] is this function:
{{{
(defun load-directory (directory)
  "Load recursively all `.el' files in DIRECTORY."
  (dolist (element (directory-files-and-attributes directory nil nil nil))
    (let* ((path (car element))
           (fullpath (concat directory "/" path))
           (isdir (car (cdr element)))
           (ignore-dir (or (string= path ".") (string= path ".."))))
      (cond
       ((and (eq isdir t) (not ignore-dir))
        (load-directory fullpath))
       ((and (eq isdir nil) (string= (substring path -3) ".el"))
        (load (file-name-sans-extension fullpath)))))))
}}}
and within [[InitFile]]:
{{{
(load-directory "~/.emacs.d/config")
}}}


Example:
<pre>
$> tree .emacs.d/ 
.emacs.d/
|-- config/
|   |-- completion.el
|   |-- control-version.el
|   |-- external-command-whole-buffer.el
|   |-- highlight-current-line.el
|   |-- modes/
|   |   |-- flymake-mode.el
|   |   |-- markdown-mode.el
|   |   |-- po-mode.el
|   |   |-- programming/
|   |   |   |-- c-mode.el
|   |   |   |-- css-mode.el
|   |   |   |-- global.el
|   |   |   |-- js-mode.el
|   |   |   |-- perl-mode.el
|   |   |   |-- php-mode.el
|   |   |   |-- python-mode.el
|   |   |   |-- sh-mode.el
|   |   |   `-- yasnippet.el
|   |   |-- xml-mode.el
|   |   `-- yaml-mode.el
|   |-- rename-frame.el
|   |-- revert-all-buffers.el
|   |-- sh.el
|   `-- windows.el
`-- load-directory.el
</pre>
<pre>$> cat ~/.emacs
...
;;; Load all ".el" files under ~/.emacs.d/config directory.
(load "~/.emacs.d/load-directory")
(load-directory "~/.emacs.d/config")
</pre>

[[targzeta]]
----------

== Splitting customizations into separate files ==

See InitSplit for a tool to move some of your Emacs customizations (from ##M-x customize##) into separate files.

== Emacs Modular Configuration ==
Making modular your config file.
[https://github.com/targzeta/emacs-modular-configuration Info and source.]

by [[targzeta]]

See DotEmacsStructuring for other options for organizing your InitFile.

CategoryDotEmacs
