# for Nedit-5.5
# display an hairline marking the wrap column
# original patch can be found here:
# http://ajbj.free.fr/nedit/nedit-scripts/INTERESTING/utilsrc/nedit/myNEditPatches/HairlineMargin.diff
#

--- doc/help.etx.orig	Fri Aug 20 21:33:08 2004
+++ doc/help.etx		Sat Dec 23 16:25:53 2006
@@ -3630,6 +3630,14 @@ Preferences
   may, be wrapped at the right margin of the window, or the margin can be set
   at a specific column.
 
+**Show Wrap Margin**
+  Visually indicate which column is set as the wrap margin by drawing a vertical
+  line. Choose between never, always and when wrap is enabled. "Never" turns
+  this feature off. "Always" will show the wrap margin irrespecitive of the
+  wrapping style. "When Wrap is Enabled" will show the wrap margin only if
+  continuous or auto-newline wrap styles are choosed and does not show the wrap
+  margin if wrapping is turned off.
+
 **Tab Stops**
   Set the tab distance (number of characters between tab stops) for tab
   characters, and control tab emulation and use of tab characters in padding
--- source/help_data.h.orig	Thu Sep 30 23:03:55 2004
+++ source/help_data.h		Sat Dec 23 16:25:53 2006
@@ -3440,6 +3440,14 @@ static char * htxt_preferences [] = {
 "may, be wrapped at the right margin of the window, or the margin can be set ",
 "at a specific column. ",
 "\n\n",
+"\01A\01BShow Wrap Margin\01A\n",
+"\01IVisually indicate which column is set as the wrap margin by drawing a vertical ",
+"line. Choose between never, always and when wrap is enabled. \"Never\" turns ",
+"this feature off. \"Always\" will show the wrap margin irrespecitive of the ",
+"wrapping style. \"When Wrap is Enabled\" will show the wrap margin only if ",
+"continuous or auto-newline wrap styles are choosed and does not show the wrap ",
+"margin if wrapping is turned off. ",
+"\n\n",
 "\01A\01BTab Stops\01A\n",
 "\01ISet the tab distance (number of characters between tab stops) for tab ",
 "characters, and control tab emulation and use of tab characters in padding ",
--- source/menu.c.orig	Thu Sep 16 00:50:57 2004
+++ source/menu.c			Sat Dec 23 16:25:53 2006
@@ -388,6 +388,8 @@ static void setWrapTextAP(Widget w, XEve
     Cardinal *nArgs);
 static void setWrapMarginAP(Widget w, XEvent *event, String *args,
     Cardinal *nArgs);
+static void setShowWrapMarginAP(Widget w, XEvent *event, String *args,
+    Cardinal *nArgs);
 static void setHighlightSyntaxAP(Widget w, XEvent *event, String *args,
     Cardinal *nArgs);
 static void setMakeBackupCopyAP(Widget w, XEvent *event, String *args,
@@ -562,6 +564,7 @@ static XtActionsRec Actions[] = {
     {"set_auto_indent", setAutoIndentAP},
     {"set_wrap_text", setWrapTextAP},
     {"set_wrap_margin", setWrapMarginAP},
+    {"set_show_wrap_margin", setShowWrapMarginAP},
     {"set_highlight_syntax", setHighlightSyntaxAP},
 #ifndef VMS
     {"set_make_backup_copy", setMakeBackupCopyAP},
@@ -4016,6 +4019,20 @@ static void setWrapMarginAP(Widget w, XE
     }
     else {
         fprintf(stderr, "nedit: set_wrap_margin requires argument\n");
+    }
+}
+
+static void setShowWrapMarginAP(Widget w, XEvent *event, String *args,
+    Cardinal *nArgs)
+{
+    WindowInfo *window = WidgetToWindow(w);
+    int showWrapMargin = 0;
+    int i;
+    
+    if (*nArgs > 0) {
+        if (sscanf(args[0], "%d", &showWrapMargin) == 1) {
+            SetShowWrapMargin(window, showWrapMargin);
+        }
     }
 }
 
--- source/nedit.h.orig	Fri Aug 20 21:33:20 2004
+++ source/nedit.h			Sat Dec 23 16:25:53 2006
@@ -50,10 +50,10 @@
 #define NEDIT_DEFAULT_HI_BG     "red"   /*   matching parens. */
 #define NEDIT_DEFAULT_LINENO_FG "black"
 #define NEDIT_DEFAULT_CURSOR_FG "black"
+#define NEDIT_DEFAULT_WRAPMARGIN_FG "black"
 #define NEDIT_DEFAULT_HELP_FG   "black"
 #define NEDIT_DEFAULT_HELP_BG   "rgb:cc/cc/cc"
 
-
 /* Tuning parameters */
 #define SEARCHMAX 511		/* Maximum length of search/replace strings */
 #define MAX_SEARCH_HISTORY 100	/* Maximum length of search string history */
@@ -101,7 +101,9 @@ enum wrapStyle {NO_WRAP, NEWLINE_WRAP, C
 enum showMatchingStyle {NO_FLASH, FLASH_DELIMIT, FLASH_RANGE};
 enum virtKeyOverride { VIRT_KEY_OVERRIDE_NEVER, VIRT_KEY_OVERRIDE_AUTO,
                        VIRT_KEY_OVERRIDE_ALWAYS };
-
+enum showWrapMarginEnums {SHOW_WRAP_MARGIN_NEVER, SHOW_WRAP_MARGIN_ALWAYS,
+                          SHOW_WRAP_MARGIN_ON_WRAP};
+ 
 #define NO_FLASH_STRING		"off"
 #define FLASH_DELIMIT_STRING	"delimiter"
 #define FLASH_RANGE_STRING	"range"
@@ -185,6 +187,7 @@ enum colorTypes {
     HILITE_BG_COLOR,
     LINENO_FG_COLOR,
     CURSOR_FG_COLOR,
+    WRAPMARGIN_FG_COLOR,
     NUM_COLORS
 };
 
@@ -486,6 +489,8 @@ typedef struct _WindowInfo {
     char	indentStyle;		/* whether/how to auto indent */
     char	wrapMode;		/* line wrap style: NO_WRAP,
     	    	    	    	    	   NEWLINE_WRAP or CONTINUOUS_WRAP */
+    int         showWrapMargin;         /* show wrap margin style: NEVER,
+                                           ALWAYS, ON-WRAP as enums */
     Boolean	overstrike;		/* is overstrike mode turned on ? */
     char 	showMatchingStyle; 	/* How to show matching parens:
 					   NO_FLASH, FLASH_DELIMIT, or
--- source/preferences.c.orig	Fri Aug 20 21:33:20 2004
+++ source/preferences.c		Sat Dec 23 16:25:53 2006
@@ -137,6 +137,9 @@ static char *AutoIndentTypes[N_INDENT_ST
 #define N_VIRTKEY_OVERRIDE_MODES 3
 static char *VirtKeyOverrideModes[N_VIRTKEY_OVERRIDE_MODES+1] = { "Never",
 	"Auto", "Always", NULL};
+#define N_SHOW_WRAP_MARGIN_STYLES 3
+static char *ShowWrapMarginStrings[N_SHOW_WRAP_MARGIN_STYLES+1] = { "Never",
+        "Always", "When Wrap is Enabled", NULL};
 
 #define N_SHOW_MATCHING_STYLES 3
 /* For backward compatibility, "False" and "True" are still accepted.
@@ -228,6 +231,8 @@ typedef struct {
     Widget lineNoFgErrW;
     Widget cursorFgW;
     Widget cursorFgErrW;
+    Widget wrapMarginFgW;
+    Widget wrapMarginFgErrW;
     WindowInfo *window;
 } colorDialog;
 
@@ -236,6 +241,7 @@ static struct prefData {
     int openInTab;		/* open files in new tabs  */
     int wrapStyle;		/* what kind of wrapping to do */
     int wrapMargin;		/* 0=wrap at window width, other=wrap margin */
+    int showWrapMargin;         /* whether to draw line at wrap margin */
     int autoIndent;		/* style for auto-indent */
     int autoSave;		/* whether automatic backup feature is on */
     int saveOldVersion;		/* whether to preserve a copy of last version */
@@ -746,6 +752,8 @@ static PrefDescripRec PrefDescrip[] = {
     	&PrefData.wrapStyle, AutoWrapTypes, True},
     {"wrapMargin", "WrapMargin", PREF_INT, "0",
     	&PrefData.wrapMargin, NULL, True},
+    {"showWrapMargin", "ShowWrapMargin", PREF_ENUM, "Never",
+        &PrefData.showWrapMargin, ShowWrapMarginStrings, True},
     {"autoIndent", "AutoIndent", PREF_ENUM, "Auto",
     	&PrefData.autoIndent, AutoIndentTypes, True},
     {"autoSave", "AutoSave", PREF_BOOLEAN, "True",
@@ -924,6 +932,9 @@ static PrefDescripRec PrefDescrip[] = {
     {"tooltipBgColor", "TooltipBgColor", PREF_STRING, "LemonChiffon1",
         PrefData.tooltipBgColor,
         (void *)sizeof(PrefData.tooltipBgColor), False},
+    {"wrapMarginForeground", "WrapMarginForeground", PREF_STRING,
+            NEDIT_DEFAULT_LINENO_FG, PrefData.colorNames[WRAPMARGIN_FG_COLOR],
+            (void *)sizeof(PrefData.colorNames[WRAPMARGIN_FG_COLOR]), True},
                 
     {"shell", "Shell", PREF_STRING,
 #if defined(__MVS__) || defined(__EMX__)
@@ -1022,7 +1033,9 @@ static Widget TabDistText, EmTabText, Em
 /* Module-global variables for Wrap Margin dialog */
 static int DoneWithWrapDialog;
 static WindowInfo *WrapDialogForWindow;
-static Widget WrapText, WrapTextLabel, WrapWindowToggle;
+static Widget WrapText, WrapTextLabel, WrapWindowToggle, ShowWrapMarginPulldown,
+        ShowWrapMarginPulldownItems[N_SHOW_WRAP_MARGIN_STYLES], 
+        ShowWrapMarginOptMenu, ShowWrapMarginLabel;
 
 static void translatePrefFormats(int convertOld, int fileVer);
 static void setIntPref(int *prefDataField, int newValue);
@@ -1415,6 +1428,16 @@ int GetPrefWrapMargin(void)
     return PrefData.wrapMargin;
 }
 
+void SetPrefShowWrapMargin(int state)
+{
+    setIntPref(&PrefData.showWrapMargin, state);
+}
+
+int GetPrefShowWrapMargin(void)
+{
+    return PrefData.showWrapMargin;
+}
+
 void SetPrefSearch(int searchType)
 {
     setIntPref(&PrefData.searchMethod, searchType);
@@ -2545,6 +2568,9 @@ void WrapMarginDialog(Widget parent, Win
     Arg selBoxArgs[2];
     XmString s1;
     int margin;
+    int showWrapMargin;
+    int i, n;
+    Arg args[20];
 
     XtSetArg(selBoxArgs[0], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL);
     XtSetArg(selBoxArgs[1], XmNautoUnmanage, False);
@@ -2587,16 +2613,70 @@ void WrapMarginDialog(Widget parent, Win
 	    XmNbottomWidget, WrapText, NULL);
     XmStringFree(s1);
 
+    ShowWrapMarginPulldown = CreatePulldownMenu(form, "ShowWrapMarginPulldown",
+            NULL, 0);
+    for(i=0; i<N_SHOW_WRAP_MARGIN_STYLES; i++) {
+        s1 = XmStringCreateSimple(ShowWrapMarginStrings[i]);
+        ShowWrapMarginPulldownItems[i] = XtVaCreateManagedWidget(
+                "ShowWrapMarginPulldown", 
+                xmPushButtonWidgetClass, ShowWrapMarginPulldown, 
+                XmNlabelString, s1,
+                XmNmarginHeight, 0,
+                XmNuserData, i,
+                NULL);
+        XmStringFree(s1);
+    }
+    n = 0;
+    XtSetArg(args[n], XmNspacing, 0); n++;
+    XtSetArg(args[n], XmNmarginWidth, 0); n++;
+    XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
+    XtSetArg(args[n], XmNtopWidget, WrapText); n++;
+    XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
+    XtSetArg(args[n], XmNsubMenuId, ShowWrapMarginPulldown); n++;
+    ShowWrapMarginOptMenu = XmCreateOptionMenu(form,
+            "ShowWrapMarginOptMenu", args, n);
+    XtManageChild(ShowWrapMarginOptMenu);
+
+    ShowWrapMarginLabel = XtVaCreateManagedWidget("ShowWrapMarginLabel", 
+            xmLabelGadgetClass, form,
+            XmNlabelString, s1=XmStringCreateSimple("Show Wrap Margin"),
+            XmNmnemonic, 'S',
+            XmNuserData, XtParent(ShowWrapMarginOptMenu),
+            XmNalignment, XmALIGNMENT_END,
+            XmNtopAttachment, XmATTACH_WIDGET,
+            XmNtopWidget, WrapText,
+            XmNleftAttachment, XmATTACH_FORM,
+            XmNrightAttachment, XmATTACH_WIDGET,
+            XmNrightWidget, ShowWrapMarginOptMenu,
+            XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
+            XmNbottomWidget, ShowWrapMarginOptMenu,
+            NULL);
+    XmStringFree(s1);
+    
     /* Set default value */
-    if (forWindow == NULL)
+    if (forWindow == NULL) {
     	margin = GetPrefWrapMargin();
-    else
+        showWrapMargin = GetPrefShowWrapMargin();
+    } else {
     	XtVaGetValues(forWindow->textArea, textNwrapMargin, &margin, NULL);
+        showWrapMargin = forWindow->showWrapMargin;
+    }
+
+    if (showWrapMargin > N_SHOW_WRAP_MARGIN_STYLES || showWrapMargin < 0)
+    {
+        fprintf(stderr, "nedit: internal error: illegal value for showWrapMargin: %d\n", showWrapMargin);
+        showWrapMargin = SHOW_WRAP_MARGIN_NEVER;
+    }
+
     XmToggleButtonSetState(WrapWindowToggle, margin==0, True);
+    XtVaSetValues(ShowWrapMarginOptMenu, XmNmenuHistory, 
+            ShowWrapMarginPulldownItems[showWrapMargin], NULL);
     if (margin != 0)
     	SetIntText(WrapText, margin);
     XtSetSensitive(WrapText, margin!=0);
     XtSetSensitive(WrapTextLabel, margin!=0);
+    XtSetSensitive(ShowWrapMarginOptMenu, margin!=0);
+    XtSetSensitive(ShowWrapMarginLabel, margin!=0);
     
     /* Handle mnemonic selection of buttons and focus to dialog */
     AddDialogMnemonicHandler(form, FALSE);
@@ -2619,6 +2699,8 @@ static void wrapOKCB(Widget w, XtPointer
 {
     int wrapAtWindow, margin, stat;
     WindowInfo *window = WrapDialogForWindow;
+    int showWrapMargin;
+    Widget showWrapMarginSelectedItem;
     
     /* get the values that the user entered and make sure they're ok */
     wrapAtWindow = XmToggleButtonGetState(WrapWindowToggle);
@@ -2638,6 +2720,13 @@ static void wrapOKCB(Widget w, XtPointer
 
     }
 
+    XtVaGetValues(ShowWrapMarginOptMenu, XmNmenuHistory, 
+            &showWrapMarginSelectedItem, NULL);
+    XtVaGetValues(showWrapMarginSelectedItem, XmNuserData, &showWrapMargin, NULL);
+    if (showWrapMargin > N_SHOW_WRAP_MARGIN_STYLES || showWrapMargin < 0) {
+        showWrapMargin = SHOW_WRAP_MARGIN_NEVER;
+    }
+
 #ifdef SGI_CUSTOM
     /* Ask the user about saving as a default preference */
     if (WrapDialogForWindow != NULL) {
@@ -2649,20 +2738,25 @@ static void wrapOKCB(Widget w, XtPointer
 	}
 	if (setDefault) {
     	    SetPrefWrapMargin(margin);
+            SetPrefShowWrapMargin(showWrapMargin);
 	    SaveNEditPrefs(window->shell, GetPrefShortMenus());
 	}
     }
 #endif
 
     /* Set the value in either the requested window or default preferences */
-    if (WrapDialogForWindow == NULL)
+    if (WrapDialogForWindow == NULL) {
     	SetPrefWrapMargin(margin);
-    else {
+        SetPrefShowWrapMargin(showWrapMargin);
+    } else {
         char *params[1];
         char marginStr[25];
         sprintf(marginStr, "%d", margin);
         params[0] = marginStr;
         XtCallActionProc(window->textArea, "set_wrap_margin", NULL, params, 1);
+        sprintf(marginStr, "%d", showWrapMargin);
+        params[0] = marginStr;
+        XtCallActionProc(window->textArea, "set_show_wrap_margin", NULL, params, 1);
     }
     DoneWithWrapDialog = True;
 }
@@ -2678,6 +2772,8 @@ static void wrapWindowCB(Widget w, XtPoi
     
     XtSetSensitive(WrapTextLabel, !wrapAtWindow);
     XtSetSensitive(WrapText, !wrapAtWindow);
+    XtSetSensitive(ShowWrapMarginOptMenu, !wrapAtWindow);
+    XtSetSensitive(ShowWrapMarginLabel, !wrapAtWindow);
 }
 
 /*
@@ -5689,7 +5785,14 @@ static void cursorFgModifiedCB(Widget w,
     showColorStatus(cd, cd->cursorFgW, cd->cursorFgErrW);
 }
 
+static void wrapMarginFgModifiedCB(Widget w, XtPointer clientData,
+        XtPointer callData)
+{
+    colorDialog *cd = (colorDialog *)clientData;
+    showColorStatus(cd, cd->wrapMarginFgW, cd->wrapMarginFgErrW);
+}
 
+
 /* 
  * Helper functions for validating colors
  */
@@ -5741,12 +5844,13 @@ static void updateColors(colorDialog *cd
             *hiliteFg = XmTextGetString(cd->hiliteFgW),
             *hiliteBg = XmTextGetString(cd->hiliteBgW),
             *lineNoFg = XmTextGetString(cd->lineNoFgW),
-            *cursorFg = XmTextGetString(cd->cursorFgW);
+            *cursorFg = XmTextGetString(cd->cursorFgW),
+            *wrapMarginFg = XmTextGetString(cd->wrapMarginFgW);
 
     for (window = WindowList; window != NULL; window = window->next)
     {
         SetColors(window, textFg, textBg, selectFg, selectBg, hiliteFg, 
-                hiliteBg, lineNoFg, cursorFg);
+                hiliteBg, lineNoFg, cursorFg, wrapMarginFg);
     }
 
     SetPrefColorName(TEXT_FG_COLOR  , textFg  );
@@ -5757,6 +5861,7 @@ static void updateColors(colorDialog *cd
     SetPrefColorName(HILITE_BG_COLOR, hiliteBg);
     SetPrefColorName(LINENO_FG_COLOR, lineNoFg);
     SetPrefColorName(CURSOR_FG_COLOR, cursorFg);
+    SetPrefColorName(WRAPMARGIN_FG_COLOR, wrapMarginFg);
 
     XtFree(textFg);
     XtFree(textBg);
@@ -5766,6 +5871,7 @@ static void updateColors(colorDialog *cd
     XtFree(hiliteBg);
     XtFree(lineNoFg);
     XtFree(cursorFg);
+    XtFree(wrapMarginFg);
 }
 
 
@@ -5935,6 +6041,20 @@ void ChooseColors(WindowInfo *window)
     
     topW = infoLbl;
     
+    /* The right column (backgrounds) */
+    tmpW = addColorGroup( form, "textBg", 'T', "Text Area Background",
+            &(cd->textBgW), &(cd->textBgErrW), topW, 51, 99, 
+            textBgModifiedCB, cd );
+    tmpW = addColorGroup( form, "selectBg", 'B', "Selection Background",
+            &(cd->selectBgW), &(cd->selectBgErrW), tmpW, 51, 99, 
+            selectBgModifiedCB, cd );
+    tmpW = addColorGroup( form, "hiliteBg", 'h', "Matching (..) Background",
+            &(cd->hiliteBgW), &(cd->hiliteBgErrW), tmpW, 51, 99, 
+            hiliteBgModifiedCB, cd );
+    tmpW = addColorGroup( form, "wrapMarginFg", 'w', "Wrap Margin Color",
+            &(cd->wrapMarginFgW), &(cd->wrapMarginFgErrW), tmpW, 51, 99, 
+            wrapMarginFgModifiedCB, cd );
+
     /* The left column (foregrounds) of color entry groups */
     tmpW = addColorGroup( form, "textFg", 'P', "Plain Text Foreground", 
             &(cd->textFgW), &(cd->textFgErrW), topW, 1, 49, 
@@ -5948,19 +6068,8 @@ void ChooseColors(WindowInfo *window)
     tmpW = addColorGroup( form, "lineNoFg", 'L', "Line Numbers",
             &(cd->lineNoFgW), &(cd->lineNoFgErrW), tmpW, 1, 49, 
             lineNoFgModifiedCB, cd );
-
-    /* The right column (backgrounds) */
-    tmpW = addColorGroup( form, "textBg", 'T', "Text Area Background",
-            &(cd->textBgW), &(cd->textBgErrW), topW, 51, 99, 
-            textBgModifiedCB, cd );
-    tmpW = addColorGroup( form, "selectBg", 'B', "Selection Background",
-            &(cd->selectBgW), &(cd->selectBgErrW), tmpW, 51, 99, 
-            selectBgModifiedCB, cd );
-    tmpW = addColorGroup( form, "hiliteBg", 'h', "Matching (..) Background",
-            &(cd->hiliteBgW), &(cd->hiliteBgErrW), tmpW, 51, 99, 
-            hiliteBgModifiedCB, cd );
     tmpW = addColorGroup( form, "cursorFg", 'C', "Cursor Color",
-            &(cd->cursorFgW), &(cd->cursorFgErrW), tmpW, 51, 99, 
+            &(cd->cursorFgW), &(cd->cursorFgErrW), tmpW, 1, 49, 
             cursorFgModifiedCB, cd );
 
     tmpW = XtVaCreateManagedWidget("infoLbl",
@@ -6043,6 +6152,7 @@ void ChooseColors(WindowInfo *window)
     XmTextSetString(cd->hiliteBgW, GetPrefColorName(HILITE_BG_COLOR));
     XmTextSetString(cd->lineNoFgW, GetPrefColorName(LINENO_FG_COLOR));
     XmTextSetString(cd->cursorFgW, GetPrefColorName(CURSOR_FG_COLOR));
+    XmTextSetString(cd->wrapMarginFgW, GetPrefColorName(WRAPMARGIN_FG_COLOR));
     
     /* Handle mnemonic selection of buttons and focus to dialog */
     AddDialogMnemonicHandler(form, FALSE);
--- source/preferences.h.orig	Wed Jul 21 13:32:05 2004
+++ source/preferences.h		Sat Dec 23 16:25:53 2006
@@ -59,6 +59,8 @@ void SetPrefWrap(int state);
 int GetPrefWrap(int langMode);
 void SetPrefWrapMargin(int margin);
 int GetPrefWrapMargin(void);
+void SetPrefShowWrapMargin(int state);
+int GetPrefShowWrapMargin(void);
 void SetPrefSearchDlogs(int state);
 int GetPrefSearchDlogs(void);
 void SetPrefKeepSearchDlogs(int state);
--- source/textDisp.c.orig	Wed Jul 21 13:32:05 2004
+++ source/textDisp.c			Sat Dec 23 16:26:28 2006
@@ -153,7 +153,8 @@ static int emptyLinesVisible(textDisp *t
 static void blankCursorProtrusions(textDisp *textD);
 static void allocateFixedFontGCs(textDisp *textD, XFontStruct *fontStruct,
         Pixel bgPixel, Pixel fgPixel, Pixel selectFGPixel, Pixel selectBGPixel,
-        Pixel highlightFGPixel, Pixel highlightBGPixel, Pixel lineNumFGPixel);
+        Pixel highlightFGPixel, Pixel highlightBGPixel, Pixel lineNumFGPixel,
+        Pixel wrapMarginFGPixel);
 static GC allocateGC(Widget w, unsigned long valueMask,
         unsigned long foreground, unsigned long background, Font font,
         unsigned long dynamicMask, unsigned long dontCareMask);
@@ -179,7 +180,9 @@ static int maintainingAbsTopLineNum(text
 static void resetAbsLineNum(textDisp *textD);
 static int measurePropChar(textDisp *textD, char c, int colNum, int pos);
 static Pixel allocBGColor(Widget w, char *colorName, int *ok);
-Pixel getRangesetColor(textDisp *textD, int ind, Pixel bground);
+static Pixel getRangesetColor(textDisp *textD, int ind, Pixel bground);
+static void drawWrapMargin(textDisp* textD);
+static void redisplayCursor(textDisp* textD);
 
 textDisp *TextDCreate(Widget widget, Widget hScrollBar, Widget vScrollBar,
         Position left, Position top, Position width, Position height,
@@ -187,8 +190,9 @@ textDisp *TextDCreate(Widget widget, Wid
         XFontStruct *fontStruct, Pixel bgPixel, Pixel fgPixel,
         Pixel selectFGPixel, Pixel selectBGPixel, Pixel highlightFGPixel,
         Pixel highlightBGPixel, Pixel cursorFGPixel, Pixel lineNumFGPixel,
-        int continuousWrap, int wrapMargin, XmString bgClassString,
-        Pixel calltipFGPixel, Pixel calltipBGPixel)
+        Pixel wrapMarginFGPixel, int continuousWrap, int wrapMargin,
+        XmString bgClassString, Pixel calltipFGPixel, Pixel calltipBGPixel,
+        int showWrapMargin)
 {
     textDisp *textD;
     XGCValues gcValues;
@@ -233,11 +237,14 @@ textDisp *TextDCreate(Widget widget, Wid
     textD->selectBGPixel = selectBGPixel;
     textD->highlightBGPixel = highlightBGPixel;
     textD->lineNumFGPixel = lineNumFGPixel;
+    textD->wrapMarginFGPixel = wrapMarginFGPixel;
     textD->cursorFGPixel = cursorFGPixel;
     textD->wrapMargin = wrapMargin;
     textD->continuousWrap = continuousWrap;
+    textD->showWrapMargin = showWrapMargin;
     allocateFixedFontGCs(textD, fontStruct, bgPixel, fgPixel, selectFGPixel,
-            selectBGPixel, highlightFGPixel, highlightBGPixel, lineNumFGPixel);
+            selectBGPixel, highlightFGPixel, highlightBGPixel, lineNumFGPixel,
+            wrapMarginFGPixel);
     textD->styleGC = allocateGC(textD->w, 0, 0, 0, fontStruct->fid,
             GCClipMask|GCForeground|GCBackground, GCArcMode);
     textD->lineNumLeft = lineNumLeft;
@@ -319,6 +326,7 @@ void TextDFree(textDisp *textD)
     releaseGC(textD->w, textD->highlightBGGC);
     releaseGC(textD->w, textD->styleGC);
     releaseGC(textD->w, textD->lineNumGC);
+    releaseGC(textD->w, textD->wrapMarginGC);
     XtFree((char *)textD->lineStarts);
     while (TextDPopGraphicExposeQueueEntry(textD)) {
     }
@@ -391,7 +399,7 @@ void TextDAttachHighlightData(textDisp *
 /* Change the (non syntax-highlit) colors */ 
 void TextDSetColors(textDisp *textD, Pixel textFgP, Pixel textBgP, 
         Pixel selectFgP, Pixel selectBgP, Pixel hiliteFgP, Pixel hiliteBgP, 
-        Pixel lineNoFgP, Pixel cursorFgP)
+        Pixel lineNoFgP, Pixel cursorFgP, Pixel wrapMarginFgP)
 {
     XGCValues values;
     Display *d = XtDisplay(textD->w);
@@ -405,6 +413,7 @@ void TextDSetColors(textDisp *textD, Pix
     textD->highlightBGPixel = hiliteBgP;
     textD->lineNumFGPixel = lineNoFgP;
     textD->cursorFGPixel = cursorFgP;
+    textD->wrapMarginFGPixel = wrapMarginFgP;
 
     releaseGC(textD->w, textD->gc);
     releaseGC(textD->w, textD->selectGC);
@@ -412,8 +421,9 @@ void TextDSetColors(textDisp *textD, Pix
     releaseGC(textD->w, textD->highlightGC);
     releaseGC(textD->w, textD->highlightBGGC);
     releaseGC(textD->w, textD->lineNumGC);
+    releaseGC(textD->w, textD->wrapMarginGC);
     allocateFixedFontGCs(textD, textD->fontStruct, textBgP, textFgP, selectFgP,
-            selectBgP, hiliteFgP, hiliteBgP, lineNoFgP);
+            selectBgP, hiliteFgP, hiliteBgP, lineNoFgP, wrapMarginFgP);
     
     /* Change the cursor GC (the cursor GC is not shared). */
     values.foreground = cursorFgP;
@@ -435,6 +445,7 @@ void TextDSetFont(textDisp *textD, XFont
     int width, height, fontWidth;
     Pixel bgPixel, fgPixel, selectFGPixel, selectBGPixel;
     Pixel highlightFGPixel, highlightBGPixel, lineNumFGPixel;
+    Pixel wrapMarginFGPixel;
     XGCValues values;
     XFontStruct *styleFont;
     
@@ -488,14 +499,19 @@ void TextDSetFont(textDisp *textD, XFont
     highlightBGPixel = values.background;
     XGetGCValues(display, textD->lineNumGC, GCForeground, &values);
     lineNumFGPixel = values.foreground;
+    XGetGCValues(display, textD->wrapMarginGC, GCForeground, &values);
+    wrapMarginFGPixel = values.foreground;
+
     releaseGC(textD->w, textD->gc);
     releaseGC(textD->w, textD->selectGC);
     releaseGC(textD->w, textD->highlightGC);
     releaseGC(textD->w, textD->selectBGGC);
     releaseGC(textD->w, textD->highlightBGGC);
     releaseGC(textD->w, textD->lineNumGC);
+    releaseGC(textD->w, textD->wrapMarginGC);
     allocateFixedFontGCs(textD, fontStruct, bgPixel, fgPixel, selectFGPixel,
-            selectBGPixel, highlightFGPixel, highlightBGPixel, lineNumFGPixel);
+            selectBGPixel, highlightFGPixel, highlightBGPixel, lineNumFGPixel,
+            wrapMarginFGPixel);
     XSetFont(display, textD->styleGC, fontStruct->fid);
     
     /* Do a full resize to force recalculation of font related parameters */
@@ -653,6 +669,9 @@ void TextDRedisplayRect(textDisp *textD,
     /* draw the line numbers if exposed area includes them */
     if (textD->lineNumWidth != 0 && left <= textD->lineNumLeft + textD->lineNumWidth)
 	redrawLineNumbers(textD, False);
+
+    /* draw wrap margin incase it got erased */
+    drawWrapMargin(textD);
 }
 
 /*
@@ -707,20 +726,87 @@ void TextDRedisplayRange(textDisp *textD
     
     /* If the starting and ending lines are the same, redisplay the single
        line between "start" and "end" */
-    if (startLine == lastLine) {
-    	redisplayLine(textD, startLine, 0, INT_MAX, startIndex, endIndex);
-    	return;
+    if (startLine == lastLine)
+    {
+        redisplayLine(textD, startLine, 0, INT_MAX, startIndex, endIndex);
+
+        if (-1 != textD->lineStarts[startLine] ||
+            (BufCountDispChars(textD->buffer, textD->lineStarts[startLine],
+                               start) <= textD->wrapMargin &&
+             BufCountDispChars(textD->buffer, textD->lineStarts[startLine],
+                               end) >= textD->wrapMargin)) 
+        {
+            /* redraw wrap margin if it got erased */
+            drawWrapMargin(textD);
+        }
+    } else
+    {
+        /* Redisplay the first line from "start" */
+        redisplayLine(textD, startLine, 0, INT_MAX, startIndex, INT_MAX);
+        
+        /* Redisplay the lines in between at their full width */
+        for (i = startLine+1; i < lastLine; i++)
+        {
+            redisplayLine(textD, i, 0, INT_MAX, 0, INT_MAX);
+        }
+        
+        /* Redisplay the last line to "end" */
+        redisplayLine(textD, lastLine, 0, INT_MAX, 0, endIndex);
+
+        /*  redraw wrap margin  */
+        drawWrapMargin(textD);
     }
+}
+
+/*
+** Custom version of TextDRedisplayRange that only updates the area around
+** the cursor. 
+*/
+static void redisplayCursor(textDisp* textD)
+{
+    int cursorLine, lineStart, start, end, startIndex, endIndex;
+    int pos = textD->cursorPos;
+
+    /* If the cursor is outside of the displayed text, just return */
+    if (pos+1 < textD->firstChar || pos-1 > textD->lastChar)
+    {
+        return;
+    }
     
-    /* Redisplay the first line from "start" */
-    redisplayLine(textD, startLine, 0, INT_MAX, startIndex, INT_MAX);
+    if (!posToVisibleLineNum(textD, pos, &cursorLine))
+    {
+        /*  This can only be reached if redisplayCursor is miscalled.  */
+        /*  fprintf(stderr, "nedit: Unreachable code has been reached!\n");  */
+        return;
+    }
+       
+    /* Find the position right before and after the cursor, but make sure
+       that we stay on the same line. */
+    lineStart = TextDStartOfLine(textD, pos);
+    start = max(lineStart, pos - 1);
+    end = min(pos+1, TextDEndOfLine(textD, lineStart, True) + 1);
     
-    /* Redisplay the lines in between at their full width */
-    for (i=startLine+1; i<lastLine; i++)
-	redisplayLine(textD, i, 0, INT_MAX, 0, INT_MAX);
+    /* Get the starting and ending positions within the lines */
+    startIndex = start - lineStart;
+    endIndex = end - lineStart;
 
-    /* Redisplay the last line to "end" */
-    redisplayLine(textD, lastLine, 0, INT_MAX, 0, endIndex);
+    /* Reset the clipping rectangles for the drawing GCs which are shared
+       using XtAllocateGC, and may have changed since the last use */
+    resetClipRectangles(textD);
+    
+    redisplayLine(textD, cursorLine, 0, INT_MAX, startIndex, endIndex);
+
+    if (BufCountDispChars(textD->buffer, lineStart, end) < textD->wrapMargin)
+    {
+        return; /* Most common case */
+    }
+    
+    if (BufCountDispChars(textD->buffer, lineStart, start) > textD->wrapMargin)
+    {
+        return;
+    }
+
+    drawWrapMargin(textD);
 }
 
 /*
@@ -780,7 +866,7 @@ void TextDSetInsertPosition(textDisp *te
     /* draw it at its new position */
     textD->cursorPos = newPos;
     textD->cursorOn = True;
-    TextDRedisplayRange(textD, textD->cursorPos-1, textD->cursorPos + 1);
+    redisplayCursor(textD);
 }
 
 void TextDBlankCursor(textDisp *textD)
@@ -790,14 +876,14 @@ void TextDBlankCursor(textDisp *textD)
     
     blankCursorProtrusions(textD);
     textD->cursorOn = False;
-    TextDRedisplayRange(textD, textD->cursorPos-1, textD->cursorPos+1);
+    redisplayCursor(textD);
 }
 
 void TextDUnblankCursor(textDisp *textD)
 {
     if (!textD->cursorOn) {
     	textD->cursorOn = True;
-    	TextDRedisplayRange(textD, textD->cursorPos-1, textD->cursorPos+1);
+        redisplayCursor(textD);
     }
 }
 
@@ -806,7 +892,9 @@ void TextDSetCursorStyle(textDisp *textD
     textD->cursorStyle = style;
     blankCursorProtrusions(textD);
     if (textD->cursorOn)
-    	TextDRedisplayRange(textD, textD->cursorPos-1, textD->cursorPos + 1);
+    {
+        redisplayCursor(textD);
+    }
 }
 
 void TextDSetWrapMode(textDisp *textD, int wrap, int wrapMargin)
@@ -841,6 +929,15 @@ void TextDSetWrapMode(textDisp *textD, i
 	    textD->height);
 }
 
+void TextDSetShowWrapMargin(textDisp* textD, int state)
+{
+    textD->showWrapMargin = state;
+
+    /* Do a full redraw */
+    TextDRedisplayRect(textD, 0, textD->top, textD->width + textD->left,
+            textD->height);
+}
+
 int TextDGetInsertPosition(textDisp *textD)
 {
     return textD->cursorPos;
@@ -2684,7 +2781,7 @@ static void setScroll(textDisp *textD, i
                     textD->top, -xOffset, textD->height);
         }
         /* Restore protruding parts of the cursor */
-        TextDRedisplayRange(textD, textD->cursorPos-1, textD->cursorPos+1);
+        redisplayCursor(textD);
     }
     
     /* Refresh line number/calltip display if its up and we've scrolled 
@@ -2971,7 +3068,8 @@ static void blankCursorProtrusions(textD
 */
 static void allocateFixedFontGCs(textDisp *textD, XFontStruct *fontStruct,
         Pixel bgPixel, Pixel fgPixel, Pixel selectFGPixel, Pixel selectBGPixel,
-        Pixel highlightFGPixel, Pixel highlightBGPixel, Pixel lineNumFGPixel)
+        Pixel highlightFGPixel, Pixel highlightBGPixel, Pixel lineNumFGPixel,
+        Pixel wrapMarginFGPixel)
 {
     textD->gc = allocateGC(textD->w, GCFont | GCForeground | GCBackground,
             fgPixel, bgPixel, fontStruct->fid, GCClipMask, GCArcMode); 
@@ -2988,6 +3086,8 @@ static void allocateFixedFontGCs(textDis
     textD->lineNumGC = allocateGC(textD->w, GCFont | GCForeground | 
             GCBackground, lineNumFGPixel, bgPixel, fontStruct->fid, 
             GCClipMask, GCArcMode);
+    textD->wrapMarginGC = allocateGC(textD->w, GCForeground | GCBackground,
+            wrapMarginFGPixel, bgPixel, fontStruct->fid, GCClipMask, GCArcMode);
 }
 
 /*
@@ -3760,5 +3860,37 @@ void TextDSetupBGClasses(Widget w, XmStr
       }
       memcpy(*pp_bgClass, bgClass, 256);
       memcpy(*pp_bgClassPixel, bgClassPixel, class_no * sizeof (Pixel));
+    }
+}
+
+/*
+** Draw wrap margin if requested
+*/
+static void drawWrapMargin(textDisp *textD)
+{
+    int fromX, fromY;
+    int fontHeight, lineHeight;
+
+    /* attempt to draw the wrap margin # only if
+       - widget has been realized
+       - font width is not -1, ie. not using different/proportional width fonts 
+       - show wrap margins is enabled*/
+    if (XtWindow(textD->w) != 0
+            && textD->fixedFontWidth != -1
+            && textD->showWrapMargin)
+    {
+        fromX = textD->left + (textD->fixedFontWidth * textD->wrapMargin)
+                - textD->horizOffset - 1;
+
+        /* compute rest of coordinates if line is to right of left margin */
+        if (fromX > textD->left)
+        {
+            fromY = textD->top;
+            fontHeight = textD->ascent + textD->descent;
+            lineHeight = textD->nVisibleLines * fontHeight - 1;
+            XDrawLine(XtDisplay(textD->w), XtWindow(textD->w),
+                    textD->wrapMarginGC, fromX, fromY, fromX,
+                    fromY + lineHeight);
+        }
     }
 }
--- source/textDisp.h.orig	Wed Jul 21 13:32:05 2004
+++ source/textDisp.h		Sat Dec 23 16:25:53 2006
@@ -103,6 +103,7 @@ typedef struct _textDisp {
     int continuousWrap;     	    	/* Wrap long lines when displaying */
     int wrapMargin; 	    	    	/* Margin in # of char positions for
     	    	    	    	    	   wrapping in continuousWrap mode */
+    int showWrapMargin;                 /* draw line at wrap margin */
     int *lineStarts;
     int topLineNum;			/* Line number of top displayed line
     					   of file (first line of file is 1) */
@@ -150,6 +151,8 @@ typedef struct _textDisp {
     calltipStruct calltip;              /* The info for the calltip itself */
     Pixel calltipFGPixel;
     Pixel calltipBGPixel;
+    Pixel wrapMarginFGPixel;            /* color for drawing wrap margin */
+    GC wrapMarginGC;                    /* GC for drawing wrap margin */
     int suppressResync;			/* Suppress resynchronization of line
                                            starts during buffer updates */
     int nLinesDeleted;			/* Number of lines deleted during
@@ -169,8 +172,9 @@ textDisp *TextDCreate(Widget widget, Wid
 	XFontStruct *fontStruct, Pixel bgPixel, Pixel fgPixel,
 	Pixel selectFGPixel, Pixel selectBGPixel, Pixel highlightFGPixel,
 	Pixel highlightBGPixel, Pixel cursorFGPixel, Pixel lineNumFGPixel,
+        Pixel wrapMarginFGPixel, 
         int continuousWrap, int wrapMargin, XmString bgClassString, 
-        Pixel calltipFGPixel, Pixel calltipBGPixel);
+        Pixel calltipFGPixel, Pixel calltipBGPixel, int showWrapMargin);
 void TextDFree(textDisp *textD);
 void TextDSetBuffer(textDisp *textD, textBuffer *buffer);
 textBuffer *TextDGetBuffer(textDisp *textD);
@@ -179,7 +183,7 @@ void TextDAttachHighlightData(textDisp *
     	unfinishedStyleCBProc unfinishedHighlightCB, void *cbArg);
 void TextDSetColors(textDisp *textD, Pixel textFgP, Pixel textBgP,
         Pixel selectFgP, Pixel selectBgP, Pixel hiliteFgP, Pixel hiliteBgP, 
-        Pixel lineNoFgP, Pixel cursorFgP);
+        Pixel lineNoFgP, Pixel cursorFgP, Pixel wrapMarginFgP);
 void TextDSetFont(textDisp *textD, XFontStruct *fontStruct);
 int TextDMinFontWidth(textDisp *textD, Boolean considerStyles);
 int TextDMaxFontWidth(textDisp *textD, Boolean considerStyles);
@@ -212,6 +216,7 @@ void TextDBlankCursor(textDisp *textD);
 void TextDUnblankCursor(textDisp *textD);
 void TextDSetCursorStyle(textDisp *textD, int style);
 void TextDSetWrapMode(textDisp *textD, int wrap, int wrapMargin);
+void TextDSetShowWrapMargin(textDisp *textD, int state);
 int TextDEndOfLine(textDisp *textD, int pos, int startPosIsLineStart);
 int TextDStartOfLine(textDisp *textD, int pos);
 int TextDCountForwardNLines(textDisp *textD, int startPos, int nLines,
--- source/textP.h.orig	Wed Jul 21 13:32:05 2004
+++ source/textP.h			Sat Dec 23 16:25:53 2006
@@ -59,6 +59,7 @@ typedef struct _TextPart {
     /* resources */
     Pixel selectFGPixel, selectBGPixel, highlightFGPixel, highlightBGPixel;
     Pixel cursorFGPixel, lineNumFGPixel, calltipFGPixel, calltipBGPixel;
+    Pixel wrapMarginFGPixel;
     XFontStruct *fontStruct;
     Boolean pendingDelete;
     Boolean autoShowInsertPos;
@@ -75,6 +76,7 @@ typedef struct _TextPart {
     int marginWidth, marginHeight;
     int cursorBlinkRate;
     int wrapMargin;
+    Boolean showWrapMargin;
     int emulateTabs;
     int lineNumCols;
     char *delimiters;
--- source/text.c.orig	Wed Jul 28 20:02:03 2004
+++ source/text.c			Sat Dec 23 16:25:53 2006
@@ -641,6 +641,9 @@ static XtResource resources[] = {
     {textNcalltipBackground, textCcalltipBackground, XmRPixel,sizeof(Pixel),
       XtOffset(TextWidget, text.calltipBGPixel), XmRString, 
       NEDIT_DEFAULT_CALLTIP_BG},
+    {textNwrapMarginForeground, textCWrapMarginForeground, XmRPixel,sizeof(Pixel),
+      XtOffset(TextWidget, text.wrapMarginFGPixel), XmRString, 
+      NEDIT_DEFAULT_WRAPMARGIN_FG},
     {textNbacklightCharTypes,textCBacklightCharTypes,XmRString,sizeof(XmString),
       XtOffset(TextWidget, text.backlightCharTypes), XmRString, NULL},
     {textNrows, textCRows, XmRInt,sizeof(int),
@@ -671,6 +674,8 @@ static XtResource resources[] = {
       XtOffset(TextWidget, text.hidePointer), XmRString, "False"},
     {textNwrapMargin, textCWrapMargin, XmRInt, sizeof(int),
       XtOffset(TextWidget, text.wrapMargin), XmRString, "0"},
+    {textNshowWrapMargin, textCShowWrapMargin, XmRBoolean, sizeof(Boolean),
+      XtOffset(TextWidget, text.showWrapMargin), XmRString, "True"},
     {textNhScrollBar, textCHScrollBar, XmRWidget, sizeof(Widget),
       XtOffset(TextWidget, text.hScrollBar), XmRString, ""},
     {textNvScrollBar, textCVScrollBar, XmRWidget, sizeof(Widget),
@@ -820,10 +825,10 @@ static void initialize(TextWidget reques
 	    new->primitive.foreground, new->text.selectFGPixel,
 	    new->text.selectBGPixel, new->text.highlightFGPixel,
 	    new->text.highlightBGPixel, new->text.cursorFGPixel,
-	    new->text.lineNumFGPixel,
-          new->text.continuousWrap, new->text.wrapMargin,
-          new->text.backlightCharTypes, new->text.calltipFGPixel,
-          new->text.calltipBGPixel);
+            new->text.lineNumFGPixel, new->text.wrapMarginFGPixel,
+            new->text.continuousWrap, new->text.wrapMargin,
+            new->text.backlightCharTypes, new->text.calltipFGPixel,
+            new->text.calltipBGPixel, new->text.showWrapMargin);
 
     /* Add mandatory delimiters blank, tab, and newline to the list of
        delimiters.  The memory use scheme here is that new values are
@@ -1120,6 +1125,9 @@ static Boolean setValues(TextWidget curr
     	    new->text.continuousWrap != current->text.continuousWrap)
     	TextDSetWrapMode(current->text.textD, new->text.continuousWrap,
     	    	new->text.wrapMargin);
+    
+    if (new->text.showWrapMargin != current->text.showWrapMargin)
+        TextDSetShowWrapMargin(current->text.textD, new->text.showWrapMargin);
     
     /* When delimiters are changed, copy the memory, so that the caller
        doesn't have to manage it, and add mandatory delimiters blank,
--- source/text.h.orig	Wed Jul 21 13:32:05 2004
+++ source/text.h			Sat Dec 23 16:25:53 2006
@@ -61,6 +61,8 @@
 #define textCcalltipForeground "CalltipForeground"
 #define textNcalltipBackground "calltipBackground"
 #define textCcalltipBackground "CalltipBackground"
+#define textNwrapMarginForeground "wrapMarginForeground"
+#define textCWrapMarginForeground "WrapMarginForeground"
 #define textNpendingDelete "pendingDelete"
 #define textCPendingDelete "PendingDelete"
 #define textNhScrollBar "hScrollBar"
@@ -95,6 +97,8 @@
 #define textCContinuousWrap "ContinuousWrap"
 #define textNwrapMargin "wrapMargin"
 #define textCWrapMargin "WrapMargin"
+#define textNshowWrapMargin "showWrapMargin"
+#define textCShowWrapMargin "ShowWrapMargin"
 #define textNautoIndent "autoIndent"
 #define textCAutoIndent "AutoIndent"
 #define textNsmartIndent "smartIndent"
--- source/window.c.orig	Thu Sep 16 00:50:58 2004
+++ source/window.c			Sat Dec 23 16:32:16 2006
@@ -262,6 +262,7 @@ WindowInfo *CreateWindow(const char *nam
     window->autoSave = GetPrefAutoSave();
     window->saveOldVersion = GetPrefSaveOldVersion();
     window->wrapMode = GetPrefWrap(PLAIN_LANGUAGE_MODE);
+    window->showWrapMargin = GetPrefShowWrapMargin();
     window->overstrike = False;
     window->showMatchingStyle = GetPrefShowMatching();
     window->matchSyntaxBased = GetPrefMatchSyntaxBased();
@@ -701,8 +702,9 @@ WindowInfo *CreateWindow(const char *nam
               GetPrefColorName(HILITE_FG_COLOR),
               GetPrefColorName(HILITE_BG_COLOR),
               GetPrefColorName(LINENO_FG_COLOR),
-              GetPrefColorName(CURSOR_FG_COLOR));
-    
+              GetPrefColorName(CURSOR_FG_COLOR),
+              GetPrefColorName(WRAPMARGIN_FG_COLOR));
+
     /* Create the right button popup menu (note: order is important here,
        since the translation for popping up this menu was probably already
        added in createTextArea, but CreateBGMenu requires window->textArea
@@ -1172,7 +1174,7 @@ void SplitPane(WindowInfo *window)
     TextDSetColors( newTextD, textD->fgPixel, textD->bgPixel, 
             textD->selectFGPixel, textD->selectBGPixel, textD->highlightFGPixel,
             textD->highlightBGPixel, textD->lineNumFGPixel, 
-            textD->cursorFGPixel );
+            textD->cursorFGPixel, textD->wrapMarginFGPixel);
     
     /* Set the minimum pane height in the new pane */
     UpdateMinPaneHeights(window);
@@ -1810,7 +1812,8 @@ void SetFonts(WindowInfo *window, const 
 
 void SetColors(WindowInfo *window, const char *textFg, const char *textBg,
         const char *selectFg, const char *selectBg, const char *hiliteFg, 
-        const char *hiliteBg, const char *lineNoFg, const char *cursorFg)
+        const char *hiliteBg, const char *lineNoFg, const char *cursorFg,
+        const char* wrapMarginFg)
 {
     int i, dummy;
     Pixel   textFgPix   = AllocColor( window->textArea, textFg, 
@@ -1828,6 +1831,8 @@ void SetColors(WindowInfo *window, const
             lineNoFgPix = AllocColor( window->textArea, lineNoFg, 
                     &dummy, &dummy, &dummy),
             cursorFgPix = AllocColor( window->textArea, cursorFg, 
+                    &dummy, &dummy, &dummy),
+            wrapMarginFgPix = AllocColor( window->textArea, wrapMarginFg, 
                     &dummy, &dummy, &dummy);
     textDisp *textD;
 
@@ -1838,7 +1843,8 @@ void SetColors(WindowInfo *window, const
             NULL);
     textD = ((TextWidget)window->textArea)->text.textD;
     TextDSetColors( textD, textFgPix, textBgPix, selectFgPix, selectBgPix, 
-            hiliteFgPix, hiliteBgPix, lineNoFgPix, cursorFgPix );
+            hiliteFgPix, hiliteBgPix, lineNoFgPix, cursorFgPix,
+            wrapMarginFgPix);
     /* Update any additional panes */
     for (i=0; i<window->nPanes; i++) {
         XtVaSetValues(window->textPanes[i],
@@ -1847,7 +1853,8 @@ void SetColors(WindowInfo *window, const
                 NULL);
         textD = ((TextWidget)window->textPanes[i])->text.textD;
         TextDSetColors( textD, textFgPix, textBgPix, selectFgPix, selectBgPix, 
-                hiliteFgPix, hiliteBgPix, lineNoFgPix, cursorFgPix );
+                hiliteFgPix, hiliteBgPix, lineNoFgPix, cursorFgPix,
+                wrapMarginFgPix);
     }
     
     /* Redo any syntax highlighting */
@@ -1883,11 +1890,14 @@ void SetAutoWrap(WindowInfo *window, int
                 textNcontinuousWrap, contWrap, NULL);
     window->wrapMode = state;
     
-    if (IsTopDocument(window)) {
-	XmToggleButtonSetState(window->newlineWrapItem, autoWrap, False);
-	XmToggleButtonSetState(window->continuousWrapItem, contWrap, False);
-	XmToggleButtonSetState(window->noWrapItem, state == NO_WRAP, False);
+    if (IsTopDocument(window))
+    {
+        XmToggleButtonSetState(window->newlineWrapItem, autoWrap, False);
+        XmToggleButtonSetState(window->continuousWrapItem, contWrap, False);
+        XmToggleButtonSetState(window->noWrapItem, state == NO_WRAP, False);
     }
+
+    SetShowWrapMargin(window, window->showWrapMargin);
 }
 
 /*
@@ -1915,6 +1925,31 @@ void SetAutoScroll(WindowInfo *window, i
 }
     
 /*
+** Set show-wrap-margin style, one of NEVER, ALWAYS, ON-WRAP
+*/
+void SetShowWrapMargin(WindowInfo *window, int state)
+{
+    int i;
+    int alwaysShowWrapMargin = (state == SHOW_WRAP_MARGIN_ALWAYS);
+    int onWrapShowWrapMargin = (state == SHOW_WRAP_MARGIN_ON_WRAP);
+    int autoWrap = (window->wrapMode == NEWLINE_WRAP);
+    int contWrap = (window->wrapMode == CONTINUOUS_WRAP);
+    /* true or false only. not never/always/on-wrap. text widget does need to know
+       exact details. it just needs to know that wrap margin should be shown */
+    int showWrapMargin = (alwaysShowWrapMargin ||
+            (onWrapShowWrapMargin && autoWrap) ||
+            (onWrapShowWrapMargin && contWrap) );
+
+    XtVaSetValues(window->textArea, textNshowWrapMargin, showWrapMargin, NULL);
+    for (i=0; i<window->nPanes; i++)
+        XtVaSetValues(window->textPanes[i], 
+                textNshowWrapMargin, showWrapMargin, NULL);
+    /* never/always/on-wrap needs to be stored in window settings, for generating
+       menu, etc */
+    window->showWrapMargin = state;
+}
+
+/*
 ** Recover the window pointer from any widget in the window, by searching
 ** up the widget hierarcy for the top level container widget where the
 ** window pointer is stored in the userData field. In a tabbed window,
@@ -2192,6 +2227,15 @@ static Widget createTextArea(Widget pare
         int lineNumCols)
 {
     Widget text, sw, hScrollBar, vScrollBar, frame;
+    int alwaysShowWrapMargin = (window->showWrapMargin == SHOW_WRAP_MARGIN_ALWAYS);
+    int onWrapShowWrapMargin = (window->showWrapMargin == SHOW_WRAP_MARGIN_ON_WRAP);
+    int autoWrap = (window->wrapMode == NEWLINE_WRAP);
+    int contWrap = (window->wrapMode == CONTINUOUS_WRAP);
+    /* true or false only. not never/always/on-wrap. text widget does need to know
+       exact details. it just needs to know that wrap margin should be shown */
+    int showWrapMargin = (alwaysShowWrapMargin ||
+            (onWrapShowWrapMargin && autoWrap) ||
+            (onWrapShowWrapMargin && contWrap) );
         
     /* Create a text widget inside of a scrolled window widget */
     sw = XtVaCreateManagedWidget("scrolledW", xmScrolledWindowWidgetClass,
@@ -2215,6 +2259,7 @@ static Widget createTextArea(Widget pare
             textNreadOnly, IS_ANY_LOCKED(window->lockReasons),
             textNwordDelimiters, delimiters,
             textNwrapMargin, wrapMargin,
+            textNshowWrapMargin, showWrapMargin,
             textNautoIndent, window->indentStyle == AUTO_INDENT,
             textNsmartIndent, window->indentStyle == SMART_INDENT,
             textNautoWrap, window->wrapMode == NEWLINE_WRAP,
@@ -3210,6 +3255,7 @@ WindowInfo *CreateDocument(WindowInfo *s
     window->autoSave = GetPrefAutoSave();
     window->saveOldVersion = GetPrefSaveOldVersion();
     window->wrapMode = GetPrefWrap(PLAIN_LANGUAGE_MODE);
+    window->showWrapMargin = GetPrefShowWrapMargin();
     window->overstrike = False;
     window->showMatchingStyle = GetPrefShowMatching();
     window->matchSyntaxBased = GetPrefMatchSyntaxBased();
@@ -3312,7 +3358,8 @@ WindowInfo *CreateDocument(WindowInfo *s
               GetPrefColorName(HILITE_FG_COLOR),
               GetPrefColorName(HILITE_BG_COLOR),
               GetPrefColorName(LINENO_FG_COLOR),
-              GetPrefColorName(CURSOR_FG_COLOR));
+              GetPrefColorName(CURSOR_FG_COLOR),
+              GetPrefColorName(WRAPMARGIN_FG_COLOR));
     
     /* Create the right button popup menu (note: order is important here,
        since the translation for popping up this menu was probably already
@@ -4073,7 +4120,7 @@ static void cloneTextPanes(WindowInfo *w
             TextDSetColors(newTextD, textD->fgPixel, textD->bgPixel, 
                     textD->selectFGPixel, textD->selectBGPixel,
                     textD->highlightFGPixel,textD->highlightBGPixel,
-                    textD->lineNumFGPixel, textD->cursorFGPixel);
+                    textD->lineNumFGPixel, textD->cursorFGPixel, textD->wrapMarginFGPixel);
 	}
         
 	/* Set the minimum pane height in the new pane */
--- source/window.h.orig	Fri Aug 20 21:33:21 2004
+++ source/window.h			Sat Dec 23 16:25:53 2006
@@ -56,10 +56,12 @@ void SetFonts(WindowInfo *window, const 
 	const char *boldName, const char *boldItalicName);
 void SetColors(WindowInfo *window, const char *textFg, const char *textBg,  
         const char *selectFg, const char *selectBg, const char *hiliteFg, 
-        const char *hiliteBg, const char *lineNoFg, const char *cursorFg);
+        const char *hiliteBg, const char *lineNoFg, const char *cursorFg,
+        const char* wrapMarginFg);
 void SetOverstrike(WindowInfo *window, int overstrike);
 void SetAutoWrap(WindowInfo *window, int state);
 void SetWrapMargin(WindowInfo *window, int margin);
+void SetShowWrapMargin(WindowInfo *window, int state);
 void SetAutoScroll(WindowInfo *window, int margin);
 void SplitPane(WindowInfo *window);
 Widget GetPaneByIndex(WindowInfo *window, int paneIndex);
--- util/prefFile.c.orig	Wed Jul 21 13:32:07 2004
+++ util/prefFile.c			Sat Dec 23 16:25:53 2006
@@ -304,7 +304,7 @@ int SavePreferences(Display *display, co
 static int stringToPref(const char *string, PrefDescripRec *rsrcDescrip)
 {
     int i;
-    char *cleanStr, *endPtr, **enumStrings;
+    char *cleanStr, *endPtr, **enumStrings, *cleanEnumStr;
     
     switch (rsrcDescrip->dataType) {
       case PREF_INT:
@@ -340,18 +340,21 @@ static int stringToPref(const char *stri
       	*(int *)rsrcDescrip->valueAddr = False;
     	return False;
       case PREF_ENUM:
-      	cleanStr = removeWhiteSpace(string);
-      	enumStrings = (char **)rsrcDescrip->arg;
-      	for (i=0; enumStrings[i]!=NULL; i++) {
-      	    if (!strcmp(enumStrings[i], cleanStr)) {
-      	    	*(int *)rsrcDescrip->valueAddr = i;
-      	    	XtFree(cleanStr);
-      	    	return True;
-      	    }
-      	}
-      	XtFree(cleanStr);
-      	*(int *)rsrcDescrip->valueAddr = 0;
-    	return False;
+        cleanStr = removeWhiteSpace(string);
+        enumStrings = (char **)rsrcDescrip->arg;
+        for (i=0; enumStrings[i]!=NULL; i++) {
+            cleanEnumStr = removeWhiteSpace(enumStrings[i]);
+            if (!strcmp(cleanEnumStr, cleanStr)) {
+                *(int *)rsrcDescrip->valueAddr = i;
+                XtFree(cleanStr);
+                XtFree(cleanEnumStr);
+                return True;
+            }
+            XtFree(cleanEnumStr);
+        }
+        XtFree(cleanStr);
+        *(int *)rsrcDescrip->valueAddr = 0;
+        return False;
       case PREF_STRING:
 	if ((int)strlen(string) >= (int)rsrcDescrip->arg)
       	    return False;
