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

python and vim questions

Expand Messages
  • Aaron
    Hi all, I am subscribed to lilypond developers list and have been asked to forward some questions to the vim users list. First a little background Lilypond is
    Message 1 of 3 , Aug 2, 2003
    • 0 Attachment
      Hi all,
      I am subscribed to lilypond developers list and have been asked to
      forward some questions to the vim users list.

      First a little background Lilypond is a ascii music notation system. The
      two editors with modes for it are emacs and vim.

      some programers developed a way to link each 'note' entered in emacs to
      midi playback so that as you type you can hear each note. They also made
      a way so that during regular playback the cursor will scan the notes
      karoke style.

      This has been done with python and emacs so far. They would like to do
      this also for VIM as well but they are both emacs people and I am pretty
      much a newbie to vim.

      So here are the questions:
      1.

      " Suppose one has to type something like (I do not know `vim', so this
      is only
      approximative syntax):

      23,30:py lilypond.vim_play_region()

      for playing lines 23 through 30. This is very tedious to write, in my
      opinion, especially if used often. We need something shorter. One
      possibility would be to make an abbreviation in Python space once, like:

      :py r = lilypond.vim_play_region

      and then use:

      23,30:py r()

      which is better already, from user standpoint. However, it would be
      nicer
      to bind even simpler keys, if there is a way in `vim' for this. One
      could
      bind <F7> (the function key) to `:py lilypond.vim_play_region()' and
      then
      merely use:

      23,30<F7>

      So <F7> hides the real details of the `:py' command. My question is
      about
      how to do this with `vim', if possible."
      -------------------------
      2. "
      > > According to the `vim' documentation, it should be equally easy to
      > > implement for `vim', _given_ there is some way to force an immediate
      > > `vim' redisplay.

      > What do you mean by an immediate vim redisplay???

      If one calls a Python function from `vim' for playing a dozen notes, the
      function returns to `vim' once all twelve notes have been played. There
      is
      a Python statement, supported by the `vim'-Python interface, for
      changing
      the cursor position. We could use this function for positioning the
      cursor
      over the note being played, in real time. However, for most editors,
      and I
      would presume `vim' is no exception, the actual change of display
      appearance, including the location of the cursor, is delayed until the
      editor becomes idle waiting for user input, and all display operations
      are
      suspended in meantime for not slowing down the actual computation which
      is
      taking place. I would presume that, while Python is in control and
      playing
      notes, `vim' considers that the editor is _not_ idle.

      I wonder if there is a way, from within Python, to ask `vim' to perform
      immediate redisplay, that is, to get the display and cursor position to
      become synchronised right now, not waiting until are notes have been
      played.
      If we cannot defeat the `vim' probable optimisation to position the
      cursor
      only once to the final position after the whole Python command, we will
      completely miss the karaoke effect you suggested."
      ------------------------------------------
      These are quotes from their emails if further clarifaction is needed
      please let me know.

      Thanks
      Aaron
    • Srinath Avadhanula
      ... mapping to any key combination is easy and is a very basic question indeed. I would suggest reading up ... to get a feel for script writing. There are also
      Message 2 of 3 , Aug 2, 2003
      • 0 Attachment
        On Sat, 3 Aug 2003, Aaron wrote:

        > which is better already, from user standpoint. However, it would be
        > nicer to bind even simpler keys, if there is a way in `vim' for this.
        > One could bind <F7> (the function key) to `:py
        > lilypond.vim_play_region()' and then merely use:
        >
        > 23,30<F7>
        >
        > So <F7> hides the real details of the `:py' command. My question is
        > about how to do this with `vim', if possible."

        mapping to any key combination is easy and is a very basic question
        indeed. I would suggest reading up

        :help vim-script-intro

        to get a feel for script writing. There are also numerous very
        comprehensive scripts at vim.sf.net which should serve as good examples
        to do what you are seeking.

        > I wonder if there is a way, from within Python, to ask `vim' to
        > perform immediate redisplay, that is, to get the display and cursor
        > position to become synchronised right now, not waiting until are notes
        > have been played. If we cannot defeat the `vim' probable optimisation
        > to position the cursor only once to the final position after the whole
        > Python command, we will completely miss the karaoke effect you
        > suggested."
        >
        Also, to force a redraw use the :redraw command.

        A template:

        nmap <F7> :call MyPythonWrapper()

        function! MyPythonWrapper() range
        let line = a:firstline
        while line < a:lastline
        python playLine(line)
        endwhile
        endfunction

        python << EOF
        import vim
        import lilypond
        def playLine(line):
        string = vim.eval('getline("%d")' % line)
        for i in range(0,len(string)):
        lilypond.play(i)
        vim.eval('cursor(%d, %d)' % (line, i))
        # :help :redraw
        vim.command('redraw!')
        EOF

        HTH
        Srinath
      • Mikolaj Machowski
        ... IMO more verbose is better. Give command like lilypond.vim_play_region(). Each user can easily cmap it to his/her favorite mapping without conflicts with
        Message 3 of 3 , Aug 2, 2003
        • 0 Attachment
          On Sun, Aug 03, 2003 at 02:59:39AM +0300, Aaron wrote:
          > 1.
          > " Suppose one has to type something like (I do not know `vim', so this
          > is only
          > approximative syntax):
          > 23,30:py lilypond.vim_play_region()
          > for playing lines 23 through 30. This is very tedious to write, in my
          > opinion, especially if used often. We need something shorter. One
          > possibility would be to make an abbreviation in Python space once, like:
          > :py r = lilypond.vim_play_region
          > and then use:
          > 23,30:py r()
          > which is better already, from user standpoint. However, it would be
          > nicer
          > to bind even simpler keys, if there is a way in `vim' for this. One
          > could
          > bind <F7> (the function key) to `:py lilypond.vim_play_region()' and
          > then
          > merely use:
          > 23,30<F7>
          > So <F7> hides the real details of the `:py' command. My question is
          > about
          > how to do this with `vim', if possible."

          IMO more verbose is better.
          Give command like lilypond.vim_play_region(). Each user can easily cmap
          it to his/her favorite mapping without conflicts with other mappings. If
          you really want give mappings make it:
          1. on vim side for easier modification (no :py r = lilypond... attitude)
          2. provide it through <Plug> and hasmapto() mechanisms.

          Also you can make :commands. Thanks to command-line completion inserting
          of even long names is fast and easy.

          m.
          --
          LaTeX + Vim = http://vim-latex.sourceforge.net/
          Vim-list(s) Users Map: (last change 27 May)
          http://skawina.eu.org/mikolaj/vimlist
          Are You There?
        Your message has been successfully submitted and would be delivered to recipients shortly.