Elisp sentinel on process waiting for input

删除回忆录丶 提交于 2019-12-11 06:34:30

问题


I made a function, compiling the current latex file:

;close the *async pdflatex* window, when pdflatex finishes
(defun latex-sentinel (process event)
      (message event)
      (cond ((string-match-p "finished" event)
                  (progn
                        (kill-buffer "*async pdflatex*")
                        (message "pdflatex done")
                        (delete-other-windows)))))

(defun latex-compile ()
      "Runs pdflatex on current file"
      (interactive)
      (let* ((file-name (shell-quote-argument (buffer-file-name)))
            (process (start-process-shell-command
                           "pdflatex"
                           "*async pdflatex*"
                           (concat "pdflatex " file-name))))
            (set-process-sentinel process 'latex-sentinel)
            (setq new-window (split-window-below 40))
            (set-window-buffer new-window "*async pdflatex*")
            (other-window 1)))

(add-hook 'LaTeX-mode-hook (lambda () 
    (define-key LaTeX-mode-map (kbd "<f2>") 'latex-compile)))

When there is an error, while compiling, pdflatex freezes, and I see this:

My current workflow:

  1. Scroll up to see the error
  2. kill-this-buffer - to kill pdflatex process
  3. delete-window - to close *async pdflatex* window and get back to editing.

Is it possible to track, that the process has stopped and is waiting for user input? My current sentinel activates only on "finished" event. So I could automate my current workflow.


回答1:


[ @Thomas: you don't need AUCTeX for that. The builtin latex-mode also provides a C-c C-c binding for that task. ]

In general, detecting that a process is "waiting for me" is difficult/impossible (at best you might monitor the process's CPU usage and if it's been 0% for a while you can decide that it's probably waiting for you, but even doing that is tricky since you need to find the proper OS-process to monitor and then use system-dependent operations to get the CPU usage). latex-modes usually "solve" this by passing \nonstopmode\input on pdflatex's command line.




回答2:


I had the same difficulty. The following seems to be OK.

(defun latex-compile()
  (interactive)
  (save-buffer)
  (set 'tex-to-pdf   "pdflatex -interaction=nonstopmode")
  (set 'file-name (shell-quote-argument (buffer-file-name)))
  (set 'comm1 (concat tex-to-pdf " " file-name ))
  (set-process-sentinel
   (start-process-shell-command "latex-compile"
                "*async latex-compile*"
                comm1)
   'tex-to-pdf-sentinel))
;;;
(defun tex-to-pdf-sentinel (process event)
    (cond
     ( (string-match-p "finished" event)
     (message "latex-compile complete"))
     ( (string-match-p "\\(exited\\|dumped\\)" event)
     (message "error in latex-compile"))
     ))

I realize use of global variables with set is not advisable; this is just a minimal example.

You can set up a second sentilel when the first completes, e.g. so that a viewer opens to display the the .pdf output, or you want to ensure bibtex runs each time. This may save on repeated calls to C-c C-c in latex mode.



来源:https://stackoverflow.com/questions/16887855/elisp-sentinel-on-process-waiting-for-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!