![]() |
Source files | ![]() |
The formats of source code (cpp, python, scheme, java… in
code-format.scm
,
python-format.scm
,
prog-format.scm
) load a file with code->texmacs and save it with texmacs->verbatim and the encoding SourceCode (
init-rewrite.scm
). A file is saved as it was loaded, byte for byte, apart from the lines which were edited (suite source-files,
source-files-test.scm
).
code->texmacs calls source_document_to_tree (cpp-source->texmacs,
verbatim.cpp
), which differs from verbatim_document_to_tree in what it keeps:
The tabs and the other control characters (below 32, and 127) are the characters <#9>, <#C>… of Cork: a byte 9 of Cork is a macron, so that a tab left as it is was shown as a macron and saved as U+00AF, and the form feeds were saved as other characters. They are not expanded (un_special expands the tabs to spaces for the other verbatim imports), and a backspace is not applied.
A file which is not valid UTF-8 is read as ISO-8859-1.
The line ends are found: LF, CR LF or CR when all the lines end so; when they are mixed, the lines end by LF and a CR left in a line is the character <#D>.
The indentation of the lines is found: by tabs when the lines which start by a tab are more than those which start by two spaces, by spaces when there are some of those, else none.
The encoding, the line ends and the indentation are in the initial environment of the document: source-encoding (utf-8, iso-8859-1), source-line-ends (unix, dos, mac) and source-indent (tabs, spaces or empty). attach_subformat (
new_buffer.cpp
) sets prog-tab-width there too, from the preference editor:code tab width (8 by default).
Code which is pasted in a program is converted by source_snippet_to_tree (code-snippet->texmacs), which keeps its tabs and its control characters in the same way; its line ends are LF.
tree_to_verbatim with the encoding SourceCode follows the initial environment of a document which has source-line-ends (tree_to_source): the lines are converted by cork_to_sourcecode, which writes a character <#9> as the byte 9 (and the ASCII as it is, unlike cork_to_utf8, which writes a backquote as an opening quote and three dots as an ellipsis); a file of ISO-8859-1 is written in it; the line ends are those of the file (not those of the system, as for the other documents). A document without these variables is saved as before.
typeset_prog_string (
concat_text.cpp
) typesets a tab as a gap up to the next tab stop, every prog-tab-width columns. The column of a tab is counted from the start of its line (prog_column: a tab goes to its stop, any other character of Cork is one column, the font of programs being monospaced), also when the line is typeset from its middle (the range primitive). The gap (typeset_prog_tab) is a light arrow resized to the width of the gap, whose box has a decorated path, between two empty boxes at the ends of the tab: the cursor goes over it at once, as over a space. A token of a language which contains tabs (a string, a comment) is typeset in pieces (typeset_prog_piece), the tabs at their stops.
The tokenizers take a tab for a blank (is_source_tab in
analyze.hpp
): the blanks_parser of the generic program languages, and cpp_language_rep, scheme_language_rep and verb_language_rep, so that the words after a tab keep their colours.
prog-edit.scm
counts the indentation in columns, a tab going to its stop (string-column, string-get-indent), and writes it as the program is indented (program-indent-tabs?: source-indent, else tabs for Go and makefiles, else the preference editor:code indent): string-set-indent writes as many tabs as fit and spaces for the rest, or spaces. One level of indentation (program-indent-step, used by the indentation of Python, Java, Scala, Fortran and dot) is a tab where the lines are indented by tabs, else get-tabstop. insert-tabstop inserts a tab there, else spaces up to the next stop from the column of the cursor. program-indent-line returns the offset of the end of the indentation, where the cursor goes (with tabs, it is not the column).
The indentation of Python strips the comments of a line before it looks at its last character: a # of a character of Cork, as in <#9>, is not a comment (
python-edit.scm
).