Erlang -- prettypr

Syntax_Tools

Reference Manual

Version 2.1.6

- User's Guide
- Reference Manual
- Release Notes
- PDF
- Top

- Expand All
- Contract All

### Table of Contents

- epp_dodger
  - Top of manual page
  - parse/1
  - parse/2
  - parse/3
  - parse_file/1
  - parse_file/2
  - parse_form/2
  - parse_form/3
  - quick_parse/1
  - quick_parse/2
  - quick_parse/3
  - quick_parse_file/1
  - quick_parse_file/2
  - quick_parse_form/2
  - quick_parse_form/3
  - tokens_to_string/1
- erl_comment_scan
  - Top of manual page
  - file/1
  - join_lines/1
  - scan_lines/1
  - string/1
- erl_prettypr
  - Top of manual page
  - best/1
  - best/2
  - format/1
  - format/2
  - get_ctxt_hook/1
  - get_ctxt_linewidth/1
  - get_ctxt_paperwidth/1
  - get_ctxt_precedence/1
  - get_ctxt_user/1
  - layout/1
  - layout/2
  - set_ctxt_hook/2
  - set_ctxt_linewidth/2
  - set_ctxt_paperwidth/2
  - set_ctxt_precedence/2
  - set_ctxt_user/2
- erl_recomment
  - Top of manual page
  - quick_recomment_forms/2
  - recomment_forms/2
  - recomment_tree/2
- erl_syntax
  - Top of manual page
  - abstract/1
  - add_ann/2
  - add_postcomments/2
  - add_precomments/2
  - annotated_type/2
  - annotated_type_body/1
  - annotated_type_name/1
  - application/2
  - application/3
  - application_arguments/1
  - application_operator/1
  - arity_qualifier/2
  - arity_qualifier_argument/1
  - arity_qualifier_body/1
  - atom/1
  - atom_literal/1
  - atom_literal/2
  - atom_name/1
  - atom_value/1
  - attribute/1
  - attribute/2
  - attribute_arguments/1
  - attribute_name/1
  - binary/1
  - binary_comp/2
  - binary_comp_body/1
  - binary_comp_template/1
  - binary_field/1
  - binary_field/2
  - binary_field/3
  - binary_field_body/1
  - binary_field_size/1
  - binary_field_types/1
  - binary_fields/1
  - binary_generator/2
  - binary_generator_body/1
  - binary_generator_pattern/1
  - bitstring_type/2
  - bitstring_type_m/1
  - bitstring_type_n/1
  - block_expr/1
  - block_expr_body/1
  - case_expr/2
  - case_expr_argument/1
  - case_expr_clauses/1
  - catch_expr/1
  - catch_expr_body/1
  - char/1
  - char_literal/1
  - char_literal/2
  - char_value/1
  - class_qualifier/2
  - class_qualifier/3
  - class_qualifier_argument/1
  - class_qualifier_body/1
  - class_qualifier_stacktrace/1
  - clause/2
  - clause/3
  - clause_body/1
  - clause_guard/1
  - clause_patterns/1
  - comment/1
  - comment/2
  - comment_padding/1
  - comment_text/1
  - compact_list/1
  - concrete/1
  - cond_expr/1
  - cond_expr_clauses/1
  - conjunction/1
  - conjunction_body/1
  - cons/2
  - constrained_function_type/2
  - constrained_function_type_argument/1
  - constrained_function_type_body/1
  - constraint/2
  - constraint_argument/1
  - constraint_body/1
  - copy_ann/2
  - copy_attrs/2
  - copy_comments/2
  - copy_pos/2
  - data/1
  - disjunction/1
  - disjunction_body/1
  - eof_marker/0
  - error_marker/1
  - error_marker_info/1
  - flatten_form_list/1
  - float/1
  - float_literal/1
  - float_value/1
  - form_list/1
  - form_list_elements/1
  - fun_expr/1
  - fun_expr_arity/1
  - fun_expr_clauses/1
  - fun_type/0
  - function/2
  - function_arity/1
  - function_clauses/1
  - function_name/1
  - function_type/1
  - function_type/2
  - function_type_arguments/1
  - function_type_return/1
  - generator/2
  - generator_body/1
  - generator_pattern/1
  - get_ann/1
  - get_attrs/1
  - get_pos/1
  - get_postcomments/1
  - get_precomments/1
  - has_comments/1
  - if_expr/1
  - if_expr_clauses/1
  - implicit_fun/1
  - implicit_fun/2
  - implicit_fun/3
  - implicit_fun_name/1
  - infix_expr/3
  - infix_expr_left/1
  - infix_expr_operator/1
  - infix_expr_right/1
  - integer/1
  - integer_literal/1
  - integer_range_type/2
  - integer_range_type_high/1
  - integer_range_type_low/1
  - integer_value/1
  - is_atom/2
  - is_char/2
  - is_form/1
  - is_integer/2
  - is_leaf/1
  - is_list_skeleton/1
  - is_literal/1
  - is_proper_list/1
  - is_string/2
  - is_tree/1
  - join_comments/2
  - list/1
  - list/2
  - list_comp/2
  - list_comp_body/1
  - list_comp_template/1
  - list_elements/1
  - list_head/1
  - list_length/1
  - list_prefix/1
  - list_suffix/1
  - list_tail/1
  - macro/1
  - macro/2
  - macro_arguments/1
  - macro_name/1
  - make_tree/2
  - map_expr/1
  - map_expr/2
  - map_expr_argument/1
  - map_expr_fields/1
  - map_field_assoc/2
  - map_field_assoc_name/1
  - map_field_assoc_value/1
  - map_field_exact/2
  - map_field_exact_name/1
  - map_field_exact_value/1
  - map_type/0
  - map_type/1
  - map_type_assoc/2
  - map_type_assoc_name/1
  - map_type_assoc_value/1
  - map_type_exact/2
  - map_type_exact_name/1
  - map_type_exact_value/1
  - map_type_fields/1
  - match_expr/2
  - match_expr_body/1
  - match_expr_pattern/1
  - meta/1
  - module_qualifier/2
  - module_qualifier_argument/1
  - module_qualifier_body/1
  - named_fun_expr/2
  - named_fun_expr_arity/1
  - named_fun_expr_clauses/1
  - named_fun_expr_name/1
  - nil/0
  - normalize_list/1
  - operator/1
  - operator_literal/1
  - operator_name/1
  - parentheses/1
  - parentheses_body/1
  - prefix_expr/2
  - prefix_expr_argument/1
  - prefix_expr_operator/1
  - receive_expr/1
  - receive_expr/3
  - receive_expr_action/1
  - receive_expr_clauses/1
  - receive_expr_timeout/1
  - record_access/3
  - record_access_argument/1
  - record_access_field/1
  - record_access_type/1
  - record_expr/2
  - record_expr/3
  - record_expr_argument/1
  - record_expr_fields/1
  - record_expr_type/1
  - record_field/1
  - record_field/2
  - record_field_name/1
  - record_field_value/1
  - record_index_expr/2
  - record_index_expr_field/1
  - record_index_expr_type/1
  - record_type/2
  - record_type_field/2
  - record_type_field_name/1
  - record_type_field_type/1
  - record_type_fields/1
  - record_type_name/1
  - remove_comments/1
  - revert/1
  - revert_forms/1
  - set_ann/2
  - set_attrs/2
  - set_pos/2
  - set_postcomments/2
  - set_precomments/2
  - size_qualifier/2
  - size_qualifier_argument/1
  - size_qualifier_body/1
  - string/1
  - string_literal/1
  - string_literal/2
  - string_value/1
  - subtrees/1
  - text/1
  - text_string/1
  - tree/1
  - tree/2
  - try_after_expr/2
  - try_expr/2
  - try_expr/3
  - try_expr/4
  - try_expr_after/1
  - try_expr_body/1
  - try_expr_clauses/1
  - try_expr_handlers/1
  - tuple/1
  - tuple_elements/1
  - tuple_size/1
  - tuple_type/0
  - tuple_type/1
  - tuple_type_elements/1
  - type/1
  - type_application/2
  - type_application/3
  - type_application_arguments/1
  - type_application_name/1
  - type_union/1
  - type_union_types/1
  - typed_record_field/2
  - typed_record_field_body/1
  - typed_record_field_type/1
  - underscore/0
  - update_tree/2
  - user_type_application/2
  - user_type_application_arguments/1
  - user_type_application_name/1
  - variable/1
  - variable_literal/1
  - variable_name/1
  - warning_marker/1
  - warning_marker_info/1
- erl_syntax_lib
  - Top of manual page
  - analyze_application/1
  - analyze_attribute/1
  - analyze_export_attribute/1
  - analyze_file_attribute/1
  - analyze_form/1
  - analyze_forms/1
  - analyze_function/1
  - analyze_function_name/1
  - analyze_implicit_fun/1
  - analyze_import_attribute/1
  - analyze_module_attribute/1
  - analyze_record_attribute/1
  - analyze_record_expr/1
  - analyze_record_field/1
  - analyze_type_application/1
  - analyze_type_name/1
  - analyze_wild_attribute/1
  - annotate_bindings/1
  - annotate_bindings/2
  - fold/3
  - fold_subtrees/3
  - foldl_listlist/3
  - function_name_expansions/1
  - is_fail_expr/1
  - limit/2
  - limit/3
  - map/2
  - map_subtrees/2
  - mapfold/3
  - mapfold_subtrees/3
  - mapfoldl_listlist/3
  - new_variable_name/1
  - new_variable_name/2
  - new_variable_names/2
  - new_variable_names/3
  - strip_comments/1
  - to_comment/1
  - to_comment/2
  - to_comment/3
  - variables/1
- erl_tidy
  - Top of manual page
  - dir/0
  - dir/1
  - dir/2
  - file/1
  - file/2
  - module/1
  - module/2
- igor
  - Top of manual page
  - create_stubs/2
  - merge/2
  - merge/3
  - merge_files/3
  - merge_files/4
  - merge_sources/3
  - parse_transform/2
  - rename/2
  - rename/3
- merl
  - Top of manual page
  - alpha/2
  - compile/1
  - compile/2
  - compile_and_load/1
  - compile_and_load/2
  - match/2
  - meta_template/1
  - print/1
  - qquote/2
  - qquote/3
  - quote/1
  - quote/2
  - show/1
  - subst/2
  - switch/2
  - template/1
  - template_vars/1
  - term/1
  - tree/1
  - tsubst/2
  - var/1
- merl_transform
  - Top of manual page
  - parse_transform/2
- prettypr
  - Top of manual page
  - above/2
  - beside/2
  - best/3
  - break/1
  - empty/0
  - floating/1
  - floating/3
  - follow/2
  - follow/3
  - format/1
  - format/2
  - format/3
  - nest/2
  - null_text/1
  - par/1
  - par/2
  - sep/1
  - text/1
  - text_par/1
  - text_par/2

# prettypr

### Module

prettypr

### Module Summary

A generic pretty printer library.

### Description

A generic pretty printer library. This module uses a strict-style context passing implementation of John Hughes algorithm, described in "The design of a Pretty-printing Library". The paragraph-style formatting, empty documents, floating documents, and null strings are my own additions to the algorithm.

To get started, you should read about the document() data type; the main constructor functions: text/1, above/2, beside/2, nest/2, sep/1, and par/2; and the main layout function format/3.

If you simply want to format a paragraph of plain text, you probably want to use the text_par/2 function, as in the following example:

      prettypr:format(prettypr:text_par("Lorem ipsum dolor sit amet"), 20)

### DATA TYPES

document()

  An abstract character-based "document" representing a number of possible layouts, which can be processed to produce a single concrete layout. A concrete layout can then be rendered as a sequence of characters containing linebreaks, which can be passed to a printer or terminal that uses a fixed-width font.

  For example, a document sep([text("foo"), text("bar")]) represents the two layouts

           foo bar

  and

           foo
           bar

  Which layout is chosen depends on the available horizontal space. When processing a document, the main parameters are the paper width and the line width (also known as the "ribbon width"). In the resulting layout, no text should be printed beyond the paper width (which by default is 80 characters) as long as it can be avoided, and each single line of text (its indentation not counted, hence "ribbon") should preferably be no wider than the specified line width (which by default is 65).

  Documents can be joined into a single new document using the constructor functions of this module. Note that the new document often represents a larger number of possible layouts than just the sum of the components.

### Exports

above(D1::document(), D2::document()) -> document()

Concatenates documents vertically. Returns a document representing the concatenation of the documents D1 and D2 such that the first line of D2 follows directly below the last line of D1, and the first character of D2 is in the same horizontal column as the first character of D1, in all possible layouts.

Examples:

         ab  cd  =>  ab
                     cd

                        abc
         abc   fgh  =>   de
          de    ij      fgh
                         ij

beside(D1::document(), D2::document()) -> document()

Concatenates documents horizontally. Returns a document representing the concatenation of the documents D1 and D2 such that the last character of D1 is horizontally adjacent to the first character of D2, in all possible layouts. (Note: any indentation of D2 is lost.)

Examples:

         ab  cd  =>  abcd

         ab  ef      ab
         cd  gh  =>  cdef
                       gh

best(D::document(), PaperWidth::integer(), LineWidth::integer()) -> empty | document()

Selects a "best" layout for a document, creating a corresponding fixed-layout document. If no layout could be produced, the atom empty is returned instead. For details about PaperWidth and LineWidth, see format/3. The function is idempotent.

One possible use of this function is to compute a fixed layout for a document, which can then be included as part of a larger document. For example:

         above(text("Example:"), nest(8, best(D, W - 12, L - 6)))

will format D as a displayed-text example indented by 8, whose right margin is indented by 4 relative to the paper width W of the surrounding document, and whose maximum individual line length is shorter by 6 than the line length L of the surrounding document.

This function is used by the format/3 function to prepare a document before being laid out as text.

break(D::document()) -> document()

Forces a line break at the end of the given document. This is a utility function; see empty/0 for details.

empty() -> document()

Yields the empty document, which has neither height nor width. (empty is thus different from an empty text string, which has zero width but height 1.)

Empty documents are occasionally useful; in particular, they have the property that above(X, empty()) will force a new line after X without leaving an empty line below it; since this is a common idiom, the utility function break/1 will place a given document in such a context.

See also: text/1.

floating(D::document()) -> document()

Equivalent to floating(D, 0, 0).

floating(D::document(), Hp::integer(), Vp::integer()) -> document()

Creates a "floating" document. The result represents the same set of layouts as D; however, a floating document may be moved relative to other floating documents immediately beside or above it, according to their relative horizontal and vertical priorities. These priorities are set with the Hp and Vp parameters; if omitted, both default to zero.

Notes: Floating documents appear to work well, but are currently less general than you might wish, losing effect when embedded in certain contexts. It is possible to nest floating-operators (even with different priorities), but the effects may be difficult to predict. In any case, note that the way the algorithm reorders floating documents amounts to a "bubblesort", so don't expect it to be able to sort large sequences of floating documents quickly.

follow(D1::document(), D2::document()) -> document()

Equivalent to follow(D1, D2, 0).

follow(D1::document(), D2::document(), Offset::integer()) -> document()

Separates two documents by either a single space, or a line break and intentation. In other words, one of the layouts

         abc def

or

         abc
          def

will be generated, using the optional offset in the latter case. This is often useful for typesetting programming language constructs.

This is a utility function; see par/2 for further details.

See also: follow/2.

format(D::document()) -> string()

Equivalent to format(D, 80).

format(D::document(), PaperWidth::integer()) -> string()

Equivalent to format(D, PaperWidth, 65).

format(D::document(), PaperWidth::integer(), LineWidth::integer()) -> string()

Computes a layout for a document and returns the corresponding text. See document() for further information. Throws no_layout if no layout could be selected.

PaperWidth specifies the total width (in character positions) of the field for which the text is to be laid out. LineWidth specifies the desired maximum width (in number of characters) of the text printed on any single line, disregarding leading and trailing white space. These parameters need to be properly balanced in order to produce good layouts. By default, PaperWidth is 80 and LineWidth is 65.

See also: best/3.

nest(N::integer(), D::document()) -> document()

Indents a document a number of character positions to the right. Note that N may be negative, shifting the text to the left, or zero, in which case D is returned unchanged.

null_text(Characters::string()) -> document()

Similar to text/1, but the result is treated as having zero width. This is regardless of the actual length of the string. Null text is typically used for markup, which is supposed to have no effect on the actual layout.

The standard example is when formatting source code as HTML to be placed within <pre>...</pre> markup, and using e.g. <i> and <b> to make parts of the source code stand out. In this case, the markup does not add to the width of the text when viewed in an HTML browser, so the layout engine should simply pretend that the markup has zero width.

See also: empty/0, text/1.

par(Docs::[document()]) -> document()

Equivalent to par(Ds, 0).

par(Docs::[document()], Offset::integer()) -> document()

Arranges documents in a paragraph-like layout. Returns a document representing all possible left-aligned paragraph-like layouts of the (nonempty) sequence Docs of documents. Elements in Docs are separated horizontally by a single space character and vertically with a single line break. All lines following the first (if any) are indented to the same left column, whose indentation is specified by the optional Offset parameter relative to the position of the first element in Docs. For example, with an offset of -4, the following layout can be produced, for a list of documents representing the numbers 0 to 15:

             0 1 2 3
         4 5 6 7 8 9
         10 11 12 13
         14 15

or with an offset of +2:

         0 1 2 3 4 5 6
           7 8 9 10 11
           12 13 14 15

The utility function text_par/2 can be used to easily transform a string of text into a par representation by splitting it into words.

Note that whenever a document in Docs contains a line break, it will be placed on a separate line. Thus, neither a layout such as

         ab cd
            ef

nor

         ab
         cd ef

will be generated. However, a useful idiom for making the former variant possible (when wanted) is beside(par([D1, text("")], N), D2) for two documents D1 and D2. This will break the line between D1 and D2 if D1 contains a line break (or if otherwise necessary), and optionally further indent D2 by N character positions. The utility function follow/3 creates this context for two documents D1 and D2, and an optional integer N.

See also: par/1, text_par/2.

sep(Docs::[document()]) -> document()

Arranges documents horizontally or vertically, separated by whitespace. Returns a document representing two alternative layouts of the (nonempty) sequence Docs of documents, such that either all elements in Docs are concatenated horizontally, and separated by a space character, or all elements are concatenated vertically (without extra separation).

Note: If some document in Docs contains a line break, the vertical layout will always be selected.

Examples:

                                      ab
         ab  cd  ef  =>  ab cd ef  |  cd
                                      ef

         ab           ab
         cd  ef  =>   cd
                      ef

See also: par/2.

text(Characters::string()) -> document()

Yields a document representing a fixed, unbreakable sequence of characters. The string should contain only printable characters (tabs allowed but not recommended), and not newline, line feed, vertical tab, etc. A tab character (\t) is interpreted as padding of 1-8 space characters to the next column of 8 characters within the string.

See also: empty/0, null_text/1, text_par/2.

text_par(Text::string()) -> document()

Equivalent to text_par(Text, 0).

text_par(Text::string(), Indentation::integer()) -> document()

Yields a document representing paragraph-formatted plain text. The optional Indentation parameter specifies the extra indentation of the first line of the paragraph. For example, text_par("Lorem ipsum dolor sit amet", N) could represent

         Lorem ipsum dolor
         sit amet

if N = 0, or

           Lorem ipsum
         dolor sit amet

if N = 2, or

         Lorem ipsum dolor
           sit amet

if N = -2.

(The sign of the indentation is thus reversed compared to the par/2 function, and the behaviour varies slightly depending on the sign in order to match the expected layout of a paragraph of text.)

Note that this is just a utility function, which does all the work of splitting the given string into words separated by whitespace and setting up a par with the proper indentation, containing a list of text elements.

See also: par/2, text/1, text_par/1.

Richard Carlsson carlsson.richard@gmail.com