Perl:我如何让Emacs在Perl脚本的完整路径中添加引号?

6
尴尬的简单问题。在Windows 7 64位上使用Emacs 23.4.1中的CPerl模式时,当我使用C-c c运行脚本时,Emacs不会将路径用引号括起来,因此任何带有空格的目录都会导致Perl无法找到文件。 "C:/Perl64/bin\perl.exe -w g:/foo/bar/first second/myscript.pl" 会生成以下错误消息: "Can't open perl script "g:/foo/bar/first": No such file or directory" 问题:如何使Emacs在将文件名传递给Perl本身时使用引号?
编辑:由于某种原因,我无法发表评论(可能是浏览器问题),因此我正在编辑原始帖子以回应@legoscia的评论:“C-c c运行命令模式编译”。在Perl菜单中,它标记为“Run”。

C-c c 似乎是一个非标准的绑定。如果您键入 C-h c C-c c,会得到什么结果? - legoscia
1
mode-compile.el 不是标准的程序包。您是否正在使用 http://perso.tls.cena.fr/boubaker/distrib/mode-compile.el 或其他程序包? - phils
@phils 我没有有意使用 mode-compile.el - 我不记得曾尝试安装它,也没有必要直接这样做,因为我只使用解释型语言(R、Perl)。也许它是随 ESS 一起安装的...?当 Perl 脚本在缓冲区中时,主模式是 CPerl,次模式是 Abbrev。没有其他明显不同的东西。 - SlowLearner
1
使用 M-x find-library RET mode-compile RET 打开该库的代码。顶部的注释可能会说明它来自哪里(并且该文件的位置可能会告诉您它是否随 ESS 一起保留在一个公共目录下的文件中?) - phils
彩票,M. Boubaker的mode-compile.el v2.29.1。那么......你会推荐我接下来做什么呢? - SlowLearner
1个回答

2
我只有可用的mode-compile.el v2.29版本,在该版本中,问题与您描述的完全相同,编译命令的参数未正确转义,并且没有选项可以启用它。
这有一点儿瑕疵,但是你应该能够重新定义相关的函数,并在文件名上使用正确的转义。在你的.emacs文件中,加入以下内容即可:
(eval-after-load 'mode-compile
  '(progn
    (defun mc--shell-compile (shell dbgflags &optional errors-regexp-alist)
      ;; Run SHELL with debug flags DBGFLAGS on current-buffer
      (let* ((shcmd   (or (mc--which shell)
                          (error "Compilation abort: command %s not found" shell)))
             (shfile  (or mc--remote-pathname (buffer-file-name)
                          (error "Compilation abort: Buffer %s has no filename"
                                 (buffer-name))))
             (run-cmd (concat shcmd " " dbgflags " " (shell-quote-argument shfile) " "
                              (setq mc--shell-args
                                    (read-string (if mode-compile-expert-p
                                                     "Argv: "
                                                   (format "Arguments to %s %s script: "
                                                           shfile shell))
                                                 mc--shell-args)))))
        ;; Modify compilation-error-regexp-alist if needed
        (if errors-regexp-alist
            (progn
              ;; Set compilation-error-regexp-alist from compile
              (or (listp errors-regexp-alist)
                  (error "Compilation abort: In mc--shell-compile errors-regexp-alist not a list."))
              ;; Add new regexp alist to compilation-error-regexp-alist
              (mapcar '(lambda(x)
                         (if (mc--member x compilation-error-regexp-alist) nil
                           (setq compilation-error-regexp-alist
                                 (append (list x)
                                         compilation-error-regexp-alist))))
                      errors-regexp-alist)))
        ;; Run compile with run-cmd
        (mc--compile run-cmd)))))

我修改的那一行变了

(run-cmd (concat shcmd " " dbgflags " " shfile " "

to

(run-cmd (concat shcmd " " dbgflags " " (shell-quote-argument shfile) " "

更完整的修复方法是在设置dbgflags(无论在何处设置,仅转义整个变量不正确)和mc--shell-args时也进行转义。


1
+1,但我建议写信给作者并在源代码中修复此问题。另外,如果您在elisp代码前加上<!-- language: lang-el -->,Stack Overflow将正确地突出显示语法。 - phils
有用的提示,谢谢,确实,将补丁发送到上游始终是明智的。 - Sam Graham

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接