From: dont.spam.earl@gmail.com
To: help-gnu-emacs@gnu.org
Subject: Specifying arguments to interactive functions
Date: Wed, 14 May 2014 22:20:51 -0700 (PDT) [thread overview]
Message-ID: <5d6e3672-b901-4e59-852d-281a3a5e4733@googlegroups.com> (raw)
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))
))
next reply other threads:[~2014-05-15 5:20 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-15 5:20 dont.spam.earl [this message]
2014-05-15 8:04 ` Specifying arguments to interactive functions 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5d6e3672-b901-4e59-852d-281a3a5e4733@googlegroups.com \
--to=dont.spam.earl@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.