Subject: read carefully: dired-file-name-at-point vs dired-filename-at-point

From: Boruch Baum <[email-masked]>
Date: Mon, 12 Oct 2020 10:26:38 -0400

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