【问题标题】:Replace file at point in emacs在emacs中替换文件
【发布时间】:2012-08-03 22:22:46
【问题描述】:

我在这里找到了替换 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"))))

【问题讨论】:

    标签: emacs elisp emacs24


    【解决方案1】:

    这里可能有一些错误/正在发生的事情。第一个是执行此操作时的点位置。第二个是,如果您使用的是 /home/user/something,则很有可能 /home/user/something 和 ~/something 之间存在不匹配(ffap 返回后者,而此时您可能已经编写了前)。

    第一:

    looking-at 与正则表达式引用的文件名一起使用,预计该点位于开头:例如|/home/user/something.

    它的合作伙伴looking-back 期望/home/user/something|。处于中间位置会引发此错误。

    对此的一个快速修复是将looking-at更改为thing-at-point-looking-at

    第二:

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

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

    /home/user/something~/somethingelse

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

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

    【讨论】:

    • 谢谢!!是的,就是它不能在完整路径名上正常工作,除非你使用 expand-file-name 但正如你所说的用缩短的版本替换它。无论如何,如果它已经被扩展或者如果前缀 arg (C-u) 被传递给函数,最好只是扩展文件名。
    【解决方案2】:

    看不到“ffap-guesser”的定义位置。看起来像一个错误。

    不妨试试

    “点查找文件”

    【讨论】:

    • 对不起,我忘了添加自动加载。它在“ffap.el”中定义,但这似乎不是我的问题所在。据我所知,它可以正确识别文件点,但它似乎在替换内容的功能中。 Replace 似乎采用了我要替换的文件而不是原始函数,因此当搜索发生时,它找不到它应该替换的内容。我可能错了。
    • 还添加了发生的确切步骤的示例。如果我使用find-file-at-point,我可以用ffap-guesser替换它吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多