Skip to content

Commit de01139

Browse files
committed
Implemented improved Python indent function.
The new indent function for Python mimics the behaviour of indent function as seen in Emacs, where hiting tab key will cycle through the following positions: - Same indent level as last line. - +1 indent level relative to last line. - -1 indent level relative to last line. There're still a flaw in this function, that I currently have no idea how to fix: ```python foo = bar| ``` after `newline-and-indent`: ```python foo = bar | ^ it should not have indented, since it's just a top-level assignment, not a `def` or `class`. ```
1 parent d99ddc3 commit de01139

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

extensions/python-mode/python-mode.lisp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,25 @@
8181

8282
#| link : https://www.python.org/dev/peps/pep-0008/ |#
8383
(defun python-calc-indent (point)
84-
(with-point ((point point))
85-
(let ((tab-width (variable-value 'tab-width :default point))
86-
(column (point-column point)))
87-
(+ column (- tab-width (rem column tab-width))))))
84+
(with-point ((point point) (last-line-point point))
85+
(let* ((tab-width (variable-value 'tab-width :default point))
86+
(last-line-indent-column
87+
(progn
88+
(line-offset last-line-point -1)
89+
(back-to-indentation last-line-point)
90+
(point-column last-line-point)))
91+
(column (point-column (back-to-indentation point)))
92+
(next-indent-column (+ last-line-indent-column tab-width))
93+
(previous-indent-column
94+
(max (- last-line-indent-column tab-width) 0)))
95+
(cond
96+
((and (>= column last-line-indent-column)
97+
(< column next-indent-column))
98+
next-indent-column)
99+
((>= column next-indent-column)
100+
previous-indent-column)
101+
(t
102+
last-line-indent-column)))))
88103

89104
(defun beginning-of-defun (point n)
90105
(loop :repeat n :do (search-backward-regexp point "^\\w")))

0 commit comments

Comments
 (0)