Function: dired-insert-subdir

dired-insert-subdir is an autoloaded, interactive and byte-compiled function defined in dired-aux.el.gz.

Signature

(dired-insert-subdir DIRNAME &optional SWITCHES NO-ERROR-IF-NOT-DIR-P)

Documentation

Insert this subdirectory into the same Dired buffer.

If it is already present, overwrite the previous entry;
  otherwise, insert it at its natural place (as ls -lR would
  have done).
With a prefix arg, you may edit the ls switches used for this listing.
  You can add R to the switches to expand the whole tree starting at
  this subdirectory.
This function takes some pains to conform to ls -lR output.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/dired-aux.el.gz
;;;###autoload
(defun dired-insert-subdir (dirname &optional switches no-error-if-not-dir-p)
  "Insert this subdirectory into the same Dired buffer.
If it is already present, overwrite the previous entry;
  otherwise, insert it at its natural place (as `ls -lR' would
  have done).
With a prefix arg, you may edit the `ls' switches used for this listing.
  You can add `R' to the switches to expand the whole tree starting at
  this subdirectory.
This function takes some pains to conform to `ls -lR' output."
  ;; NO-ERROR-IF-NOT-DIR-P needed for special filesystems like
  ;; Prospero where dired-ls does the right thing, but
  ;; file-directory-p has not been redefined.
  (interactive
   (list (dired-get-filename)
	 (if current-prefix-arg
	     (read-string "Switches for listing: "
			  (or dired-subdir-switches dired-actual-switches))))
   dired-mode)
  (setq dirname (file-name-as-directory (expand-file-name dirname)))
  (or no-error-if-not-dir-p
      (file-directory-p dirname)
      (error  "Attempt to insert a non-directory: %s" dirname))
  (let ((elt (assoc dirname dired-subdir-alist))
	(cons (assoc-string dirname dired-switches-alist))
	(modflag (buffer-modified-p))
	(old-switches switches)
	switches-have-R mark-alist case-fold-search buffer-read-only)
    (and (not switches)
         ;; FIXME: This line fixes a bug when entering `C-u i' with
         ;; `dired-auto-toggle-b-switch' non-nil, but the fix is
         ;; controversial so currently not used; see
         ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=81295#20> and
         ;; the following messages and also bug#81522 for discussion.
         ;; (not (dired-switches-escape-p dired-actual-switches))
         cons (setq switches (cdr cons)))
    ;; FIXME: Since the bug results is a bad Dired display, we warn the
    ;; user; remove this warning when an acceptable fix is installed.
    (when (and dired-auto-toggle-b-switch dired-switches-alist
               (directory-files (caar dired-switches-alist) nil "\n")
               (dired-switches-escape-p dired-actual-switches)
               (not (dired-switches-escape-p (cdar dired-switches-alist))))
      (display-warning 'dired
                       "
You have encountered a known Emacs bug
for which there is currently no acceptable solution;
see <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=81295#56>
and the related messages for details."))
    (dired-insert-subdir-validate dirname switches)
    ;; case-fold-search is nil now, so we can test for capital `R':
    (if (setq switches-have-R (and switches (string-match-p "R" switches)))
	;; avoid duplicated subdirs
	(setq mark-alist (dired-kill-tree dirname t)))
    (if elt
	;; If subdir is already present, remove it and remember its marks
	(setq mark-alist (nconc (dired-insert-subdir-del elt) mark-alist))
      (dired-insert-subdir-newpos dirname)) ; else compute new position
    (dired-insert-subdir-doupdate
     dirname elt (dired-insert-subdir-doinsert dirname switches))
    (when old-switches
      (if cons
	  (setcdr cons switches)
	(push (cons dirname switches) dired-switches-alist)))
    (when switches-have-R
      (dired-build-subdir-alist switches)
      (setq switches (string-replace "R" "" switches))
      (dolist (cur-ass dired-subdir-alist)
	(let ((cur-dir (car cur-ass)))
	  (and (dired-in-this-tree-p cur-dir dirname)
	       (let ((cur-cons (assoc-string cur-dir dired-switches-alist)))
		 (if cur-cons
		     (setcdr cur-cons switches)
		   (push (cons cur-dir switches) dired-switches-alist)))))))
    ;; Since we insert subdirs without calling `dired-internal-noselect'
    ;; we also have to test here whether the subdir contains a file name
    ;; with a newline, and if so, pop up a warning.  FIXME: Is there a
    ;; cleaner way to do this?  See bug#81295 and bug#81522.
    (unless (or dired-auto-toggle-b-switch
                (dired-switches-escape-p dired-listing-switches)
                (dired-switches-escape-p dired-actual-switches))
      (when (and (dired--filename-with-newline-p)
                 (eq 0 (call-process insert-directory-program
                                     nil nil nil "-b")))
        (dired--display-filename-with-newline-warning (current-buffer))))
    (dired-initial-position dirname)
    (save-excursion (dired-mark-remembered mark-alist))
    (restore-buffer-modified-p modflag)))