Code folding for emacs?

Just add the following to your .emacs file:

(global-set-key (kbd "<C-f5>") 'set-selective-display-dlw)

(defun set-selective-display-dlw (&optional level)
"Fold text indented same of more than the cursor.
If level is set, set the indent level to LEVEL.
If 'selective-display' is already set to LEVEL, clicking
F5 again will unset 'selective-display' by setting it to 0."
  (interactive "P")
  (if (eq selective-display (1+ (current-column)))
      (set-selective-display 0)
    (set-selective-display (or level (1+ (current-column))))))

Then put the cursor at the beginning of a block (any block: functions, loops, etc…) and press Ctrl+F5 to toggle folding at that level.

Further info here: elisp - How to achieve code folding effects in Emacs? - Stack Overflow

4 Likes