Monday, November 08, 2010 at 12:03 AM.

on saveAsPlainText (adr=nil, adrFilepath=nil) {
	<<Save as plain text. Remember that this document is a plain text document -- always save it as plain text.
		<<12/15/00; 5:04:18 PM by PBS
			<<12/29/00; 2:55:43 PM by PBS
				<<Save non file-based outlines -- such as the notepad -- as plain text.
	if adr == nil {
		adr = window.frontMost ()};
	bundle { //run callbacks
		on runCallbacks (adrtable) {
			local (flConsumed = false);
			if defined (adrtable^) {
				local (adrcallback);
				for adrcallback in adrtable {
					try { //11/30/00 JES
						while typeOf (adrcallback^) == addressType {
							adrcallback = adrcallback^};
						flConsumed = adrcallback^ (adr)};
					if flConsumed {
						return (true)}}};
			return (false)};
		if runCallbacks ( {
			return (true)};
		if system.environment.isRadio {
			if defined (user.pike.commandCallbacks.saveAsPlainText) {
				if runCallbacks (@user.pike.commandCallbacks.saveAsPlainText) {
					return (true)}}}};
	local (adrType);
	bundle { //if there's a windowType that handles this command, call it
		if (adradrtable:@adrtype, adrwindow:adr) {
			if defined (adrtype^.saveAsPlainText) {
				if adrtype^.saveAsPlainText (adr, adrFilepath) {
					return (false)}}}};
	case typeOf (adr^) { //we only know how to render outlines and tables at this time
		outlineType {
			local (adrtype);
			if ("outlinerFile", @adrtype) {
				if defined (adrtype^.saveAsPlainText) {
					if not adrtype^.saveAsPlainText (adr) {
						return (false)}}}}};
			<<bundle //old code
				<<local (adrParentTable = parentOf (adrItem^))
				<<local (title = window.getTitle (adrItem))
				<<if not window.attributes.getOne ("title", @title, adrItem) //base the filename on the window title
					<<local (l = string.parseAddress (adrItem))
					<<local (ct = sizeOf (l))
					<<if ct > 1
						<<title = l[ct-1] + "." + l[ct]
						<<title = l[ct]
					<<if system.environment.isMac //shorten the filename
						<<if sizeOf (title) > 27
							<<title = l[ct]
						<<if sizeOf (title) > 27
							<<title = string.mid (title, 1, 27)
				<<if string.lower (title) endsWith ".opml"
					<<title = string.popSuffix (title)
				<<if string.lower (title) endsWith ".txt"
					<<title = string.popSuffix (title)
				<<title = title + ".txt"
				<<local (fname = title)
				<<local (f = fname)
				<<if file.putFileDialog ("Save as plain text:", @f)
					<<pike.saveAsPlainText (adrItem, f)
					<<local (s = string (adrItem^))
					<<if system.environment.isWindows //add linefeeds
						<<s = string.replaceAll (s, "\r", "\r\n")
					<<local (creator = 'Radu')
					<<if string.lower (f) endsWith ".py" //special case for Python scripts
						<<creator = 'Pyth'
					<<file.writeWholeFile (f, s, 'TEXT', creator, ())
					<<bundle //old code fragment
						<<local (adrTable = parentOf (adrOutline^))
						<<adrTable^.title = file.fileFromPath (f)
						<<window.setTitle (adrOutline, adrTable^.title)
						<<adrTable^.f = f
						<<adrTable^.lastSaved = ()
						<<adrTable^.flOutlineDocument = false
						<<adrTable^.flPlainText = true
					<<return ( (f))
	<<else //not a local, file-based outline, but something like the notepad
		<<local (title = window.getTitle (adrItem))
		<<local (adrTable = pike.newWindow (title, false, false))
		<<adrTable^.outline = adrItem^
		<<window.getPosition (adrItem, @horiz, @vert)
		<<edit (@adrTable^.outline, windowTitle:title)
		<<window.setPosition (@adrTable^.outline, horiz + 15, vert + 15)
		<< (@adrTable^.outline)
	if adrFilepath != nil { //tell the caller where the file is -- used for View in Browser
		adrFilepath^ = f};
	return (true)};

bundle { //debugging
	saveAsPlainText (window.frontmost ())}

This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.