I have the function below in my .emacs
which I use quite frequently to put the filename/path of a local file in the current buffer. It is working nicely, however, I'd like it to have ido
completion. But I seem unable to achieve that... maybe you can help me.
(defun insert-file-name (filename &optional args)
"Insert name of file FILENAME into buffer after point.
Prefixed with \\[universal-argument], expand the file name to
its fully canocalized path. See `expand-file-name'.
Prefixed with \\[negative-argument], use relative path to file
name from current directory, `default-directory'. See
`file-relative-name'.
The default with no prefix is to insert the file name exactly as
it appears in the minibuffer prompt."
;; Based on insert-file in Emacs -- ashawley 20080926
(interactive "*fInsert file name: \nP")
(cond ((eq '- args)
(insert (expand-file-name filename)))
((not (null args))
(insert (filename)))
(t
(insert (file-relative-name filename)))))
With ido-everywhere
turned-on, (interactive "f")
will normally use ido-read-file-name
, which will not only provide automatic completion for your function, but also almost everywhere.
If you want to have ido completion only for this function, but not everywhere, you can explicitly call ido-read-file-name
in the interactive form. One side effect of using ido in your case is that it seems to always return a full path, making the distinction between filename
and (expand-file-name filename)
effectless.
(defun insert-file-name (filename &optional args)
"Insert name of file FILENAME into buffer after point.
Prefixed with \\[universal-argument], expand the file name to
its fully canocalized path. See `expand-file-name'.
Prefixed with \\[negative-argument], use relative path to file
name from current directory, `default-directory'. See
`file-relative-name'.
The default with no prefix is to insert the file name exactly as
it appears in the minibuffer prompt."
;; Based on insert-file in Emacs -- ashawley 20080926
(interactive `(,(ido-read-file-name "File Name: ")
,current-prefix-arg))
(cond ((eq '- args)
(insert (expand-file-name filename)))
((not (null args))
(insert filename))
(t
(insert (file-relative-name filename)))))