News for hackers. Stuff that matters.
Re:Ask HN: Does Anyone Use Code Folding? by kazinator on Saturday May 25, 2019 @01:04AM (#20007205)
I use code folding when editing the TXR man page, because it's close to 69000 lines long. I developed some custom Vim folding rules for that. Here is what it looks like fully folded:

  +--353 lines: .\" t '\" vim:set syntax=groff:-----------------------------------------------------------------
  +--  3 lines: .SH* NAME---------------------------------------------------------------------------------------
  +--  5 lines: .SH* SYNOPSIS-----------------------------------------------------------------------------------
  +-- 54 lines: .SH* DESCRIPTION--------------------------------------------------------------------------------
  +--563 lines: .SH* ARGUMENTS AND OPTIONS----------------------------------------------------------------------
  +-- 47 lines: .SH* STATUS AND ERROR REPORTING-----------------------------------------------------------------
  +--2290 lines: .SH* BASIC TXR SYNTAX--------------------------------------------------------------------------
  +--7519 lines: .SH* DIRECTIVES--------------------------------------------------------------------------------
  +--2238 lines: .SH* TXR LISP----------------------------------------------------------------------------------
  +--47280 lines: .SH* LISP OPERATOR, FUNCTION AND MACRO REFERENCE----------------------------------------------
  +--4613 lines: .SH* FOREIGN FUNCTION INTERFACE----------------------------------------------------------------
  +--1306 lines: .SH* LISP COMPILATION--------------------------------------------------------------------------
  +--1114 lines: .SH* INTERACTIVE LISTENER----------------------------------------------------------------------
  +-- 98 lines: .SH* SETUID/SETGID OPERATION--------------------------------------------------------------------
  +--149 lines: .SH* STAND-ALONE APPLICATION SUPPORT------------------------------------------------------------
  +--  4 lines: .SH* DEBUGGER-----------------------------------------------------------------------------------
  +--852 lines: .SH* COMPATIBILITY------------------------------------------------------------------------------
  +--315 lines: .SH* APPENDIX-----------------------------------------------------------------------------------
Then if we open, say, the FOREIGN FUNCTION INTERFACE section, that looks like this:

  .SH* FOREIGN FUNCTION INTERFACE

  +--- 35 lines: On platforms where it is supported, \*(TX provides a feature called the------------------------
  +--- 21 lines: .SS* Cautionary Notes--------------------------------------------------------------------------
  +--- 79 lines: .SS* Key Concepts------------------------------------------------------------------------------
  +--- 53 lines: .SS* The FFI Type System-----------------------------------------------------------------------
  +---428 lines: .SS* Simple FFI Types--------------------------------------------------------------------------
  +---1091 lines: .SS* Parametrized FFI Type Operators----------------------------------------------------------
  +--- 12 lines: .SS* Additional Types--------------------------------------------------------------------------
  +--- 69 lines: .SS* Endian Types------------------------------------------------------------------------------
  +--- 95 lines: .SS* Bitfield Allocation Rules-----------------------------------------------------------------
  +--- 44 lines: .SS* FFI Call Descriptors----------------------------------------------------------------------
  +---491 lines: .SS* Foreign Function Type API-----------------------------------------------------------------
  +---544 lines: .SS* Foreign Function Macro Language-----------------------------------------------------------
  +---157 lines: .SS* Zero-filled Object Support----------------------------------------------------------------
  +---177 lines: .SS* Foreign Unions----------------------------------------------------------------------------
  +--- 91 lines: .SS* FFI-type-driven I/O Functions-------------------------------------------------------------
  +---227 lines: .SS* Buffer Functions--------------------------------------------------------------------------
  +---997 lines: .SS* Foreign Arrays----------------------------------------------------------------------------
The Vim setup is this:

  :au BufRead,BufNewFile txr.1            set foldmethod=expr | set foldexpr=TxrManFoldLevel(v:lnum)
  :function TxrManFoldLevel(lno)
  :  let s:line = getline(a:lno)
  :  if a:lno == 1
  :    return ">1"
  :  elseif s:line =~# '^\.SH'
  :    return ">1"
  :  elseif s:line =~# '^\.SS'
  :    return ">2"
  :  elseif s:line =~# '^\.NP' || s:line =~# '^\.co\(NP\|SS\)'
  :    return ">3"
  :  elseif s:line =~# '^\.desc' || s:line =~# '^\.TP'
  :    return ">4"
  :  elseif s:line =~# '^\...IP' || s:line =~# '^\.IP'
  :    return ">5"
  :  elseif s:line =~# '^\.TH'
  :    return "<1"
  :  elseif a:lno == 1
  :    return ">1"
  :  elseif getline(a:lno-1) == "" && s:line != ""
  :    return ">6"
  :  else
  :    return "="
  :  endif
  :endf
It has quirks, but does the job more or less.

There are no comments on this comment yet.