HOME

TheInfoList



OR:

Pretty-printing (or prettyprinting) is the application of any of various stylistic formatting conventions to
text file A text file (sometimes spelled textfile; an old alternative name is flatfile) is a kind of computer file that is structured as a sequence of lines of electronic text. A text file exists stored as data within a computer file system. In operat ...
s, such as
source code In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
,
markup Markup or mark-up can refer to: * Markup language, a standardized set of notations used to annotate a plain-text document's content to give information regarding the structure of the text or instructions for how it is to be displayed ** Lightweigh ...
, and similar kinds of content. These formatting conventions may entail adhering to an indentation style, using different color and typeface to highlight syntactic elements of source code, or adjusting size, to make the content easier for people to read, and understand. Pretty-printers for source code are sometimes called code formatters or beautifiers.


Pretty-printing mathematics

Pretty-printing usually refers to displaying mathematical expressions similar to the way they would be typeset professionally. For example, in computer algebra systems such as Maxima or
Mathematica Wolfram Mathematica is a software system with built-in libraries for several areas of technical computing that allow machine learning, statistics, symbolic computation, data manipulation, network analysis, time series analysis, NLP, optimi ...
the system may write output like "x ^ 2 + 3 * x" as Some
graphing calculator A graphing calculator (also graphics calculator or graphic display calculator) is a handheld computer that is capable of plotting graphs, solving simultaneous equations, and performing other tasks with variables. Most popular graphing calcu ...
s, such as the Casio 9860 series,
HP-49 series The HP 49/50 series are Hewlett-Packard (HP) manufactured graphing calculators. They are the successors of the popular HP 48 series. There are five calculators in the 49/50 series of HP graphing calculators. These calculators have both al ...
, TI-84 Plus, TI-89, and TI-Nspire, the TI-83 Plus with the PrettyPt add-on, or the TI-84 Plus with the same add-on or the "MathPrint"-enabled OSes, can perform pretty-printing. Additionally, a number of newer scientific calculators are equipped with dot matrix screens capable of pretty-printing such as the Casio FX-ES series (Natural Display),
Sharp EL-W series Sharp or SHARP may refer to: Acronyms * SHARP (helmet ratings) (Safety Helmet Assessment and Rating Programme), a British motorcycle helmet safety rating scheme * Self Help Addiction Recovery Program, a charitable organisation founded in 199 ...
(WriteView), HP SmartCalc 300s, TI-30XB, and Numworks. Many text formatting programs can also typeset mathematics: TeX was developed specifically for high-quality mathematical
typesetting Typesetting is the composition of text by means of arranging physical ''type'' (or ''sort'') in mechanical systems or '' glyphs'' in digital systems representing '' characters'' (letters and other symbols).Dictionary.com Unabridged. Random ...
.


Pretty-printing markup and tag-based code

Pretty-printing in markup language instances is most typically associated with indentation of tags and string content to visually determine hierarchy and nesting. Although the syntactical structures of tag-based languages do not significantly vary, the indentation may vary significantly due to how a markup language is interpreted or due to the data it describes. In MathML,
whitespace character In computer programming, whitespace is any character or series of characters that represent horizontal or vertical space in typography. When rendered, a whitespace character does not correspond to a visible mark, but typically does occupy an area ...
s do not reflect data, meaning, or syntax above what is required by
XML Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. ...
syntax. In
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
, whitespace characters between tags are considered text and are parsed as text nodes into the parsed result. While indentation may be generously applied to a MathML document, sufficient additional care must be taken in pretty-printing an HTML document to ensure additional text nodes are not created or destroyed in general proximity to the content or content-reflective tag elements. This difference in complexity is non-trivial from the perspective of an automated pretty-print operation where no special rules or edge cases are necessary, as in the more simple MathML example. The HTML example may require a series of progressive interrelated algorithms to account for various patterns of tag elements and content that conforms to a uniform style and is consistent in application across various instances, as evidenced by the markup.ts application component used to beautify HTML, XML, and related technologies for the Pretty Diff tool.


Programming code formatting

Programmers often use tools to format
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming l ...
source code In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
in a particular manner. Proper code formatting makes it easier to read and understand. Different programmers often prefer different styles of formatting, such as the use of code indentation and whitespace or positioning of braces. A code formatter converts source code from one format style to another. This is relatively straightforward because of the unambiguous syntax of programming languages. Code beautification involves parsing the source code into component structures, such as assignment statements, ''if'' blocks,
loop Loop or LOOP may refer to: Brands and enterprises * Loop (mobile), a Bulgarian virtual network operator and co-founder of Loop Live * Loop, clothing, a company founded by Carlos Vasquez in the 1990s and worn by Digable Planets * Loop Mobile, ...
s, etc. (see also
control flow In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an '' ...
), and formatting them in a manner specified by the user in a configuration file. Code beautifiers exist as standalone applications and built into
text editor A text editor is a type of computer program that edits plain text. Such programs are sometimes known as "notepad" software (e.g. Windows Notepad). Text editors are provided with operating systems and software development packages, and can be u ...
s and
integrated development environment An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of at least a source code editor, build automation tools a ...
s. For example,
Emacs Emacs , originally named EMACS (an acronym for "Editor MACroS"), is a family of text editors that are characterized by their extensibility. The manual for the most widely used variant, GNU Emacs, describes it as "the extensible, customizable, ...
' various language modes can correctly indent blocks of code attractively.


HTML


Lisp pretty-printer

An early example of pretty-printing was Bill Gosper's "GRINDEF" (''i.e.'' 'grind function') program (''c.'' 1967), which used combinatorial search with pruning to format
LISP A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lispi ...
programs. Early versions operated on the executable (list structure) form of the Lisp program and were oblivious to the special meanings of various functions. Later versions had special read conventions for incorporating non-executable comments and also for preserving read macros in unexpanded form. They also allowed special indentation conventions for special functions such as if. The term "grind" was used in some Lisp circles as a synonym for pretty-printing.


Project style rules

Many open source projects have established rules for code layout. The most typical are the GNU formatting and the BSD style.BSD style
/ref> The biggest difference between the two is the location of the braces: in the GNU style, opening and closing braces are on lines by themselves, with the same indent. BSD style places an opening brace at the end of the preceding line, and the closing braces can be followed by else. The size of indent and location of whitespace also differs.


Example of formatting and beautifying code

The following example shows some typical C structures and how various indentation style rules format them. Without any formatting at all, it looks like this: int foo(int k) The GNU indent program produces the following output when asked to indent according to the GNU rules: int foo (int k) It produces this output when formatting according to BSD rules: int foo(int k)


See also

Related concepts * Elastic tabstop, a feature of many source code editors that detects and maintains aligned indents * Minification, making source code compact, even if it becomes harder for humans to understand *
Obfuscation Obfuscation is the obscuring of the intended meaning of communication by making the message difficult to understand, usually with confusing and ambiguous language. The obfuscation might be either unintentional or intentional (although intent u ...
, deliberately making source code very difficult for humans to understand, even if it becomes somewhat convoluted Utilities * enscript, a text-to-PostScript converter, with pretty-printing features


References

{{reflist


External links


Algorithm 268: ALGOL 60 reference language editor
''
William M. McKeeman William is a male given name of Germanic origin.Hanks, Hardcastle and Hodges, ''Oxford Dictionary of First Names'', Oxford University Press, 2nd edition, , p. 276. It became very popular in the English language after the Norman conquest of Engl ...
'': Commun. ACM 8(11): 667-668 (1965)
lgrind
Comprehensive TEX Archive Network
NEATER2: a PL/I source statement reformatter
''Kenneth Conrow, Ronald G. Smith'': Commun. ACM 13(11): 669-675 (1970)
SOAP - Simplify Obscure Algol Programs
''R. S. Scowen, D. Allin, A. L. Hillman, M. Shimell'': National Physical Laboratory Central Computer Unit repor
CCU6
(April, 1969) Includes formatted listing of SOAP source code. *
SOAP - A Program which Documents and Edits ALGOL 60 Programs.
''R. S. Scowen, D. Allin, A. L. Hillman, M. Shimell'': Comput. J. 14(2): 133-135 (1971) *
Original SOAP Source Code from the KDF9 (unformatted)

SOAP User's Guide.
(for Edinburgh IMP) ''Peter Salkeld Robertson'' (1976) *
SOAP Source Code
in/for IMP9
Soap80: A Program for Formatting IMP80 Source Programs.
''J.M. Murison, Edinburgh Regional Computer Center'' (1980) *

in/for IMP80 ''E. N. Gregory, University of Kent at Canterbury; Peter D. Stephens, Edinburgh Regional Computer Center''
PRETTYP.PAS
Early pascal prettyprinter. ''Ledgard et al.''
Pascal With Style
(1979)
style(9)
FreeBSD style guidelines

The nixHeirloom Project
Formatting your source code
GNU style guidelines Articles with example C code Source code Text editor features