1

I would like to ask how to get the file name from interactive command in lisp.

Btw, this is taken from another stackoverflow thread.

Paste an image on clipboard to Emacs Org mode file without saving it

(defun markdown-insert-clipboard-image (&optional file)
  (interactive "G")
  (shell-command (concat "pngpaste " file))
  (insert (concat "![" file "](" file ")")))

My problem is the file is using absolute path instead of just the filename causing the file to have an error when pushed to the server.

Instead of yahoocom.png only, I get

/Users/test/test/test/test/yahoocom.png

I would only want to get

yahoocom.png

Is there anyway that I can modify the code so that it only uses the filename.

Drew
  • 29,895
  • 7
  • 74
  • 104
grassbl8d
  • 2,089
  • 4
  • 24
  • 34

2 Answers2

5

I think what you are looking for is file-name-nondirectory:

Return file name FILENAME sans its directory. For example, in a Unix-syntax file name, this is everything after the last slash, or the entire name if it contains no slash.

Note that you probably do not want to pass the base name to shell-command (use either the original absolute path or file-relative-name)

PS. I suggest that you replace "G" with "fImage file name: " in your interactive form because your function will fail if the file does not exist.

sds
  • 58,617
  • 29
  • 161
  • 278
  • 1
    This perfectly solved it. Thank You. I already replaced with "F" which was the one in the original stack exchange post. I just tried the different paramters. Thank You, – grassbl8d Nov 21 '19 at 16:42
5

While sds's answer indeed gives what you directly asked for, I think what you're really looking for might be to have the relative name of the file (which will be just the same if the file is in the current working directory). If so, try file-relative-name:

Convert FILENAME to be relative to DIRECTORY (default: default-directory).
This function returns a relative file name that is equivalent to FILENAME when used with that default directory as the default.
If FILENAME is a relative file name, it will be interpreted as existing in default-directory.
If FILENAME and DIRECTORY lie on different machines or on different drives on a DOS/Windows machine, it returns FILENAME in expanded form.

Stefan
  • 27,908
  • 4
  • 53
  • 82