help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Specifying arguments to interactive functions


From: dont . spam . earl
Subject: Specifying arguments to interactive functions
Date: Wed, 14 May 2014 22:20:51 -0700 (PDT)
User-agent: G2/1.0

Hi all,

I'd like to run multiple comint sessions of the same type in different buffers 
(e.g. multiple shells, multiple sql connections, etc). I'd like to hit e.g. M-S 
for shell, then the single digit for the session ID to either create a new 
session or switch to the existing session with that ID. The following code 
works, but I'm wondering if I can refactor it to eliminate switch-to-shell and 
switch-to-sql while still keeping the global-set-key calls simple.

In essence, my problem is that I'd like to bind the keys to a function that 
interactively elicit arguments (the session #) while also accepting other 
preset arguments (the buffer-type and buffer-creation function).

The relevant code is included below. Any suggestions on how to simplify this?

Thanks!

Earl


(global-set-key "\M-Q" 'switch-to-sql)
(global-set-key "\M-S" 'switch-to-shell)

(defun switch-to-shell (buffer-id)
  (interactive
   (list (- (read-char-choice
             (format "Enter id for %s buffer: " 'shell)
             '(48 49 50 51 52 53 54 55 56 57))
            48)))
  (message nil)
  (let ((buffer-type "shell")
        (buffer-fn 'shell))
    (switch-to-interactive-buffer '() buffer-id buffer-type buffer-fn)))

(defun switch-to-sql (buffer-id)
  (interactive
   (list (- (read-char-choice
             (format "Enter id for %s buffer: " 'sql)
             '(48 49 50 51 52 53 54 55 56 57))
            48)))
  (message nil)
  (let ((buffer-type "sql")
        (buffer-fn 'sql-connect-fn))
    (switch-to-interactive-buffer '() buffer-id buffer-type buffer-fn)))

(defun switch-to-interactive-buffer (buffer-args buffer-id buffer-type 
buffer-fn)
  "Switch to the interactive buffer of the specified ID.
ID is elicited from user in minibuffer as a single digit.
Looks for buffer type name (e.g. \"shell\") in *interactive-buffer-type*.
Looks for buffer creation function (e.g. 'shell) in *interactive-buffer-fn*.
"
  ;;(message "in buffer-name: %s" (buffer-name))
  (let* ((buffer-name (format "*%s-%d*" buffer-type buffer-id))
         (buffer (or (get-buffer buffer-name)
                     (let ((new-buffer-args (append buffer-args (list 
buffer-name))))
                       (apply buffer-fn new-buffer-args)))))
    ;;(message "switching to buffer %s" buffer)
    (switch-to-buffer buffer)
    ;;(message "switched to buffer: %s" (buffer-name))
    ))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]