[Question]
  [
# Background
(Based on a true, heart-wrenching story)
In my time, I've played around with Lisp and similar languages often. I've written with them, ran them, interpreted them, designed them, and made machines write with them for me... And if there is one thing that bothers me, it's seeing Lisp that does not comply with my specific formatting style.
Unfortunately, some text editors (*cough* XCode *cough*) tend to strip my beautiful tabs and spaces whenever code is copied and pasted... Take this beautifully spaced Lisp-like syntax:
```
(A
    (B
        (C)
        (D))
    (E))
```
*(Where `ABCDE` are arbitrary functions)*
*SOME* text editors butcher this lovely code to the following end:
```
(A
(B
(C)
(D))
(E))
```
What a mess! That's not readable!
Help me out, here?
# The Challenge
Your goal in this challenge is to take a series of functions separated by newlines in a format described below and return a more beautiful arrangement that highlights readability and elegance.
# The Input
We define a function `F` of arity `N` arguments as a construct similar to the following:
```
(F (G1 ...) (G2 ...) (G3 ...) ... (GN ...))
```
where `G1, G2, ..., GN` are all functions in and of themselves. An arity `0` function `A` is simply `(A)`, while an arity `2` function `B` is of the form `(B (...) (...))`
Your code should take input as a series of functions with a single newline before every function's leading parenthesis (except for the first function). The example above is valid input.
You may assume:
* The parentheses are balanced.
* A function will never have to be indented more than 250 times.
* EVERY function is surrounded by parentheses: `()`
* A function's name will only contain printable ASCII characters.
* A function's name will never contain parentheses or spaces.
* There is an optional trailing newline on input.
# The Output
Your code should output the **same** set of functions, where the only changes made are the additions of spaces or tabs before the leading parentheses of functions.
Output should comply with the following rules:
* The first function (and later top-level functions) given should have no preceding spaces
* An argument to a function's horizontal location is exactly one tab to the right of that function's horizontal location.
* A tab is implementation-defined, but must be at least 3 spaces.
* You may optionally print a maximum of two spaces after each line.
# Rules
* This is code-golf: the shortest code wins!
* [Standard Loopholes](http://meta.codegolf.stackexchange.com/q/1061/31054) are disallowed.
# Examples
*Input:*
```
(A
(B
(C)
(D))
(E))
```
*Output:*
```
(A
    (B
        (C)
        (D))
    (E))
```
*Input:*
```
(!@#$%^&*
(asdfghjklm
(this_string_is_particularly_long
(...))
(123456789)))
(THIS_IS_TOP_LEVEL_AGAIN
(HERE'S_AN_ARGUMENT))
```
*Output:*
```
(!@#$%^&*
    (asdfghjklm
        (this_string_is_particularly_long
            (...))
        (123456789)))
(THIS_IS_TOP_LEVEL_AGAIN
    (HERE'S_AN_ARGUMENT))
```
*Input:*
```
(-:0
(*:0
(%:0
(Arg:6)
(Write:0
(Read:0
(Arg:30))
(Write:0
(Const:-6)
(Arg:10))))
(%:0
(Const:9)
(/:0
(Const:-13)
(%:0
(Arg:14)
(Arg:0)))))
(WriteArg:22
(-:0
(Const:45)
(?:0
(Arg:3)
(Arg:22)
(Arg:0)))))
```
*Output:*
```
(-:0
    (*:0
        (%:0
            (Arg:6)
            (Write:0
                (Read:0
                    (Arg:30))
                (Write:0
                    (Const:-6)
                    (Arg:10))))
        (%:0
            (Const:9)
            (/:0
                (Const:-13)
                (%:0
                    (Arg:14)
                    (Arg:0)))))
    (WriteArg:22
        (-:0
            (Const:45)
            (?:0
                (Arg:3)
                (Arg:22)
                (Arg:0)))))
```
  
[Answer]
# Pyth, 24 20 19 18 bytes
```
FN.z+*ZC9N~Z-1/N\)
```
Increments a counter for every line, counts total number of closing parentheses encountered so far, and subtracts it from the counter. Then we indent by `counter` tabs.
[Answer]
# Common Lisp - ~~486~~ 414 bytes (Rube Goldberg version)
```
(labels((p(x d)(or(when(listp x)(#2=princ #\()(p(car x)d)(incf d)(dolist(a(cdr x))(format t"~%~v{   ~}"d'(t))(p a d))(#2# #\)))(#2# x))))(let((i(make-string-input-stream(with-output-to-string(o)(#1=ignore-errors(do(b c)(())(if(member(setq c(read-char))'(#\( #\) #\  #\tab #\newline):test'char=)(progn(when b(prin1(coerce(reverse b)'string)o))(#2# c o)(setq b()))(push c b))))))))(#1#(do()(())(p(read i)0)(terpri)))))
```
# Approach
Instead of doing like everybody else and count parentheses by hand, let's invoke the Lisp reader and do it *The Right Way* :-)
* Read from input stream and write to a *temporary* output stream.
* While doing so, aggregate characters different from `(`, `)` or whitespace as strings.
* The intermediate output is used to build a string, which contains syntactically well-formed Common-Lisp forms: nested lists of strings.
* Using that string as an input stream, call the standard `read` function to build actual lists.
* Call `p` on each of those lists, which recursively write them to the standard output with the requested format. In particular, strings are printed unquoted.
As a consequence of this approach:
1. There are less restrictions on the input format: you can read arbitrarly formatted inputs, not just "one function per line" (ugh).
2. Also, if the input is not well-formed, an error will be signaled.
3. Finally, the pretty-printing function is well decoupled from parsing: you can easily switch to another way of pretty-printing S-expressions (and you should, if you value your vertical space).
# Example
Reading from a file, using this wrapper:
```
(with-open-file (*standard-input* #P"path/to/example/file")
    ...)
```
Here is the result:
```
(!@#$%^&*
    (asdfghjklm
        (this_string_is_particularly_long
            (...))
        (123456789)))
(THIS_IS_TOP_LEVEL_AGAIN
    (HERE'S_AN_ARGUMENT))
(-:0
    (*:0
        (%:0
            (Arg:6)
            (Write:0
                (Read:0
                    (Arg:30))
                (Write:0
                    (Const:-6)
                    (Arg:10))))
        (%:0
            (Const:9)
            (/:0
                (Const:-13)
                (%:0
                    (Arg:14)
                    (Arg:0)))))
    (WriteArg:22
        (-:0
            (Const:45)
            (?:0
                (Arg:3)
                (Arg:22)
                (Arg:0)))))
```
*(it seems that tabs are converted to spaces here)*
# Pretty-printed (golfed version)
Contrary to the safer original version we expect input to be valid.
```
(labels ((p (x d)
           (or
            (when (listp x)
              (princ #\()
              (p (car x) d)
              (incf d)
              (dolist (a (cdr x)) (format t "~%~v{  ~}" d '(t)) (p a d))
              (princ #\)))
            (princ x))))
  (let ((i
         (make-string-input-stream
          (with-output-to-string (o)
            (ignore-errors
             (do (b
                  c)
                 (nil)
               (if (member (setq c (read-char)) '(#\( #\) #\  #\tab #\newline)
                           :test 'char=)
                   (progn
                    (when b (prin1 (coerce (reverse b) 'string) o))
                    (princ c o)
                    (setq b nil))
                   (push c b))))))))
    (ignore-errors (do () (nil) (p (read i) 0) (terpri)))))
```
[Answer]
# [Retina](https://github.com/mbuettner/retina), ~~89~~ 83 bytes
```
s`.+
$0<tab>$0
s`(?<=<tab>.*).
<tab>
+ms`^((\()|(?<-2>\))|[^)])+^(?=\(.*^((?<-2><tab>)+))
$0$3
<tab>+$
<empty>
```
Where `<tab>` stands for an actual tab character (0x09) and `<empty>` stands for an empty line. After making those replacements, you can run the above code with the `-s` flag. However, I'm not counting that flag, because you could also just put each line in its own source file, in which case the 7 newlines would be replaced by 7 penalty bytes for the additional source files.
This is a full program, taking input on STDIN and printing the result to STDOUT.
## Explanation
Every pair of lines defines a regex substitution. The basic idea is to make use of .NET's [balancing groups](https://stackoverflow.com/a/17004406/1633117) to count the current depth up to a given `(`, and then insert that many tabs before that `(`.
```
s`.+
$0<tab>$0
```
First, we prepare the input. We can't really write back a conditional number of tabs, if we can't find them somewhere in the input string to capture them. So we start by duplicating the entire input, separated by a tab. Note that the `s`` just activates the single-line (or "dot-all") modifier, which ensures that the `.` also matches newlines.
```
s`(?<=<tab>.*).
<tab>
```
Now we turn every character after that tab into a tab as well. This gives us a sufficient amount of tabs at the end of the string, without modifying the original string so far.
```
+ms`^((\()|(?<-2>\))|[^)])+^(?=\(.*^((?<-2><tab>)+))
$0$3
```
This is the meat of the solution. The `m` and `s` activate multi-line mode (so that `^` matches the beginnings of lines) and single-line mode. The `+` tells Retina to keep repeating this substitution until the output stops changing (in this case, that means until the pattern no longer matches the string).
The pattern itself matches a prefix of the input up to an unprocessed `(` (that is, a `(` that doesn't have any tabs before it, but should). At the same time it determines the depth of the prefix with balancing groups, such that the height of stack `2` will correspond to the current depth, and therefore to number of tabs we need to append. That is this part:
```
((\()|(?<-2>\))|[^)])+
```
It either matches a `(`, pushing it onto the `2` stack, or it matches a `)`, popping the last capturing from the `2` stack, or it matches something else and leaves the stack untouched. Since the parentheses are guaranteed to be balanced we don't need to worry about trying to pop from an empty stack.
After we've gone through the string like this and found an unprocessed `(` to stop at, the lookahead then skips ahead to the end of the string, and captures tabs into group `3` while popping from the `2` stack until its empty:
```
(?=\(.*^((?<-2><tab>)+))
```
By using a `+` in there, we ensure that the pattern only matches anything if at least one tab should be inserted into the match - this avoids an infinite loop when there are multiple root-level functions.
```
<tab>+$
<empty>
```
Lastly, we just get rid of those helper tabs at the end of the string to clean up the result.
[Answer]
# C: ~~95~~ 94 characters
It's not very golfed yet, and from the question I'm unsure whether tabs are acceptable, which is what I use here.
```
i,j;main(c){for(;putchar(c=getchar()),c+1;i+=c==40,i-=c==41)if(c==10)for(j=i;j--;putchar(9));}
```
Ungolfed:
```
i,j;
main(c){
  for(
    ;
    putchar(c=getchar()),
    c+1;
    i+=c==40,
    i-=c==41
  )
    if(c==10)
      for(
        j=i;
        j--;
        putchar(9)
      );
}
```
Edit: Made it so that it quits on EOF.
[Answer]
# Julia, ~~103~~ ~~99~~ ~~97~~ ~~94~~ 88 bytes
```
p->(i=j=0;for l=split(p,"\n") i+=1;println("\t"^abs(i-j-1)*l);j+=count(i->i=='\)',l)end)
```
This defines an unnamed function that accepts a string and prints the indented version. To call it, give it a name, e.g. `f=p->...`. Note that the input must be a valid Julia string, so dollar signs (`$`) must be escaped.
Ungolfed + explanation:
```
function f(p)
    # Set counters for the line number and the number of close parens
    i = j = 0
    # Loop over each line of the program
    for l in split(p, "\n")
        # Increment the line number
        i += 1
        # Print the program line with |i-j-1| tabs
        println("\t"^abs(i-j-1) * l)
        # Count the number of close parens on this line
        j += count(i -> i == '\)', l)
    end
end
```
Example, pretending each set of four spaces is a tab:
```
julia> f("(A
(B
(C)
(D))
(E))")
(A
    (B
        (C)
        (D))
    (E))
```
Any suggestions are more than welcome!
[Answer]
# Haskell, ~~83~~ 81
```
unlines.(scanl(\n s->drop(sum[1|')'<-s])$n++['\t'|'('<-s])"">>=zipWith(++)).lines
```
a very points free solution.
[Answer]
# Perl, 41
```
$_="\t"x($i-$j).$_;$i+=y/(/(/;$j+=y/)/)/
```
`40` characters `+1` for `-p`.
Run with:
```
cat input.txt | perl -pe'$_="\t"x($i-$j).$_;$i+=y/(/(/;$j+=y/)/)/'
```
[Answer]
# Python 2 - ~~88~~ 78 Bytes
Fairly straightforward (and not very short) solution:
```
l=0
for x in raw_input().split():g=x.count;d=l*'\t'+x;l+=g("(")-g(")");print d
```
[Answer]
# CJam, 20 bytes
```
r{_')e=NU)@-:U9c*r}h
```
Try it online in the [CJam interpreter](http://cjam.aditsu.net/#code=r%7B_')e%3DNU)%40-%3AU9c*r%7Dh&input=(!%40%23%24%25%5E%26*%0A(asdfghjklm%0A(this_string_is_particularly_long%0A(...))%0A(123456789)))%0A(THIS_IS_TOP_LEVEL_AGAIN%0A(HERE'S_AN_ARGUMENT))).
### How it works
```
r                    e# Read a whitespace-separated token R from STDIN.
{                 }h e# Do, while R is truthy: 
  _')e=              e#   Push C, the number of right parentheses in R. 
       NU            e#   Push a linefeed and U (initially 0).
         )@-         e#   Compute U + 1 - C.
            :U       e#   Save in U.
              9c*    e#   Push a string of U tabulators.
                 r   e#   Read a whitespace-separated token R from STDIN.
```
]