2012-08-03 47 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