Merge branch 'master' into xwidget
[emacs.git] / lisp / emacs-parallel / parallel-remote.el
blob54626afc2677546dad271eae53721ac482c373b1
1 ;; -*- mode: emacs-lisp; lexical-binding: t; -*-
2 ;;; parallel-remote.el ---
4 ;; Copyright (C) 2013 Grégoire Jadi
6 ;; Author: Grégoire Jadi <gregoire.jadi@gmail.com>
8 ;; This program is free software: you can redistribute it and/or
9 ;; modify it under the terms of the GNU General Public License as
10 ;; published by the Free Software Foundation, either version 3 of
11 ;; the License, or (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;;; Code:
25 (require 'cl)
27 (defvar parallel-service nil)
28 (defvar parallel-task-id nil)
29 (defvar parallel-client nil)
30 (defvar parallel--executed nil)
31 (defvar parallel-continue-when-executed nil)
33 (defun parallel-remote-send (data)
34 (process-send-string parallel-client
35 (format "%S " (cons parallel-task-id data))))
37 (defun parallel-remote--init ()
38 (setq parallel-client (make-network-process :name "emacs-parallel"
39 :buffer nil
40 :server nil
41 :service parallel-service
42 :host "localhost"
43 :family 'ipv4))
44 (set-process-filter parallel-client #'parallel-remote--filter)
45 (parallel-remote-send 'code)
46 (when noninteractive ; Batch Mode
47 ;; The evaluation is done in the `parallel--filter' but in Batch
48 ;; Mode, Emacs doesn't wait for the input, it stops as soon as
49 ;; `parallel--init' has been executed.
50 (while (null parallel--executed)
51 (sleep-for 10)))) ; arbitrary chosen
53 (defun parallel-remote--filter (_proc output)
54 (dolist (code (parallel--read-output output))
55 (parallel-remote-send
56 (if (or noninteractive
57 (not debug-on-error))
58 (condition-case err
59 (eval code)
60 (error err))
61 (eval code))))
62 (unless parallel-continue-when-executed
63 (setq parallel--executed t)
64 (kill-emacs)))
66 (defun parallel--read-output (output)
67 "Read lisp forms from output and return them as a list."
68 (loop with output = (replace-regexp-in-string
69 "\\`[ \t\n]*" ""
70 (replace-regexp-in-string "[ \t\n]*\\'" "" output)) ; trim string
71 with start = 0
72 with end = (length output)
73 for ret = (read-from-string output start end)
74 for data = (first ret)
75 do (setq start (rest ret))
76 collect data
77 until (= start end)))
79 (provide 'parallel-remote)
81 ;;; parallel-remote.el ends here