Difference between revisions of "Emacs/Code folding"

From HaskellWiki
Jump to navigation Jump to search
m (Added infobox.)
m (To be deleted if no new content appears...)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
[[Category:Emacs|*]]
+
[[Category:Pages to be removed]]
{{Haskell infobox}}
 
 
=== Folding ===
 
For folding parts of code you can use
 
*'''hide-show.el'''
 
*and '''hs-outline-level''' (based on py-outline-level created by Gb)
 
 
<code>
 
;; 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)
 
))
 
</code>
 
 
*Also, you can use '''toggle-selective-display''' for global folding
 
 
<code>
 
;; 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)
 
</code>
 
 
*and '''narrowing capabilities''' for folding the rest of code
 
<code>
 
(put 'narrow-to-defun 'disabled nil)
 
(put 'narrow-to-page 'disabled nil)
 
(put 'narrow-to-region 'disabled nil)
 
</code>
 
 
*or '''folding-mode''', if you do not mind add markers such as '''{-{{{-}''' and '''{-}}}-}''' in code.
 

Latest revision as of 12:16, 19 April 2021