<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
- (defun toggle-hiding-block (column)
(interactive "P") (if hs-minor-mode
- (if (condition-case nil
(hs-toggle-hiding)(error t))
(hs-show-all))
(toggle-selective-display column)))
</p> <p> (defvar hs-hide-state nil "Current state of hideshow for toggling all.")
- (defun toggle-hiding-all ()
(interactive) (setq hs-hide-state (not hs-hide-state)) (if hs-hide-state
(hs-hide-all)(hs-show-all)))
</p> <p>Next I added hooks to automatically enable HideShow minor mode for specific major modes. </p> <p>.. code-block:: common-lisp
(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)
</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
(global-set-key (kbd "M-+") 'toggle-hiding-all) (global-set-key (kbd "M-=") 'toggle-hiding-block)
</p>