Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.4 KiB

  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-indentation--around (orig &rest args)
  28. "False if there is no python block yet in this cell."
  29. (condition-case _
  30. (ein:and-let* ((cell (ein:worksheet-get-current-cell))
  31. (beg (ein:cell-input-pos-min cell))
  32. (p (point))
  33. ((< beg (point))))
  34. (if (not (search-backward-regexp ein:python-block-start beg t))
  35. 0
  36. (goto-char p)
  37. (apply orig args)))
  38. (error (apply orig args))))
  39. (advice-add 'python-indent--calculate-indentation :around #'ein:python-indent-calculate-indentation--around)
  40. ;; (defadvice python-indent-calculate-indentation
  41. ;; (around ein:python-indent-calculate-levels activate)
  42. ;; "Hack `python-indent-calculate-levels' to reset indent per cell.
  43. ;; Let's say you have a notebook something like this::
  44. ;; In [1]:
  45. ;; def func():
  46. ;; pass
  47. ;; In [2]:
  48. ;; something[]
  49. ;; Here, ``[]`` is the cursor position. When you hit the tab here,
  50. ;; you don't expect it to indent. However, python.el tries to follow
  51. ;; the indent of ``func()`` then you get indentation. This advice
  52. ;; workaround this problem.
  53. ;; Note that this workaround does not work with the MuMaMo based
  54. ;; notebook mode."
  55. ;; (if (ein:ein-block-start-p)
  56. ;; ad-do-it
  57. ;; 0))
  58. (provide 'ein-python)
  59. ;;; ein-python.el ends here