diff --git a/lisp/ffap.el b/lisp/ffap.el index f892bb81e40..1c9f0294601 100644 --- a/lisp/ffap.el +++ b/lisp/ffap.el @@ -1140,7 +1140,7 @@ The arguments CHARS, BEG and END are handled as described in "Last string returned by the function `ffap-string-at-point'.") (defcustom ffap-file-name-with-spaces nil - "If non-nil, enable looking for paths with spaces in `ffap-string-at-point'. + "If non-nil, allow file names with spaces in `ffap-string-at-point'. Enabling this variable may lead to `find-file-at-point' guessing wrong more often when trying to find a file name intermingled with normal text, but can be useful when working on systems that @@ -1286,10 +1286,16 @@ return an empty string, and set `ffap-string-at-point-region' to `(1 1)'." (if (and ffap-file-name-with-spaces (memq mode '(nil file))) (when (setq dir-separator (ffap-dir-separator-near-point)) - (while (re-search-backward - (regexp-quote dir-separator) - (line-beginning-position) t) - (goto-char (match-beginning 0)))) + (let ((dirsep-re (regexp-quote dir-separator)) + (line-beg (line-beginning-position))) + (while (re-search-backward dirsep-re line-beg t) + (goto-char (match-beginning 0))) + (if (and (looking-at dirsep-re) + (looking-back + ;; Either "~[USER]" or drive letter. + "\\(~[[:graph:]]*\\|[a-zA-Z]:\\)" + line-beg)) + (goto-char (match-beginning 0))))) (skip-chars-backward (car args)) (skip-chars-forward (nth 1 args) pt)) (point)))) diff --git a/test/lisp/ffap-tests.el b/test/lisp/ffap-tests.el index 3c6e1e8e18e..ea5e745bfaf 100644 --- a/test/lisp/ffap-tests.el +++ b/test/lisp/ffap-tests.el @@ -105,7 +105,7 @@ left alone when opening a URL in an external browser." (with-temp-buffer (insert string) (goto-char (point-min)) - (forward-char 10) + (forward-char 3) (ffap-string-at-point)))) (ert-deftest ffap-test-with-spaces () @@ -113,7 +113,7 @@ left alone when opening a URL in an external browser." (equal (ffap-test-string t "c:/Program Files/Open Text Evaluation Media/Open Text Exceed 14 x86/Program here.txt") - "/Program Files/Open Text Evaluation Media/Open Text Exceed 14 x86/Program here.txt")) + "c:/Program Files/Open Text Evaluation Media/Open Text Exceed 14 x86/Program here.txt")) (should (equal (ffap-test-string @@ -122,23 +122,39 @@ left alone when opening a URL in an external browser." (should (equal (ffap-test-string - t "c:/Program Files/Open Text Evaluation Media/Open Text Exceed 14 x86/Program Files/Hummingbird/") - "/Program Files/Open Text Evaluation Media/Open Text Exceed 14 x86/Program Files/Hummingbird/")) + t "z:/Program Files/Open Text Evaluation Media/Open Text Exceed 14 x86/Program Files/Hummingbird/") + "z:/Program Files/Open Text Evaluation Media/Open Text Exceed 14 x86/Program Files/Hummingbird/")) (should (equal (ffap-test-string t "c:\\Program Files\\Open Text Evaluation Media\\Open Text Exceed 14 x86\\Program Files\\Hummingbird\\") - "\\Program Files\\Open Text Evaluation Media\\Open Text Exceed 14 x86\\Program Files\\Hummingbird\\")) + "c:\\Program Files\\Open Text Evaluation Media\\Open Text Exceed 14 x86\\Program Files\\Hummingbird\\")) (should (equal (ffap-test-string - t "c:\\Program Files\\Freescale\\CW for MPC55xx and MPC56xx 2.10\\PowerPC_EABI_Tools\\Command_Line_Tools\\CLT_Usage_Notes.txt") - "\\Program Files\\Freescale\\CW for MPC55xx and MPC56xx 2.10\\PowerPC_EABI_Tools\\Command_Line_Tools\\CLT_Usage_Notes.txt")) + t "d:\\Program Files\\Freescale\\CW for MPC55xx and MPC56xx 2.10\\PowerPC_EABI_Tools\\Command_Line_Tools\\CLT_Usage_Notes.txt") + "d:\\Program Files\\Freescale\\CW for MPC55xx and MPC56xx 2.10\\PowerPC_EABI_Tools\\Command_Line_Tools\\CLT_Usage_Notes.txt")) (should (equal (ffap-test-string t "C:\\temp\\program.log on Windows or /var/log/program.log on Unix.") - "\\temp\\program.log"))) + "C:\\temp\\program.log")) + (should + (equal + (ffap-test-string t "~/tmp/") + "~/tmp/")) + (should + (equal + (ffap-test-string nil "~/tmp/") + "~/tmp/")) + (should + (equal + (ffap-test-string t "~abc123_áè/foo") + "~abc123_áè/foo")) + (should + (equal + (ffap-test-string t "c:/Program Files/my program.exe and here's more text") + "c:/Program Files/my program.exe"))) (ert-deftest ffap-test-no-newlines () (should-not