Skip to content

Commit 1fb984f

Browse files
Daniel Dinnyesnecaris
authored andcommitted
Register process sentinel to log status on process exit
Enables lexical scoping, which is needed for closures to work in sentinel callbacks. The complexity in this handler is due to the need to ensure that any sentinels already registered for the process are correctly preserved. For example, I personally have a hook for term-mode, which registers a sentinel to prompt for closing the buffer on process exit. Without this logic the logger for the completion status would override my handler completely.
1 parent 8929aa2 commit 1fb984f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

conda.el

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
;; -*- lexical-binding: t; -*-
12
;;; conda.el --- Work with your conda environments
23

34
;; Copyright (C) 2016-2024 Rami Chowdhury
@@ -705,6 +706,15 @@ environment YAML file or similar at the project level."
705706
(if conda-message-on-environment-switch
706707
(message "No Conda environment found for <%s>" (buffer-file-name))))))
707708

709+
(defun conda--env-process-exit-message (op env-name &optional callback)
710+
;; capture dynamically scoped closures
711+
(lambda (proc _event)
712+
(when (memq (process-status proc) '(exit signal))
713+
(if (zerop (process-exit-status proc))
714+
(message "Finished %s Conda environment <%s>" op env-name)
715+
(message "Error while %s Conda environment <%s>" op env-name)))
716+
(when callback (funcall callback proc _event))))
717+
708718
;;;###autoload
709719
(defun conda-env-yaml-process-for-buffer (&optional remove env-file)
710720
"Operate on conda environment defined by ENV-FILE, a YAML file.
@@ -746,6 +756,12 @@ or reports an error otherwise."
746756
(conda--get-executable-path) nil "env" (cdr params)))))
747757
(when term-buffer
748758
(message "%s Conda environment <%s>" (car params) env-name)
759+
(let* ((proc (get-buffer-process term-buffer))
760+
(current-sentinel (process-sentinel proc)))
761+
(set-process-sentinel proc
762+
(conda--env-process-exit-message
763+
(downcase (car params)) env-name
764+
current-sentinel)))
749765
(with-current-buffer term-buffer
750766
(unless (term-check-proc term-buffer)
751767
(term-mode)

0 commit comments

Comments
 (0)