Vim : how to index a plain text file?
Asked Answered
M

3

5

Is it possible to index a plain text file (a book) in vim such as :

1. This line contains the words : London, Berlin, Paris
2. In this line, I write about : New-York, London, Berlin
...
100. And, to conclude, my last comments about : New-York, Paris

and have this resulting index :

Berlin : 1
London : 1, 2
New-York : 2, ..., 100
Paris : 1, ..., 100

and, if it is possible, what is the tagging method ? I have read about ctags, but it seems to be dedicated to specific languages (and to say the truth, a bit overkill for my needs)

Marentic answered 3/5, 2011 at 13:13 Comment(0)
A
2

I took the liberty of writing the following function, based on using the :g/STRING/# command to get the matches. I read the results of this command into a list, and then process it to return a list of matching line numbers:

function! IndexByWord( this_word )
    redir => result
    sil! exe ':g/' . a:this_word . '/#'
    redir END
    let tmp_list = split(strtrans(result),"\\^\@ *")
    let res_list = []
    call map(tmp_list, 'add(res_list,matchstr(v:val,"^[0-9]*"))')
    let res = a:this_word . ' : ' . string(res_list)
    let res = substitute(res, "[\\[\\]\\']", "", "g")
    echo res
endfunction

So you could call this function on all the words you wish (or write a script to do so) and direct the output to a file. Not very elegant, perhaps, but nicely self-contained.

Hope this helps, rather than hinders.

Aetna answered 3/5, 2011 at 14:31 Comment(2)
superb, it worked perfectly, but only for one word (e.g. : :call IndexByWord("Berlin"). How must I proceed to have it work for an array of words (e.g. : :call IndexByWord("Berlin", "London", "New-York", "Paris") ? And what could be the size limit of this array ?Marentic
@ThG: I took my own liberty of adding a separate post revising Prince G's function to do what you're asking here. (Of course, Prince G is the one whose post should be your "answer"; my revisions were small.)Irvin
I
2

Here is a revised version of the function posted by Prince Goulash. This version takes a list of words as input and returns a formatted and alphabetized string of the result:

function! IndexByWord( wordlist )
    let temp_dict = {}
    for word in a:wordlist
        redir => result
        sil! exe ':g/' . word . '/#'
        redir END
        let tmp_list = split(strtrans(result),"\\^\@ *")
        let res_list = []
        call map(tmp_list, 'add(res_list,str2nr(matchstr(v:val,"^[0-9]*")))')
        let temp_dict[word]  = res_list
    endfor
    let result_list = []
    for key in sort(keys(temp_dict))
        call add(result_list, key . ' : ' . string(temp_dict[key])[1:-2])
    endfor
    return join(result_list, "\n")
endfunction

One way to call it would be:

echo IndexByWord(['word1', 'word2', 'word3', etc])

There should be no problem with having a long list of words, although in that case you would probably want to use a list variable and getting the results would of course take more time. For example:

let my_word_list = ['word1', 'word2', . . . 'word1000']
echo IndexByWord(my_word_list)
Irvin answered 3/5, 2011 at 19:17 Comment(3)
it worked ! Thanks a lot to you and Prince Goulash. BTW, I can thus have many indexes for 1 text : one for names (IndexByNames), one for topics (IndexByTopics), etc... bundle them in a script and append them at the end of the text... Thanks againMarentic
Thanks for making these modifications - I'm glad you were able to follow my code!Aetna
and @Prince Goulash : it seems I bragged above my competences : how can I redirect the results of IndexByWord at the end of the file which is indexed ? (like in a real book, with a real index). Thanks in advanceMarentic
T
0

Have a look at ptx, perhaps

:%!cut -d: -f2 | ptx -Ar

Will output something like this, when unmodified:

:1:                         London,   Berlin, Paris
:2:               New-York, London,   Berlin
:1:                                   London, Berlin, Paris
:2:                       New-York,   London, Berlin
:2:                                   New-York, London, Berlin
:4:                                   New-York, Paris
:1:                 London, Berlin,   Paris
:4:                       New-York,   Paris
:2:                            New-   York, London, Berlin
:4:                            New-   York, Paris

I'll see if I can the rest of the steps too

Tribadism answered 3/5, 2011 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.