]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix misalignment in Dired when dired-directory is a cons
authorTino Calancha <tino.calancha@gmail.com>
Tue, 1 Aug 2017 15:01:45 +0000 (00:01 +0900)
committerTino Calancha <tino.calancha@gmail.com>
Tue, 1 Aug 2017 15:01:45 +0000 (00:01 +0900)
* 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.

lisp/dired.el
test/lisp/dired-tests.el

index c502dd8a50916236aa7bfe8c879403058e2b1cbc..4f8f615a34b2e3521f50c3addb7b043e22b7ce92 100644 (file)
@@ -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 @@ periodically reverts at specified time intervals."
   :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* ((distance (- (point) (point-at-bol)))
+                    (higher (> distance target)))
+          (setq target distance)))
+      (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)
+          ;; Use point difference instead of `current-column', because
+          ;; the former works when `dired-hide-details-mode' is enabled.
+          (let ((distance (- target (- (point) (point-at-bol))))
+                (inhibit-read-only t))
+            (unless (zerop distance)
+              (re-search-backward regexp nil t)
+              (goto-char (match-beginning 0))
+              (search-backward-regexp "[[:space:]]" nil t)
+              (skip-chars-forward "[:space:]")
+              (insert-char ?\s distance 'inherit))
+            (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).
@@ -940,6 +981,8 @@ periodically reverts at specified time intervals."
          (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))
 
index 8657910a496ec73967076d32adeaa0290c4430a3..047bfdcf71c4c3660d59dcb221d29ca00fa6393d 100644 (file)
 
 (ert-deftest dired-test-bug27762 ()
   "Test for http://debbugs.gnu.org/27762 ."
-  :expected-result :failed
   (require 'ls-lisp)
   (let* ((dir source-directory)
          (default-directory dir)