ldionmarcil
ldionmarcil

Reputation: 150

Collapse/expand parts of .emacs file with org-mode

I recently learned the basics of emacs' org-mode and couldn't help but imagine applying the collapse/expand concept to parts of a source file. I would like to be able to divide my .emacs file in subparts and only display headers on load, somewhat like the following:

; ERC config...

; DIRED config...

; MISC config...

Each of these would of course be headers to many lines of codes once expanded, like this:

; ERC config
(defun start-irc ()
  (interactive)
  (erc-tls :server "irc.freenode.net" :port 6697 :nick "foo"))

; DIRED config...

; MISC config...

So is this possible? How could I accomplish something like this with emacs 24.2?

Thanks!

Upvotes: 8

Views: 2852

Answers (3)

Alex Vorobiev
Alex Vorobiev

Reputation: 4359

You can also take a look at the new Outshine package which works together with outline-minor-mode to make it feel more like org-mode. In (e)lisp files outshine interprets sequences of semicolons as headers so all existing code which follows standard conventions for comments becomes foldable without any changes. Many org-mode-like key bindings (like TAB to fold/unfold heading, etc) work too.

Upvotes: 4

phils
phils

Reputation: 73344

Babel enables you to achieve exactly this (i.e. managing your init file in org-mode). Specifically, see: http://orgmode.org/worg/org-contrib/babel/intro.html#emacs-initialization

Myself, I make use of outline-minor-mode in my init file for vaguely similar purposes. Various things are treated as outline headings, but you can set outline-regexp as a file local variable to restrict that behaviour, and then you toggle things open and closed with outline-toggle-children (which you would bind to some convenient key). The toggle command works from anywhere in the section, not just on the heading.

I start the headings I want to be collapsed by default with ;;;; * and end my init file with:

;;; Local Variables:
;;; outline-regexp: ";;;; "
;;; eval:(progn (outline-minor-mode 1) (while (re-search-forward "^;;;; \\* " nil t) (outline-toggle-children)))
;;; End:

In your instance you could use:

;;; Local Variables:
;;; outline-regexp: "; "
;;; eval:(progn (outline-minor-mode 1) (hide-body))
;;; End:

Pretty similar in effect to Trey's suggestion, although I expect with folding you can trivially nest sections which I'm not accounting for (having no need to do so). I feel the outline approach leaves the file looking slightly cleaner, if that matters to you.

Upvotes: 7

Trey Jackson
Trey Jackson

Reputation: 74480

As nice as org-mode is, it does require some structure, which I don't believe can be maintained the way you want in your .emacs file.

What does work well is folding-mode. Check out the information for it on the wiki page, but basically what you do is set up comments around the chunks of code you want to put in a fold, like so:

;;{{{ some folder of some kind

(a few lines)
(of lisp)
(this "code" is just filler)

;;}}}


;;{{{ a different folder

(some more elisp code)

;;}}}

And when it is folded, it will look like:

;;{{{ some folder of some kind...

;;{{{ a different folder...

Upvotes: 8

Related Questions