emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/pyim 4e7fe1b 12/36: Add new file: pyim-dcache.el


From: ELPA Syncer
Subject: [elpa] externals/pyim 4e7fe1b 12/36: Add new file: pyim-dcache.el
Date: Thu, 22 Apr 2021 22:57:16 -0400 (EDT)

branch: externals/pyim
commit 4e7fe1b55a761ee901d2f8a161c3dc8c2f39de11
Author: Feng Shu <tumashu@163.com>
Commit: Feng Shu <tumashu@163.com>

    Add new file: pyim-dcache.el
---
 pyim-common.el                   |  94 -------------------
 pyim-common.el => pyim-dcache.el | 197 +++++++++++++++++----------------------
 pyim-dhashcache.el               |   9 +-
 pyim.el                          |  76 +--------------
 4 files changed, 93 insertions(+), 283 deletions(-)

diff --git a/pyim-common.el b/pyim-common.el
index b52cea9..741c529 100644
--- a/pyim-common.el
+++ b/pyim-common.el
@@ -33,100 +33,6 @@
 
 (defvar pyim-debug nil)
 
-(defvar pyim-prefer-emacs-thread nil
-  "是否优先使用 emacs thread 功能来生成 dcache.
-
-如果这个变量设置为 t, 那么当 emacs thread 功能可以使用时,
-pyim 优先使用 emacs thread 功能来生成 dcache, 如果设置为 nil,
-pyim 总是使用 emacs-async 包来生成 dcache.
-
-不过这个选项开启之后,会显著的减慢词库加载速度,特别是
-五笔等形码输入法。")
-
-(defcustom pyim-dcache-directory (locate-user-emacs-file "pyim/dcache/")
-  "一个目录,用于保存 pyim 词库对应的 cache 文件."
-  :type 'directory
-  :group 'pyim)
-
-(defun pyim-dcache-get-value-from-file (file)
-  "读取保存到 FILE 里面的 value."
-  (when (file-exists-p file)
-    (with-temp-buffer
-      (insert-file-contents file)
-      (let ((output
-             (condition-case nil
-                 (read (current-buffer))
-               (error nil))))
-        (unless output
-          ;; 有时候词库缓存会发生错误,这时候,就将词库缓存转存到一个
-          ;; 带时间戳的文件中,方便用户手动修复。
-          (write-file (concat file "-dump-" (format-time-string 
"%Y%m%d%H%M%S"))))
-        output))))
-
-(defun pyim-dcache-get-variable (variable)
-  "从 `pyim-dcache-directory' 中读取与 VARIABLE 对应的文件中保存的值."
-  (let ((file (expand-file-name (symbol-name variable)
-                                pyim-dcache-directory)))
-    (pyim-dcache-get-value-from-file file)))
-
-(defun pyim-dcache-set-variable (variable &optional force-restore 
fallback-value)
-  "设置变量.
-
-如果 VARIABLE 的值为 nil, 则使用 ‘pyim-dcache-directory’ 中对应文件的内容来设置
-VARIABLE 变量,FORCE-RESTORE 设置为 t 时,强制恢复,变量原来的值将丢失。
-如果获取的变量值为 nil 时,将 VARIABLE 的值设置为 FALLBACK-VALUE ."
-  (when (or force-restore (not (symbol-value variable)))
-    (let ((file (expand-file-name (symbol-name variable)
-                                  pyim-dcache-directory)))
-      (set variable (or (pyim-dcache-get-value-from-file file)
-                        fallback-value
-                        (make-hash-table :test #'equal))))))
-
-(defun pyim-dcache-save-value-to-file (value file)
-  "将 VALUE 保存到 FILE 文件中."
-  (when value
-    (with-temp-buffer
-      ;; FIXME: We could/should set the major mode to `lisp-data-mode'.
-      (insert ";; Auto generated by `pyim-dhashcache-save-variable-to-file', 
don't edit it by hand!\n")
-      (insert (format ";; Build time: %s\n\n" (current-time-string)))
-      (insert (prin1-to-string value))
-      (insert "\n\n")
-      (insert ";; Local\sVariables:\n") ;Use \s to avoid a false positive!
-      (insert ";; coding: utf-8-unix\n")
-      (insert ";; End:")
-      (make-directory (file-name-directory file) t)
-      (let ((save-silently t))
-        (pyim-dcache-write-file file)))))
-
-
-(defun pyim-dcache-save-variable (variable)
-  "将 VARIABLE 变量的取值保存到 `pyim-dcache-directory' 中对应文件中."
-  (let ((file (expand-file-name (symbol-name variable)
-                                pyim-dcache-directory))
-        (value (symbol-value variable)))
-    (pyim-dcache-save-value-to-file value file)))
-
-;; ** 处理词库文件
-(defun pyim-use-emacs-thread-p ()
-  "判断是否使用 emacs thread 功能来生成 thread."
-  (and pyim-prefer-emacs-thread
-       (>= emacs-major-version 26)))
-
-(defun pyim-dcache-write-file (filename &optional confirm)
-  "A helper function to write dcache files."
-  (let ((coding-system-for-write 'utf-8-unix))
-    (when (and confirm
-               (file-exists-p filename)
-               ;; NS does its own confirm dialog.
-               (not (and (eq (framep-on-display) 'ns)
-                         (listp last-nonmenu-event)
-                         use-dialog-box))
-               (or (y-or-n-p (format-message
-                              "File `%s' exists; overwrite? " filename))
-                   (user-error "Canceled"))))
-    (write-region (point-min) (point-max) filename nil :silent)
-    (message "Saving file %s..." filename)))
-
 (defun pyim-string-match-p (regexp string &optional start)
   "与 `string-match-p' 类似,如果 REGEXP 和 STRING 是非字符串时,
 不会报错。"
diff --git a/pyim-common.el b/pyim-dcache.el
similarity index 57%
copy from pyim-common.el
copy to pyim-dcache.el
index b52cea9..7ae2204 100644
--- a/pyim-common.el
+++ b/pyim-dcache.el
@@ -1,12 +1,13 @@
-;;; pyim-common.el --- common utilities for pyim    -*- lexical-binding: t; -*-
+;;; pyim-dcache.el --- dcache tools for pyim.        -*- lexical-binding: t; 
-*-
 
 ;; * Header
-;; Copyright (C) 2015-2021 Free Software Foundation, Inc.
+;; Copyright (C) 2021 Free Software Foundation, Inc.
 
 ;; Author: Feng Shu <tumashu@163.com>
 ;; Maintainer: Feng Shu <tumashu@163.com>
 ;; URL: https://github.com/tumashu/pyim
 ;; Keywords: convenience, Chinese, pinyin, input-method
+;; Package-Requires: ((emacs "24.4") (async "1.6") (xr "1.13"))
 
 ;; This file is part of GNU Emacs.
 
@@ -25,15 +26,22 @@
 
 ;;; Commentary:
 
-;; * 说明文档                                                              :doc:
-;; This file has common utilities used by pyim
-
 ;;; Code:
-;; * 代码                                                                 :code:
+;; * 代码                                                           :code:
+(require 'cl-lib)
+(require 'pyim-common)
+
+(defcustom pyim-dcache-backend 'pyim-dhashcache
+  "词库后端引擎.负责缓冲词库并提供搜索词的算法.
+可选项为 `pyim-dhashcache' 或 `pyim-dregcache'.
+前者搜索单词速度很快,消耗内存多.  后者搜索单词速度较快,消耗内存少.
 
-(defvar pyim-debug nil)
+`pyim-dregcache' 速度和词库大小成正比.  当词库接近100M大小时,
+在六年历史的笔记本上会有一秒的延迟. 这时建议换用 `pyim-dhashcache'."
+  :type 'symbol)
 
-(defvar pyim-prefer-emacs-thread nil
+(define-obsolete-variable-alias 'pyim-prefer-emacs-thread 
'pyim-dcache-prefer-emacs-thread "3.0")
+(defvar pyim-dcache-prefer-emacs-thread nil
   "是否优先使用 emacs thread 功能来生成 dcache.
 
 如果这个变量设置为 t, 那么当 emacs thread 功能可以使用时,
@@ -43,11 +51,29 @@ pyim 总是使用 emacs-async 包来生成 dcache.
 不过这个选项开启之后,会显著的减慢词库加载速度,特别是
 五笔等形码输入法。")
 
+(defvar pyim-dcache-auto-update t
+  "是否自动创建和更新词库对应的 dcache 文件.
+
+这个变量默认设置为 t, 如果有词库文件添加到 `pyim-dicts' 或者
+`pyim-extra-dicts' 时,pyim 会自动生成相关的 dcache 文件。
+
+一般不建议将这个变量设置为 nil,除非有以下情况:
+
+1. 用户的词库已经非常稳定,并且想通过禁用这个功能来降低
+pyim 对资源的消耗。
+2. 自动更新功能无法正常工作,用户通过手工从其他机器上拷贝
+dcache 文件的方法让 pyim 正常工作。")
+
 (defcustom pyim-dcache-directory (locate-user-emacs-file "pyim/dcache/")
   "一个目录,用于保存 pyim 词库对应的 cache 文件."
   :type 'directory
   :group 'pyim)
 
+(defun pyim-dcache-use-emacs-thread-p ()
+  "判断是否使用 emacs thread 功能来生成 thread."
+  (and pyim-dcache-prefer-emacs-thread
+       (>= emacs-major-version 26)))
+
 (defun pyim-dcache-get-value-from-file (file)
   "读取保存到 FILE 里面的 value."
   (when (file-exists-p file)
@@ -98,7 +124,6 @@ VARIABLE 变量,FORCE-RESTORE 设置为 t 时,强制恢复,变量原来的
       (let ((save-silently t))
         (pyim-dcache-write-file file)))))
 
-
 (defun pyim-dcache-save-variable (variable)
   "将 VARIABLE 变量的取值保存到 `pyim-dcache-directory' 中对应文件中."
   (let ((file (expand-file-name (symbol-name variable)
@@ -106,12 +131,6 @@ VARIABLE 变量,FORCE-RESTORE 设置为 t 时,强制恢复,变量原来的
         (value (symbol-value variable)))
     (pyim-dcache-save-value-to-file value file)))
 
-;; ** 处理词库文件
-(defun pyim-use-emacs-thread-p ()
-  "判断是否使用 emacs thread 功能来生成 thread."
-  (and pyim-prefer-emacs-thread
-       (>= emacs-major-version 26)))
-
 (defun pyim-dcache-write-file (filename &optional confirm)
   "A helper function to write dcache files."
   (let ((coding-system-for-write 'utf-8-unix))
@@ -127,103 +146,59 @@ VARIABLE 变量,FORCE-RESTORE 设置为 t 时,强制恢复,变量原来的
     (write-region (point-min) (point-max) filename nil :silent)
     (message "Saving file %s..." filename)))
 
-(defun pyim-string-match-p (regexp string &optional start)
-  "与 `string-match-p' 类似,如果 REGEXP 和 STRING 是非字符串时,
-不会报错。"
-  (and (stringp regexp)
-       (stringp string)
-       (string-match-p regexp string start)))
-
-(defun pyim-dline-parse (&optional seperaters)
-  "解析词库文件当前行的信息,SEPERATERS 为词库使用的分隔符。"
-  (let* ((begin (line-beginning-position))
-         (end (line-end-position))
-         (items (split-string
-                 (buffer-substring-no-properties begin end)
-                 seperaters)))
-    items))
-
-(defun pyim-permutate-list (list)
-  "使用排列组合的方式重新排列 LIST.
-这个函数由 ‘二中’ 提供,`pyim-hanzi2pinyin' 没有使用这个函数
-(速度稍微有点慢)。"
-  (let ((list-head (car list))
-        (list-tail (cdr list)))
-    (cond ((null list-tail)
-           (cl-loop for element0 in list-head
-                    append (cons (cons element0 nil) nil)))
-          (t (cl-loop for element in list-head
-                      append (mapcar (lambda (l) (cons element l))
-                                     (pyim-permutate-list list-tail)))))))
-
-(defun pyim-permutate-list2 (list)
-  "使用排列组合的方式重新排列 LIST.
-这个函数由 ’翀/ty‘ 提供,`pyim-hanzi2pinyin' 默认使用这个函数。"
-  (if (= (length list) 1)
-      (mapcar #'list (car list))
-    (pyim-permutate-list2-internal (car list) (cdr list))))
-
-(defun pyim-permutate-list2-internal (one two)
-  "`pyim-permutate-list2' 的内部函数。"
-  (let (return)
-    (if (null (car two))
-        one
-      (dolist (x1 one)
-        (dolist (x2 (car two))
-          (push (if (listp x1)
-                    (append x1 (list x2))
-                  (list x1 x2))
-                return)))
-      (setq one return)
-      (pyim-permutate-list2-internal one (cdr two)))))
-
-(defun pyim-flatten-list (my-list)
-  (cond
-   ((null my-list) nil)
-   ((atom my-list) (list my-list))
-   (t (append (pyim-flatten-list (car my-list))
-              (pyim-flatten-list (cdr my-list))))))
-
-(defun pyim-list-merge (a b)
-  "Join list A and B to a new list, then delete dups."
-  (let ((a (if (listp a)
-               a
-             (list a)))
-        (b (if (listp b)
-               b
-             (list b))))
-    (delete-dups `(,@a ,@b))))
-
-(defun pyim-char-before-to-string (num)
-  "得到光标前第 `num' 个字符,并将其转换为字符串。"
-  (let* ((point (point))
-         (point-before (- point num)))
-    (when (and (> point-before 0)
-               (char-before point-before))
-      (char-to-string (char-before point-before)))))
-
-(defun pyim-char-after-to-string (num)
-  "得到光标后第 `num' 个字符,并将其转换为字符串。"
-  (let* ((point (point))
-         (point-after (+ point num)))
-    (when (char-after point-after)
-      (char-to-string (char-after point-after)))))
-
-(defun pyim-posframe-valid-p ()
-  "Test posframe's status."
-  (and (>= emacs-major-version 26)
-       (featurep 'posframe)
-       (not (or noninteractive
-                emacs-basic-display
-                (not (display-graphic-p))))))
-
-(defun pyim-exwm-enable-p ()
-  "测试当前是否是 exwm 环境。"
-  (string-match-p " \\*temp\\*" (buffer-name)))
+(defun pyim-dcache-call-api (api-name &rest api-args)
+  "Get backend API named API-NAME then call it with arguments API-ARGS."
+  ;; make sure the backend is load
+  (unless (featurep pyim-dcache-backend)
+    (require pyim-dcache-backend))
+  (let ((func (intern (concat (symbol-name pyim-dcache-backend)
+                              "-" (symbol-name api-name)))))
+    (if (functionp func)
+        (apply func api-args)
+      (when pyim-debug
+        (message "%S 不是一个有效的 dcache api 函数." (symbol-name func))
+        ;; Need to return nil
+        nil))))
+
+(defun pyim-dcache-update-code2word (&optional force)
+  "读取并加载词库.
+
+读取 `pyim-dicts' 和 `pyim-extra-dicts' 里面的词库文件,生成对应的
+词库缓冲文件,然后加载词库缓存。
+
+如果 FORCE 为真,强制加载。"
+  (let* ((dict-files (mapcar #'(lambda (x)
+                                 (unless (plist-get x :disable)
+                                   (plist-get x :file)))
+                             `(,@pyim-dicts ,@pyim-extra-dicts)))
+         (dicts-md5 (pyim-create-dicts-md5 dict-files)))
+    (pyim-dcache-call-api 'update-code2word dict-files dicts-md5 force)))
+
+(defun pyim-dcache-init-variables ()
+  "初始化 dcache 缓存相关变量."
+  (pyim-dcache-call-api 'init-variables))
+
+(defun pyim-dcache-save-caches ()
+  "保存 dcache.
+
+  将用户选择过的词生成的缓存和词频缓存的取值
+  保存到它们对应的文件中.
+
+  这个函数默认作为 `kill-emacs-hook' 使用。"
+  (interactive)
+  (pyim-dcache-call-api 'save-personal-dcache-to-file)
+  t)
+
+;; ** 从词库中搜索中文词条
+(defun pyim-dcache-get (code &optional from)
+  "从 FROM 对应的 dcache 中搜索 CODE, 得到对应的词条.
+
+当词库文件加载完成后,pyim 就可以用这个函数从词库缓存中搜索某个
+code 对应的中文词条了."
+  (pyim-dcache-call-api 'get code from))
 
 
 ;; * Footer
-(provide 'pyim-common)
-
-;;; pyim-common.el ends here
+(provide 'pyim-dcache)
 
+;;; pyim-dcache.el ends here
diff --git a/pyim-dhashcache.el b/pyim-dhashcache.el
index b12f471..de9cffd 100644
--- a/pyim-dhashcache.el
+++ b/pyim-dhashcache.el
@@ -34,6 +34,7 @@
 ;;; Code:
 ;; * 代码                                                                 :code:
 (require 'pyim-common)
+(require 'pyim-dcache)
 (require 'async nil t)
 
 ;; Pyim 词库缓存文件,注意:变量名称中不能出现 ":" 等,不能作为文件名称的字符。
@@ -79,7 +80,7 @@
 如果 FORCE 为真,强制加载缓存。"
   (interactive)
   (when (or force (not pyim-dhashcache-update-ishortcode2word))
-    (if (pyim-use-emacs-thread-p)
+    (if (pyim-dcache-use-emacs-thread-p)
         (make-thread
          `(lambda ()
             (maphash
@@ -141,7 +142,7 @@
 如果 FORCE 为真,强制运行。"
   (interactive)
   (when (or force (not pyim-dhashcache-update-shortcode2word))
-    (if (pyim-use-emacs-thread-p)
+    (if (pyim-dcache-use-emacs-thread-p)
         (make-thread
          `(lambda ()
             (maphash
@@ -269,7 +270,7 @@ DCACHE 是一个 code -> words 的 hashtable.
          (code2word-md5-file (pyim-dhashcache-get-path 
'pyim-dhashcache-code2word-md5)))
     (when (or force (not (equal dicts-md5 (pyim-dcache-get-value-from-file 
code2word-md5-file))))
       ;; use hashtable
-      (if (pyim-use-emacs-thread-p)
+      (if (pyim-dcache-use-emacs-thread-p)
           (make-thread
            `(lambda ()
               (let ((dcache (pyim-dhashcache-generate-dcache-file ',dict-files 
,code2word-file)))
@@ -339,7 +340,7 @@ code 对应的中文词条了。
 如果 FORCE 为真,强制排序。"
   (interactive)
   (when (or force (not pyim-dhashcache-update-icode2word-p))
-    (if (pyim-use-emacs-thread-p)
+    (if (pyim-dcache-use-emacs-thread-p)
         (make-thread
          `(lambda ()
             (maphash
diff --git a/pyim.el b/pyim.el
index 92049e1..9e1cef0 100644
--- a/pyim.el
+++ b/pyim.el
@@ -583,6 +583,7 @@
 (require 'pyim-pinyin)
 (require 'pyim-punctuation)
 (require 'pyim-dict)
+(require 'pyim-dcache)
 (require 'pyim-scheme)
 
 (defgroup pyim nil
@@ -769,15 +770,6 @@ Only useful when use posframe.")
 
 注意:当使用 minibuffer 为选词框时,这个选项才有用处。")
 
-(defcustom pyim-dcache-backend 'pyim-dhashcache
-  "词库后端引擎.负责缓冲词库并提供搜索词的算法.
-可选项为 `pyim-dhashcache' 或 `pyim-dregcache'.
-前者搜索单词速度很快,消耗内存多.  后者搜索单词速度较快,消耗内存少.
-
-`pyim-dregcache' 速度和词库大小成正比.  当词库接近100M大小时,
-在六年历史的笔记本上会有一秒的延迟. 这时建议换用 `pyim-dhashcache'."
-  :type 'symbol)
-
 ;;;###autoload
 (defvar pyim-titles '("PYIM " "PYIM-EN " "PYIM-AU ") "Pyim 在 mode-line 
中显示的名称.")
 
@@ -906,19 +898,6 @@ imobj 组合构成在一起,构成了 imobjs 这个概念。比如:
 (defvar pyim-active-hook nil)
 (defvar pyim-inactive-hook nil)
 
-(defvar pyim-dcache-auto-update t
-  "是否自动创建和更新词库对应的 dcache 文件.
-
-这个变量默认设置为 t, 如果有词库文件添加到 `pyim-dicts' 或者
-`pyim-extra-dicts' 时,pyim 会自动生成相关的 dcache 文件。
-
-一般不建议将这个变量设置为 nil,除非有以下情况:
-
-1. 用户的词库已经非常稳定,并且想通过禁用这个功能来降低
-pyim 对资源的消耗。
-2. 自动更新功能无法正常工作,用户通过手工从其他机器上拷贝
-dcache 文件的方法让 pyim 正常工作。")
-
 (defvar pyim-page-tooltip-posframe-buffer " 
*pyim-page-tooltip-posframe-buffer*"
   "这个变量用来保存做为 page tooltip 的 posframe 的 buffer.")
 
@@ -1192,49 +1171,6 @@ pyim 使用函数 `pyim-start' 启动输入法的时候,会将变量
   (pyim-start "pyim" nil t
               save-personal-dcache refresh-common-dcache))
 
-(defun pyim-dcache-call-api (api-name &rest api-args)
-  "Get backend API named API-NAME then call it with arguments API-ARGS."
-  ;; make sure the backend is load
-  (unless (featurep pyim-dcache-backend)
-    (require pyim-dcache-backend))
-  (let ((func (intern (concat (symbol-name pyim-dcache-backend)
-                              "-" (symbol-name api-name)))))
-    (if (functionp func)
-        (apply func api-args)
-      (when pyim-debug
-        (message "%S 不是一个有效的 dcache api 函数." (symbol-name func))
-        ;; Need to return nil
-        nil))))
-
-(defun pyim-dcache-update-code2word (&optional force)
-  "读取并加载词库.
-
-读取 `pyim-dicts' 和 `pyim-extra-dicts' 里面的词库文件,生成对应的
-词库缓冲文件,然后加载词库缓存。
-
-如果 FORCE 为真,强制加载。"
-  (let* ((dict-files (mapcar #'(lambda (x)
-                                 (unless (plist-get x :disable)
-                                   (plist-get x :file)))
-                             `(,@pyim-dicts ,@pyim-extra-dicts)))
-         (dicts-md5 (pyim-create-dicts-md5 dict-files)))
-    (pyim-dcache-call-api 'update-code2word dict-files dicts-md5 force)))
-
-(defun pyim-dcache-init-variables ()
-  "初始化 dcache 缓存相关变量."
-  (pyim-dcache-call-api 'init-variables))
-
-(defun pyim-dcache-save-caches ()
-  "保存 dcache.
-
-  将用户选择过的词生成的缓存和词频缓存的取值
-  保存到它们对应的文件中.
-
-  这个函数默认作为 `kill-emacs-hook' 使用。"
-  (interactive)
-  (pyim-dcache-call-api 'save-personal-dcache-to-file)
-  t)
-
 (defun pyim-export (file &optional confirm)
   "将个人词条以及词条对应的词频信息导出到文件 FILE.
 
@@ -1287,21 +1223,13 @@ MERGE-METHOD 是一个函数,这个函数需要两个数字参数,代表
   ;; 中读取变量值, 然后再对用户选择过的词生成的缓存排序,如果没
   ;; 有这一步骤,导入的词条就会被覆盖,使用 emacs-thread 机制来更新 dcache
   ;; 不存在此问题。
-  (unless pyim-prefer-emacs-thread
+  (unless pyim-dcache-prefer-emacs-thread
     (pyim-dcache-save-caches))
   ;; 更新相关的 dcache
   (pyim-dcache-call-api 'update-personal-words t)
 
   (message "pyim: 词条相关信息导入完成!"))
 
-;; ** 从词库中搜索中文词条
-(defun pyim-dcache-get (code &optional from)
-  "从 FROM 对应的 dcache 中搜索 CODE, 得到对应的词条.
-
-当词库文件加载完成后,pyim 就可以用这个函数从词库缓存中搜索某个
-code 对应的中文词条了."
-  (pyim-dcache-call-api 'get code from))
-
 (defun pyim-insert-word-into-icode2word (word pinyin prepend)
   (pyim-dcache-call-api 'insert-word-into-icode2word word pinyin prepend))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]