24

Haskeline makes it very easy to get filename tab-completion functionality:

module Main where

import System.Console.Haskeline
import System.Environment

mySettings :: Settings IO
mySettings = defaultSettings {historyFile = Just "myhist"}

main :: IO ()
main = do
        args <- getArgs
        let inputFunc = getInputLine
        runInputT mySettings $ withInterrupt $ loop inputFunc 0
    where
        loop inputFunc n = do
            minput <-  handleInterrupt (return (Just "Caught interrupted"))
                        $ inputFunc (show n ++ ":")
            case minput of
                Nothing -> return ()
                Just s -> do
                            outputStrLn ("line " ++ show n ++ ":" ++ s)
                            loop inputFunc (n+1)

It also provides functions like completeWord and completeQuotedWord, which should be able to be used in the same way that completeFilename is used to make the above functionality.
(In other words, have tab-completion based on a list of words (say, function names), instead of based on the contents of a folder)

Can anyone provide a working example - or working code - of this?

Recommendations for functions from other packages (like HCL) are helpful also.

amindfv
  • 8,438
  • 5
  • 36
  • 58
  • 1
    I've never used these functions, but if you're ever looking for example code with a certain function or module, you can always try [Google Code Search](http://www.google.com/codesearch). It's not always helpful, but it'll often give you some great examples in a variety of contexts. – Jeff Burka May 27 '11 at 02:28
  • I didn't find any references for the functions, but thanks for the suggestion. – amindfv May 27 '11 at 06:11
  • Offering 62% of my reputation for an answer to this question :) – amindfv May 29 '11 at 03:50

1 Answers1

24

Is this the sort of thing you're after?

import Data.List

wordList = [ "Apple", "Pear", "Peach", "Grape", "Grapefruit", "Slime Mold"]

searchFunc :: String -> [Completion]
searchFunc str = map simpleCompletion $ filter (str `isPrefixOf`) wordList

mySettings :: Settings IO
mySettings = Settings { historyFile = Just "myhist"
                      , complete = completeWord Nothing " \t" $ return . searchFunc
                      , autoAddHistory = True
                      }

Replace the definition of mySettings in your snippet with that and it should work.

C. A. McCann
  • 76,893
  • 19
  • 209
  • 302
  • @amindfv: You're welcome! Not that I really *need* more rep anyway, but I don't usually see bounties on Haskell questions so I guess it served its purpose of getting attention? I did test it quickly to verify that it worked at all, but if you have any problems I'll be happy to help (since that didn't really feel like +50 worth of effort, hahaha). – C. A. McCann May 29 '11 at 04:24
  • It seems that when I use a history file that is not in the current directory(such as "~/.myhist"), haskeline history no longer works. Any thoughts on that? – hellerve Mar 02 '15 at 12:26
  • 1
    @Carson: Have you tried giving an absolute path that doesn't rely on expanding ~? Other than that, I have no idea. – C. A. McCann Mar 02 '15 at 17:52
  • Yes, I have. Doesn't work as well. I'm guessing it's just a simple bug on my side. – hellerve Mar 03 '15 at 12:19