如何计算 emacs org 模式下子树的字符数?

How to count characters of a subtree in emacs org mode?

我想在 org 模式下计算子树(标题)中的字符数。现在我已经想出了如何计算单个段落中的字符数,而不是多个段落中的字符数。我先定义一个源码块:

#+NAME: countChars
#+BEGIN_SRC sh :var X="" :results output
echo "$X" | wc --chars
#+END_SRC

然后我在命名段落上使用它:

#+NAME: paragraph
This is the paragraph

#+CALL: countChars(paragraph)

这很有效,但#+NAME: 仅包含一个段落。我曾尝试使用标题作为参数,但我无法使其正常工作。

编辑:根据评论,我想出了:

#+NAME: countChars
#+BEGIN_SRC emacs-lisp :results output :eval no-export :exports results
(interactive)
(save-excursion
  (org-mark-subtree)
  (setq a (- (mark) (point)))
  (deactivate-mark)
  (prin1 'Count= )
  (prin1 a))
#+END_SRC

当调用

时几乎可以满足我的要求
#+CALL: countChars()

但是在计算源代码块(包括它本身)和文本时存在问题。我只想计算文本(不包括标题)。

只能在源块前使用#+NAME,不能在子树前使用。

用 emacs lisp 写起来更容易。

此代码块将统计当前子树中的字符数,不包括header或最后一行内容: 如果你想使用 emacs lisp 计算当前子树中的字符数,试试这个:

(save-excursion
  (org-mark-subtree) ;mark the whole subtre
  (forward-line 1)   ;move past header
  (exchange-point-and-mark) ;swap point and mark (ends of region)
  (forward-line -1)  ;move backwards past the last line
  (let ((nchars (- (point) (mark))))
    (deactivate-mark) ;clear the region
    (message "%d" nchars))))