help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: conditional text insertion


From: Deniz Dogan
Subject: Re: conditional text insertion
Date: Tue, 19 Jul 2011 09:24:42 +0200
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]