ein-python.el 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ;;; ein-python.el --- Workarounds for python.el
  2. ;; Copyright (C) 2012 Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-python.el is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; ein-python.el is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with ein-python.el.
  15. ;; If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;;; Code:
  19. (require 'python)
  20. (require 'ein-worksheet)
  21. (defvar ein:python-block-start
  22. (rx line-start
  23. symbol-start
  24. (or "def" "class" "if" "elif" "else" "try"
  25. "except" "finally" "for" "while" "with")
  26. symbol-end))
  27. (defun ein:python-indent-calculate-levels ()
  28. "Forcefully set indent level to 0 when there is no python block
  29. yet in this cell."
  30. (ein:and-let* ((cell (ein:worksheet-get-current-cell :noerror t))
  31. (beg (ein:cell-input-pos-min cell))
  32. ((< beg (point))))
  33. (save-excursion
  34. (unless (search-backward-regexp ein:python-block-start beg t)
  35. (setq python-indent-levels (list 0))
  36. (setq python-indent-current-level 0)
  37. t))))
  38. (defadvice python-indent-calculate-levels
  39. (around ein:python-indent-calculate-levels activate)
  40. "Hack `python-indent-calculate-levels' to reset indent per cell.
  41. Let's say you have a notebook something like this::
  42. In [1]:
  43. def func():
  44. pass
  45. In [2]:
  46. something[]
  47. Here, ``[]`` is the cursor position. When you hit the tab here,
  48. you don't expect it to indent. However, python.el tries to follow
  49. the indent of ``func()`` then you get indentation. This advice
  50. workaround this problem.
  51. Note that this workaround does not work with the MuMaMo based
  52. notebook mode."
  53. (unless (ein:python-indent-calculate-levels)
  54. ad-do-it))
  55. (provide 'ein-python)
  56. ;;; ein-python.el ends here