Count the number of paragraphs
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
There are plenty of ways to count lines, words, and characters in Emacs. For instance, invoking M-=
(AKA count-words-region
) does that very thing on a given region. However, I am having trouble finding a way to do this for paragraphs. By paragraph, I mean the same pattern that is used for forward-paragraph
and backward-paragraph
. I see no easy or straightforward way to count the number of paragraphs in a region or buffer without using regexps or similar. I'm sure that I'm not the first person to face this problem, so I must be missing something. If someone could provide an easy, straightforward way to count the paragraphs in a region or buffer, I would be glad.
buffers region commands text paragraphs
add a comment |Â
up vote
1
down vote
favorite
There are plenty of ways to count lines, words, and characters in Emacs. For instance, invoking M-=
(AKA count-words-region
) does that very thing on a given region. However, I am having trouble finding a way to do this for paragraphs. By paragraph, I mean the same pattern that is used for forward-paragraph
and backward-paragraph
. I see no easy or straightforward way to count the number of paragraphs in a region or buffer without using regexps or similar. I'm sure that I'm not the first person to face this problem, so I must be missing something. If someone could provide an easy, straightforward way to count the paragraphs in a region or buffer, I would be glad.
buffers region commands text paragraphs
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
There are plenty of ways to count lines, words, and characters in Emacs. For instance, invoking M-=
(AKA count-words-region
) does that very thing on a given region. However, I am having trouble finding a way to do this for paragraphs. By paragraph, I mean the same pattern that is used for forward-paragraph
and backward-paragraph
. I see no easy or straightforward way to count the number of paragraphs in a region or buffer without using regexps or similar. I'm sure that I'm not the first person to face this problem, so I must be missing something. If someone could provide an easy, straightforward way to count the paragraphs in a region or buffer, I would be glad.
buffers region commands text paragraphs
There are plenty of ways to count lines, words, and characters in Emacs. For instance, invoking M-=
(AKA count-words-region
) does that very thing on a given region. However, I am having trouble finding a way to do this for paragraphs. By paragraph, I mean the same pattern that is used for forward-paragraph
and backward-paragraph
. I see no easy or straightforward way to count the number of paragraphs in a region or buffer without using regexps or similar. I'm sure that I'm not the first person to face this problem, so I must be missing something. If someone could provide an easy, straightforward way to count the paragraphs in a region or buffer, I would be glad.
buffers region commands text paragraphs
buffers region commands text paragraphs
asked 6 hours ago
GDP2
702218
702218
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Here is how the builtin count-lines
works:
(- (buffer-size) (forward-line (buffer-size)))
The following changes forward-line
to forward-paragraph
to implement the function.
(defun count-paragraphs (start end)
"Return number of paragraphs between START and END."
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(- (buffer-size) (forward-paragraph (buffer-size))))))
(defun count-paragraphs-region-or-buffer ()
"Report number of paragraphs in the region (if it's active) or the entire buffer."
(declare (interactive-only count-paragraphs))
(interactive)
(let ((paragraphs (if (use-region-p)
(count-paragraphs (region-beginning) (region-end))
(count-paragraphs (point-min) (point-max)))))
(message "%s has %d paragraph%s"
(if (use-region-p) "Region" "Buffer")
paragraphs
(if (> paragraphs 1) "s" ""))))
Thank you very much! This is well-written Elisp and I appreciate the explanation at the beginning. It does what I want quite well, and it has both an interactive & non-interactive version to boot! I think I will make good use out of this in the future. Thanks again!
â GDP2
13 mins ago
add a comment |Â
up vote
2
down vote
Does this do the right thing?
(count-matches paragraph-start)
Unfortunately, it doesn't work for paragraphs which are split by newlines, such as withfill-paragraph
. You can see the flaw in a Help buffer or when composing an email in Message mode. The paragraphs in those buffers are often not just one line, for formatting's sake, and so there are many false positives. But I'll give you an upvote for simplicity! Thanks anyway!
â GDP2
10 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Here is how the builtin count-lines
works:
(- (buffer-size) (forward-line (buffer-size)))
The following changes forward-line
to forward-paragraph
to implement the function.
(defun count-paragraphs (start end)
"Return number of paragraphs between START and END."
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(- (buffer-size) (forward-paragraph (buffer-size))))))
(defun count-paragraphs-region-or-buffer ()
"Report number of paragraphs in the region (if it's active) or the entire buffer."
(declare (interactive-only count-paragraphs))
(interactive)
(let ((paragraphs (if (use-region-p)
(count-paragraphs (region-beginning) (region-end))
(count-paragraphs (point-min) (point-max)))))
(message "%s has %d paragraph%s"
(if (use-region-p) "Region" "Buffer")
paragraphs
(if (> paragraphs 1) "s" ""))))
Thank you very much! This is well-written Elisp and I appreciate the explanation at the beginning. It does what I want quite well, and it has both an interactive & non-interactive version to boot! I think I will make good use out of this in the future. Thanks again!
â GDP2
13 mins ago
add a comment |Â
up vote
2
down vote
accepted
Here is how the builtin count-lines
works:
(- (buffer-size) (forward-line (buffer-size)))
The following changes forward-line
to forward-paragraph
to implement the function.
(defun count-paragraphs (start end)
"Return number of paragraphs between START and END."
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(- (buffer-size) (forward-paragraph (buffer-size))))))
(defun count-paragraphs-region-or-buffer ()
"Report number of paragraphs in the region (if it's active) or the entire buffer."
(declare (interactive-only count-paragraphs))
(interactive)
(let ((paragraphs (if (use-region-p)
(count-paragraphs (region-beginning) (region-end))
(count-paragraphs (point-min) (point-max)))))
(message "%s has %d paragraph%s"
(if (use-region-p) "Region" "Buffer")
paragraphs
(if (> paragraphs 1) "s" ""))))
Thank you very much! This is well-written Elisp and I appreciate the explanation at the beginning. It does what I want quite well, and it has both an interactive & non-interactive version to boot! I think I will make good use out of this in the future. Thanks again!
â GDP2
13 mins ago
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Here is how the builtin count-lines
works:
(- (buffer-size) (forward-line (buffer-size)))
The following changes forward-line
to forward-paragraph
to implement the function.
(defun count-paragraphs (start end)
"Return number of paragraphs between START and END."
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(- (buffer-size) (forward-paragraph (buffer-size))))))
(defun count-paragraphs-region-or-buffer ()
"Report number of paragraphs in the region (if it's active) or the entire buffer."
(declare (interactive-only count-paragraphs))
(interactive)
(let ((paragraphs (if (use-region-p)
(count-paragraphs (region-beginning) (region-end))
(count-paragraphs (point-min) (point-max)))))
(message "%s has %d paragraph%s"
(if (use-region-p) "Region" "Buffer")
paragraphs
(if (> paragraphs 1) "s" ""))))
Here is how the builtin count-lines
works:
(- (buffer-size) (forward-line (buffer-size)))
The following changes forward-line
to forward-paragraph
to implement the function.
(defun count-paragraphs (start end)
"Return number of paragraphs between START and END."
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(- (buffer-size) (forward-paragraph (buffer-size))))))
(defun count-paragraphs-region-or-buffer ()
"Report number of paragraphs in the region (if it's active) or the entire buffer."
(declare (interactive-only count-paragraphs))
(interactive)
(let ((paragraphs (if (use-region-p)
(count-paragraphs (region-beginning) (region-end))
(count-paragraphs (point-min) (point-max)))))
(message "%s has %d paragraph%s"
(if (use-region-p) "Region" "Buffer")
paragraphs
(if (> paragraphs 1) "s" ""))))
answered 5 hours ago
xuchunyang
7,6141825
7,6141825
Thank you very much! This is well-written Elisp and I appreciate the explanation at the beginning. It does what I want quite well, and it has both an interactive & non-interactive version to boot! I think I will make good use out of this in the future. Thanks again!
â GDP2
13 mins ago
add a comment |Â
Thank you very much! This is well-written Elisp and I appreciate the explanation at the beginning. It does what I want quite well, and it has both an interactive & non-interactive version to boot! I think I will make good use out of this in the future. Thanks again!
â GDP2
13 mins ago
Thank you very much! This is well-written Elisp and I appreciate the explanation at the beginning. It does what I want quite well, and it has both an interactive & non-interactive version to boot! I think I will make good use out of this in the future. Thanks again!
â GDP2
13 mins ago
Thank you very much! This is well-written Elisp and I appreciate the explanation at the beginning. It does what I want quite well, and it has both an interactive & non-interactive version to boot! I think I will make good use out of this in the future. Thanks again!
â GDP2
13 mins ago
add a comment |Â
up vote
2
down vote
Does this do the right thing?
(count-matches paragraph-start)
Unfortunately, it doesn't work for paragraphs which are split by newlines, such as withfill-paragraph
. You can see the flaw in a Help buffer or when composing an email in Message mode. The paragraphs in those buffers are often not just one line, for formatting's sake, and so there are many false positives. But I'll give you an upvote for simplicity! Thanks anyway!
â GDP2
10 mins ago
add a comment |Â
up vote
2
down vote
Does this do the right thing?
(count-matches paragraph-start)
Unfortunately, it doesn't work for paragraphs which are split by newlines, such as withfill-paragraph
. You can see the flaw in a Help buffer or when composing an email in Message mode. The paragraphs in those buffers are often not just one line, for formatting's sake, and so there are many false positives. But I'll give you an upvote for simplicity! Thanks anyway!
â GDP2
10 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Does this do the right thing?
(count-matches paragraph-start)
Does this do the right thing?
(count-matches paragraph-start)
answered 5 hours ago
John Kitchin
5,2101520
5,2101520
Unfortunately, it doesn't work for paragraphs which are split by newlines, such as withfill-paragraph
. You can see the flaw in a Help buffer or when composing an email in Message mode. The paragraphs in those buffers are often not just one line, for formatting's sake, and so there are many false positives. But I'll give you an upvote for simplicity! Thanks anyway!
â GDP2
10 mins ago
add a comment |Â
Unfortunately, it doesn't work for paragraphs which are split by newlines, such as withfill-paragraph
. You can see the flaw in a Help buffer or when composing an email in Message mode. The paragraphs in those buffers are often not just one line, for formatting's sake, and so there are many false positives. But I'll give you an upvote for simplicity! Thanks anyway!
â GDP2
10 mins ago
Unfortunately, it doesn't work for paragraphs which are split by newlines, such as with
fill-paragraph
. You can see the flaw in a Help buffer or when composing an email in Message mode. The paragraphs in those buffers are often not just one line, for formatting's sake, and so there are many false positives. But I'll give you an upvote for simplicity! Thanks anyway!â GDP2
10 mins ago
Unfortunately, it doesn't work for paragraphs which are split by newlines, such as with
fill-paragraph
. You can see the flaw in a Help buffer or when composing an email in Message mode. The paragraphs in those buffers are often not just one line, for formatting's sake, and so there are many false positives. But I'll give you an upvote for simplicity! Thanks anyway!â GDP2
10 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2femacs.stackexchange.com%2fquestions%2f45584%2fcount-the-number-of-paragraphs%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password