From 81c5cf82cffd2b01fac39eaaf7a1a6ee58802e25 Mon Sep 17 00:00:00 2001 From: TEC Date: Sat, 17 Sep 2022 17:53:01 +0800 Subject: [PATCH 3/3] ob-core: Display babel execution time * lisp/ob-core.el (org-babel-execute-src-block, org-babel-format-result): Record the babel execution time, and then supplement the "Code block evaluation complete." (etc.) messages with the execution time when >0.05s. --- lisp/ob-core.el | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index c2f5eccfd..e6802e739 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -791,7 +791,7 @@ (defun org-babel-execute-src-block (&optional arg info params executor) (make-directory d 'parents) d)))) (cmd (intern (concat "org-babel-execute:" lang))) - result) + result exec-start-time) (unless (fboundp cmd) (error "No org-babel-execute function for %s!" lang)) (message "executing %s %s %s..." @@ -806,7 +806,8 @@ (defun org-babel-execute-src-block (&optional arg info params executor) (if name (format "(%s)" name) (format "at point %d" (nth 5 info))))) - (setq result + (setq exec-start-time (current-time) + result (let ((r (funcall cmd body params))) (if (and (eq (cdr (assq :result-type params)) 'value) (or (member "vector" result-params) @@ -849,7 +850,8 @@ (defun org-babel-execute-src-block (&optional arg info params executor) (if (member "none" result-params) (message "result silenced") (org-babel-insert-result - result result-params info new-hash lang))) + result result-params info new-hash lang + (time-subtract (current-time) exec-start-time)))) (run-hooks 'org-babel-after-execute-hook) result))))))) @@ -2260,7 +2262,7 @@ (defun org-babel-format-result (result &optional sep) ;; scalar result (funcall echo-res result)))) -(defun org-babel-insert-result (result &optional result-params info hash lang) +(defun org-babel-insert-result (result &optional result-params info hash lang exec-time) "Insert RESULT into the current buffer. By default RESULT is inserted after the end of the current source @@ -2268,7 +2270,8 @@ (defun org-babel-insert-result (result &optional result-params info hash lang) wrapped inside a `results' macro and placed on the same line as the inline source block. The macro is stripped upon export. Multiline and non-scalar RESULTS from inline source blocks are -not allowed. With optional argument RESULT-PARAMS controls +not allowed. When EXEC-TIME is provided it may be included in a +generated message. With optional argument RESULT-PARAMS controls insertion of results in the Org mode file. RESULT-PARAMS can take the following values: @@ -2573,11 +2576,18 @@ (defun org-babel-insert-result (result &optional result-params info hash lang) (not (and (listp result) (member "append" result-params)))) (indent-rigidly beg end indent)) - (if (null result) - (if (member "value" result-params) - (message "Code block returned no value.") - (message "Code block produced no output.")) - (message "Code block evaluation complete."))) + (let ((time-info + ;; Only show the time when something other than + ;; 0s will be shown, i.e. check if the time is at + ;; least half of the displayed precision. + (if (and exec-time (> (float-time exec-time) 0.05)) + (format " (took %.1fs)" (float-time exec-time)) + ""))) + (if (null result) + (if (member "value" result-params) + (message "Code block returned no value%s." time-info) + (message "Code block produced no output%s." time-info)) + (message "Code block evaluation complete%s." time-info)))) (set-marker end nil) (when outside-scope (narrow-to-region visible-beg visible-end)) (set-marker visible-beg nil) -- 2.37.1