emacs-devel
[Top][All Lists]
Advanced

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

Re: Entering emojis


From: Lars Ingebrigtsen
Subject: Re: Entering emojis
Date: Wed, 27 Oct 2021 14:18:40 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Lars Ingebrigtsen <larsi@gnus.org> writes:

> I haven't looked at transient.el internals in details, but extending a
> columnar display to variable-pitch circumstances usually isn't a lot of
> work: You basically just use window-text-pixel-size to get the width of
> the texts, and align with an :align-to display spec.
>
> Have you looked at extending transient this way?

I've now done so -- by adding a new slot to the transient-prefix class.
I don't know whether that's the most likely place to stash the info, but
this seems to work.  Perhaps stashing it in transient-columns would make
more sense...  Or at least propagating it there.

What do you think?  (It should be fully backwards compatible -- the
output when not supplying this keyword should be identical to the old
output.)

diff --git a/lisp/transient.el b/lisp/transient.el
index c33a4c722a..13dfbf53ec 100644
--- a/lisp/transient.el
+++ b/lisp/transient.el
@@ -590,7 +590,8 @@ transient-prefix
    (transient-suffix     :initarg :transient-suffix     :initform nil)
    (transient-non-suffix :initarg :transient-non-suffix :initform nil)
    (incompatible         :initarg :incompatible         :initform nil)
-   (suffix-description   :initarg :suffix-description))
+   (suffix-description   :initarg :suffix-description)
+   (variable-pitch       :initarg :variable-pitch       :initform nil))
   "Transient prefix command.
 
 Each transient prefix command consists of a command, which is
@@ -2941,6 +2942,18 @@ transient--insert-group
       (unless (string-match-p ".\n\\'" str)
         (insert ?\n)))))
 
+(defun transient--pixel-width (string)
+  (with-temp-buffer
+    (insert string)
+    (if (not (get-buffer-window (current-buffer)))
+        (save-window-excursion
+          ;; Avoid errors if the selected window is a dedicated one,
+          ;; and they just want to insert a document into it.
+          (set-window-dedicated-p nil nil)
+         (set-window-buffer nil (current-buffer))
+         (car (window-text-pixel-size nil (line-beginning-position) (point))))
+      (car (window-text-pixel-size nil (line-beginning-position) (point))))))
+
 (cl-defmethod transient--insert-group ((group transient-columns))
   (let* ((columns
           (mapcar
@@ -2951,11 +2964,18 @@ transient--insert-group
                  (push desc rows))
                rows))
            (oref group suffixes)))
+         (vp (oref transient--prefix variable-pitch))
          (rs (apply #'max (mapcar #'length columns)))
          (cs (length columns))
-         (cw (mapcar (lambda (col) (apply #'max (mapcar #'length col)))
+         (cw (mapcar (lambda (col)
+                       (apply #'max (mapcar (if vp
+                                                #'transient--pixel-width
+                                              #'length)
+                                            col)))
                      columns))
-         (cc (transient--seq-reductions-from (apply-partially #'+ 3) cw 0)))
+         (cc (transient--seq-reductions-from
+              (apply-partially #'+ (if vp 30 3))
+              cw 0)))
     (if transient-force-single-column
         (dotimes (c cs)
           (dotimes (r rs)
@@ -2966,11 +2986,21 @@ transient--insert-group
             (insert ?\n)))
       (dotimes (r rs)
         (dotimes (c cs)
-          (insert (make-string (- (nth c cc) (current-column)) ?\s))
-          (when-let ((cell (nth r (nth c columns))))
-            (insert cell))
-          (when (= c (1- cs))
-            (insert ?\n)))))))
+          (if vp
+              (progn
+                (when-let ((cell (nth r (nth c columns))))
+                  (insert cell))
+                (if (= c (1- cs))
+                    (insert ?\n)
+                  (insert
+                   (propertize
+                   " " 'display
+                    `(space :align-to (,(nth (1+ c) cc)))))))
+            (insert (make-string (- (nth c cc) (current-column)) ?\s))
+            (when-let ((cell (nth r (nth c columns))))
+              (insert cell))
+            (when (= c (1- cs))
+              (insert ?\n))))))))
 
 (cl-defmethod transient--insert-group ((group transient-subgroups))
   (let* ((subgroups (oref group suffixes))


-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



reply via email to

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