# http://www.nr.no/~joachim/Niki/index.php/ParagraphFilling # # Better fill_paragraph respecting mail-quotings. It replaces Ctrl-J. The two # following subroutines jf_wrapquote and jf_para_rewrap are needed. # Changes by Volker Kuhlmann, 5 Jan 2004: # - Turned the outer code into a function so it can be called easily from a # macro menu shortcut without having to paste heaps # - Added an optional parameter to ParagraphFilling() which is a string of # characters to be recognised as constant line prefix. # The default is "\\>\\|\\}\\s", for LaTeX you want to add a %. # # Bugs: # - When running this on selected lines, one empty line following the # selection is removed. # - The balancing algorithm is inferior to the builtin ^J. define ParagraphFilling { if ($n_args == 0) { $par_fill_prefix_chars = "\\>\\|\\}\\s" } else { $par_fill_prefix_chars = $1 } if ($selection_start==-1) { pos=$cursor if ($column == 0) forward_character() backward_paragraph() start = $cursor forward_paragraph() end = $cursor set_cursor_pos(pos) replace_range(start, end, jf_wrapquote(get_range(start, end), $wrap_margin)) set_cursor_pos(pos) } else { # test for rectangle if ($selection_left == -1) { # no rectangle replace_selection(jf_wrapquote(get_selection(), $wrap_margin)) } else { # rectangular selection width = $selection_right - $selection_left set_cursor_pos($selection_end) select_rectangle($selection_start, $selection_end,\ $selection_left, 10 * $wrap_margin) replace_selection(jf_wrapquote(get_selection(), width)) } } } # Rewrapping quotings in e-mail define jf_wrapquote { part = $1 margin = $2 #quote_pats = "\\n[\\>\\|\\}\\s]*[^\\>\\|\\}\\s]" quote_pats = "\\n["$par_fill_prefix_chars"]*[^"$par_fill_prefix_chars"]" part = split(part, "\n\n") for (i = 0; i < part[]; i++) { wrapped[i] = "" # get first quote first = search_string(part[i], "["$par_fill_prefix_chars"]+", 0, "regex") if (first == -1 || first != 0) { # this means no quoting (and no indenation) first_quote = "" } else { first_quote = substring(part[i], 0, $search_end) part[i] = substring(part[i], $search_end, length(part[i])) } # look for next quote found = search_string(part[i], quote_pats, 0, "regex") while (found != -1) { eof_quote = $search_end - 1 next_quote = substring(part[i], found + 1, eof_quote) if (first_quote != next_quote) { text = jf_para_rewrap(substring(part[i], 0, found), margin, first_quote) part[i] = substring(part[i], eof_quote, length(part[i])) first_quote = next_quote wrapped[i] = wrapped[i] text "\n" found = search_string(part[i], quote_pats, 0, "regex") } else { found = search_string(part[i], quote_pats, eof_quote++, "regex") } } if (part[i] != "") { wrapped[i] = wrapped[i] jf_para_rewrap(part[i], margin, first_quote) } } out = "" for (i = 0; i < part[] - 1; i++) { out = out wrapped[i] "\n\n" } out = out wrapped[(part[] - 1)] return(out) } # simple rewrapping of paragraphs # basic algorithm like the one in NEdit's fillParagraph (shift.c) # takes the string to rewrap, the margin and optionally a separator # default separator is \n of course define jf_para_rewrap { if ($n_args >= 2) { para_in = $1 margin = $2 } else { dialog("jf_para_rewrap: too few arguments") return ("") } separator = "" if ($n_args >= 3) { separator = $3 } #prepare input string first = search_string(para_in, separator, 0) if (first == 0) { para_in = substring(para_in, $search_end, length(para_in)) } para_in = replace_in_string(para_in, "\n" separator, " ", "copy") para_in = replace_in_string(para_in, "(?n\\s+)", " ", "regex", "copy") para_in = separator para_in # calculate rewrapped output para_out = "" while (length(para_in) > margin) { space = search_string(para_in, " ", margin, "backward") if (space != -1) { para_out = para_out substring(para_in, 0, space) "\n" para_in = separator substring(para_in, space + 1, length(para_in)) } else { dialog("jf_para_rewrap: doesn't fit into margin") return("") } } para_out = para_out para_in #"\n" return(para_out) } # Last edited on Saturday, December 27, 2003 7:43:13 pm.