Emacs/Code folding

From HaskellWiki
< Emacs
Revision as of 10:47, 18 May 2012 by Chrisdone (talk | contribs) (Added infobox.)
Jump to navigation Jump to search
Emacs for Haskell

Inferior Haskell processes
Automatic unit testing
Automatic building
API searching
Project navigation
Snippets
Literate programming

Folding

For folding parts of code you can use

  • hide-show.el
  • and hs-outline-level (based on py-outline-level created by Gb)

;; this gets called by outline to determine the level. Just use the length of the whitespace
 (defun hsk-outline-level ()
   (let (buffer-invisibility-spec)
     (save-excursion
       (skip-chars-forward "\t ")
       (current-column))))
;; this get called after haskell mode is enabled 
   (add-hook
    'haskell-mode-hook
    (lambda ()
       ;; outline uses this regexp to find headers. I match lines with no indent and indented
       ;; some lines, such as "--" ... "class"
        (setq outline-regexp "^[^\t ].*\\|^.*[\t ]+\\(where\\|of\\|do\\|in\\|if\\|then\\|else\\|let\\|module\\|import\\|deriving\\|instance\\|class\\)[\t\n ]")
       ;; enable our level computation
         (setq outline-level 'hsk-outline-level)
       ;; do not use their \C-c@ prefix, too hard to type. Note this overides some python mode bindings
       ;;(setq outline-minor-mode-prefix "C-c")
       ;; turn on outline mode
         (outline-minor-mode t)
       ;; initially hide all but the headers
       ;;(hide-body)
        ))

  • Also, you can use toggle-selective-display for global folding

;; folding for all rows, starting on the current column
   (defun toggle-selective-display (column)
     (interactive "P")
     (set-selective-display
      (or column
          (unless selective-display
            (1+ (current-column))))))
 (global-set-key (kbd "C-x $") 'toggle-selective-display)
   (defun toggle-hiding (column)
     (interactive "P")
     (if hs-minor-mode
         (if (condition-case nil
                 (hs-toggle-hiding)
               (error t))
             (hs-show-all))
       (toggle-selective-display column)))
 (global-set-key (kbd "C-@") 'toggle-hiding)

  • and narrowing capabilities for folding the rest of code

   (put 'narrow-to-defun 'disabled nil)
   (put 'narrow-to-page 'disabled nil)
   (put 'narrow-to-region 'disabled nil)

  • or folding-mode, if you do not mind add markers such as {-{{{-} and {-}}}-} in code.