;; -*- Emacs-Lisp -*- ;; ;; Translate sentence/region using kingtr command. ;; Copyright (c) 2002-2006, Hiroyuki Ohsaki. ;; All rights reserved. ;; ;; $Id: king.el,v 1.10 2015/03/15 15:08:03 ohsaki Exp ohsaki $ ;; ;; This code is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY. No author or distributor ;; accepts responsibility to anyone for the consequences of using it ;; or for whether it serves any particular purpose or works at all, ;; unless he says so in writing. Refer to the GNU Emacs General Public ;; License for full details. ;; Everyone is granted permission to copy, modify and redistribute ;; this code, but only under the conditions described in the ;; GNU Emacs General Public License. A copy of this license is ;; supposed to have been given to you along with GNU Emacs so you ;; can know your rights and responsibilities. It should be in a ;; file named COPYING. Among other things, the copyright notice ;; and this notice must be preserved on all copies. ;; usage: ;; Put pkingtr (available at http://www.lsnl.jp/~ohsaki/software/perl/pkingtr) ;; in your path, and add the following lines to your ~/.emacs. ;; (autoload 'king-translate "king" nil t) ;; (global-set-key "\C-cT" 'king-translate) (setq king-translate-window-height 0.3) (defun king-translate-string (str &optional no-config) "Run \"pkingtr\" for STR and returrn its translation." (let ((cmd (if no-config "pkingtr -n" "pkingtr"))) (with-temp-buffer (insert str) (shell-command-on-region (point-min) (point-max) cmd t t) (fill-region (point-min) (point-max)) (buffer-string)))) (defun king-translate-region (start end &optional do-insert) "Run \"pkingtr\" for the region specified by START and END." (interactive "r\nP") (let ((str (buffer-substring start end))) (if do-insert ;; replace the region with translation (save-restriction (narrow-to-region start end) ;; comment translated region ;; FIXME: should not assume LaTeX-style comment (let ((comment-start "%") (comment-padding " ")) (comment-region (point-min) (point-max))) ;; insert translation result (goto-char (point-max)) (insert (king-translate-string str))) ;; otherwise, display translation in separate buffer (let ((buf (get-buffer-create "*King Translation*")) (out1 (king-translate-string str)) (out2 (king-translate-string str 'no-config))) (save-excursion (set-buffer buf) (erase-buffer) (insert str "\n" out1 "\n" out2) (LaTeX-mode) (delete-other-windows) (split-window nil (truncate (* (window-height) (- 1 king-translate-window-height)))) (set-window-buffer (next-window) buf)))))) (defun king-translate (&optional do-insert) (interactive "P") (king-translate-region (save-excursion (if (re-search-backward "\\(\n\n\\|。\\|\\. *\n\\)" nil t) (progn (skip-chars-forward " \t\n。\\.") ;; skip comments (while (looking-at " *%") (forward-line 1)) (point)) (point-min))) (save-excursion (if (re-search-forward "\\(\n\n\\|。\\|\\. *\n\\)" nil t) (progn (skip-chars-forward " \t") (and (looking-at "\n") (forward-char 1)) (point)) (point-max))) do-insert))