all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Break code lines fluently
@ 2014-03-12  5:42 Andrey Tykhonov
  2014-03-12 10:07 ` Andreas Röhler
  2014-03-12 14:25 ` Drew Adams
  0 siblings, 2 replies; 5+ messages in thread
From: Andrey Tykhonov @ 2014-03-12  5:42 UTC (permalink / raw
  To: help-gnu-emacs


Hi all!

When I've used Eclipse I liked its default key binding "Shift-Enter" which
inserts an empty line (with indentation) after the current line. It is very
easy to implement such function in Emacs, please read, if you're
interested: http://emacsredux.com/blog/2013/03/26/smarter-open-line/ .

I used to use this function. It is, as for me, quite handy while browsing
the code to hit "Shift-Enter" and just insert some piece of code. But what
I really were missing is having the same line opening command but for
elisp. It would be better explain by means of example. Let say I have the
following code:

(defun test ()
  (let ((test t))
    (if test
        (message "True")
      (mes|sage "False"))))

Point is located in the middle of "message" function. And I would like to
insert more code to the `let' form (after the `if'). What I was always
doing in such situations, I did always navigate to the appropriate closing
bracket (in this case closing bracket of the `if') and hit Enter. (Yes,
probably there are more better ways to do that. Please share. It is very
interesting to know). Despite the fact that I quite often investigate emacs
configurations of others, wiki, documentation and packages only today I
found the solution to have possibility to break the lines more fluently. So
having such mentioned code I can execute sequentially the command several
times and have the following code to be ready to be extended in the right
place:

(defun test ()
  (let ((test t))
    (if test
        (message "True")
      (message "False"))
    |))

(First execution of this command breaks a line for "message", the second
execution undos this change, the third execution will break a line for
`if', the fourth undos and fifth will break a line for `let').

The command which I just mentioned is the `smart-return' command from the
`emacs-smart-return' tiny package which I just recently wrote and which you
could find at https://github.com/atykhonov/emacs-smart-return It is a very
thin wrapper around `smart-forward' package (Read:
https://github.com/magnars/smart-forward.el). This is just very initial
version. Thus any comments, suggestions are highly appreciated.


With hope that mentioned functionality will be helpful and useful,
Andrey.


Thanks!



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

* Re: Break code lines fluently
  2014-03-12  5:42 Break code lines fluently Andrey Tykhonov
@ 2014-03-12 10:07 ` Andreas Röhler
  2014-03-12 14:25 ` Drew Adams
  1 sibling, 0 replies; 5+ messages in thread
From: Andreas Röhler @ 2014-03-12 10:07 UTC (permalink / raw
  To: help-gnu-emacs

Am 12.03.2014 06:42, schrieb Andrey Tykhonov:
>
> Hi all!
>
> When I've used Eclipse I liked its default key binding "Shift-Enter" which
> inserts an empty line (with indentation) after the current line. It is very
> easy to implement such function in Emacs, please read, if you're
> interested: http://emacsredux.com/blog/2013/03/26/smarter-open-line/ .
>
> I used to use this function. It is, as for me, quite handy while browsing
> the code to hit "Shift-Enter" and just insert some piece of code. But what
> I really were missing is having the same line opening command but for
> elisp. It would be better explain by means of example. Let say I have the
> following code:
>
> (defun test ()
>    (let ((test t))
>      (if test
>          (message "True")
>        (mes|sage "False"))))
>
> Point is located in the middle of "message" function. And I would like to
> insert more code to the `let' form (after the `if'). What I was always
> doing in such situations, I did always navigate to the appropriate closing
> bracket (in this case closing bracket of the `if') and hit Enter. (Yes,
> probably there are more better ways to do that. Please share. It is very
> interesting to know). Despite the fact that I quite often investigate emacs
> configurations of others, wiki, documentation and packages only today I
> found the solution to have possibility to break the lines more fluently. So
> having such mentioned code I can execute sequentially the command several
> times and have the following code to be ready to be extended in the right
> place:
>
> (defun test ()
>    (let ((test t))
>      (if test
>          (message "True")
>        (message "False"))
>      |))
>
> (First execution of this command breaks a line for "message", the second
> execution undos this change, the third execution will break a line for
> `if', the fourth undos and fifth will break a line for `let').
>
> The command which I just mentioned is the `smart-return' command from the
> `emacs-smart-return' tiny package which I just recently wrote and which you
> could find at https://github.com/atykhonov/emacs-smart-return It is a very
> thin wrapper around `smart-forward' package (Read:
> https://github.com/magnars/smart-forward.el). This is just very initial
> version. Thus any comments, suggestions are highly appreciated.
>
>
> With hope that mentioned functionality will be helpful and useful,
> Andrey.
>
>
> Thanks!
>
>

Very interesting, thanks!



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

* RE: Break code lines fluently
  2014-03-12  5:42 Break code lines fluently Andrey Tykhonov
  2014-03-12 10:07 ` Andreas Röhler
@ 2014-03-12 14:25 ` Drew Adams
  2014-03-12 15:55   ` Drew Adams
  2014-03-13  0:14   ` Andrey Tykhonov
  1 sibling, 2 replies; 5+ messages in thread
From: Drew Adams @ 2014-03-12 14:25 UTC (permalink / raw
  To: Andrey Tykhonov, help-gnu-emacs

> (defun test ()
>   (let ((test t))
>     (if test
>         (message "True")
>       (mes|sage "False"))))
> Point is located in the middle of "message" function. And I would like to
> insert more code to the `let' form (after the `if').

Here is one approach:

C-- 2 C-M-u RET

C-M-u is `backward-up-list'.  You give it a negative prefix arg to go
forward instead of backward.  -2 means go forward and up past (-1) the
`message' sexp and then (-2) past the `if' sexp.  RET then indents
(with recent Emacs dev snapshots - use C-j with older versions).

If you don't want to count, just repeat C-- C-M-u until you get where
you want to go.

You can also bind `up-list' to a key, and use that to do the same
thing as C-M-u with a negative arg.  E.g.

M-x global-set-key C-o RET up-list RET

C-o C-o RET ; move up & forward 2 list levels, then newline & indent

The real advantage of binding a key to `up-list' is that you can
just hold that key (C-o or whatever) down until you get where you
want (the same as you can do with `backward-up-list', C-M-u).

Prefix key `C-M-' introduces Lisp stuff in Emacs-Lisp mode, including
commands that move over lists: e.g., C-M-u, C-M-d, C-M-n, C-M-p.  And
commands that move over sexps, whether lists or not: C-M-f, C-M-b.

Others will no doubt give you other ways, in particular that make use
of 3rd-party libraries.  There are many ways to skin this cat.

(If you use a mouse, you can of course also just click where you want
to insert the newline: direct access.  That's sometimes easier or
quicker, since you can see the destination and you don't need to move
there incrementally or by counting list levels.  But it does take one
hand off the keyboard.)



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

* RE: Break code lines fluently
  2014-03-12 14:25 ` Drew Adams
@ 2014-03-12 15:55   ` Drew Adams
  2014-03-13  0:14   ` Andrey Tykhonov
  1 sibling, 0 replies; 5+ messages in thread
From: Drew Adams @ 2014-03-12 15:55 UTC (permalink / raw
  To: Andrey Tykhonov, help-gnu-emacs

> If you don't want to count, just repeat C-- C-M-u until you get where
> you want to go.
> 
> You can also bind `up-list' to a key, and use that to do the same
> thing as C-M-u with a negative arg.

I should perhaps have also mentioned that you can use one or more
C-M-u followed by a C-M-f to get the same effect as the same number of
calls to `up-list'.  IOW, go up backwards to get to the list level you
want, then go forward to the end of that list instead of its beginning.



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

* Re: Break code lines fluently
  2014-03-12 14:25 ` Drew Adams
  2014-03-12 15:55   ` Drew Adams
@ 2014-03-13  0:14   ` Andrey Tykhonov
  1 sibling, 0 replies; 5+ messages in thread
From: Andrey Tykhonov @ 2014-03-13  0:14 UTC (permalink / raw
  To: Drew Adams, help-gnu-emacs

Drew Adams <drew.adams@oracle.com> writes:

>> (defun test ()
>>   (let ((test t))
>>     (if test
>>         (message "True")
>>       (mes|sage "False"))))
>> Point is located in the middle of "message" function. And I would like to
>> insert more code to the `let' form (after the `if').
>
> Here is one approach:
>
> C-- 2 C-M-u RET
>
> C-M-u is `backward-up-list'.  You give it a negative prefix arg to go
> forward instead of backward.  -2 means go forward and up past (-1) the
> `message' sexp and then (-2) past the `if' sexp.  RET then indents
> (with recent Emacs dev snapshots - use C-j with older versions).
>
> If you don't want to count, just repeat C-- C-M-u until you get where
> you want to go.
>
> You can also bind `up-list' to a key, and use that to do the same
> thing as C-M-u with a negative arg.  E.g.
>
> M-x global-set-key C-o RET up-list RET
>
> C-o C-o RET ; move up & forward 2 list levels, then newline & indent
>
> The real advantage of binding a key to `up-list' is that you can
> just hold that key (C-o or whatever) down until you get where you
> want (the same as you can do with `backward-up-list', C-M-u).
>
> Prefix key `C-M-' introduces Lisp stuff in Emacs-Lisp mode, including
> commands that move over lists: e.g., C-M-u, C-M-d, C-M-n, C-M-p.  And
> commands that move over sexps, whether lists or not: C-M-f, C-M-b.
>
> Others will no doubt give you other ways, in particular that make use
> of 3rd-party libraries.  There are many ways to skin this cat.
>
> (If you use a mouse, you can of course also just click where you want
> to insert the newline: direct access.  That's sometimes easier or
> quicker, since you can see the destination and you don't need to move
> there incrementally or by counting list levels.  But it does take one
> hand off the keyboard.)

Thank you for such detailed the answer! I learned something new. I didn't
know about `up-list' command and most of the mentioned key bindings.

The goal of my little package is to insert new line (based on context) to a
Emacs Lisp code by means of very handy way (I believe so).

I like approach used by this package because it allows: 

1) do not pay additional attention (by looking at highlighted opened
bracket) whether my cursor is located after the right closing bracket (so
then I can hit C-j and input a code); after execution of `smart-return'
command new line indents so it is a little bit easily to note by means of
indentation level whether it is right place to input new code or not;

2) do not perform additional *navigation* through the code (within opened
and closed brackets) but just insert new line from the current cursor
position and to be ready to input new code immediately (by means of one
`smart-return' command execution or several).

3) to have one shorthand key binding (for example H-RET, as I have) instead
of different combinations of key bindings. For example:

(setq gnus-gcc-mark-as-read t
      gnus-expert-user t
      gnus-suppress-duplicates t
      gnus-con|firm-mail-reply-to-news t
      gnus-ignored-newsgroups ""
      nnml-get-new-mail t
      gnus-add-to-list t)

to append new SYM/VAL pairs to the `setq' form I need to C-o (`up-list'),
then C-b and lastly C-j. (Or probably the best way to do that is to hit
C-M-f as much as required and then C-j). But instead of such navigation and
following C-j I would like to have one command which can just *put* cursor
in the right place and will allow just to input SYM/VAL pair to the `setq'
form. Is case of such `setq' and `smart-return' it is required to hit key
binding only once.

"one hit" instead of several hits (in such case and others) probably
doesn't bring a huge advantage. But for me valuable advantage is to have
one key binding instead of several (such as `forward-sexp', `up-line') for
such trivial purpose as insert new line.


(By the way, I just updated this package and removed surplus `undo'
step. So mentioned example in the original message could be performed by
means of just three command executions instead of five).


Thank you!

Andrey



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

end of thread, other threads:[~2014-03-13  0:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-12  5:42 Break code lines fluently Andrey Tykhonov
2014-03-12 10:07 ` Andreas Röhler
2014-03-12 14:25 ` Drew Adams
2014-03-12 15:55   ` Drew Adams
2014-03-13  0:14   ` Andrey Tykhonov

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.