Evaluating integral $\int\frac{e^{\cos x}(x\sin^3x+\cos x)}{\sin^2x}dx $ $$\int\frac{e^{\cos x}(x\sin^3x+\cos x)}{\sin^2x}dx $$ 
The usual form $\int e^x(f(x)+f'(x))dx $ does not apply here. What substitution should I make ? 
 A: This is a tricky one! We spot a lot of things here that look like derivatives, so we should try reframe our expression to reflect that, and make it useful for us. Split it up as follows:
$$[e^{\cos x}\sin x]x+e^{\cos x}[\frac{\cos x}{\sin^2 x}]$$
Let's integrate by parts here, setting $u'=e^{\cos x}\sin x, \frac{\cos x}{\sin^2 x}$ and $v=x, e^{\cos x}$ respectively. Noting that $\int u'v=uv-\int uv'$, we see:
$$\int [e^{\cos x}\sin x]xdx=(-e^{\cos x})x-\int[-e^{\cos x}]dx$$
$$\int e^{\cos x}[\frac{\cos x}{\sin^2 x}]dx=(e^{\cos x})(\frac{-1}{\sin x})-\int[-e^{\cos x}\sin x](\frac{-1}{\sin x})$$
We then notice that the extra integrals we pick up cancel each other out perfectly, leaving us with:
$$-(x+\frac{1}{\sin x})e^{\cos x}+C$$
A: The integral is of the form 
\begin{equation*}
\int \left( x\sin x+\frac{\cos x}{\sin ^{2}x}\right) e^{\cos x}dx=\int
h(x)e^{g(x)}dx.
\end{equation*}
This form recalls the well-known formula
\begin{equation*}
\int \left( f^{\prime }(x)+g^{\prime }(x)f(x)\right)
e^{g(x)}dx=f(x)e^{g(x)}+C.
\end{equation*}
Its proof maybe found at
Proof for formula $\int e^{g(x)}[f'(x) + g'(x)f(x)] dx = f(x) e^{g(x)}+C$
So we are done if we find a function $f(x)$ such that
\begin{equation*}
h(x)=f^{\prime }(x)+g^{\prime }(x)f(x).
\end{equation*}
In what follows, I will show that $f(x)=-x-\csc x,$ and therefore
\begin{equation*}
\int \left( x\sin x+\frac{\cos x}{\sin ^{2}x}\right) e^{\cos x}dx=\left(
-x-\csc x\right) e^{\cos x}+C.
\end{equation*}
$\color{red}{\bf Problem:}$ We want to write $x\sin x+\frac{\cos x}{\sin ^{2}x}$ as $
f^{\prime }(x)+g^{\prime }(x)f(x)$ where $g(x)=\cos x,$ $g^{\prime
}(x)=-\sin x$ and $f(x)$ is to be determined. 
First, it is easy to see that 
\begin{equation*}
x\sin x+\frac{\cos x}{\sin ^{2}x}=\frac{\cos x}{\sin ^{2}x}+(-\sin x)(-x)=
\frac{\cos x}{\sin ^{2}x}+g^{\prime }(x)(-x).
\end{equation*}
If we put $f_{1}(x)=(-x),$ then 
\begin{equation*}
f_{1}^{\prime }(x)+g^{\prime }(x)f_{1}(x)=\left( -1\right) +(-\sin x)\left(
-x\right) =-1+x\sin x
\end{equation*}
This suggests to add and to subtract the term $-1$ as follows
\begin{equation*}
x\sin x+\frac{\cos x}{\sin ^{2}x}=x\sin x+\frac{\cos x}{\sin ^{2}x}%
-1+1=\left( -1+x\sin x\right) +\left( 1+\frac{\cos x}{\sin ^{2}x}\right) ,
\end{equation*}
Now let us find $f_{2}(x)$ such that : 
\begin{equation*}
\left( 1+\frac{\cos x}{\sin ^{2}x}\right) =f_{2}^{\prime }(x)+g^{\prime
}(x)f_{2}(x)=f_{2}^{\prime }(x)-(\sin x)f_{2}(x).
\end{equation*}
It is easy to see that 
\begin{equation*}
\frac{\cos x}{\sin ^{2}x}+1=\frac{\cos x}{\sin ^{2}x}+(-\sin x)\left( \frac{
-1}{\sin x}\right) =\frac{\cos x}{\sin ^{2}x}+g^{\prime }(x)\left( \frac{-1}{
\sin x}\right) 
\end{equation*}
Then if we put $f_{2}(x)=\left( \frac{-1}{\sin x}\right) $ it follows that
\begin{equation*}
f_{2}^{\prime }(x)+g^{\prime }(x)f_{2}(x)=\left( \frac{-1}{\sin x}\right)
^{\prime }-(\sin x)\left( \frac{-1}{\sin x}\right) =\frac{\cos x}{\sin ^{2}x}
+1.
\end{equation*}
It follows that
\begin{eqnarray*}
\left( x\sin x+\frac{\cos x}{\sin ^{2}x}\right)  &=&\left( -1+x\sin x\right)
+\left( \frac{\cos x}{\sin ^{2}x}+1\right)  \\
&& \\
&=&\left( f_{1}^{\prime }(x)+g^{\prime }(x)f_{1}(x)\right) +\left(
f_{2}^{\prime }(x)+g^{\prime }(x)f_{2}(x)\right)  \\
&& \\
&=&\left( (f_{1}(x)+f_{2}(x))^{\prime }+g^{\prime
}(x)(f_{1}(x)+f_{2}(x))\right) 
\end{eqnarray*}
then, it suffices to take 
\begin{equation*}
f(x)=f_{1}(x)+f_{2}(x)=-x-\frac{1}{\sin x}=-x-\csc x.\ \ \ 
\color{red}
\blacksquare 
\end{equation*}
