From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Arthur Miller Newsgroups: gmane.emacs.devel Subject: Lazy wdired preprocessing - Question Date: Wed, 24 Mar 2021 21:20:29 +0100 Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13242"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: emacs-devel@gnu.org To: monnier@iro.umontreal.ca Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Mar 24 21:37:11 2021 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lPAFa-0003Ka-QK for ged-emacs-devel@m.gmane-mx.org; Wed, 24 Mar 2021 21:37:11 +0100 Original-Received: from localhost ([::1]:36708 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lPAFZ-0002qG-Mc for ged-emacs-devel@m.gmane-mx.org; Wed, 24 Mar 2021 16:37:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44438) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPAE9-0001nt-Lw for emacs-devel@gnu.org; Wed, 24 Mar 2021 16:35:41 -0400 Original-Received: from mail-vi1eur05olkn2099.outbound.protection.outlook.com ([40.92.90.99]:18176 helo=EUR05-VI1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPAE4-0006vI-37 for emacs-devel@gnu.org; Wed, 24 Mar 2021 16:35:41 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iJjV3Gs2y2I9oK0JSlDtstSEEHaCqI5TP88r5p4uceHeLN0zi1fYrRz7U1RUDXv+ZmeFTyYkh8k3YzZnW1oxv/+M7ZtX9wQeW3jeC6lJkUsLH1Y9hIdjNQp0imRNjf8UL+7eMeHfQEKbPdc7rhBAwn4THJI00rG1QSpgCvyWwLajrqDIGVvJD34060J8F2p4Z63M14sM47bz/9XjHmYGkj5J/rIEgl94IxVCQjyS4PNgbyX/RLYzzZGlIN9XETqqqyiEcdiO29Grw4bsCeQfnb4ZjOzOjLD+reLEutH5jxGWx6olhpb/cEK1z+mPJxu/VRgEY6sIoix0ogz10q2Ijg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=OyFJjDxE8cEokKLaJEWSrypYBw1l8/XsBbWW9LEVV7c=; b=AKHtuhP9zqUg1amQDEpz7d+wMQI6GNf544xFrKanRTviPCZs730yx3+XchWDg7saRC1GaShq1w7D9WeR959jbn0XOIeUsEssrasVdL5fxiGdZW6rjclbRLEUNlF97zM2QrWEaJ0d2Sf1/2DzWWsqf2Ci8HMpdpiP+i1Nl0P526ty8nD8sVqVGCMVcRoT9W5lysRRWhUExidE9XQRp0tyLNHUwuGT6srDUIIblfgqgmzv8RqXiNZ6yJjzKJrdyl+oISPpKcostFqgVPEkAFHo1xrjmORwgjMc5HCow//DYAXGiHpZMjmmgJh4gKVpdzUm1xZRaGgksLXtblrfIFDzHw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=live.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=OyFJjDxE8cEokKLaJEWSrypYBw1l8/XsBbWW9LEVV7c=; b=uzzWaR8daigZ1mPx2E3A33tcNmFMNzTI3Aiqzyppo3FtAoV5kewf+C/Vdp0C9EMKBtVfBEdtsAaj6Q0NyFbOb4kbU6LoEMjWUJpv2LBlHkIO7XgoyyBjQMJrm8lWN+EaWA1uDDfVcDLTVYxw10Ca92c/MMI4p2PFei9gt6EumbjhN24JBWMfYklpvJL8rXVeO1D1NP47xw11eCYq3rs80m38VLgYTUQEV+ABrOIz1aM9hRiz8Mz1Nsx4opeFaOp6apvONAjNTmsp3cNv0TqUr3CM1beuTTWqY0ffNusyI8FLEQ4hqZjud2KmylErFUSpMpB5/7/RHfY6uLIV+T/2VA== Original-Received: from VI1EUR05FT004.eop-eur05.prod.protection.outlook.com (2a01:111:e400:fc12::42) by VI1EUR05HT246.eop-eur05.prod.protection.outlook.com (2a01:111:e400:fc12::429) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18; Wed, 24 Mar 2021 20:20:30 +0000 Original-Received: from AM9PR09MB4977.eurprd09.prod.outlook.com (2a01:111:e400:fc12::4b) by VI1EUR05FT004.mail.protection.outlook.com (2a01:111:e400:fc12::283) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 20:20:30 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:F109569769F1D915C6B90DE72E3E3499BCEC3E7301EBF9428D7CD936D4DE79AC; UpperCasedChecksum:D018248AD8D85F77E6FC15121791E4BA0FB45384F6F49CC12757793DF43D6D20; SizeAsReceived:7241; Count:44 Original-Received: from AM9PR09MB4977.eurprd09.prod.outlook.com ([fe80::2103:e705:bc0c:5a8b]) by AM9PR09MB4977.eurprd09.prod.outlook.com ([fe80::2103:e705:bc0c:5a8b%6]) with mapi id 15.20.3977.025; Wed, 24 Mar 2021 20:20:30 +0000 X-TMN: [FU9oPpkOjG6drNMz9yfZ3NOd4Y8O7tHY] X-ClientProxiedBy: AM6P195CA0015.EURP195.PROD.OUTLOOK.COM (2603:10a6:209:81::28) To AM9PR09MB4977.eurprd09.prod.outlook.com (2603:10a6:20b:304::20) X-Microsoft-Original-Message-ID: <875z1guwvm.fsf@live.com> X-MS-Exchange-MessageSentRepresentingType: 1 Original-Received: from pascal.homepc (90.230.29.56) by AM6P195CA0015.EURP195.PROD.OUTLOOK.COM (2603:10a6:209:81::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 20:20:30 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 44 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: 965bf681-bdd6-48af-a926-08d8ef02453a X-MS-TrafficTypeDiagnostic: VI1EUR05HT246: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pW4DBSXK6tPnz2XBCdD7tCImAKmHreQuvEhGA2kr/ZkTdWWMhAK94gwzPAqQRNOr6KniejU4ORelJwgTnOtVypkRuKS+r7zH7HEQi08pLSh/C3MNAgOPVm5V//HmF8ZAUbQ3us4BIVCnfqQDwSX8H8c0b6zHt8R7uGeAkQ3HAIQ+Nnt0xGRNVqxCBacFYKWqM8HMsBNu5o/RCG8o7lDNnXYvXrVBskx3RWufQPEwytDpJ6Z0IgmopC3WnD+olyuHExh7N+IlZu/2kTEBMXLK1okhEfOVHTJIqnhP7MBaHlTsMExhaGJk3IU4YVhTYmT50g/97/jHtTd33AF67yxd5JXLpnyrFP4nMmjNqdegEHBg2Jn/sOSBvRPqfQvaqOO26DPJWCVZCAFHAIWTvds6+A== X-MS-Exchange-AntiSpam-MessageData: AYSoNBt+NhS9gEEIe7/xUpI5hgdhUWNgB2oW+pUZIDIf7LSx1qov5w/vBzl45Y2ixtVrERNbjPjt3oRZgCgpFmSNxYieyrZJNpg8DoVd0fmad2Xi5QhTZckX1SNYRKcT73xDJpaqCqcLymi2v44CyQ== X-OriginatorOrg: live.com X-MS-Exchange-CrossTenant-Network-Message-Id: 965bf681-bdd6-48af-a926-08d8ef02453a X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 20:20:30.6765 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: VI1EUR05FT004.eop-eur05.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: Internet X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1EUR05HT246 Received-SPF: pass client-ip=40.92.90.99; envelope-from=arthur.miller@live.com; helo=EUR05-VI1-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:266980 Archived-At: --=-=-= Content-Type: text/plain I hope I don't bother, too much. There is some problem I see when building Emacs with the patch: It builds fine, but when I test it, it seems that I need to re-eval after Emacs started wdired.el in order to get it to work properly. What happeneds it that Emacs seems to run self-insert command upon entering wdired mode anyway, despite being build with patched wdired. However when I eval my working file "lazy-wdired.el" it works fine. The code is identical as one in the patch. I also noticed when I unload-feature lazy-wdired, so I unload all those wdired definitions, and if I eval patched wdired.el from lisp directory which I built Emacs with, everything also works correctly too. I am little bit confused, why do I need to re-evaluate wdired.el in a freshly patched and recompiled Emacs to get this to work? -------------------- Start of forwarded message -------------------- From: Arthur Miller To: emacs-devel@gnu.org Subject: Patch: Lazy wdired preprocessing Date: Tue, 23 Mar 2021 15:52:08 +0100 --=-=-= Content-Type: multipart/mixed; boundary="==-=-=" --==-=-= Content-Type: text/plain Content-Disposition: inline The original thread seems to have become more of a lisp language thread, so to make it easier, I made a new one, with more descriptive name. Attached is new a version of patch. Seems to correctly work with permissions now. I have also discovered (current-column) function so I no longer calculate columns in my own code :-). I have tested on my ext4 file system where my normal system is installed, and there it works correctly. I have also tested on one NTFS drive, where I can not change permissions normally and there is following behaviour, don't know if it is acceptable or not: In wdired buffer I can seemingly change permission, but when I save buffer, then permission is of course not saved and reverted dired buffer shows correct permission. In Dolphin, it seems to work similar (I can set pulldown menu in permissions dialog to only read, and press OK, but it is not saved). There is also a question what happeneds when one have an empty folder and switches to wdired buffer. I don't see it handled in old wdired, so I have added an error condition for that in wdired-change-to-wdired-mode. Please test it, there might be some other test condition I haven't tested. I am attaching both the working file for easy evaluation and test, and the patch. best regards /a --==-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Lazy-wdired-preprocessing.patch >From a0f6a988576c6083b86e3b01e47c4967194e2822 Mon Sep 17 00:00:00 2001 From: Arthur Miller Date: Tue, 23 Mar 2021 15:38:43 +0100 Subject: [PATCH] Lazy wdired preprocessing. --- lisp/wdired.el | 191 +++++++++++++++++++++++++++++-------------------- 1 file changed, 113 insertions(+), 78 deletions(-) diff --git a/lisp/wdired.el b/lisp/wdired.el index 43026d4bb7..b1ba815a68 100644 --- a/lisp/wdired.el +++ b/lisp/wdired.el @@ -189,6 +189,8 @@ wdired-mode-hook ;; Local variables (put here to avoid compilation gripes) (defvar wdired-col-perm) ;; Column where the permission bits start +(defvar wdired-perm-beg) ;; Column where the permission bits start +(defvar wdired-perm-end) ;; Column where the permission bits stop (defvar wdired-old-content) (defvar wdired-old-point) (defvar wdired-old-marks) @@ -235,6 +237,8 @@ wdired-change-to-wdired-mode (setq-local wdired-old-marks (dired-remember-marks (point-min) (point-max))) (setq-local wdired-old-point (point)) + (setq-local wdired-perm-beg nil) + (setq-local wdired-perm-end nil) (setq-local query-replace-skip-read-only t) (add-function :after-while (local 'isearch-filter-predicate) #'wdired-isearch-filter-read-only) @@ -243,22 +247,25 @@ wdired-change-to-wdired-mode (setq buffer-read-only nil) (dired-unadvertise default-directory) (add-hook 'kill-buffer-hook #'wdired-check-kill-buffer nil t) + (add-hook 'before-change-functions #'wdired--before-change-fn nil t) (add-hook 'after-change-functions #'wdired--restore-properties nil t) + ;;(advice-add 'self-insert-command :around #'wdired--self-insert-advice) (setq major-mode 'wdired-mode) (setq mode-name "Editable Dired") - (add-function :override (local 'revert-buffer-function) #'wdired-revert) - ;; I temp disable undo for performance: since I'm going to clear the - ;; undo list, it can save more than a 9% of time with big - ;; directories because setting properties modify the undo-list. - (buffer-disable-undo) - (wdired-preprocess-files) - (if wdired-allow-to-change-permissions - (wdired-preprocess-perms)) - (if (fboundp 'make-symbolic-link) - (wdired-preprocess-symlinks)) - (buffer-enable-undo) ; Performance hack. See above. + (setq revert-buffer-function 'wdired-revert) (set-buffer-modified-p nil) (setq buffer-undo-list nil) + ;; find one column with permissions and set permision text boundaries + (save-excursion + (goto-char (point-min)) + (unless (re-search-forward dired-re-perms nil t 1) + (wdired-abort-changes) + (error "No files to be renamed - Exiting to Dired mode.")) + (goto-char (match-beginning 0)) + (setq-local wdired-perm-beg (current-column)) + (goto-char (match-end 0)) + (setq-local wdired-perm-end (current-column))) + (define-key wdired-mode-map [remap self-insert-command] #'wdired--self-insert) (run-mode-hooks 'wdired-mode-hook) (message "%s" (substitute-command-keys "Press \\[wdired-finish-edit] when finished \ @@ -269,16 +276,49 @@ wdired-isearch-filter-read-only (not (text-property-not-all (min beg end) (max beg end) 'read-only nil))) +(defun wdired--point-at-perms-p () + (and (>= (current-column) wdired-perm-beg) + (<= (current-column) wdired-perm-end))) + +(defun wdired--self-insert () + (interactive) + (if (wdired--point-at-perms-p) + (when (not (get-text-property (line-beginning-position) 'front-sticky)) + (wdired--before-change-fn (line-beginning-position) (line-end-position)) + (setq unread-command-events (nconc (listify-key-sequence + (this-command-keys)) + unread-command-events))) + (call-interactively 'self-insert-command))) + +(defun wdired--before-change-fn (beg end) + (save-excursion + ;; make sure to process at least entire line + (goto-char beg) + (setq beg (line-beginning-position)) + (goto-char end) + (setq end (line-end-position)) + + (while (< beg end) + (unless (get-text-property beg 'front-sticky) + (put-text-property beg (1+ beg) 'front-sticky t) + (wdired--preprocess-files beg) + (when wdired-allow-to-change-permissions + (wdired--preprocess-perms beg)) + (when (fboundp 'make-symbolic-link) + (wdired--preprocess-symlinks beg))) + (forward-line) + (setq beg (point))) + ;; is this good enough? assumes no extra white lines from dired + (put-text-property (1- (point-max)) (point-max) 'read-only t))) + ;; Protect the buffer so only the filenames can be changed, and put ;; properties so filenames (old and new) can be easily found. -(defun wdired-preprocess-files () - (put-text-property (point-min) (1+ (point-min))'front-sticky t) +(defun wdired--preprocess-files (beg) (save-excursion - (goto-char (point-min)) - (let ((b-protection (point)) - (used-F (dired-check-switches dired-actual-switches "F" "classify")) - filename) - (while (not (eobp)) + (with-silent-modifications + (goto-char beg) + (let ((used-F (dired-check-switches dired-actual-switches "F" "classify")) + filename) (setq filename (dired-get-filename nil t)) (when (and filename (not (member (file-name-nondirectory filename) '("." "..")))) @@ -287,19 +327,16 @@ wdired-preprocess-files ;; the filename can't be modified. (add-text-properties (1- (point)) (point) `(old-name ,filename rear-nonsticky (read-only))) - (put-text-property b-protection (point) 'read-only t) + (put-text-property beg (point) 'read-only t) (dired-move-to-end-of-filename t) (put-text-property (point) (1+ (point)) 'end-name t)) - (when (and used-F (looking-at "[*/@|=>]$")) (forward-char)) - (when (save-excursion - (and (re-search-backward - dired-permission-flags-regexp nil t) - (looking-at "l") - (search-forward " -> " (line-end-position) t))) - (goto-char (line-end-position))) - (setq b-protection (point)) - (forward-line)) - (put-text-property b-protection (point-max) 'read-only t)))) + (when (and used-F (looking-at "[*/@|=>]$")) (forward-char)) + (when (save-excursion + (and (re-search-backward + dired-permission-flags-regexp nil t) + (looking-at "l") + (search-forward " -> " (line-end-position) t))) + (goto-char (line-end-position))))))) ;; This code is a copy of some dired-get-filename lines. (defsubst wdired-normalize-filename (file unquotep) @@ -362,7 +399,6 @@ wdired-get-filename (and file (> (length file) 0) (concat (dired-current-directory) file)))))) - (defun wdired-change-to-dired-mode () "Change the mode back to dired." (or (eq major-mode 'wdired-mode) @@ -379,14 +415,16 @@ wdired-change-to-dired-mode (setq major-mode 'dired-mode) (setq mode-name "Dired") (dired-advertise) - (remove-hook 'kill-buffer-hook #'wdired-check-kill-buffer t) - (remove-hook 'after-change-functions #'wdired--restore-properties t) - (remove-function (local 'revert-buffer-function) #'wdired-revert)) + (remove-hook 'kill-buffer-hook 'wdired-check-kill-buffer t) + (remove-hook 'before-change-functions 'wdired--before-change-fn t) + (remove-hook 'after-change-functions 'wdired--restore-properties t) + (setq-local revert-buffer-function 'dired-revert)) (defun wdired-abort-changes () - "Abort changes and return to dired mode." + "Abort changes and return to dired mode. " (interactive) - (let ((inhibit-read-only t)) + (remove-hook 'before-change-functions 'wdired--before-change-fn t) + (with-silent-modifications (erase-buffer) (insert wdired-old-content) (goto-char wdired-old-point)) @@ -702,21 +740,19 @@ wdired-previous-line (dired-move-to-filename))) ;; Put the needed properties to allow the user to change links' targets -(defun wdired-preprocess-symlinks () - (let ((inhibit-read-only t)) - (save-excursion - (goto-char (point-min)) - (while (not (eobp)) - (when (looking-at dired-re-sym) - (re-search-forward " -> \\(.*\\)$") - (put-text-property (1- (match-beginning 1)) - (match-beginning 1) 'old-link - (match-string-no-properties 1)) - (put-text-property (match-end 1) (1+ (match-end 1)) 'end-link t) - (unless wdired-allow-to-redirect-links - (put-text-property (match-beginning 0) - (match-end 1) 'read-only t))) - (forward-line))))) +(defun wdired--preprocess-symlinks (beg) + (save-excursion + (with-silent-modifications + (goto-char beg) + (when (looking-at dired-re-sym) + (re-search-forward " -> \\(.*\\)$") + (put-text-property (1- (match-beginning 1)) + (match-beginning 1) 'old-link + (match-string-no-properties 1)) + (put-text-property (match-end 1) (1+ (match-end 1)) 'end-link t) + (unless wdired-allow-to-redirect-links + (put-text-property (match-beginning 0) + (match-end 1) 'read-only t)))))) (defun wdired-get-previous-link (&optional old move) "Return the next symlink target. @@ -822,34 +858,33 @@ wdired-perm-mode-map ;; Put a keymap property to the permission bits of the files, and store the ;; original name and permissions as a property -(defun wdired-preprocess-perms () - (let ((inhibit-read-only t)) - (setq-local wdired-col-perm nil) - (save-excursion - (goto-char (point-min)) - (while (not (eobp)) - (when (and (not (looking-at dired-re-sym)) - (wdired-get-filename) - (re-search-forward dired-re-perms (line-end-position) 'eol)) - (let ((begin (match-beginning 0)) - (end (match-end 0))) - (unless wdired-col-perm - (setq wdired-col-perm (- (current-column) 9))) - (if (eq wdired-allow-to-change-permissions 'advanced) - (progn - (put-text-property begin end 'read-only nil) - ;; make first permission bit writable - (put-text-property - (1- begin) begin 'rear-nonsticky '(read-only))) - ;; avoid that keymap applies to text following permissions - (add-text-properties - (1+ begin) end - `(keymap ,wdired-perm-mode-map rear-nonsticky (keymap)))) - (put-text-property end (1+ end) 'end-perm t) - (put-text-property - begin (1+ begin) 'old-perm (match-string-no-properties 0)))) - (forward-line) - (beginning-of-line))))) +(defun wdired--preprocess-perms (beg) + (save-excursion + (with-silent-modifications + (setq-local wdired-col-perm nil) + (goto-char beg) + (when (and (not (looking-at dired-re-sym)) + (wdired-get-filename) + (re-search-forward dired-re-perms + (line-end-position) 'eol)) + (let ((begin (match-beginning 0)) + (end (match-end 0))) + (unless wdired-col-perm + (setq wdired-col-perm (- (current-column) 9))) + (if (eq wdired-allow-to-change-permissions 'advanced) + (progn + (put-text-property begin end 'read-only nil) + ;; make first permission bit writable + (put-text-property + (1- begin) begin 'rear-nonsticky '(read-only))) + ;; avoid that keymap applies to text following permissions + (add-text-properties + (1+ begin) end + `(keymap ,wdired-perm-mode-map rear-nonsticky (keymap)))) + (put-text-property end (1+ end) 'end-perm t) + (put-text-property + begin (1+ begin) + 'old-perm (match-string-no-properties 0))))))) (defun wdired-perm-allowed-in-pos (char pos) (cond -- 2.31.0 --==-=-= Content-Type: text/plain Content-Disposition: attachment; filename=lazy-wdired.el ;;; lazy-wdired.el --- -*- lexical-binding: t; -*- ;; Copyright (C) 2020 Arthur Miller ;; Author: Arthur Miller ;; Keywords: ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;;; Enable editing of file name and properties only at the point. ;;; Code: (require 'wdired) (defvar wdired-perm-beg) ;; Column where the permission bits start (defvar wdired-perm-end) ;; Column where the permission bits stop ;;;###autoload (defun wdired-change-to-wdired-mode () "Put a Dired buffer in Writable Dired (WDired) mode. \\ In WDired mode, you can edit the names of the files in the buffer, the target of the links, and the permission bits of the files. After typing \\[wdired-finish-edit], Emacs modifies the files and directories to reflect your edits. See `wdired-mode'." (interactive) (unless (derived-mode-p 'dired-mode) (error "Not a Dired buffer")) (setq-local wdired-old-content (buffer-substring (point-min) (point-max))) (setq-local wdired-old-marks (dired-remember-marks (point-min) (point-max))) (setq-local wdired-old-point (point)) (setq-local wdired-perm-beg nil) (setq-local wdired-perm-end nil) (setq-local query-replace-skip-read-only t) (add-function :after-while (local 'isearch-filter-predicate) #'wdired-isearch-filter-read-only) (use-local-map wdired-mode-map) (force-mode-line-update) (setq buffer-read-only nil) (dired-unadvertise default-directory) (add-hook 'kill-buffer-hook #'wdired-check-kill-buffer nil t) (add-hook 'before-change-functions #'wdired--before-change-fn nil t) (add-hook 'after-change-functions #'wdired--restore-properties nil t) ;;(advice-add 'self-insert-command :around #'wdired--self-insert-advice) (setq major-mode 'wdired-mode) (setq mode-name "Editable Dired") (setq revert-buffer-function 'wdired-revert) (set-buffer-modified-p nil) (setq buffer-undo-list nil) ;; find one column with permissions and set permision text boundaries (save-excursion (goto-char (point-min)) (unless (re-search-forward dired-re-perms nil t 1) (wdired-abort-changes) (error "No files to be renamed - Exiting to Dired mode.")) (goto-char (match-beginning 0)) (setq-local wdired-perm-beg (current-column)) (goto-char (match-end 0)) (setq-local wdired-perm-end (current-column))) (define-key wdired-mode-map [remap self-insert-command] #'wdired--self-insert) (run-mode-hooks 'wdired-mode-hook) (message "%s" (substitute-command-keys "Press \\[wdired-finish-edit] when finished \ or \\[wdired-abort-changes] to abort changes"))) (defun wdired--before-change-fn (beg end) (save-excursion ;; make sure to process at least entire line (goto-char beg) (setq beg (line-beginning-position)) (goto-char end) (setq end (line-end-position)) (while (< beg end) (unless (get-text-property beg 'front-sticky) (put-text-property beg (1+ beg) 'front-sticky t) (wdired--preprocess-files beg) (when wdired-allow-to-change-permissions (wdired--preprocess-perms beg)) (when (fboundp 'make-symbolic-link) (wdired--preprocess-symlinks beg))) (forward-line) (setq beg (point))) ;; is this good enough? assumes no extra white lines from dired (put-text-property (1- (point-max)) (point-max) 'read-only t))) (defun wdired--point-at-perms-p () (and (>= (current-column) wdired-perm-beg) (<= (current-column) wdired-perm-end))) (defun wdired--self-insert () (interactive) (if (wdired--point-at-perms-p) (when (not (get-text-property (line-beginning-position) 'front-sticky)) (wdired--before-change-fn (line-beginning-position) (line-end-position)) (setq unread-command-events (nconc (listify-key-sequence (this-command-keys)) unread-command-events))) (call-interactively 'self-insert-command))) ;; Protect the buffer so only the filenames can be changed, and put ;; properties so filenames (old and new) can be easily found. (defun wdired--preprocess-files (beg) (save-excursion (with-silent-modifications (goto-char beg) (let ((used-F (dired-check-switches dired-actual-switches "F" "classify")) filename) (setq filename (dired-get-filename nil t)) (when (and filename (not (member (file-name-nondirectory filename) '("." "..")))) (dired-move-to-filename) ;; The rear-nonsticky property below shall ensure that text preceding ;; the filename can't be modified. (add-text-properties (1- (point)) (point) `(old-name ,filename rear-nonsticky (read-only))) (put-text-property beg (point) 'read-only t) (dired-move-to-end-of-filename t) (put-text-property (point) (1+ (point)) 'end-name t)) (when (and used-F (looking-at "[*/@|=>]$")) (forward-char)) (when (save-excursion (and (re-search-backward dired-permission-flags-regexp nil t) (looking-at "l") (search-forward " -> " (line-end-position) t))) (goto-char (line-end-position))))))) (defun wdired-change-to-dired-mode () "Change the mode back to dired." (or (eq major-mode 'wdired-mode) (error "Not a Wdired buffer")) (let ((inhibit-read-only t)) (remove-text-properties (point-min) (point-max) '(front-sticky nil rear-nonsticky nil read-only nil keymap nil))) (remove-function (local 'isearch-filter-predicate) #'wdired-isearch-filter-read-only) (use-local-map dired-mode-map) (force-mode-line-update) (setq buffer-read-only t) (setq major-mode 'dired-mode) (setq mode-name "Dired") (dired-advertise) (remove-hook 'kill-buffer-hook 'wdired-check-kill-buffer t) (remove-hook 'before-change-functions 'wdired--before-change-fn t) (remove-hook 'after-change-functions 'wdired--restore-properties t) (setq-local revert-buffer-function 'dired-revert)) (defun wdired-abort-changes () "Abort changes and return to dired mode. " (interactive) (remove-hook 'before-change-functions 'wdired--before-change-fn t) (with-silent-modifications (erase-buffer) (insert wdired-old-content) (goto-char wdired-old-point)) (wdired-change-to-dired-mode) (set-buffer-modified-p nil) (setq buffer-undo-list nil) (message "Changes aborted")) ;; Put the needed properties to allow the user to change links' targets (defun wdired--preprocess-symlinks (beg) (save-excursion (with-silent-modifications (goto-char beg) (when (looking-at dired-re-sym) (re-search-forward " -> \\(.*\\)$") (put-text-property (1- (match-beginning 1)) (match-beginning 1) 'old-link (match-string-no-properties 1)) (put-text-property (match-end 1) (1+ (match-end 1)) 'end-link t) (unless wdired-allow-to-redirect-links (put-text-property (match-beginning 0) (match-end 1) 'read-only t)))))) (defun wdired--preprocess-perms (beg) (save-excursion (with-silent-modifications (setq-local wdired-col-perm nil) (goto-char beg) (when (and (not (looking-at dired-re-sym)) (wdired-get-filename) (re-search-forward dired-re-perms (line-end-position) 'eol)) (let ((begin (match-beginning 0)) (end (match-end 0))) (unless wdired-col-perm (setq wdired-col-perm (- (current-column) 9))) (if (eq wdired-allow-to-change-permissions 'advanced) (progn (put-text-property begin end 'read-only nil) ;; make first permission bit writable (put-text-property (1- begin) begin 'rear-nonsticky '(read-only))) ;; avoid that keymap applies to text following permissions (add-text-properties (1+ begin) end `(keymap ,wdired-perm-mode-map rear-nonsticky (keymap)))) (put-text-property end (1+ end) 'end-perm t) (put-text-property begin (1+ begin) 'old-perm (match-string-no-properties 0))))))) (provide 'lazy-wdired) --==-=-=-- --=-=-= Content-Type: text/plain -------------------- End of forwarded message -------------------- --=-=-=--