Skip to content

Commit

Permalink
Implemented improved Python indent function.
Browse files Browse the repository at this point in the history
The new indent function for Python mimics the behaviour of indent function
as seen in Emacs, where hitting 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
         v Cursor.
foo = bar|
```

after `newline-and-indent`:

```python
foo = bar
    |
    ^ It should not have indented, since the last line is just a
      top-level declaration.
```
  • Loading branch information
funk443 committed Jan 5, 2025
1 parent d99ddc3 commit e3f25c6
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions extensions/python-mode/python-mode.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,25 @@

#| link : https://www.python.org/dev/peps/pep-0008/ |#
(defun python-calc-indent (point)
(with-point ((point point))
(let ((tab-width (variable-value 'tab-width :default point))
(column (point-column point)))
(+ column (- tab-width (rem column tab-width))))))
(with-point ((point point) (last-line-point point))
(let* ((tab-width (variable-value 'tab-width :default point))
(last-line-indent-column
(progn
(line-offset last-line-point -1)
(back-to-indentation last-line-point)
(point-column last-line-point)))
(column (point-column (back-to-indentation point)))
(next-indent-column (+ last-line-indent-column tab-width))
(previous-indent-column
(max (- last-line-indent-column tab-width) 0)))
(cond
((and (>= column last-line-indent-column)
(< column next-indent-column))
next-indent-column)
((>= column next-indent-column)
previous-indent-column)
(t
last-line-indent-column)))))

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

0 comments on commit e3f25c6

Please sign in to comment.