Hello,
Can you review and comment on the below patch. I came up with a necessity to copy symlinks from one dired buffer to another dired buffer as a dereferenced copy of that symlink.
In terminal, I would use the -L option of the cp command to do the same.
The current default behavior is unchanged as the new defcustom dired-copy-dereference is set to nil.
From 4c4185b9d4df2dcbbb6e1e7fa8a21b75e84ca79f Mon Sep 17 00:00:00 2001
Date: Wed, 30 Nov 2016 12:52:42 -0500
Subject: [PATCH] Dired can now dereference symlinks when copying
* lisp/dired.el (dired-copy-dereference): New defcustom, defaults to
nil.
* lisp/dired-aux.el (dired-copy-file-recursive): Add new optional
argument `dereference'.
* lisp/dired-aux.el (dired-copy-file): Use `dired-copy-dereference' as
the `dereference' argument to `dired-copy-file-recursive'.
---
lisp/dired-aux.el | 8 +++++---
lisp/dired.el | 6 ++++++
2 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el
index f94e053..149c540 100644
--- a/lisp/dired-aux.el
+++ b/lisp/dired-aux.el
@@ -1507,12 +1507,13 @@ dired-handle-overwrite
(defun dired-copy-file (from to ok-flag)
(dired-handle-overwrite to)
(dired-copy-file-recursive from to ok-flag dired-copy-preserve-time t
- dired-recursive-copies))
+ dired-recursive-copies dired-copy-dereference))
(declare-function make-symbolic-link "fileio.c")
(defun dired-copy-file-recursive (from to ok-flag &optional
- preserve-time top recursive)
+ preserve-time top recursive
+ dereference)
(when (and (eq t (car (file-attributes from)))
(file-in-directory-p to from))
(error "Cannot copy `%s' into its subdirectory `%s'" from to))
@@ -1524,7 +1525,8 @@ dired-copy-file-recursive
(copy-directory from to preserve-time)
(or top (dired-handle-overwrite to))
(condition-case err
- (if (stringp (car attrs))
+ (if (and (not dereference)
+ (stringp (car attrs)))
;; It is a symlink
(make-symbolic-link (car attrs) to ok-flag)
(copy-file from to ok-flag preserve-time))
diff --git a/lisp/dired.el b/lisp/dired.el
index daa6d77..ca7ab28 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -191,6 +191,12 @@ dired-copy-preserve-time
:type 'boolean
:group 'dired)
+(defcustom dired-copy-dereference nil
+ "If non-nil, Dired dereferences symlinks when copying them.
+This is similar to the \"-L\" option for the \"cp\" shell command."
+ :type 'boolean
+ :group 'dired)
+ ;
; These variables were deleted and the replacements are on files.el.
; We leave aliases behind for back-compatibility.
(defvaralias 'dired-free-space-program 'directory-free-space-program)
--
2.10.0