+(defun until-closed-helper (level)
+ (if
+ (re-search-backward "\\_<\\(with\\|where\\|begin\\|end\\|let\\|val\\|type\\|if\\)\\_>"
+ nil t)
+ (cond
+ ((string= (match-string 0) "end")
+ (until-closed-helper (+ level 1)))
+ ((= level 0)
+ (current-indentation))
+ ((and
+ (string= (match-string 0) "with")
+ (save-excursion
+ (backward-char)
+ (looking-at "\\s-")))
+ (until-closed-helper level))
+ (t
+ (until-closed-helper (- level 1))))
+
+ 0))
+
+(defun until-closed ()
+ (save-excursion
+ (until-closed-helper 0)))
+