branch: externals/pyim commit 3bbfe10856ff778ae75f32ab2ccd812cc2e958fa Author: Feng Shu <tuma...@163.com> Commit: Feng Shu <tuma...@163.com>
Let pyim-dcache-save-value-to-file more reliable. * pyim-dcache.el (pyim-dcache-save-value-to-file): Let save more reliable. (pyim-dcache-get-value-from-file): do not dump. --- pyim-dcache.el | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/pyim-dcache.el b/pyim-dcache.el index 261d2a3..d6b2ee8 100644 --- a/pyim-dcache.el +++ b/pyim-dcache.el @@ -112,34 +112,41 @@ VARIABLE 变量,FORCE-RESTORE 设置为 t 时,强制恢复,变量原来的 (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))))) + (make-directory (file-name-directory file) t) + (let ((dump-file (concat file "-dump-" (format-time-string "%Y%m%d%H%M%S")))) + (when value + (with-temp-buffer + (insert ";; -*- lisp-data -*-\n") + (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 (let ((print-level nil) + (print-length nil)) + (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:") + (goto-char (point-min)) + (let ((save-silently t)) + ;; 使用 read 读取一下当前 buffer,读取没问题后再保存到 dcache 文件,因 + ;; 为我发现保存的词库文件偶尔会出现 "..." 这样的字符串,可能是 print1 + ;; abbreviating 导致的,但暂时没有发现原因,这个问题非常严重,会导致词 + ;; 库损坏,用户自定义词条丢失。 + (if (ignore-errors (read (current-buffer))) + (pyim-dcache-write-file file) + ;; 如果词库内容有问题,就保存到 dump 文件,这样用户可以通过 dump 文 + ;; 件发现问题原因,需要注意的是,这个操作会丢失当前 sesson 的自定义 + ;; 词条内容。 + (message "PYIM: %S 保存出错,执行 dump 操作!" file) + (pyim-dcache-write-file dump-file))))))) (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)))) + (ignore-errors + (read (current-buffer)))))) ;; ** Dcache 文件处理功能 (defun pyim-dcache-write-file (filename &optional confirm)