3
在 fcitx 中切换国标与传统引号
source link: https://blog.lilydjwg.me/2012/3/2/switch-between-two-quote-styles-for-fcitx.32306.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
在 fcitx 中切换国标与传统引号
本文来自依云's Blog,转载请注明。
国家标准使用这些引号:‘’“”
,而我发现传统中文的引号更漂亮:「」『』
。我切换到传统引号已经有一段时间了,但最近发现有时还是需要使用国标引号,而 fcitx 的现任开发者认为不需要加入该切换功能。好在 fcitx 的配置文件都是文本,又有 fcitx-remote 工具,所以自己很容易地手动实现了两个版本的——Haskell 版本纯粹是练习用,因为没有扩展路径中的~
的现成函数,所以只好自己找了个实现,代码有些长。
fcitx-switch-quote
import
Control.Applicative ((<$>))
import
System.Cmd (rawSystem)
import
System.Directory (getHomeDirectory)
import
System.Posix.User
import
qualified
Data.Text
as
T
import
qualified
Data.Text.
IO
as
TIO
main =
do
file <- getFile
TIO.
readFile
file >>= (TIO.
writeFile
file) . (T.
map
(trChar
"“”‘’『』「」"
"『』「」“”‘’"
))
reloadFcitx
getFile ::
IO
FilePath
getFile = expandUser
"~/.config/fcitx/data/punc.mb.zh_CN"
reloadFcitx ::
IO
()
reloadFcitx = rawSystem
"fcitx-remote"
[
"fcitx-remote"
,
"-r"
] >>
return
()
expandUser ::
FilePath
->
IO
FilePath
expandUser
"~"
= getHomeDirectory
expandUser (
'~'
:
'/'
:p) =
fmap
(++
"/"
++ p) getHomeDirectory
expandUser (
'~'
:up) =
let
(u, p) =
break
(==
'/'
) up
in
fmap
(++
tail
p) (homeDirectory <$> getUserEntryForName u)
expandUser p =
return
p
trChar :: [
Char
] -> [
Char
] ->
Char
->
Char
trChar from to ch =
case
i
of
Just
i -> to !! i
_ -> ch
where
i = elemIndex ch from
Python 版本就很简洁了:
fcitx-switch-quote
#!/usr/bin/env python3
# vim:fileencoding=utf-8
import
os
m
=
str
.maketrans(
'“”‘’『』「」'
,
'『』「」“”‘’'
)
file
=
os.path.expanduser(
"~/.config/fcitx/data/punc.mb.zh_CN"
)
c
=
open
(
file
).read().translate(m)
open
(
file
,
'w'
).write(c)
os.execvp(
'fcitx-remote'
, [
'fcitx-remote'
,
'-r'
])
评论 (6)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK