@if-using not(REGIONS)
  @use (REGIONS)
  {{{  libs
  {{{F fun/go-line
  :::F fun/go-line
  }}}
  }}}
  {{{  vars
  ( defvar
     ( start-x        ; start of defined block
       start-y        ;           "
       end-x          ; end of defined block
       end-y          ;           "
       folderr        ; return: could the folding be done?
       end-splitted   ; return: was the last line of the block splitted?
       start-splitted ; return: was the first line of the block splitted?
     )
  )
  }}}
  {{{  set-start-mark
  ( deffun set-start-mark
     ( set-counter start-y store-line
       set-counter start-x store-pos
     )
  )
  }}}
  {{{  go-start-mark
  ( deffun go-start-mark
     ( set-counter go-line-arg start-y
       go-line
       goto-counter start-x
       redraw-display
     )
  )
  }}}
  {{{  set-end-mark
  ( deffun set-end-mark
     ( set-counter end-y store-line
       set-counter end-x store-pos
     )
  )
  }}}
  {{{  clear-marks
  ( deffun clear-marks
     ( set-counter start-x 0
       set-counter start-y 0
       set-counter end-x 0
       set-counter end-y 0
     )
  )
  }}}
  {{{  change-mark-counters
  ( defvar ( dummy ) )
  ( deffun change-mark-counters
     (
       {{{  change x
       set-counter dummy end-x
       set-counter end-x start-x
       set-counter start-x dummy
       }}}
       {{{  change y
       set-counter dummy end-y
       set-counter end-y start-y
       set-counter start-y dummy
       }}}
     )
  )
  ( undeclare ( dummy ) )
  }}}
  {{{  fold-region
  {{{  vars
  ( defvar
     ( entered        ; help, number of minimum leading spaces in given block
       bool           ; help
       y              ; help
       cy             ; help
     )
  )
  }}}
  {{{  mini-x
  ( deffun mini-x
     ( goto-counter 1
       if not(test-end-line)
        ( while test-char "  ( forward-character )
          set-counter entered -(cy store-pos)
          if and(not(test-end-line),counter>0 entered)
           ( set-counter cy store-pos )
          fi
        )
       fi
     )
  )
  }}}
  (deffun fold-region
    ( set-counter folderr 1
      {{{  start-y = start, end-y = end, cy=start, y=0, entered=0
      set-counter entered 0
      {{{  maybe change end/start-positions
      case
       {{{  start under end -> change
       ( pre
          ( set-counter y -(start-y end-y) )
            counter>0 y
         ( change-mark-counters )
       )
       }}}
       {{{  one line, start after end
       ( and ( counter-0 y,
               pre (set-counter y -(start-x end-x) ) counter>0 y
             )
         ( change-mark-counters )
       )
       }}}
      esac
      }}}
      {{{  correct marks given?
      if not(and(start-x start-y end-x end-y)) ( return-from-macro ) fi
      }}}
      set-counter y 0
      set-counter cy start-y
      }}}
      @if-using (SCR-OFF) screen-off @fi
      {{{  go from start to end and count steps
      {{{  go-line(start-y)
      set-counter go-line-arg start-y
      go-line
      }}}
      while pre ( set-counter bool -(cy end-y) ) not(counter-0 bool)
       {{{  move one step and update counters
       ( if counter>0 bool
          {{{  cannot fold
          ( @if-using (SCR-OFF)
              screen-on
              redraw-display
            @fi
            return-from-macro
          )
          }}}
         fi
         set-counter y +(y 1)
         case
          {{{  handle fold
          (or(test-fold-line,test-filed)
           ( next-line set-counter cy store-line )
          )
          }}}
          {{{  handle fold-open
          (test-begin-fold
           ( set-counter entered +(entered 1)
             next-line
             set-counter cy +(cy 1)
           )
          )
          }}}
          {{{  handle fold-end
          (test-end-fold
            ( if counter-0 entered
               {{{  cannot fold
               ( @if-using (SCR-OFF)
                   screen-on
                   redraw-display
                 @fi
                 return-from-macro
               )
               }}}
              fi
              set-counter entered +(entered -1)
              next-line
              set-counter cy +(cy 1)
            )
          )
          }}}
         default
          {{{  step
          ( next-line set-counter cy +(cy 1) )
          }}}
         esac
       )
       }}}
      if or(and(    test-end-fold  not(counter-0 -(1 entered)))
            and(not(test-end-fold) not(counter-0 entered)))
       {{{  cannot fold
       ( @if-using (SCR-OFF)
           screen-on
           redraw-display
         @fi
         return-from-macro
       )
       }}}
      fi
      }}}
      {{{  maybe splitt last line of the marked region
      goto-counter end-x
      forward-character
      if or(not(test-text),test-end-line,pre ( "$ ) last-message M_ERR_PO)
       {{{  no splitt
       ( set-counter end-splitted 0 )
       }}}
      else
       {{{  do the splitt
       ( set-counter end-splitted 1
         backward-character
         newline-and-indent
         newline-and-indent
         previous-line
         beginning-of-line
         do
          ( delete-character )
         while not(test-char "$)
         delete-character
         previous-line
       )
       }}}
      fi
      }}}
      {{{  move back to start, set cy to minimum x-position, entered becomes invalid
      goto-counter end-x
      set-counter cy store-pos
      mini-x
      local (y)
       ( while not(counter-0 y)
          ( previous-line
            set-counter y +(y -1)
            mini-x
          )
       )
      }}}
      {{{  maybe splitt first line of the marked region
      if and(test-text,
             pre
              ( beginning-of-line
                set-counter start-splitted -(start-x store-pos)
                goto-counter start-x
              )
             counter>0 start-splitted
            )
       {{{  do the splitt
       ( set-counter start-splitted 1
         "$
         backward-character
         newline-and-indent
         newline-and-indent
         previous-line
         beginning-of-line
         do
          ( delete-character )
         while not(test-char "$)
         delete-character
         mini-x
       )
       }}}
      else
       {{{  no splitt
       ( set-counter start-splitted 0 )
       }}}
      fi
      }}}
      {{{  do the fold
      goto-counter cy
      delete-mode-view
      create-fold
      while counter>0 y ( next-line set-counter y +(y -1))
      next-line
      create-fold
      }}}
      @if-using (SCR-OFF) screen-on redraw-display @fi
      set-counter folderr 0
    )
  )
  {{{  undeclare
  ( undeclare ( mini-x entered bool y cy ) )
  }}}
  }}}
  {{{  combine-splitted-parts-start
  ( deffun combine-splitted-parts-start
     (
       {{{  go-line start-y
       set-counter go-line-arg start-y
       go-line
       }}}
       {{{  generate $ at real start of line
       end-of-line
       newline-and-indent
       beginning-of-line
       next-line
       "$
       }}}
       {{{  move to start-x/y
       previous-line
       previous-line
       goto-counter start-x
       "$
       }}}
       {{{  remove to $
       do
        ( delete-character )
       while test-char "   ;
       }}}
       delete-character
       delete-previous-character
     )
  )
  }}}
  {{{  combine-splitted-parts-end
  ( deffun combine-splitted-parts-end
     (
       {{{  go-line end-y
       set-counter go-line-arg end-y
       go-line
       }}}
       if and(test-text,pre ( next-line ) test-text )
        ( previous-line
          {{{  generate $ at real start of line
          end-of-line
          newline-and-indent
          beginning-of-line
          next-line
          "$
          }}}
          {{{  move to end-x/y
          previous-line
          previous-line
          goto-counter end-x
          forward-character
          "$
          }}}
          {{{  remove to $
          do
           ( delete-character )
          while test-char "   ;
          }}}
          delete-character
          delete-previous-character
        )
       else
        ( if test-end-fold (close-fold) fi )
       fi
     )
  )
  }}}
  {{{  indent-region
  {{{  comment
  ; try to indent a region +x spaces
  ; x-positions are set to beginning of first and end of last line
  ; x=0: the marked lines are folded. Start- and end-line are NOT splitted and
  ; are moved complete in the fold!
  }}}
  ( deffun indent-region
     ( local
        ( x )
        ( @if-using (SCR-OFF)
             screen-off
          @fi
          if counter>0 -(start-y store-line) ( change-mark-counters ) fi
          set-counter end-x 32000
          set-counter start-x 1
          fold-region
          if counter-0 folderr
           ( beginning-of-line
             while counter>0 x
              ( "  set-counter x +(x -1) )
             while not(counter-0 x)
              ( delete-previous-character set-counter x +(x 1) )
             unfold-fold
           )
          fi
          @if-using (SCR-OFF)
            screen-on
            redraw-display
          @fi
        )
     )
  )
  }}}
@fi
