Our Blogs Feeds:

"You can tell the ideals of a nation by its advertisements."

-Douglas

Blogs

Emacs code folding

Published by Thomas Schreiber on July 15, 2011, 9:04 a.m.

<p>This morning I setup code folding in Emacs with its bundled .. _HideShow: <a href="http://www.python.org/">http://www.python.org/</a> mode. </p> <p>I started by adding the following two functions to my emacs config. The first toggles hide/show of individual blocks and the second toggles all blocks. </p> <p>.. code-block:: common-lisp

System Message: ERROR/3 (<string>, line 6)

Unexpected indentation.
(defun toggle-hiding-block (column)

(interactive "P") (if hs-minor-mode

System Message: ERROR/3 (<string>, line 9)

Unexpected indentation.
(if (condition-case nil
(hs-toggle-hiding)

System Message: WARNING/2 (<string>, line 11)

Block quote ends without a blank line; unexpected unindent.

(error t))

System Message: WARNING/2 (<string>, line 12)

Block quote ends without a blank line; unexpected unindent.

(hs-show-all))

System Message: WARNING/2 (<string>, line 13)

Block quote ends without a blank line; unexpected unindent.

(toggle-selective-display column)))

System Message: WARNING/2 (<string>, line 14)

Block quote ends without a blank line; unexpected unindent.

</p> <p> (defvar hs-hide-state nil "Current state of hideshow for toggling all.")

System Message: ERROR/3 (<string>, line 16)

Unexpected indentation.
(defun toggle-hiding-all ()

(interactive) (setq hs-hide-state (not hs-hide-state)) (if hs-hide-state

System Message: ERROR/3 (<string>, line 20)

Unexpected indentation.
(hs-hide-all)

System Message: WARNING/2 (<string>, line 21)

Block quote ends without a blank line; unexpected unindent.

(hs-show-all)))

System Message: WARNING/2 (<string>, line 22)

Block quote ends without a blank line; unexpected unindent.

</p> <p>Next I added hooks to automatically enable HideShow minor mode for specific major modes. </p> <p>.. code-block:: common-lisp

System Message: ERROR/3 (<string>, line 26)

Unexpected indentation.
(add-hook 'css-mode-hook 'hs-minor-mode) (add-hook 'emacs-lisp-mode-hook 'hs-minor-mode) (add-hook 'html-mode-hook 'hs-minor-mode) (add-hook 'javascript-mode-hook 'hs-minor-mode) (add-hook 'lisp-mode-hook 'hs-minor-mode) (add-hook 'pony-mode-hook 'hs-minor-mode) (add-hook 'python-mode-hook 'hs-minor-mode)

System Message: WARNING/2 (<string>, line 33)

Block quote ends without a blank line; unexpected unindent.

</p> <p>Finally I added two new keybings. (M + equal-symbol) to toggle a block and (M + plus-symbol) to toggle all. I chose plus/equals because they are on the same key and also Meta over Ctrl because I prefer Meta+Shift to Ctrl+Shift as a keybinding to when combined with the equals/plus key. </p> <p> .. code-block:: common-lisp

System Message: ERROR/3 (<string>, line 37)

Unexpected indentation.
(global-set-key (kbd "M-+") 'toggle-hiding-all) (global-set-key (kbd "M-=") 'toggle-hiding-block)

System Message: WARNING/2 (<string>, line 39)

Block quote ends without a blank line; unexpected unindent.

</p>

blog comments powered by Disqus