help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Renumbering paragraphs automatically


From: Benjamin Rutt
Subject: Re: Renumbering paragraphs automatically
Date: Tue, 24 Feb 2004 16:22:37 -0500
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.3.50 (usg-unix-v)

Timur Aydin <ta@taydin.org> writes:

> When writing an itemized list as follows:
>
> 1) Sentences in first paragraph.
>
> 2) The second paragraph.
>
> 3) The third paragraph.
>
> Can emacs help me regenerate the paragraph numbers when I
> insert/delete items or if I move items around?

I use outline-mode directly to build lists like this for maximum
flexibility.  When I want to produce numbered output for laymen to
read, I use the following shell script:

# filename: indentN.awk
#   author: Eric Pement - pemente@northpark.edu
#     date: 02/14/2001
#
#  purpose: awk script to modify files created and saved in GNU Emacs
#           "outline-mode" to a numbered, indented output format. E.g.,
#
#  INPUT FILE        OUTPUT FILE
#  ==============    ===========================            
#  * Line 1        | 1. Line 1
#  ** Line 2       |   1.1. Line 2
#  *** Line 3      |     1.1.1. Line 3
#  *** Line 4      |     1.1.2. Line 4
#  **** Line 5     |       1.1.2.1. Line 5
#  ***** Line 6    |         1.1.2.1.1. Line 6
#  ***** Line 7    |         1.1.2.1.2. Line 7
#  ** Line 8       |   1.2. Line 8
#  * Line 9        | 2. Line 9
#  ** Line 10      |   2.1. Line 10
#
# Note: this script can handle any number of asterisks on a line. 
# Lines of plain text (no asterisks) in source file are NOT indented.
/^\*/ {

  this_len = match($0,/\*[^*]/);  # get number of stars in 1st field
  array[this_len]++;              # increment index of current leaf

  if ( this_len < last_len ) {    # if we have moved up a branch...
     for (i = this_len + 1; i <= last_len; i++)
        array[i] = 0;             # .. reset the leaves below us
  }

  for (j=1; j <= this_len; j++) {   # build up the prefix string
     if   (j == 1)   prefix = array[j] "."
     else            prefix = "  " prefix array[j] "."
  }

  sub(/^\*+/, prefix)
  last_len = this_len
  prefix = ""
}
{ print }
# --- end of script ---

which I call from emacs using:

(defun my-outline-produce-numbered ()
  (interactive)
  (assert (string-match "\\.otl$" (buffer-file-name)))
  (let ((s (shell-command-to-string
            (format "nawk -f ~/bin/indentN.awk < %s" (buffer-file-name)))))
    (find-file (my-re-replace-in-string "otl$" "txt" (buffer-file-name)))
    (erase-buffer)
    (insert s)
    (goto-char (point-min))))
-- 
Benjamin


reply via email to

[Prev in Thread] Current Thread [Next in Thread]