;;; tq.el --- utility to maintain a transaction queue
-;; Copyright (C) 1985, 1986, 1987, 1992, 2002, 2003, 2004,
-;; 2005, 2006 Free Software Foundation, Inc.
+;; Copyright (C) 1985-1987, 1992, 2001-2011 Free Software Foundation, Inc.
;; Author: Scott Draves <spot@cs.cmu.edu>
;; Maintainer: FSF
;; This file is part of GNU Emacs.
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; regexp: regular expression that matches the end of a response from
;; the process
(defun tq-queue-head-regexp (tq) (car (cdr (car (tq-queue tq)))))
-;; closure: additional data to pass to function
+;; closure: additional data to pass to the function
(defun tq-queue-head-closure (tq) (car (cdr (cdr (car (tq-queue tq))))))
;; fn: function to call upon receiving a complete response from the
;; process
(generate-new-buffer
(concat " tq-temp-"
(process-name process)))))))
+ (buffer-disable-undo (tq-buffer tq))
(set-process-filter process
`(lambda (proc string)
(tq-filter ',tq string)))
(defun tq-queue-pop (tq)
(setcar tq (cdr (car tq)))
(let ((question (tq-queue-head-question tq)))
- (when question
- (process-send-string (tq-process tq) question)))
+ (condition-case nil
+ (process-send-string (tq-process tq) question)
+ (error nil)))
(null (car tq)))
(defun tq-enqueue (tq question regexp closure fn &optional delay-question)
the process has finished replying to any previous questions.
This produces more reliable results with some processes."
(let ((sendp (or (not delay-question)
- (not (tq-queue-head-question tq)))))
+ (not (tq-queue tq)))))
(tq-queue-add tq (unless sendp question) regexp closure fn)
(when sendp
(process-send-string (tq-process tq) question))))
(defun tq-filter (tq string)
"Append STRING to the TQ's buffer; then process the new data."
- (with-current-buffer (tq-buffer tq)
- (goto-char (point-max))
- (insert string)
- (tq-process-buffer tq)))
+ (let ((buffer (tq-buffer tq)))
+ (when (buffer-live-p buffer)
+ (with-current-buffer buffer
+ (goto-char (point-max))
+ (insert string)
+ (tq-process-buffer tq)))))
(defun tq-process-buffer (tq)
"Check TQ's buffer for the regexp at the head of the queue."
- (set-buffer (tq-buffer tq))
- (if (= 0 (buffer-size)) ()
- (if (tq-queue-empty tq)
- (let ((buf (generate-new-buffer "*spurious*")))
- (copy-to-buffer buf (point-min) (point-max))
- (delete-region (point-min) (point))
- (pop-to-buffer buf nil)
- (error "Spurious communication from process %s, see buffer %s"
- (process-name (tq-process tq))
- (buffer-name buf)))
- (goto-char (point-min))
- (if (re-search-forward (tq-queue-head-regexp tq) nil t)
- (let ((answer (buffer-substring (point-min) (point))))
- (delete-region (point-min) (point))
- (unwind-protect
- (condition-case nil
- (funcall (tq-queue-head-fn tq)
- (tq-queue-head-closure tq)
- answer)
- (error nil))
- (tq-queue-pop tq))
- (tq-process-buffer tq))))))
+ (let ((buffer (tq-buffer tq)))
+ (when (buffer-live-p buffer)
+ (set-buffer buffer)
+ (if (= 0 (buffer-size)) ()
+ (if (tq-queue-empty tq)
+ (let ((buf (generate-new-buffer "*spurious*")))
+ (copy-to-buffer buf (point-min) (point-max))
+ (delete-region (point-min) (point))
+ (pop-to-buffer buf nil)
+ (error "Spurious communication from process %s, see buffer %s"
+ (process-name (tq-process tq))
+ (buffer-name buf)))
+ (goto-char (point-min))
+ (if (re-search-forward (tq-queue-head-regexp tq) nil t)
+ (let ((answer (buffer-substring (point-min) (point))))
+ (delete-region (point-min) (point))
+ (unwind-protect
+ (condition-case nil
+ (funcall (tq-queue-head-fn tq)
+ (tq-queue-head-closure tq)
+ answer)
+ (error nil))
+ (tq-queue-pop tq))
+ (tq-process-buffer tq))))))))
(provide 'tq)
-;;; arch-tag: 65dea08c-4edd-4cde-83a5-e8a15b993b79
;;; tq.el ends here