From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Schulte Subject: Re: [PATCH 1/2] clean up two macros in ob-comint Date: Tue, 29 Oct 2013 19:53:29 -0600 Message-ID: <87txfzbjzp.fsf@gmail.com> References: <1383001124-6513-1-git-send-email-aaronecay@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:58765) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VbL0m-00036x-58 for emacs-orgmode@gnu.org; Tue, 29 Oct 2013 21:56:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VbL0g-0005rB-Gy for emacs-orgmode@gnu.org; Tue, 29 Oct 2013 21:55:56 -0400 Received: from mail-pb0-x236.google.com ([2607:f8b0:400e:c01::236]:36834) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VbL0g-0005r6-5k for emacs-orgmode@gnu.org; Tue, 29 Oct 2013 21:55:50 -0400 Received: by mail-pb0-f54.google.com with SMTP id ro8so670255pbb.41 for ; Tue, 29 Oct 2013 18:55:49 -0700 (PDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Aaron Ecay Cc: emacs-orgmode@gnu.org Applied, thanks! Aaron Ecay writes: > * lisp/ob-comint.el (org-babel-comint-with-output, > org-babel-comint-in-buffer): clean up code. > > This patch replaces some deeply nested ca/dr calls with =E2=80=98nth=E2= =80=99, and > replaces a setq/unwind-protect/setq incantation with a simple let > binding. Finally, it also restructures =E2=80=99org-babel-comint-in-buff= er=E2=80=99 > to not needlessly save/restore match and excursion data if the buffer > process is not live, and to use with-current-buffer instead of > save-excursion+set-buffer > (info "(elisp) Excursions") > --- > lisp/ob-comint.el | 84 ++++++++++++++++++++++++++-----------------------= ------ > 1 file changed, 40 insertions(+), 44 deletions(-) > > diff --git a/lisp/ob-comint.el b/lisp/ob-comint.el > index 8b03e2d..4e2b352 100644 > --- a/lisp/ob-comint.el > +++ b/lisp/ob-comint.el > @@ -48,12 +48,12 @@ BUFFER is checked with `org-babel-comint-buffer-livep= '. BODY is > executed inside the protection of `save-excursion' and > `save-match-data'." > (declare (indent 1)) > - `(save-excursion > + `(progn > + (unless (org-babel-comint-buffer-livep ,buffer) > + (error "Buffer %s does not exist or has no process" ,buffer)) > (save-match-data > - (unless (org-babel-comint-buffer-livep ,buffer) > - (error "Buffer %s does not exist or has no process" ,buffer)) > - (set-buffer ,buffer) > - ,@body))) > + (with-current-buffer ,buffer > + ,@body)))) > (def-edebug-spec org-babel-comint-in-buffer (form body)) >=20=20 > (defmacro org-babel-comint-with-output (meta &rest body) > @@ -69,46 +69,42 @@ elements are optional. > This macro ensures that the filter is removed in case of an error > or user `keyboard-quit' during execution of body." > (declare (indent 1)) > - (let ((buffer (car meta)) > - (eoe-indicator (cadr meta)) > - (remove-echo (cadr (cdr meta))) > - (full-body (cadr (cdr (cdr meta))))) > + (let ((buffer (nth 0 meta)) > + (eoe-indicator (nth 1 meta)) > + (remove-echo (nth 2 meta)) > + (full-body (nth 3 meta))) > `(org-babel-comint-in-buffer ,buffer > - (let ((string-buffer "") dangling-text raw) > - ;; setup filter > - (setq comint-output-filter-functions > - (cons (lambda (text) (setq string-buffer (concat string-buffer t= ext))) > - comint-output-filter-functions)) > - (unwind-protect > - (progn > - ;; got located, and save dangling text > - (goto-char (process-mark (get-buffer-process (current-buffer)))) > - (let ((start (point)) > - (end (point-max))) > - (setq dangling-text (buffer-substring start end)) > - (delete-region start end)) > - ;; pass FULL-BODY to process > - ,@body > - ;; wait for end-of-evaluation indicator > - (while (progn > - (goto-char comint-last-input-end) > - (not (save-excursion > - (and (re-search-forward > - (regexp-quote ,eoe-indicator) nil t) > - (re-search-forward > - comint-prompt-regexp nil t))))) > - (accept-process-output (get-buffer-process (current-buffer))) > - ;; thought the following this would allow async > - ;; background running, but I was wrong... > - ;; (run-with-timer .5 .5 'accept-process-output > - ;; (get-buffer-process (current-buffer))) > - ) > - ;; replace cut dangling text > - (goto-char (process-mark (get-buffer-process (current-buffer)))) > - (insert dangling-text)) > - ;; remove filter > - (setq comint-output-filter-functions > - (cdr comint-output-filter-functions))) > + (let ((string-buffer "") > + (comint-output-filter-functions > + (cons (lambda (text) (setq string-buffer (concat string-buffer te= xt))) > + comint-output-filter-functions)) > + dangling-text raw) > + ;; got located, and save dangling text > + (goto-char (process-mark (get-buffer-process (current-buffer)))) > + (let ((start (point)) > + (end (point-max))) > + (setq dangling-text (buffer-substring start end)) > + (delete-region start end)) > + ;; pass FULL-BODY to process > + ,@body > + ;; wait for end-of-evaluation indicator > + (while (progn > + (goto-char comint-last-input-end) > + (not (save-excursion > + (and (re-search-forward > + (regexp-quote ,eoe-indicator) nil t) > + (re-search-forward > + comint-prompt-regexp nil t))))) > + (accept-process-output (get-buffer-process (current-buffer))) > + ;; thought the following this would allow async > + ;; background running, but I was wrong... > + ;; (run-with-timer .5 .5 'accept-process-output > + ;; (get-buffer-process (current-buffer))) > + ) > + ;; replace cut dangling text > + (goto-char (process-mark (get-buffer-process (current-buffer)))) > + (insert dangling-text) > + > ;; remove echo'd FULL-BODY from input > (if (and ,remove-echo ,full-body > (string-match --=20 Eric Schulte https://cs.unm.edu/~eschulte PGP: 0x614CA05D