2012-08-03 51 views
3

我在这里找到了替换filen的函数,但它似乎无法正常工作:http://www.emacswiki.org/emacs/InsertFileName。它正确地找到了文件中的点,但替换似乎将您输入的文件,而不是从我能看到的最初检测到的文件。我不知道如何解决这个问题。替换emacs处的文件

如果我上的/ home/testfile的运行功能

首先,它说,文件替换等等,例如:/家庭/ secondfile 于是说更换一个 '/ home/secondfile' 有:/家庭/ secondfile 然后它说:没有文件在点

任何想法??

下面是函数:

(autoload 'ffap-guesser "ffap") 
(autoload 'ffap-read-file-or-url "ffap") 

(defun my-replace-file-at-point (currfile newfile) 
"Replace CURRFILE at point with NEWFILE. 

    When interactive, CURRFILE will need to be confirmed by user 
    and will need to exist on the file system to be recognized, 
    unless it is a URL. 

    NEWFILE does not need to exist. However, Emacs's minibuffer 
    completion can help if it needs to be. 
    " 

(interactive 
(let ((currfile (ffap-read-file-or-url "Replace filename: " 
             (ffap-guesser)))) 
    (list currfile 
     (ffap-read-file-or-url (format "Replace `%s' with: " 
             currfile) currfile)))) 
(save-match-data 
    (if (or (looking-at (regexp-quote currfile)) 
      (let ((filelen (length currfile)) 
       (opoint (point)) 
       (limit (+ (point) (length currfile)))) 
      (save-excursion 
       (goto-char (1- filelen)) 
       (and (search-forward currfile limit 
            'noerror) 
        (< (match-beginning 0) opoint)) 
        (>= (match-end 0) opoint)))) 
     (replace-match newfile) 
    (error "No file at point to replace")))) 

回答

1

可能有几件事错了/怎么回事。首先是,当你执行这个时你的点位置。第二个是,如果你正在使用/ home/user /的东西,你很有可能会在/ home/user/something和〜/ something之间产生不匹配(ffap返回后者,而你可能写了前任的)。

第一:

looking-at与正则表达式引用文件名中的使用期望点是在开头:例如|/home/user/something

其合作伙伴looking-back预计/home/user/something|。在中间的某个地方会抛出这个错误。

其中一个快速修复为此正在改变looking-atthing-at-point-looking-at

二:

如果您采写/家庭/用户/东西,FFAP功能(对我来说)这缩短使用〜。 可能有一些设置来管理这个,但我知道的最简单,快速的修复方法是使用expand-file-name。这将处理第一种情况,并且如果它被写为〜/某物,save-excursion正文将替代它。

唯一的负面结果,我看到的是,你有时可能会替换:

/home/user/something~/somethingelse

但是,不管怎么说,这两个快速修复只是导致这种彻底的改变:

(thing-at-point-looking-at (regexp-quote (expand-file-name currfile)))

+0

谢谢!是的,它不能正常工作在完整的路径名称上,除非你使用expand-file-name,但正如你所说的用简写的版本替换它。如果它已经被扩展,或者最好只是扩展文件名,如果前缀arg(C-u)被传递给函数,是否有轻易强制扩展它的方法? – 2012-08-06 22:46:31

0

看不到的地方 “FFAP-推测” 的定义。看起来像一个错误。

也许会转而

“查找文件,在点”

+0

对不起,我忘了添加自动加载。它在“ffap.el”中定义,但似乎并不是我的问题所在。它能够正确识别文件中的点,但它似乎是在替换内容的函数中。替换似乎采取我想要替换的文件,而不是原来的功能,所以当搜索发生时,它没有找到它应该取代什么。我可能是错的。 – 2012-08-04 10:42:27

+0

还添加了发生什么的确切步骤的示例。如果我用find-file-at-point,我可以用ffap-guesser来替换它吗? – 2012-08-04 11:09:40