packages feed

cgrep 6.6.25 → 6.6.30

raw patch · 28 files changed

+96/−57 lines, 28 filesdep +extra

Dependencies added: extra

Files

README.md view
@@ -7,7 +7,7 @@ Usage ----- -Cgrep 6.6.25. Usage: cgrep [OPTION] [PATTERN] files...+Cgrep 6.6.30. Usage: cgrep [OPTION] [PATTERN] files...  cgrep [OPTIONS] [ITEM] 
cgrep.cabal view
@@ -1,6 +1,6 @@ Name:                cgrep Description:         Cgrep: a context-aware grep for source codes-Version:             6.6.25+Version:             6.6.30 Synopsis:            Command line tool Homepage:            http://awgn.github.io/cgrep/ License:             GPL-2@@ -77,6 +77,7 @@                        process,                        aeson,                        yaml,-                       exceptions+                       exceptions,+                       extra   Ghc-options:         -O2 -Wall -threaded -rtsopts -with-rtsopts=-N   Default-language:    Haskell2010
src/CGrep/CGrep.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Common.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Context.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2012-2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Distance.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Filter.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2012-2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Lang.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by@@ -16,6 +16,8 @@ -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- +{-# LANGUAGE TupleSections #-}+ module CGrep.Lang ( Lang(..)                   , langMap                   , getFileLang@@ -65,7 +67,8 @@     ,  (Coffee,    [Ext "coffee"])     ,  (Conf,      [Ext "config", Ext "conf", Ext "cfg", Ext "doxy"])     ,  (Cpp,       [Ext "cpp", Ext "CPP", Ext "cxx", Ext "cc", Ext "cp", Ext "c++", Ext "tcc",-                    Ext "h", Ext "H", Ext "hpp", Ext "ipp", Ext "HPP", Ext "hxx", Ext "hh", Ext "hp", Ext "h++"])+                    Ext "h", Ext "H", Ext "hpp", Ext "ipp", Ext "HPP", Ext "hxx", Ext "hh", Ext "hp", Ext "h++",+                    Ext "cu", Ext "cuh"])     ,  (Csharp,    [Ext "cs", Ext "CS"])     ,  (Css,       [Ext "css"])     ,  (D,         [Ext "d", Ext "D"])@@ -106,7 +109,7 @@   langRevMap :: LangRevMapType-langRevMap = Map.fromList $ concatMap (\(l, xs) -> map (\x -> (x,l)) xs ) $ Map.toList langMap+langRevMap = Map.fromList $ concatMap (\(l, xs) -> map (,l) xs ) $ Map.toList langMap  -- utility functions 
src/CGrep/Output.hs view
@@ -1,4 +1,4 @@--- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Parser/Cpp/Token.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2012-2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Parser/Generic/Token.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Parser/Token.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Parser/WildCard.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Strategy/BoyerMoore.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Strategy/Cpp/Semantic.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Strategy/Cpp/Tokenizer.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Strategy/Generic/Semantic.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Strategy/Levenshtein.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Strategy/Regex.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Token.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CGrep/Types.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/CmdOptions.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by@@ -64,8 +64,9 @@           ,     no_column = False           &= help "Suppress the column number on output" &= explicit &= name "no-column"           ,     count = False               &= help "Print only a count of matching lines per file" &= explicit &= name "count"           ,     filename_only = False       &= help "Print only the name of files containing matches" &= explicit &= name "filename-only"+          ,     vim = False                 &= help "Run vim editor passing the files that match" &= explicit &=name "vim"           ,     editor = False              &= help "Run the editor specified by EDITOR var., passing the files that match" &= explicit &=name "editor"-          ,     vim = False                 &= help "Run vim the editor passing the files that match" &= explicit &=name "vim"+          ,     fileline = False            &= help "When edit option is specified, pass the list of matching files in file:line format (e.g. vim 'file-line' plugin)" &= explicit &=name "fileline" #ifdef ENABLE_HINT           ,     hint = Nothing  &= typ "STRING" &= help "Haskell interpreter output. Var: file, row, line, tokens.\ne.g. \"file ++ show (tokens)\"" &= explicit &= name "hint" #endif
src/Config.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by@@ -45,6 +45,7 @@   ,   configColors     :: Bool   ,   configColorFile  :: [SGR]   ,   configColorMatch :: [SGR]+  ,   configFileLine   :: Bool   } deriving (Show, Read)  @@ -55,6 +56,7 @@   ,   configColors      = False   ,   configColorFile   = [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid Blue]   ,   configColorMatch  = [SetConsoleIntensity BoldIntensity]+  ,   configFileLine    = False   }  @@ -65,6 +67,7 @@        configColors     = yamlColors        configColorFile  = fromMaybe [] (yamlColorFileName >>= readColor)        configColorMatch = fromMaybe [] (yamlColorMatch >>= readColor)+       configFileLine   = yamlFileLine     in Config {..}  @@ -74,6 +77,7 @@   ,   yamlColors        :: Bool   ,   yamlColorFileName :: Maybe String   ,   yamlColorMatch    :: Maybe String+  ,   yamlFileLine      :: Bool   } deriving (Show, Generic)  @@ -84,6 +88,7 @@                <*> v .:? "colors"           .!= False                <*> v .:? "color_filename"   .!= Nothing                <*> v .:? "color_match"      .!= Nothing+               <*> v .:? "file_line"        .!= False  parseJSON _ = mzero  @@ -110,7 +115,5 @@ readColor "Cyan"      =  Just [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid Cyan] readColor "White"     =  Just [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid White] readColor _           =  Nothing--  
src/Debug.hs view
@@ -1,5 +1,5 @@ ----- copyright (c) 2013 bonelli nicola <bonelli@antifork.org>+-- copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- this program is free software; you can redistribute it and/or modify -- it under the terms of the gnu general public license as published by
src/Main.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by@@ -16,10 +16,12 @@ -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- +{-# LANGUAGE RecordWildCards #-}+ module Main where  import Data.List-import Data.List.Split+import Data.List.Split (chunksOf) import qualified Data.Map as M import Data.Maybe import Data.Char@@ -45,7 +47,7 @@  import System.Console.CmdArgs import System.Directory-import System.FilePath ((</>), takeFileName)+import System.FilePath ((</>)) import System.Environment import System.PosixCompat.Files as PosixCompat import System.IO@@ -69,6 +71,7 @@ import qualified Data.ByteString.Char8 as C import qualified Codec.Binary.UTF8.String as UC +import Data.Tuple.Extra  fileFilter :: Options -> [Lang] -> FilePath -> Bool fileFilter opts langs filename = maybe False (liftA2 (||) (const $ null langs) (`elem` langs)) (getFileLang opts filename)@@ -81,7 +84,7 @@ -- push file names in Chan...  withRecursiveContents :: Options -> FilePath -> [Lang] -> [String] -> Set.Set FilePath -> ([FilePath] -> IO ()) -> IO ()-withRecursiveContents opts dir langs prunedir visited action = do+withRecursiveContents opts dir langs pdirs visited action = do     isDir <-  doesDirectoryExist dir     if isDir then do                xs <- getDirectoryContents dir@@ -105,15 +108,23 @@                -- process dirs                --                forM_ dirs $ \path -> do-                    let dirname = takeFileName path                     lstatus <- getSymbolicLinkStatus path                     when ( deference_recursive opts || not (PosixCompat.isSymbolicLink lstatus)) $-                        unless (dirname `elem` prunedir) $ do -- this is a good directory (unless already visited)!+                        unless (isPruneableDir path pdirs) $ do -- this is a good directory (unless already visited)!                             cpath <- canonicalizePath path-                            unless (cpath `Set.member` visited) $ withRecursiveContents opts path langs prunedir (Set.insert cpath visited) action+                            unless (cpath `Set.member` visited) $+                                withRecursiveContents opts path langs pdirs (Set.insert cpath visited) action              else action [dir]  +isPruneableDir:: FilePath -> [FilePath] -> Bool+isPruneableDir dir = any (`isSuffixOf` pdir)+    where pdir = mkPrunableDirName dir++mkPrunableDirName :: FilePath -> FilePath+mkPrunableDirName xs | "/" `isSuffixOf` xs = xs+                     | otherwise           = xs ++ "/"+ -- read patterns from file  readPatternsFromFile :: FilePath -> IO [C.ByteString]@@ -131,7 +142,7 @@ parallelSearch :: [FilePath] -> [C.ByteString] -> [Lang] -> (Bool, Bool) -> OptionT IO () parallelSearch paths patterns langs (isTermIn, _) = do -    (conf,opts) <- ask+    (conf@Config{..}, opts@Options{..}) <- ask      -- create Transactional Chan and Vars... @@ -141,16 +152,16 @@      -- launch worker threads... -    forM_ [1 .. jobs opts] $ \_ -> liftIO . forkIO $+    forM_ [1 .. jobs] $ \_ -> liftIO . forkIO $         void $ runExceptT . forever $ do             fs <- lift $ atomically $ readTChan in_chan             lift $                 E.catch (                     case fs of                         [] -> atomically $ writeTChan out_chan []-                        xs -> void $ (if asynch opts then flip mapConcurrently-                                                     else forM) xs $ \x -> do-                                out <- fmap (take (max_count opts)) (runReaderT (runCgrep conf opts x patterns) (conf, sanitizeOptions x opts))+                        xs -> void $ (if asynch then flip mapConcurrently+                                                else forM) xs $ \x -> do+                                out <- fmap (take max_count ) (runReaderT (runCgrep conf opts x patterns) (conf, sanitizeOptions x opts))                                 unless (null out) $ atomically $ writeTChan out_chan out)                        (\e -> let msg = show (e :: SomeException) in                             hPutStrLn stderr (showFileName conf opts (getTargetName (head fs))@@ -162,19 +173,22 @@      _ <- liftIO . forkIO $ do -        if recursive opts || deference_recursive opts-            then forM_ (if null paths then ["."] else paths) $ \p -> withRecursiveContents opts p langs (configPruneDirs conf ++ prune_dir opts) (Set.singleton p) (atomically . writeTChan in_chan)+        if recursive || deference_recursive+            then forM_ (if null paths then ["."] else paths) $ \p ->+                    withRecursiveContents opts p langs+                        (mkPrunableDirName <$> configPruneDirs ++ prune_dir) (Set.singleton p) (atomically . writeTChan in_chan)+             else forM_ (if null paths && not isTermIn then [""] else paths) (atomically . writeTChan in_chan . (:[]))          -- enqueue EOF messages: -        replicateM_ (jobs opts) ((atomically . writeTChan in_chan) [])+        replicateM_ jobs ((atomically . writeTChan in_chan) [])      -- dump output until workers are done      putPrettyHeader -    let stop = jobs opts+    let stop = jobs      matchingFiles <- liftIO $ newIORef Set.empty @@ -185,26 +199,42 @@                       [] -> action (n+1) m                       _  -> do                           case () of-                            _ | json opts -> when m $ liftIO $ putStrLn ","+                            _ | json -> when m $ liftIO $ putStrLn ","                               | otherwise -> return ()                           let out' = map (\p -> p {outTokens = map (\(off, s) -> (length $ UC.decode $ B.unpack $ C.take off $ outLine p, UC.decodeString s)) $ outTokens p}) out                           prettyOutput out' >>= mapM_ (liftIO . putStrLn)-                          liftIO $ when (vim opts || editor opts) $ mapM_ (modifyIORef matchingFiles . Set.insert . outFilePath) out+                          liftIO $ when (vim || editor) $+                                    mapM_ (modifyIORef matchingFiles . Set.insert . (outFilePath &&& outLineNo)) out                           action n True         )  0 False +     putPrettyFooter      -- run editor... -    liftIO $ when (vim opts || editor opts) $ do-        editor' <- if vim opts+    when (vim || editor ) $ liftIO $ do++        editor' <- if vim                     then return (Just "vim")                     else lookupEnv "EDITOR"-        files   <- readIORef matchingFiles-        void (runProcess (fromJust $ editor' <|> Just "vi") (Set.toList files) Nothing Nothing (Just stdin) (Just stdout) (Just stderr) >>= waitForProcess) +        files   <- Set.toList <$> readIORef matchingFiles +        let editFiles = (if fileline || configFileLine+                            then fmap (\(a,b) -> a ++ ":" ++ show b)+                            else nub . sort . fmap fst) files++        putStrLn $ "cgrep: open files " ++ unwords editFiles ++ "..."+        void $ runProcess (fromJust $ editor' <|> Just "vi")+                          editFiles+                          Nothing+                          Nothing+                          (Just stdin)+                          (Just stdout)+                          (Just stderr) >>= waitForProcess++ main :: IO () main = do @@ -263,7 +293,7 @@      -- load files to parse: -    let paths = getFilePaths (not $ null (file opts)) (others opts)+    let paths = getFilePaths (notNull (file opts)) (others opts)      -- parse cmd line language list: 
src/Options.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by@@ -73,8 +73,9 @@     ,   format              :: Maybe String     ,   json                :: Bool     ,   xml                 :: Bool-    ,   editor              :: Bool     ,   vim                 :: Bool+    ,   editor              :: Bool+    ,   fileline            :: Bool     -- Parallel:     ,   jobs                :: Int     ,   cores               :: Int
src/Reader.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by
src/Util.hs view
@@ -1,5 +1,5 @@ ----- Copyright (c) 2013 Bonelli Nicola <bonelli@antifork.org>+-- Copyright (c) 2013-2019 Nicola Bonelli <nicola@pfq.io> -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by