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

Re: vimdiff-like highlighting within the same file?

Expand Messages
  • Charles Campbell
    ... Look into Christian Brabandt s NrrwRgn plugin, available at http://www.vim.org/scripts/script.php?script_id=3075 . Regards, Chip Campbell -- You received
    Message 1 of 28 , Jul 1, 2010
    • 0 Attachment
      Efraim Yawitz wrote:
      > I've also wanted to do diffs within the same file, and now I'm
      > thinking that this is really just a special case of something more
      > general that may have other uses (although I haven't thought of them
      > yet). The concept here is "virtual buffers" which would represent a
      > portion of a file, or more generally, a number of portions of a number
      > of files concatenated together. Once you could do something like
      >
      > :virtbuf bufname filename startline endline
      >
      > to create a virtual buffer, diffing it with another real or virtual
      > buffer would be easy.
      >
      > I'm wondering if this isn't already possible with some of the
      > vimscript commands. If not, I may look into how to do it in the C code.
      Look into Christian Brabandt's NrrwRgn plugin, available at
      http://www.vim.org/scripts/script.php?script_id=3075 .

      Regards,
      Chip Campbell

      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php
    • Adam Monsen
      FYI, I also asked this question on stackoverflow.com:
      Message 2 of 28 , Jul 12, 2010
      • 0 Attachment
        FYI, I also asked this question on stackoverflow.com:

        http://stackoverflow.com/questions/3231759/how-can-i-visualize-per-character-differences-in-a-unified-diff-file
        (same link, tiny: http://tinyurl.com/363f8he )

        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php
      • Adam Monsen
        ... Someone posted a response to that question. Apparently there s another editor that does highlight intra-line differences in a unified diff file. It s
        Message 3 of 28 , Aug 19, 2010
        • 0 Attachment
          > FYI, I also asked this question on stackoverflow.com:
          >
          > http://tinyurl.com/363f8he

          Someone posted a response to that question. Apparently there's another editor
          that does highlight intra-line differences in a unified diff file. It's
          interesting, it works... I tried it out. Having used Vim for about 10 years, I'm
          having trouble adjusting to the crazy keystrokes of that other editor.

          Sorry, just a little kidding around there when I say "other editor". Anyway, I
          brought this up because maybe that would be a useful working demo on which to
          base a Vim intra-file intra-line highlighting script.

          Seems like the major bits of functionality in such a script might be

          * confirm current buffer is a unified diff file, if so, proceed
          * identify diff lines
          * identify parts of lines that differ
          (maybe with the help of "diff" or " program?)
          * alter highlighting so intra-line differences stand out
          * bind some keystroke to jump to each difference

          I'm fairly certain Vimscript could do all this, but I'm stumped on the third
          bullet... anyone have ideas on how to do this?

          Hmm, another idea would be using Python... that might be more straightforward.

          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php
        • Charles Campbell
          ... May I suggest that you look into Christian Brabandt s NarrowRegion plugin. A procedure, after having installed NarrowRegion: * edit buffer * select lines
          Message 4 of 28 , Aug 19, 2010
          • 0 Attachment
            Adam Monsen wrote:
            >> FYI, I also asked this question on stackoverflow.com:
            >>
            >> http://tinyurl.com/363f8he
            >>
            > Someone posted a response to that question. Apparently there's another editor
            > that does highlight intra-line differences in a unified diff file. It's
            > interesting, it works... I tried it out. Having used Vim for about 10 years, I'm
            > having trouble adjusting to the crazy keystrokes of that other editor.
            >
            > Sorry, just a little kidding around there when I say "other editor". Anyway, I
            > brought this up because maybe that would be a useful working demo on which to
            > base a Vim intra-file intra-line highlighting script.
            >
            > Seems like the major bits of functionality in such a script might be
            >
            > * confirm current buffer is a unified diff file, if so, proceed
            > * identify diff lines
            > * identify parts of lines that differ
            > (maybe with the help of "diff" or " program?)
            > * alter highlighting so intra-line differences stand out
            > * bind some keystroke to jump to each difference
            >
            > I'm fairly certain Vimscript could do all this, but I'm stumped on the third
            > bullet... anyone have ideas on how to do this?
            >
            > Hmm, another idea would be using Python... that might be more straightforward.
            >
            >
            May I suggest that you look into Christian Brabandt's NarrowRegion
            plugin. A procedure, after having installed NarrowRegion:

            * edit buffer
            * select lines with V
            * :NR (this will make a "narrow region" buffer holding just those lines)
            * select other lines with V
            * :NR
            * in each narrow-region window, type :diffthis

            Regards,
            Chip Campbell

            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php
          • Adam Monsen
            ... Thanks, Chip! I have indeed tried that, and it works fine. But, IMHO, * it is cumbersome (unless scripted/mapped, but see below) * it takes the diff out of
            Message 5 of 28 , Aug 19, 2010
            • 0 Attachment
              > May I suggest that you look into Christian Brabandt's NarrowRegion plugin.
              > A procedure, after having installed NarrowRegion:
              >
              > * edit buffer
              > * select lines with V
              > * :NR (this will make a "narrow region" buffer holding just those lines)
              > * select other lines with V
              > * :NR
              > * in each narrow-region window, type :diffthis

              Thanks, Chip!

              I have indeed tried that, and it works fine.

              But, IMHO,
              * it is cumbersome (unless scripted/mapped, but see below)
              * it takes the diff out of context

              What if you have a patch with 30 hunks? Yes, I could map a small function to
              create the narrow regions while cycling through each diff, but why can't Vim
              just go ahead and highlight all intra-line differences for a whole diff file?

              --
              You received this message from the "vim_use" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php
            • Andy Wokula
              ... Attached script defines a :DiffMsgId command, which attempts to overlay diff highlighting to the current section using matchadd(). E.g. it works on the
              Message 6 of 28 , Aug 20, 2010
              • 0 Attachment
                Am 30.06.2010 11:59, schrieb Andrei Popescu:
                > On Mi, 30 iun 10, 11:12:03, Christian Brabandt wrote:
                >> On Wed, June 30, 2010 10:52 am, Andrei Popescu wrote:
                >>> I'm trying to add new features to vim's handling of .po files. How could
                >>> I highlight the differences between the current msgid and the previous
                >>> one?
                >>
                >> I don't understand your question. Can you elaborate, on what the file
                >> looks like and where the previous message id comes from? Please show a
                >> sample file, with which we can see your problem.
                >
                > What I posted was an excerpt of a .po file. Here is a full "string" with
                > comments:
                >
                > [blank line]
                > #. Type: select
                > #. Description
                > #. :sl5:
                > #: ../s390-dasd.templates:1002
                > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ other stuff, not interesting in this case
                > #, fuzzy
                > ^^^^^^^^ fuzzy flag, indicates the original (usually English) msgid
                > (string) has changed
                > #| msgid ""
                > #| "The following disk access storage devices (DASD) are available. Please "
                > #| "select each device you want to use one at a time."
                > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ old English msgid
                > msgid ""
                > "The following direct access storage devices (DASD) are available. Please"
                > "select each device you want to use one at a time."
                > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ new English msgid
                > msgstr ""
                > "Următoarele Dispozitive de stocare cu acces la disc (DASD) sunt disponibile."
                > "Vă rugăm să alegeți pe rând fiecare dispozitiv pe care doriți să-l folosiți."
                > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ translation
                > [blank line]
                >
                > "strings" are always separated by (at least) one blank line and a .po
                > file usually contains a lot of them (hundreds or more), which is why I
                > won't post a complete file. A translator must not, ever, touch the
                > "other stuff" and the current msgid.
                >
                > After editing the translation the fuzzy flag must be removed to indicate
                > the translation is ok (handled by po.vim) and the previous msgid becomes
                > useless and is removed as well (I have a patch for po.vim).
                >
                > It would be of great help to the translator to have the differences
                > between the previous and current msgid highlighted, especially in long
                > strings with only small changes.
                >
                > Regards,
                > Andrei

                Attached script defines a :DiffMsgId command, which attempts to
                overlay diff highlighting to the current section using matchadd().
                E.g. it works on the following section:

                #| msgid ""
                #| "The following disk access storage devices (DASD) are available. Please "
                #| "select each device you want to use one at a time."

                msgid ""
                "The following direct access storage devices (DASD) are available. Please"
                "select each device you want to use one at a time."

                msgstr ""
                "Urmatoarele Dispozitive de stocare cu acces la disc (DASD) sunt disponibile."
                "Va rugam sa alege?i pe rând fiecare dispozitiv pe care dori?i sa-l folosi?i."


                Not yet tested with UTF-8

                --
                Andy

                --
                You received this message from the "vim_use" maillist.
                Do not top-post! Type your reply below the text you are replying to.
                For more information, visit http://www.vim.org/maillist.php
              • Christian Brabandt
                Hi Adam! ... […] ... How would you like the plugin to handle that automatically. If you make good suggestions, I might implement that. regards, Christian --
                Message 7 of 28 , Aug 20, 2010
                • 0 Attachment
                  Hi Adam!

                  On Do, 19 Aug 2010, Adam Monsen wrote:

                  > > May I suggest that you look into Christian Brabandt's NarrowRegion plugin.
                  > > A procedure, after having installed NarrowRegion:
                  > >
                  > > * edit buffer
                  > > * select lines with V
                  > > * :NR (this will make a "narrow region" buffer holding just those lines)
                  > > * select other lines with V
                  > > * :NR
                  > > * in each narrow-region window, type :diffthis
                  […]
                  >
                  > But, IMHO,
                  > * it is cumbersome (unless scripted/mapped, but see below)

                  How would you like the plugin to handle that automatically. If you make
                  good suggestions, I might implement that.


                  regards,
                  Christian

                  --
                  You received this message from the "vim_use" maillist.
                  Do not top-post! Type your reply below the text you are replying to.
                  For more information, visit http://www.vim.org/maillist.php
                • Adam Monsen
                  ... Do you mean the narrow region plugin? Actually, that plugin may not be involved at all. Here s a use case for what I m envisioning. (1) one plain text file
                  Message 8 of 28 , Aug 20, 2010
                  • 0 Attachment
                    > How would you like the plugin to handle that automatically. If you make
                    > good suggestions, I might implement that.

                    Do you mean the narrow region plugin? Actually, that plugin may not be
                    involved at all.

                    Here's a use case for what I'm envisioning.

                    (1) one plain text file in unified diff format is opened in vim
                    (2) vim auto-detects that this is a unified diff file
                    (3) for lines that have changed, the *parts* of lines that have
                    changed are specifically highlighted (similar to the highlighting
                    provided when two files are opened with vimdiff)

                    Here's a mockup:
                    * http://yfrog.com/j1vimintrafilediffhighligp
                    * http://imagebin.ca/view/LmlbQoR.html

                    Bonus use case step:

                    (4) provide keybindings that can
                    (a) cycle through differences
                    (b) cycle through intra-line differences

                    Does that make sense?

                    --
                    You received this message from the "vim_use" maillist.
                    Do not top-post! Type your reply below the text you are replying to.
                    For more information, visit http://www.vim.org/maillist.php
                  • Christian Brabandt
                    Hi Adam! ... Well, Andy provided a short script for po files, that attempted to overlay diff-highlighting to changed regions. Something like this might be
                    Message 9 of 28 , Aug 22, 2010
                    • 0 Attachment
                      Hi Adam!

                      On Fr, 20 Aug 2010, Adam Monsen wrote:

                      > > How would you like the plugin to handle that automatically. If you make
                      > > good suggestions, I might implement that.
                      >
                      > Do you mean the narrow region plugin? Actually, that plugin may not be
                      > involved at all.
                      >
                      > Here's a use case for what I'm envisioning.
                      >
                      > (1) one plain text file in unified diff format is opened in vim
                      > (2) vim auto-detects that this is a unified diff file
                      > (3) for lines that have changed, the *parts* of lines that have
                      > changed are specifically highlighted (similar to the highlighting
                      > provided when two files are opened with vimdiff)
                      >
                      > Here's a mockup:
                      > * http://yfrog.com/j1vimintrafilediffhighligp
                      > * http://imagebin.ca/view/LmlbQoR.html
                      >
                      > Bonus use case step:
                      >
                      > (4) provide keybindings that can
                      > (a) cycle through differences
                      > (b) cycle through intra-line differences
                      >
                      > Does that make sense?

                      Well, Andy provided a short script for po files, that attempted to
                      overlay diff-highlighting to changed regions. Something like this might
                      be possible with unified diff files, although I am not sure how to get
                      the right lines to compare with.

                      So I went the other way and hacked my NrrwRgn Plugin to provide the :NUD
                      (Narrow Unified Diff, it does not really make sense, to use :NUD in any
                      other filetype) command, that opens a chunk in diff mode in a separate
                      window.
                      :NUD will open the chunk on which the cursor is currently in 2 new split
                      windows in diff mode and whenever you move around in your patch file,
                      use :NUD to update your windows. (I find it easy enough to navigate in
                      such a patch file [/^@@ to move to the next chunk], so I did not include
                      a mapping to move to the next chunk, besides, such a mapping would not
                      fit well into the NarrowRegion plugin anyway, but would need to be in a
                      filetype specific plugin).

                      This is an experimental feature, but please try it out. Unfortunately
                      vim.org seems to have database problems currently, but you can also get
                      it from http://github.com/chrisbra/NrrwRgn (the NrrwRgn.vba file
                      contains everything). Granted, this is not exactly what you asked for,
                      but this seems to work well enough.

                      regards,
                      Christian

                      --
                      You received this message from the "vim_use" maillist.
                      Do not top-post! Type your reply below the text you are replying to.
                      For more information, visit http://www.vim.org/maillist.php
                    • Adam Monsen
                      ... I did, and I m impressed! Nice work! This is actually a novel way to tweak a diff, if one wanted to do such a thing. The only issue I ran into was when I
                      Message 10 of 28 , Aug 25, 2010
                      • 0 Attachment
                        > This is an experimental feature, but please try it out.

                        I did, and I'm impressed! Nice work! This is actually a novel way to tweak a
                        diff, if one wanted to do such a thing. The only issue I ran into was when I did
                        ":only!" in the window with the original .diff file I got a bunch of errors.

                        I'm still hacking on my "highlight only" version, but it's slow going.

                        --
                        You received this message from the "vim_use" maillist.
                        Do not top-post! Type your reply below the text you are replying to.
                        For more information, visit http://www.vim.org/maillist.php
                      • Christian Brabandt
                        Hi Adam! ... Nice to hear it is actually useful. I am looking into your error (I only seldomly use :only, so this is a bug I have never run into). ... Please
                        Message 11 of 28 , Aug 26, 2010
                        • 0 Attachment
                          Hi Adam!

                          On Do, 26 Aug 2010, Adam Monsen wrote:

                          > > This is an experimental feature, but please try it out.
                          >
                          > I did, and I'm impressed! Nice work! This is actually a novel way to
                          > tweak a diff, if one wanted to do such a thing. The only issue I ran
                          > into was when I did ":only!" in the window with the original .diff
                          > file I got a bunch of errors.

                          Nice to hear it is actually useful. I am looking into your error (I only
                          seldomly use :only, so this is a bug I have never run into).

                          >
                          > I'm still hacking on my "highlight only" version, but it's slow going.

                          Please share!

                          regards,
                          Christian
                          --
                          Was zugunsten des Staates begonnen wird, geht oft zuungunsten der Welt
                          aus.
                          -- Karl Kraus

                          --
                          You received this message from the "vim_use" maillist.
                          Do not top-post! Type your reply below the text you are replying to.
                          For more information, visit http://www.vim.org/maillist.php
                        • Adam Monsen
                          ... I don t really have anything useful to share at this point, but I can tell you where I m at. I ll start at the beginning so folks can join in even if
                          Message 12 of 28 , Aug 30, 2010
                          • 0 Attachment
                            >> I'm still hacking on my "highlight only" version, but it's slow going.
                            >
                            > Please share!

                            I don't really have anything useful to share at this point, but I can
                            tell you where I'm at. I'll start at the beginning so folks can join
                            in even if they've missed earlier posts in this thread.

                            I want improved syntax highlighting for unified diff/patch files in
                            Vim. I've mentioned this in various forms earlier, but I think that
                            statement hits it more accurately and succinctly.

                            I'm trying to use Vim script identify areas in unified diffs showing
                            changes in lines (as opposed to additions or deletions) and use a more
                            striking highlighting mode to point out, say, when a single letter or
                            space changes in a line. This is quite difficult to spot visually in a
                            unified diff. Vimdiff finds and highlights these types of changes
                            quite nicely, emacs diff mode does as well, and "git diff
                            --color-words" (now called --word-diff, I think) also does a great
                            job. I think I need to use or emulate the intra-line difference
                            location algorithm(s) from those tools. I deliberately left out wdiff,
                            whose output I don't particularly like. Of those listed, only emacs
                            diff mode actually does the intra-line word difference highlighting
                            for a single unified diff file (the others require both the old and
                            new files).

                            Once I've identified changes in hunks, I plan on using something like
                            the following snippet to highlight specific parts of lines (thanks to
                            bairui on #vim for the pointer on \%v):

                            :highlight DiffRegion ctermbg=red guibg=red
                            :let m = matchadd("DiffRegion", '\%6l\%9v.*\%14v')
                            :let n = matchadd("DiffRegion", '\%7l\%9v.*\%16v')

                            I'm stuck trying to identify changes in words or spaces.

                            I started by writing a line-by-line scanner ( http://pastey.net/140210
                            ) that would detect when it started seeing the "old" ('^-') and "new"
                            ('^+') parts of a change hunk, but then I realized that I'd rather
                            just manipulate pointers to those different parts of hunks and walk
                            through the parts in parallel... and I'm kinda lost here.

                            Another approach I played with a little was using a regex (
                            http://pastey.net/140211 ) to get to each hunk, something like this:

                            '\nindex.*\n--- .*\n+++ .*\n@@.*@@.*$'

                            And this is about the point that I've decided it's going to be quite a
                            tricky task to actually get all the use cases covered for intra-line
                            differences.

                            Here's sort of a collection of use cases, in one unified diff.

                            tmp.diff: http://pastey.net/140214
                            old : http://pastey.net/140212
                            new : http://pastey.net/140213

                            To see how other tools highlight stuff, I've been doing stuff like:

                            - vimdiff old new
                            - git diff old new
                            - git diff --color-words old new
                            - emacs tmp.diff (then ALT-n once emacs starts up)

                            Anyway, I'm probably going to set this stuff down for a while. It
                            would be fun to have a little in-person hack session on this,
                            especially with someone at least mildly talented in Vim script and/or
                            general scanning/parsing algorithms. Vim script might not even be
                            what's blocking me... for some reason I'm also really getting stuck
                            breaking down/identifying/solving the problem about how to identify
                            which parts of a line have changed. Tokenizing/scanning/whatever you
                            call it is partly subjective--see the new "git --word-diff-regex"
                            command line argument, and discussion around same on the git mailing
                            list. In fact, the git solution might be the most elegant and flexible
                            algorithm to emulate.

                            That's all for now, cheers,
                            -Adam

                            --
                            You received this message from the "vim_use" maillist.
                            Do not top-post! Type your reply below the text you are replying to.
                            For more information, visit http://www.vim.org/maillist.php
                          • James Cole
                            Hi, ... I haven t really been following this thread in detail, and I m no expert on all-things-diffing, but I thought that Neil Fraser s diff_match_patch
                            Message 13 of 28 , Sep 1, 2010
                            • 0 Attachment
                              Hi,

                              > ...I'm trying to use Vim script identify areas in unified diffs showing
                              > changes in lines (as opposed to additions or deletions) and use a more
                              > striking highlighting mode to point out, say, when a single letter or
                              > space changes in a line.
                              >
                              > ...
                              >
                              > To see how other tools highlight stuff, I've been doing stuff like:
                              > - vimdiff old new
                              > - git diff old new
                              > - git diff --color-words old new
                              > - emacs tmp.diff (then ALT-n once emacs starts up)
                              >
                              > ...
                              >
                              > for some reason I'm also really getting stuck
                              > breaking down/identifying/solving the problem about how to identify
                              > which parts of a line have changed. Tokenizing/scanning/whatever you
                              > call it is partly subjective--see the new "git --word-diff-regex"
                              > command line argument, and discussion around same on the git mailing
                              > list. In fact, the git solution might be the most elegant and flexible
                              > algorithm to emulate.
                              >

                              I haven't really been following this thread in detail, and I'm no
                              expert on all-things-diffing, but I thought that Neil Fraser's
                              diff_match_patch (which I have no affilation with; available here:
                              http://code.google.com/p/google-diff-match-patch/) might be of
                              interest. It handles intra-line changes nicely, and the souce code is
                              available to look at. According to its page:

                              "This library implements Myer's diff algorithm (http://
                              neil.fraser.name/software/diff_match_patch/myers.pdf) which is
                              generally considered to be the best general-purpose diff. A layer of
                              pre-diff speedups and post-diff cleanups surround the diff algorithm,
                              improving both performance and output quality.

                              This library also implements a Bitap matching algorithm (http://
                              neil.fraser.name/software/diff_match_patch/bitap.ps) at the heart
                              of a flexible matching and patching strategy (http://neil.fraser.name/
                              writing/patch/)."

                              Regards,
                              James.

                              --
                              You received this message from the "vim_use" maillist.
                              Do not top-post! Type your reply below the text you are replying to.
                              For more information, visit http://www.vim.org/maillist.php
                            • Adam Monsen
                              ... Awesome! Thanks for the pointer, James! The Python version of diff-match-patch seems to work pretty dang well. I did a few quick tests with diff_main() and
                              Message 14 of 28 , Sep 2, 2010
                              • 0 Attachment
                                > Neil Fraser's diff_match_patch ... might be of interest.

                                Awesome! Thanks for the pointer, James!

                                The Python version of diff-match-patch seems to work pretty dang well.
                                I did a few quick tests with diff_main() and diff_cleanupSemantic().
                                The API is simple, clear, and predictable! This could be used in a Vim
                                script if Python support is compiled into Vim.

                                Porting diff-match-patch to Vim script is another option... that might
                                be kinda fun.

                                Hmm, that gives me another idea... Vim also has/uses some diffing
                                algorithm since Vimdiff highlights differences in lines. We could
                                change Vim itself to expose whatever diffing logic vimdiff uses so Vim
                                scripts could use it. Maybe even using an API similar to
                                diff-match-patch.

                                --
                                You received this message from the "vim_use" maillist.
                                Do not top-post! Type your reply below the text you are replying to.
                                For more information, visit http://www.vim.org/maillist.php
                              Your message has been successfully submitted and would be delivered to recipients shortly.