一直在用有道词典的插件进行翻译,但是存在一个问题。
有道对于可视模式选中的区域不会做二次处理,导致转行、注释标识以及别的一些字符会破坏段落的语句。
比如这样:
还是比较烦的。
最近打算学习emacs.所以尝试自己解决一下这个问题:
;; 定义一些测试用的新函数
;; text 是输入的段落
;; regexp-replace-list形如:
;; '(
;; (regexp1 . replace1)
;; (regexp2 . replace2)
;; ...
;; )
(defun replace-unnecessary-string (text regexp-replace-list)
"Sequncely match and replace text by regexp-replace-list"
(if (not regexp-replace-list)
text
(let ((regexp (car (car regexp-replace-list)))
(replace (cdr (car regexp-replace-list)))
(rest-regexp-replace-list (cdr regexp-replace-list)))
(replace-unnecessary-string
(replace-regexp-in-string regexp replace text) rest-regexp-replace-list))))
(require 'youdao-dictionary)
(defun test-search-at-point- (func)
"Search word at point and display result with given FUNC. my version"
(let ((word (youdao-dictionary--region-or-word)))
(if word
(funcall func (youdao-dictionary--format-result
(youdao-dictionary--request
;; 把youdao-dictionary源码中这里的word进行处理
(replace-unnecessary-string word '(
("[\n\r]" . " ")
(";;" . "")
(" +" . " ")))))
(message "Nothing to look up"))))
(defun test-search-at-point+ ()
"Search word at point and display result with popup-tip."
(interactive)
(test-search-at-point- #'popup-tip))
我的逻辑上就是简单的把源码中从选区获取到的段落加一个处理的步骤(replace-unnecessary-string
)。
这还是第一次写不是照抄的配置用elisp代码,感觉自己写的不是很好,希望大家帮我看看如何改进。
这是效果: