/****************************** Module Header ******************************* * * Module Name: sortepm.e * * Copyright (c) Netlabs EPM Distribution Project 2002 * * $Id$ * * =========================================================================== * * This file is part of the Netlabs EPM Distribution package and is free * software. You can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software * Foundation, in version 2 as it comes in the "COPYING" file of the * Netlabs EPM Distribution. This library is distributed in the hope that it * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * ****************************************************************************/ ; --------------------------------------------------------------------------- ; Different from the original version, the global var rc is now used and no ; value returned. defproc Sort( firstline, lastline, firstcol, lastcol, fid) Opt = upcase( arg( 6)) Flags = (1 * (verify( Opt, 'RD', 'M') > 0)) bitor -- Reverse = Descending (2 * (verify( Opt, 'I', 'M') > 0)) bitor -- case Insensitive (4 * (verify( Opt, 'C', 'M') > 0)) -- Collating order rc = dynalink32( E_DLL, 'EtkSort', GethWndC( 5) || atol( fid) || atol( firstline) || atol( lastline) || atol( firstcol) || atol( lastcol) || atol( Flags), 2) return ; --------------------------------------------------------------------------- defc Sort if browse() then 'SayError 'BROWSE_IS__MSG ON__MSG return endif getfileid fid TypeMark = marktype() if TypeMark = '' then -- if no mark, default to entire file firstline = 1 lastline = .last firstcol = 1 lastcol = 40 else getmark firstline, lastline, firstcol, lastcol, fid if TypeMark = 'CHAR' then -- Change to line mark if lastcol = 0 then lastLine = lastLine - 1 endif firstcol = 1 lastcol = MAXCOL call pSet_Mark( firstline, lastline, firstcol, lastcol, 'LINE', fid) endif call NextCmdAltersText() -- Sort always works on lines. -- Block mark is possible, but the entire lines are sorted. The sort -- order is controlled by the marked area. if TypeMark = 'LINE' then -- If it was a line mark, the LastCol value can be 1599. Can't -- imagine anyone needing a key longer than 40. lastcol = 40 endif if lastcol = 0 then lastline = lastline - 1 getline line, lastline line = translate( line, ' ', \9) -- handle tabs correctly lastcol = length( strip( line, 't')) + 1 endif endif if fid.readonly then 'SayError 'READ_ONLY__MSG return endif 'SayError 'SORTING__MSG lastline - firstline + 1 LINES__MSG'...' SortOptions = upcase( arg( 1)) -- R = D | I | C -- PostMe required if char mark was changed to line mark 'PostMe Sort2' firstline lastline firstcol lastcol fid SortOptions defc Sort2 parse value arg( 1) with firstline lastline firstcol lastcol fid SortOptions . -- Bug in EtkSort? -- Undo to previous states doesn't work after defproc sort. -- defc treesort uses defproc sort as well and it correctly creates a new -- undo state, after the file was sorted once. if fid = '' then 'SayError Error: No file id for the source file submitted.' return endif sortfirstline = firstline sortlastline = lastline sortfid = fid tempfid = '' do once = 1 to 1 -- Workaround only needed if not a virtual and hidden file if leftstr( fid.filename, 1) = '.' & fid.visible = 0 then leave endif -- Begin workaround call pSave_Pos( save_pos) call pSave_Mark( save_mark) -- Create a temp. file 'xcom e /c /q .tempfile' if rc <> -282 then -- sayerror('New file') 'SayError 'ERROR__MSG rc BAD_TMP_FILE__MSG sayerrortext( rc)'.' return endif getfileid tempfid .autosave = 0 .visible = 0 browse_mode = browse() -- query current state if browse_mode then call browse( 0) endif -- Delete first empty line deleteline activatefile fid unmark firstline markline lastline markline activatefile tempfid call pCopy_Mark() sortfirstline = 1 sortlastline = .last sortfid = tempfid -- End workaround enddo call Sort( sortfirstline, sortlastline, firstcol, lastcol, sortfid, SortOptions) if rc then 'SayError Error. Sort returned with rc = 'rc'.' else sayerror 0 -- Begin workaround if tempfid <> '' then -- Replace previously marked lines of fid with the lines of .tempfile activatefile fid unmark call pSet_Mark( firstline, lastline, 1, MAXCOL, 'LINE', fid) .line = firstline - 1 .col = 1 deletemark activatefile tempfid unmark .line = 1 markline .last markline activatefile fid -- Add line if mark is at the end of file fLineInserted = 0 if firstline > .last then insertline '', .last + 1 fLineInserted = 1 endif .line = firstline -- Copy sorted lines to current file at cursor pos. call pCopy_Mark() -- Delete added line (now after the mark) if fLineInserted then deleteline lastline + 1 endif endif -- End workaround endif -- Begin workaround if tempfid <> '' then -- Quit temp file activatefile tempfid .modify = 0 'xcom quit' activatefile fid call pRestore_Pos( save_pos) call pRestore_Mark( save_mark) endif -- End workaround ; --------------------------------------------------------------------------- ; To sort a new-format directory listing by date & time, enter the command: ; sortcols 11 15 16 16 1 5 7 8 ; hh:mm a/p mm/dd yy ; For an old-format directory listing, enter: ; sortcols 34 38 39 39 23 28 30 31 ; hh:mm a/p mm/dd yy defc SortCols if browse() then 'SayError 'BROWSE_IS__MSG ON__MSG'.' return endif TypeMark = marktype() if TypeMark = '' then -- if no mark, default to entire file getfileid fid firstline = 1 lastline = .last else getmark firstline, lastline, firstcol, lastcol, fid endif if fid.readonly then 'SayError 'READ_ONLY__MSG'.' return endif cols = arg( 1) sort_flags = '' do while cols <> '' if not IsNum( word( cols, 1)) then parse value cols with sort_flags c1 c2 cols else parse value cols with c1 c2 cols endif if not IsNum( c1) or not IsNum( c2) then 'SayError -336' stop endif 'SayError 'SORTING__MSG lastline - firstline + 1 LINES__MSG '('c1 '-' c2') ...' -- Pass the sort switches "rc", if any, as a sixth argument to sort(). call NextCmdAltersText() call Sort( firstline, lastline, c1, c2, fid, sort_flags) if rc then 'SayError Error. Sort returned with rc = 'rc stop endif enddo sayerror 0