0

The following function replaces filenames and places the result in a list. Although the list gets filled, I do not want to add the filename when there is no match. How can I avoid sending non-matching situations from being added to the list?

(defvar my-list-for-later-access '())

(defun mash-homedir (username)
  "Substitute USERNAME into the pathname at point."
  (interactive "MUsername: ")
  (add-to-list
   'my-list-for-later-access
   (s-replace-regexp
    "/home/[^/]+/"
    (format "/home/%s/" username)
    (thing-at-point 'filename :no-properties))))
Drew
  • 75,699
  • 9
  • 109
  • 225
Dilna
  • 1,173
  • 3
  • 10

1 Answers1

0

IIUC, C-hig file-exists-p is the function you want.

See also C-h i g (elisp)Testing Accessibility


Not checking if file exists but if there is a match to "/home/[^/]+/".

I'd do something like this, then:

(when-let* ((filename (thing-at-point 'filename :no-properties))
            (index (string-match "^/home/[^/]+/\\(.+\\)" filename)))
  (add-to-list 'my-list-for-later-access
               (format "/home/%s/%s" username (match-string 1 filename))))
phils
  • 48,657
  • 3
  • 76
  • 115