Before you say "thing-at-point" - I cannot install it (work network).
thing-at-point
has been built into Emacs since 1993.
thing-at-point is an autoloaded compiled Lisp function in
‘thingatpt.el’.
(thing-at-point THING &optional NO-PROPERTIES)
Probably introduced at or before Emacs version 20.
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’.
When the optional argument NO-PROPERTIES is non-nil,
strip text properties from the return value.
See the file ‘thingatpt.el’ for documentation on how to define
a symbol as a valid THING.
For example:
(thing-at-point 'word)
(thing-at-point 'symbol)
(re-search-backward "\_<" nil t 1)
This is invalid regexp syntax - it should be either "\\<"
for "beginning of word", or "\\_<"
for "beginning of symbol". Similarly with the matching "end of X" regexp. See (info "(elisp) Syntax of Regexps")
and its subnodes.
this is some /very/long/name-of/my.file/that-might_contain_letters/andnumbers0-9/anywhere in it.
Hm, that's not an easy construct. The problem is that word/symbol boundaries depend on the current buffer's syntax table, so they will work differently depending on which buffer you're in.
This means you need to specify exactly what constitutes your desired boundaries, or in which mode you will be using this command.
For instance, the following should work with your example in a Lisp buffer:
(defun my-save-symbol-at-point ()
"Make symbol at point the latest kill in the kill ring."
(interactive)
(let ((symbol (thing-at-point 'symbol)))
(when symbol (kill-new symbol))))
But it won't work the same in a Text buffer. Ideally the following should work more reliably regardless of mode:
(defun my-save-filename-at-point ()
"Make file name at point the latest kill in the kill ring."
(interactive)
(let ((file (thing-at-point 'filename)))
(when file (kill-new file))))
You could even complete different types of thing at point that you may be interested in:
(defun my-save-thing-at-point (&optional thing)
"Make THING at point the latest kill in the kill ring.
See `thing-at-point' for possible values of THING.
It defaults to `filename'.
With a prefix argument, select a type of THING with completion."
(interactive
(when current-prefix-arg
;; Add whatever you like here.
(let ((things '("filename" "symbol" "sexp" "url" "word")))
(list (intern (completing-read "Type of thing (default filename): "
things nil t nil nil things))))))
(kill-new (or (thing-at-point (or thing 'filename))
(user-error "No such thing at point"))))
If all you want is to detect text boundaries that consist of spaces, then see Firmin Martin's answer.
(save-excursion ...)
instead of saving/restoring your point position. See for instance here.