Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 952
ldionmarcil Avatar asked Feb 07 '13 05:02

ldionmarcil


2 Answers

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...
like image 107
Trey Jackson Avatar answered Nov 05 '22 15:11

Trey Jackson


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.

like image 34
phils Avatar answered Nov 05 '22 16:11

phils