1 %  Copyright (C) 2002-2004 David Roundy
    2 %
    3 %  This program is free software; you can redistribute it and/or modify
    4 %  it under the terms of the GNU General Public License as published by
    5 %  the Free Software Foundation; either version 2, or (at your option)
    6 %  any later version.
    7 %
    8 %  This program is distributed in the hope that it will be useful,
    9 %  but WITHOUT ANY WARRANTY; without even the implied warranty of
   10 %  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11 %  GNU General Public License for more details.
   12 %
   13 %  You should have received a copy of the GNU General Public License
   14 %  along with this program; see the file COPYING.  If not, write to
   15 %  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   16 %  Boston, MA 02110-1301, USA.
   17 
   18 \darcsCommand{help}
   19 \begin{code}
   20 module Darcs.Commands.Help (
   21  help_cmd,
   22  command_control_list, environmentHelp,          -- these are for preproc.hs
   23  print_version,
   24  list_available_commands ) where
   25 
   26 import Darcs.Arguments ( DarcsFlag(..), environmentHelpSendmail )
   27 import Darcs.Commands (
   28  CommandArgs(..), CommandControl(..), DarcsCommand(..),
   29  disambiguate_commands, extract_commands, get_command_help, nodefaults, usage )
   30 import Darcs.External ( viewDoc )
   31 import Darcs.Lock ( environmentHelpTmpdir, environmentHelpKeepTmpdir )
   32 import Darcs.Patch.Match ( helpOnMatchers )
   33 import Darcs.Repository.Prefs ( binaries_file_help, environmentHelpHome )
   34 import Darcs.Utils ( withCurrentDirectory, environmentHelpEditor, environmentHelpPager )
   35 import Data.Char ( isAlphaNum, toLower )
   36 import Data.List ( groupBy )
   37 import English ( andClauses )
   38 import Printer ( text )
   39 import Ssh ( environmentHelpSsh, environmentHelpScp, environmentHelpSshPort )
   40 import System.Exit ( ExitCode(..), exitWith )
   41 import ThisVersion ( darcs_version )
   42 import URL (environmentHelpProxy, environmentHelpProxyPassword)
   43 import Workaround ( getCurrentDirectory )
   44 import qualified Darcs.TheCommands as TheCommands
   45 
   46 help_description :: String
   47 help_description = "Display help about darcs and darcs commands."
   48 
   49 help_help :: String
   50 help_help =
   51  "Without arguments, `darcs help' prints a categorized list of darcs\n" ++
   52  "commands and a short description of each one.  With an extra argument,\n" ++
   53  "`darcs help foo' prints detailed help about the darcs command foo.\n"
   54 
   55 help :: DarcsCommand
   56 help = DarcsCommand {command_name = "help",
   57                      command_help = help_help,
   58                      command_description = help_description,
   59                      command_extra_args = -1,
   60                      command_extra_arg_help = ["[<DARCS_COMMAND> [DARCS_SUBCOMMAND]]  "],
   61                      command_command = \ x y -> help_cmd x y >> exitWith ExitSuccess,
   62                      command_prereq = \_ -> return $ Right (),
   63                      command_get_arg_possibilities = return [],
   64                      command_argdefaults = nodefaults,
   65                      command_advanced_options = [],
   66                      command_basic_options = []}
   67 
   68 help_cmd :: [DarcsFlag] -> [String] -> IO ()
   69 help_cmd _ ["manpage"] = putStr $ unlines manpageLines
   70 help_cmd _ ["patterns"] = viewDoc $ text $ helpOnMatchers
   71 help_cmd _ ["environment"] = viewDoc $ text $ helpOnEnvironment
   72 help_cmd _ [] = viewDoc $ text $ usage command_control_list
   73 
   74 help_cmd _ (cmd:args) =
   75     let disambiguated = disambiguate_commands command_control_list cmd args
   76     in case disambiguated of
   77          Left err -> fail err
   78          Right (cmds,_) ->
   79              let msg = case cmds of
   80                          CommandOnly c       -> get_command_help Nothing  c
   81                          SuperCommandOnly c  -> get_command_help Nothing  c
   82                          SuperCommandSub c s -> get_command_help (Just c) s
   83              in viewDoc $ text msg
   84 
   85 list_available_commands :: IO ()
   86 list_available_commands =
   87     do here <- getCurrentDirectory
   88        is_valid <- sequence $ map
   89                    (\c-> withCurrentDirectory here $ (command_prereq c) [])
   90                    (extract_commands command_control_list)
   91        putStr $ unlines $ map (command_name . fst) $
   92                 filter (isRight.snd) $
   93                 zip (extract_commands command_control_list) is_valid
   94        putStrLn "--help"
   95        putStrLn "--version"
   96        putStrLn "--exact-version"
   97        putStrLn "--overview"
   98     where isRight (Right _) = True
   99           isRight _ = False
  100 
  101 print_version :: IO () 
  102 print_version = putStrLn $ "darcs version " ++ darcs_version
  103 
  104 -- avoiding a module import cycle between Help and TheCommands
  105 command_control_list :: [CommandControl] 
  106 command_control_list =
  107   Command_data help : TheCommands.command_control_list
  108 
  109 -- | Help on each environment variable in which Darcs is interested.
  110 environmentHelp :: [([String], [String])]
  111 environmentHelp = [
  112  -- General-purpose
  113  environmentHelpHome,
  114  environmentHelpEditor,
  115  environmentHelpPager,
  116  environmentHelpTmpdir,
  117  environmentHelpKeepTmpdir,
  118  environmentHelpSendmail,
  119  -- Remote Repositories
  120  environmentHelpSsh,
  121  environmentHelpScp,
  122  environmentHelpSshPort,
  123  environmentHelpProxy,
  124  environmentHelpProxyPassword]
  125 
  126 -- | The rendered form of the data in 'environment_help'.
  127 helpOnEnvironment :: String
  128 helpOnEnvironment =
  129     "Environment Variables\n" ++
  130     "=====================\n\n" ++
  131     unlines [andClauses ks ++ ":\n" ++
  132                      (unlines $ map ("  " ++) ds)
  133                      | (ks, ds) <- environmentHelp]
  134 
  135 -- | This module is responsible for emitting a darcs "man-page", a
  136 -- reference document used widely on Unix-like systems.  Manpages are
  137 -- primarily used as a quick reference, or "memory jogger", so the
  138 -- output should be terser than the user manual.
  139 --
  140 -- Before modifying the output, please be sure to read the man(7) and
  141 -- man-pages(7) manpages, as these respectively describe the relevant
  142 -- syntax and conventions.
  143 
  144 -- | The lines of the manpage to be printed.
  145 manpageLines :: [String]
  146 manpageLines = [
  147  ".TH DARCS 1 \"" ++ darcs_version ++ "\"",
  148  ".SH NAME",
  149  "darcs \\- an advanced revision control system",
  150  ".SH SYNOPSIS",
  151  ".B darcs", ".I command", ".RI < arguments |[ options ]>...",
  152  "",
  153  "Where the", ".I commands", "and their respective", ".I arguments", "are",
  154  "",
  155  unlines synopsis,
  156  ".SH DESCRIPTION",
  157  -- FIXME: this is copy-and-pasted from darcs.cabal, so
  158  -- it'll get out of date as people forget to maintain
  159  -- both in sync.
  160  "Darcs is a free, open source revision control",
  161  "system. It is:",
  162  ".TP 3", "\\(bu",
  163  "Distributed: Every user has access to the full",
  164  "command set, removing boundaries between server and",
  165  "client or committer and non-committers.",
  166  ".TP", "\\(bu",
  167  "Interactive: Darcs is easy to learn and efficient to",
  168  "use because it asks you questions in response to",
  169  "simple commands, giving you choices in your work",
  170  "flow. You can choose to record one change in a file,",
  171  "while ignoring another. As you update from upstream,",
  172  "you can review each patch name, even the full `diff'",
  173  "for interesting patches.",
  174  ".TP", "\\(bu",
  175  "Smart: Originally developed by physicist David",
  176  "Roundy, darcs is based on a unique algebra of",
  177  "patches.",
  178  "This smartness lets you respond to changing demands",
  179  "in ways that would otherwise not be possible. Learn",
  180  "more about spontaneous branches with darcs.",
  181  ".SH OPTIONS",
  182  "Different options are accepted by different Darcs commands.",
  183  "Each command's most important options are listed in the",
  184  ".B COMMANDS",
  185  "section.  For a full list of all options accepted by",
  186  "a particular command, run `darcs", ".I command", "--help'.",
  187  ".SS " ++ helpOnMatchers, -- FIXME: this is a kludge.
  188  ".SH COMMANDS",
  189  unlines commands,
  190  unlines environment,
  191  ".SH FILES",
  192  ".SS \"_darcs/prefs/binaries\"",
  193  unlines binaries_file_help,
  194  ".SH BUGS",
  195  "At http://bugs.darcs.net/ you can find a list of known",
  196  "bugs in Darcs.  Unknown bugs can be reported at that",
  197  "site (after creating an account) or by emailing the",
  198  "report to bugs@darcs.net.",
  199  -- ".SH EXAMPLE",
  200  -- FIXME:
  201  -- new project: init, rec -la;
  202  -- track upstream project: get, pull -a;
  203  -- contribute to project: add, rec, push/send.
  204  ".SH SEE ALSO",
  205  "A user manual is included with Darcs, in PDF and HTML",
  206  "form.  It can also be found at http://darcs.net/manual/."]
  207     where
  208       -- | A synopsis line for each command.  Uses 'foldl' because it is
  209       -- necessary to avoid blank lines from Hidden_commands, as groff
  210       -- translates them into annoying vertical padding (unlike TeX).
  211       synopsis :: [String]
  212       synopsis = foldl iter [] command_control_list
  213           where iter :: [String] -> CommandControl -> [String]
  214                 iter acc (Group_name _) = acc
  215                 iter acc (Hidden_command _) = acc
  216                 iter acc (Command_data c@SuperCommand {}) =
  217                     acc ++ concatMap
  218                             (render (command_name c ++ " "))
  219                             (extract_commands (command_sub_commands c))
  220                 iter acc (Command_data c) = acc ++ render "" c
  221                 render :: String -> DarcsCommand -> [String]
  222                 render prefix c =
  223                     [".B darcs " ++ prefix ++ command_name c] ++
  224                     (map mangle_args $ command_extra_arg_help c) ++
  225                     -- In the output, we want each command to be on its own
  226                     -- line, but we don't want blank lines between them.
  227                     -- AFAICT this can only be achieved with the .br
  228                     -- directive, which is probably a GNUism.
  229                     [".br"]
  230 
  231       -- | As 'synopsis', but make each group a subsection (.SS), and
  232       -- include the help text for each command.
  233       commands :: [String]
  234       commands = foldl iter [] command_control_list
  235           where iter :: [String] -> CommandControl -> [String]
  236                 iter acc (Group_name x) = acc ++ [".SS \"" ++ x ++ "\""]
  237                 iter acc (Hidden_command _) = acc
  238                 iter acc (Command_data c@SuperCommand {}) =
  239                     acc ++ concatMap
  240                             (render (command_name c ++ " "))
  241                             (extract_commands (command_sub_commands c))
  242                 iter acc (Command_data c) = acc ++ render "" c
  243                 render :: String -> DarcsCommand -> [String]
  244                 render prefix c =
  245                     [".B darcs " ++ prefix ++ command_name c] ++
  246                     (map mangle_args $ command_extra_arg_help c) ++
  247                     [".RS 4", command_help c, ".RE"]
  248 
  249       -- | Now I'm showing off: mangle the extra arguments of Darcs commands
  250       -- so as to use the ideal format for manpages, italic words and roman
  251       -- punctuation.
  252       mangle_args :: String -> String
  253       mangle_args s =
  254           ".RI " ++ (unwords $ map show (groupBy cmp $ map toLower $ gank s))
  255               where cmp x y = not $ xor (isAlphaNum x) (isAlphaNum y)
  256                     xor x y = (x && not y) || (y && not x)
  257                     gank (' ':'o':'r':' ':xs) = '|' : gank xs
  258                     gank (x:xs) = x : gank xs
  259                     gank [] = []
  260 
  261       environment :: [String]
  262       environment = ".SH ENVIRONMENT" : concat
  263                     [(".SS \"" ++ andClauses ks ++ "\"") : ds
  264                      | (ks, ds) <- environmentHelp]
  265 
  266 \end{code}