From b8c43b594e4e8bf27b1b1c1693d48f07799a1aaf Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Wed, 4 Aug 2021 10:35:53 +0200 Subject: [PATCH] Add a new thing-at-point type: existing-filename * doc/lispref/text.texi (Buffer Contents): Mention it. * lisp/thingatpt.el (thing-at-point-file-at-point): New function. (existing-filename): Register it. --- doc/lispref/text.texi | 9 +++++---- etc/NEWS | 5 +++++ lisp/thingatpt.el | 19 +++++++++++++++---- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi index e18ba472822..9e0401fffb9 100644 --- a/doc/lispref/text.texi +++ b/doc/lispref/text.texi @@ -315,10 +315,11 @@ word on the same line is acceptable. @defun thing-at-point thing &optional no-properties Return the @var{thing} around or next to point, as a string. -The argument @var{thing} is a symbol which specifies a kind of syntactic -entity. Possibilities include @code{symbol}, @code{list}, @code{sexp}, -@code{defun}, @code{filename}, @code{url}, @code{word}, @code{sentence}, -@code{whitespace}, @code{line}, @code{page}, and others. +The argument @var{thing} is a symbol which specifies a kind of +syntactic entity. Possibilities include @code{symbol}, @code{list}, +@code{sexp}, @code{defun}, @code{filename}, @code{existing-filename}, +@code{url}, @code{word}, @code{sentence}, @code{whitespace}, +@code{line}, @code{page}, and others. When the optional argument @var{no-properties} is non-@code{nil}, this function strips text properties from the return value. diff --git a/etc/NEWS b/etc/NEWS index 18cca8b4ab3..f0fa686bc9c 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2639,6 +2639,11 @@ The semantics are as with 'walk-windows'. If non-nil, 'find-file-at-point' and friends will try to guess more expansively to identify a file name with spaces. ++++ +*** New 'thing-at-point' target: 'existing-filename'. +This is like 'filename', but is a full path, and is nil if the file +doesn't exist. + --- *** Two new commands for centering in 'doc-view-mode'. The new commands 'doc-view-center-page-horizontally' (bound to 'c h') diff --git a/lisp/thingatpt.el b/lisp/thingatpt.el index 4c2470fbcb6..5bbf1a85b36 100644 --- a/lisp/thingatpt.el +++ b/lisp/thingatpt.el @@ -73,8 +73,8 @@ provider functions are called with no parameters at the point in question. \"things\" include `symbol', `list', `sexp', `defun', `filename', -`url', `email', `uuid', `word', `sentence', `whitespace', `line', -and `page'.") +`existing-filename', `url', `email', `uuid', `word', `sentence', +`whitespace', `line', and `page'.") ;; Basic movement @@ -156,8 +156,8 @@ positions of the thing found." "Return the THING at point. THING should be a symbol specifying a type of syntactic entity. Possibilities include `symbol', `list', `sexp', `defun', -`filename', `url', `email', `uuid', `word', `sentence', `whitespace', -`line', `number', and `page'. +`filename', `existing-filename', `url', `email', `uuid', `word', +`sentence', `whitespace', `line', `number', and `page'. When the optional argument NO-PROPERTIES is non-nil, strip text properties from the return value. @@ -301,6 +301,17 @@ E.g.: (define-thing-chars filename thing-at-point-file-name-chars) +;; Files + +(defun thing-at-point-file-at-point (&optional _lax _bounds) + "Return the name of the existing file at point." + (when-let ((filename (thing-at-point 'filename))) + (setq filename (expand-file-name filename)) + (and (file-exists-p filename) + filename))) + +(put 'existing-filename 'thing-at-point 'thing-at-point-file-at-point) + ;; URIs (defvar thing-at-point-beginning-of-url-regexp nil -- 2.39.2