]> code.delx.au - gnu-emacs/blobdiff - lisp/ls-lisp.el
Merge from emacs-24; up to 2012-12-30T19:34:25Z!jan.h.d@swipnet.se
[gnu-emacs] / lisp / ls-lisp.el
index 14a8cabf1a74384a1b892bd751aed3cb9842da8f..82a78545d6298270aa281d1ae871b639ad6d746a 100644 (file)
@@ -1,6 +1,6 @@
 ;;; ls-lisp.el --- emulate insert-directory completely in Emacs Lisp
 
-;; Copyright (C) 1992, 1994, 2000-201 Free Software Foundation, Inc.
+;; Copyright (C) 1992, 1994, 2000-2013 Free Software Foundation, Inc.
 
 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
 ;; Modified by: Francis J. Wright <F.J.Wright@maths.qmw.ac.uk>
@@ -331,6 +331,7 @@ not contain `d', so that a full listing is expected."
             ;; do all bindings here for speed
             total-line files elt short file-size attr
             fuid fgid uid-len gid-len)
+       (setq file-alist (ls-lisp-sanitize file-alist))
        (cond ((memq ?A switches)
               (setq file-alist
                     (ls-lisp-delete-matching "^\\.\\.?$" file-alist)))
@@ -403,6 +404,13 @@ not contain `d', so that a full listing is expected."
                ;; the wildcard; let's say something similar.
                (insert "(No match)\n"))
            (insert (format "total %.0f\n" (fceiling (/ sum 1024.0))))))
+       ;; dired-insert-directory expects to find point after the
+       ;; text.  But if the listing is empty, as e.g. in empty
+       ;; directories with -a removed from switches, point will be
+       ;; before the inserted text, and dired-insert-directory will
+       ;; not indent the listing correctly.  Going to the end of the
+       ;; buffer fixes that.
+       (unless files (goto-char (point-max)))
        (if (memq ?R switches)
            ;; List the contents of all directories recursively.
            ;; cadr of each element of `file-alist' is t for
@@ -437,6 +445,22 @@ not contain `d', so that a full listing is expected."
        (message "%s: doesn't exist or is inaccessible" file)
        (ding) (sit-for 2)))))          ; to show user the message!
 
+(defun ls-lisp-sanitize (file-alist)
+  "Sanitize the elements in FILE-ALIST.
+Fixes any elements in the alist for directory entries whose file
+attributes are nil (meaning that `file-attributes' failed for
+them).  This is known to happen for some network shares, in
+particular for the \"..\" directory entry.
+
+If the \"..\" directory entry has nil attributes, the attributes
+are copied from the \".\" entry, if they are non-nil.  Otherwise,
+the offending element is removed from the list, as are any
+elements for other directory entries with nil attributes."
+  (if (and (null (cdr (assoc ".." file-alist)))
+          (cdr (assoc "." file-alist)))
+      (setcdr (assoc ".." file-alist) (cdr (assoc "." file-alist))))
+  (rassq-delete-all nil file-alist))
+
 (defun ls-lisp-column-format (file-alist)
   "Insert the file names (only) in FILE-ALIST into the current buffer.
 Format in columns, sorted vertically, following GNU ls -C.