在emacs中的点替换文件

时间:2012-08-03 22:22:46

标签: emacs elisp emacs24

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

如果我在/ home / testfile上运行该功能

首先它说要替换的文件,例如:/ home / secondfile 然后它用'/ home / secondfile替换'/ home / 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"))))

2 个答案:

答案 0 :(得分:1)

这里可能存在一些错误/错误。第一个是,当你执行它时,你的位置。第二个是,如果你正在使用/ home / user / something,你很可能会在/ home / user / something和〜/ something之间出现不匹配(ffap返回后者,而你可能已经编写了前)。

<强>首先

looking-at与regexp引用的文件名的使用期望该点位于开头:例如|/home/user/something

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

一次快速修复正在将looking-at更改为thing-at-point-looking-at

<强>第二

如果您已写/ home / user / something,ffap函数(在我的情况下)使用〜来缩短它。 可能有一些设置可以控制这一点,但我所知道的最简单,快速的解决方法是使用expand-file-name。这将处理第一种情况,如果它被写为〜/某事,save-excursion正文将在备用情况下替换它。

我看到的唯一不利结果是,您有时可能会替换:

带有/home/user/something

~/somethingelse

但是,无论如何,这两个快速修复只会导致这一彻底的改变:

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

答案 1 :(得分:0)

无法查看“ffap-guesser”的定义位置。看起来像个bug。

也许尝试改为

“找到文件-在点”

相关问题