Subject: bug#27762: 26.0.50; ls-lisp: misalignment when dired-directory is a cons

From: Tino Calancha <[email-masked]>
Date: Fri, 21 Jul 2017 13:55:26 +0900

> With emacs -Q:
>
> (let* ((dir (expand-file-name "src" source-directory))
>          (default-directory dir))
>   (dired (list dir "cygw32.c" "alloc.c" "w32xfns.c" "xdisp.c")) ; Wrong aligment.
>   ;; Following just fix the first file, but xdisp.c keeps misaligned.
>   (dired-goto-file (expand-file-name "cygw32.c"))
>   (forward-line 0)
>   (let ((inhibit-read-only t))
>     (dired-align-file (point) (point-max))))
>
>
> Note that when i eval the previous form GNU 'ls' is used, and i see misalignment.
> I would expect `dired-align-file' fix this problem, but i doesn't.
>
> *) Since this is not specific of `ls-lisp', one alternative fix could
>    be to run `dired-align-file' at the end of `dired-internal-noselect'
>    when `dired-directory' is a cons.
>
> **) First, we must fix `dired-lign-file', which is not doing its job
>     in the snippet above.
Eli, how do you think about following patch?

--8<-----------------------------cut here---------------start------------->8---
commit 458fe93358594cf7338180b6feb54eb37d28ed21
Author: Tino Calancha <[email-masked]>
Date:   Fri Jul 21 13:50:56 2017 +0900

    Fix misalignment in Dired when dired-directory is a cons
    
    * lisp/dired.el (dired--need-align-p, dired--align-all-files):
    New defuns.
    (dired-internal-noselect): Call dired--align-all-files when
    dired-directory is a cons (Bug#27762).
    * test/lisp/dired-tests.el (dired-test-bug27762): Test should pass.

diff --git a/lisp/dired.el b/lisp/dired.el
index 9d500a9f52..371af15f79 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -34,6 +34,7 @@
 
 ;;; Code:
 
+(eval-when-compile (require 'subr-x))
 ;; When bootstrapping dired-loaddefs has not been generated.
 (require 'dired-loaddefs nil t)
 
@@ -871,6 +872,46 @@ dired-auto-revert-buffer
   :group 'dired
   :version "23.2")
 
+(defun dired--need-align-p ()
+  "Return non-nil if some file names are misaligned.
+The return value is the target column for the file names."
+  (save-excursion
+    (goto-char (point-min))
+    (dired-goto-next-file)
+    ;; Use point difference instead of `current-column', because
+    ;; the former works when `dired-hide-details-mode' is enabled.
+    (let* ((first (- (point) (point-at-bol)))
+           (target first))
+      (while (and (not (eobp))
+                  (progn
+                    (forward-line)
+                    (dired-move-to-filename)))
+        (when-let* ((col-diff (- (point) (point-at-bol)))
+                    (higher (> col-diff target)))
+          (setq target col-diff)))
+      (and (/= first target) target))))
+
+(defun dired--align-all-files ()
+  "Align all files adding spaces in front of the size column."
+  (let ((target (dired--need-align-p))
+        (regexp directory-listing-before-filename-regexp))
+    (when target
+      (save-excursion
+        (goto-char (point-min))
+        (dired-goto-next-file)
+        (while (dired-move-to-filename)
+          (let ((col-diff (- target (- (point) (point-at-bol))))
+                (inhibit-read-only t)
+                props)
+            (unless (zerop col-diff)
+              (re-search-backward regexp nil t)
+              (goto-char (match-beginning 0))
+              (search-backward-regexp "[[:space:]]" nil t)
+              (setq props (text-properties-at (1- (point))))
+              (skip-chars-forward "[:space:]")
+              (insert (apply #'propertize (make-string col-diff ?\s) props)))
+            (forward-line)))))))
+
 (defun dired-internal-noselect (dir-or-list &optional switches mode)
   ;; If DIR-OR-LIST is a string and there is an existing dired buffer
   ;; for it, just leave buffer as it is (don't even call dired-revert).
@@ -939,6 +980,8 @@ dired-internal-noselect
 	  (if failed (kill-buffer buffer))))
       (goto-char (point-min))
       (dired-initial-position dirname))
+    (when (consp dired-directory)
+      (dired--align-all-files))
     (set-buffer old-buf)
     buffer))
 
diff --git a/test/lisp/dired-tests.el b/test/lisp/dired-tests.el
index bd1816172e..d5f999aab6 100644
--- a/test/lisp/dired-tests.el
+++ b/test/lisp/dired-tests.el
@@ -147,7 +147,6 @@
 
 (ert-deftest dired-test-bug27762 ()
   "Test for http://debbugs.gnu.org/27762 ."
-  :expected-result :failed
   (let* ((dir source-directory)
          (default-directory dir)
          (files (mapcar (lambda (f) (concat "src/" f))

--8<-----------------------------cut here---------------end--------------->8---
In GNU Emacs 26.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.11)
 of 2017-07-21
Repository revision: 1d559e384b467b3f74e8b78695f124b561c884d9