unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#67161: 30.0.50; [PATCH] Add option `dired-filename-display-length'
@ 2023-11-14  9:52 Liu Hui
  2023-11-14 13:26 ` Eli Zaretskii
  2023-11-15 15:54 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 2 replies; 37+ messages in thread
From: Liu Hui @ 2023-11-14  9:52 UTC (permalink / raw)
  To: 67161

[-- Attachment #1: Type: text/plain, Size: 508 bytes --]

Hi,

The attached patch implements the feature of controlling the display
length of filename in dired buffers.

When setting this option to `window', the long filename is shortened
by hiding the middle part to ensure that the whole line needs not to
be wrapped. The option can also be set to an integer, meaning the
maximum display length of filename. For users who turn on line
truncation, this feature can display the end part including the file
extension, which I think is convenient.


Best,

--
Liu Hui

[-- Attachment #2: 0001-Add-option-dired-filename-display-length.patch --]
[-- Type: text/x-patch, Size: 8964 bytes --]

From 43a72585ff11b342cb48f454b4466ef5ed4b6f9b Mon Sep 17 00:00:00 2001
From: Liu Hui <liuhui1610@gmail.com>
Date: Tue, 14 Nov 2023 16:14:12 +0800
Subject: [PATCH] Add option `dired-filename-display-length'

* lisp/dired.el (dired-filename-display-length):
(dired-filename-hiding-ellipsis): New options.
(dired-insert-set-properties): Set invisibility spec for long
filenames.
(dired--get-filename-display-length)
(dired-filename-update-invisibility-spec): New functions.
(dired-mode): Add filename invisibility spec.
* lisp/wdired.el (wdired-change-to-wdired-mode)
(wdired-change-to-dired-mode): Update filename invisibility spec.
---
 lisp/dired.el  | 124 +++++++++++++++++++++++++++++++++----------------
 lisp/wdired.el |   2 +
 2 files changed, 85 insertions(+), 41 deletions(-)

diff --git a/lisp/dired.el b/lisp/dired.el
index 8919d2c223f..b650f15e28f 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -517,6 +517,21 @@ dired-hide-details-preserved-columns
   :type '(repeat integer)
   :version "30.1")
 
+(defcustom dired-filename-display-length nil
+  "If non-nil, hide middle part of long filenames in Dired buffers.
+If the value is the symbol `window', then filenames are shortened
+to not exceed the right edge of current window.  Otherwise, it
+should be an integer representing the maximum filename length."
+  :type '(choice (integer :tag "Full" nil)
+                 (const   :tag "Window" window)
+                 (integer :tag "Integer"))
+  :version "30.1")
+
+(defcustom dired-filename-hiding-ellipsis "…"
+  "String to indicate filename hiding."
+  :type 'string
+  :version "30.1")
+
 \f
 ;;; Internal variables
 
@@ -1903,48 +1918,62 @@ dired-insert-set-properties
   "Add various text properties to the lines in the region, from BEG to END."
   (save-excursion
     (goto-char beg)
-    (while (< (point) end)
-      (ignore-errors
-	(if (not (dired-move-to-filename))
-	    (unless (or (looking-at-p "^$")
-			(looking-at-p dired-subdir-regexp))
-	      (put-text-property (line-beginning-position)
-				 (1+ (line-end-position))
-				 'invisible 'dired-hide-details-information))
-	  (save-excursion
-            (let ((end (1- (point)))
-                  (opoint (goto-char (1+ (pos-bol))))
-                  (i 0))
-              (put-text-property opoint end 'invisible 'dired-hide-details-detail)
-              (while (re-search-forward "[^ ]+" end t)
-                (when (member (cl-incf i) dired-hide-details-preserved-columns)
-                  (put-text-property opoint (point) 'invisible nil))
-                (setq opoint (point)))))
-          (let ((beg (point)) (end (save-excursion
-	                             (dired-move-to-end-of-filename)
-	                             (1- (point)))))
-            (if dired-click-to-select-mode
-                (put-text-property beg end 'keymap
-                                   dired-click-to-select-map)
-              (when (and dired-mouse-drag-files (fboundp 'x-begin-drag))
-                (put-text-property beg end 'keymap
-                                   dired-mouse-drag-files-map)))
-	    (add-text-properties
-	     beg (1+ end)
-	     `(mouse-face
-	       highlight
-	       dired-filename t
-	       help-echo ,(if dired-click-to-select-mode
-                              "mouse-2: mark or unmark this file"
-                            (if (and dired-mouse-drag-files
-                                     (fboundp 'x-begin-drag))
-                                "down-mouse-1: drag this file to another program
+    (let ((ell-len (string-width dired-filename-hiding-ellipsis))
+          maxlen filename-col)
+      (while (< (point) end)
+        (ignore-errors
+	  (if (not (dired-move-to-filename))
+	      (unless (or (looking-at-p "^$")
+			  (looking-at-p dired-subdir-regexp))
+	        (put-text-property (line-beginning-position)
+				   (1+ (line-end-position))
+				   'invisible 'dired-hide-details-information))
+	    (save-excursion
+              (let ((end (1- (point)))
+                    (opoint (goto-char (1+ (pos-bol))))
+                    (i 0))
+                (put-text-property opoint end 'invisible 'dired-hide-details-detail)
+                (while (re-search-forward "[^ ]+" end t)
+                  (when (member (cl-incf i) dired-hide-details-preserved-columns)
+                    (put-text-property opoint (point) 'invisible nil))
+                  (setq opoint (point)))))
+            (let ((beg (point)) (end (save-excursion
+	                               (dired-move-to-end-of-filename)
+	                               (1- (point)))))
+              (if dired-click-to-select-mode
+                  (put-text-property beg end 'keymap
+                                     dired-click-to-select-map)
+                (when (and dired-mouse-drag-files (fboundp 'x-begin-drag))
+                  (put-text-property beg end 'keymap
+                                     dired-mouse-drag-files-map)))
+              (when dired-filename-display-length
+                (let ((len (string-width (buffer-substring beg (1+ end))))
+                      ell-beg)
+                  (or maxlen (setq maxlen (dired--get-filename-display-length)))
+                  (when (and (integerp maxlen) (> len maxlen (+ ell-len 2)))
+                    (or filename-col (setq filename-col (current-column)))
+                    (move-to-column (+ filename-col (/ maxlen 2)))
+                    (setq ell-beg (point))
+                    (move-to-column (+ filename-col (/ maxlen 2)
+                                       (- len maxlen) ell-len))
+                    (put-text-property
+                     ell-beg (point) 'invisible 'dired-filename-hide))))
+	      (add-text-properties
+	       beg (1+ end)
+	       `(mouse-face
+	         highlight
+	         dired-filename t
+	         help-echo ,(if dired-click-to-select-mode
+                                "mouse-2: mark or unmark this file"
+                              (if (and dired-mouse-drag-files
+                                       (fboundp 'x-begin-drag))
+                                  "down-mouse-1: drag this file to another program
 mouse-2: visit this file in other window"
-                              "mouse-2: visit this file in other window"))))
-	    (when (< (+ end 5) (line-end-position))
-	      (put-text-property (+ end 5) (line-end-position)
-			         'invisible 'dired-hide-details-link)))))
-      (forward-line 1))))
+                                "mouse-2: visit this file in other window"))))
+	      (when (< (+ end 5) (line-end-position))
+	        (put-text-property (+ end 5) (line-end-position)
+			           'invisible 'dired-hide-details-link)))))
+        (forward-line 1)))))
 
 (defun dired--make-directory-clickable ()
   (save-excursion
@@ -1976,6 +2005,12 @@ dired--make-directory-clickable
                           "RET" click))))
           (setq segment-start (point)))))))
 
+(defun dired--get-filename-display-length ()
+  "Return maximum display length of filename."
+  (if (integerp dired-filename-display-length)
+      dired-filename-display-length
+    (- (window-max-chars-per-line) 2 (current-column))))
+
 \f
 ;;; Reverting a dired buffer
 
@@ -2617,6 +2652,7 @@ dired-mode
 	mode-line-buffer-identification
 	(propertized-buffer-identification "%17b"))
   (add-to-invisibility-spec '(dired . t))
+  (dired-filename-update-invisibility-spec)
   ;; Ignore dired-hide-details-* value of invisible text property by default.
   (when (eq buffer-invisibility-spec t)
     (setq buffer-invisibility-spec (list t)))
@@ -3106,6 +3142,12 @@ dired-hide-details-update-invisibility-spec
 \f
 ;;; Functions to hide/unhide text
 
+(defun dired-filename-update-invisibility-spec ()
+  (funcall (if (derived-mode-p 'dired-mode)
+               'add-to-invisibility-spec
+             'remove-from-invisibility-spec)
+           `(dired-filename-hide . ,dired-filename-hiding-ellipsis)))
+
 (defun dired--find-hidden-pos (start end)
   (text-property-any start end 'invisible 'dired))
 
diff --git a/lisp/wdired.el b/lisp/wdired.el
index 079d93d6011..5d50a574290 100644
--- a/lisp/wdired.el
+++ b/lisp/wdired.el
@@ -261,6 +261,7 @@ wdired-change-to-wdired-mode
   (add-function :override (local 'revert-buffer-function) #'wdired-revert)
   (set-buffer-modified-p nil)
   (setq buffer-undo-list nil)
+  (dired-filename-update-invisibility-spec)
   (run-mode-hooks 'wdired-mode-hook)
   (message "%s" (substitute-command-keys
 		 "Press \\[wdired-finish-edit] when finished \
@@ -456,6 +457,7 @@ wdired-change-to-dired-mode
   (dired-sort-set-mode-line)
   (dired-advertise)
   (dired-hide-details-update-invisibility-spec)
+  (dired-filename-update-invisibility-spec)
   (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)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 37+ messages in thread

end of thread, other threads:[~2023-11-27 17:16 UTC | newest]

Thread overview: 37+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-14  9:52 bug#67161: 30.0.50; [PATCH] Add option `dired-filename-display-length' Liu Hui
2023-11-14 13:26 ` Eli Zaretskii
2023-11-15 10:04   ` Liu Hui
2023-11-15 12:32     ` Eli Zaretskii
2023-11-16 10:07       ` Liu Hui
2023-11-16 12:11         ` Eli Zaretskii
2023-11-18  9:23           ` Liu Hui
2023-11-18 10:55             ` Eli Zaretskii
2023-11-18 16:12               ` Drew Adams
2023-11-20  4:34               ` Liu Hui
2023-11-20 12:10                 ` Eli Zaretskii
2023-11-20 17:54                   ` Juri Linkov
2023-11-20 18:42                     ` Eli Zaretskii
2023-11-20 18:55                   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-20 19:17                     ` Eli Zaretskii
2023-11-21  7:52                     ` Juri Linkov
2023-11-21 11:55                       ` Eli Zaretskii
2023-11-21 17:12                         ` Juri Linkov
2023-11-20 17:20                 ` Drew Adams
2023-11-22  5:41                 ` Liu Hui
2023-11-25 10:52                   ` Eli Zaretskii
2023-11-25 17:51                     ` Juri Linkov
2023-11-25 20:02                       ` Eli Zaretskii
2023-11-26  2:56                         ` Liu Hui
2023-11-26  5:59                           ` Eli Zaretskii
2023-11-26 10:49                             ` Eli Zaretskii
2023-11-26 14:03                             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-26 14:53                               ` Eli Zaretskii
2023-11-26 17:08                                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-26 17:58                                   ` Eli Zaretskii
2023-11-26 18:06                                     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-27  7:19                           ` Juri Linkov
2023-11-27  8:32                             ` Liu Hui
2023-11-27 17:16                               ` Juri Linkov
2023-11-15 18:06     ` Drew Adams
2023-11-15 15:54 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-16  3:44   ` Liu Hui

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).