From e3f25c6814e0470e079582f81087562d1a9cd887 Mon Sep 17 00:00:00 2001 From: CToID Date: Mon, 6 Jan 2025 01:23:13 +0800 Subject: [PATCH] Implemented improved Python indent function. 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. ``` --- extensions/python-mode/python-mode.lisp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/extensions/python-mode/python-mode.lisp b/extensions/python-mode/python-mode.lisp index 2f1afbef6..d44bbb112 100644 --- a/extensions/python-mode/python-mode.lisp +++ b/extensions/python-mode/python-mode.lisp @@ -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")))