From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Deniz Dogan Newsgroups: gmane.emacs.help Subject: Re: conditional text insertion Date: Tue, 19 Jul 2011 09:24:42 +0200 Message-ID: <4E25313A.3080405@dogan.se> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1311060835 23001 80.91.229.12 (19 Jul 2011 07:33:55 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 19 Jul 2011 07:33:55 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Jul 19 09:33:52 2011 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Qj4oO-0003IE-T6 for geh-help-gnu-emacs@m.gmane.org; Tue, 19 Jul 2011 09:33:49 +0200 Original-Received: from localhost ([::1]:42219 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qj4oN-0002Qk-4z for geh-help-gnu-emacs@m.gmane.org; Tue, 19 Jul 2011 03:33:47 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:55060) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qj4fw-00011V-G2 for help-gnu-emacs@gnu.org; Tue, 19 Jul 2011 03:25:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Qj4ft-0000pt-15 for help-gnu-emacs@gnu.org; Tue, 19 Jul 2011 03:25:03 -0400 Original-Received: from ch-smtp05.sth.basefarm.net ([80.76.153.6]:39345) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qj4fs-0000pe-HA for help-gnu-emacs@gnu.org; Tue, 19 Jul 2011 03:25:00 -0400 Original-Received: from c80-216-105-155.bredband.comhem.se ([80.216.105.155]:49176 helo=[192.168.0.10]) by ch-smtp05.sth.basefarm.net with esmtp (Exim 4.76) (envelope-from ) id 1Qj4fh-0005bk-Gt for help-gnu-emacs@gnu.org; Tue, 19 Jul 2011 09:24:50 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: X-Originating-IP: 80.216.105.155 X-Scan-Result: No virus found in message 1Qj4fh-0005bk-Gt. X-Scan-Signature: ch-smtp05.sth.basefarm.net 1Qj4fh-0005bk-Gt 9e6d39f05b1d28081444a2958dfe974b X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.76.153.6 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:81670 Archived-At: On 2011-07-19 07:47, Lister Account wrote: > I have a keybinding I just made that essentially will go to the end of a > line, insert a semicolon, return, and auto-indent. > > I'd like to only add the semicolon if it doesn't already exist. > > In other words, go to the end of the line, if a semicolon is there, > return. If a semicolon is not there, add one, then return. > > I'm brand spanking new to emacs, and I'm sure this is a task that others > have resolved, but I'm having trouble googling for a solution. > > Thanks, > Steve (defun hello-there () (interactive) (move-end-of-line) (unless (looking-back ";") (insert ";")) (newline-and-indent)) Then you would want to bind this to only some modes, and not globally. E.g., this way: (add-hook 'prog-mode-hook (lambda () (local-set-key (kbd "C-c ;") 'hello-there))) This binds "C-c ;" to that command. Hope that helps, Deniz