From f6520a5f8d406c6cf3097c8536ab3b0961f9a9c9 Mon Sep 17 00:00:00 2001
From: Artem Pelenitsyn <a.pelenitsyn@gmail.com>
Date: Sat, 20 Aug 2022 12:07:35 -0400
Subject: [PATCH] cabal man handles $PAGER with more than one word now (fix
 #8405)

---
 cabal-install/src/Distribution/Client/Manpage.hs | 12 +++++++-----
 changelog.d/issue-8405                           | 14 ++++++++++++++
 2 files changed, 21 insertions(+), 5 deletions(-)
 create mode 100644 changelog.d/issue-8405

diff --git a/cabal-install/src/Distribution/Client/Manpage.hs b/cabal-install/src/Distribution/Client/Manpage.hs
index f93c711753..19e80ba9f5 100644
--- a/cabal-install/src/Distribution/Client/Manpage.hs
+++ b/cabal-install/src/Distribution/Client/Manpage.hs
@@ -31,10 +31,10 @@ import Distribution.Compat.Process      (proc)
 import Distribution.Simple.Command
 import Distribution.Simple.Flag         (fromFlag, fromFlagOrDefault)
 import Distribution.Simple.Utils
-  ( IOData(..), IODataMode(..), ignoreSigPipe, rawSystemStdInOut, rawSystemProcAction, fromCreatePipe )
+  ( IOData(..), IODataMode(..), ignoreSigPipe, rawSystemStdInOut, rawSystemProcAction,
+    fromCreatePipe, die' )
 import System.IO                        (hClose, hPutStr)
 import System.Environment               (lookupEnv)
-import System.FilePath                  (takeFileName)
 import qualified System.Process as Process
 
 data FileInfo = FileInfo String String -- ^ path, description
@@ -78,9 +78,11 @@ manpageCmd pname commands flags
 
         unless (ec1 == ExitSuccess) $ exitWith ec1
 
-        pager <- fromMaybe "less" <$> lookupEnv "PAGER"
-        -- 'less' is borked with color sequences otherwise
-        let pagerArgs = if takeFileName pager == "less" then ["-R"] else []
+        pagerAndArgs <- fromMaybe "less -R" <$> lookupEnv "PAGER"
+        -- 'less' is borked with color sequences otherwise, hence -R
+        (pager, pagerArgs) <- case words pagerAndArgs of
+          []     -> die' verbosity "man: empty value of the PAGER environment variable"
+          (p:pa) -> pure (p, pa)
         -- Pipe output of @nroff@ into @less@
         (ec2, _) <- rawSystemProcAction verbosity
             (proc pager pagerArgs) { Process.std_in = Process.CreatePipe }
diff --git a/changelog.d/issue-8405 b/changelog.d/issue-8405
new file mode 100644
index 0000000000..5ed2759fec
--- /dev/null
+++ b/changelog.d/issue-8405
@@ -0,0 +1,14 @@
+synopsis: `cabal man` handles $PAGER containing arguments now
+packages: cabal-install
+prs: #8353
+issues: #8405
+
+description: {
+
+Things like `PAGER="less -FX" cabal man` work now.
+
+There's a slight change in the default behavior. We still use `less -R` as the default,
+but if the user passes `PAGER=less`, we won't add -R to that, unlike before. This is
+reasonable, as the user should be able to set the variable as they see fit.
+
+}
-- 
GitLab