Awesome 窗口管理器——换种方式使用星际译王

星际译王被广泛使用的两个功能应该是划词翻译和手动输入翻译。由于星际译王缺少划词功能的开关快捷键,导致每次划词功能使用结束后,必须手动关闭此功能。

否则在 vim 视图模式下选择文本的时候会弹出不需要的翻译窗口,这有点让人苦恼。虽然它提供了‘在修饰键按下时才取词’的功能,但是如果你真的使用过你就知道,它需要你一直按住修饰键不松,否则翻译窗口立即被隐藏。另一种情况是,你不必始终按住修饰键,但是当你想隐藏翻译窗口时,你必须动动你的鼠标。简单来说,你有两个选择,要么按住修饰键5秒、10秒、甚至20秒,要么每次查询结束你的手就得离开键盘动动鼠标。

对于手动输入翻译这个功能来说,sdcv 是个很好的想法,但它需要你始终保持一个虚拟终端以备你随时的查询,而且这将破坏你目前的状态,因为你必须切换终端,甚至切换到另一个虚拟桌面。

Awesome 能帮我较好的解决这些苦恼,我甚至不用打开星际译王的程序,就可以使用‘在修饰键按下时才取词’的划词翻译功能,和更方便的手动输入翻译功能。只需两个额外的软件包 sdcv 和 xsel 就能换来更好的使用体验。

无论是用鼠标在 Firefox 里,还是用键盘在 Vim 里,当你选中一个单词,按 Meta-d 就会在右上方弹出翻译结果。当你要手动输入某个单词的时候,按 Meta-Shift-d 就会在任务栏那里弹出一个 Dict: 输入栏,输入后回车即可看到翻译结果。可设置超时自动关闭翻译结果,也可以再按 Meta-d 立即关闭翻译结果。当然你愿意的话,用鼠标点一下翻译结果也可以关闭它。

配置如下:

3.3 || 3.4

...
globalkeys = awful.util.table.join (
...
-- {{{ sdcv/stardict
awful.key({ modkey }, "d", function ()
    local f = io.popen("xsel -o")
    local new_word = f:read("*a")
    f:close()

    if frame ~= nil then
        naughty.destroy(frame)
        frame = nil
        if old_word == new_word then
            return
        end
    end
    old_word = new_word

    local fc = ""
    local f  = io.popen("sdcv -n --utf8-output -u '牛津英汉双解美化版' "..new_word)
    for line in f:lines() do
        fc = fc .. line .. '\n'
    end
    f:close()
    frame = naughty.notify({ text = fc, timeout = 10, width = 320 })
end),
awful.key({ modkey, "Shift" }, "d", function ()
    awful.prompt.run({prompt = "Dict: "}, mypromptbox[mouse.screen].widget, function(cin_word)
        naughty.destroy(frame)
        if cin_word == "" then
            return
        end

        local fc = ""
        local f  = io.popen("sdcv -n --utf8-output -u '牛津英汉双解美化版' "..cin_word)
        for line in f:lines() do
            fc = fc .. line .. '\n'
        end
        f:close()
        frame = naughty.notify({ text = fc, timeout = 10, width = 320 })
    end, nil, awful.util.getdir("cache").."/dict")
end),
-- }}}
...
) -- globalkeys

简单说明一下,sdcv -u 参数后面跟的是字典名称,用 sdcv -l 可以查询现有的字典。推荐两本字典 xdict (简要), 牛津英汉双解美化版 (丰富)

Read More:

20 Comments

  1. 1 GunsNRose Commented @ 2009-11-10 20:38Reply to this comment

    沙发,顶一个,现在用awesome 3.4准备试一下

  2. 2 gc Commented @ 2009-11-10 22:12Reply to this comment

    恩 不错 这样用起来爽多了。。。

  3. 3 GunsNRose Commented @ 2009-11-10 22:31Reply to this comment

    第二个函数 function(cinword) cinword cin_word 是不是笔误呀,我是修改后才能用的,非常感谢,期待更多这样的好文章 要自己把字典还sdcv安好才能用,都在aur仓库里有

    BTW,以前版本里的 Mod + Shift + y的功能好像不见了

  4. 4 GunsNRose Commented @ 2009-11-10 22:50Reply to this comment
    # Maintainer: zhengyi 
    
    pkgname=stardict-oxford-gb-formated
    pkgver=2.4.2
    pkgrel=1
    pkgdesc="English Chinese dictionary Oxford-gb-formated for Stardict"
    license=('GPL')
    depends=('stardict')
    url="http://stardict.sourceforge.net"
    source=(http://prdownloads.sourceforge.net/stardict/${pkgname}-${pkgver}.tar.bz2)    
    arch=('i686' 'x86_64')
    
    build() {
        cd $startdir/src/${pkgname}-$pkgver
        mkdir -p $startdir/pkg/usr/share/stardict/dic/
        install -m 644 oxford-gb-formated.dict.dz   $startdir/pkg/usr/share/stardict/dic/
        install -m 644 oxford-gb-formated.idx       $startdir/pkg/usr/share/stardict/dic/
        install -m 644 oxford-gb-formated.ifo       $startdir/pkg/usr/share/stardict/dic/
    }
    
    
    md5sums=('c71b174f6c2aeebbf4deeab365e837ff')
    

    在aur上没看到有这个包,就小改了一下

  5. 5 iarch Commented @ 2009-11-11 2:06Reply to this comment

    可惜stardict基本上不怎么更新了

  6. 6 Terry Commented @ 2009-11-11 4:59Reply to this comment

    @iarch: 用LDOCE5,DVD里有for Linux的。如果要用英汉,直接用网上那些有道,iciba就够了。

  7. 7 Josh Commented @ 2009-11-11 9:50Reply to this comment

    工作不是太好,经常出错。 W: awesome: eventhandlekey:567: error running function: /home/joshua/.config/awesome/rc.lua:253: attempt to concatenate global 'cinword' (a nil value) W: awesome: drawtextcontextinit:108: cannot parse pango markup: Error on line 26 char 17: '<? ? ? ? ' is not a valid name W: awesome: drawtextcontextinit:108: cannot parse pango markup: Error on line 26 char 17: '<? ? ? ? ' is not a valid name W: awesome: drawtextcontextinit:108: cannot parse pango markup: Error on line 192 char 11: '<? ? ' is not a valid name W: awesome: drawtextcontextinit:108: cannot parse pango markup: Error on line 82 char 59: '<? ? ? ? ' is not a valid name W: awesome: drawtextcontextinit:108: cannot parse pango markup: Error on line 82 char 59: '<? ? ? ? ' is not a valid name

  8. 8 hantsy Commented @ 2009-11-11 10:02Reply to this comment

    google 为什么不开发linux版本的词霸。。。

  9. 9 walkerxk Commented @ 2009-11-11 10:08Reply to this comment

    sdcv还活着?

  10. 10 GunsNRose Commented @ 2009-11-11 11:00Reply to this comment

    @Josh 把 cinword改成cin_word试试看

  11. 11 GunsNRose Commented @ 2009-11-11 11:08Reply to this comment

    @Josh 把 cinword改成cin_word试试看

    提交linuxtoy的回复信息真慢,现在是第三次提交

    。。还没成功,试一下第四次 还不行,第五次 第六次 .... 提交出来不知道是第几回了

    成都电信ADSL,toy看看是网络问题还是linuxtoy提交部分有BUG,在家1M跟公司3M都这样,提交N次都不成功

  12. 12 maokk Commented @ 2009-11-11 11:37Reply to this comment

    GoldenDict的取词也有问题,不知用那些方法改进改进?

  13. 13 Ian Yang Commented @ 2009-11-11 11:39Reply to this comment

    stardict比较恶心的是最小化到系统托盘后,除了用鼠标点没有其它办法恢复。

    XMonad 0.9加了XMonad.Util.NamedScratchpad,可以把任意程序放在一个workspace里,需要的时候显示在当前workspace (quake terminal),正好可以用来呼出stardict。为了只使用一个stardict实例,启动命令可以用"killall stardict; stardict"

  14. 14 bobye Commented @ 2009-11-11 14:43Reply to this comment

    一直在emacs里用这个sdcv……

  15. 15 逸飞 Commented @ 2009-11-12 11:42Reply to this comment

    呵呵,真是高级使用方法。 学习了。

  16. 16 liuxht Commented @ 2009-11-12 12:12Reply to this comment

    cin_word -- > cinword 呵呵,真的好好用哟,不过找了很久的错误,顺便学了会lua,搞得我都想去wiki认真学习写自己的rc.lua了。 width建议搞成380,我是1024x768的桌面,看习惯了stardict的默认浮窗宽度,但是stardict的浮动窗口320比起notify的320要宽一些。

  17. 17 icend Commented @ 2009-11-12 12:14Reply to this comment

    nice post. 非awesome用户可以用dzen2和sselp来做。

  18. 18 Kyle Wu Commented @ 2009-11-12 20:07Reply to this comment

    为什么我这里显示不出来呢?只能看到一个空白的frame

  19. 19 alexmajy Commented @ 2009-11-17 3:17Reply to this comment

    cool, 搞了正版xp一关程序老是报错,不如d的用的好,受不了了……装上910彻底转linux,这几天正头疼电子词典不好使,vern的这个方案相当赞!

    @Kyle Wu 自己调一下,应该不是太大的问题,我一开始也有问题,后来仔细搞搞就好了,估计是哪里敲错了。

  20. 20 Kyle Wu Commented @ 2009-11-18 21:44Reply to this comment

    @alexmajy: 调用sdcv时去掉 -n --utf8-output,我这里就可以了,好奇怪的。直接命令行里敲是没问题。