diff --git a/components/synedit/synhighlighterpas.pp b/components/synedit/synhighlighterpas.pp
index 48f139066f..ebc64ca243 100644
--- a/components/synedit/synhighlighterpas.pp
+++ b/components/synedit/synhighlighterpas.pp
@@ -1327,6 +1327,8 @@ begin
 end;
 
 function TSynPasSyn.Func37: TtkTokenKind;
+var
+  tbf: TPascalCodeFoldBlockType;
 begin
   if KeyComp('Begin') then begin
     // if we are in an include file, we may not know the state
@@ -1336,9 +1338,17 @@ begin
     if TopPascalCodeFoldBlockType in [cfbtVarType, cfbtLocalVarType] then
       EndPascalCodeFoldBlockLastLine;
     Result := tkKey;
-    if TopPascalCodeFoldBlockType in [cfbtProcedure]
-    then StartPascalCodeFoldBlock(cfbtTopBeginEnd)
-    else StartPascalCodeFoldBlock(cfbtBeginEnd);
+    tbf := TopPascalCodeFoldBlockType;
+    if tbf in [cfbtProcedure]
+    then StartPascalCodeFoldBlock(cfbtTopBeginEnd, True)
+    else StartPascalCodeFoldBlock(cfbtBeginEnd, tbf in [
+      cfbtProgram, cfbtUnit, cfbtUnitSection, cfbtPackage,
+      cfbtTopBeginEnd, cfbtBeginEnd,
+      cfbtTry, cfbtExcept, cfbtAsm,
+      cfbtCase, cfbtCaseElse,
+      // TODO: cfbtIfThen..cfbtWithDo => only if they are nested in one of the above
+      cfbtIfThen, cfbtIfElse, cfbtForDo, cfbtWhileDo, cfbtWithDo
+    ]);
     //debugln('TSynPasSyn.Func37 BEGIN ',dbgs(ord(TopPascalCodeFoldBlockType)),' LineNumber=',dbgs(fLineNumber),' ',dbgs(MinimumNestFoldBlockLevel),' ',dbgs(CurrentCodeFoldBlockLevel));
   end else
   if FExtendedKeywordsMode and KeyComp('Break') then
diff --git a/components/synedit/test/testhighlightpas.pas b/components/synedit/test/testhighlightpas.pas
index b17c74946e..e801a3bffe 100644
--- a/components/synedit/test/testhighlightpas.pas
+++ b/components/synedit/test/testhighlightpas.pas
@@ -363,6 +363,45 @@ begin
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
   {%endregion}
 
+  {%region}
+  SetLines(TestTextFoldInfo1);
+  EnableFolds([cfbtBeginEnd..cfbtNone]-[cfbtBeginEnd]);
+  PopPushBaseName('Text 1 all folds, except cfbtBeginEnd');
+
+  AssertEquals('Len Prog',  10, PasHighLighter.FoldLineLength(0,0));
+  AssertEquals('Len Proc',   7, PasHighLighter.FoldLineLength(1,0));
+  AssertEquals('Len IF A',   2, PasHighLighter.FoldLineLength(2,0));
+  AssertEquals('Len Begin',  5, PasHighLighter.FoldLineLength(3,0));
+  AssertEquals('Len if beg (not avail)', -1, PasHighLighter.FoldLineLength(5,0));
+  //AssertEquals('Len PrgBeg', 1, PasHighLighter.FoldLineLength(9,0));
+
+  AssertEquals('Len invalid', -1, PasHighLighter.FoldLineLength(4,0)); // endif
+  AssertEquals('Len // (no hide)', -1, PasHighLighter.FoldLineLength(11,0));
+
+  //                       Pg pc $I bg $E be w  e  e be  e  //
+  CheckFoldOpenCounts('', [1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]);
+  {%endregion}
+
+  {%region}
+  SetLines(TestTextFoldInfo1);
+  EnableFolds([cfbtBeginEnd..cfbtNone]-[cfbtBeginEnd,cfbtTopBeginEnd]);
+  PopPushBaseName('Text 1 all folds, except cfbtBeginEnd,cfbtTopBeginEnd');
+
+  AssertEquals('Len Prog',  10, PasHighLighter.FoldLineLength(0,0));
+  AssertEquals('Len Proc',   7, PasHighLighter.FoldLineLength(1,0));
+  AssertEquals('Len IF A',   2, PasHighLighter.FoldLineLength(2,0));
+  AssertEquals('Len Begin (not avail)',  -1, PasHighLighter.FoldLineLength(3,0));
+  AssertEquals('Len if beg (not avail)', -1, PasHighLighter.FoldLineLength(5,0));
+  //AssertEquals('Len PrgBeg', 1, PasHighLighter.FoldLineLength(9,0));
+
+  AssertEquals('Len // (no hide)', -1, PasHighLighter.FoldLineLength(11,0));
+
+  //                       Pg pc $I bg $E be w  e  e be  e  //
+  CheckFoldOpenCounts('', [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
+  {%endregion}
+
+
+
   {%region}
   SetLines(TestTextFoldInfo2);
   EnableFolds([cfbtBeginEnd..cfbtNone]-[cfbtRepeat], [cfbtSlashComment]);