From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Kevin Rodgers Newsgroups: gmane.emacs.help Subject: Re: local key swap? alternatives? Date: Wed, 07 Sep 2005 11:47:41 -0600 Message-ID: References: <20050901124642.GB13963@let.rug.nl> <20050907135325.GA13168@let.rug.nl> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Trace: sea.gmane.org 1126116064 12236 80.91.229.2 (7 Sep 2005 18:01:04 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Wed, 7 Sep 2005 18:01:04 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Sep 07 20:01:01 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1ED4Aj-0003Wy-H7 for geh-help-gnu-emacs@m.gmane.org; Wed, 07 Sep 2005 19:57:21 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ED4Ai-0006RD-Sk for geh-help-gnu-emacs@m.gmane.org; Wed, 07 Sep 2005 13:57:20 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1ED49h-0005or-AP for help-gnu-emacs@gnu.org; Wed, 07 Sep 2005 13:56:17 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1ED49e-0005m0-Bt for help-gnu-emacs@gnu.org; Wed, 07 Sep 2005 13:56:15 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ED49d-0005lW-Jk for help-gnu-emacs@gnu.org; Wed, 07 Sep 2005 13:56:13 -0400 Original-Received: from [80.91.229.2] (helo=ciao.gmane.org) by monty-python.gnu.org with esmtp (TLS-1.0:RSA_AES_128_CBC_SHA:16) (Exim 4.34) id 1ED4Cv-00084Z-Vw for help-gnu-emacs@gnu.org; Wed, 07 Sep 2005 13:59:38 -0400 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1ED451-0001cf-0m for help-gnu-emacs@gnu.org; Wed, 07 Sep 2005 19:51:27 +0200 Original-Received: from 207.167.42.60 ([207.167.42.60]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 07 Sep 2005 19:51:27 +0200 Original-Received: from ihs_4664 by 207.167.42.60 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 07 Sep 2005 19:51:27 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-To: help-gnu-emacs@gnu.org Original-Lines: 72 Original-X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: 207.167.42.60 User-Agent: Mozilla Thunderbird 0.9 (X11/20041105) X-Accept-Language: en-us, en In-Reply-To: <20050907135325.GA13168@let.rug.nl> 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:29300 Archived-At: Francisco Borges wrote: > That does not work because both keys are bound to > self-insert-command. That's the reason I had failed to do it myself. Yes, that's a tricky situation. > I (finally) solved the problem by doing something as ugly as: > > (defun my-four () > (interactive) > (insert-char (string-to-char "4") 1)) > > (defun my-dollar () > (interactive) > (insert-char (string-to-char "$") 1)) > > (local-set-key "$" (quote my-four)) > (local-set-key "4" (quote my-dollar)) > > Would there be a better way to do it? I don't really know Lisp... Just a few minor points: You could allow a prefix argument to insert multiple characters, provide a doc string, and avoid converting from a single-character string to the character: (defun my-four (&optional arg) "Insert \"4\" at point. With a prefix ARG, insert that many characters." (interactive "p") (insert-char ?4 (or arg 1))) (defun my-dollar () "Insert \"$\" at point. With a prefix ARG, insert that many characters." (interactive "p") (insert-char ?$ (or arg 1))) (local-set-key "$" 'my-four) (local-set-key "4" 'my-dollar) If you have many such functions, you'd want to abstract the common parts with a function-defining macro: (defmacro define-my-insert (name char) "Define the `my-NAME' command, to insert CHAR." `(defun ,(intern (format "my-%s" name)) (&optional arg) ,(format "Insert \"%c\" at point. With a prefix ARG, insert that many characters." char) (interactive "p") (insert-char ,char (or arg 1)))) (define-my-insert four ?4) (define-my-insert dollar ?$) > I think that the right thing to do is to use a function such as the one > you send and treat self-insert-command cases specially, but with my > knowledge of Lisp making these changes to the code above would take more > than the free time I have to spend on it... If the define-my-insert macro were rewritten to evalutate the NAME and CHAR arguments, you could add this to swap-keys: (when (eq binding-1 'self-insert-command) (setq binding-1 (define-my-insert (intern key-1) (string-to-char key-1)))) (when (eq binding-2 'self-insert-command) (setq binding-2 (define-my-insert (intern key-2) (string-to-char key-2)))) -- Kevin Rodgers