From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Lister Account Newsgroups: gmane.emacs.help Subject: Re: conditional text insertion Date: Tue, 19 Jul 2011 01:54:04 -0700 Message-ID: References: <4E25313A.3080405@dogan.se> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=0016364c7235ff1ba604a868417e X-Trace: dough.gmane.org 1311065780 19404 80.91.229.12 (19 Jul 2011 08:56:20 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 19 Jul 2011 08:56:20 +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 10:56:14 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 1Qj669-0007bc-K8 for geh-help-gnu-emacs@m.gmane.org; Tue, 19 Jul 2011 10:56:13 +0200 Original-Received: from localhost ([::1]:46585 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qj668-0000r5-GX for geh-help-gnu-emacs@m.gmane.org; Tue, 19 Jul 2011 04:56:12 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:54724) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qj64B-0000po-VV for help-gnu-emacs@gnu.org; Tue, 19 Jul 2011 04:54:16 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Qj646-0006T3-Cv for help-gnu-emacs@gnu.org; Tue, 19 Jul 2011 04:54:11 -0400 Original-Received: from mail-ey0-f174.google.com ([209.85.215.174]:47130) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qj645-0006Ss-U2 for help-gnu-emacs@gnu.org; Tue, 19 Jul 2011 04:54:06 -0400 Original-Received: by eyx24 with SMTP id 24so3758832eyx.19 for ; Tue, 19 Jul 2011 01:54:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=QOZpA+keWhcJMUTqBTrTkuaN1edGvQShlZXBx6lEM00=; b=AT2z5T+PsNI0huUfwLsaTuLGvHvmza7bizTnQwNmx/sRbH3PXb1eXjrvGRg4ZFQfUu mDECOI79pIzAEjiOpHVPxWZlFrU/pW9vL1mqBVbtBnR135a/zo3redQ8QK+p6J30XPqo MdDreF4lHuTYVH7OzhYTgqmjlEDcq4GcNYTjs= Original-Received: by 10.14.10.83 with SMTP id 59mr2693226eeu.154.1311065644145; Tue, 19 Jul 2011 01:54:04 -0700 (PDT) Original-Received: by 10.14.119.202 with HTTP; Tue, 19 Jul 2011 01:54:04 -0700 (PDT) In-Reply-To: <4E25313A.3080405@dogan.se> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 209.85.215.174 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:81671 Archived-At: --0016364c7235ff1ba604a868417e Content-Type: text/plain; charset=ISO-8859-1 That's exactly what I was looking for. I'm only binding it in c-mode derivatives, so mine looks like: (add-hook 'c-mode-common-hook 'my-cool-return) I hope that's OK. Seems to work, anyways. :) I very much appreciate the help. I'm slowly making the shift to emacs and this kind of programmability really has me floored. On Tue, Jul 19, 2011 at 12:24 AM, Deniz Dogan wrote: > 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 > > > --0016364c7235ff1ba604a868417e Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable That's exactly what I was looking for.=A0 I'm only binding it in c-= mode derivatives, so mine looks like:

(add-hook 'c-mode-common-h= ook 'my-cool-return)

I hope that's OK.=A0 Seems to work, any= ways. :)

I very much appreciate the help.=A0 I'm slowly making the shift to = emacs and this kind of programmability really has me floored.=A0

On Tue, Jul 19, 2011 at 12:24 AM, Deniz Dogan <deniz@dogan.se><= /span> wrote:
<= div class=3D"h5">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. =A0If 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 o= thers
have resolved, but I'm having trouble googling for a solution.

Thanks,
Steve

(defun hello-there ()
=A0(interactive)
=A0(move-end-of-line)
=A0(unless (looking-back ";")
=A0 =A0(insert ";"))
=A0(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
=A0 =A0 =A0 =A0 =A0(lambda ()
=A0 =A0 =A0 =A0 =A0 =A0(local-set-key (kbd "C-c ;") 'hello-t= here)))

This binds "C-c ;" to that command.

Hope that helps,
Deniz



--0016364c7235ff1ba604a868417e--