Insert appropriate line-end character (like ';' for C*)
  Home FAQ Contact Sign in
gnu.emacs.help only
 
Advanced search
POPULAR GROUPS

more...

gnu.emacs.help Profile…
 Up
Insert appropriate line-end character (like ';' for C*)         


Author: Josh
Date: Jun 10, 2008 15:11

I think I've seen this behavior in Emacs somewhere, but I can't
remember where and I'm either using the wrong terms when searching or
I'm looking in the wrong places because I can't find anything about
it. What I'm looking for is a emacs command that inserts the
appropriate end-of-line character based on mode. For example, if
you're in c-mode or java-mode or whatever it would insert a ';'.

Ideally it would be context aware so that if you were in python-mode
it would add a ':' where appropriate, but not elsewhere (and even a
',' if you're making a list), but that's not essential.

Does this already exist? If not, does anyone have any pointers on how
I could go about writing it? The only part I can't figure out is how
to determine what the right character is.
8 Comments
Re: Insert appropriate line-end character (like ';' for C*)         


Author: David Hansen
Date: Jun 10, 2008 21:17

On Tue, 10 Jun 2008 15:11:07 -0700 (PDT) Josh dydxtech.com> wrote:
> I think I've seen this behavior in Emacs somewhere, but I can't
> remember where and I'm either using the wrong terms when searching or
> I'm looking in the wrong places because I can't find anything about
> it. What I'm looking for is a emacs command that inserts the
> appropriate end-of-line character based on mode. For example, if
> you're in c-mode or java-mode or whatever it would insert a ';'.
>
> Ideally it would be context aware so that if you were in python-mode
> it would add a ':' where appropriate, but not elsewhere (and even a
> ',' if you're making a list), but that's not essential.
>
> Does this already exist? If not, does anyone have any pointers on how
> I could go about writing it? The only part I can't figure out is how
> to determine what the right character is.

`;' is not a "line-end" character in C but a "end-of-statement"
character. Not even a full featured C parser can know if you want to
continue the statement on the next line or not:
Show full article (1.10Kb)
no comments
Re: Insert appropriate line-end character (like ';' for C*)         


Author: Josh
Date: Jun 11, 2008 12:09

On Jun 11, 12:17 am, David Hansen gmx.net> wrote:
> On Tue, 10 Jun 2008 15:11:07 -0700 (PDT) Josh dydxtech.com> wrote:
>
>> I think I've seen this behavior in Emacs somewhere, but I can't
>> remember where and I'm either using the wrong terms when searching or
>> I'm looking in the wrong places because I can't find anything about
>> it. What I'm looking for is a emacs command that inserts the
>> appropriate end-of-line character based on mode. For example, if
>> you're in c-mode or java-mode or whatever it would insert a ';'.
>
>> Ideally it would be context aware so that if you were in python-mode
>> it would add a ':' where appropriate, but not elsewhere (and even a
>> ',' if you're making a list), but that's not essential.
>
>> Does this already exist? If not, does anyone have any pointers on how
>> I could go about writing it? The only part I can't figure out is how
>> to determine what the right character is.
>
> `;' is not a "line-end" character in C but a "end-of-statement"
> character.  Not even a full featured C parser can know if you want to ...
Show full article (2.00Kb)
no comments
Re: Insert appropriate line-end character (like ';' for C*)         


Author: Thien-Thi Nguyen
Date: Jun 11, 2008 13:20

() Josh dydxtech.com>
() Wed, 11 Jun 2008 12:09:58 -0700 (PDT)

I don't know how to programatically
determine what the correct character is.

That depends on the programming language, which in the
context of Emacs is largely tied to its major mode.

Thus, you might get by w/ something like:

(defvar finish-statement-character
'((c-mode . ";")
(c++-mode . ";"))
"Alist mapping major mode to a \"finish statement\" character.")

(defun finish-statement-and-start-another ()
(interactive)
(end-of-line)
(let ((s (cdr (assq major-mode finish-statement-character))))
(when s (insert s)))
(newline-and-indent))

You can then extend finish-statement-character to DTRT, over time.

thi
no comments
Re: Insert appropriate line-end character (like ';' for C*)         


Author: David Hansen
Date: Jun 11, 2008 13:58

On Wed, 11 Jun 2008 12:09:58 -0700 (PDT) Josh dydxtech.com> wrote:
> On Jun 11, 12:17 am, David Hansen gmx.net> wrote:
>
>> `;' is not a "line-end" character in C but a "end-of-statement"
>> character.  Not even a full featured C parser can know if you want...
Show full article (1.96Kb)
no comments
Re: Insert appropriate line-end character (like ';' for C*)         


Author: Jason Rumney
Date: Jun 11, 2008 15:03

On Jun 11, 8:09 pm, Josh dydxtech.com> wrote:
> I know that emacs can't possibly know what my intention is, but it
> _can_ know what the appropriate end-of-statement character is for the
> current context.

Sure, theoretically it can, but it doesn't. In c-mode and other
derived modes, you'd set c-electric-flag to get the sort of behaviour
you describe (bound to semi-colons, commas etc, rather than C-S-RET),
but it is down to each mode to provide such a feature unless someone
writes such a generic feature and encourages major mode authors to
support it.
no comments
Re: Insert appropriate line-end character (like ';' for C*)         


Author: Josh
Date: Jun 12, 2008 11:30

So the various modes don' t include that information? It seems like
they must, otherwise how would Emacs be able to indent things
appropriately? In python-mode it's aware that a line ending in ':' is
special, and so it automatically indents the following line(s),
similarly it un-indents the lines following a 'return' statement. It's
not doing this solely based on the presence of key terms such as 'if'
and 'def' either (I tested); it's aware of ':' as an important
character in python-mode at least.

All I'm trying to figure out is if there's an easy way to determine,
even if it's just for python-mode, what that special character is.
no comments
Re: Insert appropriate line-end character (like ';' for C*)         


Author: David Hansen
Date: Jun 12, 2008 12:21

On Thu, 12 Jun 2008 11:30:05 -0700 (PDT) Josh dydxtech.com> wrote:
> So the various modes don' t include that information? It seems like
> they must, otherwise how would Emacs be able to indent things
> appropriately? In python-mode it's aware that a line ending in ':' is
> special, and so it automatically indents the following line(s),
> similarly it un-indents the lines following a 'return' statement. It's
> not doing this solely based on the presence of key terms such as 'if'
> and 'def' either (I tested); it's aware of ':' as an important
> character in python-mode at least.
>
> All I'm trying to figure out is if there's an easy way to determine,
> even if it's just for python-mode, what that special character is.

Of course every major-mode implementation somewhere has this information
stored but there is no standard way of doing this. Emacs builtin
support for parsing languages is pretty low level and focused on lisp
(read about syntax tables in the manual).

All major modes come with additions to this low level parser (mostly a
bunch of regexps and good guess work, no real parser). If python is
your only concern read the source.
Show full article (1.17Kb)
no comments
Re: Insert appropriate line-end character (like ';' for C*)         


Author: Josh
Date: Jun 13, 2008 11:37

Hmm, that's annoying. I was hoping to avoid having to go through the
code for the modes of all the appropriate modes. I guess it would be
simpler to just use the hooks for those modes to add the appropriate
behavior separately. :/

Thanks.
no comments