lfe_io_pretty.erl - Pretty Printer
Purpose: Write LFE terms in formatted, indented, multi-line layout for readability.
Location: src/lfe_io_pretty.erl
Size: 405 LOC, 15KB
Module Classification: I/O support, pretty formatting
Public API
term(Term) -> [char()]
term(Term, Depth) -> [char()]
term(Term, Depth, Indentation, LineLength) -> [char()]
Pretty print term with specified formatting parameters. Located at lfe_io_pretty.erl:47-49.
Formatting Strategy
Layout Modes:
-
Horizontal - Single line:
(a b c) -
Vertical - Multi-line with indentation:
(defun foo (x) (let ((y (* x 2))) (+ y 1)))
Line Length Tracking:
The pretty printer tries to fit output within LineLength (default 80) by:
- Attempting horizontal layout
- Falling back to vertical if too long
- Intelligent indentation based on form type
Special Form Formatting (format_form/4 at lines 178-267):
Different forms get custom layouts:
; Function definitions - indent body
(defun name (args)
body)
; Let bindings - align bindings
(let ((x 1)
(y 2))
body)
; Conditionals - indent branches
(if test
then-branch
else-branch)
; Case - indent clauses
(case expr
(pattern1 result1)
(pattern2 result2))
Indentation Rules
By Form Type:
defun,defmacro- Indent 2 after function headlet,let*,letrec- Align binding pairsif,cond- Indent brancheslambda- Indent body- Default lists - Indent 1 from opening paren
Column Tracking (write_tail/4 at lines 298-356):
Maintains current column position to:
- Decide horizontal vs vertical layout
- Calculate indentation
- Enforce line length limits
Dependencies
lists,io_lib,string(Erlang stdlib)
Used By
lfe_io- Viaprettyprint1/*lfe_shell- Value displaylfe_error- Error formatting
Special Considerations
Performance: Slower than compact writing due to layout calculations.
Configurable: LineLength and initial indentation customizable.
Readable Output: Prioritizes human readability over machine parsing.