David shows that in his trouble all his comfort was in prayer to God, 1-7.
1 [Maschil of David: a prayer when he was in the cave.] I cried to the LORD with my voice. With my voice to the LORD I made my supplication.
2 I poured out my complaint before him. I uttered before him my trouble.
3 When my spirit was overwhelmed within me, you knew my path. In the way in which I walked they have secretly laid a snare for me.
4 I looked on my right hand and beheld, but there was no man who would know me. Refuge failed me. No man cared for my soul.
5 I cried to you, O LORD. I said, "You are my refuge and my portion in the land of the living."
6 Attend to my cry, for I am brought very low. Deliver me from my persecutors, for they are stronger than I am.
7 Bring my soul out of prison so that I may praise your name. The righteous shall encompass me, for you will deal bountifully with me.
Matthew Henry Commentary - Psalms, Chapter 142[➚]
John Calvin's Chapter Summary:
When Saul came into the cave where David lay concealed, this saint of God might upon such an occurrence have been either thrown into consternation, or led by his alarm into some unwarrantable step, it being common for persons in despair either to be prostrated with dismay, or driven into frenzy. But it appears from this Psalm that David retained his composure, relying with assured confidence upon God, and resigning himself to vows and prayers instead of taking any unauthorized steps.