Emacs and UACalc

Preface

This page collects some very basic notes on using Python within Emacs. Specifically, we show how to start the Python interpreter and use the Python Read-Evaluate-Print Loop (REPL) while working in Emacs so that it’s easy to evaluate sections of Python code without leaving the editor. Some simple examples are given, including some examples that make use of UACalc Java classes. The last section contains a couple of notes about Magit, a nice tool for keeping Git repositories up to date without leaving Emacs.

If you find any mistakes or typos on this page, or if you have other suggestions for improving it, please post a comment in the box below. We would especially like to hear if you tried something on this page and it didn’t work.

Notation. Emacs key sequences are denoted below using the standard shorthand notation. For example,
`M-x` means type the meta key (e.g. Esc) followed by x.
`C-c` means hold down the control key and type c.

(Commands that appear within paragraphs are delimited by tick marks, as in `command`. The tick marks are not part of the command and should not be typed.)


Contents


Preliminary setup

Each section below assumes you have a recent version of Emacs installed. If not and you’re using Ubuntu Linux, try the following:

# Install emacs24
# https://launchpad.net/~cassou/+archive/emacs
sudo apt-add-repository -y ppa:cassou/emacs
sudo apt-get update
sudo apt-get install -y emacs24 emacs24-el emacs24-common-non-dfsg

If you’re starting with a minimal Ubuntu Linux distribution—e.g., you’re working off a new Amazon EC2 instance in the cloud—consider running the setup.sh script available here, which makes some convenient customizations, and installs some useful packages, including Emacs.

The section Jython/UACalc REPL in Emacs below assumes you have already installed Jython by running the setup.sh script described in other notes on this site.


Python REPL in Emacs

  1. Open a .py file
    Start Emacs and create a new file (`C-x C-f`), say, test.py. This should put the Emacs buffer in Python mode automatically. (If “(Python)” is not displayed at the bottom of the Emacs buffer, then you don’t have python-mode configured to start up automatically. See this page for how to fix this. For now, you can put the buffer in Python mode manually with `M-x python-mode`.) If you don’t have any Python code to try out at the moment, put the following in your test.py file:

    class Test():
        def test_print(self):                                            
            print "The Python interpreter works in Emacs!" 
    
    t = Test()
    t.test_print()
  2. Start the Python interpreter
    In Emacs, do `C-c C-z` (which is a shortcut for: `M-x python-shell-switch-to-shell`). In response, the Emacs mini-buffer should offer you a default Python interpreter to use, for example, /usr/bin/python. (If not, then you might need to upgrade your version of Emacs. See the comments at the beginning of the next section.) Hit enter to accept the default Python interpreter. (We’ll use the Jython interpreter and UACalc classes in the next section.)
  3. Use the Python REPL
    A few things to try: In the *Python* buffer, at the >>> prompt, type a line of Python, e.g. 2+2, and hit enter. This shows the REPL is working. Now switch to the test.py buffer (`C-c C-o`) and type `C-c C-c` to send the whole file to the Python REPL. Then you can use the Test() class at the command line. For example, switch back to the *Python* buffer and, at the >>> prompt, type

    >>> t2 = Test()
    >>> t2.test_print()

    You can also select a region of code in your .py file (`C-space` to set the mark, `C-n` to go down), and type `C-c C-r` to send just that region to the Python REPL.


Jython/UACalc REPL in Emacs

This section assumes you have followed the steps described in the notes on configuring the UACalc command line. You will then have a file called ~/UACalc/Examples/AlgebraConstructionExample.py.

Important Note: Peter Mayr pointed out that the commands in this section will not work if you’re using emacs23. Please upgrade to emacs24 using the following commands:

# Remove emacs23
sudo apt-get remove emacs23
# Install emacs24 (see https://launchpad.net/~cassou/+archive/emacs)
sudo apt-add-repository -y ppa:cassou/emacs
sudo apt-get update
sudo apt-get install emacs24 emacs24-el emacs24-common-non-dfsg

Now that emacs24 is installed, we can demonstrate the Emacs Jython REPL.

  1. Open a .py file
    In Emacs, type `C-x C-f` and enter the file name or, if Emacs is not running, enter the following at a Linux prompt:

    emacs ~/UACalc/Examples/AlgebraConstructionExample.py

    As mentioned in the previous section, this should put the Emacs buffer in Python mode.

  2. Start the Jython interpreter
    In Emacs, do `C-c C-z` (which is a shortcut for: `M-x python-shell-switch-to-shell`). In response, the Emacs mini-buffer should offer a default Python interpreter to use, for example, /usr/bin/python. (Do not hit Enter at this point!) Your Emacs window should look something like this

    JythonREPL

    Important: instead of accepting the default choice (/usr/bin/python), replace this with uacalc and hit enter. A new buffer window should open with the following greeting:

    Starting Jython with UACalc dependencies.  Please be patient...
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    Welcome to the command line version of UACalc!
      to exit type quit()
      (more help coming)
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    Now you can try out some of the code in the AlgebraConstructionExample.py file, which you should have open in the other Emacs buffer. For example, highlight the first few lines of code (`C-space` to set the mark, `C-n` to go down) and then type `C-c C-r`. To evaluate the whole file, type `C-c C-c`. The following output will appear in the *Python* buffer:

    >>> __pyfile = open('''/tmp/py15317oiI''');exec(compile(__pyfile.read(), '''/home/ubuntu/UACalc/UACalc_Jython/Jython/AlgebraConstructionExample.py''', 'exec'));__pyfile.close()
    ---- Example 1 ----
    Quick sanity check of the operations:
       binaryPlusMod5:  4 + 10 mod 5 =  4
       ternaryPlusMod5:  4 + 10 + 1 mod 5 =  0
    Quick check that we constructed an algebra:
       alg.getName() =  MyAlgebra
       alg.universe() =  [0, 1, 2, 3, 4]
    UACalc algebra file created: ../Algebras/Example1_ConstructAlgebra.ua
    
    ---- Example 2 ----
    The unary operations are:
       f0: 7 6 6 7 3 2 2 3
       f1: 0 1 1 0 4 5 5 4
       f2: 0 2 3 1 0 2 3 1
    
    Quick check that we constructed an algebra:
       alg.getName() =  MyUnaryAlgebra
       alg.universe() =  [0, 1, 2, 3, 4, 5, 6, 7]
    UACalc algebra file created: ../Algebras/Example2_MutliunaryAlgebra.ua
    >>>

Git in Emacs

Git is a free and open source distributed version control system created by Linus Torvalds. (To learn the basics of Git, check out the 15 minute tutorial or the the “no ds” guide.) Although one should know how to use Git from the command line first, it is possible to speed up the usual modify-commit-push workflow by interacting with a Git repository from within Emacs. Magit is an Emacs extension that provides this functionality. The notes in this section give some tips on installing Magit and mention a few of the most frequently used Magit commands.

  • Install Magit
    1. Add the following to your ~/.emacs.d/init.el file (create that file if it doesn’t exist):
      (setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
      ("marmalade" . "http://marmalade-repo.org/packages/")
      ("melpa" . "http://melpa.milkbox.net/packages/")))
      (define-key global-map "\M-gm" 'magit-status)
    2. Start Emacs and invoke the following:
      M-x package-install

      when it prompts you with “Install package:”, enter magit.

  • Use Magit
    Follow the directions here: http://magit.github.io/magit/magit.html
    They might seem a bit confusing at first, but here’s a quick example to get you started:

    1. Stage a file for commit. After you’ve used Emacs to edit a file, enter `M-x magit-status` to open the magit status buffer (shortcut: `M-g m`). In the magit status buffer, move to the line
      where the file you want to stage for commit is listed.  Type s to stage that file.
    2. Commit changes. Type `c c`, which will bring up a new buffer. Write your commit comments
      there, then type `C-c C-c` to commit the change. The first two steps above are covered at:
      http://magit.github.io/magit/magit.html#Staging-and-Committing
    3. Push to a remote repository. Assuming a remote (e.g. GitHub) repository is configured, if you now type  `P P`  (capitals!) from within the magit-status window, this will push everything that is listed under “Unpushed,” as explained here:
      http://magit.github.io/magit/magit.html#Pushing-and-Pulling

References

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s