Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New keybinding for code cell execution + cell insertion #2090

Merged
merged 5 commits into from
Jul 20, 2012
Merged

New keybinding for code cell execution + cell insertion #2090

merged 5 commits into from
Jul 20, 2012

Conversation

v923z
Copy link
Contributor

@v923z v923z commented Jul 3, 2012

Hi all,

As discussed in http://mail.scipy.org/pipermail/ipython-dev/2012-July/009844.html and thereafter, I have added a new Alt+Enter combination for executing a cell and inserting a new code cell, even if the present cell is not at the bottom of the notebook. Files notebook.js, and quickhelp.js are affected. Let me know, if this combination is not appropriate for some reason. We could still go with Cntr+Shift+Enter.

Cheers,
Zoltán

@fperez
Copy link
Member

fperez commented Jul 4, 2012

Can't look at the code right now, but I think this is super useful functionality. I keep myself wanting it all the time. Thanks!

} else if (event.which === key.ENTER && event.altKey) {
that.execute_selected_cell();
that.insert_cell_above('code');
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you are using insert cell above because when executing the focus goes to the next cell...
Could you add a comment to explain it, it is not obvious when looking at the code.
Also, how does it behave at the end of a notebook ?

You could also add one line in one of the example notebook for user to discover it.

@v923z
Copy link
Contributor Author

v923z commented Jul 14, 2012

@Carreau You are right in your reasoning, and one of my reasons for implementing the feature in this particular way was that I didn't want to create untested code, it seemed so much safer to use proven and working code. As for the end of the notebook, you are right that this is going to create newer and newer cells, and perhaps, I could check for that. That should not be too hard, for there is code for that, too:)

I also wanted to point out earlier that I hadn't updated the user manual pages either. I will do that in the next iteration. But I would have a very dumb question here. There is a pending pull request here. What happens, if I implement what you suggest (checking of the last code cell), update the manual, and extend one of the notebooks? How will the PR be handled? Do I have to issue a new PR then, or I could still work on the code that I uploaded to my branch?

@minrk
Copy link
Member

minrk commented Jul 15, 2012

@v923z any changes you make to this branch will be reflected in the PR. For this reason, it is appropriate to create a PR when you have enough code to start a discussion about it (e.g. if you have a question about part of the implementation), and just continue to commit to the branch until it is done. You can also rebase on master and force push, in case you want to clean up a PR or squash commits.

@v923z
Copy link
Contributor Author

v923z commented Jul 19, 2012

@Carreau Thanks for the suggestions, I have a new implementation now. First, the code contains a couple of comment lines, second, I also check whether we really have to insert a new code cell. That would cover the case, when cell execution happens at a place where there is already an empty cell below. In such a case, the cursor simply jumps to the next (and empty) cell, but no new cell is created. This should prevent pollution of the notebook by lots of empty cells.

I am not sure whether the method that I used for checking for the emptiness of the cell is the best one. Brian could perhaps comment on this?

I have also added a section to the doc source file. In order to accommodate the explanation of the Alt-Enter combination, I had to change the section on 'Ctrl-Enter' a bit. I hope it is OK.

// Execute code cell, and insert new in place
that.execute_selected_cell();
// Only insert a new cell, if we ended up in an already populated cell
if (that.get_selected_cell().toJSON().input !== "") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.toJSON().input == .get_text()in this case, and it won't work if cell has only \nand inside (and obviously it is considered empty).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the equality really true in this case? Would there be any differences in terms of performance? I have left .toJSON() in place, but if you say that get_text() is better, I could change that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codecell.js

301     CodeCell.prototype.toJSON = function () {$
302         var data = IPython.Cell.prototype.toJSON.apply(this);$
303  >>>>   data.input = this.get_text();$
304         data.cell_type = 'code';$
305         if (this.input_prompt_number) {$
306             data.prompt_number = this.input_prompt_number;$
307         };$
308         var outputs = this.output_area.toJSON();$
309         data.outputs = outputs;$
310         data.language = 'python';$
311         data.collapsed = this.collapsed;$
312         return data;$
313     };$

so toJSON is an unnecessary extra call as it uses .get_text() under the hood, and does not apply any transform to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comments! I have changed the code accordingly.

@Carreau
Copy link
Member

Carreau commented Jul 20, 2012

And I'm far from beeing the best to review english in doc...

@fperez
Copy link
Member

fperez commented Jul 20, 2012

English looks good, and I'm +1 on the feature. Haven't reviewed the implementation though.

@v923z
Copy link
Contributor Author

v923z commented Jul 20, 2012

@Carreau You are absolutely right, so I added checking for this. A new cell will be inserted only if the cell below contains non-whitespace characters (tabs, spaces, and linefeeds will be regarded as empty cell.) I think this should address the issue you raised.

@v923z
Copy link
Contributor Author

v923z commented Jul 20, 2012

I don't want to throw this discussion off track, but I was wondering whether, along the lines that Matthias brought up, we should prevent "execution" of a cell, if it is empty, i.e., it contains only whitespaces. This could be done by adding
the single line

    if(/\S/.test(cell.get_text()) == false) return;

to the prototype function

Notebook.prototype.execute_selected_cell = function (options)

This would simply return, if there is nothing in the current code cell. Should I add this to the present pull request?

@Carreau
Copy link
Member

Carreau commented Jul 20, 2012

I would say no, as it is a convenient way to execute an empty cell to remove output.

@v923z
Copy link
Contributor Author

v923z commented Jul 20, 2012

On one hand, I see your point, but on the other hand, one could simply delete the cell, couldn't they? If you are executing an empty cell in order to remove the output, you have to remove the input, too, so nothing is left, really...

@Carreau
Copy link
Member

Carreau commented Jul 20, 2012

No, what I mean, if that if you want to start a cell from scratch, you will Ctrl-A, Ctrl-Enter, type.
Even Ctrl-A,Ctrl-C, Ctrl-Enter,Ctrl-V is faster than go for the menu to clear output.

Anyway, this shouldn't be added to the same pull request.

Carreau added a commit that referenced this pull request Jul 20, 2012
Notebook, Alt-enter : execute cell, append codecell below.
@Carreau Carreau merged commit 3a57667 into ipython:master Jul 20, 2012
@v923z
Copy link
Contributor Author

v923z commented Jul 20, 2012

Matthias, thanks for pulling this in!

@wltrimbl
Copy link

According to this discussion thread:
http://grokbase.com/t/scipy.org/ipython-user/12bxs04f8f/notebook-alt-enter-on-the-mac
and my experience with ipython 0.13.1 on OSX 10.8 / Firefox 18
the Alt-enter keybinding has been implemented in a way that simply does not work on some Mac OS systems. These mac users must use the mouse to create insert new cells in the middle of a notebook.

According to this article, capturing alt- and such are involved:
http://unixpapa.com/js/key.html

@Carreau
Copy link
Member

Carreau commented Jan 14, 2013

This will probably be fixable once we have properly configurable keybindings in notebook.
There are a lot of gotchas with all os/browser/layout and we can't please everyone right now.

But thanks for the link that might help when we work on that later.

mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
Notebook, Alt-enter : execute cell, append codecell below.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants