3

在 fcitx 中切换国标与传统引号

 2 years ago
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'])

发送到 Kindle

Category: Linux | Tags: fcitx Haskell python | Read Count: 7280

评论 (6)
MaskRay 说:
9 年前

没办法,Haskell 的库分得比较散

依云 说:
9 年前

主要是它自己没有 expanduser,得自己写。

自由建客 说:
9 年前

嘿嘿!我早就定制标点符号表了!直接修改 punc.mb,连单书名号都做进去了!
只是成对符号和引号样都用一键表示了。用用就习惯了。

依云 说:
9 年前

定制不是问题,我解决的问题是一条命令就切换。

vx13 说:
9 年前

我还是看国标的引号比较顺眼。记得在大陆,那种传统的引号似乎是用于竖排文字的。

yx_wh 说:
9 年前

国标与传统引号的切换,我是一直切汉语和日语输入法的……

[取消回复评论]

昵称 登录 E-mail: *
Web:
Twitter:
当有新评论通过 E-mail 通知我

loading captcha image...
(输入验证码)

or Ctrl+Enter


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK