emacs中如何获取一行的缩进状态?(已缩进,为缩进)

如题,有没有相关的函数呢

「未缩进」是指「缩进量为 0」还是「没有缩进到正确的位置」?

back-to-indentation is an interactive compiled Lisp function in ‘simple.el’.

It is bound to M-m.

(back-to-indentation)

Move point to the first non-whitespace character on this line.
current-indentation is a built-in function in ‘C source code’.

(current-indentation)

  This function does not change global state, including the match data.

Return the indentation of the current line.
This is the horizontal position of the character
following any initial whitespace.

是没有缩进到正确的位置,不过我放弃这个想法了

(with-temp-buffer
  (insert "\
(list a
b)")
  (emacs-lisp-mode)
  (goto-char (1+ (length "(list a\n")))
  (calculate-lisp-indent))
;; => 6
calculate-lisp-indent is a compiled Lisp function in ‘lisp-mode.el’.

(calculate-lisp-indent &optional PARSE-START)

Return appropriate indentation for current line as Lisp code.
In usual case returns an integer: the column to indent to.
If the value is nil, that means don’t change the indentation
because the line starts inside a string.

PARSE-START may be a buffer position to start parsing from, or a
parse state as returned by calling ‘parse-partial-sexp’ up to the
beginning of the current line.

The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
This means that following lines at the same level of indentation
should not necessarily be indented the same as this line.
Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
is the buffer position of the start of the containing expression.

EDIT: 2021-09-14 11.50.42

  1. 其它模式可找找对应的 *-calculate-indent 函数。

  2. 并非所有的模式都直接提供 *-calculate-indent 函数,但是仍然可以从 TAB 键绑定的函数中回溯找到真正的实现。

    例如 c-mode,其实现缩进的函数是 c-indent-line,其中又可以找到计算缩进的语句是:(c-get-syntactic-indentation (c-guess-basic-syntax))

  3. 标题错别字改一下。

1赞

了解了,谢谢兄弟