emacs-devel
[Top][All Lists]
Advanced

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

Re: on helm substantial differences


From: Juri Linkov
Subject: Re: on helm substantial differences
Date: Fri, 27 Nov 2020 10:45:10 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (x86_64-pc-linux-gnu)

>> How "character2 (\t with :align-to) name2" could predict the right value
>> of this 'align-to' if it doesn't know what value of 'align-to' will use
>> 'completion--insert-strings' in its separator?
>
> Ah, okay.  Would it work to change read-char-by-name to generate
> strings "character1 TAB name1" and "character2 TAB name2", and then
> make completion--insert-strings to produce a suitably calculated
> (SPC with :align-to) separator between them?  Then all you'd need to
> do is set tab-width in the completions buffer.

Thanks, the suggested solution works well.  This screenshot demonstrates
that spacing between a character and its name is the same in the first column
and in the second column:

PNG image

And the patch that implements this:

diff --git a/lisp/international/mule-cmds.el b/lisp/international/mule-cmds.el
index d361971a1f..d59f2c0ebf 100644
--- a/lisp/international/mule-cmds.el
+++ b/lisp/international/mule-cmds.el
@@ -3084,13 +3084,11 @@ ucs-names
         (puthash "BELL (BEL)" ?\a names)
         (setq ucs-names names))))
 
-(defun mule--ucs-names-annotation (name)
-  ;; FIXME: It would be much better to add this annotation before rather than
-  ;; after the char name, so the annotations are aligned.
-  ;; FIXME: The default behavior of displaying annotations in italics
-  ;; doesn't work well here.
-  (let ((char (gethash name ucs-names)))
-    (when char (format " (%c)" char))))
+(defun mule--ucs-names-affixation (names)
+  (mapcar (lambda (name)
+            (let ((char (gethash name ucs-names)))
+              (list name (concat (if char (format "%c" char) " ") "\t") "")))
+          names))
 
 (defun char-from-name (string &optional ignore-case)
   "Return a character as a number from its Unicode name STRING.
@@ -3133,13 +3131,14 @@ read-char-by-name
 as names, not numbers."
   (let* ((enable-recursive-minibuffers t)
         (completion-ignore-case t)
+        (completion-tab-width 4)
         (input
          (completing-read
           prompt
           (lambda (string pred action)
             (if (eq action 'metadata)
                 '(metadata
-                  (annotation-function . mule--ucs-names-annotation)
+                  (affixation-function . mule--ucs-names-affixation)
                   (category . unicode-name))
               (complete-with-action action (ucs-names) string pred)))))
         (char
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 87bf3d36fa..64498b3729 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1134,6 +1134,7 @@ completion--cycle-threshold
 (defvar-local completion-all-sorted-completions nil)
 (defvar-local completion--all-sorted-completions-location nil)
 (defvar completion-cycling nil)      ;Function that takes down the cycling map.
+(defvar completion-tab-width 8)
 
 (defvar completion-fail-discreetly nil
   "If non-nil, stay quiet when there  is no match.")
@@ -1718,6 +1719,10 @@ completion--insert-strings
           (row 0)
            (first t)
           (laststring nil))
+      (unless (or tab-stop-list (zerop (mod colwidth completion-tab-width)))
+        ;; Align to tab positions for the case
+        ;; when the caller uses tabs inside prefix.
+        (setq colwidth (- colwidth (mod colwidth completion-tab-width))))
       ;; The insertion should be "sensible" no matter what choices were made
       ;; for the parameters above.
       (dolist (str strings)
@@ -1758,9 +1763,10 @@ completion--insert-strings
                  ;; already past the goal column, there is still
                  ;; a space displayed.
                  (set-text-properties (1- (point)) (point)
-                                      ;; We can't just set tab-width, because
-                                      ;; completion-setup-function will kill
-                                      ;; all local variables :-(
+                                      ;; We can set tab-width using
+                                      ;; completion-tab-width, but
+                                      ;; the caller can prefer using
+                                      ;; \t to align prefixes.
                                       `(display (space :align-to ,column)))
                  nil))))
             (setq first nil)
diff --git a/lisp/simple.el b/lisp/simple.el
index c9f4f2bb44..8cd5de20d7 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -8822,6 +8822,8 @@ completion-setup-function
             insert-fun))
       (set (make-local-variable 'completion-reference-buffer) mainbuf)
       (if base-dir (setq default-directory base-dir))
+      (when completion-tab-width
+        (setq tab-width completion-tab-width))
       ;; Maybe insert help string.
       (when completion-show-help
        (goto-char (point-min))

reply via email to

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