def capitalizesentence(s): cap = True r = '' letters = flauxtext.string.letters for c in s: if c in '.!?': r += c cap = True elif cap and (c in letters): r += c.upper() cap = False else: r += c return r def sort(s, r=False): L = flauxtext.re.split('\r?\n?', s) L.sort() if r: L.reverse() return (('\r' in s) and '\r\n' or '\n').join(L) def spaceselected(s): r = '' opts = ['', flauxtext.spaceselectedinsertstring] for c in s: r += c + opts[c == '\n'] return r def stripspaceselected(s): """filter "\n " or "\r\t" or "\r " or "\n\t" down to "\n" or "\r".""" r = p = '' for c in s: if not (p and (p in '\r\n') and (c in ' \t')): r += c p = c return r def translate(s): d = flauxtext.gtk.Dialog("translate chars", self.w, flauxtext.gtk.DIALOG_NO_SEPARATOR | flauxtext.gtk.DIALOG_DESTROY_WITH_PARENT, (flauxtext.gtk.STOCK_OK, 1, flauxtext.gtk.STOCK_CANCEL, 2)) tbl = flauxtext.gtk.Table(4, 2) d.vbox.pack_start(tbl) ls = ["from chars", "to char", "delete chars"] for lblt in range(len(ls)): tbl.attach(flauxtext.gtk.Label(ls[lblt]) , 0, 1, lblt, lblt + 1) frm = flauxtext.gtk.Entry() tbl.attach(frm, 1, 2, 0, 1) tot = flauxtext.gtk.Entry() tbl.attach(tot, 1, 2, 1, 2) deltns = flauxtext.gtk.Entry() tbl.attach(deltns, 1, 2, 2, 3) tbl.attach(flauxtext.gtk.Label("replaces each char in 'from' with its corresponding char in 'to'," +"\nAFTER deleting all instances of all chars in 'del'.\n'from' must be exactly as long as 'to'." +"\nif you've selected any text, this only operates on the selected text." +"\n\nfyi, this uses string.translate(text,string.maketrans('from','to'),'deltns')"), 0, 2, 3, 4) d.show_all() if d.run(): return s.translate(flauxtext.string.maketrans(frm.get_text(), tot.get_text()), deltns.get_text()) else: return s d.destroy() def adjustindentation(text): d = self.mk_dialog('ratio?', (flauxtext.gtk.STOCK_OK, 1, flauxtext.gtk.STOCK_CANCEL, 0)) d.vbox.pack_start(flauxtext.gtk.Label('ratio of current spaces to desired spaces beginning each [selected] line:')) e = flauxtext.gtk.SpinButton(flauxtext.gtk.Adjustment(4, 0, 1e3, 1./30., 1, 1./30.), 0, 2) e.connect('activate', lambda e_: d.response(1)) d.vbox.pack_start(e) if d.run(): reptext = '' ratio = e.get_value() print len(text), ratio newline = ['', '\r']['\r' in text] + '\n' for lin in flauxtext.re.split('\r?\n?', text): j = len(lin) - len(lin.lstrip()) reptext += (' ' * int(ratio * j)) + lin[j:] + newline return reptext[:-len(newline)] else: return text def commentout(s, uncomment=False): ins = followtag = reptext = '' fn = self.current_filename() for ptrn, chrs in flauxtext.commentchars.items(): if flauxtext.fnmatch.fnmatch(fn, ptrn): if isinstance(chrs, str): ins=chrs elif isinstance(chrs, (list, tuple)) and (len(chrs) == 2): ins=chrs[0] followtag=chrs[1] else: self.flash_info(chrs+" is not a valid set of comment chars. please adjust 'commentchars' in config.flauxtext.") return s # now that we know what chars indicate comments, we iterate through the text, creating reptext if uncomment: p = '' for i in s: if not (p and (p in '\r\n') and (c in ' \t')): reptext += s[i] p = c else: opts = ['', ins] for c in s: reptext += c + opts[c == '\n'] reptext += followtag return reptext def htmlunescape(text): reptext = '' for tkn in text.split('&'): if text.startswith(tkn) or (text.startswith('&'+tkn) and not tkn[0]=='#'): reptext += tkn elif (tkn[0] == '#') and (2 < tkn.index(';') < 5): try: reptext += chr(int(tkn[1:tkn.index(';')])) + tkn[tkn.index(';')+1:] except ValueError: reptext += '&' + tkn else: reptext += '&' + tkn return reptext def htmltotext(s): flauxtext.imp0rt('formatter', 'htmllib', 'StringIO') tempbufr = flauxtext.StringIO.StringIO() flauxtext.htmllib.HTMLParser(flauxtext.formatter.AbstractFormatter(flauxtext.formatter.DumbWriter(tempbufr))).feed(a) return tempbufr.getvalue() def charreverse(s): L = list(s) L.reverse() return ''.join(L) def linereverse(s): L = flauxtext.re.split('\r?\n?', s) L.reverse() return ['\n', '\r\n']['\r' in s].join(L) swapcase = str.swapcase lower = str.lower rot13 = lambda s: s.translate(flauxtext.string.maketrans('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM')) rot47 = lambda s: s.translate(flauxtext.string.maketrans(''.join(map(chr, range(33, 127))), ''.join(map(chr, range(80, 127) + range(33, 80))))) atbash26 = lambda s: s.translate(flauxtext.string.maketrans(string.lowercase + string.uppercase, 'zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA')) tabsto4spaces = lambda s: (' '*4).join(s.split('\t')) tabsto8spaces = lambda s: (' '*8).join(s.split('\t')) spacestotabs = lambda s: '\t'.join(s.split(' '*4)) uncommentout = lambda s: commentout(s, True) htmlescape = lambda text: ''.join([('&#%d;' % ord(char)) for char in text]) crlf_lf = lambda s: s.replace(['\n', '\r\n']['\r\n' in s], ['\r\n', '\n']['\r\n' in s]) actions = globals().keys() macros = { 'unhilite': 'txtb.remove_tag_by_name("becky", starts, ends)', 'hilite': 'txtb.apply_tag_by_name("becky", starts, ends)', } actions += macros.keys() ################################################################################ def editselected(action, *a, **kw): txtb = self.current_buffer() try: starts, ends = txtb.get_selection_bounds() if starts.get_offset() > ends.get_offset(): starts, ends = ends, starts except ValueError: starts, ends = txtb.get_start_iter(), txtb.get_end_iter() text = txtb.get_text(starts, ends) if isinstance(action, (str, unicode)): dowhat = str(action) elif isinstance(action, flauxtext.gtk.Action): dowhat = action.get_name() else: self.flash_info('invalid action:' + repr(action)) return reptext = text if dowhat in globals(): reptext = eval(dowhat)(text) elif dowhat in macros: exec macros[dowhat] in locals() else: self.flash_info('sorry, i forgot how to do that. :-/', 2e3) if reptext != text: begin = starts.get_offset() txtb.delete(starts, ends) txtb.insert(txtb.get_iter_at_offset(begin),str(reptext)) txtb.select_range(txtb.get_iter_at_offset(begin),txtb.get_iter_at_offset(begin+len(reptext)))