Wednesday, October 29, 2008

emacs python mode from scratch: stage 9 - rest of the python indent code

So my idea was to move onto something completely new but wouldn't you know it, I found a couple more indentation related functions that needed dealing with.

The functions/variables in question are:

python-guess-indent (both variable and function with this name)

And just for fun we'll throw in the key mapping for python-backspace

(define-key map "\177" 'python-backspace)

So let's quickly browse through these functions and see how they work and make sure each works as promised.

  • python-guess-indent (variable)

    Seems weird to me that there can be a variable and function with the same name. I'm guessing this is a due to emacslisp being a lisp-2

    This guess seems to be confirmed by:

    In any case having this being true means that python mode will be using the following indentation guessing function.

  • python-guess-indent (function)

    This function does what it's name implies.

    Looks for a ":" character and then moves to the beginning of that line. Then it moves to the following line and takes the difference. If the different is >= 2 and <= 8 then it take that as a guess. Otherwise it continues to the next ":".

    If the value it found is different from the default value then it sets it to this new value.

  • python-beginning-of-defun

    Look for a regular expression consisting of space and either def or class.

  • python-end-of-defun

    This is a little trickier than python-beginning-of-defun. Here's another place where python's whitespace makes life hard. Good thing it's pretty to look at.

    First find the beginning of the block we are currently in. If we are at the beginning of a line then but not at the start of a block, move forward until we find a block starting line.

    From here python-end-of-block does the rest of the work.

  • python-backspace

    Look at the list of valid indentation levels for the current line and then back track to the next smallest.

    Also present a message explaining what block you just closed.

I'm now 40% through. Almost starting to seem like I might make it all the way through this puppy.

No comments: