Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: How can I get Emacs to add quotes to full path of a Perl script?

Tags:

emacs

perl

Embarassingly simple question. Using CPerl mode in Emacs 23.4.1 on Windows 7 64, when I use C-c c to run the script Emacs doesn't wrap the path in quotes, so any directories with spaces cause Perl to be unable to find the file. "C:/Perl64/bin\perl.exe -w g:/foo/bar/first second/myscript.pl" generates this error message: "Can't open perl script "g:/foo/bar/first": No such file or directory"

Question: how do I make Emacs use quotes when passing the file name to Perl itself?

Edit: for some reason I can't comment (perhaps a browser issue) so I am editing the original post in response to the comment from @legoscia: "C-c c runs the command mode-compile". In the Perl menu it is marked as "Run".

like image 414
SlowLearner Avatar asked Jun 19 '12 07:06

SlowLearner


1 Answers

I only have mode-compile.el v2.29 available, but in that version the issue is exactly as you describe, the arguments to the compile command are not escaped properly, and there's no option to enable it.

It's a bit of hack but you should be able to redefine the relevant function with correct escaping for the filename with this in your .emacs file:

(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)))))

The line I changed was changing

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

to

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

A more complete fix would be to also escape the dbgflags (wherever they're set, just escaping the entire variable won't be right) and mc--shell-args when they're set too.

like image 70
Sam Graham Avatar answered Sep 17 '22 10:09

Sam Graham