Truly elastic word-wrapping in jEdit

Suraj N. Kurapati


  1. Problem
    1. Approach
      1. Solution

        Problem

        jEdit’s soft word-wrap feature, when used with a wrap-margin of zero, dynamically wraps text to fit the window. However, this feature isn’t very useful on a wide-screen display because most lines of text are not wrapped since they are not long enough to span the entire screen.

        In such cases, I would prefer having a finite wrap-margin to limit how long lines need to be in order to be wrapped. However, when a finite wrap-margin is used, the text does not elastically wrap to the window when the window is thinner than the limit.

        Approach

        Thus, I wanted the best of both worlds: a maximum width feature, like the max-width property in CSS, which would:

        1. elastically wrap text to the window’s width when it is thinner than the maximum width

        2. wrap text to the maximum width when the window’s width is wider than the maximum width

        When I requested this feature on the jEdit users mailing list, the developers had mixed opinions about incorporating it. Nevertheless, they helped me implement the feature in my own copy of the jEdit source code.

        I posted the patch file for this feature to the mailing list soon after.

        Solution

        Index: org/gjt/sp/jedit/textarea/TextArea.java
        ===================================================================
        --- org/gjt/sp/jedit/textarea/TextArea.java (revision 11614)
        +++ org/gjt/sp/jedit/textarea/TextArea.java (working copy)
        @@ -5775,6 +5775,41 @@
                {
                        this.maxLineLen = maxLineLen;
        
        +       /**
        +           Makes soft word-wrap mode behave like the CSS "max-width" property:
        +
        +           1. When the window width is less than the maximum, the wrapping
        +              behaves like wrap-margin is 0 (fits to the window).
        +
        +           2. When the window width is greater than the maximum, the wrapping behaves
        +              like fixed wrap-margin (wrapping restricted to the maximum width).
        +
        +           @see discussion of this feature at:
        +                http://sourceforge.net/mailarchive/forum.php?thread_id=31224167&forum_id=5663
        +       */
        +       if(softWrap)
        +       {
        +           // determine width of maximum rendered line
        +           char[] foo = new char[maxLineLen];
        +           for(int i = 0; i < foo.length; i++)
        +           {
        +               foo[i] = ' ';
        +           }
        +
        +           int maxRenderedLineLen = (int)painter.getFont().getStringBounds(
        +               foo,0,foo.length,
        +               painter.getFontRenderContext())
        +               .getWidth();
        +
        +
        +           if(painter.getWidth() < maxRenderedLineLen)
        +           {
        +               wrapToWidth = true;
        +               wrapMargin = painter.getWidth() - charWidth * 3;
        +               return;
        +           }
        +       }
        +
                        if(maxLineLen <= 0)
                        {
                                if(softWrap)
        

        Updates