diff options
Diffstat (limited to 'chess-process.el')
| -rw-r--r-- | chess-process.el | 105 |
1 files changed, 72 insertions, 33 deletions
diff --git a/chess-process.el b/chess-process.el index 729cc8b..21d4cdb 100644 --- a/chess-process.el +++ b/chess-process.el @@ -36,23 +36,7 @@ related to the resulting process.") (make-variable-buffer-local 'chess-process-last-pos) (make-variable-buffer-local 'chess-process-working) -(defvar chess-process-triggers - (list (list - (concat "\\s-*\\(white\\|black\\)\\s-*([0-9]+):\\s-+\\(" - chess-algebraic-regexp "\\)\\s-*$") - (function - (lambda (color move) - (if (if (chess-game-side-to-move chess-process-game) - (string= (downcase color) "white") - (string= (downcase color) "black")) - (chess-session-event - chess-current-session 'move - (chess-algebraic-to-ply - (chess-game-pos chess-process-game) move))))) - 1 2) - '(".*illegal move:\\s-*\\(.*\\)" - (signal 'chess-illegal (match-string 1))) - '(".+?\015" (replace-match ""))) +(defvar chess-process-triggers nil "A list of regexps and the commands that they trigger. The alist should be of the form: @@ -102,25 +86,15 @@ must be handled by modules that derive from this module.") (set-process-filter proc 'chess-process-filter)) buf))) ((eq event 'shutdown) - (when (buffer-live-p buffer) - (ignore-errors - (process-send-string (get-buffer-process buffer) "quit\n")) - (kill-buffer buffer))) + (if (buffer-live-p buffer) + (kill-buffer buffer))) (t (ignore (with-current-buffer buffer - (let (cmdstr) - (cond - ((eq event 'setup) - (setq chess-process-game (car args) - chess-process-last-pos (point-min))) - ((eq event 'move) - (setq cmdstr (concat (chess-ply-to-algebraic (car args)) "\n"))) - ((eq event 'pass) - (setq cmdstr "go\n"))) - (if (and cmdstr (not chess-process-working)) - (process-send-string (get-buffer-process (current-buffer)) - cmdstr)))))))) + (cond + ((eq event 'setup) + (setq chess-process-game (car args) + chess-process-last-pos (point-min))))))))) (defun chess-process-filter (proc string) "Process filter for receiving text from a chess process." @@ -163,6 +137,71 @@ must be handled by modules that derive from this module.") (setq chess-process-last-pos (point) chess-process-working nil))))))) +(defun chess-process-let (forms) + `(let ((str (progn ,@forms))) + (if (stringp str) + (ignore + (process-send-string (get-buffer-process (current-buffer)) + (concat str "\n"))) + str))) + +(defun chess-process-insert-forms (event) + (if (assq event forms) + (chess-process-let + (prog1 + (cdr (assq event forms)) + (setq forms (assq-delete-all event forms)))))) + +(defmacro define-chess-engine (name ignored triggers &rest forms) + "Define a chess engine. +NAME is an unquoted symbol name that denotes the engine. This name is +used as the default string for the chess engine's external command +name. +TRIGGERS is a list of process triggers, which fire when the output +from the process matches certain regexps. See +`chess-process-triggers' for more information. +FORMS is an alist of event symbols, and forms to evaluate when such an +event is received by the module. If these forms return a string, this +string will be sent to the engine process. +See the file chess-engines.el for code examples." + (let ((namestr (symbol-name name))) + `(progn + (defcustom ,(intern (concat "chess-" namestr "-command")) + (and (require 'executable) + (executable-find ,namestr)) + ,(concat "The name of the " namestr " program.") + :type 'file + :group 'chess-process) + + (defun ,(intern (concat "chess-" namestr)) + (session buffer event &rest args) + (cond + ((eq event 'initialize) + (with-current-buffer + (chess-process session buffer event ,triggers + ,(intern (concat "chess-" namestr "-command"))) + ,(chess-process-insert-forms 'init) + (current-buffer))) + ((eq event 'shutdown) + (when (buffer-live-p buffer) + (ignore-errors + ,(chess-process-insert-forms 'shutdown)) + (kill-buffer buffer))) + (t + (ignore + (with-current-buffer buffer + (cond + ((eq event 'setup) + (apply 'chess-process session buffer event args) + ,(chess-process-insert-forms 'setup)) + ,@(mapcar + (function + (lambda (entry) + `((eq event (quote ,(car entry))) + ,(chess-process-let (cdr entry))))) forms) + (t + (apply 'chess-process session buffer event args))))))))))) + (provide 'chess-process) ;;; chess-process.el ends here |
