Skip to content
This repository
Browse code

Implement two shorthands that Emacs allows in file prompt

minibuffers, which I find very useful: "somedir//someotherdir" is
taken to mean "/someotherdir", "somedir/~/someotherdir" is taken to
mean "~/someotherdir".
  • Loading branch information...
commit a1807e95beccfb90a18b64a6042222f768679e94 1 parent 6ba5bb1
Dominique Devriese dominiquedevriese authored
18 yi/src/library/System/CanonicalizePath.hs
@@ -4,6 +4,7 @@
4 4 module System.CanonicalizePath
5 5 ( canonicalizePath
6 6 , normalisePath
  7 + , replaceShorthands
7 8 ) where
8 9
9 10 #ifdef mingw32_HOST_OS
@@ -12,8 +13,8 @@ import qualified System.Win32 as Win32
12 13
13 14 import Control.Applicative
14 15 import Control.Monad
15   -import Data.List.Split (splitOn)
16   -import System.FilePath ((</>), isAbsolute, takeDirectory, pathSeparator)
  16 +import Data.List.Split (splitOn, splitOneOf)
  17 +import System.FilePath ((</>), isAbsolute, takeDirectory, pathSeparator, pathSeparators)
17 18 import System.Directory (getCurrentDirectory)
18 19 import System.Posix.Files (readSymbolicLink)
19 20
@@ -61,6 +62,17 @@ combinePath x "." = x
61 62 combinePath x ".." = takeDirectory x
62 63 combinePath x y = x </> y
63 64
  65 +replaceInit :: [a] -> [[a]] -> [a]
  66 +replaceInit init [] = []
  67 +replaceInit init [a] = a
  68 +replaceInit init (a:as) = init ++ last as
  69 +
  70 +-- replace utility shorthands, similar to Emacs
  71 +-- somepath//someotherpath is equivalent to /someotherpath
  72 +-- somepath/~/someotherpath is equivalent to ~/someotherpath
  73 +replaceShorthands :: FilePath -> FilePath
  74 +replaceShorthands = replaceInit "~" . splitOn "/~" . replaceInit "/" . splitOn "//"
  75 +
64 76 -- | Splits path into parts by path separator
65 77 splitPath :: FilePath -> [String]
66   -splitPath = filter (not . null) . splitOn [pathSeparator]
  78 +splitPath = filter (not . null) . splitOneOf pathSeparators
10 yi/src/library/Yi/Misc.hs
@@ -38,7 +38,7 @@ import Yi.MiniBuffer
38 38 ( simpleComplete
39 39 , withMinibufferGen
40 40 )
41   -import System.CanonicalizePath (canonicalizePath)
  41 +import System.CanonicalizePath (canonicalizePath, replaceShorthands)
42 42
43 43 -- | Given a possible starting path (which if not given defaults to
44 44 -- the current directory) and a fragment of a path we find all
@@ -49,12 +49,13 @@ import System.CanonicalizePath (canonicalizePath)
49 49 -- we return all of the filenames then we get a 'hint' which is way too
50 50 -- long to be particularly useful.
51 51 getAppropriateFiles :: Maybe String -> String -> YiM (String, [ String ])
52   -getAppropriateFiles start s = do
  52 +getAppropriateFiles start s' = do
53 53 curDir <- case start of
54 54 Nothing -> do bufferPath <- withBuffer $ gets file
55 55 liftIO $ getFolder bufferPath
56 56 (Just path) -> return path
57   - let sDir = if hasTrailingPathSeparator s then s else takeDirectory s
  57 + let s = replaceShorthands s'
  58 + sDir = if hasTrailingPathSeparator s then s else takeDirectory s
58 59 searchDir = if null sDir then curDir
59 60 else if isAbsolute' sDir then sDir
60 61 else curDir </> sDir
@@ -103,7 +104,8 @@ promptFile :: String -> (String -> YiM ()) -> YiM ()
103 104 promptFile prompt act = do maybePath <- withBuffer $ gets file
104 105 startPath <- addTrailingPathSeparator <$> (liftIO $ canonicalizePath =<< getFolder maybePath)
105 106 -- TODO: Just call withMinibuffer
106   - withMinibufferGen startPath (findFileHint startPath) prompt (simpleComplete $ matchingFileNames (Just startPath)) act
  107 + withMinibufferGen startPath (findFileHint startPath) prompt (simpleComplete $ matchingFileNames (Just startPath))
  108 + (act . replaceShorthands)
107 109
108 110
109 111 -- | For use as the hint when opening a file using the minibuffer.

0 comments on commit a1807e9

Please sign in to comment.
Something went wrong with that request. Please try again.