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

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

[elpa] externals/pyim 588bb4d1a5: Let popon page tooltip support backgro


From: ELPA Syncer
Subject: [elpa] externals/pyim 588bb4d1a5: Let popon page tooltip support background color.
Date: Thu, 23 Jun 2022 21:57:48 -0400 (EDT)

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

    Let popon page tooltip support background color.
---
 pyim-page.el        | 29 +++++++++++++++++++++++++++--
 tests/pyim-tests.el | 16 ++++++++++++++++
 2 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/pyim-page.el b/pyim-page.el
index e578a5e2d0..6e44ca3ee7 100644
--- a/pyim-page.el
+++ b/pyim-page.el
@@ -55,7 +55,7 @@
    这个选项可以在 emacs 图形版和终端版使用,速度没有 posframe 快,
    偶尔会遇到选词框错位的问题。
 3. 当这个变量取值为 popon 时,使用 popon 包来绘制选词框,这个选项
-   效果类似 popup, 不过目前不支持背景颜色。
+   效果类似 popup。
 4. 当这个变量取值为 minibuffer 时,minibuffer 将做为选词框,
    这个选项也作为其他选项不可用时的 fallback.
 5. 当这个变量的取值是为一个 list 时,pyim 将按照优先顺序动态
@@ -376,7 +376,32 @@ pyim-page 的核心的功能,为此增加代码的复杂度和测试的难度
          (x (car x-y))
          (y (cdr x-y)))
     (setq pyim-page-last-popon
-          (popon-create string (cons x (+ y 1))))))
+          (popon-create
+           (pyim-page--add-default-page-face
+            (pyim-page--align-lines string))
+           (cons x (+ y 1))))))
+
+(defun pyim-page--add-default-page-face (string)
+  "为 STRING 添加默认 page face."
+  (with-temp-buffer
+    (insert string)
+    (add-face-text-property
+     (point-min) (point-max) 'pyim-page t)
+    (buffer-string)))
+
+(defun pyim-page--align-lines (string)
+  "用空格将 STRING 的每一行都对齐。"
+  (let* ((lines (split-string string "\n"))
+         (widths (mapcar #'string-width lines))
+         (max-width (apply #'max widths))
+         (new-lines
+          (mapcar (lambda (line)
+                    (concat line
+                            (make-string
+                             (- max-width (string-width line))
+                             ?\ )))
+                  lines)))
+    (string-join new-lines "\n")))
 
 (cl-defgeneric pyim-page-info-format (style page-info)
   "将 PAGE-INFO 按照 STYLE 格式化为选词框中显示的字符串。")
diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el
index e055b75f71..68fca132df 100644
--- a/tests/pyim-tests.el
+++ b/tests/pyim-tests.el
@@ -1717,6 +1717,22 @@ Transfer-Encoding: chunked
   )
 
 ;; ** pyim-probe 相关单元测试
+(ert-deftest pyim-tests-pyim-page--add-default-page-face ()
+  (let ((string (pyim-page--add-default-page-face
+                 (concat "aaa\n"
+                         (propertize "bbb\n" 'face 'pyim-page-select-word)
+                         "ccc"))))
+    (should (equal (get-text-property 0 'face string) 'pyim-page))
+    (should (equal (get-text-property 1 'face string) 'pyim-page))
+    (should (equal (get-text-property 10 'face string) 'pyim-page))
+    (should (equal (get-text-property 4 'face string) '(pyim-page-select-word 
pyim-page)))))
+
+(ert-deftest pyim-tests-pyim-page--align-lines ()
+  (should (equal (pyim-page--align-lines "你好
+abc 这是")
+                 "你好    
+abc 这是")))
+
 (ert-deftest pyim-tests-pyim-page-tooltip-valid-p ()
   (let ((pyim-page-tooltip-infos
          `((posframe1 :package posframe1)



reply via email to

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