ein-kernelinfo.el 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ;;; ein-kernelinfo.el --- Kernel info module
  2. ;; Copyright (C) 2012 Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-kernelinfo.el is free software: you can redistribute it and/or
  6. ;; modify it under the terms of the GNU General Public License as
  7. ;; published by the Free Software Foundation, either version 3 of the
  8. ;; License, or (at your option) any later version.
  9. ;; ein-kernelinfo.el is distributed in the hope that it will be
  10. ;; useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  11. ;; of 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-kernelinfo.el. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (require 'ein-kernel)
  20. (require 'eieio)
  21. (defclass ein:kernelinfo ()
  22. ((kernel
  23. :initarg :kernel :type ein:$kernel
  24. :documentation "Kernel instance.")
  25. (get-buffers
  26. :initarg :get-buffers
  27. :documentation "A packed function to get buffers associated
  28. with the kernel. The buffer local `default-directory' variable
  29. in these buffer will be synced with the kernel's cwd.")
  30. (hostname
  31. :initarg :hostname :type string
  32. :documentation "Host name of the machine where the kernel is running on.")
  33. (ccwd
  34. :initarg :ccwd :type string
  35. :documentation "cached CWD (last time checked CWD)."))
  36. :documentation "Info related (but unimportant) to kernel")
  37. (defun ein:kernelinfo-new (kernel get-buffers)
  38. "Make a new `ein:kernelinfo' instance based on KERNEL and GET-BUFFERS."
  39. (let ((kerinfo (make-instance 'ein:kernelinfo)))
  40. (oset kerinfo :kernel kernel)
  41. (oset kerinfo :get-buffers get-buffers)
  42. (ein:kernelinfo-setup-hooks kerinfo)
  43. kerinfo))
  44. (defun ein:kernelinfo-setup-hooks (kerinfo)
  45. "Add `ein:kernelinfo-update-*' to `ein:$kernel-after-*-hook'."
  46. (with-slots (kernel) kerinfo
  47. (push (cons #'ein:kernelinfo-update-all kerinfo)
  48. (ein:$kernel-after-start-hook kernel))
  49. (push (cons #'ein:kernelinfo-update-ccwd kerinfo)
  50. (ein:$kernel-after-execute-hook kernel))))
  51. (defun ein:kernelinfo-update-all (kerinfo)
  52. "Update KERINFO slots by triggering all update functions."
  53. (ein:log 'debug "EIN:KERNELINFO-UPDATE-ALL")
  54. (ein:log 'debug "(ein:kernel-live-p kernel) = %S"
  55. (ein:kernel-live-p (oref kerinfo :kernel)))
  56. (ein:kernelinfo-update-ccwd kerinfo)
  57. (ein:kernelinfo-update-hostname kerinfo))
  58. (defun ein:kernelinfo-update-ccwd (kerinfo)
  59. "Update cached current working directory (CCWD) and change
  60. `default-directory' of kernel related buffers."
  61. (ein:kernel-request-stream
  62. (oref kerinfo :kernel)
  63. "__import__('sys').stdout.write(__import__('os').getcwd())"
  64. (lambda (cwd kerinfo)
  65. (with-slots (kernel get-buffers) kerinfo
  66. (setq cwd (ein:kernel-filename-from-python kernel cwd))
  67. (oset kerinfo :ccwd cwd)
  68. ;; sync buffer's `default-directory' with CWD
  69. (when (file-accessible-directory-p cwd)
  70. (mapc (lambda (buffer)
  71. (with-current-buffer buffer
  72. (setq default-directory (file-name-as-directory cwd))))
  73. (ein:filter #'buffer-live-p
  74. (ein:funcall-packed get-buffers))))))
  75. (list kerinfo)))
  76. (defun ein:kernelinfo-update-hostname (kerinfo)
  77. "Get hostname in which kernel is running and store it in KERINFO."
  78. (ein:kernel-request-stream
  79. (oref kerinfo :kernel)
  80. "__import__('sys').stdout.write(__import__('os').uname()[1])"
  81. (lambda (hostname kerinfo)
  82. (oset kerinfo :hostname hostname))
  83. (list kerinfo)))
  84. (provide 'ein-kernelinfo)
  85. ;;; ein-kernelinfo.el ends here