all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Specifying arguments to interactive functions
@ 2014-05-15  5:20 dont.spam.earl
  2014-05-15  8:04 ` Thien-Thi Nguyen
       [not found] ` <mailman.1382.1400140784.1147.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 5+ messages in thread
From: dont.spam.earl @ 2014-05-15  5:20 UTC (permalink / raw)
  To: help-gnu-emacs

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))
    ))



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-05-18 17:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-15  5:20 Specifying arguments to interactive functions dont.spam.earl
2014-05-15  8:04 ` Thien-Thi Nguyen
     [not found] ` <mailman.1382.1400140784.1147.help-gnu-emacs@gnu.org>
2014-05-15 17:43   ` dont.spam.earl
2014-05-15 18:06     ` Thien-Thi Nguyen
     [not found]     ` <mailman.1412.1400176929.1147.help-gnu-emacs@gnu.org>
2014-05-18 17:07       ` dont.spam.earl

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.