mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-21 09:02:50 -06:00
the shell now supports cp1251 coding with se command. works with the word completion as well
This commit is contained in:
@@ -3,6 +3,7 @@ module GF.Compile.Coding where
|
|||||||
import GF.Grammar.Grammar
|
import GF.Grammar.Grammar
|
||||||
import GF.Grammar.Macros
|
import GF.Grammar.Macros
|
||||||
import GF.Text.UTF8
|
import GF.Text.UTF8
|
||||||
|
import GF.Text.CP1251
|
||||||
import GF.Infra.Modules
|
import GF.Infra.Modules
|
||||||
import GF.Infra.Option
|
import GF.Infra.Option
|
||||||
import GF.Data.Operations
|
import GF.Data.Operations
|
||||||
@@ -47,9 +48,3 @@ codeSourceModule co (id,moi) = case moi of
|
|||||||
PSeq p q -> PSeq (codp p) (codp q)
|
PSeq p q -> PSeq (codp p) (codp q)
|
||||||
PAlt p q -> PAlt (codp p) (codp q)
|
PAlt p q -> PAlt (codp p) (codp q)
|
||||||
_ -> p
|
_ -> p
|
||||||
|
|
||||||
---- from Text.Lexing
|
|
||||||
decodeCP1251 = map convert where
|
|
||||||
convert c
|
|
||||||
| c >= '\192' && c <= '\255' = chr (ord c + 848)
|
|
||||||
| otherwise = c
|
|
||||||
|
|||||||
18
src/GF/Text/CP1251.hs
Normal file
18
src/GF/Text/CP1251.hs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
module GF.Text.CP1251 where
|
||||||
|
|
||||||
|
import Data.Char
|
||||||
|
|
||||||
|
decodeCP1251 = map convert where
|
||||||
|
convert c
|
||||||
|
| c >= '\192' && c <= '\255' = chr (ord c + 848)
|
||||||
|
| c == '\168' = chr 1025 -- cyrillic capital letter lo
|
||||||
|
| c == '\184' = chr 1105 -- cyrillic small letter lo
|
||||||
|
| otherwise = c
|
||||||
|
|
||||||
|
encodeCP1251 = map convert where
|
||||||
|
convert c
|
||||||
|
| oc >= 1040 && oc <= 1103 = chr (oc - 848)
|
||||||
|
| oc == 1025 = chr 168 -- cyrillic capital letter lo
|
||||||
|
| oc == 1105 = chr 184 -- cyrillic small letter lo
|
||||||
|
| otherwise = c
|
||||||
|
where oc = ord c
|
||||||
@@ -2,6 +2,7 @@ module GF.Text.Lexing (stringOp) where
|
|||||||
|
|
||||||
import GF.Text.Transliterations
|
import GF.Text.Transliterations
|
||||||
import GF.Text.UTF8
|
import GF.Text.UTF8
|
||||||
|
import GF.Text.CP1251
|
||||||
|
|
||||||
import Data.Char
|
import Data.Char
|
||||||
import Data.List (intersperse)
|
import Data.List (intersperse)
|
||||||
@@ -98,18 +99,3 @@ unlexMixed = concat . alternate False where
|
|||||||
isPunct = flip elem ".?!,:;"
|
isPunct = flip elem ".?!,:;"
|
||||||
isParen = flip elem "()[]{}"
|
isParen = flip elem "()[]{}"
|
||||||
isClosing = flip elem ")]}"
|
isClosing = flip elem ")]}"
|
||||||
|
|
||||||
|
|
||||||
-- might be in a file of its own: Windows Cyrillic, used in Bulgarian resource
|
|
||||||
|
|
||||||
decodeCP1251 = map convert where
|
|
||||||
convert c
|
|
||||||
| c >= '\192' && c <= '\255' = chr (ord c + 848)
|
|
||||||
| otherwise = c
|
|
||||||
|
|
||||||
encodeCP1251 = map convert where
|
|
||||||
convert c
|
|
||||||
| oc >= 1040 && oc <= 1103 = chr (oc - 848)
|
|
||||||
| otherwise = c
|
|
||||||
where oc = ord c
|
|
||||||
|
|
||||||
|
|||||||
16
src/GFI.hs
16
src/GFI.hs
@@ -12,6 +12,7 @@ import GF.Infra.Option
|
|||||||
import GF.System.Readline
|
import GF.System.Readline
|
||||||
|
|
||||||
import GF.Text.UTF8 ----
|
import GF.Text.UTF8 ----
|
||||||
|
import GF.Text.CP1251
|
||||||
|
|
||||||
import PGF
|
import PGF
|
||||||
import PGF.Data
|
import PGF.Data
|
||||||
@@ -42,7 +43,7 @@ loop :: Options -> GFEnv -> IO GFEnv
|
|||||||
loop opts gfenv0 = do
|
loop opts gfenv0 = do
|
||||||
let env = commandenv gfenv0
|
let env = commandenv gfenv0
|
||||||
let sgr = sourcegrammar gfenv0
|
let sgr = sourcegrammar gfenv0
|
||||||
setCompletionFunction (Just (wordCompletion (commandenv gfenv0)))
|
setCompletionFunction (Just (wordCompletion gfenv0))
|
||||||
s0 <- fetchCommand (prompt env)
|
s0 <- fetchCommand (prompt env)
|
||||||
let gfenv = gfenv0 {history = s0 : history gfenv0}
|
let gfenv = gfenv0 {history = s0 : history gfenv0}
|
||||||
let loopNewCPU gfenv' = do
|
let loopNewCPU gfenv' = do
|
||||||
@@ -111,7 +112,7 @@ loop opts gfenv0 = do
|
|||||||
|
|
||||||
"ph":_ ->
|
"ph":_ ->
|
||||||
mapM_ (putStrLn . enc) (reverse (history gfenv0)) >> loopNewCPU gfenv
|
mapM_ (putStrLn . enc) (reverse (history gfenv0)) >> loopNewCPU gfenv
|
||||||
"se":c -> loopNewCPU $ gfenv {coding = s}
|
"se":c:_ -> loopNewCPU $ gfenv {coding = c}
|
||||||
|
|
||||||
-- ordinary commands, working on CommandEnv
|
-- ordinary commands, working on CommandEnv
|
||||||
_ -> do
|
_ -> do
|
||||||
@@ -170,15 +171,16 @@ emptyGFEnv = GFEnv emptyGrammar (mkCommandEnv encodeUTF8 emptyPGF) [] 0 "utf8"
|
|||||||
|
|
||||||
encode env = case coding env of
|
encode env = case coding env of
|
||||||
"utf8" -> encodeUTF8
|
"utf8" -> encodeUTF8
|
||||||
|
"cp1251" -> encodeCP1251
|
||||||
_ -> id
|
_ -> id
|
||||||
|
|
||||||
decode env = case coding env of
|
decode env = case coding env of
|
||||||
"utf8" -> decodeUTF8
|
"utf8" -> decodeUTF8
|
||||||
|
"cp1251" -> decodeCP1251
|
||||||
_ -> id
|
_ -> id
|
||||||
|
|
||||||
|
|
||||||
|
wordCompletion gfenv line0 prefix0 p =
|
||||||
wordCompletion cmdEnv line prefix p =
|
|
||||||
case wc_type (take p line) of
|
case wc_type (take p line) of
|
||||||
CmplCmd pref
|
CmplCmd pref
|
||||||
-> ret ' ' [name | name <- Map.keys (commands cmdEnv), isPrefixOf pref name]
|
-> ret ' ' [name | name <- Map.keys (commands cmdEnv), isPrefixOf pref name]
|
||||||
@@ -188,7 +190,7 @@ wordCompletion cmdEnv line prefix p =
|
|||||||
Right state0 -> let ws = words (take (length s - length prefix) s)
|
Right state0 -> let ws = words (take (length s - length prefix) s)
|
||||||
state = foldl nextState state0 ws
|
state = foldl nextState state0 ws
|
||||||
compls = getCompletions state prefix
|
compls = getCompletions state prefix
|
||||||
in ret ' ' (Map.keys compls)
|
in ret ' ' (map (encode gfenv) (Map.keys compls))
|
||||||
Left _ -> ret ' ' []
|
Left _ -> ret ' ' []
|
||||||
CmplOpt (Just (Command n _ _)) pref
|
CmplOpt (Just (Command n _ _)) pref
|
||||||
-> case Map.lookup n (commands cmdEnv) of
|
-> case Map.lookup n (commands cmdEnv) of
|
||||||
@@ -206,7 +208,11 @@ wordCompletion cmdEnv line prefix p =
|
|||||||
Left _ -> ret ' ' []
|
Left _ -> ret ' ' []
|
||||||
_ -> ret ' ' []
|
_ -> ret ' ' []
|
||||||
where
|
where
|
||||||
|
line = decode gfenv line0
|
||||||
|
prefix = decode gfenv prefix0
|
||||||
|
|
||||||
pgf = multigrammar cmdEnv
|
pgf = multigrammar cmdEnv
|
||||||
|
cmdEnv = commandenv gfenv
|
||||||
optLang opts = valIdOpts "lang" (head (languages pgf)) opts
|
optLang opts = valIdOpts "lang" (head (languages pgf)) opts
|
||||||
optCat opts = valIdOpts "cat" (lookStartCat pgf) opts
|
optCat opts = valIdOpts "cat" (lookStartCat pgf) opts
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user