unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#43961: read carefully: dired-file-name-at-point vs dired-filename-at-point
@ 2020-10-12 14:26 Boruch Baum
  2020-10-13  3:49 ` Richard Stallman
  0 siblings, 1 reply; 12+ messages in thread
From: Boruch Baum @ 2020-10-12 14:26 UTC (permalink / raw)
  To: 43961

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

The attached patch takes a step to remove confusion surrounding two
unfortunately named functions, fixes bugs in one of them, and makes them
more user-friendly. If this is all acceptable, let me know and I'll add
a NEWS entry.

+ dired-filename-at-point is moved from dired-x.el to a position
  alongside its sister function dired-file-name-at-point.

+ The code for dired-filename-at-point is simplified to an almost
  carbon copy of its sister function.

  + This also fixes bugs for cases of filenames with embedded spaces

  + This also suppresses errors, due to use of dired-get-filename with
    its arg NOERROR.

+ Both get a small style tweak to remove duplicated function calls to
  abbrev- or expand- file-name.

+ Both get aliases to make them distinguishable.

A follow-up to the attached patch, if accepted, would be to refactor all
the occurrences within dired.el / dired-x.el.

It would probably be a good idea to also add a deprecation notice to the
docstrings for the original names.

I came across this while developing the diredc package I've been
mentioning a lot lately, and presenting this as a separate patch removes
it from being snagged as part of discussion about that other proposal.

--
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0

[-- Attachment #2: dired-name-confusion.patch --]
[-- Type: text/x-diff, Size: 3640 bytes --]

diff --git a/dired-x.el b/dired-x.el
index b09ef90..d3b9dc9 100644
--- a/dired-x.el
+++ b/dired-x.el
@@ -1482,37 +1482,6 @@ a prefix argument, when it offers the filename near point as a default."

 ;;; Internal functions.

-;; Fixme: This should probably use `thing-at-point'.  -- fx
-(defun dired-filename-at-point ()
-  "Return the filename closest to point, expanded.
-Point should be in or after a filename."
-  (save-excursion
-    ;; First see if just past a filename.
-    (or (eobp)                             ; why?
-        (when (looking-at-p "[] \t\n[{}()]") ; whitespace or some parens
-          (skip-chars-backward " \n\t\r({[]})")
-          (or (bobp) (backward-char 1))))
-    (let ((filename-chars "-.[:alnum:]_/:$+@")
-          start prefix)
-      (if (looking-at-p (format "[%s]" filename-chars))
-          (progn
-            (skip-chars-backward filename-chars)
-            (setq start (point)
-                  prefix
-                  ;; This is something to do with ange-ftp filenames.
-                  ;; It convert foo@bar to /foo@bar.
-                  ;; But when does the former occur in dired buffers?
-		  (and (string-match-p
-			"^\\w+@"
-			(buffer-substring start (line-end-position)))
-		       "/"))
-            (if (string-match-p "[/~]" (char-to-string (preceding-char)))
-                (setq start (1- start)))
-            (skip-chars-forward filename-chars))
-        (error "No file found around point!"))
-      ;; Return string.
-      (expand-file-name (concat prefix (buffer-substring start (point)))))))
-
 (defun dired-x-read-filename-at-point (prompt)
   "Return filename prompting with PROMPT with completion.
 If `current-prefix-arg' is non-nil, uses name at point as guess."
diff --git a/dired.el b/dired.el
index 2ecd6bd..69aa41d 100644
--- a/dired.el
+++ b/dired.el
@@ -958,16 +958,31 @@ ERROR can be a string with the error message."
 ;;             (read-file-name (format "Dired %s(directory): " str)
 ;;                             nil default-directory nil))))))))

-(defun dired-file-name-at-point ()
-  "Try to get a file name at point in the current dired buffer.
-This hook is intended to be put in `file-name-at-point-functions'.
-Note that it returns an abbreviated name that can't be used
-as an argument to `dired-goto-file'."
+(defun dired-filename-at-point-abbrev ()
+  "Return the filename closest to point, abbreviated.
+Point should be in or after a filename. Do not confuse this
+function with `dired-filename-at-point', which expands
+the file name."
   (let ((filename (dired-get-filename nil t)))
     (when filename
-      (if (file-directory-p filename)
-	  (file-name-as-directory (abbreviate-file-name filename))
-	(abbreviate-file-name filename)))))
+      (abbreviate-file-name
+        (if (file-directory-p filename)
+	  (file-name-as-directory filename)
+	 filename)))))
+(defalias 'dired-file-name-at-point 'dired-filename-at-point-abbrev)
+
+(defun dired-filename-at-point-expand ()
+  "Return the filename closest to point, expanded.
+Point should be in or after a filename. Do not confuse this
+function with `dired-file-name-at-point', which abbreviates
+the file name."
+  (let ((filename (dired-get-filename nil t)))
+    (when filename
+      (expand-file-name
+        (if (file-directory-p filename)
+	  (file-name-as-directory filename)
+	 filename)))))
+(defalias 'dired-filename-at-point 'dired-filename-at-point-expand)

 (defun dired-grep-read-files ()
   "Use file at point as the file for grep's default file-name pattern suggestion.

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

end of thread, other threads:[~2020-10-15  3:57 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-12 14:26 bug#43961: read carefully: dired-file-name-at-point vs dired-filename-at-point Boruch Baum
2020-10-13  3:49 ` Richard Stallman
2020-10-13  4:08   ` Boruch Baum
2020-10-13  4:54     ` Lars Ingebrigtsen
2020-10-13 10:25       ` Boruch Baum
2020-10-13 10:58         ` Arthur Miller
2020-10-13 11:27           ` Boruch Baum
2020-10-14  3:45         ` Lars Ingebrigtsen
2020-10-14  4:19           ` Boruch Baum
2020-10-14  4:43     ` Richard Stallman
2020-10-14  4:58       ` Lars Ingebrigtsen
2020-10-15  3:57         ` Richard Stallman

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).