def filter(sMode, sQuery, rawresults):
    searchutil.checkSupportedString(sQuery)
    arResults = []
    
    # tag each hit as a type
    txt = rawresults.replace('\r\n','\n')
    for line in txt.split('\n'):
        if line.startswith('error:') or line.startswith('warning:'):
            print line
        elif line.count('\\')==0:
            print '(no filepath found?)', line
        else:
            path, num, context = pullapartline(line)
            context = searchutil.killcomments(context)
            ret = determineResultType(sQuery, context, path)
            if ret:
                arResults.append((ret, line, path))
    
    arResults = filterEliminateWhereTrue(arResults, lambda item: item[0]==None)
    if sMode==Searchmodes.SEARCH_DEFNS or sMode==Searchmodes.SEARCH_IMPLS:
        arResults = filterEliminateWhereTrue(arResults, lambda item: item[0]=='OTHER_HIT')
        if sMode==Searchmodes.SEARCH_DEFNS:
            mult = -1
            # kill constructor impl
            arResults = filterEliminateWhereTrue(arResults, lambda item: item[0]=='CONSTR_IMPL')
            # if it could be either, but is in a .c, don't show it.
            arResults = filterEliminateWhereTrue(arResults, lambda item: item[0]=='METHOD_EITHER' and not searchutil.isHeader(item[2]))
            
        elif sMode==Searchmodes.SEARCH_IMPLS:
            mult = 1
            # kill methods that end with ');'
            arResults = filterEliminateWhereTrue(arResults, lambda item: 
                item[0]=='METHOD_EITHER' and (item[1].rstrip().endswith(');') or item[1].rstrip().endswith(') ;')))
        
        # sort by headers first if that, else other.
        arResults.sort(key=lambda item: mult if searchutil.isHeader(item[2]) else 0)
        
        # other ideas:
        # if looking for an implementation and there's one without an extern, show that one?
        # ideally, if there's a paired method+impl, only show the right one...
        
    elif sMode==Searchmodes.ALL_EXCEPT_COMMENTS:
        pass
        
    else:
        raise SipException("unknown mode")
        
    return arResults
def searchUnindexedMain(sRelativeDir):
    print 'searching source-code filetypes in %s.'%sRelativeDir
    sQuery = getQuery()
    sPath = ScApp.GetFilePath()
    searchutil.checkSupportedString(sQuery)
    inidir = os.path.join(ScApp.GetSciteDirectory(), 'plugins', 'plugin_search')
    sTxtCfg = createCfgTemporary(sPath, sRelativeDir)
    fcfg=open(os.path.join(inidir, 'db', 'ssip.cfg'), 'w')
    sipname = os.path.join(inidir, 'db', 'ssip.exe')
    fcfg.write(sTxtCfg)
    fcfg.close()
    assert 'dbpath=' not in sTxtCfg
    
    os.chdir(os.path.join(inidir, 'db')) # ssip.exe will see that config file
    rawtext = searchutil.runReturnStdout(sipname, ['-noindex', sQuery])
    print rawtext
def searchIndexedMain(sModeIn):
    if sModeIn=='impl': sMode = searchsipimpl.Searchmodes.SEARCH_IMPLS
    elif sModeIn=='defn': sMode = searchsipimpl.Searchmodes.SEARCH_DEFNS
    elif sModeIn=='all': sMode = searchsipimpl.Searchmodes.ALL_EXCEPT_COMMENTS
    else: assert False
    sPath = ScApp.GetFilePath()
    inidir = os.path.join(ScApp.GetSciteDirectory(), 'plugins', 'plugin_search')
    ininame = os.path.join(inidir, 'projects.cfg')
    ret = searchutil.getprojsection(ininame, sPath)
    if not ret:
        if not os.path.exists(ininame): f=open(ininame,'w'); f.write(' '); f.close()
        n = scmsg.getChoiceShowDialog('This file is not part of a project. Add it to a new project?', ['Yes', 'No'] )
        if n!=0: return
        pos1 = 0
        if n==0:
            folder, leaf = os.path.split(sPath)
            f=open(ininame,'a');
            pos1 = f.tell()
            print 'pos = ',pos1
            f.write('\n\n[project_new_%d]\nsrcdir1=%s\n\n'%(sum(map(ord, sPath)), folder))
            f.close()
        ScApp.OpenFile(ininame)
        if pos1: ScEditor.Select(pos1, pos1+20)
        return
    
    sProjname, sTxtCfg = ret
    sQuery = getQuery()
    searchutil.checkSupportedString(sQuery)
    sipname = os.path.join(inidir, 'db', 'ssip.exe')
    fcfg=open(os.path.join(inidir, 'db', 'ssip.cfg'), 'w')
    fcfg.write(sTxtCfg)
    assert 'dbpath=' not in sTxtCfg
    assert len(sProjname)>0 and '\n' not in sProjname
    fcfg.write('\ndbpath=%s\n'%os.path.join(inidir, 'db', sProjname+'.db'))
    fcfg.close()
    
    os.chdir(os.path.join(inidir, 'db')) # ssip.exe will see that config file
    rawtext = searchutil.runReturnStdout(sipname, ['-s', sQuery])
    results = searchsipimpl.filter(sMode, sQuery, rawtext)
    # todo: if there is only one result, consider opening it automatically
    searchsipimpl.displayResults(results)