Skip to content

sagemath/sage-shell-mode

 
 

Repository files navigation

sage-shell-mode

http://melpa.org/packages/sage-shell-mode-badge.svg http://stable.melpa.org/packages/sage-shell-mode-badge.svg https://travis-ci.org/stakemori/sage-shell-mode.svg

Overview

sage-shell-mode is an elisp package and provides an Emacs front end for Sage.

By sage-shell-mode, you can run Sage process in GNU Emacs and send contents of a buffer or a file to the Sage process.

This package also provides a major-mode derived from python-mode.

There are extensions for this package, auto-complete-sage, helm-sage and anything-sage.

Table of Contents

Installation and Setup

You can install sage-shell-mode from MELPA.

  1. See http://melpa.org/#/getting-started if you do not have a configuration for MELPA.
  2. Install sage-shell-mode by M-x package-install RET sage-shell-mode RET.
  3. You can run Sage inside Emacs by M-x sage-shell:run-sage if Emacs can find the executable file of Sage.

If Emacs cannot find the executable file, put the following line to ~/.emacs.d/init.el.

(setq sage-shell:sage-root "/path/to/sage/root_directory")

And replace /path/to/sage_root_directory by the root directory of Sage, i.e. $SAGE_ROOT. If you do not know the root directory of Sage, evaluate the following code in Sage:

import os; print os.environ["SAGE_ROOT"]

Alternatively, instead of setting sage-shell:sage-root, you may set the variable sage-shell:sage-executable.

(setq sage-shell:sage-executable "/path/to/sage/executable")

Here /path/to/sage/executable is the path of the executable file of Sage. This may be a symbolic link.

Sample configuration

Here is a sample configuration.

;; Run SageMath by M-x run-sage instead of M-x sage-shell:run-sage
(sage-shell:define-alias)
;; Turn on eldoc-mode
(add-hook 'sage-shell-mode-hook #'eldoc-mode)
(add-hook 'sage-shell:sage-mode-hook #'eldoc-mode)

Aliases

The major mode sage-mode and the command run-sage are provided by sage-mode (the official sage-mode). To avoid name conflicts, sage-shell-mode uses redundant names. By putting the following code in ~/.emacs.d/init.el,

(sage-shell:define-alias)

the following aliases will be defined.

Original nameAlias
sage-shell:run-sagerun-sage
sage-shell:run-new-sagerun-new-sage
sage-shell:sage-modesage-mode
sage-shell:sage-mode-mapsage-mode-map
sage-shell:sage-mode-hooksage-mode-hook

Then you can run Sage by M-x run-sage instead of M-x sage-shell:run-sage with these aliases.

Basic Usage

Running a Sage process

You can run Sage by M-x sage-shell:run-sage. You can run new Sage process by M-x sage-shell:run-new-sage.

CommandAliasDescription
sage-shell:run-sagerun-sageRun a Sage process.
sage-shell:run-new-sagerun-new-sageRun a new Sage process.

The major-mode of the Sage process buffer is sage-shell-mode. The basic key-bidings in sage-shell-mode are as follows:

Key StrokeCommandDescription
RETsage-shell:send-inputSend the current input to the Sage process.
TABsage-shell-tab-commandComplete words at the point or indent a line.
C-dsage-shell:delchar-or-maybe-eofDelete backward a character or send EOF if there are no inputs.
C-c C-csage-shell:interrupt-subjobInterrupt the current subjob.
M-pcomint-previous-inputGo backward through input history.
M-nsage-shell:next-inputGo forward through input history.
C-c C-osage-shell:delete-outputDelete all outputs since last input.
C-c M-osage-shell:clear-current-bufferDelete all outputs in the current buffer. This does not delete the last prompt.
C-c C-lsage-shell:load-fileSend contents of a file to the Sage process.
C-c C-hsage-shell:helpShow a document string of a Sage object.
C-c osage-shell:list-outputsList inputs and outputs in a buffer.
C-c M-wsage-shell:copy-previous-output-to-kill-ringCopy the previous output to kill-ring

For more commands and key-bindings see the help by M-x describle-mode sage-shell-mode.

TAB completion

By default, TAB completion uses completion-at-point. You can use pcomplete by the following setting:

(setq sage-shell:completion-function 'pcomplete)

You can also use auto-complete, anything or helm for completion. This requires extensions.

Editing a Sage file

When you visit a file ended with .sage, then sage-shell:sage-mode will be the major-mode of the buffer automatically. If you want to edit a file ended with .py in sage-shell:sage-mode, then use the following magic comment at the first line of the file:

# -*- mode: sage-shell:sage -*-

With aliases above, instead of the line above you can use the following magic comment:

# -*- mode: sage -*-

The major mode sage-shell:sage-mode is almost same as python-mode you use. The differences are some of key-bidings.

The basic key-bidings in sage-shell:sage-mode are as follows:

KeyCommandDescription
C-c C-csage-shell-edit:send-bufferSend the current buffer to the process.
C-c C-rsage-shell-edit:send-regionSend the region to the process.
C-c C-jsage-shell-edit:send-lineSend the current line to the process.
C-c C-lsage-shell-edit:load-fileSend the file to the process.
C-c C-zsage-shell-edit:pop-to-process-bufferPop to the process buffer.

If you run multiple Sage processes, you can choose which process to send by M-x sage-shell:set-process-buffer.

Input history

If the variable sage-shell:input-history-cache-file is non-nil and it is a file name, then the input history (comint-input-ring) will be saved to the file. Here is a sample configuration:

(setq sage-shell:input-history-cache-file "~/.emacs.d/.sage_shell_input_history")

SageTeX

TEXINPUTS

When a Sage process is spawned by sage-shell:run-sage or sage-shell:run-new-sage, then sage-shell-mode adds $SAGE_ROOT/local/share/texmf/tex/generic/sagetex/ to the environment variable TEXINPUTS in Emacs. If you do not want to change the environment variable, set sage-shell-sagetex:add-to-texinputs-p to nil.

Commands for SageTeX

Here is a list of commands for SageTeX. These commands load a .sagetex.sage file generated by SageTeX to the existing Sage process.

CommandRun latex before loadingRun latex after loading
sage-shell-sagetex:load-fileNoNo
sage-shell-sagetex:run-latex-and-load-fileYesNo
sage-shell-sagetex:compile-fileYesYes

There are similar commands to above, sage-shell-sagetex:load-current-file, sage-shell-sagetex:run-latex-and-load-current-file and sage-shell-sagetex:compile-current-file.

Here is a sample setting for AUCTeX users.

(eval-after-load "latex"
  '(mapc (lambda (key-cmd) (define-key LaTeX-mode-map (car key-cmd) (cdr key-cmd)))
         `((,(kbd "C-c s c") . sage-shell-sagetex:compile-current-file)
           (,(kbd "C-c s C") . sage-shell-sagetex:compile-file)
           (,(kbd "C-c s r") . sage-shell-sagetex:run-latex-and-load-current-file)
           (,(kbd "C-c s R") . sage-shell-sagetex:run-latex-and-load-file)
           (,(kbd "C-c s l") . sage-shell-sagetex:load-current-file)
           (,(kbd "C-c s L") . sage-shell-sagetex:load-file)
           (,(kbd "C-c C-z") . sage-shell-edit:pop-to-process-buffer))))

For example, you can run sage-shell-sagetex:compile-current-file by C-c s c in a LaTeX-mode buffer with this setting.

Customize latex command

You can change a latex command used by sage-shell-sagetex:compile-file and sage-shell-sagetex:compile-current-file by setting sage-shell-sagetex:latex-command or sage-shell-sagetex:auctex-command-name.

If you are an AUCTeX user, then customize sage-shell-sagetex:auctex-command-name to change the latex command. The value of sage-shell-sagetex:auctex-command-name should be a name of a command in TeX-command-list (i.e car of an element of the list TeX-command-list), e.g.:

(setq sage-shell-sagetex:auctex-command-name "LaTeX")

You can also use the variable sage-shell-sagetex:latex-command to change the latex command. For example, if you want to run latexmk after loading a .sagetex.sage file, then use the following setting:

(setq sage-shell-sagetex:latex-command "latexmk")

The default value of sage-shell-sagetex:latex-command is latex -interaction=nonstopmode. If sage-shell-sagetex:auctex-command-name is non-nil, then the value of sage-shell-sagetex:latex-command is ignored.

Customization

To customize sage-shell-mode, M-x customize-group RET sage-shell or M-x customize-group RET sage-shell-sagetex.

Extensions

Screenshots

Automatic indentation and syntax highlighting work.

./images/indent.png

Completion with auto-complete-sage.

./images/ac.png

Completion with helm-sage.

./images/helm.png

./images/helm1.png

Workaround for flycheck

To use flycheck-mode in a sage-shell:sage-mode buffer and a python-mode buffer, try the following code.

(defun sage-shell:flycheck-add-mode (checker mode) "Add `mode' to
  `checker'."  (let ((modes (get checker 'flycheck-modes)))
  (unless (memq mode modes) (put checker 'flycheck-modes (cons
  mode modes)))))

(dolist (ckr '(python-pylint python-flake8))
  (sage-shell:flycheck-add-mode ckr 'sage-shell:sage-mode))

(defun sage-shell:flycheck-turn-on () "Enable flycheck-mode only
  in a file ended with py."  (when (let ((bfn (buffer-file-name)))
  (and bfn (string-match (rx ".py" eol) bfn))) (flycheck-mode 1)))

(add-hook 'python-mode-hook 'sage-shell:flycheck-turn-on)

License

Licensed under the GPL.