本文介绍如何在Emacs中使用Bing translate api实现高效翻译。之所 以使用bing translate api,一方面因为bing translate api翻译效果很好, 另一方面是因为google translate api总是提示说今天的访问超量了 (dailyLimitExceeded)。
安装
- 将emacs解压。
- 克隆translate-emacs-toolkit最新版本,或者下载压缩包解压。
- 在bing开发者申请appId。
- 启动emacs。
- 在.emacs文件中加入下列代码,配置appengine-emacs-toolkit。
(add-to-list 'load-path (concat git-path "translate-emacs-toolkit"))
;; Your appId. Application at http://www.bing.com/toolbox/bingdeveloper/
(defvar bingtranslate-appId "your-appId")
;; Your priority language to translate from.
(defvar bingtranslate-from-priority "en")
;; Your priority language to translate to.
(defvar bingtranslate-to-priority "zh-CHS")
(require 'bing-translate-api)
;; key bounding
(global-set-key [M-f1] 'bingtranslate-region-or-input)
;; add a pair of language
;; Parameters: "pair name" "from language" "to language"
(bingtranslate-add-pair "1" "zh-CHS" "en")
其中plugin-path是你存放translate-emacs-toolkit文件夹的路径(如 d:/home/emacs-23/site-lisp/)。 最后一句的意思给“简体中文”到“英文”的翻译设置快捷方式。
- 重启emacs。
- 调用命令bingtranslate-region-or-input(M-x bingtranslate-region-or-input)。输入翻译文本、原始语言(或快捷 方式)、翻译语言。在minibuffer中会出现翻译结果。并且翻译结果会 自动加载在剪切板中。
- 调用命令bingtranslate-show-history(M-x bingtranslate-show-history)可以调出翻译历史记录。
There are lots of version of python, We have to face different python in one system. I use python25 and python27 to develop GoogleAppEngine and use python26 to develop SinaAppEngine. I want to try python32. So following I will set up python with emacs step by step.
Firstly we install all python to be used. Such as python25, python26 and python27. I put all of them to “d:/home/program/”.
Run emacs and find file .emacs. Add following code to set python27‘s root in emacs’s path.
(setq pythonversion "27")
(setq program-path "d:/home/program/")
(setenv "PATH" (concat (concat program-path"python"pythonversion";") (getenv "PATH")))
Then add following function to switch between pythons.
(defun* zxy-switch-python-version (&optional (pver nil))
"Swith python version at runtime"
(interactive)
(if (equal nil pver)
(let ((tempver (read-string (format "[zxy] Swith to which version of python (default %s)? "
pythonversion) nil nil pythonversion nil)))
(setq pver tempver)))
(unless (file-exists-p (concat program-path"python"pver""))
(error (format "[zxy] Cannot find python%s!" pver)))
(message (format "[zxy] Switch to python%s." pver))
(setq pythonversion pver)
(setenv "PATH" (concat (concat program-path"python"pythonversion";") (getenv "PATH"))))
Set up python-mode as following.
;; emacs python
(require 'python)
(defun zxy-python-compile ()
"Use compile to run python programs"
(interactive)
(compile (concat "python " (buffer-name))))
(setq compilation-scroll-output t)
(define-key python-mode-map "\C-cc" 'zxy-python-compile)
Restart emacs and open a py file. M-x zxy-python-compile will run this file in a new buffer. M-x zxy-switch-python-version help to switch python version.
由于 python 版本的不断更新,我们经常会在同一个系统中使用若干种 版本的 python 。比如我使用 python25 和 python27 开发 GoogleAppEngine应用,使用 python26 开发SinaAppEngine应用,还会尝试 一下 python32 。本文主要介绍如何在emacs环境下便捷的切换python版本。
首先安装不同版本的 python ,这里我们以安装 python25 , python26 和 python27 为例。将上述三个版本的/python/ 都安装在 d:/home/program文件夹下。
启动emacs,打开.emacs文件。添加下列代码,将 python27 的根目录路径 写进emacs环境变量。
(setq pythonversion "27")
(setq program-path "d:/home/program/")
(setenv "PATH" (concat (concat program-path"python"pythonversion";") (getenv "PATH")))
然后添加下面函数,用于切换 python 版本。
(defun* zxy-switch-python-version (&optional (pver nil))
"Swith python version at runtime"
(interactive)
(if (equal nil pver)
(let ((tempver (read-string (format "[zxy] Swith to which version of python (default %s)? "
pythonversion) nil nil pythonversion nil)))
(setq pver tempver)))
(unless (file-exists-p (concat program-path"python"pver""))
(error (format "[zxy] Cannot find python%s!" pver)))
(message (format "[zxy] Switch to python%s." pver))
(setq pythonversion pver)
(setenv "PATH" (concat (concat program-path"python"pythonversion";") (getenv "PATH"))))
配置python-mode如下。
;; emacs python
(require 'python)
(defun zxy-python-compile ()
"Use compile to run python programs"
(interactive)
(compile (concat "python " (buffer-name))))
(setq compilation-scroll-output t)
(define-key python-mode-map "\C-cc" 'zxy-python-compile)
重起emacs。打开任意py文件。M-x zxy-python-compile运行该文件。 M-x zxy-switch-python-version,切换python版本。
段首空两格从第一堂作文课开始就深深植入每一个中国人的心中,这是 我们的习惯和文化。这里我提供两种方法,使用emacs、org2blog,在 wordpress上发布段首空格的中文博客。
浏览过我的博客的朋友可能会问,“为什么你喜欢用蹩脚的英文写博 客?”原因有三,首先英文的博客容易被google搜索到;其次我使用emacs 发布博客,且打开了自动换行,这样中文段落中会有很多不必要的空格,至 今没有好的解决办法;最后我想联系一下英文的写作,学了十几年的英语, 听书读写样样不行,我不甘心。
废话不多说了,下面是博客中文段首空格的两种解决办法。
方法一:段首插入中文空格
在.emacs文件中加入下面函数。然后在每次开始新段落时只要 shift+spc两次即可。
(defun insert-big-spc (p)
"insert a Chinese spc."
(interactive "*p")
(insert " ")) ;; 中文空格
(global-set-key (kbd "<S-SPC>") 'insert-big-spc)
方法二:自定义CSS方式
在org模式下,中文段落前后用如下css格式的标签包裹即可。
@<p style="text-indent:2em">
另外我还要隆重的向大家推荐一下小黑。小黑是我住处附近的一条看门
狗。它全身乌黑,个头比常见的宠物狗略小一号。小黑不是宠物,我从来没
有见过他的主人,它总是独自在我经常散步的草地上休息,偶尔有宠物伙伴
来访,会兴奋异常,但很快又会孤单一人。当有陌生人或者车子进入草地旁
一所单位的大门时,小黑会立即跑过去,叫个不停。它对我很警惕,也不敢
正眼看我,今天我特意留着中午吃排骨剩下的骨头,准备晚上贿赂一下小黑。
@</p>
Everytime I have to reset .emacs file when I use emacs on a new computer. I can not avoid to find out default home path and write a .emacs file with only one line “(load-file “new/emacs/home/path/.emacs”)”. Now I don’t have to do this because I add following code on top of my .emacs file.
(defun zxy-reset-dotemacs ()
"Reset .emacs file"
(interactive)
(with-temp-buffer
(let (print-level print-length)
(insert (format "(load-file \"%s\")" load-file-name))
(if (file-exists-p "~/.emacs")
(message "Don't need reset .emacs file!")
(progn
(message "Reset .emacs file.")
(write-file "~/.emacs"))))))
(zxy-reset-dotemacs)
Then I can open emacs on a new computer, input “(load-file “d:/zxy/home/.emacs”)” at scratch buffer and type C-j. This function will automatically reset my .emacs file.
Add the following code to .emacs file. It will automatically set emacs’s home to your .emacs file path.
(if (eq system-type 'windows-nt)
(progn
(setenv "HOME" (car (split-string default-directory "emacs")))
(setq plugin-path "~/emacs-23.3/site-lisp/plugins/"))
(setq plugin-path "/usr/share/emacs23/site-lisp/plugins/"))
The plugin-path is a constant for positioning my plugins. For example, I can add org to my load-path as follow.
(add-to-list 'load-path (concat plugin-path "org-7.7/lisp"))