Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Pretty-printing
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{short description|Formatting to make code or markup easier to read}} {{Use dmy dates|date=April 2016|cs1-dates=y}} '''Pretty-printing''' (or '''prettyprinting''') is the application of any of various stylistic [[text formatting|formatting]] conventions to [[text file]]s, such as [[source code]], [[markup language|markup]], and similar kinds of content. These formatting conventions may entail adhering to an [[indentation style]], using different color and typeface to [[Syntax highlighting|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== [[Image:MathematicaTypesetExpression.png|thumb|210px|A typeset mathematical expression]] Pretty-printing usually refers to displaying [[mathematical expression]]s similar to the way they would be [[formula editor|typeset]] professionally. For example, in [[computer algebra system]]s such as [[Maxima (software)|Maxima]] or [[Mathematica]] the system may write output like {{No wrap|"<samp>x ^ 2 + 3 * x</samp>"}} as {{No wrap|"<math>x^2+3x</math>".}} Some [[graphing calculator]]s, such as the [[Casio 9860 series]], [[HP-49 series|HP-49/50 series]] and [[HP Prime]], [[TI-84 Plus]], [[TI-89]], and [[TI-Nspire]], the [[TI-83 Plus]] with the PrettyPt<ref>{{Cite web |title=PrettyPrint - ticalc.org |url=https://www.ticalc.org/archives/files/fileinfo/208/20807.html |access-date=2022-04-13 |website=www.ticalc.org}}</ref> 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]] (WriteView), [[HP SmartCalc series|HP SmartCalc 300s]], [[TI-30XB]], and [[NumWorks|Numworks]]. Many text formatting programs can also typeset mathematics: [[TeX]] was developed specifically for high-quality mathematical [[typesetting]]. ==Pretty-printing markup and tag-based code== [[File:HTML source code example.svg|thumb|HTML source code, pretty-printed to better show the hierarchical relationships of its elements (called tags)]] Pretty-printing in markup language instances is most typically associated with [[indentation (typesetting)|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]]s do not reflect data, meaning, or syntax above what is required by [[XML]] syntax. In [[HTML]], whitespace characters between tags are considered text and are parsed as text nodes into the parsed result.<ref>{{cite web |url=https://developer.mozilla.org/en-US/docs/Whitespace_in_the_DOM |title=Whitespace in the DOM |first=L. David |last=Baron |publisher=Mozilla Developer Network |accessdate=27 August 2012}}</ref> 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<ref>[https://github.com/prettydiff/prettydiff/blob/master/beautify/markup.ts markup.ts]</ref> 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]] [[source code]] 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 (typesetting)|indentation]] and whitespace or positioning of [[bracket|braces]]. A code formatter or [[code indenter]] 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, [[program loop|loop]]s, etc. (see also [[control flow]]), 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]]s and [[integrated development environment]]s. For example, [[Emacs]]' various language modes can correctly [[indentation (typesetting)|indent]] blocks of code attractively.<ref>{{cite web |url=https://www.gnu.org/software/emacs/manual/html_node/emacs/Program-Indent.html#Program-Indent |title=Indentation for Programs |first=Richard M. |last=Stallman |publisher=Free Software Foundation |work=GNU Emacs Manual |accessdate=20 October 2011}}</ref> ===HTML=== {{see|HTML Tidy}} ===Lisp pretty-printer=== An early example of pretty-printing was [[Bill Gosper]]'s "GRINDEF" (''i.e.'' 'grind function') program (''c.'' 1967), which used [[combinatorial optimization|combinatorial search]] with pruning to format [[Lisp programming language|LISP]] 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 macro]]s in unexpanded form. They also allowed special indentation conventions for special functions such as <code>if</code>.<ref>Ira Goldstein, "Pretty Printing : Converting List to Linear Structure", Artificial Intelligence Memo 279, Massachusetts Institute of Technology, February 1973. [http://www.softwarepreservation.org/projects/LISP/MIT/AIM-279-Goldstein-Pretty_Printing.pdf/view full text]</ref><ref>Richard C. Waters, "Using the new common Lisp pretty printer", ''ACM SIGPLAN Lisp Pointers'' '''5''':2:27-34, April–June 1992. [https://web.archive.org/web/20170706120420/ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-1102.pdf full text]</ref> The term "grind" was used in some Lisp circles as a synonym for pretty-printing.<ref>[[Jargon File]], ''s.v.'' grind</ref> ===Project style rules=== Many open source projects have established rules for code layout. The most typical are the [[GNU]] formatting<ref>[https://www.gnu.org/prep/standards/standards.html#Formatting GNU style]</ref> and the BSD style.<ref>[http://www.freebsd.org/cgi/man.cgi?query=style&sektion=9 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: <syntaxhighlight lang="c"> int foo(int k){if(k<1||k>2){printf("out of range\n"); printf("this function requires a value of 1 or 2\n");}else{ printf("Switching\n");switch(k){case 1:printf("1\n");break;case 2:printf("2\n");break;}}} </syntaxhighlight> The [[indent (Unix)|GNU indent program]] produces the following output when asked to indent according to the [[indent style#GNU style|GNU rules]]: <syntaxhighlight lang="c"> int foo (int k) { if (k < 1 || k > 2) { printf ("out of range\n"); printf ("this function requires a value of 1 or 2\n"); } else { printf ("Switching\n"); switch (k) { case 1: printf ("1\n"); break; case 2: printf ("2\n"); break; } } } </syntaxhighlight> It produces this output when formatting according to BSD rules: <syntaxhighlight lang="c"> int foo(int k) { if (k < 1 || k > 2) { printf("out of range\n"); printf("this function requires a value of 1 or 2\n"); } else { printf("Switching\n"); switch (k) { case 1: printf("1\n"); break; case 2: printf("2\n"); break; } } } </syntaxhighlight> ==See also== Related concepts *[[Elastic tabstop]], a feature of many source code editors that detects and maintains aligned indents * [[Minification (programming)|Minification]], making source code compact, even if it becomes harder for humans to understand * [[Obfuscation (software)|Obfuscation]], deliberately making source code very difficult for humans to understand - especially as it becomes more convoluted Utilities *[[enscript]], a text-to-PostScript converter, with pretty-printing features ==References== {{reflist}} ==External links== * [http://portal.acm.org/citation.cfm?id=365673&dl=ACM&coll=ACM Algorithm 268: ALGOL 60 reference language editor] ''[[William M. McKeeman]]'': Commun. ACM 8(11): 667-668 (1965) * [https://www.ctan.org/pkg/lgrind?lang=en lgrind], Comprehensive TEX Archive Network * [http://doi.acm.org/10.1145/362790.362796 NEATER2: a PL/I source statement reformatter] ''Kenneth Conrow, Ronald G. Smith'': Commun. ACM 13(11): 669-675 (1970) * [http://gtoal.com/languages/algol60/KDF9/SOAP%20description%20and%20Algol60%20source%20-%20CCU6.pdf SOAP - Simplify Obscure Algol Programs] ''R. S. Scowen, D. Allin, A. L. Hillman, M. Shimell'': National Physical Laboratory Central Computer Unit report [https://discovery.nationalarchives.gov.uk/details/r/C4192654 CCU6] (April, 1969) Includes formatted listing of SOAP source code. ** [https://web.archive.org/web/20061012173444/http://comjnl.oxfordjournals.org/cgi/content/abstract/14/2/133 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) ** [https://gtoal.com/languages/algol60/KDF9/soap.a60.html Original SOAP Source Code from the KDF9] * [http://history.dcs.ed.ac.uk/archive/os/emas/users/ecslib/emas-a/impis-unpacked/soapdoc.txt SOAP User's Guide.] (for [[Edinburgh IMP]]) ''Peter Salkeld Robertson'' (1976) ** [http://history.dcs.ed.ac.uk/archive/os/emas/users/ecslib/emas-a/impis-unpacked/soaps.txt SOAP Source Code] in/for [[Edinburgh IMP|IMP9]] * [http://history.dcs.ed.ac.uk/archive/os/emas/emas2/compilers/imputils/soap80/soap80li.html Soap80: A Program for Formatting IMP80 Source Programs.] ''J.M. Murison, Edinburgh Regional Computer Center'' (1980) ** [http://history.dcs.ed.ac.uk/archive/os/emas/emas2/compilers/imputils/soap80/soap80s.imp.html SOAP Source Code] in/for [[Edinburgh IMP|IMP80]] ''E. N. Gregory, University of Kent at Canterbury; Peter D. Stephens, Edinburgh Regional Computer Center'' * [http://www.moorecad.com/standardpascal/prettyp.pas PRETTYP.PAS] Early pascal prettyprinter. ''Ledgard et al.'' * [http://www.alibris.com/search/books/qwork/5001951/used/Pascal%20with%20Style:%20Programming%20Proverbs Pascal With Style] (1979) * [http://www.freebsd.org/cgi/man.cgi?query=style&sektion=9 style(9)] FreeBSD style guidelines * [http://heirloom.sourceforge.net/doctools/vgrind.1b.html vgrind], The [Unix] Heirloom Project * [https://www.gnu.org/prep/standards/standards.html#Formatting Formatting your source code] GNU style guidelines [[Category:Articles with example C code]] [[Category:Source code]] [[Category:Text editor features]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite web
(
edit
)
Template:No wrap
(
edit
)
Template:Reflist
(
edit
)
Template:See
(
edit
)
Template:Short description
(
edit
)
Template:Use dmy dates
(
edit
)