diff --git a/lisp/ein-python.el b/lisp/ein-python.el index ece6ab0..454b029 100644 --- a/lisp/ein-python.el +++ b/lisp/ein-python.el @@ -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)