如何连接要在启动过程中使用的双引号

时间:2013-10-14 18:18:11

标签: emacs elisp

任何人都可以给我一个连接双引号的手:

在这个例子中,我正在编写一个与Windows操作系统上的Emacs一起使用的函数。带空格的文件名需要用双引号括起来。

缓冲区文件名是:c:/Documents and Settings/All Users/Desktop/foo.tex

我正在尝试使用:

(setq pdf-file
  (concat "\"" (car (split-string (buffer-file-name) "\\.tex")) ".pdf" "\""))

当我致电(start-process "display" nil c:/SumatraPDF.exe pdf-file)时,pdf查看器会尝试打开它:

c:\Documents and Settings\All Users\Desktop"c:\Documents and Settings\All Users\Desktop\foo.pdf"

编辑:这是我尝试修改的功能。正如我在下面的回答中所述,错误是由于我无意中使用 double-double 引用了文件名 - 即start-process将变量视为双引号,因此我从来没有需要连接另一组双引号。

(defun latexmk ()
".latexmkrc contains the following entries:
  $pdflatex = 'pdflatex -file-line-error -synctex=1 %O %S';
  $pdf_mode = 1;
  $recorder = 0;
  $clean_ext = 'synctex.gz synctex.gz(busy) aux fdb_latexmk log';"
(interactive)
  (setq tex-file (file-name-nondirectory buffer-file-name))
  (setq base-file (car (split-string (file-name-nondirectory buffer-file-name) "\\.tex")))
  (setq pdf-file (concat base-file ".pdf"))
  (setq line (format "%d" (line-number-at-pos)))
  (setq sumatra "C:/SumatraPDF/SumatraPDF.exe")
  (setq tex-output (concat "*" (file-name-nondirectory buffer-file-name) "*") )
  (setq latexmk "c:/texlive/2013/bin/win32/latexmk.exe")
  (setq latexmkrc "c:/Documents and Settings/Administrator/Application Data/.emacs.d/.latexmkrc")
  (if (buffer-modified-p)
    (save-buffer))
  (delete-other-windows)
  (set-window-buffer (split-window-horizontally) (get-buffer-create tex-output))
  (with-current-buffer tex-output (erase-buffer))
  (start-process "tskill" nil "c:/WINDOWS/system32/tskill.exe" "SumatraPDF")
  (set-process-sentinel 
     (start-process "deep-clean" nil latexmk "-C" "-r" latexmkrc tex-file)
     (lambda (p e) (when (= 0 (process-exit-status p))
       (set-process-sentinel 
         (start-process "compile" tex-output latexmk "-r" latexmkrc tex-file)
         (lambda (p e) (when (= 0 (process-exit-status p))
       (set-process-sentinel 
           (start-process "displayline" nil sumatra "-forward-search" tex-file line pdf-file)
           (lambda (p e) (when (= 0 (process-exit-status p))
             (start-process "clean" nil latexmk "-c" "-r" latexmkrc tex-file)
             (switch-to-buffer (get-file-buffer tex-file))
             (if (get-buffer-process (get-buffer tex-output))
               (process-kill-without-query (get-buffer-process (get-buffer tex-output))))
             (kill-buffer tex-output)
             (delete-other-windows)))))))))))

3 个答案:

答案 0 :(得分:2)

我猜你想要:

(replace-regexp-in-string
 "\\.tex$"
 ".pdf"
 "c:/Documents and Settings/All Users/Desktop/foo.tex")    

答案 1 :(得分:2)

(setq pdf-file (concat (file-name-sans-extension (buffer-file-name)) ".pdf"))

此外,如果您需要引用传递给shell的参数,请使用shell-quote-argument

答案 2 :(得分:0)

start-process将变量视为双引号。本质上,我的变量最终被 double-double - 引用 - 这是阻止我使用文件的绝对路径的原因。因此,简而言之,当使用start-process变量时,需要进行双引号。

感谢所有帮助我解决问题的人 - 非常感谢!

以下是修订后的工作职能:

(defun latexmk ()
".latexmkrc contains the following entries:
  $pdflatex = 'pdflatex -file-line-error -synctex=1 %O %S';
  $pdf_mode = 1;
  $recorder = 0;
  $clean_ext = 'synctex.gz synctex.gz(busy) aux fdb_latexmk log';"
(interactive)
  (setq tex-file (buffer-file-name))
  (setq base-file (car (split-string (buffer-file-name) "\\.tex")))
  (setq pdf-file (concat base-file ".pdf"))
  (setq line (format "%d" (line-number-at-pos)))
  (setq sumatra "c:/Program Files/SumatraPDF/SumatraPDF.exe")
  (setq tex-output (concat "*" (file-name-nondirectory buffer-file-name) "*") )
  (setq latexmk "c:/texlive/2013/bin/win32/latexmk.exe")
  (setq latexmkrc "c:/Documents and Settings/Administrator/Application Data/.emacs.d/.latexmkrc")
  (if (buffer-modified-p)
    (save-buffer))
  (delete-other-windows)
  (set-window-buffer (split-window-horizontally) (get-buffer-create tex-output))
  (with-current-buffer tex-output (erase-buffer))
  (start-process "tskill" nil "c:/WINDOWS/system32/tskill.exe" "SumatraPDF")
  (set-process-sentinel 
     (start-process "deep-clean" nil latexmk "-C" "-r" latexmkrc tex-file)
     (lambda (p e) (when (= 0 (process-exit-status p))
       (set-process-sentinel 
         (start-process "compile" tex-output latexmk "-r" latexmkrc tex-file)
         (lambda (p e) (when (= 0 (process-exit-status p))
          (start-process "displayline" nil sumatra "-forward-search" tex-file line pdf-file)
          (start-process "clean" nil latexmk "-c" "-r" latexmkrc tex-file)
          (switch-to-buffer (get-file-buffer tex-file))
          (if (get-buffer-process (get-buffer tex-output))
            (process-kill-without-query (get-buffer-process (get-buffer tex-output))))
          (kill-buffer tex-output)
          (delete-other-windows))))))))