使用ido插入文件名

时间:2013-05-26 23:07:35

标签: emacs ido

我的.emacs中有以下函数,我经常使用它将本地文件的文件名/路径放在当前缓冲区中。它工作得很好,但是,我希望它能完成ido。但我似乎无法实现这一点......也许你可以帮助我。

(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)))))

1 个答案:

答案 0 :(得分:4)

启用ido-everywhere后,(interactive "f")通常会使用ido-read-file-name,这不仅会为您的功能提供自动完成功能,而且几乎无处不在。

如果您希望仅为此功能完成ido,而不是在任何地方,则可以在交互式表单中显式调用ido-read-file-name。在你的情况下使用ido的一个副作用是它似乎总是返回一个完整的路径,使filename(expand-file-name filename)之间的区别无效。

(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)))))