I use many major modes (sql, scheme, sawfish) which often have a comint subprocess associated. I wanted to unify the binding. Here’s the code:
(defconst major-mode-comint-mapping
'((sql-mode sql-buffer
(my-sql-postgres))
(scheme-mode (and (functionp 'scheme-proc) ; (boundp 'scheme-proc)
(inferior-process-standard
'scheme-buffer
'inferior-scheme-mode))
(run-scheme (scheme-determine-program)))
(sawfish-mode (inferior-process-standard
'sawfish-buffer
'inferior-sawfish-mode)
;(sawfish-proc)
(sawfish-console))
;(lisp)
)
"mapping: mode -> form, which returns the associated commint buffer
and start-form, which starts (a new) comint subprocess") ;; i could have a mapping major-mode -> inferior-mode
(defun inferior-process-via-variable (local-variable inferior-mode)
"Return the associated comint subprocess (in the INFERIOR-MODE). It is either the current buffer,
or stored in the LOCAL-VARIABLE."
(let ((proc (get-buffer-process
(if (eq major-mode inferior-mode)
(current-buffer)
(symbol-value local-variable)))))
(or proc nil
;(error (format "No current process. See variable `%s'" buffer-variable))
))) ;; todo `throw'
(defun display-inferior-process (mode)
"Get an associated inferior process, for the major MODE."
(interactive
(list major-mode))
(let ((buffer (inferior-process mode)))
(display-buffer buffer 't))) ;; (defun display-comint-buffer (get-buffer start) ;variable
;; ""
;; (let ((buffer (eval get-buffer))
;; )
;; (if (and (bufferp buffer)
;; (buffer-live-p buffer))
;; (display-buffer buffer 't)
;; (eval start))
;; )) (defun switch-to-inferior-process (mode)
"go to the inferrior process buffer (for major MODE)."
(interactive (list major-mode))
(let ((buffer (inferior-process mode)))
(if (processp buffer)
(setq buffer (process-buffer buffer)))
(if buffer
(unless (eq buffer (current-buffer))
(switch-to-buffer-other-window buffer 't))
(error "major mode not supported")))) ;;; common to sql, lisp, scheme ....
(defun inferior-process (mode) ; variable
""
;(interactive (list major-mode))
(let ((info (aget major-mode-comint-mapping mode't)))
(if info
(let* ((get-buffer (nth 0 info))
(start (nth 1 info))
(buffer (eval get-buffer)))
(if (or (and (processp buffer)
(buffer-live-p (process-buffer buffer)))
(and (bufferp buffer)
(buffer-live-p buffer)))
;(display-buffer buffer 't)
buffer
(eval start)))))) ;; (proc (get-buffer-process
;; (if (eq major-mode mode) ;inferior-lisp-mode
;; (current-buffer)
;; (symbol-value variable)))))
;; (or proc (error "No Lisp subprocess; see variable `inferior-lisp-buffer'"))))(global-set-key [(control ?c) ?d] 'switch-to-inferior-process)
(provide 'my-major-coming)