unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Insert appropriate line-end character (like ';' for C*)
@ 2008-06-10 22:11 Josh
  2008-06-11  4:17 ` David Hansen
       [not found] ` <mailman.13053.1213158202.18990.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 9+ messages in thread
From: Josh @ 2008-06-10 22:11 UTC (permalink / raw)
  To: help-gnu-emacs

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.


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

* Re: Insert appropriate line-end character (like ';' for C*)
  2008-06-10 22:11 Insert appropriate line-end character (like ';' for C*) Josh
@ 2008-06-11  4:17 ` David Hansen
       [not found] ` <mailman.13053.1213158202.18990.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 9+ messages in thread
From: David Hansen @ 2008-06-11  4:17 UTC (permalink / raw)
  To: help-gnu-emacs

On Tue, 10 Jun 2008 15:11:07 -0700 (PDT) Josh <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:

foo = bar () 
        && baz ();

David





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

* Re: Insert appropriate line-end character (like ';' for C*)
       [not found] ` <mailman.13053.1213158202.18990.help-gnu-emacs@gnu.org>
@ 2008-06-11 19:09   ` Josh
  2008-06-11 20:20     ` Thien-Thi Nguyen
                       ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Josh @ 2008-06-11 19:09 UTC (permalink / raw)
  To: help-gnu-emacs

On Jun 11, 12:17 am, David Hansen <david.han...@gmx.net> wrote:
> On Tue, 10 Jun 2008 15:11:07 -0700 (PDT) Josh <j...@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:
>
> foo = bar ()
>         && baz ();
>
> David

Ok, end of statement is what I was looking for. Thanks.

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. All I'm looking for is a command that will insert
that character; I'll take care of making sure it's inserted at the
appropriate time myself.

To be more clear, I'm trying to replecate a behavior in TextMate. In
TextMate, if you hit command-shift-return it adds the context-
appropriate end-of-statement character to the end of the current line,
creates a new line below it, and indents that new line. I can already
make Emacs do everything other than insert the end-of-statement
character, but that's only because I don't know how to programatically
determine what the correct character is.


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

* Re: Insert appropriate line-end character (like ';' for C*)
  2008-06-11 19:09   ` Josh
@ 2008-06-11 20:20     ` Thien-Thi Nguyen
  2008-06-11 20:58     ` David Hansen
  2008-06-11 22:03     ` Jason Rumney
  2 siblings, 0 replies; 9+ messages in thread
From: Thien-Thi Nguyen @ 2008-06-11 20:20 UTC (permalink / raw)
  To: Josh; +Cc: help-gnu-emacs

() Josh <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




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

* Re: Insert appropriate line-end character (like ';' for C*)
  2008-06-11 19:09   ` Josh
  2008-06-11 20:20     ` Thien-Thi Nguyen
@ 2008-06-11 20:58     ` David Hansen
  2008-06-11 22:03     ` Jason Rumney
  2 siblings, 0 replies; 9+ messages in thread
From: David Hansen @ 2008-06-11 20:58 UTC (permalink / raw)
  To: help-gnu-emacs

On Wed, 11 Jun 2008 12:09:58 -0700 (PDT) Josh <josh@dydxtech.com> wrote:

> On Jun 11, 12:17 am, David Hansen <david.han...@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 to
>> continue the statement on the next line or not:
>>
>> foo = bar ()
>>         && baz ();
>
> Ok, end of statement is what I was looking for. Thanks.
>
> 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. All I'm looking for is a command that will insert
> that character; I'll take care of making sure it's inserted at the
> appropriate time myself.
>
> To be more clear, I'm trying to replecate a behavior in TextMate. In
> TextMate, if you hit command-shift-return it adds the context-
> appropriate end-of-statement character to the end of the current line,
> creates a new line below it, and indents that new line. I can already
> make Emacs do everything other than insert the end-of-statement
> character, but that's only because I don't know how to programatically
> determine what the correct character is.

Well, set it manually (not tested).  Emacs can not possibly know about
all statement delimiters of all languages out there:

;;; Add more either here or using
;;;   (setq dh-eos-alist (cons '(mode . "str") dh-eos-alist)
(defvar dh-eos-alist '((c-mode    . ";")
                       (c++-mode  . ";")
                       (java-mode . ";")))

(defun dh-insert-eos ()
  (interactive)
  (let ((eos (assq major-mode dh-eos-alist)))
    (insert (if eos
                (cdr eos)
              ""))
    ;; Change this if you don't want auto-indent.
    (newline-and-indent)))

;; May be problematic outside of X.  Use the output of C-h k as an
;; argument to `kbd' for "weird" keys.
(global-set-key (kbd "<S-return>") #'dh-insert-eos)

David





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

* Re: Insert appropriate line-end character (like ';' for C*)
  2008-06-11 19:09   ` Josh
  2008-06-11 20:20     ` Thien-Thi Nguyen
  2008-06-11 20:58     ` David Hansen
@ 2008-06-11 22:03     ` Jason Rumney
  2008-06-12 18:30       ` Josh
  2 siblings, 1 reply; 9+ messages in thread
From: Jason Rumney @ 2008-06-11 22:03 UTC (permalink / raw)
  To: help-gnu-emacs

On Jun 11, 8:09 pm, Josh <j...@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.


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

* Re: Insert appropriate line-end character (like ';' for C*)
  2008-06-11 22:03     ` Jason Rumney
@ 2008-06-12 18:30       ` Josh
  2008-06-12 19:21         ` David Hansen
       [not found]         ` <mailman.13154.1213298609.18990.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 9+ messages in thread
From: Josh @ 2008-06-12 18:30 UTC (permalink / raw)
  To: help-gnu-emacs

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.


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

* Re: Insert appropriate line-end character (like ';' for C*)
  2008-06-12 18:30       ` Josh
@ 2008-06-12 19:21         ` David Hansen
       [not found]         ` <mailman.13154.1213298609.18990.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 9+ messages in thread
From: David Hansen @ 2008-06-12 19:21 UTC (permalink / raw)
  To: help-gnu-emacs

On Thu, 12 Jun 2008 11:30:05 -0700 (PDT) Josh <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.

David





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

* Re: Insert appropriate line-end character (like ';' for C*)
       [not found]         ` <mailman.13154.1213298609.18990.help-gnu-emacs@gnu.org>
@ 2008-06-13 18:37           ` Josh
  0 siblings, 0 replies; 9+ messages in thread
From: Josh @ 2008-06-13 18:37 UTC (permalink / raw)
  To: help-gnu-emacs

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.


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

end of thread, other threads:[~2008-06-13 18:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-10 22:11 Insert appropriate line-end character (like ';' for C*) Josh
2008-06-11  4:17 ` David Hansen
     [not found] ` <mailman.13053.1213158202.18990.help-gnu-emacs@gnu.org>
2008-06-11 19:09   ` Josh
2008-06-11 20:20     ` Thien-Thi Nguyen
2008-06-11 20:58     ` David Hansen
2008-06-11 22:03     ` Jason Rumney
2008-06-12 18:30       ` Josh
2008-06-12 19:21         ` David Hansen
     [not found]         ` <mailman.13154.1213298609.18990.help-gnu-emacs@gnu.org>
2008-06-13 18:37           ` Josh

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).