[Question]
  [
I heard somewhere that one thing that technology cannot do yet is fold towels1. So it is now your job to prove that statement false!
Given a string as input, made up of rectangles (towels), like the following, fold each towel in half twice. For example:
```
+------+    +------+        +--+
|      |    |      |        |  |
|      |    |      |        |  |
|      | -> +------+ ->     +--+
|      |    
|      |    
|      |    
+------+    
```
Notice that when a towel is folded, it is first folded up, then from left to right. You program must mimic this behavior as well. Also notice that in the test cases, the towels stays in the same place, but folded.
**Rules:**
* Standard methods of input/output.
* Standard loopholes apply.
* Input and output should be as a string.
* Trailing whatevers are okay in output, as long as the towels are in the right place relative to each other.
* You may assume that the length of each side of the towel will always be divisible by 2.
* The towels passed as input will always be rectangular.
* The towels will always be separated-- however, they may be separated by variable amounts.
* [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest code wins!
**Test cases:**
```
Input:
+------+
|      |
|      |
|      |
|      |
|      |
|      |
+------+
Output:
    +--+
    |  |
    |  |
    +--+
Input:
+--+ +--+ +--+
|  | |  | |  |
|  | |  | |  |
+--+ +--+ +--+
Output:
  ++   ++   ++
  ++   ++   ++
Input:
+----+
|    |
|    |
|    |
|    | ++
+----+ ++
Output:
   +-+
   | |
   +-+
        +
Input:
+--+
+--+     ++
         ||
         ||
         ++
Output:
  ++
          +
          +
```
1: This has been disproved by Geobits and Laikoni. However, I did hear it somewhere.
  
[Answer]
# [Retina](https://github.com/m-ender/retina), 245 bytes
```
m1+`^((.)*(?!\+ )[+|]([- ])*[+|].*¶(?<-2>.)*)([+|][- ]*[+|])
$1$.4$* 
m+`^((.)*) ( *) (.*¶(?<-2>.)*)(?(2)(?!))\|\3\|
$1|$3|$4 $3 
m+`^((.)*)\|( *)\|(?=.*¶(?<-2>.)*(?(2)(?!)) )
$1+$.3$*-+
([+|])\1
 $1
(?!\+ )([+|])([- ])\2(\2*)\3\1
$.3$*   $1$3$1
```
[Try it online!](https://tio.run/nexus/retina#lU/LDYMwDL1nCiP5YMcCKYRjWwYhVF2ADTxXB@hiNA6ftohLfXh5sd97iecpyONO1LCnvkoCPIiONNQwsjfa@NeT@kvd3rKEyVo2LDN2GLDp0IObthQGAoODr6c2Q8WcNMWk2agYFTvA@G1Oau6M/fUn4OMHe1SwiehrceU/nIIDDG5dYOktK6SWUpsDY5YUD0BWYsQwz1KXEqdQSv8ju92YwA6mUNjheD2Il5j1D3p6gMiqMlYClhSr3IGtVM@5yBs "Retina – TIO Nexus")
Note: some lines end in spaces. Explanation:
```
m1+`                    Repeatedly search from the beginning of input
    ^((.)*              Optional indentation
      (?!\+ )           Towel does not start with a plus and a space
      [+|]([- ])*[+|]   A row of towel
      .*¶               Rest of the line
      (?<-2>.)*         Some indentation on the next line
     )
     ([+|][- ]*[+|])    Another row of towel
$1$.4$*                 Replace the second row with blanks
```
Delete every other line of every towel (this works because all towels have even height),
```
m+`             Repeatedly search
   ^((.)*)      Optional indentation
    ( *)        Find some space
    (.*¶        Rest of the line
     (?<-2>.)*) Matching indentation on the next line
    (?(2)(?!))  Ensure that the indentation exactly matches
    \|\3\|      Piece of towel
$1|$3|$4 $3     Swap the piece into the space above
```
shift all the detached towel pieces up,
```
m+`                 Repeatedly search
   ^((.)*)          Optional indentation
    \|( *)\|        Piece of towel
    (?=             Followed by
       .*¶          Rest of the line
       (?<-2>.)*    Matching indentation on the next line
       (?(2)(?!))   Ensure that the indentation exactly matches
        )           Nothing on the next line
$1+$.3$*-+          Change the piece into a towel bottom
```
and fix the bottom of the towels, effectively folding them up.
```
([+|])\1    Match a row of towel of width 2, which is just ++ or ||
 $1         Change the first column to a space, which folds it
```
Fold the towels of width 2 to the right.
```
(?!\+ )     Towel does not start with a plus and a space
([+|])      Start with a + or a |
([- ])\2    Then at least two -s or spaces
(\2*)\3     Then an even number of -s or spaces
\1          Then another + or |
$.3$*       Replace the left half with spaces
$1$3$1      Replace the first character of the right half
```
Fold the remaining towels to the right.
[Answer]
# [Octave](https://www.gnu.org/software/octave/) with [Image Package](https://octave.sourceforge.io/image/overview.html), ~~277~~ 272 bytes
```
function x=f(x)
[i,j,v]=find(bwlabel(x-32));a=@(w)accumarray(v,w,[],@max);r=-a(-i);R=a(i);s=-a(-j);S=a(j);x(:)=32;for k =1:nnz(r)
u=(r(k)+R(k)-1)/2;v=(s(k)+S(k)+1)/2;p=v:S(k);x(r(k),p)=45;x(u,p)=45;q=r(k):u;x(q,v)=124;x(q,S(k))=124;x(u,[v S(k)])=43;x(r(k),[v S(k)])=43;end
```
Input and output are 2D char arrays.
[Try it online!](https://tio.run/nexus/octave#nVHLbsIwELz7K3ypsiscVQn0EmslvgGOUVQZSCTzcIITBxfx79QORVUPXLrSPmY0O4fde@PMdtCt4Z4a8MhKLfZirKjRZgeby1Ft6iP4dJ4jSkVLuKDabt1JWau@YBQXUVZieVIepaVUQapRrkhBaP2E9yjXAYfmoUCa57JpLT9wygpjrmCROQILB5ytQkkzfM/lSNBHZh3LxHQ0FhEFk6gVHdLiIwD3M50p0oUL1FmMSFm@mMa480ROlCOPRBVW5k@nP1xtdveQz5sw5jnxkiWzdIpZwpIbn@L2//HXrJJsp/sunJ2/jDeuTeeGh9DW3UkNkHwmIhO9vtbgRXgMPoR93SmrhtY@xPGd@NK1dUOwvX8D) Or verify all test cases: [1](https://tio.run/nexus/octave#nVHLbsIwELz7K3ypsiscVQn0EmslvgGOUVQZSCTzcIITBxfx79QORVUPXLrSPmY0O4fde@PMdtCt4Z4a8MhKLfZirKjRZgeby1Ft6iP4dJ4jSkVLuKDabt1JWau@YBQXUVZieVIepaVUQapRrkhBaP2E9yjXAYfmoUCa57JpLT9wygpjrmCROQILB5ytQkkzfM/lSNBHZh3LxHQ0FhEFk6gVHdLiIwD3M50p0oUL1FmMSFm@mMa480ROlCOPRBVW5k@nP1xtdveQz5sw5jnxkiWzdIpZwpIbn@L2//HXrJJsp/sunJ2/jDeuTeeGh9DW3UkNkHwmIhO9vtbgRXgMPoR93SmrhtY@xPGd@NK1dUOwvX8D), [2](https://tio.run/nexus/octave#dVHLbsIwELz7K3ypsiscVQn0EmslvgG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Nj6Y0p0dJqOpu4scIP6KecZ7da/BcX8GnIx93Ukjh9ZM5nA8fDu1tYMf@/wB). (Note that `endfunction` in the test cases is only needed to separate the function from the subsequent code. It is not necessary if the function is saved in its own file.)
### Readable version and explanation
```
function x = f(x)
[i,j,v] = find(bwlabel(x-32)); % make background equal to 0 by subtracting 32.
% Then label each connected component (i.e. towel) with a unique integer
% Then get row indices (i) and column indices (j) of nonzero values (v)
a = @(w)accumarray(v,w,[],@max); % helper function. Gives the maximum of w for
% each group given by an integer label in v
r = -a(-i); % upper coordinate of each towel (minimum row index)
R = a(i); % lower coordinate of each towel (maximum row index)
s = -a(-j); % left coordinate of each towel (minimum column index)
S = a(j); % right coordinate of each towel (maximum column index)
x(:) = 32; % remove all towels: fill x with spaces
for k = 1:nnz(r) % for each original towel: create the new, folded towel 
    u = (r(k)+R(k)-1)/2; % new lower coordinate
    v = s(k)+S(k)+1)/2; % new left coordinate
    p = v:S(k); % column indices of horizontal edges
    x(r(k),p) = 45; % fill upper horizontal edge with '-'
    x(u,p) = 45; % fill lower horizontal edge with '-'
    q = r(k):u; % row indices of vertical edges
    x(q,v) = 124; % fill left vertical edge with '|'
    x(q,S(k)) = 124; % fill right vertical edge with '|'
    x(u,[v S(k)]) = 43; % fill lower corners with '+'
    x(r(k),[v S(k)]) = 43; % fill upper corners with '+'
end
```
]