Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.7 KiB

  1. ;;; ein-timestamp.el --- Elisp implementation of the ExecuteTime nbextension
  2. ;; Copyright (C) 2018- John M. Miller
  3. ;; Author: John M. Miller <millejoh at mac.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-timestamp.el is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; ein-timestamp.el is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with ein-timestamp.el. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; A rough approximation of the ExecuteTime (https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/execute_time/readme.html) nbextension
  17. ;;; Code:
  18. (require 'ein-kernel)
  19. (require 'ein-cell)
  20. (defcustom ein:timestamp-format "%FT%T"
  21. "The format spec for timestamps.
  22. See `ein:format-time-string'."
  23. :type '(or string function)
  24. :group 'ein)
  25. (defun ein:timestamp--shell-reply-hook (msg-type header content metadata)
  26. (when (string-equal msg-type "execute_reply")
  27. (let ((start-time (plist-get metadata :started))
  28. (end-time (plist-get header :date)))
  29. (plist-put metadata :execute-time (list start-time end-time)))))
  30. (defun ein:timestamp--execute-reply-hook (cell content metadata)
  31. (if-let ((etime (plist-get metadata :execute-time)))
  32. (if (ein:cell-metadata cell)
  33. (plist-put (ein:cell-metadata cell)
  34. :execute-time
  35. etime)
  36. (setf (ein:cell-metadata cell) (list :execute-time etime))))
  37. (ein:cell-running-set cell nil)
  38. (let ((buffer-undo-list t))
  39. (ewoc-invalidate (ein:basecell--ewoc cell) (ein:cell-element-get cell :footer))))
  40. (cl-defmethod ein:cell-insert-footer :after ((cell ein:codecell))
  41. (if (slot-value cell 'running)
  42. (ein:insert-read-only "Execution pending\n\n")
  43. (if-let ((etime (plist-get (ein:cell-metadata cell) :execute-time)))
  44. (let ((start-time (date-to-time (car etime)))
  45. (end-time (date-to-time (cadr etime))))
  46. (ein:insert-read-only
  47. (format "Last executed %s in %ss\n\n"
  48. (ein:format-time-string ein:timestamp-format start-time)
  49. (float-time (time-subtract end-time start-time))))))))
  50. (add-hook 'ein:on-shell-reply-functions 'ein:timestamp--shell-reply-hook)
  51. (add-hook 'ein:on-execute-reply-functions 'ein:timestamp--execute-reply-hook)
  52. (provide 'ein-timestamp)