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.

115 lines
4.4 KiB

  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. (require 'ein-kernel)
  19. (require 'eieio)
  20. (defclass ein:kernelinfo ()
  21. ((kernel
  22. :initarg :kernel :type ein:$kernel
  23. :documentation "Kernel instance.")
  24. (get-buffers
  25. :initarg :get-buffers
  26. :documentation "A packed function to get buffers associated
  27. with the kernel. The buffer local `default-directory' variable
  28. in these buffer will be synced with the kernel's cwd.")
  29. (hostname
  30. :initarg :hostname :type string
  31. :documentation "Host name of the machine where the kernel is running on.")
  32. (language
  33. :initarg :language :type string
  34. :accessor ein:kernelinfo-language
  35. :documentation "Language for the running kernel.")
  36. (ccwd
  37. :initarg :ccwd :type string
  38. :documentation "cached CWD (last time checked CWD)."))
  39. :documentation "Info related (but unimportant) to kernel")
  40. (defun ein:kernelinfo-new (kernel get-buffers kernel-language)
  41. "Make a new `ein:kernelinfo' instance based on KERNEL and GET-BUFFERS."
  42. (let ((kerinfo (make-instance 'ein:kernelinfo)))
  43. (setf (slot-value kerinfo 'kernel) kernel)
  44. (setf (slot-value kerinfo 'get-buffers) get-buffers)
  45. (setf (slot-value kerinfo 'language) kernel-language)
  46. (ein:case-equal kernel-language
  47. ("python" (ein:kernelinfo-setup-hooks kerinfo)))
  48. kerinfo))
  49. (defun ein:kernelinfo-setup-hooks (kerinfo)
  50. "Add `ein:kernelinfo-update-*' to `ein:$kernel-after-*-hook'."
  51. (with-slots (kernel) kerinfo
  52. (push (cons #'ein:kernelinfo-update-all kerinfo)
  53. (ein:$kernel-after-start-hook kernel))
  54. (push (cons #'ein:kernelinfo-update-ccwd kerinfo)
  55. (ein:$kernel-after-execute-hook kernel))))
  56. (defun ein:kernelinfo-update-all (kerinfo)
  57. "Update KERINFO slots by triggering all update functions."
  58. (ein:log 'debug "(ein:kernel-live-p kernel) = %S"
  59. (ein:kernel-live-p (slot-value kerinfo 'kernel)))
  60. (ein:kernelinfo-update-ccwd kerinfo)
  61. (ein:kernelinfo-update-hostname kerinfo))
  62. (defun ein:kernelinfo-update-ccwd (kerinfo)
  63. "Update cached current working directory (CCWD) and change
  64. `default-directory' of kernel related buffers."
  65. (let ((ccwd-string (ein:case-equal (ein:kernelinfo-language kerinfo)
  66. (("python") "__import__('sys').stdout.write(__import__('os').getcwd())")
  67. ((t) nil))))
  68. (when ccwd-string
  69. (ein:kernel-request-stream
  70. (slot-value kerinfo 'kernel)
  71. ccwd-string
  72. (lambda (cwd kerinfo)
  73. (with-slots (kernel get-buffers) kerinfo
  74. (setq cwd (ein:kernel-filename-from-python kernel cwd))
  75. (oset kerinfo :ccwd cwd)
  76. ;; sync buffer's `default-directory' with CWD
  77. (when (file-accessible-directory-p cwd)
  78. (dolist (buffer (ein:funcall-packed get-buffers))
  79. (when (buffer-live-p buffer)
  80. (with-current-buffer buffer
  81. (setq default-directory (file-name-as-directory cwd))))))))
  82. (list kerinfo)))))
  83. (defun ein:kernelinfo-update-hostname (kerinfo)
  84. "Get hostname in which kernel is running and store it in KERINFO."
  85. (let ((hostname-string (ein:case-equal (ein:kernelinfo-language kerinfo)
  86. (("python") "__import__('sys').stdout.write(__import__('socket').gethostname())")
  87. ((t) nil))))
  88. (when hostname-string
  89. (ein:kernel-request-stream
  90. (slot-value kerinfo 'kernel)
  91. hostname-string ; "__import__('sys').stdout.write(__import__('socket').gethostname())" ; uname() not available in windows
  92. (lambda (hostname kerinfo)
  93. (oset kerinfo :hostname hostname))
  94. (list kerinfo)))))
  95. (provide 'ein-kernelinfo)
  96. ;;; ein-kernelinfo.el ends here