{-# LANGUAGE CPP #-}-- Copyright (c) Tuomo Valkonen 2004.-- Copyright (c) Don Stewart 2004-5.-- Copyright (c) Jean-Philippe Bernardy 2006,2007.-- | This is the main module of Yi, called with configuration from the user.-- Here we mainly process command line arguments.moduleYi.Main(main)whereimportPrelude()importControl.Monad.ErrorimportData.CharimportData.List(intercalate)importDistribution.Text(display)importSystem.Console.GetOptimportSystem.Environment(getArgs)importSystem.Exit#include "ghcconfig.h"importYi.ConfigimportYi.Config.DefaultimportYi.CoreimportYi.DiredimportPaths_yi#ifdef TESTINGimportqualifiedTestSuite#endif#ifdef FRONTEND_COCOAimportHOC(withAutoreleasePool)#endiffrontendNames::[String]frontendNames=fmapfst'availableFrontendswherefst'::(a,UIBoot)->afst'(x,_)=xdataErr=ErrStringExitCodeinstanceErrorErrwherestrMsgs=Errs(ExitFailure1)-- ----------------------------------------------------------------------- | Argument parsing. Pretty standard.dataOpts=Help|Version|LineNoString|EditorNmString|FileString|FrontendString|ConfigFileString|SelfCheck|Debug-- | List of editors for which we provide an emulation.editors::[(String,Config->Config)]editors=[("emacs",toEmacsStyleConfig),("vim",toVimStyleConfig),("cua",toCuaStyleConfig)]options::[OptDescrOpts]options=[Option[]["self-check"](NoArgSelfCheck)"Run self-checks",Option['f']["frontend"](ReqArgFrontend"FRONTEND")frontendHelp,Option['y']["config-file"](ReqArgConfigFile"PATH")"Specify a configuration file",Option['V']["version"](NoArgVersion)"Show version information",Option['h']["help"](NoArgHelp)"Show this help",Option[]["debug"](NoArgDebug)"Write debug information in a log file",Option['l']["line"](ReqArgLineNo"NUM")"Start on line number",Option[]["as"](ReqArgEditorNm"EDITOR")editorHelp]wherefrontendHelp=("Select frontend, which can be one of:\n"++intercalate", "frontendNames)editorHelp=("Start with editor keymap, where editor is one of:\n"++(intercalate", ".fmapfst)editors)-- | usage string.usage,versinfo::Stringusage=usageInfo("Usage: yi [option...] [file]")optionsversinfo="yi "++displayversion-- | Transform the config with optionsdo_args::Config->[String]->EitherErrConfigdo_argscfgargs=case(getOpt(ReturnInOrderFile)optionsargs)of(o,[],[])->foldMgetConfigcfgo(_,_,errs)->fail(concaterrs)-- | Update the default configuration based on a command-line option.getConfig::Config->Opts->EitherErrConfiggetConfigcfgopt=caseoptofFrontendf->caselookupfavailableFrontendsofJustfrontEnd->returncfg{startFrontEnd=frontEnd}Nothing->fail"Panic: frontend not found"Help->throwError$ErrusageExitSuccessVersion->throwError$ErrversinfoExitSuccessDebug->returncfg{debugMode=True}LineNol->casestartActionscfgofx:xs->returncfg{startActions=x:makeAction(gotoLn(readl)):xs}[]->fail"The `-l' option must come after a file argument"Filefilename->prependAction(fnewEfilename)EditorNmemul->caselookup(fmaptoLoweremul)editorsofJustmodifyCfg->return$modifyCfgcfgNothing->fail$"Unknown emulation: "++showemul_->returncfgwhereprependActiona=return$cfg{startActions=makeActiona:startActionscfg}-- ----------------------------------------------------------------------- | Static main. This is the front end to the statically linked-- application, and the real front end, in a sense. 'dynamic_main' calls-- this after setting preferences passed from the boot loader.--main::Config->MaybeEditor->IO()maincfgstate=do#ifdef FRONTEND_COCOAwithAutoreleasePool$do#endifargs<-getArgs#ifdef TESTINGwhen("--self-check"`elem`args)TestSuite.main#endifcasedo_argscfgargsofLeft(Errerrcode)->doputStrLnerrexitWithcodeRightfinalCfg->dowhen(debugModefinalCfg)$initDebug".yi.dbg"startEditorfinalCfgstate