sagemath / sage-shell-mode

Emacs front end for SageMath
GNU General Public License v3.0
98 stars 16 forks source link

Add "blocks" movement and send functions #14

Closed johanrosenkilde closed 7 years ago

johanrosenkilde commented 7 years ago

This pull request adds some simple functions to sage-shell-mode and sage-shell:sage-mode to accommodate convenient workflow when structuring experimental code in "sheets" of blocks, similar to how one works with cells in a Jupyter Notebook.

The use case is as follows: a user creates a "sheet" file, e.g. experiment.sage with blocks of code which represents both his library code as well as his experiments:

### Implement the new algorithm
def new_algo(x, y):
    return x + y

def random_input():
    return 7

### Check the new algorithm on small input
print new_algo(1, 2)

### Check the new algorithm on big input
def construct_my_big_number():
    return 100
print new_algo(construct_my_big_number(), 3)

### Check on some random input
print new_algo(random_input() , random_input())

In this case load(experiment.sage) is not a good alternative to the way one works with the Jupyter Notebook: rather, you want to evaluate the code block by block.

This pull request gives a few functions: sage-shell-blocks:forward and sage-shell-blocks:backward to move according to the delimiter ### (which can be changed). It also provides the function sage-shell-blocks:send-current which sends the region delimited by the previos ### and the following ###.

If my cursor is in the body of new_algo, then sage-shell-blocks:send-current would send the defs of new_algo and random_input to the Sage shell. Furthermore, it would print the "title" of the block:

sage: load('/tmp/sage_shell_mode3946wC1/sage_shell_mode_temp.sage')
--- Implement the new algorithm ---
sage:

The cursor has moved to the end of the sent block, and calling sage-shell-blocks:send-current again will send the next block.

A last function is sage-shell-blocks:pull-next for sage-shell-mode to "pull" the next block from the last visited sage-shell:sage-mode file. This is a convenient shorthand for visiting that file yourself and sending the block.

By default, I've not set sage-shell-blocks.el to be included; the user has to add (request sage-shell-blocks) to his .emacs.

stakemori commented 7 years ago

Thank you very for the pull request. Could you use sage-shell instead of sage the for :group keyword in defcustom? Also could you write document for blocks in README.org (just before or after ** SageTeX section)?

By default, I've not set sage-shell-blocks.el to be included; the user has to add (request sage-shell-blocks) to his .emacs.

I think it would be better to use autoload. And I have to change the Melpa recipe. Currently it is a recipe for a package with a single elisp file.

stakemori commented 7 years ago

Also please clarify the license. (sage-shell-mode is GPL.)