Subject: bug#14791: 24.3.50; [PATCH] find-dired doesn't work correctly

From: Shigeru Fukaya <[email-masked]>
Date: Fri, 05 Jul 2013 02:12:56 +0900

find-dired doesn't work well, at least for GNU find and ls output.

It is `find-dired-filter' doesn't format the process output correctly.
Its hard-coded regexp never match `find DIR -ls' output.
The function also fails right-adjusting filesize columns of `ls -ld'
output.

The following sample is a changed code that handles GNU find and ls
output.

Regards,
Shigeru

;;; find-dired.el

(defun find-dired-filter (proc string)
  ;; Filter for \\[find-dired] processes.
  (let ((buf (process-buffer proc))
	(inhibit-read-only t))
    (if (buffer-name buf)
	(with-current-buffer buf
	  (save-excursion
	    (save-restriction
	      (widen)
	      (let ((beg (point-max))
		    (l-opt (and (consp find-ls-option)
				(string-match "l" (cdr find-ls-option))))
		    ls-regexp
		    ls-replace)
		(when l-opt
		  (if (equal "-ls" (car find-ls-option))
		      (setq ls-regexp (concat
				       ;; 1 - inode,allocSize to remove
				       "^ +\\([0-9]+ +[0-9]+ +\\)"
				       ;; modes
				       "[^ \t\r\n]+"
				       ;; 3 - # of links
				       "\\( +\\([0-9]+\\) +\\)"
				       ;; 5 - uid,gid
				       "\\(\\([^ \t\r\n]+ +[^ \t\r\n]+\\)"
				       ;; 6 - filesize
				       " +\\([0-9.]+[^ ]?\\)\\)")
			    ls-replace
			    #'(lambda ()
				(replace-match "" nil t nil 1)
				(replace-match (format " %3s "
						       (match-string 3))
					       nil t nil 2)
				;; Try justification of uid,gid and filesize
				(let ((len (+ (- (match-end 5)
						 (match-beginning 5))
					      (- (match-end 6)
						 (match-beginning 6)))))
				  (replace-match (concat
						  (match-string 5)
						  (make-string
						   (max (- 30 len) 1) ?\s)
						  (match-string 6))
						 nil t nil 4))))
		    (setq ls-regexp
			  ;; modes, 2 - # of links
			  "^ +[^ \t\r\n]+\\( +\\([0-9]+\\) +\\)"
			  ls-replace
			  #'(lambda ()
			      (replace-match (format " %3s " (match-string 2))
					     nil t nil 1)))))
		(goto-char beg)
		(insert string)
		(goto-char beg)
		(while (re-search-forward "^." nil t)
		  (backward-char)
		  (insert "  "))
		;; Convert ` ./FILE' to ` FILE'
		;; This would lose if the current chunk of output
		;; starts or ends within the ` ./', so back up a bit:
		(goto-char (- beg 3))	; no error if < 0
		(while (search-forward " ./" nil t)
		  (delete-char -2))
		;; Pad the number of links and file size.  This is a
		;; quick and dirty way of getting the columns to line up
		;; most of the time, but it's not foolproof.
		(when l-opt
		  (goto-char beg)
		  (goto-char (line-beginning-position))
		  (while (re-search-forward ls-regexp nil t)
		    (funcall ls-replace)))
		;; Find all the complete lines in the unprocessed
		;; output and process it to add text properties.
		(goto-char (point-max))
		(if (search-backward "\n" (process-mark proc) t)
		    (progn
		      (dired-insert-set-properties (process-mark proc)
						   (1+ (point)))
		      (move-marker (process-mark proc) (1+ (point)))))))))
      ;; The buffer has been killed.
      (delete-process proc))))