[Question]
  [
OCTOBER 22 IS [INTERNATIONAL CAPS LOCK DAY](http://capslockday.com)! UNFORTUNATELY, SOME DO NOT RECOGNIZE THE GLORY OF THE ALMIGHTY CAPS LOCK. THEY SAY IT SEEMS "OBNOXIOUS" OR "LIKE SHOUTING" OR SOME NONSENSE. IN ORDER TO CONFORM TO THESE OBVIOUSLY ILLOGICAL AND INANE COMPLAINTS, PLEASE WRITE ME A PROGRAM THAT TURNS NORMAL TEXT INTO "SENSIBLE" OR "REASONABLE" TEXT TO MAKE PEOPLE STOP COMPLAINING.
## Description
The input and output for your solution will both be strings that contain only printable ASCII characters.
The input string will contain zero or more *caps lock runs*. A *caps lock run* (or CLR for short) is defined as the following:
* The CLR must contain no lowercase letters (`a-z`), *except* as the first character of a *word*.
	+ A *word*, for the purposes of this challenge, is a sequence of non-spaces. So, `PPCG`, `correcthorsebatterystaple`, and `jkl#@_>00()@#__f-023\f[` are all considered *word*s.
* The CLR must also contain at least one space; hence, it must be at least two *word*s.
* Each of the *word*s in the CLR must contain at least two letters (`A-Za-z`).
	+ Note that this refers to the CLR taken by itself, without any surrounding characters that may not have been included in the CLR. For example, `fo**O B**ar` is *not* a CLR because the string `O B` by itself has *word*s with less than two letters.
CLRs should be parsed "greedily"—that is, you should always find the longest CLRs possible.
Once you have identified all of the CLRs in the input string, swap the case of all letters inside of the CLRs and output the resulting string.
## Test cases
The first line is input, and the second is output. Bolded portions of the input are substrings that are considered CLRs.
```
**CAPS LOCK IS THE BEST!**
caps lock is the best!
```
```
I really **LOVE pROGRAMMING pUZZLES AND cO**de Golf!
I really love Programming Puzzles and Code Golf!
```
```
This is a challenge on PPCG. This is a test **CASE. TEST**
This is a challenge on PPCG. This is a test case. test
```
```
Lor**EM iPSUM DOL**oR sIT amet, conSECTETur ADIPISci**NG eLIT. MAECENAS iD** orci
Lorem Ipsum doloR sIT amet, conSECTETur ADIPIScing Elit. maecenas Id orci
```
```
**;'>}{/[]'"A\*(389971(\*(#$&B#@\*(% c'>#{@D#$!** :,>/;[e.[{$893F
;'>}{/[]'"a*(389971(*(#$&b#@*(% C'>#{@d#$! :,>/;[e.[{$893F
```
```
**iT'S cAPS lOCK DAY!!! cEL**ebra**TE TH**is WONDERFUL key
It's Caps Lock day!!! Celebrate this WONDERFUL key
```
```
aBc**DE fGHI**j KLm**NO pQ**rST (uVw**XY) ZZ\_\_\_Z**z__Z
aBcde Fghij KLmno PqrST (uVwxy) zz___zz__Z
```
```
#aA# aA
#aA# aA
```
## Rules
* You may assume that the input will never contain two or more spaces in a row, and that it will never contain a leading or trailing space.
* 20% bonus (multiply your code length by .8) if your entire code is a CLR. ;) (mostly just for fun, since it's unlikely that the winning submission will have this bonus)
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes wins.
  
[Answer]
# CJam, ~~100~~ ~~86~~ ~~83~~ 81 bytes
```
Ml{_,),{1$<_S/(4$!>\1f>s+_eu=*S%_{'[,_el^:Af&s,2<},!*1>},_{W=/(AA26m>er}{;(}?\s}h
```
Try [this fiddle](http://cjam.aditsu.net/#code=Ml%7B_%2C)%2C%7B1%24%3C_S%2F(4%24!%3E%5C1f%3Es%2B_eu%3D*S%25_%7B'%5B%2C_el%5E%3AAf%26s%2C2%3C%7D%2C!*1%3E%7D%2C_%7BW%3D%2F(AA26m%3Eer%7D%7B%3B(%7D%3F%5Cs%7Dh&input=I%20really%20LOVE%20pROGRAMMING%20pUZZLES%20AND%20cOde%20Golf!) in the CJam interpreter or [verify all test cases at once](http://cjam.aditsu.net/#code=qN%2F%7B%0A%20%20ML%7B_%2C)%2C%7B1%24%3C_S%2F(4%24!%3E%5C1f%3Es%2B_eu%3D*S%25_%7B'%5B%2C_el%5E%3AAf%26s%2C2%3C%7D%2C!*1%3E%7D%2C_%7BW%3D%2F(AA26m%3Eer%7D%7B%3B(%7D%3F%5Cs%7Dh%0AN%5Do%7DfL&input=CAPS%20LOCK%20IS%20THE%20BEST!%0AI%20really%20LOVE%20pROGRAMMING%20pUZZLES%20AND%20cOde%20Golf!%0AThis%20is%20a%20challenge%20on%20PPCG.%20This%20is%20a%20test%20CASE.%20TEST%0ALorEM%20iPSUM%20DOLoR%20sIT%20amet%2C%20conSECTETur%20ADIPISciNG%20eLIT.%20MAECENAS%20iD%20orci%0A%3B'%3E%7D%7B%2F%5B%5D'%22A*(389971(*(%23%24%26B%23%40*(%25%20c'%3E%23%7B%40D%23%24!%20%3A%2C%3E%2F%3B%5Be.%5B%7B%24893F%0AiT'S%20cAPS%20lOCK%20DAY!!!%20cELebraTE%20THis%20WONDERFUL%20key%0AaBcDE%20fGHIj%20KLmNO%20pQrST%20(uVwXY)%20ZZ___Zz__Z%0A%23aA%23%20aA).
### Algorithm
1. Identify the longest possible CLR that starts with the first character.
2. If it exists, swap its case, print it, and remove it from the beginning of the string.
Else, remove a single character from the beginning of the string, and print it unmodified.
3. If there are more characters left, go back to step 1.
### How it works
```
Ml         e# Push an empty string and a line from STDIN.
{          e# Do:
  _,       e#   Copy the string on the stack and compute its length (L).
  ),       e#   Push [0 ... L].
  {        e#   Filter; for each integer I in that array:
    1$<    e#     Copy the string and keep its first I characters.
    _S/    e#     Push a copy and split at spaces.
    (      e#     Shift out the first word.
    4$!    e#     Push the logical NOT of the fifth topmost item of the stack.
           e#     This pushes 1 for the empty string on the bottom, and 0
           e#     for non-empty strings and printable characters.
    >      e#     Remove that many characters from the beginning of the first word.
           e#     This will remove the first character iff the string on the
           e#     stack is the entire input. This is to account for the fact that
           e#     the first word is not preceded by a space.
    \1f>   e#     Remove the first character of all remaining words.
    s+     e#     Concatenate all of them.
    _eu=   e#     Convert a copy to uppercase and check for equality.
    *      e#     Repeat the I characters 1 or 0 times.
    S%_    e#     Split at runs of spaces, and push a copy.
    {      e#     Filter; for each non-empty word:
      '[,  e#       Push the string of all ASCII characters up to 'Z'.
      _el  e#       Push a copy and convert to lowercase.
      ^    e#       Perform symmetric difference, pushing all letters (both cases).
      :A   e#       Store the result in A.
      f&s  e#       Intersect A with each character of the word. Cast to string.
      s    e#       This removes all non-letters from the word.
      ,2<  e#       Count the letters, and compare the result to 2.
    },     e#     If there are less than 2 letters, keep the word.
    !      e#     Push the logical NOT of the result.
           e#     This pushes 1 iff all words contain enough letters.
    *      e#     Repeat the array of words that many times.
    1>     e#     Remove the first word.
  },       e#   Keep I if there are still words left.
  _{       e#   If at least one I was kept:
    W=     e#     Select the last (highest) one.
    /      e#     Split the string on the stack into chunks of that length.
    (      e#     Shift out the first chunk.
    AA26m> e#     Push "A...Za...z" and "a...zA...Z".
    er     e#     Perform transliteration to swap cases.
  }{       e#   Else:
    ;      e#     Discard the filtered array.
    (      e#     Shift out the first character of the string on the stack.
  }?       e#
  \s       e#   Swap the shifted out chunk/character with the rest of the string.
}h         e# If the remainder of the string is non-empty, repeat.
```
[Answer]
## Perl, 96 82 80 bytes
```
-pe'$y=qr/[^a-z ]{2,}|\b\S[^a-z ]+/;s#$y( $y)+#join$,,map{uc eq$_?lc:uc}$&=~/./g#eg'
```
---
Passes all tests. Assumes input from `STDIN`, prints to `STDOUT`.
How it works:
* setup a regex (`$y`) that matches
	+ at least two non-lowercase, non-whitespace characters **OR**
	+ a word boundary, followed by a non-whitespace character, followed by one or more non-lowercase, non-whitespace characters
* match multiple instances of space-separated strings that match `$y`, use `s///` to invert case
I'm sure there's room for improvement. If there is a way to get rid of the whole `join-map-split` deal there may still be a chance to qualify for the bonus :)
[Answer]
# Javascript, 193
```
decapslock =
a=>a.replace(/(^[a-z][^a-z ]+|[^a-z ]{2,})( [a-z][^a-z ]+| [^a-z ]{2,})+/g,b=>b.split` `.some(f=>f.split(/[a-z]/i).length<3)?b:b.split``.map(e=>e==(E=e.toUpperCase())?e.toLowerCase():E).join``)
```
```
<!-- Snippet UI -->
<input placeholder='sAMPLE tEXT' oninput="document.getElementsByTagName('p')[0].innerText=decapslock(this.value)" />
<p></p>
```
Explanation:
```
a=>a.replace(/* giant regex */,
  b=>
    b.split` `.some(
      f=>
        f.split(/[a-z]/i).length < 3   // check for 2+ letters
    )
      ? b                              // .some() immediately returns true if it's invalid
      : b.split``.map(                 // otherwise it's valid, so flip case
          e=>
            e == (E = e.toUpperCase()) // is it uppercase?
              ? e.toLowerCase()        // change it to LC
              : E                      // change it to UC, which was already done for
                                       // the case check
            ).join``
        )
```
```
(
^[a-z][^a-z ]+ // check for a CLR starting at the beginning with LC
|
[^a-z ]{2,}    // check for a CLR that begins in the middle of a word or starts at the
               // beginning with UC
               // in both cases, 2+ letters are required
)
(
 [a-z][^a-z ]+ // check for the next word of the CLR, starting with LC
|
 [^a-z ]{2,}   // check for the next word of the CLR, starting with UC
)+             // check for 1 or more next words
```
]