Count the number of paragraphs

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question

























    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.










    share|improve this question























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 6 hours ago









      GDP2

      702218




      702218




















          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" ""))))





          share|improve this answer




















          • 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
            12 mins ago

















          up vote
          2
          down vote













          Does this do the right thing?



          (count-matches paragraph-start)





          share|improve this answer




















          • 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
            9 mins ago










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "583"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          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" ""))))





          share|improve this answer




















          • 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
            12 mins ago














          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" ""))))





          share|improve this answer




















          • 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
            12 mins ago












          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" ""))))





          share|improve this answer












          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" ""))))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
            12 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
            12 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
          12 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
          12 mins ago










          up vote
          2
          down vote













          Does this do the right thing?



          (count-matches paragraph-start)





          share|improve this answer




















          • 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
            9 mins ago














          up vote
          2
          down vote













          Does this do the right thing?



          (count-matches paragraph-start)





          share|improve this answer




















          • 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
            9 mins ago












          up vote
          2
          down vote










          up vote
          2
          down vote









          Does this do the right thing?



          (count-matches paragraph-start)





          share|improve this answer












          Does this do the right thing?



          (count-matches paragraph-start)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 5 hours ago









          John Kitchin

          5,2101520




          5,2101520











          • 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
            9 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
            9 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
          9 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
          9 mins ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?