unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* conditional text insertion
@ 2011-07-19  5:47 Lister Account
  2011-07-19  7:24 ` Deniz Dogan
  0 siblings, 1 reply; 7+ messages in thread
From: Lister Account @ 2011-07-19  5:47 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 469 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 524 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: conditional text insertion
  2011-07-19  5:47 conditional text insertion Lister Account
@ 2011-07-19  7:24 ` Deniz Dogan
  2011-07-19  8:54   ` Lister Account
  0 siblings, 1 reply; 7+ messages in thread
From: Deniz Dogan @ 2011-07-19  7:24 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: conditional text insertion
  2011-07-19  7:24 ` Deniz Dogan
@ 2011-07-19  8:54   ` Lister Account
  2011-07-19  9:57     ` Lister Account
  2011-07-19 10:02     ` Deniz Dogan
  0 siblings, 2 replies; 7+ messages in thread
From: Lister Account @ 2011-07-19  8:54 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1370 bytes --]

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 <deniz@dogan.se> 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
>
>
>

[-- Attachment #2: Type: text/html, Size: 2055 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: conditional text insertion
  2011-07-19  8:54   ` Lister Account
@ 2011-07-19  9:57     ` Lister Account
  2011-07-19 14:02       ` Memnon Anon
  2011-07-19 10:02     ` Deniz Dogan
  1 sibling, 1 reply; 7+ messages in thread
From: Lister Account @ 2011-07-19  9:57 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1907 bytes --]

by the way - for anybody reading the archives:

I had to make a minor update to the code that Deniz provided:

(defun hello-there ()
 (interactive)
 (call-interactively 'move-end-of-line)
 (unless (looking-back ";")
   (insert ";"))
 (newline-and-indent))

I updated line 3 above because I was getting the error "Wrong Number of
Arguments".  A little googling led me to the solution.

Thanks again!

On Tue, Jul 19, 2011 at 1:54 AM, Lister Account <lister345@gmail.com> wrote:

> 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 <deniz@dogan.se> 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
>>
>>
>>
>

[-- Attachment #2: Type: text/html, Size: 2935 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: conditional text insertion
  2011-07-19  8:54   ` Lister Account
  2011-07-19  9:57     ` Lister Account
@ 2011-07-19 10:02     ` Deniz Dogan
  1 sibling, 0 replies; 7+ messages in thread
From: Deniz Dogan @ 2011-07-19 10:02 UTC (permalink / raw)
  To: help-gnu-emacs

On 2011-07-19 10:54, Lister Account wrote:
> 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.
>

I'm glad you're taking the programmability aspect into account on your 
journey into Emacs.  I've seen way too many new users be appalled by 
things such as when they ask "how do I paste a line 1000 times" and they 
get an answer along the lines of C-x ( C-y C-x ) C-u 999 C-x e.  If 
anyone is not okay with that behavior, it's so easy to change it.

As a matter of fact, I think I'm going to change that behavior right away...

> On Tue, Jul 19, 2011 at 12:24 AM, Deniz Dogan <deniz@dogan.se
> <mailto:deniz@dogan.se>> 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
>
>
>




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: conditional text insertion
  2011-07-19  9:57     ` Lister Account
@ 2011-07-19 14:02       ` Memnon Anon
  2011-07-19 17:40         ` Lister Account
  0 siblings, 1 reply; 7+ messages in thread
From: Memnon Anon @ 2011-07-19 14:02 UTC (permalink / raw)
  To: help-gnu-emacs

Hi Lister,
Lister Account <lister345@gmail.com> writes:

> I updated line 3 above because I was getting the error "Wrong Number
> of Arguments".  A little googling led me to the solution.

Google works of course, but it is *really* faster to get used 
to emacs self documenting facilities.

In this case, I used `C-h f' move-end-of-line:

,----
| move-end-of-line is an interactive compiled Lisp function in
| `simple.el'.
| 
| It is bound to C-e, <end>.
| 
| (move-end-of-line ARG)
| 
| For more information check the manuals.
| 
! Move point to end of current line as displayed.
! With argument ARG not nil or 1, move forward ARG - 1 lines first.
| If point reaches the beginning or end of buffer, it stops there.
| 
| To ignore the effects of the `intangible' text or overlay
| property, bind `inhibit-point-motion-hooks' to t.
| If there is an image in the current line, this function
| disregards newlines that are part of the text on which the image
| rests.
`----

So we need an arg, (move-end-of-line nil) should do.

What is the advantage of this approach to google?

a) Faster: Its all there, already. I can type `C-h f' faster
           than my browser starts up.
b) Up to date: There is lots of stuff on the net, but
           believe it or not, emacs changes. The build in 
           documentation is always uptodate, or at least
           up to the date you are using now. 
c) See the source: With a single <ret> on "in `simple.el', you
           get the source of the function. If you want to learn
           elisp, just go and browse ;).

Well, there are other benefits, but those seem to me the most potent
ones. 

hth 
Memnon

P.S: (info "(emacs) Help Summary") <--- C-x C-e here 
     is a good starting point. 




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: conditional text insertion
  2011-07-19 14:02       ` Memnon Anon
@ 2011-07-19 17:40         ` Lister Account
  0 siblings, 0 replies; 7+ messages in thread
From: Lister Account @ 2011-07-19 17:40 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 2071 bytes --]

It's funny you should say that.  I made a note to myself this morning:

"Learn how to use the help system better"

:)

Your advice is not being taken lightly.

On Tue, Jul 19, 2011 at 7:02 AM, Memnon Anon <
gegendosenfleisch@googlemail.com> wrote:

> Hi Lister,
> Lister Account <lister345@gmail.com> writes:
>
> > I updated line 3 above because I was getting the error "Wrong Number
> > of Arguments".  A little googling led me to the solution.
>
> Google works of course, but it is *really* faster to get used
> to emacs self documenting facilities.
>
> In this case, I used `C-h f' move-end-of-line:
>
> ,----
> | move-end-of-line is an interactive compiled Lisp function in
> | `simple.el'.
> |
> | It is bound to C-e, <end>.
> |
> | (move-end-of-line ARG)
> |
> | For more information check the manuals.
> |
> ! Move point to end of current line as displayed.
> ! With argument ARG not nil or 1, move forward ARG - 1 lines first.
> | If point reaches the beginning or end of buffer, it stops there.
> |
> | To ignore the effects of the `intangible' text or overlay
> | property, bind `inhibit-point-motion-hooks' to t.
> | If there is an image in the current line, this function
> | disregards newlines that are part of the text on which the image
> | rests.
> `----
>
> So we need an arg, (move-end-of-line nil) should do.
>
> What is the advantage of this approach to google?
>
> a) Faster: Its all there, already. I can type `C-h f' faster
>           than my browser starts up.
> b) Up to date: There is lots of stuff on the net, but
>           believe it or not, emacs changes. The build in
>           documentation is always uptodate, or at least
>           up to the date you are using now.
> c) See the source: With a single <ret> on "in `simple.el', you
>           get the source of the function. If you want to learn
>           elisp, just go and browse ;).
>
> Well, there are other benefits, but those seem to me the most potent
> ones.
>
> hth
> Memnon
>
> P.S: (info "(emacs) Help Summary") <--- C-x C-e here
>     is a good starting point.
>
>
>

[-- Attachment #2: Type: text/html, Size: 2732 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-07-19 17:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-07-19  5:47 conditional text insertion Lister Account
2011-07-19  7:24 ` Deniz Dogan
2011-07-19  8:54   ` Lister Account
2011-07-19  9:57     ` Lister Account
2011-07-19 14:02       ` Memnon Anon
2011-07-19 17:40         ` Lister Account
2011-07-19 10:02     ` Deniz Dogan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).