maxChars = len;
                }
                result[i].shown = result[i].real;
+               if(SFpathFlag) { // [HGM] only show directories
+                       struct stat statBuf;
+                       if (stat(str, &statBuf) || SFstatChar(&statBuf) != '/') continue;
+               }
                i++;
        }
 
 
 
 int (*SFfunc)();
 
+Boolean SFpathFlag; // [HGM]
+
 static char *oneLineTextEditTranslations = "\
        <Key>Return:    redraw-display()\n\
        Ctrl<Key>M:     redraw-display()\n\
                cancel = "Cancel";
        }
 
+       if(SFpathFlag != (mode && mode[0] == 'p')) { // [HGM] ignore everything that is not a directory
+               if(SFdirs) XtFree(SFdirs);
+               SFdirs = NULL; // kludge to throw away all cached info
+               SFpathFlag = !SFpathFlag;
+       }
+
        if (firstTime) {
                firstTime = 0;
                SFdisplay = XtDisplay(toplevel);
                        break;
                case SEL_FILE_OK:
                        *name_return = SFgetText();
+                       if(mode && (mode[0] == 'p' || mode[0] == 'f')) { // [HGM] for use in file-option browse button
+                               SFprepareToReturn();
+                               return stderr;
+                       }
                        if (fp = SFopenFile(*name_return, mode,
                                            prompt, failed)) {
                                SFprepareToReturn();