From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Edward Newsgroups: gmane.emacs.help Subject: Re: HELP: One Bindkey for Two Different Commands Date: Wed, 21 Nov 2007 04:46:18 -0800 (PST) Organization: http://groups.google.com Message-ID: <665ebabd-0a59-43c3-a720-13db124257d8@a28g2000hsc.googlegroups.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1195652668 12449 80.91.229.12 (21 Nov 2007 13:44:28 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 21 Nov 2007 13:44:28 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Nov 21 14:44:34 2007 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Iups6-0002UX-Ee for geh-help-gnu-emacs@m.gmane.org; Wed, 21 Nov 2007 14:44:06 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Iuprq-0004PR-4Y for geh-help-gnu-emacs@m.gmane.org; Wed, 21 Nov 2007 08:43:50 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!postnews.google.com!a28g2000hsc.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 80 Original-NNTP-Posting-Host: 143.182.124.4 Original-X-Trace: posting.google.com 1195649179 29463 127.0.0.1 (21 Nov 2007 12:46:19 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Wed, 21 Nov 2007 12:46:19 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: a28g2000hsc.googlegroups.com; posting-host=143.182.124.4; posting-account=Ebm03goAAAB8zC3V0HH22-SboyfzeP0u User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727),gzip(gfe),gzip(gfe) X-HTTP-Via: 1.0 chfwpr04.ch.intel.com:911 (squid/2.6.STABLE12) Content-Disposition: inline Original-Xref: shelby.stanford.edu gnu.emacs.help:153964 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:49395 Archived-At: On Nov 21, 2:33 am, "Drew Adams" wrote: > > >> Why waste two bindkeys on two similar commands when you can simply > > >> write a function to choose the between commands based on context? > > > >> Unfortunately, this very simple idea doesn't seem to work for me. > > >> Here's what I have in my .emacs so far: > > > >> (defun ya-ya () > > >> (if (cdr (window-list)) > > >> 'other-window > > >> 'switch-to-buffer)) > > > >> (global-set-key "\M-o" (ya-ya)) > > >> Any ideas how this might be accomplished? > > > > To turn a function into a command, add an `interactive' spec. > > > > The functions other-window and switch-to-buffer require arguments. > > > Your point being? other-window and switch-to-buffer both have an > > interactive spec. > > Just trying to help. > > To be more clear: You need to call function `other-window' or > `switch-to-buffer', not just return the symbol. And calling them means > providing their required arguments - use either (call-interactively > 'other-window) or (other-window ). > > > > The second argument to global-set-key is a command, not a list such as > > > you have supplied. > > > He does not supply a list. He supplies a command. > > Sorry, I misread '(ya-ya) instead of (ya-ya). > > > The problem merely is that he makes his choice of command at > > the time of global-set-key, not at keypress time. > > To be more helpful: You need to pass the symbol `ya-ya', instead of calling > the function `ya-ya': (global-set-key "\M-o" 'ya-ya). A key is bound to a > command (or its symbol), not to the result of calling the command. > > This is the opposite mistake from that made with `other-window' (returning > the symbol instead of calling the function). As David suggested, you called > `ya-ya' at key-binding time, so one of the symbols it returns at that time, > not `ya-ya', gets bound to `M-o'.- Hide quoted text - > > - Show quoted text - Drew & David, Thank you so much for your help! I wish Lisp had been my first language, because then it would have been easier to remember how simple and powerful command syntax can be. Althouth I've learned a lot about Lisp in the past year, I'm still used to languages where the syntax is more idiosyncratic and arbitrary. Because of this I find myself making the sort of mistakes you have mentioned. Anyway, here is the new code that works for anyone who is interested: (defun ya-ya () (interactive) ; new (if (cdr (window-list)) (other-window 1) ; command with argument (call-interactively 'switch-to-buffer))) ; command called interactively, argument to be passed at that time (global-set-key "\M-o" 'ya-ya) ; command symbol passed, instead of the last symbol returned by the function! Your guidance has helped me quite a bit here. I plan on using this same technique to contextualize my keybindings for more commands and editing situations. Thanks again, Edward