Loading ...
Sorry, an error occurred while loading the content.

Re: Tabulerizations :)

Expand Messages
  • Christian Brabandt
    ... Interesting problem. Here is my proposal: ,---- ... `---- This seems to work with lowercase and uppercase letters in the range of [a-z] without umlauts or
    Message 1 of 7 , Jun 30, 2009
    • 0 Attachment
      On Wed, July 1, 2009 5:11 am, meino.cramer@... wrote:
      > Charlie Kester <corky1951@...> [09-07-01 04:50]:
      >> On Tue 30 Jun 2009 at 19:31:41 PDT meino.cramer@... wrote:
      >> >I want to make from this input (example):
      >> >
      >> >a d f g h
      >> >b c d e f
      >> >g h i
      >> >a i
      >> >b f g j
      >> >
      >> >this output
      >> >
      >> >
      >> >a d f g h
      >> > b c d e f
      >> > g h i
      >> >a i
      >> > b f g j
      >> >
      >> >
      >> >is there any vim script, which does this for me?
      >> >I am working under Linux and vim (console).

      Interesting problem. Here is my proposal:
      ,----
      | fu! Position(char)
      | " lowercase letters
      | if ((char2nr(a:char) >= 97) && (char2nr(a:char) <= 122))
      | return (char2nr(a:char)-97)*2
      | " uppercase letters
      | elseif ((char2nr(a:char) >= 65) && (char2nr(a:char) <= 90))
      | return (char2nr(a:char)-65)*2
      | endif
      | return -1
      | endfu
      |
      | fu! Tab()
      | let i=repeat(' ', 51)
      | let mylist=split(i,'\zs')
      | let j=split(getline('.'), '\s')
      | for char in j
      | let pos=Position(char)
      | if pos<0
      | call add(mylist, char)
      | else
      | let mylist[pos]=char
      | endif
      | endfor
      | let i=join(mylist, '')
      | let i=substitute(i,'\s\+$','','')
      | call setline('.', i)
      | endfunction
      | com! -range Tabularize :<line1>,<line2>call Tab()
      `----

      This seems to work with lowercase and uppercase letters in the range of
      [a-z] without umlauts or special chars. To make it work, all chars need
      to be separated by whitespace as your example shows, if not the line is
      ignored. Special chars like digits or umlauts will be silently appended
      at the end.


      >> What are the constraints?
      >>
      >> Will the input lines always be pre-sorted, as in your example? I.e.,
      >> will you ever have a row like "b d a f"? If so, should the
      >> "tabulerization" sort it?

      For my proposal, the letters do not need to be presorted. The function
      will take care of it.

      regards,
      Christian
      --
      :wq!


      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Andy Wokula
      ... hey, try this one: com! -range Tabularize , call Tabularize() func! Tabularize() range abort let wdict = Tabul_GetWDict(getline(a:firstline,
      Message 2 of 7 , Jul 2, 2009
      • 0 Attachment
        meino.cramer@... schrieb:
        > Charlie Kester <corky1951@...> [09-07-01 04:50]:
        >> On Tue 30 Jun 2009 at 19:31:41 PDT meino.cramer@... wrote:
        >>>
        >>>
        >>> Hi,
        >>>
        >>> I want to make from this input (example):
        >>>
        >>> a d f g h
        >>> b c d e f
        >>> g h i
        >>> a i
        >>> b f g j
        >>>
        >>> this output
        >>>
        >>>
        >>> a d f g h
        >>> b c d e f
        >>> g h i
        >>> a i
        >>> b f g j
        >>>
        >>>
        >>> is there any vim script, which does this for me?
        >>> I am working under Linux and vim (console).
        >>>
        >> What are the constraints?
        >>
        >> Will the input lines always be pre-sorted, as in your example? I.e.,
        >> will you ever have a row like "b d a f"? If so, should the
        >> "tabulerization" sort it?
        >>
        >> Will each element always be one character wide and no wider?
        >>
        >> In your example, for each character between a and j, there is at least
        >> one row containing it. Will you need to handle cases where one or more
        >> elements in the range are not represented? I.e., should the result
        >> contain empty columns?
        >>
        >>
        >>
        > Oh ... uuuuhhh ... hrrrmm
        >
        > Yes, the input was incomplete, sorry.
        >
        > I will try to complete it:
        >
        > "a b c" stands for three words that is each character is one word.
        > Separation is done by blanks (\s) of a unknown count, anything else
        > is part of a word. Or in other words: "a=2.5 Anoth"er"(word)!=23/8:l" are
        > two words.
        > The words are presorted.
        > There are no empty columns.
        >
        > Hope, that is more complete as my first attempt...

        hey, try this one:

        com! -range Tabularize <line1>,<line2>call Tabularize()

        func! Tabularize() range abort
        let wdict = Tabul_GetWDict(getline(a:firstline, a:lastline))
        let ordered_words = Tabul_Sort(keys(wdict))
        let virtcols = Tabul_GetColumnOffsets(ordered_words)
        for lnum in range(a:firstline, a:lastline)
        let words = Tabul_GetWords(getline(lnum))
        call setline(lnum, Tabul_MkTabLine(Tabul_Sort(words), virtcols))
        endfor
        endfunc

        " return a tabularized line:
        func! Tabul_MkTabLine(ordered_words, virtcols)
        " ok a bit redundant: both ordered_words and virtcols include
        " information about order
        let words = copy(a:ordered_words)
        let nwords = len(words)
        if nwords < 2
        return nwords == 1 ? words[0] : ""
        endif
        let idx = 0
        let nextword = words[0]
        let indent = repeat(" ", a:virtcols[nextword]-1)
        while idx < nwords-1
        let word = nextword
        let nextword = words[idx+1]
        let fmt = printf("%%-%ds",
        \ a:virtcols[nextword] - a:virtcols[word])
        let words[idx] = printf(fmt, word)
        let idx += 1
        endwhile
        return indent. join(words, "")
        endfunc

        " for given list of strings: extract the words into a wdict (= set of
        " words)
        func! Tabul_GetWDict(lines)
        let wd = {}
        for line in a:lines
        for word in Tabul_GetWords(line)
        let wd[word] = 1
        endfor
        endfor
        return wd
        endfunc

        " return words ordered:
        func! Tabul_Sort(words)
        return sort(a:words)
        endfunc

        " extract the words from a string as a list
        func! Tabul_GetWords(str)
        return split(a:str)
        endfunc

        " assign virtual columns (in the window) for each word in ordered_wlist
        func! Tabul_GetColumnOffsets(ordered_wlist)
        let gap = 1 " least number of spaces between words
        let off = 1 " leftmost virtcol()
        let offsets = {}
        for word in a:ordered_wlist
        let offsets[word] = off
        let off += strlen(word) + gap
        endfor
        return offsets
        endfunc

        " vim:set tw=72 et sw=2 sts=2:




        extra functions for sorting and extracting words from the text ...
        should be quite easy to find the positions in the code where
        to make changes ...

        --
        Andy

        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • meino.cramer@gmx.de
        Hi, sorry for being offline for a week. I had to make an unplanned journey... Now I am busy in doing all that stuff, which had to wait a week and is now crying
        Message 3 of 7 , Jul 12, 2009
        • 0 Attachment
          Hi,

          sorry for being offline for a week. I had to make
          an unplanned journey...

          Now I am busy in doing all that stuff, which had
          to wait a week and is now crying "URGENT!" at me.

          Please give me some time for it -- after that I will
          respond more directly to your help.

          Have a nice sunday and thank you very much for all
          your help and response !

          VIM!
          mcc



          Andy Wokula <anwoku@...> [09-07-02 17:27]:
          >
          > meino.cramer@... schrieb:
          > > Charlie Kester <corky1951@...> [09-07-01 04:50]:
          > >> On Tue 30 Jun 2009 at 19:31:41 PDT meino.cramer@... wrote:
          > >>>
          > >>>
          > >>> Hi,
          > >>>
          > >>> I want to make from this input (example):
          > >>>
          > >>> a d f g h
          > >>> b c d e f
          > >>> g h i
          > >>> a i
          > >>> b f g j
          > >>>
          > >>> this output
          > >>>
          > >>>
          > >>> a d f g h
          > >>> b c d e f
          > >>> g h i
          > >>> a i
          > >>> b f g j
          > >>>
          > >>>
          > >>> is there any vim script, which does this for me?
          > >>> I am working under Linux and vim (console).
          > >>>
          > >> What are the constraints?
          > >>
          > >> Will the input lines always be pre-sorted, as in your example? I.e.,
          > >> will you ever have a row like "b d a f"? If so, should the
          > >> "tabulerization" sort it?
          > >>
          > >> Will each element always be one character wide and no wider?
          > >>
          > >> In your example, for each character between a and j, there is at least
          > >> one row containing it. Will you need to handle cases where one or more
          > >> elements in the range are not represented? I.e., should the result
          > >> contain empty columns?
          > >>
          > >>
          > >>
          > > Oh ... uuuuhhh ... hrrrmm
          > >
          > > Yes, the input was incomplete, sorry.
          > >
          > > I will try to complete it:
          > >
          > > "a b c" stands for three words that is each character is one word.
          > > Separation is done by blanks (\s) of a unknown count, anything else
          > > is part of a word. Or in other words: "a=2.5 Anoth"er"(word)!=23/8:l" are
          > > two words.
          > > The words are presorted.
          > > There are no empty columns.
          > >
          > > Hope, that is more complete as my first attempt...
          >
          > hey, try this one:
          >
          > com! -range Tabularize <line1>,<line2>call Tabularize()
          >
          > func! Tabularize() range abort
          > let wdict = Tabul_GetWDict(getline(a:firstline, a:lastline))
          > let ordered_words = Tabul_Sort(keys(wdict))
          > let virtcols = Tabul_GetColumnOffsets(ordered_words)
          > for lnum in range(a:firstline, a:lastline)
          > let words = Tabul_GetWords(getline(lnum))
          > call setline(lnum, Tabul_MkTabLine(Tabul_Sort(words), virtcols))
          > endfor
          > endfunc
          >
          > " return a tabularized line:
          > func! Tabul_MkTabLine(ordered_words, virtcols)
          > " ok a bit redundant: both ordered_words and virtcols include
          > " information about order
          > let words = copy(a:ordered_words)
          > let nwords = len(words)
          > if nwords < 2
          > return nwords == 1 ? words[0] : ""
          > endif
          > let idx = 0
          > let nextword = words[0]
          > let indent = repeat(" ", a:virtcols[nextword]-1)
          > while idx < nwords-1
          > let word = nextword
          > let nextword = words[idx+1]
          > let fmt = printf("%%-%ds",
          > \ a:virtcols[nextword] - a:virtcols[word])
          > let words[idx] = printf(fmt, word)
          > let idx += 1
          > endwhile
          > return indent. join(words, "")
          > endfunc
          >
          > " for given list of strings: extract the words into a wdict (= set of
          > " words)
          > func! Tabul_GetWDict(lines)
          > let wd = {}
          > for line in a:lines
          > for word in Tabul_GetWords(line)
          > let wd[word] = 1
          > endfor
          > endfor
          > return wd
          > endfunc
          >
          > " return words ordered:
          > func! Tabul_Sort(words)
          > return sort(a:words)
          > endfunc
          >
          > " extract the words from a string as a list
          > func! Tabul_GetWords(str)
          > return split(a:str)
          > endfunc
          >
          > " assign virtual columns (in the window) for each word in ordered_wlist
          > func! Tabul_GetColumnOffsets(ordered_wlist)
          > let gap = 1 " least number of spaces between words
          > let off = 1 " leftmost virtcol()
          > let offsets = {}
          > for word in a:ordered_wlist
          > let offsets[word] = off
          > let off += strlen(word) + gap
          > endfor
          > return offsets
          > endfunc
          >
          > " vim:set tw=72 et sw=2 sts=2:
          >
          >
          >
          >
          > extra functions for sorting and extracting words from the text ...
          > should be quite easy to find the positions in the code where
          > to make changes ...
          >
          > --
          > Andy
          >
          >
          --
          Please don't send me any Word- or Powerpoint-Attachments
          unless it's absolutely neccessary. - Send simply Text.
          See http://www.gnu.org/philosophy/no-word-attachments.html
          In a world without fences and walls nobody needs gates and windows.


          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • Raúl Núñez de Arenas Coronado
          Saluton Meino :) ... I suppose you re in a hurry, Meino, but please don t toppost, specially if you are going to quote the entire message you re
          Message 4 of 7 , Jul 12, 2009
          • 0 Attachment
            Saluton Meino <m...@...> :)

            <m...@...> dixit:
            > Now I am busy in doing all that stuff, which had
            > to wait a week and is now crying "URGENT!" at me.

            I suppose you're in a hurry, Meino, but please don't toppost, specially
            if you are going to quote the entire message you're replying to which is
            150 lines long ;)

            --
            Raúl "DervishD" Núñez de Arenas Coronado
            Linux Registered User 88736 | http://www.dervishd.net
            It's my PC and I'll cry if I want to... RAmen!

            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          Your message has been successfully submitted and would be delivered to recipients shortly.