From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Keith David Bershatsky Newsgroups: gmane.emacs.bugs Subject: bug#31601: Dired/Wdired: Play nicely with recursive list of files and directories. Date: Fri, 25 May 2018 21:36:37 -0700 Message-ID: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1527309371 377 195.159.176.226 (26 May 2018 04:36:11 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 26 May 2018 04:36:11 +0000 (UTC) To: 31601@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat May 26 06:36:06 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fMQw1-0008R8-8b for geb-bug-gnu-emacs@m.gmane.org; Sat, 26 May 2018 06:36:06 +0200 Original-Received: from localhost ([::1]:47674 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fMQy8-0006Jj-8q for geb-bug-gnu-emacs@m.gmane.org; Sat, 26 May 2018 00:38:16 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57513) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fMQxx-0006Jd-SR for bug-gnu-emacs@gnu.org; Sat, 26 May 2018 00:38:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fMQxu-0001MM-Or for bug-gnu-emacs@gnu.org; Sat, 26 May 2018 00:38:05 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:41020) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fMQxu-0001LY-Kq for bug-gnu-emacs@gnu.org; Sat, 26 May 2018 00:38:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1fMQxu-0000eV-Eo for bug-gnu-emacs@gnu.org; Sat, 26 May 2018 00:38:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Keith David Bershatsky Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 26 May 2018 04:38:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 31601 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: X-Debbugs-Original-To: Emacs Bug Reports Original-Received: via spool by submit@debbugs.gnu.org id=B.15273094262434 (code B ref -1); Sat, 26 May 2018 04:38:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 26 May 2018 04:37:06 +0000 Original-Received: from localhost ([127.0.0.1]:48917 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fMQwz-0000dC-VQ for submit@debbugs.gnu.org; Sat, 26 May 2018 00:37:06 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:38152) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fMQwx-0000ce-Cy for submit@debbugs.gnu.org; Sat, 26 May 2018 00:37:04 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fMQwq-0007vu-DT for submit@debbugs.gnu.org; Sat, 26 May 2018 00:36:57 -0400 Original-Received: from lists.gnu.org ([2001:4830:134:3::11]:55837) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fMQwq-0007vm-9W for submit@debbugs.gnu.org; Sat, 26 May 2018 00:36:56 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57344) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fMQwo-0006GG-Oe for bug-gnu-emacs@gnu.org; Sat, 26 May 2018 00:36:56 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fMQwl-0007mb-I8 for bug-gnu-emacs@gnu.org; Sat, 26 May 2018 00:36:54 -0400 Original-Received: from gateway30.websitewelcome.com ([192.185.198.26]:45033) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fMQwl-0007Y1-8F for bug-gnu-emacs@gnu.org; Sat, 26 May 2018 00:36:51 -0400 Original-Received: from cm10.websitewelcome.com (cm10.websitewelcome.com [100.42.49.4]) by gateway30.websitewelcome.com (Postfix) with ESMTP id E274ACEA for ; Fri, 25 May 2018 23:36:38 -0500 (CDT) Original-Received: from gator3053.hostgator.com ([50.87.144.69]) by cmsmtp with SMTP id MQwYfkqJhBcCXMQwYfeVum; Fri, 25 May 2018 23:36:38 -0500 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lawlist.com ; s=default; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:To :From:Message-ID:Date:Sender:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=QKQhYJDH6sfSQiJ91xYDY8u8rbSM3kCslFxXiNUkFOs=; b=DaluByGt3lmfKsxj341OPQSEfa tZPjqam9RRAT25R80GbpXKlFAuW0gSI5GqVuAi6FE0QMLkTXQQYUApgO2ufUhApsZNgyngFgDjVRN EavAcsd7XWGpOD2gWCZCkpsvOE047fhdQExaBE7Lg9ZoKUO8dHcaQlUwKkEaZYfZfpX1pJ0xdajcN D4WxMyzL8xMa0wxCcbL7TYlNf34CBJ2HdbAgytwV3QtUAoXRIi0MisKP2uSJUYdjVB5A3ssyjuayB JJtxQkyq5gjOlxKlEQGffHEHK7VBu0DnUzWe/DPRaOssUfJXM3u8iIs2qXSKttY6l+L4edFOhbylj l+3ffaOQ==; Original-Received: from cpe-45-48-239-195.socal.res.rr.com ([45.48.239.195]:49903 helo=server.local) by gator3053.hostgator.com with esmtpsa (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.89_1) (envelope-from ) id 1fMQwY-002tAV-0W; Fri, 25 May 2018 23:36:38 -0500 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator3053.hostgator.com X-AntiAbuse: Original Domain - gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - lawlist.com X-BWhitelist: no X-Source-IP: 45.48.239.195 X-Source-L: No X-Exim-ID: 1fMQwY-002tAV-0W X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: cpe-45-48-239-195.socal.res.rr.com (server.local) [45.48.239.195]:49903 X-Source-Auth: lawlist X-Email-Count: 2 X-Source-Cap: bGF3bGlzdDtsYXdsaXN0O2dhdG9yMzA1My5ob3N0Z2F0b3IuY29t X-Local-Domain: yes X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:146562 Archived-At: I occasionally have the need to rename files and directories that are rec= ursively located. I create a dired-mode buffer as follows: (dired (directory-files-recursively "/path/to/directory" "" 'include-= directories)) Wdired: The first problem is that `wdired-get-filename' needs to be modi= fied so that it handles absolute paths differently than relative filename= s. Here is an example that works -- using `file-name-absolute-p`: (defun wdired-get-filename (&optional no-dir old) "Return the filename at line. Similar to `dired-get-filename' but it doesn't rely on regexps. It relies on WDired buffer's properties. Optional arg NO-DIR with value non-nil means don't include directory. Optional arg OLD with value non-nil means return old filename." ;; FIXME: Use dired-get-filename's new properties. (let (beg end file) (save-excursion (setq end (line-end-position)) (beginning-of-line) (setq beg (next-single-property-change (point) 'old-name nil end)) (unless (eq beg end) (if old (setq file (get-text-property beg 'old-name)) ;; In the following form changed `(1+ beg)' to `beg' so that ;; the filename end is found even when the filename is empty. ;; Fixes error and spurious newlines when marking files for ;; deletion. (if (=3D (point-at-eol) (point-max)) (setq end (point-max)) ;; fix for @lawlist eliminating final new li= ne at eob. (setq end (next-single-property-change beg 'end-name))) (setq file (buffer-substring-no-properties (1+ beg) end))) (and file (setq file (wdired-normalize-filename file)))) (if (or no-dir old) file (cond ;;; When FILE is relative, concatenate default-directory to beg= inning. ((and file (> (length file) 0) (not (file-name-absolute-p file)) (concat (dired-current-directory) file))) ;;; When FILE is absolute, no need to concatenate the default-d= irectory. ((and file (> (length file) 0) (file-name-absolute-p file) file))))))) Dired: The second problem is a dired-mode problem in that the `dired-dir= ectory' variable is not updated when renaming a file. I haven't tested d= eleting a file, but that probably suffers the same problem. Absent updat= ing the `dired-directory` with the renamed filename, the `revert-buffer` = will encounter errors because `ls` or `gls` will try to find files that n= o longer exist, and that errors will be inserted into the dired- buffer. = A quick solution for renaming the file is to modify `dired-rename-file` = as follows: ;;; (setq mylist '("apple" "pear" "peach" "nectarine" "watermelon")) ;;; (ar-replace--in-list "apple" "cherry" mylist) ;;; Written by @Andreas R=C3=B6hler: https://emacs.stackexchange.com/a/4= 1631/2287 (defun ar-replace--in-list (elem replacement list) "Expects a LIST of strings. ELEM: element to replace by arg REPLACEMENT" (let (newlist) (dolist (ele list) (if (string=3D ele elem) (push replacement newlist) (push ele newlist))) (nreverse newlist))) (defun dired-rename-file (file newname ok-if-already-exists) (dired-handle-overwrite newname) (rename-file file newname ok-if-already-exists) ; error is caught in -c= reate-files ;;; Update the `dired-directory' (when (and (listp dired-directory) (member file dired-directory)) (setq dired-directory (ar-replace--in-list file newname dired-directory))) ;; Silently rename the visited file of any buffer visiting this file. (and (get-file-buffer file) (with-current-buffer (get-file-buffer file) (set-visited-file-name newname nil t))) (dired-remove-file file) ;; See if it's an inserted subdir, and rename that, too. (dired-rename-subdir file newname)) However, this is understandably slow if there are a lot of files. Perhap= s a hash-table system would be better suited to keep track of files and d= irectories instead of just a plain old list with file/directory names. I haven't yet figured out the best approach to modify a wdired-mode buffe= r that has a combination of directories and files. Presumably some porti= on of the absolute filename/directory-name will need read-only attributes= .... But, that is as far as my thinking has gone on this issue. There may be many other situations that I haven't thought of because I've= never used all of the features of dired-mode and/or wdired-mode. Thanks, Keith