From fec859e58745990dab9a3814ac9044fd1dfa97d4 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Sun, 5 Mar 2017 22:14:34 -0800 Subject: [PATCH] Move the silently macro to helpers library --- emacs/.emacs.d/site-lisp/oni-helpers.el | 27 +++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/emacs/.emacs.d/site-lisp/oni-helpers.el b/emacs/.emacs.d/site-lisp/oni-helpers.el index 3b3ad99..1807658 100644 --- a/emacs/.emacs.d/site-lisp/oni-helpers.el +++ b/emacs/.emacs.d/site-lisp/oni-helpers.el @@ -24,10 +24,33 @@ ;;; Code: +(defmacro silently (title &rest body) + "Only output something when an error occurs. +Prefix with TITLE any output that occurs while executing BODY, +but only when an error occurs, otherwise discard it." + (declare (indent 1)) + (let ((buffer-var (cl-gensym)) + (error-var (cl-gensym))) + `(with-temp-buffer + (let ((,buffer-var (current-buffer))) + (cl-letf (((symbol-function 'message) + (lambda (msg &rest args) + (with-current-buffer ,buffer-var + (insert " " (apply 'format msg args) "\n"))))) + (condition-case ,error-var + (progn ,@body) + (error + (princ ,(concat title " output:\n")) + (princ (with-current-buffer ,buffer-var (buffer-string))) + (princ "Error:\n") + (princ " ") + (princ (cadr ,error-var)) + (princ "\n")))))))) + (defun oni:data-location (file-name) "Return the location of FILE-NAME within my data directory. - This is currently the data directory under the - `user-emacs-directory'." +This is currently the data directory under the +`user-emacs-directory'." (concat user-emacs-directory "data/" file-name)) (with-eval-after-load 'ert