diff options
Diffstat (limited to 'lisp/thingatpt.el')
-rw-r--r-- | lisp/thingatpt.el | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/lisp/thingatpt.el b/lisp/thingatpt.el index c52fcfcc051..66bbfb0f9f6 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 @@ -481,7 +492,7 @@ looks like an email address, \"ftp://\" if it starts with (and (string-match "\\`[[:alnum:]]+\\'" str) (eq (char-before (car bounds)) ?<) (eq (char-after (cdr bounds)) ?>) - (not (string-match "~" (expand-file-name (concat "~" str)))) + (not (string-search "~" (expand-file-name (concat "~" str)))) (setq str (concat "mailto:" str))) ;; If it looks like news.example.com, treat it as news. (if (thing-at-point-newsgroup-p str) @@ -600,10 +611,14 @@ with angle brackets.") (buffer-substring-no-properties (car boundary-pair) (cdr boundary-pair)))))) -;; Buffer +;; Buffer and region (put 'buffer 'end-op (lambda () (goto-char (point-max)))) (put 'buffer 'beginning-op (lambda () (goto-char (point-min)))) +(put 'region 'bounds-of-thing-at-point + (lambda () + (when (use-region-p) + (cons (region-beginning) (region-end))))) ;; UUID @@ -673,14 +688,14 @@ Signal an error if the entire string was not used." "Return the number at point, or nil if none is found. Decimal numbers like \"14\" or \"-14.5\", as well as hex numbers like \"0xBEEF09\" or \"#xBEEF09\", are recognized." - (when (thing-at-point-looking-at - "\\(-?[0-9]+\\.?[0-9]*\\)\\|\\(0x\\|#x\\)\\([a-zA-Z0-9]+\\)" 500) - (if (match-beginning 1) - (string-to-number - (buffer-substring (match-beginning 1) (match-end 1))) - (string-to-number - (buffer-substring (match-beginning 3) (match-end 3)) - 16)))) + (cond + ((thing-at-point-looking-at "\\(0x\\|#x\\)\\([a-fA-F0-9]+\\)" 500) + (string-to-number + (buffer-substring (match-beginning 2) (match-end 2)) + 16)) + ((thing-at-point-looking-at "-?[0-9]+\\.?[0-9]*" 500) + (string-to-number + (buffer-substring (match-beginning 0) (match-end 0)))))) (put 'number 'thing-at-point 'number-at-point) ;;;###autoload |