1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
|
<html lang="en">
<head>
<title>GNU Source-highlight Library 3.1.7</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Source-highlight Library 3.1.7">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="top" href="#Top">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This manual is for GNU Source-highlight Library
(version 3.1.7, 16 December 2011),
which given a source file, produces a document with syntax highlighting.
Copyright (C) 2005-2008 Lorenzo Bettini, `http://www.lorenzobettini.it'.
Permission is granted to copy, distribute and/or modify this
document under the terms of the GNU Free Documentation License,
Version 1.1 or any later version published by the Free Software
Foundation; with no Invariant Sections, with the Front-Cover Texts
being "A GNU Manual," and with the Back-Cover Texts as in (a)
below. A copy of the license is included in the section entitled
"GNU Free Documentation License."
(a) The FSF's Back-Cover Text is: "You have freedom to copy and
modify this GNU Manual, like GNU software. Copies published by
the Free Software Foundation raise funds for GNU development."
-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<h1 class="settitle">GNU Source-highlight Library 3.1.7</h1>
<div class="contents">
<h2>Table of Contents</h2>
<ul>
<li><a name="toc_Top" href="#Top">GNU Source-highlight Library</a>
<li><a name="toc_Introduction" href="#Introduction">1 Introduction</a>
<li><a name="toc_Installation" href="#Installation">2 Installation</a>
<li><a name="toc_Use-of-GNU-Source_002dhighlight-Library" href="#Use-of-GNU-Source_002dhighlight-Library">3 Use of GNU Source-highlight Library</a>
<ul>
<li><a href="#Using-Automake-and-Autotools">3.1 Using Automake and Autotools</a>
</li></ul>
<li><a name="toc_Main-Classes" href="#Main-Classes">4 Main Classes</a>
<ul>
<li><a href="#SourceHighlight-class">4.1 SourceHighlight class</a>
<li><a href="#Customizing-Formatting">4.2 Customizing Formatting</a>
<ul>
<li><a href="#Completely-Customized-Formatting">4.2.1 Completely Customized Formatting</a>
<li><a href="#Style_002dbased-Customized-Formatting">4.2.2 Style-based Customized Formatting</a>
</li></ul>
<li><a href="#Events-and-Listeners">4.3 Events and Listeners</a>
<li><a href="#Settings">4.4 Settings</a>
<li><a href="#Utility-functions">4.5 Utility functions</a>
<li><a href="#Global-instances">4.6 Global instances</a>
</li></ul>
<li><a name="toc_Problems" href="#Problems">5 Reporting Bugs</a>
<li><a name="toc_Mailing-Lists" href="#Mailing-Lists">6 Mailing Lists</a>
<li><a name="toc_Concept-Index" href="#Concept-Index">Concept Index</a>
</li></ul>
</div>
<div class="node">
<a name="Top"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Introduction">Introduction</a>,
Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
</div>
<h2 class="unnumbered">GNU Source-highlight Library</h2>
<p>GNU Source-highlight, given a source file, produces a document with
syntax highlighting.
<p>This is Edition 3.1.7 of the Source-highlight Library manual.
<p>This file documents GNU Source-highlight Library version 3.1.7.
<p>This manual is for GNU Source-highlight Library
(version 3.1.7, 16 December 2011),
which given a source file, produces a document with syntax highlighting.
<p>Copyright © 2005-2008 Lorenzo Bettini, <a href="http://www.lorenzobettini.it">http://www.lorenzobettini.it</a>.
<blockquote>
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.1 or
any later version published by the Free Software Foundation; with no
Invariant Sections, with the Front-Cover Texts being “A GNU Manual,”
and with the Back-Cover Texts as in (a) below. A copy of the
license is included in the section entitled “GNU Free Documentation
License.”
<p>(a) The FSF's Back-Cover Text is: “You have freedom to copy and modify
this GNU Manual, like GNU software. Copies published by the Free
Software Foundation raise funds for GNU development.”
</blockquote>
<!-- All the nodes can be updated using the EMACS command -->
<!-- texinfo-every-node-update, which is normally bound to C-c C-u C-e. -->
<!-- @node Top, Introduction, (dir), (dir) -->
<!-- All the menus can be updated with the EMACS command -->
<!-- texinfo-all-menus-update, which is normally bound to C-c C-u C-a. -->
<ul class="menu">
<li><a accesskey="1" href="#Introduction">Introduction</a>: What's it for?
<li><a accesskey="2" href="#Installation">Installation</a>
<li><a accesskey="3" href="#Use-of-GNU-Source_002dhighlight-Library">Use of GNU Source-highlight Library</a>
<li><a accesskey="4" href="#Main-Classes">Main Classes</a>
<li><a accesskey="5" href="#Global-instances">Global instances</a>
<li><a accesskey="6" href="#Problems">Problems</a>: Reporting bugs.
<li><a accesskey="7" href="#Mailing-Lists">Mailing Lists</a>
<li><a accesskey="8" href="#Concept-Index">Concept Index</a>: Index of concepts.
</ul>
<div class="node">
<a name="Introduction"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Installation">Installation</a>,
Previous: <a rel="previous" accesskey="p" href="#Top">Top</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">1 Introduction</h2>
<p><a name="index-introduction-1"></a><a name="index-features-2"></a>GNU Source-highlight, given a source file, produces a document with
syntax highlighting. see <a href="source-highlight.html#Introduction">Introduction</a> for a
wider introduction about GNU Source-highlight.
<p>This file documents the Library provided by GNU Source-highlight, thus
its audience is programmers only, who want to use source-highlight
features inside their programs, not the users of Source-highlight.
This library is part of GNU Source-highlight since version 3.0.
<p>However, the main principles of GNU Source-highlight will be given for
granted, together with all the notions for writing language definition
files, output definition files, and so on. Again, we refer to the
documentation of GNU Source-highlight for all these features.
<div class="node">
<a name="Installation"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Use-of-GNU-Source_002dhighlight-Library">Use of GNU Source-highlight Library</a>,
Previous: <a rel="previous" accesskey="p" href="#Introduction">Introduction</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">2 Installation</h2>
<p>GNU Source-highlight library is part of GNU Source-highlight, thus it
will be installed together with Source-highlight itself; we refer to
see <a href="source-highlight.html#Installation">Installation</a> for further instructions on
installing GNU Source-highlight. Here we detail only the parts
concerning the library.
<p><a name="index-g_t_0040code_007b_002d_002dwith_002ddoxygen_007d-3"></a><a name="index-doxygen-4"></a>If you want to build and install the API documentation of
Source-highlight library, you need to run <code>configure</code> with the
option <code>--with-doxygen</code>, but you need the program <em>Doxygen</em>,
<a href="http://www.doxygen.org">http://www.doxygen.org</a>, to build the documentation.
The documentation will be installed in the following directory:
<dl>
<dt><code>Library API documentation</code><dd> <code>prefix/share/doc/source-highlight/api</code>
<br><dt><code>library examples</code><dd> <code>prefix/share/doc/source-highlight/examples</code>
<br><dt><code>conf files</code><dd> <code>prefix/share/source-highlight</code>
</dl>
<div class="node">
<a name="Use-of-GNU-Source-highlight-Library"></a>
<a name="Use-of-GNU-Source_002dhighlight-Library"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Main-Classes">Main Classes</a>,
Previous: <a rel="previous" accesskey="p" href="#Installation">Installation</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">3 Use of GNU Source-highlight Library</h2>
<p><a name="index-libsource_002dhighlight-5"></a>You can use GNU Source-highlight library in your programs, by including
its headers and linking to the file
<samp><span class="file">libsource-highlight</span></samp>.ext<a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a>.
<p><a name="index-namespace-6"></a><a name="index-headers-7"></a>All the classes of the library are part of the namespace
<code>srchilite</code>, and all the header files are in the subdirectory
<code>srchilite</code>.
<ul class="menu">
<li><a accesskey="1" href="#Using-Automake-and-Autotools">Using Automake and Autotools</a>
</ul>
<div class="node">
<a name="Using-Automake-and-Autotools"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#Use-of-GNU-Source_002dhighlight-Library">Use of GNU Source-highlight Library</a>,
Up: <a rel="up" accesskey="u" href="#Use-of-GNU-Source_002dhighlight-Library">Use of GNU Source-highlight Library</a>
</div>
<h3 class="section">3.1 Using Automake and Autotools</h3>
<p><a name="index-autotools-8"></a><a name="index-pkg_002dconfig-9"></a>The easiest way to use GNU Source-highlight library in your program is
to rely on autotools, i.e., <em>Automake</em>, <em>Autoconf</em>, etc. In
particular, the library is installed with a
<code>pkg-config</code><a rel="footnote" href="#fn-2" name="fnd-2"><sup>2</sup></a>
configuration file (metadata file), <samp><span class="file">source-highlight.pc</span></samp>.
<p>pkg-config is a tool for helping compiling applications and
libraries. It helps you insert the correct compiler options on the
command line so an application can use Source-highlight library simply
by running
<pre class="example"> gcc -o test test.c `pkg-config --libs --cflags source-highlight`
</pre>
<p class="noindent">rather than hard-coding values on where to find the library. Moreover,
this will provide also with the correct compiler flags and libraries
used by Source-highlight library itself, e.g., Boost Regex library.
<p><a name="index-g_t_0040code_007bPKG_005fCONFIG_005fPATH_007d-10"></a>Note that <code>pkg-config</code> searches for <samp><span class="file">.pc</span></samp> files in its
standard directories. If you installed the library in a non standard
directory, you'll need to set the <code>PKG_CONFIG_PATH</code> environment
variable accordingly.
For instance, if I install the library into
<code>/usr/local/lib</code>, the <samp><span class="file">.pc</span></samp> file will be installed into
<code>/usr/local/lib/pkgconfig</code>, and then I'll need to call
<code>pkg-config</code> as follows:
<pre class="example"> PKG_CONFIG_PATH=/usr/local/lib/pkgconfig \
pkg-config --libs --cflags source-highlight
</pre>
<p>In your <samp><span class="file">configure.ac</span></samp> you can use the autoconf macro provided
by <code>pkg-config</code>; here is an example:
<pre class="example"> # Checks for libraries.
PKG_CHECK_MODULES(SRCHILITE, [source-highlight >= 3.0])
AC_SUBST(SRCHILITE_CFLAGS)
AC_SUBST(SRCHILITE_LIBS)
</pre>
<p>Then, you can use the variables <code>SRCHILITE_CFLAGS</code> and
<code>SRCHILITE_LIBS</code> in your makefiles accordingly.
For instance,
<pre class="example"> ...
AM_CPPFLAGS = $(SRCHILITE_CFLAGS)
...
LDADD = $(SRCHILITE_LIBS)
...
</pre>
<div class="node">
<a name="Main-Classes"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Global-instances">Global instances</a>,
Previous: <a rel="previous" accesskey="p" href="#Use-of-GNU-Source_002dhighlight-Library">Use of GNU Source-highlight Library</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">4 Main Classes</h2>
<p>Here we present the main classes of the Source-highlight library,
together with some example of use.
For the documentation of all the classes (and methods of the classes)
we refer to the generated API documentation (see See <a href="#Installation">Installation</a>).
<p>You will note that often, methods and constructors of the
classes of the libraries do not take a pointer or a reference
to a class, say <code>MyClass</code>, but an object of type <code>MyClassPtr</code>;
these are
<a name="index-shared-pointers-11"></a><em>shared pointers</em>, in particular the ones provided by the Boost
libraries (they are typedefs using, e.g.,
<code>boost::shared_ptr<MyClass></code>). This will avoid dangerous dangling
pointers and possible memory leaks in the library.
<p>If on the contrary, a method or a constructor in a class of the library
takes a standard pointer, say <code>MyClass *</code>, then that class will
NEVER delete such pointer. It is up to the actual owner the object of
<code>MyClass *</code> to delete the object when it is not needed anymore.
<p>The classes of the libraries can raise exceptions if errors are
encountered (e.g., an input file cannot be opened, or a language
definition file cannot be parsed); the exception classes can be found in
the API documentation, and all exception classes inherit from
<a name="index-g_t_0040code_007bstd_003a_003aexception_007d-class-12"></a><code>std::exception</code> class.
<ul class="menu">
<li><a accesskey="1" href="#SourceHighlight-class">SourceHighlight class</a>
<li><a accesskey="2" href="#Customizing-Formatting">Customizing Formatting</a>
<li><a accesskey="3" href="#Events-and-Listeners">Events and Listeners</a>
<li><a accesskey="4" href="#Settings">Settings</a>
<li><a accesskey="5" href="#Utility-functions">Utility functions</a>
</ul>
<div class="node">
<a name="SourceHighlight-class"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Customizing-Formatting">Customizing Formatting</a>,
Previous: <a rel="previous" accesskey="p" href="#Main-Classes">Main Classes</a>,
Up: <a rel="up" accesskey="u" href="#Main-Classes">Main Classes</a>
</div>
<h3 class="section">4.1 SourceHighlight class</h3>
<p>The <a name="index-g_t_0040code_007bSourceHighlight_007d-class-13"></a><code>SourceHighlight</code> class is the class of the library that basically
implements all the functionalities used by the program
<a name="index-g_t_0040code_007bsource_002dhighlight_007d-14"></a><code>source-highlight</code> itself; thus it highlights an input file generating
an output file. It can be configured with many options, and basically
it has a get/set methods for all the command line options of
<a name="index-g_t_0040code_007bsource_002dhighlight_007d-15"></a><code>source-highlight</code> (we refer also to see <a href="source-highlight.html#Invoking-source_002dhighlight">Invoking source-highlight</a>).
<p>For instance, the following example
(<samp><span class="file">source-highlight-console-main.cpp</span></samp>) highlights an input file to
the console (the colors are obtained through ANSI color escape sequences
(so you need a console program that supports this):
<!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite -->
<pre class="example">
<b>#ifdef</b> HAVE_CONFIG_H
<b>#include</b> <tt>"config.h"</tt>
<b>#endif</b>
<b>#include</b> <tt><iostream></tt>
<b>#include</b> <tt>"srchilite/sourcehighlight.h"</tt>
<b>#include</b> <tt>"srchilite/langmap.h"</tt>
<b>using</b> <b>namespace</b> std;
<b>#ifndef</b> DATADIR
<b>#define</b> DATADIR <tt>""</tt>
<b>#endif</b>
<b>int</b> main(<b>int</b> argc, <b>char</b> *argv[]) {
<i>// we highlight to the console, through ANSI escape sequences</i>
srchilite::SourceHighlight sourceHighlight(<tt>"esc.outlang"</tt>);
<i>// make sure we find the .lang and .outlang files</i>
sourceHighlight.setDataDir(DATADIR);
<i>// by default we highlight C++ code</i>
string inputLang = <tt>"cpp.lang"</tt>;
<b>if</b> (argc > 1) {
<i>// we have a file name so we detect the input source language</i>
srchilite::LangMap langMap(DATADIR, <tt>"lang.map"</tt>);
string lang = langMap.getMappedFileNameFromFileName(argv[1]);
<b>if</b> (lang != <tt>""</tt>) {
inputLang = lang;
} <i>// otherwise we default to C++</i>
<i>// output file name is empty => cout</i>
sourceHighlight.highlight(argv[1], <tt>""</tt>, inputLang);
} <b>else</b> {
<i>// input file name is empty => cin</i>
sourceHighlight.highlight(<tt>""</tt>, <tt>""</tt>, inputLang);
}
<b>return</b> 0;
}
</pre>
<p>Note that if a file name is passed at the command line, the program
tries to detect the source language by using a <a name="index-g_t_0040code_007bLangMap_007d-class-16"></a><code>LangMap</code> class
object, specifying the map file <samp><span class="file">lang.map</span></samp>, which is the one
mapping file extensions to language definition files (e.g., if the file
name has extension <samp><span class="file">.java</span></samp> it will use the corresponding
<samp><span class="file">java.lang</span></samp>). Otherwise we assume that we want to highlight
a C++ file.
<p>All the highlighting is performed by the <a name="index-g_t_0040code_007bhighlight_007d-method-17"></a><code>highlight</code> method; since
we don't specify an output file name it will output the highlighted
result directly to the console. In case we don't have an input filename
either, <a name="index-g_t_0040code_007bhighlight_007d-method-18"></a><code>highlight</code> method will read from the standard input. Since
the highlighting takes place one line per time, you can test the program
this way: you'll enter a line on the console and when you press enter,
the program will echo the same line highlighted.
<p>The <code>DATADIR</code> is not even mandatory, provided you installed
Source-highlight correctly, or that you set it up, using
<a name="index-g_t_0040code_007bsource_002dhighlight_002dsettings_007d-19"></a><code>source-highlight-settings</code> program.
<div class="node">
<a name="Customizing-Formatting"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Events-and-Listeners">Events and Listeners</a>,
Previous: <a rel="previous" accesskey="p" href="#SourceHighlight-class">SourceHighlight class</a>,
Up: <a rel="up" accesskey="u" href="#Main-Classes">Main Classes</a>
</div>
<h3 class="section">4.2 Customizing Formatting</h3>
<p>The formatting of Source-highlight library, i.e., how to actually perform
the highlighting, or what to do when we need to highlight something, can
be completely customized; the library detects (using regular expressions
based on language definition files) that something must be highlighted
as, say, a keyword, and you can then do whatever you want with this
information. The default formatting strategy is to output an
highlighted text using a specific formatting format, but you're free to
do whatever you like, if you want.
<p>This formatting abstraction is done through <a name="index-g_t_0040code_007bFormatter_007d-class-20"></a><code>Formatter</code> class, which
basically declares only the abstract <a name="index-g_t_0040code_007bformat_007d-method-21"></a><code>format</code> method which takes as
parameters the string to format, and further (possibly empty) additional
parameters, implemented by <a name="index-g_t_0040code_007bFormatterParams_007d-class-22"></a><code>FormatterParams</code> class. Note that the
<a name="index-g_t_0040code_007bformat_007d-method-23"></a><code>format</code> method does not get as an argument how the passed string
must be formatted (e.g., as a keyword, as a type, etc.); this
information must be stored in the formatter from the start. Indeed, the
mapping between a language element and a formatter is performed by
<a name="index-g_t_0040code_007bFormatterManager_007d-class-24"></a><code>FormatterManager</code> class. An object of this class must be created
by specifying a default formatter object, that will be used when the
formatter manager will be queried for a formatter for a specific
language element that it is not able to handle (in this it will fall
back by returning the default formatter).
<ul class="menu">
<li><a accesskey="1" href="#Completely-Customized-Formatting">Completely Customized Formatting</a>
<li><a accesskey="2" href="#Style_002dbased-Customized-Formatting">Style-based Customized Formatting</a>
</ul>
<div class="node">
<a name="Completely-Customized-Formatting"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Style_002dbased-Customized-Formatting">Style-based Customized Formatting</a>,
Previous: <a rel="previous" accesskey="p" href="#Customizing-Formatting">Customizing Formatting</a>,
Up: <a rel="up" accesskey="u" href="#Customizing-Formatting">Customizing Formatting</a>
</div>
<h4 class="subsection">4.2.1 Completely Customized Formatting</h4>
<p>You can implement a completely customized formatting strategy. For
instance, this is a customized formatter (<samp><span class="file">infoformatter.h</span></samp>) which,
when requested to format a string, it simply writes this information
specifying which kind of language element it is, and the position in the
line (the <a name="index-g_t_0040code_007bstart_007d-field-25"></a><code>start</code> field in <a name="index-g_t_0040code_007bFormatterParams_007d-class-26"></a><code>FormatterParams</code> class). Note that
the language element is stored in a field of the class, and it is set at
object creation time. We avoid to write anything if we are requested to
format something as <code>"normal"</code>, or if the string to format is empty.
<!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite -->
<pre class="example"> <b>class</b> InfoFormatter: <b>public</b> srchilite::Formatter {
<i>/// the language element represented by this formatter</i>
std::string elem;
<b>public</b>:
InfoFormatter(<b>const</b> std::string &elem_ = <tt>"normal"</tt>) :
elem(elem_) {
}
<b>virtual</b> <b>void</b> format(<b>const</b> std::string &s,
<b>const</b> srchilite::FormatterParams *params = 0) {
<i>// do not print anything if normal or string to format is empty</i>
<b>if</b> (elem != <tt>"normal"</tt> || !s.size()) {
std::cout << elem << <tt>": "</tt> << s;
<b>if</b> (params)
std::cout << <tt>", start: "</tt> << params->start;
std::cout << std::endl;
}
}
};
<i>/// shared pointer for InfoFormatter</i>
<b>typedef</b> boost::shared_ptr<InfoFormatter> InfoFormatterPtr;
</pre>
<p>For convenience we also declare a typedef for the shared pointer (since
the formatter manager takes only shared pointers to formatters).
<p>In order to customize the formatting, there are some more steps
to do, and in particular, you cannot use <a name="index-g_t_0040code_007bSourceHighlight_007d-class-27"></a><code>SourceHighlight</code> class anymore
but you need to use more classes.
<p>First of all, you need <a name="index-g_t_0040code_007bLangDefManager_007d-class-28"></a><code>LangDefManager</code> class which takes care of
building the regular expressions starting from a language definition
file; in order to do this it uses a <a name="index-g_t_0040code_007bHighlightRuleFactory_007d-class-29"></a><code>HighlightRuleFactory</code> class
object; for the moment, only the implementation based on boost regular
expression exists, so you can simply pass an object of
<a name="index-g_t_0040code_007bRegexRuleFactory_007d-class-30"></a><code>RegexRuleFactory</code> class. Once you have an object of
<a name="index-g_t_0040code_007bLangDefManager_007d-class-31"></a><code>LangDefManager</code> class, you can use the
<a name="index-g_t_0040code_007bgetHighlightState_007d-method-32"></a><code>getHighlightState</code> method to build the
<a name="index-automaton-33"></a>automaton to perform the
highlight (in particular the initial state of such automaton, of
<a name="index-g_t_0040code_007bHighlightState_007d-class-34"></a><code>HighlightState</code> class), and you should pass this to an object that
can use the automaton to perform the highlighting. To do this, you can
use <a name="index-g_t_0040code_007bSourceHighlighter_007d-class-35"></a><code>SourceHighlighter</code> class whose objects can be used to highlight
a line of text, using <a name="index-g_t_0040code_007bhighlightParagraph_007d-method-36"></a><code>highlightParagraph</code> method.
<p>You can then create a <a name="index-g_t_0040code_007bFormatterManager_007d-class-37"></a><code>FormatterManager</code> class object and populate
it with your formatters and set it to the <a name="index-g_t_0040code_007bSourceHighlighter_007d-class-38"></a><code>SourceHighlighter</code> class
object. The following example (<samp><span class="file">infoformatter-main.cpp</span></samp>) shows how
to perform these steps; note that we can share the same formatter for
different language elements:
<!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite -->
<pre class="example">
<b>#ifdef</b> HAVE_CONFIG_H
<b>#include</b> <tt>"config.h"</tt>
<b>#endif</b>
<b>#include</b> <tt><iostream></tt>
<b>#include</b> <tt>"srchilite/langdefmanager.h"</tt>
<b>#include</b> <tt>"srchilite/regexrulefactory.h"</tt>
<b>#include</b> <tt>"srchilite/sourcehighlighter.h"</tt>
<b>#include</b> <tt>"srchilite/formattermanager.h"</tt>
<b>#include</b> <tt>"infoformatter.h"</tt>
<b>using</b> <b>namespace</b> std;
<b>#ifndef</b> DATADIR
<b>#define</b> DATADIR <tt>""</tt>
<b>#endif</b>
<b>int</b> main() {
srchilite::RegexRuleFactory ruleFactory;
srchilite::LangDefManager langDefManager(&ruleFactory);
<i>// we highlight C++ code for simplicity</i>
srchilite::SourceHighlighter highlighter(langDefManager.getHighlightState(
DATADIR, <tt>"cpp.lang"</tt>));
srchilite::FormatterManager formatterManager(InfoFormatterPtr(
<b>new</b> InfoFormatter));
InfoFormatterPtr keywordFormatter(<b>new</b> InfoFormatter(<tt>"keyword"</tt>));
formatterManager.addFormatter(<tt>"keyword"</tt>, keywordFormatter);
formatterManager.addFormatter(<tt>"string"</tt>, InfoFormatterPtr(<b>new</b> InfoFormatter(
<tt>"string"</tt>)));
<i>// for "type" we use the same formatter as for "keyword"</i>
formatterManager.addFormatter(<tt>"type"</tt>, keywordFormatter);
formatterManager.addFormatter(<tt>"comment"</tt>, InfoFormatterPtr(
<b>new</b> InfoFormatter(<tt>"comment"</tt>)));
formatterManager.addFormatter(<tt>"symbol"</tt>, InfoFormatterPtr(<b>new</b> InfoFormatter(
<tt>"symbol"</tt>)));
formatterManager.addFormatter(<tt>"number"</tt>, InfoFormatterPtr(<b>new</b> InfoFormatter(
<tt>"number"</tt>)));
formatterManager.addFormatter(<tt>"preproc"</tt>, InfoFormatterPtr(
<b>new</b> InfoFormatter(<tt>"preproc"</tt>)));
highlighter.setFormatterManager(&formatterManager);
<i>// make sure it uses additional information</i>
srchilite::FormatterParams params;
highlighter.setFormatterParams(&params);
string line;
<i>// we now highlight a line a time</i>
<b>while</b> (getline(cin, line)) {
<i>// reset position counter within a line</i>
params.start = 0;
highlighter.highlightParagraph(line);
}
<b>return</b> 0;
}
</pre>
<p>Note that, since we highlight a line a time, we must reset the
<a name="index-g_t_0040code_007bstart_007d-field-39"></a><code>start</code> field each time we start to examine a new line.
<p>For simplicity this example highlights only C++ code and reads directly
from the standard input and writes to the standard output. This is a
run of the example reading from the standard input (so each time you
insert a line you get the output of your formatters):
<pre class="example"> // this is a comment
comment: //, start: 0
comment: this is a comment, start: 2
#include <foobar.h>
preproc: #include, start: 0
string: <foobar.h>, start: 9
int abc = 100 + 5;
keyword: int, start: 0
symbol: =, start: 8
number: 100, start: 10
symbol: +, start: 14
number: 5, start: 16
symbol: ;, start: 17
</pre>
<div class="node">
<a name="Style-based-Customized-Formatting"></a>
<a name="Style_002dbased-Customized-Formatting"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#Completely-Customized-Formatting">Completely Customized Formatting</a>,
Up: <a rel="up" accesskey="u" href="#Customizing-Formatting">Customizing Formatting</a>
</div>
<h4 class="subsection">4.2.2 Style-based Customized Formatting</h4>
<p>Source-highlight can rely on style (and css style) files for generating
formatting. Usually, the formatters are built according to the output
format, specified through <samp><span class="file">.outlang</span></samp> files, see <a href="source-highlight.html#Output-Language-Definitions">Output Language Definitions</a>. However, you can also create
your own formatters based on the information of the style file (or css
style file). During the parsing of these style files, a
<a name="index-g_t_0040code_007bFormatterFactory_007d-class-40"></a><code>FormatterFactory</code> class object is used by the library, and you can
provide a customized factory (the one that is used by the library is
<a name="index-g_t_0040code_007bTextStyleFormatterFactory_007d-class-41"></a><code>TextStyleFormatterFactory</code> class). The only abstract method
of <a name="index-g_t_0040code_007bFormatterFactory_007d-class-42"></a><code>FormatterFactory</code> class is <a name="index-g_t_0040code_007bcreateFormatter_007d-method-43"></a><code>createFormatter</code> method.
<p>In order to parse a style file, you can use the static methods of the
<a name="index-g_t_0040code_007bStyleFileParser_007d-class-44"></a><code>StyleFileParser</code> class, which require the file name of the style
file (and possibly the path to search for the style file, otherwise the
default one is used), the factory to create formatters, and a reference
to a string where the document background color will be stored. The
methods are <a name="index-g_t_0040code_007bparseStyleFile_007d-method-45"></a><code>parseStyleFile</code> method and
<a name="index-g_t_0040code_007bparseCssStyleFile_007d-method-46"></a><code>parseCssStyleFile</code> method.
<p>For instance, let's create a customized formatter
<samp><span class="file">styleformatter.h</span></samp> that simply prints how a language element will
be formatted (but no formatting will take place); for the sake of
simplicity we will use only public fields:
<!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite -->
<pre class="example"> <b>struct</b> StyleFormatter: <b>public</b> srchilite::Formatter {
<i>/// the language element represented by this formatter</i>
std::string elem;
<b>bool</b> bold, italic, underline, fixed, not_fixed;
std::string color;
std::string bgColor;
StyleFormatter(<b>const</b> std::string &elem_ = <tt>"normal"</tt>) :
elem(elem_), bold(<b>false</b>), italic(<b>false</b>), underline(<b>false</b>),
fixed(<b>false</b>), not_fixed(<b>false</b>) {
}
<b>virtual</b> <b>void</b> format(<b>const</b> std::string &s,
<b>const</b> srchilite::FormatterParams *params = 0) {
<i>// do not print anything if normal or string to format is empty</i>
<b>if</b> (elem != <tt>"normal"</tt> || !s.size()) {
std::cout << elem << <tt>": </tt>\"<tt>"</tt> << s << <tt>"</tt>\"<tt>"</tt> << std::endl;
std::cout << <tt>"formatted as: "</tt> << (bold ? <tt>"bold "</tt> : <tt>""</tt>)
<< (italic ? <tt>"italic "</tt> : <tt>""</tt>) << (underline ? <tt>"underline "</tt>
: <tt>""</tt>);
std::cout << (color.size() ? <tt>"color: "</tt> + color + <tt>" "</tt> : <tt>""</tt>);
std::cout << (bgColor.size() ? <tt>"bgcolor: "</tt> + bgColor : <tt>""</tt>)
<< std::endl;
}
}
};
<i>/// shared pointer for StyleFormatter</i>
<b>typedef</b> boost::shared_ptr<StyleFormatter> StyleFormatterPtr;
</pre>
<p>Now, we create a customized factory (file
<samp><span class="file">styleformatterfactory.h</span></samp>), implementing the method
<a name="index-g_t_0040code_007bcreateFormatter_007d-method-47"></a><code>createFormatter</code> method. Note that the base class
<a name="index-g_t_0040code_007bFormatterFactory_007d-class-48"></a><code>FormatterFactory</code> class does not provide any means to store the
created formatters, so it's up to the derived classes to store the
created formatters somewhere:
<!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite -->
<pre class="example"> <b>struct</b> StyleFormatterFactory: <b>public</b> srchilite::FormatterFactory {
StyleFormatterMap formatterMap;
<b>bool</b> hasFormatter(<b>const</b> string &key) <b>const</b> {
<b>return</b> formatterMap.find(key) != formatterMap.end();
}
<b>bool</b> createFormatter(<b>const</b> string &key, <b>const</b> string &color,
<b>const</b> string &bgcolor,
srchilite::StyleConstantsPtr styleconstants) {
<b>if</b> (hasFormatter(key))
<b>return</b> <b>false</b>;
StyleFormatter *formatter = <b>new</b> StyleFormatter(key);
formatterMap[key] = StyleFormatterPtr(formatter);
<b>if</b> (styleconstants.get()) {
<b>for</b> (srchilite::StyleConstantsIterator it =
styleconstants->begin(); it
!= styleconstants->end(); ++it) {
<b>switch</b> (*it) {
<b>case</b> srchilite::ISBOLD:
formatter->bold = <b>true</b>;
<b>break</b>;
<b>case</b> srchilite::ISITALIC:
formatter->italic = <b>true</b>;
<b>break</b>;
<b>case</b> srchilite::ISUNDERLINE:
formatter->underline = <b>true</b>;
<b>break</b>;
<b>case</b> srchilite::ISFIXED:
formatter->fixed = <b>true</b>;
<b>break</b>;
<b>case</b> srchilite::ISNOTFIXED:
formatter->not_fixed = <b>true</b>;
<b>break</b>;
<b>case</b> srchilite::ISNOREF: <i>// ignore references here</i>
<b>break</b>;
}
}
}
formatter->color = color;
formatter->bgColor = bgcolor;
<b>return</b> <b>true</b>;
}
};
</pre>
<p>The <a name="index-g_t_0040code_007bcreateFormatter_007d-method-49"></a><code>createFormatter</code> method will be called when parsing a style
file to create a formatter corresponding to a specific language element;
this method should return false if the creation of a formatter failed
(e.g., in this case, if a formatter for a given element had already been
created). The method is passed the language element name, the colors
for the element as specified in the style file (that can be empty if no
color was specified), and a <a name="index-g_t_0040code_007bStyleConstants_007d-enum-50"></a><code>StyleConstants</code> enum shared pointer
with formatting informations such as, boldface, italics, etc. The
factory can use this information to create the customized formatter.
<p>Now, we can use this customized formatter factory in our program
(file <samp><span class="file">styleformatter-main.cpp</span></samp>):
<!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite -->
<pre class="example">
<b>#ifdef</b> HAVE_CONFIG_H
<b>#include</b> <tt>"config.h"</tt>
<b>#endif</b>
<b>#include</b> <tt><iostream></tt>
<b>#include</b> <tt>"srchilite/langdefmanager.h"</tt>
<b>#include</b> <tt>"srchilite/regexrulefactory.h"</tt>
<b>#include</b> <tt>"srchilite/sourcehighlighter.h"</tt>
<b>#include</b> <tt>"srchilite/formattermanager.h"</tt>
<b>#include</b> <tt>"srchilite/stylefileparser.h"</tt> <i>// for parsing style files</i>
<b>#include</b> <tt>"styleformatterfactory.h"</tt>
<b>using</b> <b>namespace</b> std;
<b>#ifndef</b> DATADIR
<b>#define</b> DATADIR <tt>""</tt>
<b>#endif</b>
<b>int</b> main() {
srchilite::RegexRuleFactory ruleFactory;
srchilite::LangDefManager langDefManager(&ruleFactory);
<i>// we highlight C++ code for simplicity</i>
srchilite::SourceHighlighter highlighter(langDefManager.getHighlightState(
DATADIR, <tt>"cpp.lang"</tt>));
<i>// our factory for our formatters</i>
StyleFormatterFactory factory;
<i>// the background color for the document (not used here)</i>
string docBgColor;
<i>// let's parse the default.style and create our formatters</i>
srchilite::StyleFileParser::parseStyleFile(DATADIR, <tt>"default.style"</tt>,
&factory, docBgColor);
<i>// now we need to fill up the formatter manager with our formatters</i>
srchilite::FormatterManager formatterManager(StyleFormatterPtr(
<b>new</b> StyleFormatter));
<b>for</b> (StyleFormatterMap::const_iterator it = factory.formatterMap.begin(); it
!= factory.formatterMap.end(); ++it) {
formatterManager.addFormatter(it->first, it->second);
}
highlighter.setFormatterManager(&formatterManager);
string line;
<i>// we now highlight a line a time</i>
<b>while</b> (getline(cin, line)) {
highlighter.highlightParagraph(line);
}
<b>return</b> 0;
}
</pre>
<p>Note that, once we created all the formatters with our factory (while
parsing the style file <samp><span class="file">default.style</span></samp>), we still need to manually
set these formatters in the <a name="index-g_t_0040code_007bFormatterManager_007d-class-51"></a><code>FormatterManager</code> class object used by
our highlighter.
<p>For simplicity this example highlights only C++ code and reads directly
from the standard input and writes to the standard output. This is a
run of the example reading from the standard input (so each time you
insert a line you get the output of your formatters):
<pre class="example"> /// my class TODO: nothing special
comment: "///"
formatted as: italic color: brown
comment: " my class "
formatted as: italic color: brown
todo: "TODO:"
formatted as: bold bgcolor: cyan
comment: " nothing special"
formatted as: italic color: brown
#include <foobar.h>
preproc: "#include"
formatted as: bold color: darkblue
string: "<foobar.h>"
formatted as: color: red
</pre>
<div class="node">
<a name="Events-and-Listeners"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Settings">Settings</a>,
Previous: <a rel="previous" accesskey="p" href="#Customizing-Formatting">Customizing Formatting</a>,
Up: <a rel="up" accesskey="u" href="#Main-Classes">Main Classes</a>
</div>
<h3 class="section">4.3 Events and Listeners</h3>
<p>During the highlighting (and regular expression matching) the library
generates events that can be “listened” by using a customized event
listener. An event is represented by an object of
<a name="index-g_t_0040code_007bHighlightEvent_007d-class-52"></a><code>HighlightEvent</code> class, which stores the <a name="index-g_t_0040code_007bHighlightToken_007d-class-53"></a><code>HighlightToken</code> class
object and the type (an <a name="index-g_t_0040code_007bHighlightEventType_007d-enum-54"></a><code>HighlightEventType</code> enum) of the event.
<p>A customized listener can be implemented by deriving from
<a name="index-g_t_0040code_007bHighlightEventListener_007d-class-55"></a><code>HighlightEventListener</code> class and by defining the virtual method
<a name="index-g_t_0040code_007bnotify_007d-method-56"></a><code>notify</code> method, which, of course, takes an
<a name="index-g_t_0040code_007bHighlightEvent_007d-class-57"></a><code>HighlightEvent</code> class object as parameter.
<p>For instance, <a name="index-g_t_0040code_007bsource_002dhighlight_007d-58"></a><code>source-highlight</code> implements the debugging
functionalities by using a customized listener,
<a name="index-g_t_0040code_007bDebugListener_007d-class-59"></a><code>DebugListener</code> class, whose method implementation we report here as
an example:
<!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite -->
<pre class="example"> <b>void</b> DebugListener::notify(<b>const</b> HighlightEvent &event) {
<b>switch</b> (event.type) {
<b>case</b> HighlightEvent::FORMAT:
<i>// print information about the rule</i>
<b>if</b> (event.token.rule) {
os << event.token.rule->getAdditionalInfo() << endl;
os << <tt>"expression: </tt>\"<tt>"</tt> << event.token.rule->toString() << <tt>"</tt>\"<tt>"</tt>
<< endl;
}
<i>// now format the matched strings</i>
<b>for</b> (MatchedElements::const_iterator it = event.token.matched.begin(); it
!= event.token.matched.end(); ++it) {
os << <tt>"formatting </tt>\"<tt>"</tt> << it->second << <tt>"</tt>\"<tt> as "</tt> << it->first
<< endl;
}
step();
<b>break</b>;
<b>case</b> HighlightEvent::FORMATDEFAULT:
os << <tt>"formatting </tt>\"<tt>"</tt> << event.token.matched.front().second
<< <tt>"</tt>\"<tt> as default"</tt> << endl;
step();
<b>break</b>;
<b>case</b> HighlightEvent::ENTERSTATE:
os << <tt>"entering state: "</tt> << event.token.rule->getNextState()->getId()
<< endl;
<b>break</b>;
<b>case</b> HighlightEvent::EXITSTATE:
<b>int</b> level = event.token.rule->getExitLevel();
os << <tt>"exiting state, level: "</tt>;
<b>if</b> (level < 0)
os << <tt>"all"</tt>;
<b>else</b>
os << level;
os << endl;
<b>break</b>;
}
}
</pre>
<div class="node">
<a name="Settings"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Utility-functions">Utility functions</a>,
Previous: <a rel="previous" accesskey="p" href="#Events-and-Listeners">Events and Listeners</a>,
Up: <a rel="up" accesskey="u" href="#Main-Classes">Main Classes</a>
</div>
<h3 class="section">4.4 Settings</h3>
<p>Source-highlight library reads language map files, language definition files,
output format definitions, styles, and other files it needs during the
execution from a specific directory, which we call
<a name="index-data-dir-60"></a><em>data dir</em>;
the library comes with an hardcoded value for this path, which is
based on the <code>--prefix</code> value specified at configuration
time (in particular, it is <code>prefix/share/source-highlight</code>).
In particular, the user can set the value also with the environment
variable
<a name="index-g_t_0040code_007bSOURCE_005fHIGHLIGHT_005fDATADIR_007d-61"></a><code>SOURCE_HIGHLIGHT_DATADIR</code> (see also the program
<a name="index-g_t_0040code_007bsource_002dhighlight_002dsettings_007d-62"></a><code>source-highlight-settings</code> which can store settings in a configuration
file of the user's home, see <a href="source-highlight.html#The-program-source_002dhighlight_002dsettings">The program source-highlight-settings</a>).
<p>When running the program <a name="index-g_t_0040code_007bsource_002dhighlight_007d-63"></a><code>source-highlight</code> this value can be overridden with
the command line option <code>--data-dir</code>
(see <a href="source-highlight.html#Configuration-files">Configuration files</a>).
<p>When using the Source-highlight library from a program, one might need to
change the value for data dir, dynamically, and in a consistent way,
i.e., to have a static and single point where this setting can be set
and retrieved. Note that for the moment, the only setting you can
manage is the value of data dir.
<p>The library provides the <a name="index-g_t_0040code_007bSettings_007d-class-64"></a><code>Settings</code> class for this purpose.
Although you can create objects of this class to mainuplate, check and
save settings (you may want to look at the source code of the program
<a name="index-g_t_0040code_007bsource_002dhighlight_002dsettings_007d-65"></a><code>source-highlight-settings</code>), you probably only need the static methods
of this class. You can set the global value of data dir with the
<a name="index-g_t_0040code_007bsetGlobalDataDir_007d-method-66"></a><code>setGlobalDataDir</code> method. The <a name="index-g_t_0040code_007bretrieveDataDir_007d-method-67"></a><code>retrieveDataDir</code> method
retrieves the value for the data dir. If the global value was set with
<a name="index-g_t_0040code_007bsetGlobalDataDir_007d-method-68"></a><code>setGlobalDataDir</code> method then always returns this global
value. Otherwise, it returns the value of the environment variable
<a name="index-g_t_0040code_007bSOURCE_005fHIGHLIGHT_005fDATADIR_007d-69"></a><code>SOURCE_HIGHLIGHT_DATADIR</code> if set. Otherwise, it returns the value
read from the configuration file. If also the reading of configuration
file fails, then it returns the hardcoded value.
<div class="node">
<a name="Utility-functions"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#Settings">Settings</a>,
Up: <a rel="up" accesskey="u" href="#Main-Classes">Main Classes</a>
</div>
<h3 class="section">4.5 Utility functions</h3>
<p>If you need to get a list of all the files in the data dir with a
specific role (e.g., language definition files, style files, etc.) you
can use the static methods of the <a name="index-g_t_0040code_007bSourceHighlightUtils_007d-class-70"></a><code>SourceHighlightUtils</code> class,
which will take care of using the data dir specified in the settings
(<a href="#Settings">Settings</a>).
<div class="node">
<a name="Global-instances"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Problems">Problems</a>,
Previous: <a rel="previous" accesskey="p" href="#Main-Classes">Main Classes</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h3 class="section">4.6 Global instances</h3>
<p>The <a name="index-g_t_0040code_007bInstances_007d-class-71"></a><code>Instances</code> class provides access to static instances of some
classes that can be used, e.g., to read a language definition file and
create the <a name="index-automaton-72"></a>automaton for the highlighting, using
<a name="index-g_t_0040code_007bLangDefManager_007d-class-73"></a><code>LangDefManager</code> class, or to access the map of language definition
files, using <a name="index-g_t_0040code_007bLangMap_007d-class-74"></a><code>LangMap</code> class. This class ensures that these
instances use the global settings; in particular, if you change the
global settings, you should call the static <a name="index-g_t_0040code_007breload_007d-method-75"></a><code>reload</code> method, so that
the instances are updated.
<p>Using these instances also makes the use of some classes easier; for
instance, the beginning part of the <code>main</code> of the examples shown in
<a href="#Customizing-Formatting">Customizing Formatting</a> can be written as follows:
<!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite -->
<pre class="example"> <b>#include</b> <tt>"srchilite/langdefmanager.h"</tt>
<b>#include</b> <tt>"srchilite/instances.h"</tt>
<b>int</b> main() {
<i>// we highlight C++ code for simplicity</i>
srchilite::SourceHighlighter highlighter
(srchilite::Instances::getLangDefManager().getHighlightState(
DATADIR, <tt>"cpp.lang"</tt>));
</pre>
<p>If you know that you will not use these instances anymore in your
application, and it is crucial to recover all the memory used by these
instances, you then need to call the static <a name="index-g_t_0040code_007bunload_007d-method-76"></a><code>unload</code> method, and the
memory of these instances will be released.
<div class="node">
<a name="Problems"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Mailing-Lists">Mailing Lists</a>,
Previous: <a rel="previous" accesskey="p" href="#Global-instances">Global instances</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">5 Reporting Bugs</h2>
<p><a name="index-bugs-77"></a><a name="index-problems-78"></a>
If you find a bug in <samp><span class="command">source-highlight</span></samp>, please send electronic
mail to
<p><code>bug-source-highlight at gnu dot org</code>
<p>Include the version
number, which you can find by running ‘<samp><span class="samp">source-highlight --version</span></samp>’<!-- /@w -->. Also include in your message the output that the program
produced and the output you expected.
<p>If you have other questions, comments or suggestions about
<samp><span class="command">source-highlight</span></samp>, contact the author via electronic mail
(find the address at <a href="http://www.lorenzobettini.it">http://www.lorenzobettini.it</a>). The author will try to help
you out, although he may not have time to fix your problems.
<div class="node">
<a name="Mailing-Lists"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Concept-Index">Concept Index</a>,
Previous: <a rel="previous" accesskey="p" href="#Problems">Problems</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">6 Mailing Lists</h2>
<p><a name="index-mailing-list-79"></a>
The following mailing lists are available:
<p><code>help-source-highlight at gnu dot org</code>
<p>for generic discussions about the program and for asking for help about
it (open mailing list),
<a href="http://mail.gnu.org/mailman/listinfo/help-source-highlight">http://mail.gnu.org/mailman/listinfo/help-source-highlight</a>
<p><code>info-source-highlight at gnu dot org</code>
<p>for receiving information about new releases and features (read-only
mailing list),
<a href="http://mail.gnu.org/mailman/listinfo/info-source-highlight">http://mail.gnu.org/mailman/listinfo/info-source-highlight</a>.
<p>If you want to subscribe to a mailing list just go to the URL and follow
the instructions, or send me an e-mail and I'll subscribe you.
<p>I'll describe new features in new releases also in my blog, at
this URL:
<p><a href="http://tronprog.blogspot.com/search/label/source-highlight">http://tronprog.blogspot.com/search/label/source-highlight</a>
<div class="node">
<a name="Concept-Index"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#Mailing-Lists">Mailing Lists</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="unnumbered">Concept Index</h2>
<p><a name="index-tail-recursion-80"></a>
<ul class="index-cp" compact>
<li><a href="#index-g_t_0040code_007b_002d_002dwith_002ddoxygen_007d-3"><code>--with-doxygen</code></a>: <a href="#Installation">Installation</a></li>
<li><a href="#index-automaton-72">automaton</a>: <a href="#Global-instances">Global instances</a></li>
<li><a href="#index-automaton-33">automaton</a>: <a href="#Completely-Customized-Formatting">Completely Customized Formatting</a></li>
<li><a href="#index-autotools-8">autotools</a>: <a href="#Using-Automake-and-Autotools">Using Automake and Autotools</a></li>
<li><a href="#index-bugs-77">bugs</a>: <a href="#Problems">Problems</a></li>
<li><a href="#index-g_t_0040code_007bcreateFormatter_007d-method-43"><code>createFormatter</code> method</a>: <a href="#Style_002dbased-Customized-Formatting">Style-based Customized Formatting</a></li>
<li><a href="#index-data-dir-60">data dir</a>: <a href="#Settings">Settings</a></li>
<li><a href="#index-g_t_0040code_007bDebugListener_007d-class-59"><code>DebugListener</code> class</a>: <a href="#Events-and-Listeners">Events and Listeners</a></li>
<li><a href="#index-doxygen-4">doxygen</a>: <a href="#Installation">Installation</a></li>
<li><a href="#index-features-2">features</a>: <a href="#Introduction">Introduction</a></li>
<li><a href="#index-g_t_0040code_007bformat_007d-method-21"><code>format</code> method</a>: <a href="#Customizing-Formatting">Customizing Formatting</a></li>
<li><a href="#index-g_t_0040code_007bFormatter_007d-class-20"><code>Formatter</code> class</a>: <a href="#Customizing-Formatting">Customizing Formatting</a></li>
<li><a href="#index-g_t_0040code_007bFormatterFactory_007d-class-40"><code>FormatterFactory</code> class</a>: <a href="#Style_002dbased-Customized-Formatting">Style-based Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bFormatterManager_007d-class-51"><code>FormatterManager</code> class</a>: <a href="#Style_002dbased-Customized-Formatting">Style-based Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bFormatterManager_007d-class-37"><code>FormatterManager</code> class</a>: <a href="#Completely-Customized-Formatting">Completely Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bFormatterManager_007d-class-24"><code>FormatterManager</code> class</a>: <a href="#Customizing-Formatting">Customizing Formatting</a></li>
<li><a href="#index-g_t_0040code_007bFormatterParams_007d-class-26"><code>FormatterParams</code> class</a>: <a href="#Completely-Customized-Formatting">Completely Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bFormatterParams_007d-class-22"><code>FormatterParams</code> class</a>: <a href="#Customizing-Formatting">Customizing Formatting</a></li>
<li><a href="#index-g_t_0040code_007bgetHighlightState_007d-method-32"><code>getHighlightState</code> method</a>: <a href="#Completely-Customized-Formatting">Completely Customized Formatting</a></li>
<li><a href="#index-headers-7">headers</a>: <a href="#Use-of-GNU-Source_002dhighlight-Library">Use of GNU Source-highlight Library</a></li>
<li><a href="#index-g_t_0040code_007bhighlight_007d-method-17"><code>highlight</code> method</a>: <a href="#SourceHighlight-class">SourceHighlight class</a></li>
<li><a href="#index-g_t_0040code_007bHighlightEvent_007d-class-52"><code>HighlightEvent</code> class</a>: <a href="#Events-and-Listeners">Events and Listeners</a></li>
<li><a href="#index-g_t_0040code_007bHighlightEventListener_007d-class-55"><code>HighlightEventListener</code> class</a>: <a href="#Events-and-Listeners">Events and Listeners</a></li>
<li><a href="#index-g_t_0040code_007bHighlightEventType_007d-enum-54"><code>HighlightEventType</code> enum</a>: <a href="#Events-and-Listeners">Events and Listeners</a></li>
<li><a href="#index-g_t_0040code_007bhighlightParagraph_007d-method-36"><code>highlightParagraph</code> method</a>: <a href="#Completely-Customized-Formatting">Completely Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bHighlightRuleFactory_007d-class-29"><code>HighlightRuleFactory</code> class</a>: <a href="#Completely-Customized-Formatting">Completely Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bHighlightState_007d-class-34"><code>HighlightState</code> class</a>: <a href="#Completely-Customized-Formatting">Completely Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bHighlightToken_007d-class-53"><code>HighlightToken</code> class</a>: <a href="#Events-and-Listeners">Events and Listeners</a></li>
<li><a href="#index-g_t_0040code_007bInstances_007d-class-71"><code>Instances</code> class</a>: <a href="#Global-instances">Global instances</a></li>
<li><a href="#index-introduction-1">introduction</a>: <a href="#Introduction">Introduction</a></li>
<li><a href="#index-g_t_0040code_007bLangDefManager_007d-class-73"><code>LangDefManager</code> class</a>: <a href="#Global-instances">Global instances</a></li>
<li><a href="#index-g_t_0040code_007bLangDefManager_007d-class-28"><code>LangDefManager</code> class</a>: <a href="#Completely-Customized-Formatting">Completely Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bLangMap_007d-class-74"><code>LangMap</code> class</a>: <a href="#Global-instances">Global instances</a></li>
<li><a href="#index-g_t_0040code_007bLangMap_007d-class-16"><code>LangMap</code> class</a>: <a href="#SourceHighlight-class">SourceHighlight class</a></li>
<li><a href="#index-libsource_002dhighlight-5">libsource-highlight</a>: <a href="#Use-of-GNU-Source_002dhighlight-Library">Use of GNU Source-highlight Library</a></li>
<li><a href="#index-mailing-list-79">mailing list</a>: <a href="#Mailing-Lists">Mailing Lists</a></li>
<li><a href="#index-namespace-6">namespace</a>: <a href="#Use-of-GNU-Source_002dhighlight-Library">Use of GNU Source-highlight Library</a></li>
<li><a href="#index-g_t_0040code_007bnotify_007d-method-56"><code>notify</code> method</a>: <a href="#Events-and-Listeners">Events and Listeners</a></li>
<li><a href="#index-g_t_0040code_007bparseCssStyleFile_007d-method-46"><code>parseCssStyleFile</code> method</a>: <a href="#Style_002dbased-Customized-Formatting">Style-based Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bparseStyleFile_007d-method-45"><code>parseStyleFile</code> method</a>: <a href="#Style_002dbased-Customized-Formatting">Style-based Customized Formatting</a></li>
<li><a href="#index-pkg_002dconfig-9">pkg-config</a>: <a href="#Using-Automake-and-Autotools">Using Automake and Autotools</a></li>
<li><a href="#index-g_t_0040code_007bPKG_005fCONFIG_005fPATH_007d-10"><code>PKG_CONFIG_PATH</code></a>: <a href="#Using-Automake-and-Autotools">Using Automake and Autotools</a></li>
<li><a href="#index-problems-78">problems</a>: <a href="#Problems">Problems</a></li>
<li><a href="#index-g_t_0040code_007bRegexRuleFactory_007d-class-30"><code>RegexRuleFactory</code> class</a>: <a href="#Completely-Customized-Formatting">Completely Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007breload_007d-method-75"><code>reload</code> method</a>: <a href="#Global-instances">Global instances</a></li>
<li><a href="#index-g_t_0040code_007bretrieveDataDir_007d-method-67"><code>retrieveDataDir</code> method</a>: <a href="#Settings">Settings</a></li>
<li><a href="#index-g_t_0040code_007bsetGlobalDataDir_007d-method-66"><code>setGlobalDataDir</code> method</a>: <a href="#Settings">Settings</a></li>
<li><a href="#index-g_t_0040code_007bSettings_007d-class-64"><code>Settings</code> class</a>: <a href="#Settings">Settings</a></li>
<li><a href="#index-shared-pointers-11">shared pointers</a>: <a href="#Main-Classes">Main Classes</a></li>
<li><a href="#index-g_t_0040code_007bsource_002dhighlight_007d-63"><code>source-highlight</code></a>: <a href="#Settings">Settings</a></li>
<li><a href="#index-g_t_0040code_007bsource_002dhighlight_007d-58"><code>source-highlight</code></a>: <a href="#Events-and-Listeners">Events and Listeners</a></li>
<li><a href="#index-g_t_0040code_007bsource_002dhighlight_007d-14"><code>source-highlight</code></a>: <a href="#SourceHighlight-class">SourceHighlight class</a></li>
<li><a href="#index-g_t_0040code_007bsource_002dhighlight_002dsettings_007d-62"><code>source-highlight-settings</code></a>: <a href="#Settings">Settings</a></li>
<li><a href="#index-g_t_0040code_007bsource_002dhighlight_002dsettings_007d-19"><code>source-highlight-settings</code></a>: <a href="#SourceHighlight-class">SourceHighlight class</a></li>
<li><a href="#index-g_t_0040code_007bSOURCE_005fHIGHLIGHT_005fDATADIR_007d-61"><code>SOURCE_HIGHLIGHT_DATADIR</code></a>: <a href="#Settings">Settings</a></li>
<li><a href="#index-g_t_0040code_007bSourceHighlight_007d-class-27"><code>SourceHighlight</code> class</a>: <a href="#Completely-Customized-Formatting">Completely Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bSourceHighlight_007d-class-13"><code>SourceHighlight</code> class</a>: <a href="#SourceHighlight-class">SourceHighlight class</a></li>
<li><a href="#index-g_t_0040code_007bSourceHighlighter_007d-class-35"><code>SourceHighlighter</code> class</a>: <a href="#Completely-Customized-Formatting">Completely Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bSourceHighlightUtils_007d-class-70"><code>SourceHighlightUtils</code> class</a>: <a href="#Utility-functions">Utility functions</a></li>
<li><a href="#index-g_t_0040code_007bstart_007d-field-25"><code>start</code> field</a>: <a href="#Completely-Customized-Formatting">Completely Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bstd_003a_003aexception_007d-class-12"><code>std::exception</code> class</a>: <a href="#Main-Classes">Main Classes</a></li>
<li><a href="#index-g_t_0040code_007bStyleConstants_007d-enum-50"><code>StyleConstants</code> enum</a>: <a href="#Style_002dbased-Customized-Formatting">Style-based Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bStyleFileParser_007d-class-44"><code>StyleFileParser</code> class</a>: <a href="#Style_002dbased-Customized-Formatting">Style-based Customized Formatting</a></li>
<li><a href="#index-tail-recursion-80">tail recursion</a>: <a href="#Concept-Index">Concept Index</a></li>
<li><a href="#index-g_t_0040code_007bTextStyleFormatterFactory_007d-class-41"><code>TextStyleFormatterFactory</code> class</a>: <a href="#Style_002dbased-Customized-Formatting">Style-based Customized Formatting</a></li>
<li><a href="#index-g_t_0040code_007bunload_007d-method-76"><code>unload</code> method</a>: <a href="#Global-instances">Global instances</a></li>
</ul>
<div class="shortcontents">
<h2>Short Contents</h2>
<ul>
<li><a href="#toc_Top">GNU Source-highlight Library</a></li>
<li><a href="#toc_Introduction">1 Introduction</a></li>
<li><a href="#toc_Installation">2 Installation</a></li>
<li><a href="#toc_Use-of-GNU-Source_002dhighlight-Library">3 Use of GNU Source-highlight Library</a></li>
<li><a href="#toc_Main-Classes">4 Main Classes</a></li>
<li><a href="#toc_Problems">5 Reporting Bugs</a></li>
<li><a href="#toc_Mailing-Lists">6 Mailing Lists</a></li>
<li><a href="#toc_Concept-Index">Concept Index</a></li>
</ul>
</div>
<div class="footnote">
<hr>
<a name="texinfo-footnotes-in-document"></a><h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> The extension of course depends
on the library being shared or static, e.g., <code>.so</code>, <code>.la</code>,
<code>.a</code>, and on the system</p>
<p class="footnote"><small>[<a name="fn-2" href="#fnd-2">2</a>]</small> <a href="http://pkg-config.freedesktop.org">http://pkg-config.freedesktop.org</a>.</p>
<hr></div>
</body></html>
|