* * SORTBLK.KEX * * Sort groups of contiguous lines in a file and use the first line of each * group to determine the sort order. Sorts are case insensitive. This is * useful for sorting this like bibliography listings. * * The macro gathers a copy of all of the first lines of each block, puts them * in a temporary file and sorts these. It then steps through the sorted file * and uses it as an index to determine the proper order for the original file * containing the blocks. * * Note: If you have any blocks of text that have identical first lines then * these will not be sorted correctly. The macro assumes that each block of * text has a unique first line. * * Works with: * * KEDIT for Windows 1.5 * KEDIT 5.0 for DOS * KEDIT 5.0 for OS/2 * * Original author: Kent Downs MSG 8/96 * "preserve" "set autosave off" "set wrap off" "set zone 1 *" BlockFile = fileid.1() TempFile = "zzzzzzzz.zzz" "kedit" TempFile "(new noprofile" "kedit" BlockFile * Go through the file, locate the first line of each paragraph and copy these * lines to a scratch file. "top" do forever * Find the first line of the next block "nomsg locate ~blank" if rc > 0 then leave * Get a copy of it line = curline.3() * Place this in the scratch file "kedit" TempFile "input" line * Skip to the end of the current block "kedit" BlockFile "nomsg locate blank" if rc > 0 then leave end * Sort the lines in the scratch file to make a sort of index of the blocks "kedit" TempFile "set case mixed ignore" "sort all" ":1" InsertPos = 0 SortCount = 0 * Reposition the blocks to their correct sort order do forever * Get a copy of the next sorted item from the index SearchString = curline.3() * Find it in the block file "kedit" BlockFile "top" "find" SearchString "mark line" * Mark a line block around this block including all blank lines below it, * up to the next block "nomsg locate blank" if rc > 0 then do * The last block in the original file may not have a blank line after it "bottom" "add 1" end * Move to the start of the next block (if any) and back up one line "nomsg locate ~blank" if rc \= 0 then "*" else "backward 1 line" * Complete the block "mark line" * Move to the last line of the previously sorted block ":"InsertPos "move block" SortCount = SortCount + 1 * Remember where to place the next block "sos blockend" InsertPos = line.1() "reset block" "kedit" TempFile "down 1" * Are we done? if focuseof() then leave end * Throw away the index file "kedit" TempFile "qquit" "kedit" BlockFile "top" "restore" "sos beep" say SortCount "text blocks sorted..."