Simplify history retrieval code by moving query building and running into a common helper function.
Simplify history retrieval code by moving query building and running into a common helper function.

ipython/ipython,ipython/ipython