ein-python: Fix for #252

EIN should properly handle indentation in the worksheet as it once did long ago.
Issues was that python mode no longer uses the global variables
`python-ident-levels' for tracking ident. Now we advise around
`python-indent--calculate-indentation'. This is an internal function so may not
be the best call, but it works.

If it breaks maybe take a look at advising `python-ident-calculate-indentation'.
This commit is contained in:
John Miller 2017-11-18 11:24:24 -06:00
parent f63451ca03
commit dd0c0bfc52

View file

@ -37,40 +37,45 @@
"except" "finally" "for" "while" "with")
symbol-end))
(defun ein:python-indent-calculate-levels ()
"Forcefully set indent level to 0 when there is no python block
yet in this cell."
(ein:and-let* ((cell (ein:worksheet-get-current-cell :noerror t))
(beg (ein:cell-input-pos-min cell))
((< beg (point))))
(save-excursion
(unless (search-backward-regexp ein:python-block-start beg t)
(setq python-indent-levels (list 0))
(setq python-indent-current-level 0)
t))))
(defun ein:python-indent-calculate-indentation--around (orig &rest args)
"False if there is no python block yet in this cell."
(condition-case _
(ein:and-let* ((cell (ein:worksheet-get-current-cell))
(beg (ein:cell-input-pos-min cell))
(p (point))
((< beg (point))))
(if (not (search-backward-regexp ein:python-block-start beg t))
0
(goto-char p)
(apply orig args)))
(error (apply orig args))))
(defadvice python-indent-calculate-levels
(around ein:python-indent-calculate-levels activate)
"Hack `python-indent-calculate-levels' to reset indent per cell.
(advice-add 'python-indent--calculate-indentation :around #'ein:python-indent-calculate-indentation--around)
Let's say you have a notebook something like this::
;; (defadvice python-indent-calculate-indentation
;; (around ein:python-indent-calculate-levels activate)
;; "Hack `python-indent-calculate-levels' to reset indent per cell.
In [1]:
def func():
pass
;; Let's say you have a notebook something like this::
In [2]:
something[]
;; In [1]:
;; def func():
;; pass
Here, ``[]`` is the cursor position. When you hit the tab here,
you don't expect it to indent. However, python.el tries to follow
the indent of ``func()`` then you get indentation. This advice
workaround this problem.
;; In [2]:
;; something[]
;; Here, ``[]`` is the cursor position. When you hit the tab here,
;; you don't expect it to indent. However, python.el tries to follow
;; the indent of ``func()`` then you get indentation. This advice
;; workaround this problem.
;; Note that this workaround does not work with the MuMaMo based
;; notebook mode."
;; (if (ein:ein-block-start-p)
;; ad-do-it
;; 0))
Note that this workaround does not work with the MuMaMo based
notebook mode."
(unless (ein:python-indent-calculate-levels)
ad-do-it))
(provide 'ein-python)