Function: claude-code-ide-mcp--handle-ediff-startup

claude-code-ide-mcp--handle-ediff-startup is a natively compiled function defined in claude-code-ide-mcp-handlers.el.

Signature

(claude-code-ide-mcp--handle-ediff-startup TAB-NAME SESSION SAVED-WINCONF STARTUP-HOOK-FN)

Documentation

Handle ediff startup for TAB-NAME with SESSION and SAVED-WINCONF.

STARTUP-HOOK-FN is the hook function to remove after use. ediff-startup-hook is global, so when two instances enter ediff concurrently the wrong closure can fire first. Our own ediff is recognized by its buffer-B identity (the control buffer's name cannot carry a marker: ediff-control-buffer-suffix is derived by ediff from the generated name, not consumed); a foreign ediff is left untouched and the hook stays armed for ours.

Source Code

;; Defined in /nix/store/yjvbhwl7p6zrbw9xlnni7y4hra8sa3pl-emacs-packages-deps/share/emacs/site-lisp/claude-code-ide-mcp-handlers.el
(defun claude-code-ide-mcp--handle-ediff-startup (tab-name session saved-winconf startup-hook-fn)
  "Handle ediff startup for TAB-NAME with SESSION and SAVED-WINCONF.
STARTUP-HOOK-FN is the hook function to remove after use.
`ediff-startup-hook' is global, so when two instances enter ediff
concurrently the wrong closure can fire first.  Our own ediff is
recognized by its buffer-B identity (the control buffer's name cannot
carry a marker: `ediff-control-buffer-suffix' is derived by ediff from
the generated name, not consumed); a foreign ediff is left untouched
and the hook stays armed for ours."
  ;; Capture the control buffer and store it in diff-info
  (let* ((active-diffs (claude-code-ide-mcp--get-active-diffs session))
         (diff-info (and active-diffs (gethash tab-name active-diffs))))
    (when (and ediff-control-buffer
               diff-info
               (eq (buffer-local-value 'ediff-buffer-B ediff-control-buffer)
                   (alist-get 'buffer-B diff-info)))
      ;; Store the control buffer in our diff-info
      (setf (alist-get 'control-buffer diff-info) ediff-control-buffer)
      (puthash tab-name diff-info active-diffs)

      (with-current-buffer ediff-control-buffer
        ;; Set up quit hook with captured values in lexical closure
        (setq-local ediff-quit-hook
                    (list (lambda ()
                            ;; Check if this quit was initiated by Claude
                            (let* ((active-diffs (claude-code-ide-mcp--get-active-diffs session))
                                   (diff-info (gethash tab-name active-diffs))
                                   (quit-from-claude (alist-get 'quit-from-claude diff-info)))
                              (unless quit-from-claude
                                ;; Only handle quit if not initiated by Claude
                                (claude-code-ide-mcp--handle-ediff-quit
                                 tab-name
                                 session)))
                            ;; Always restore window configuration
                            (when saved-winconf
                              (condition-case nil
                                  (set-window-configuration saved-winconf)
                                (error nil)))))))

      ;; Jump to the first difference if there are any
      (ignore-errors (ediff-next-difference))

      ;; Save the current window before any operations
      (let ((original-window (selected-window))
            (claude-window nil))
        ;; Restore Claude side window only if user wants it shown during ediff
        (when claude-code-ide-show-claude-window-in-ediff
          (when-let* ((claude-buffer (claude-code-ide-mcp-session-buffer session)))
            (when (buffer-live-p claude-buffer)
              ;; Display Claude buffer in side window and save the window
              (setq claude-window (claude-code-ide--display-buffer-in-side-window claude-buffer)))))

        ;; Handle focus based on user preference
        (cond
         ;; If user wants Claude window focus and it's visible, select it
         ((and claude-code-ide-focus-claude-after-ediff claude-window)
          (select-window claude-window))
         ;; Otherwise, restore the original window (which might be one of the ediff windows)
         (t
          (select-window original-window))))

      ;; Remove this startup hook after use
      (remove-hook 'ediff-startup-hook startup-hook-fn))))