1a3a3aa19a41

Improve lisp folding
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 25 Mar 2016 21:37:22 +0000
parents 153e4af91684
children 90ce3d4bf723
branches/tags (none)
files vim/ftplugin/lisp/lispfolding.vim

Changes

--- a/vim/ftplugin/lisp/lispfolding.vim	Fri Mar 25 21:36:26 2016 +0000
+++ b/vim/ftplugin/lisp/lispfolding.vim	Fri Mar 25 21:37:22 2016 +0000
@@ -7,48 +7,70 @@
 "  Automagic Lisp folding on defn's and defmacro's
 "
 
-function GetLispFold()
-      if getline(v:lnum) =~ '^\s*(defun.*\s'
-            return ">1"
-      elseif getline(v:lnum) =~ '^\s*(defmacro.*\s'
-            return ">1"
-      elseif getline(v:lnum) =~ '^\s*(defmethod.*\s'
-            return ">1"
-      elseif getline(v:lnum) =~ '^\s*(defparameter.*\s'
-            return ">1"
-      elseif getline(v:lnum) =~ '^\s*(defvar.*\s'
-            return ">1"
-      elseif getline(v:lnum) =~ '^\s*(defclass.*\s'
-            return ">1"
-      elseif getline(v:lnum) =~ '^\s*(defstruct.*\s'
-            return ">1"
-      elseif getline(v:lnum) =~ '^\s*(define-.*\s'
-            return ">1"
-      elseif getline(v:lnum) =~ '^\s*$'
-            let my_lispnum = v:lnum
-            let my_lispmax = line("$")
+function! GetLispFold()
+    let inline_fn_comment_re = '\v^ *;;( .*)?$'
 
-            while (1)
-                  let my_lispnum = my_lispnum + 1
-                  if my_lispnum > my_lispmax
-                        return "<1"
-                  endif
-
-                  let my_lispdata = getline(my_lispnum)
+    if getline(v:lnum) =~ '^;;;; .*'
+        return "0"
+    elseif getline(v:lnum) =~ '^;;;.*'
+        return "1"
+    elseif getline(v:lnum) =~ inline_fn_comment_re
+        if getline(v:lnum + 1) =~ inline_fn_comment_re
+            return "2"
+        else
+            return "<2"
+        endif
+    elseif getline(v:lnum) =~ '^; .*'
+        return "0"
+    elseif getline(v:lnum) =~ '^(defun.*\s'
+        return ">1"
+    elseif getline(v:lnum) =~ '^(test.*\s'
+        return ">1"
+    elseif getline(v:lnum) =~ '^(defmacro.*\s'
+        return ">1"
+    elseif getline(v:lnum) =~ '^(defmethod.*\s'
+        return ">1"
+    elseif getline(v:lnum) =~ '^(defgeneric.*\s'
+        return ">1"
+    elseif getline(v:lnum) =~ '^(defparameter.*\s'
+        return ">1"
+    elseif getline(v:lnum) =~ '^(defconstant.*\s'
+        return ">1"
+    elseif getline(v:lnum) =~ '^(deftype.*\s'
+        return ">1"
+    elseif getline(v:lnum) =~ '^(defvar.*\s'
+        return ">1"
+    elseif getline(v:lnum) =~ '^(defclass.*\s'
+        return ">1"
+    elseif getline(v:lnum) =~ '^(defstruct.*\s'
+        return ">1"
+    elseif getline(v:lnum) =~ '^(define-.*\s'
+        return ">1"
+    elseif getline(v:lnum) =~ '^\s*$'
+        let my_lispnum = v:lnum
+        let my_lispmax = line("$")
 
-                  " If we match an empty line, stop folding
-                  if my_lispdata =~ '^$'
-                        return "<1"
-                  else
-                        return "="
-                  endif
-            endwhile
-      else
-            return "="
-      endif
+        while (1)
+            let my_lispnum = my_lispnum + 1
+            if my_lispnum > my_lispmax
+                return "<1"
+            endif
+
+            let my_lispdata = getline(my_lispnum)
+
+            " If we match an empty line, stop folding
+            if my_lispdata =~ '^$'
+                return "<1"
+            else
+                return "="
+            endif
+        endwhile
+    else
+        return "="
+    endif
 endfunction
 
-function TurnOnLispFolding()
-      setlocal foldexpr=GetLispFold()
-      setlocal foldmethod=expr
+function! TurnOnLispFolding()
+    setlocal foldexpr=GetLispFold()
+    setlocal foldmethod=expr
 endfunction