htmlize.el.html 154 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
  2. <!-- Created by htmlize-1.45 in css mode. -->
  3. <html>
  4. <head>
  5. <title>htmlize.el</title>
  6. <style type="text/css">
  7. <!--
  8. body {
  9. color: #839496;
  10. background-color: #002b36;
  11. }
  12. .builtin {
  13. /* font-lock-builtin-face */
  14. color: #859900;
  15. }
  16. .comment {
  17. /* font-lock-comment-face */
  18. color: #586e75;
  19. font-style: italic;
  20. }
  21. .comment-delimiter {
  22. /* font-lock-comment-delimiter-face */
  23. color: #586e75;
  24. font-style: italic;
  25. }
  26. .constant {
  27. /* font-lock-constant-face */
  28. color: #2aa198;
  29. }
  30. .doc {
  31. /* font-lock-doc-face */
  32. color: #586e75;
  33. font-style: italic;
  34. }
  35. .function-name {
  36. /* font-lock-function-name-face */
  37. color: #268bd2;
  38. }
  39. .keyword {
  40. /* font-lock-keyword-face */
  41. color: #859900;
  42. }
  43. .negation-char {
  44. /* font-lock-negation-char-face */
  45. color: #dc322f;
  46. }
  47. .regexp-grouping-backslash {
  48. /* font-lock-regexp-grouping-backslash */
  49. color: #b58900;
  50. }
  51. .regexp-grouping-construct {
  52. /* font-lock-regexp-grouping-construct */
  53. color: #cb4b16;
  54. }
  55. .string {
  56. /* font-lock-string-face */
  57. color: #2aa198;
  58. }
  59. .type {
  60. /* font-lock-type-face */
  61. color: #b58900;
  62. }
  63. .variable-name {
  64. /* font-lock-variable-name-face */
  65. color: #268bd2;
  66. }
  67. .warning {
  68. /* font-lock-warning-face */
  69. color: #dc322f;
  70. font-weight: bold;
  71. }
  72. a {
  73. color: inherit;
  74. background-color: inherit;
  75. font: inherit;
  76. text-decoration: inherit;
  77. }
  78. a:hover {
  79. text-decoration: underline;
  80. }
  81. -->
  82. </style>
  83. </head>
  84. <body>
  85. <pre>
  86. <span class="comment-delimiter">;;; </span><span class="comment">htmlize.el --- Convert buffer text and decorations to HTML.
  87. </span>
  88. <span class="comment-delimiter">;; </span><span class="comment">Copyright (C) 1997-2003,2005,2006,2009,2011,2012 Hrvoje Niksic
  89. </span>
  90. <span class="comment-delimiter">;; </span><span class="comment">Author: Hrvoje Niksic <a href="mailto:hniksic%40xemacs.org">&lt;hniksic@xemacs.org&gt;</a>
  91. </span><span class="comment-delimiter">;; </span><span class="comment">Keywords: hypermedia, extensions
  92. </span><span class="comment-delimiter">;; </span><span class="comment">Version: 1.45
  93. </span>
  94. <span class="comment-delimiter">;; </span><span class="comment">This program is free software; you can redistribute it and/or modify
  95. </span><span class="comment-delimiter">;; </span><span class="comment">it under the terms of the GNU General Public License as published by
  96. </span><span class="comment-delimiter">;; </span><span class="comment">the Free Software Foundation; either version 2, or (at your option)
  97. </span><span class="comment-delimiter">;; </span><span class="comment">any later version.
  98. </span>
  99. <span class="comment-delimiter">;; </span><span class="comment">This program is distributed in the hope that it will be useful,
  100. </span><span class="comment-delimiter">;; </span><span class="comment">but WITHOUT ANY WARRANTY; without even the implied warranty of
  101. </span><span class="comment-delimiter">;; </span><span class="comment">MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  102. </span><span class="comment-delimiter">;; </span><span class="comment">GNU General Public License for more details.
  103. </span>
  104. <span class="comment-delimiter">;; </span><span class="comment">You should have received a copy of the GNU General Public License
  105. </span><span class="comment-delimiter">;; </span><span class="comment">along with this program; see the file COPYING. If not, write to the
  106. </span><span class="comment-delimiter">;; </span><span class="comment">Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  107. </span><span class="comment-delimiter">;; </span><span class="comment">Boston, MA 02111-1307, USA.
  108. </span>
  109. <span class="comment-delimiter">;;; </span><span class="comment">Commentary:
  110. </span>
  111. <span class="comment-delimiter">;; </span><span class="comment">This package converts the buffer text and the associated
  112. </span><span class="comment-delimiter">;; </span><span class="comment">decorations to HTML. Mail to <a href="mailto:hniksic%40xemacs.org">&lt;hniksic@xemacs.org&gt;</a> to discuss
  113. </span><span class="comment-delimiter">;; </span><span class="comment">features and additions. All suggestions are more than welcome.
  114. </span>
  115. <span class="comment-delimiter">;; </span><span class="comment">To use it, just switch to the buffer you want HTML-ized and type
  116. </span><span class="comment-delimiter">;; </span><span class="comment">`M-x htmlize-buffer'. You will be switched to a new buffer that
  117. </span><span class="comment-delimiter">;; </span><span class="comment">contains the resulting HTML code. You can edit and inspect this
  118. </span><span class="comment-delimiter">;; </span><span class="comment">buffer, or you can just save it with C-x C-w. `M-x htmlize-file'
  119. </span><span class="comment-delimiter">;; </span><span class="comment">will find a file, fontify it, and save the HTML version in
  120. </span><span class="comment-delimiter">;; </span><span class="comment">FILE.html, without any additional intervention. `M-x
  121. </span><span class="comment-delimiter">;; </span><span class="comment">htmlize-many-files' allows you to htmlize any number of files in
  122. </span><span class="comment-delimiter">;; </span><span class="comment">the same manner. `M-x htmlize-many-files-dired' does the same for
  123. </span><span class="comment-delimiter">;; </span><span class="comment">files marked in a dired buffer.
  124. </span>
  125. <span class="comment-delimiter">;; </span><span class="comment">htmlize supports three types of HTML output, selected by setting
  126. </span><span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">htmlize-output-type</span></span><span class="comment">': `</span><span class="comment"><span class="constant">css</span></span><span class="comment">', `</span><span class="comment"><span class="constant">inline-css</span></span><span class="comment">', and `</span><span class="comment"><span class="constant">font</span></span><span class="comment">'. In `</span><span class="comment"><span class="constant">css</span></span><span class="comment">'
  127. </span><span class="comment-delimiter">;; </span><span class="comment">mode, htmlize uses cascading style sheets to specify colors; it
  128. </span><span class="comment-delimiter">;; </span><span class="comment">generates classes that correspond to Emacs faces and uses &lt;span
  129. </span><span class="comment-delimiter">;; </span><span class="comment">class=FACE&gt;...&lt;/span&gt; to color parts of text. In this mode, the
  130. </span><span class="comment-delimiter">;; </span><span class="comment">produced HTML is valid under the 4.01 strict DTD, as confirmed by
  131. </span><span class="comment-delimiter">;; </span><span class="comment">the W3C validator. `</span><span class="comment"><span class="constant">inline-css</span></span><span class="comment">' is like `</span><span class="comment"><span class="constant">css</span></span><span class="comment">', except the CSS is
  132. </span><span class="comment-delimiter">;; </span><span class="comment">put directly in the STYLE attribute of the SPAN element, making it
  133. </span><span class="comment-delimiter">;; </span><span class="comment">possible to paste the generated HTML into existing HTML documents.
  134. </span><span class="comment-delimiter">;; </span><span class="comment">In `</span><span class="comment"><span class="constant">font</span></span><span class="comment">' mode, htmlize uses &lt;font color="..."&gt;...&lt;/font&gt; to
  135. </span><span class="comment-delimiter">;; </span><span class="comment">colorize HTML, which is not standard-compliant, but works better in
  136. </span><span class="comment-delimiter">;; </span><span class="comment">older browsers. `</span><span class="comment"><span class="constant">css</span></span><span class="comment">' mode is the default.
  137. </span>
  138. <span class="comment-delimiter">;; </span><span class="comment">You can also use htmlize from your Emacs Lisp code. When called
  139. </span><span class="comment-delimiter">;; </span><span class="comment">non-interactively, `</span><span class="comment"><span class="constant">htmlize-buffer</span></span><span class="comment">' and `</span><span class="comment"><span class="constant">htmlize-region</span></span><span class="comment">' will
  140. </span><span class="comment-delimiter">;; </span><span class="comment">return the resulting HTML buffer, but will not change current
  141. </span><span class="comment-delimiter">;; </span><span class="comment">buffer or move the point. htmlize will do its best to work on
  142. </span><span class="comment-delimiter">;; </span><span class="comment">non-windowing Emacs sessions but the result will be limited to
  143. </span><span class="comment-delimiter">;; </span><span class="comment">colors supported by the terminal.
  144. </span>
  145. <span class="comment-delimiter">;; </span><span class="comment">htmlize aims for compatibility with Emacsen version 21 and later.
  146. </span><span class="comment-delimiter">;; </span><span class="comment">Please let me know if it doesn't work on the version of XEmacs or
  147. </span><span class="comment-delimiter">;; </span><span class="comment">GNU Emacs that you are using. The package relies on the presence
  148. </span><span class="comment-delimiter">;; </span><span class="comment">of CL extensions, especially for cross-emacs compatibility; please
  149. </span><span class="comment-delimiter">;; </span><span class="comment">don't try to remove that dependency. I see no practical problems
  150. </span><span class="comment-delimiter">;; </span><span class="comment">with using the full power of the CL extensions, except that one
  151. </span><span class="comment-delimiter">;; </span><span class="comment">might learn to like them too much.
  152. </span>
  153. <span class="comment-delimiter">;; </span><span class="comment">The latest version is available as a git repository at:
  154. </span><span class="comment-delimiter">;;</span><span class="comment">
  155. </span><span class="comment-delimiter">;; </span><span class="comment"><a href="http://fly.srk.fer.hr/~hniksic/emacs/htmlize.git">&lt;http://fly.srk.fer.hr/~hniksic/emacs/htmlize.git&gt;</a>
  156. </span><span class="comment-delimiter">;;</span><span class="comment">
  157. </span><span class="comment-delimiter">;; </span><span class="comment">The snapshot of the latest release can be obtained at:
  158. </span><span class="comment-delimiter">;;</span><span class="comment">
  159. </span><span class="comment-delimiter">;; </span><span class="comment"><a href="http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el.cgi">&lt;http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el.cgi&gt;</a>
  160. </span><span class="comment-delimiter">;;</span><span class="comment">
  161. </span><span class="comment-delimiter">;; </span><span class="comment">You can find a sample of htmlize's output (possibly generated with
  162. </span><span class="comment-delimiter">;; </span><span class="comment">an older version) at:
  163. </span><span class="comment-delimiter">;;</span><span class="comment">
  164. </span><span class="comment-delimiter">;; </span><span class="comment"><a href="http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el.html">&lt;http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el.html&gt;</a>
  165. </span>
  166. <span class="comment-delimiter">;; </span><span class="comment">Thanks go to the many people who have sent reports and contributed
  167. </span><span class="comment-delimiter">;; </span><span class="comment">comments, suggestions, and fixes. They include Ron Gut, Bob
  168. </span><span class="comment-delimiter">;; </span><span class="comment">Weiner, Toni Drabik, Peter Breton, Ville Skytta, Thomas Vogels,
  169. </span><span class="comment-delimiter">;; </span><span class="comment">Juri Linkov, Maciek Pasternacki, and many others.
  170. </span>
  171. <span class="comment-delimiter">;; </span><span class="comment">User quotes: "You sir, are a sick, sick, _sick_ person. :)"
  172. </span><span class="comment-delimiter">;; </span><span class="comment">-- Bill Perry, author of Emacs/W3
  173. <hr /></span>
  174. <span class="comment-delimiter">;;; </span><span class="comment">Code:
  175. </span>
  176. (<span class="keyword">require</span> '<span class="constant">cl</span>)
  177. (<span class="keyword">eval-when-compile</span>
  178. (<span class="keyword">defvar</span> <span class="variable-name">unresolved</span>)
  179. (<span class="keyword">if</span> (string-match <span class="string">"XEmacs"</span> emacs-version)
  180. (byte-compiler-options
  181. (warnings (- unresolved))))
  182. (<span class="keyword">defvar</span> <span class="variable-name">font-lock-auto-fontify</span>)
  183. (<span class="keyword">defvar</span> <span class="variable-name">font-lock-support-mode</span>)
  184. (<span class="keyword">defvar</span> <span class="variable-name">global-font-lock-mode</span>))
  185. (<span class="keyword">defconst</span> <span class="variable-name">htmlize-version</span> <span class="string">"1.45"</span>)
  186. (<span class="keyword">defgroup</span> <span class="type">htmlize</span> nil
  187. <span class="doc">"Convert buffer text and faces to HTML."</span>
  188. <span class="builtin">:group</span> 'hypermedia)
  189. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-head-tags</span> <span class="string">""</span>
  190. <span class="doc">"Additional tags to insert within HEAD of the generated document."</span>
  191. <span class="builtin">:type</span> 'string
  192. <span class="builtin">:group</span> 'htmlize)
  193. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-output-type</span> 'css
  194. <span class="doc">"Output type of generated HTML, one of `</span><span class="doc"><span class="constant">css</span></span><span class="doc">', `</span><span class="doc"><span class="constant">inline-css</span></span><span class="doc">', or `</span><span class="doc"><span class="constant">font</span></span><span class="doc">'.
  195. When set to `</span><span class="doc"><span class="constant">css</span></span><span class="doc">' (the default), htmlize will generate a style sheet
  196. with description of faces, and use it in the HTML document, specifying
  197. the faces in the actual text with &lt;span class=\"FACE\"&gt;.
  198. When set to `</span><span class="doc"><span class="constant">inline-css</span></span><span class="doc">', the style will be generated as above, but
  199. placed directly in the STYLE attribute of the span ELEMENT: &lt;span
  200. style=\"STYLE\"&gt;. This makes it easier to paste the resulting HTML to
  201. other documents.
  202. When set to `</span><span class="doc"><span class="constant">font</span></span><span class="doc">', the properties will be set using layout tags
  203. &lt;font&gt;, &lt;b&gt;, &lt;i&gt;, &lt;u&gt;, and &lt;strike&gt;.
  204. `</span><span class="doc"><span class="constant">css</span></span><span class="doc">' output is normally preferred, but `</span><span class="doc"><span class="constant">font</span></span><span class="doc">' is still useful for
  205. supporting old, pre-CSS browsers, and both `</span><span class="doc"><span class="constant">inline-css</span></span><span class="doc">' and `</span><span class="doc"><span class="constant">font</span></span><span class="doc">' for
  206. easier embedding of colorized text in foreign HTML documents (no style
  207. sheet to carry around)."</span>
  208. <span class="builtin">:type</span> '(choice (const css) (const inline-css) (const font))
  209. <span class="builtin">:group</span> 'htmlize)
  210. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-use-images</span> t
  211. <span class="doc">"Whether htmlize generates `</span><span class="doc"><span class="constant">img</span></span><span class="doc">' for images attached to buffer contents."</span>
  212. <span class="builtin">:type</span> 'boolean
  213. <span class="builtin">:group</span> 'htmlize)
  214. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-force-inline-images</span> nil
  215. <span class="doc">"Non-nil means generate all images inline using data URLs.
  216. Normally htmlize converts image descriptors with :file properties to
  217. relative URIs, and those with :data properties to data URIs. With this
  218. flag set, the images specified as a file name are loaded into memory and
  219. embedded in the HTML as data URIs."</span>
  220. <span class="builtin">:type</span> 'boolean
  221. <span class="builtin">:group</span> 'htmlize)
  222. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-max-alt-text</span> 100
  223. <span class="doc">"Maximum size of text to use as ALT text in images.
  224. Normally when htmlize encounters text covered by the `</span><span class="doc"><span class="constant">display</span></span><span class="doc">' property
  225. that specifies an image, it generates an `</span><span class="doc"><span class="constant">alt</span></span><span class="doc">' attribute containing the
  226. original text. If the text is larger than `</span><span class="doc"><span class="constant">htmlize-max-alt-text</span></span><span class="doc">' characters,
  227. this will not be done."</span>)
  228. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-transform-image</span> 'htmlize-default-transform-image
  229. <span class="doc">"Function called to modify the image descriptor.
  230. The function is called with the image descriptor found in the buffer and
  231. the text the image is supposed to replace. It should return a (possibly
  232. different) image descriptor property list or a replacement string to use
  233. instead of of the original buffer text.
  234. Returning nil is the same as returning the original text."</span>
  235. <span class="builtin">:type</span> 'boolean
  236. <span class="builtin">:group</span> 'htmlize)
  237. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-generate-hyperlinks</span> t
  238. <span class="doc">"Non-nil means auto-generate the links from URLs and mail addresses in buffer.
  239. This is on by default; set it to nil if you don't want htmlize to
  240. autogenerate such links. Note that this option only turns off automatic
  241. search for contents that looks like URLs and converting them to links.
  242. It has no effect on whether htmlize respects the `</span><span class="doc"><span class="constant">htmlize-link</span></span><span class="doc">' property."</span>
  243. <span class="builtin">:type</span> 'boolean
  244. <span class="builtin">:group</span> 'htmlize)
  245. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-hyperlink-style</span> <span class="string">"
  246. a {
  247. color: inherit;
  248. background-color: inherit;
  249. font: inherit;
  250. text-decoration: inherit;
  251. }
  252. a:hover {
  253. text-decoration: underline;
  254. }
  255. "</span>
  256. <span class="doc">"The CSS style used for hyperlinks when in CSS mode."</span>
  257. <span class="builtin">:type</span> 'string
  258. <span class="builtin">:group</span> 'htmlize)
  259. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-replace-form-feeds</span> t
  260. <span class="doc">"Non-nil means replace form feeds in source code with HTML separators.
  261. Form feeds are the ^L characters at line beginnings that are sometimes
  262. used to separate sections of source code. If this variable is set to
  263. `t', form feed characters are replaced with the &lt;hr&gt; separator. If this
  264. is a string, it specifies the replacement to use. Note that &lt;pre&gt; is
  265. temporarily closed before the separator is inserted, so the default
  266. replacement is effectively \"&lt;/pre&gt;&lt;hr /&gt;&lt;pre&gt;\". If you specify
  267. another replacement, don't forget to close and reopen the &lt;pre&gt; if you
  268. want the output to remain valid HTML.
  269. If you need more elaborate processing, set this to nil and use
  270. htmlize-after-hook."</span>
  271. <span class="builtin">:type</span> 'boolean
  272. <span class="builtin">:group</span> 'htmlize)
  273. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-html-charset</span> nil
  274. <span class="doc">"The charset declared by the resulting HTML documents.
  275. When non-nil, causes htmlize to insert the following in the HEAD section
  276. of the generated HTML:
  277. &lt;meta http-equiv=\"Content-Type\" content=\"text/html; charset=CHARSET\"&gt;
  278. where CHARSET is the value you've set for htmlize-html-charset. Valid
  279. charsets are defined by MIME and include strings like \"iso-8859-1\",
  280. \"iso-8859-15\", \"utf-8\", etc.
  281. If you are using non-Latin-1 charsets, you might need to set this for
  282. your documents to render correctly. Also, the W3C validator requires
  283. submitted HTML documents to declare a charset. So if you care about
  284. validation, you can use this to prevent the validator from bitching.
  285. Needless to say, if you set this, you should actually make sure that
  286. the buffer is in the encoding you're claiming it is in. (This is
  287. normally achieved by using the correct file coding system for the
  288. buffer.) If you don't understand what that means, you should probably
  289. leave this option in its default setting."</span>
  290. <span class="builtin">:type</span> '(choice (const <span class="builtin">:tag</span> <span class="string">"Unset"</span> nil)
  291. string)
  292. <span class="builtin">:group</span> 'htmlize)
  293. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-convert-nonascii-to-entities</span> t
  294. <span class="doc">"Whether non-ASCII characters should be converted to HTML entities.
  295. When this is non-nil, characters with codes in the 128-255 range will be
  296. considered Latin 1 and rewritten as \"&amp;#CODE;\". Characters with codes
  297. above 255 will be converted to \"&amp;#UCS;\", where UCS denotes the Unicode
  298. code point of the character. If the code point cannot be determined,
  299. the character will be copied unchanged, as would be the case if the
  300. option were nil.
  301. When the option is nil, the non-ASCII characters are copied to HTML
  302. without modification. In that case, the web server and/or the browser
  303. must be set to understand the encoding that was used when saving the
  304. buffer. (You might also want to specify it by setting
  305. `</span><span class="doc"><span class="constant">htmlize-html-charset</span></span><span class="doc">'.)
  306. Note that in an HTML entity \"&amp;#CODE;\", CODE is always a UCS code point,
  307. which has nothing to do with the charset the page is in. For example,
  308. \"&amp;#169;\" *always* refers to the copyright symbol, regardless of charset
  309. specified by the META tag or the charset sent by the HTTP server. In
  310. other words, \"&amp;#169;\" is exactly equivalent to \"&amp;copy;\".
  311. For most people htmlize will work fine with this option left at the
  312. default setting; don't change it unless you know what you're doing."</span>
  313. <span class="builtin">:type</span> 'sexp
  314. <span class="builtin">:group</span> 'htmlize)
  315. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-ignore-face-size</span> 'absolute
  316. <span class="doc">"Whether face size should be ignored when generating HTML.
  317. If this is nil, face sizes are used. If set to t, sizes are ignored
  318. If set to `</span><span class="doc"><span class="constant">absolute</span></span><span class="doc">', only absolute size specifications are ignored.
  319. Please note that font sizes only work with CSS-based output types."</span>
  320. <span class="builtin">:type</span> '(choice (const <span class="builtin">:tag</span> <span class="string">"Don't ignore"</span> nil)
  321. (const <span class="builtin">:tag</span> <span class="string">"Ignore all"</span> t)
  322. (const <span class="builtin">:tag</span> <span class="string">"Ignore absolute"</span> absolute))
  323. <span class="builtin">:group</span> 'htmlize)
  324. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-css-name-prefix</span> <span class="string">""</span>
  325. <span class="doc">"The prefix used for CSS names.
  326. The CSS names that htmlize generates from face names are often too
  327. generic for CSS files; for example, `</span><span class="doc"><span class="constant">font-lock-type-face</span></span><span class="doc">' is transformed
  328. to `</span><span class="doc"><span class="constant">type</span></span><span class="doc">'. Use this variable to add a prefix to the generated names.
  329. The string \"htmlize-\" is an example of a reasonable prefix."</span>
  330. <span class="builtin">:type</span> 'string
  331. <span class="builtin">:group</span> 'htmlize)
  332. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-use-rgb-txt</span> t
  333. <span class="doc">"Whether `</span><span class="doc"><span class="constant">rgb.txt</span></span><span class="doc">' should be used to convert color names to RGB.
  334. This conversion means determining, for instance, that the color
  335. \"IndianRed\" corresponds to the (205, 92, 92) RGB triple. `</span><span class="doc"><span class="constant">rgb.txt</span></span><span class="doc">'
  336. is the X color database that maps hundreds of color names to such RGB
  337. triples. When this variable is non-nil, `</span><span class="doc"><span class="constant">htmlize</span></span><span class="doc">' uses `</span><span class="doc"><span class="constant">rgb.txt</span></span><span class="doc">' to
  338. look up color names.
  339. If this variable is nil, htmlize queries Emacs for RGB components of
  340. colors using `</span><span class="doc"><span class="constant">color-instance-rgb-components</span></span><span class="doc">' and `</span><span class="doc"><span class="constant">color-values</span></span><span class="doc">'.
  341. This can yield incorrect results on non-true-color displays.
  342. If the `</span><span class="doc"><span class="constant">rgb.txt</span></span><span class="doc">' file is not found (which will be the case if you're
  343. running Emacs on non-X11 systems), this option is ignored."</span>
  344. <span class="builtin">:type</span> 'boolean
  345. <span class="builtin">:group</span> 'htmlize)
  346. (<span class="keyword">defcustom</span> <span class="variable-name">htmlize-html-major-mode</span> nil
  347. <span class="doc">"The mode the newly created HTML buffer will be put in.
  348. Set this to nil if you prefer the default (fundamental) mode."</span>
  349. <span class="builtin">:type</span> '(radio (const <span class="builtin">:tag</span> <span class="string">"No mode (fundamental)"</span> nil)
  350. (function-item html-mode)
  351. (function <span class="builtin">:tag</span> <span class="string">"User-defined major mode"</span>))
  352. <span class="builtin">:group</span> 'htmlize)
  353. (<span class="keyword">defvar</span> <span class="variable-name">htmlize-before-hook</span> nil
  354. <span class="doc">"Hook run before htmlizing a buffer.
  355. The hook functions are run in the source buffer (not the resulting HTML
  356. buffer)."</span>)
  357. (<span class="keyword">defvar</span> <span class="variable-name">htmlize-after-hook</span> nil
  358. <span class="doc">"Hook run after htmlizing a buffer.
  359. Unlike `</span><span class="doc"><span class="constant">htmlize-before-hook</span></span><span class="doc">', these functions are run in the generated
  360. HTML buffer. You may use them to modify the outlook of the final HTML
  361. output."</span>)
  362. (<span class="keyword">defvar</span> <span class="variable-name">htmlize-file-hook</span> nil
  363. <span class="doc">"Hook run by `</span><span class="doc"><span class="constant">htmlize-file</span></span><span class="doc">' after htmlizing a file, but before saving it."</span>)
  364. (<span class="keyword">defvar</span> <span class="variable-name">htmlize-buffer-places</span>)
  365. <hr />
  366. <span class="comment-delimiter">;;; </span><span class="comment">Some cross-Emacs compatibility.
  367. </span>
  368. <span class="comment-delimiter">;; </span><span class="comment">I try to conditionalize on features rather than Emacs version, but
  369. </span><span class="comment-delimiter">;; </span><span class="comment">in some cases checking against the version *is* necessary.
  370. </span>(<span class="keyword">defconst</span> <span class="variable-name">htmlize-running-xemacs</span> (string-match <span class="string">"XEmacs"</span> emacs-version))
  371. <span class="comment-delimiter">;; </span><span class="comment">We need a function that efficiently finds the next change of a
  372. </span><span class="comment-delimiter">;; </span><span class="comment">property regardless of whether the change occurred because of a
  373. </span><span class="comment-delimiter">;; </span><span class="comment">text property or an extent/overlay.
  374. </span>(<span class="keyword">cond</span>
  375. (htmlize-running-xemacs
  376. (<span class="keyword">defun</span> <span class="function-name">htmlize-next-change</span> (pos prop <span class="type">&amp;optional</span> limit)
  377. (<span class="keyword">if</span> prop
  378. (next-single-char-property-change pos prop nil (or limit (point-max)))
  379. (next-property-change pos nil (or limit (point-max)))))
  380. (<span class="keyword">defun</span> <span class="function-name">htmlize-next-face-change</span> (pos <span class="type">&amp;optional</span> limit)
  381. (htmlize-next-change pos 'face limit)))
  382. ((fboundp 'next-single-char-property-change)
  383. <span class="comment-delimiter">;; </span><span class="comment">GNU Emacs 21+
  384. </span> (<span class="keyword">defun</span> <span class="function-name">htmlize-next-change</span> (pos prop <span class="type">&amp;optional</span> limit)
  385. (<span class="keyword">if</span> prop
  386. (next-single-char-property-change pos prop nil limit)
  387. (next-char-property-change pos limit)))
  388. (<span class="keyword">defun</span> <span class="function-name">htmlize-overlay-faces-at</span> (pos)
  389. (delq nil (mapcar (<span class="keyword">lambda</span> (o) (overlay-get o 'face)) (overlays-at pos))))
  390. (<span class="keyword">defun</span> <span class="function-name">htmlize-next-face-change</span> (pos <span class="type">&amp;optional</span> limit)
  391. <span class="comment-delimiter">;; </span><span class="comment">(htmlize-next-change pos 'face limit) would skip over entire
  392. </span> <span class="comment-delimiter">;; </span><span class="comment">overlays that specify the `</span><span class="comment"><span class="constant">face</span></span><span class="comment">' property, even when they
  393. </span> <span class="comment-delimiter">;; </span><span class="comment">contain smaller text properties that also specify `</span><span class="comment"><span class="constant">face</span></span><span class="comment">'.
  394. </span> <span class="comment-delimiter">;; </span><span class="comment">Emacs display engine merges those faces, and so must we.
  395. </span> (or limit
  396. (setq limit (point-max)))
  397. (<span class="keyword">let</span> ((next-prop (next-single-property-change pos 'face nil limit))
  398. (overlay-faces (htmlize-overlay-faces-at pos)))
  399. (<span class="keyword">while</span> (<span class="keyword">progn</span>
  400. (setq pos (next-overlay-change pos))
  401. (and (&lt; pos next-prop)
  402. (equal overlay-faces (htmlize-overlay-faces-at pos)))))
  403. (setq pos (min pos next-prop))
  404. <span class="comment-delimiter">;; </span><span class="comment">Additionally, we include the entire region that specifies the
  405. </span> <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">display</span></span><span class="comment">' property.
  406. </span> (<span class="keyword">when</span> (get-char-property pos 'display)
  407. (setq pos (next-single-char-property-change pos 'display nil limit)))
  408. pos)))
  409. (t
  410. (<span class="warning">error</span> <span class="string">"htmlize requires next-single-property-change or \
  411. next-single-char-property-change"</span>)))
  412. (<span class="keyword">defmacro</span> <span class="function-name">htmlize-lexlet</span> (<span class="type">&amp;rest</span> letforms)
  413. (<span class="keyword">declare</span> (indent 1) (debug let))
  414. (<span class="keyword">if</span> (and (boundp 'lexical-binding)
  415. lexical-binding)
  416. `(<span class="keyword">let</span> ,@letforms)
  417. <span class="comment-delimiter">;; </span><span class="comment">cl extensions have a macro implementing lexical let
  418. </span> `(<span class="keyword">lexical-let</span> ,@letforms)))
  419. <span class="comment-delimiter">;; </span><span class="comment">Simple overlay emulation for XEmacs
  420. </span>
  421. (<span class="keyword">cond</span>
  422. (htmlize-running-xemacs
  423. (<span class="keyword">defalias</span> '<span class="function-name">htmlize-make-overlay</span> 'make-extent)
  424. (<span class="keyword">defalias</span> '<span class="function-name">htmlize-overlay-put</span> 'set-extent-property)
  425. (<span class="keyword">defalias</span> '<span class="function-name">htmlize-overlay-get</span> 'extent-property)
  426. (<span class="keyword">defun</span> <span class="function-name">htmlize-overlays-in</span> (beg end) (extent-list nil beg end))
  427. (<span class="keyword">defalias</span> '<span class="function-name">htmlize-delete-overlay</span> 'detach-extent))
  428. (t
  429. (<span class="keyword">defalias</span> '<span class="function-name">htmlize-make-overlay</span> 'make-overlay)
  430. (<span class="keyword">defalias</span> '<span class="function-name">htmlize-overlay-put</span> 'overlay-put)
  431. (<span class="keyword">defalias</span> '<span class="function-name">htmlize-overlay-get</span> 'overlay-get)
  432. (<span class="keyword">defalias</span> '<span class="function-name">htmlize-overlays-in</span> 'overlays-in)
  433. (<span class="keyword">defalias</span> '<span class="function-name">htmlize-delete-overlay</span> 'delete-overlay)))
  434. <hr />
  435. <span class="comment-delimiter">;;; </span><span class="comment">Transformation of buffer text: HTML escapes, untabification, etc.
  436. </span>
  437. (<span class="keyword">defvar</span> <span class="variable-name">htmlize-basic-character-table</span>
  438. <span class="comment-delimiter">;; </span><span class="comment">Map characters in the 0-127 range to either one-character strings
  439. </span> <span class="comment-delimiter">;; </span><span class="comment">or to numeric entities.
  440. </span> (<span class="keyword">let</span> ((table (make-vector 128 ?\0)))
  441. <span class="comment-delimiter">;; </span><span class="comment">Map characters in the 32-126 range to themselves, others to
  442. </span> <span class="comment-delimiter">;; </span><span class="comment">&amp;#CODE entities;
  443. </span> (<span class="keyword">dotimes</span> (i 128)
  444. (setf (aref table i) (<span class="keyword">if</span> (and (&gt;= i 32) (&lt;= i 126))
  445. (char-to-string i)
  446. (format <span class="string">"&amp;#%d;"</span> i))))
  447. <span class="comment-delimiter">;; </span><span class="comment">Set exceptions manually.
  448. </span> (setf
  449. <span class="comment-delimiter">;; </span><span class="comment">Don't escape newline, carriage return, and TAB.
  450. </span> (aref table ?\n) <span class="string">"\n"</span>
  451. (aref table ?\r) <span class="string">"\r"</span>
  452. (aref table ?\t) <span class="string">"\t"</span>
  453. <span class="comment-delimiter">;; </span><span class="comment">Escape &amp;, &lt;, and &gt;.
  454. </span> (aref table ?&amp;) <span class="string">"&amp;amp;"</span>
  455. (aref table ?&lt;) <span class="string">"&amp;lt;"</span>
  456. (aref table ?&gt;) <span class="string">"&amp;gt;"</span>
  457. <span class="comment-delimiter">;; </span><span class="comment">Not escaping '"' buys us a measurable speedup. It's only
  458. </span> <span class="comment-delimiter">;; </span><span class="comment">necessary to quote it for strings used in attribute values,
  459. </span> <span class="comment-delimiter">;; </span><span class="comment">which htmlize doesn't typically do.
  460. </span> <span class="comment-delimiter">;</span><span class="comment">(aref table ?\") "&amp;quot;"
  461. </span> )
  462. table))
  463. <span class="comment-delimiter">;; </span><span class="comment">A cache of HTML representation of non-ASCII characters. Depending
  464. </span><span class="comment-delimiter">;; </span><span class="comment">on the setting of `</span><span class="comment"><span class="constant">htmlize-convert-nonascii-to-entities</span></span><span class="comment">', this maps
  465. </span><span class="comment-delimiter">;; </span><span class="comment">non-ASCII characters to either "&amp;#&lt;code&gt;;" or "&lt;char&gt;" (mapconcat's
  466. </span><span class="comment-delimiter">;; </span><span class="comment">mapper must always return strings). It's only filled as characters
  467. </span><span class="comment-delimiter">;; </span><span class="comment">are encountered, so that in a buffer with e.g. French text, it will
  468. </span><span class="comment-delimiter">;; </span><span class="comment">only ever contain French accented characters as keys. It's cleared
  469. </span><span class="comment-delimiter">;; </span><span class="comment">on each entry to htmlize-buffer-1 to allow modifications of
  470. </span><span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">htmlize-convert-nonascii-to-entities</span></span><span class="comment">' to take effect.
  471. </span>(<span class="keyword">defvar</span> <span class="variable-name">htmlize-extended-character-cache</span> (make-hash-table <span class="builtin">:test</span> 'eq))
  472. (<span class="keyword">defun</span> <span class="function-name">htmlize-protect-string</span> (string)
  473. <span class="doc">"HTML-protect string, escaping HTML metacharacters and I18N chars."</span>
  474. <span class="comment-delimiter">;; </span><span class="comment">Only protecting strings that actually contain unsafe or non-ASCII
  475. </span> <span class="comment-delimiter">;; </span><span class="comment">chars removes a lot of unnecessary funcalls and consing.
  476. </span> (<span class="keyword">if</span> (not (string-match <span class="string">"[</span><span class="string"><span class="negation-char">^</span></span><span class="string">\r\n\t -%'-;=?-~]"</span> string))
  477. string
  478. (mapconcat (<span class="keyword">lambda</span> (char)
  479. (<span class="keyword">cond</span>
  480. ((&lt; char 128)
  481. <span class="comment-delimiter">;; </span><span class="comment">ASCII: use htmlize-basic-character-table.
  482. </span> (aref htmlize-basic-character-table char))
  483. ((gethash char htmlize-extended-character-cache)
  484. <span class="comment-delimiter">;; </span><span class="comment">We've already seen this char; return the cached
  485. </span> <span class="comment-delimiter">;; </span><span class="comment">string.
  486. </span> )
  487. ((not htmlize-convert-nonascii-to-entities)
  488. <span class="comment-delimiter">;; </span><span class="comment">If conversion to entities is not desired, always
  489. </span> <span class="comment-delimiter">;; </span><span class="comment">copy the char literally.
  490. </span> (setf (gethash char htmlize-extended-character-cache)
  491. (char-to-string char)))
  492. ((&lt; char 256)
  493. <span class="comment-delimiter">;; </span><span class="comment">Latin 1: no need to call encode-char.
  494. </span> (setf (gethash char htmlize-extended-character-cache)
  495. (format <span class="string">"&amp;#%d;"</span> char)))
  496. ((encode-char char 'ucs)
  497. <span class="comment-delimiter">;; </span><span class="comment">Must check if encode-char works for CHAR;
  498. </span> <span class="comment-delimiter">;; </span><span class="comment">it fails for Arabic and possibly elsewhere.
  499. </span> (setf (gethash char htmlize-extended-character-cache)
  500. (format <span class="string">"&amp;#%d;"</span> (encode-char char 'ucs))))
  501. (t
  502. <span class="comment-delimiter">;; </span><span class="comment">encode-char doesn't work for this char. Copy it
  503. </span> <span class="comment-delimiter">;; </span><span class="comment">unchanged and hope for the best.
  504. </span> (setf (gethash char htmlize-extended-character-cache)
  505. (char-to-string char)))))
  506. string <span class="string">""</span>)))
  507. (<span class="keyword">defun</span> <span class="function-name">htmlize-attr-escape</span> (string)
  508. <span class="comment-delimiter">;; </span><span class="comment">Like htmlize-protect-string, but also escapes double-quoted
  509. </span> <span class="comment-delimiter">;; </span><span class="comment">strings to make it usable in attribute values.
  510. </span> (setq string (htmlize-protect-string string))
  511. (<span class="keyword">if</span> (not (string-match <span class="string">"\""</span> string))
  512. string
  513. (mapconcat (<span class="keyword">lambda</span> (char)
  514. (<span class="keyword">if</span> (eql char ?\")
  515. <span class="string">"&amp;quot;"</span>
  516. (char-to-string char)))
  517. string <span class="string">""</span>)))
  518. (<span class="keyword">defsubst</span> <span class="function-name">htmlize-concat</span> (list)
  519. (<span class="keyword">if</span> (and (consp list) (null (cdr list)))
  520. <span class="comment-delimiter">;; </span><span class="comment">Don't create a new string in the common case where the list only
  521. </span> <span class="comment-delimiter">;; </span><span class="comment">consists of one element.
  522. </span> (car list)
  523. (apply #'concat list)))
  524. (<span class="keyword">defun</span> <span class="function-name">htmlize-format-link</span> (linkprops text)
  525. (<span class="keyword">let</span> ((uri (<span class="keyword">if</span> (stringp linkprops)
  526. linkprops
  527. (plist-get linkprops <span class="builtin">:uri</span>)))
  528. (escaped-text (htmlize-protect-string text)))
  529. (<span class="keyword">if</span> uri
  530. (format <span class="string">"&lt;a href=\"%s\"&gt;%s&lt;/a&gt;"</span> (htmlize-attr-escape uri) escaped-text)
  531. escaped-text)))
  532. (<span class="keyword">defun</span> <span class="function-name">htmlize-escape-or-link</span> (string)
  533. <span class="comment-delimiter">;; </span><span class="comment">Escape STRING and/or add hyperlinks. STRING comes from a
  534. </span> <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">display</span></span><span class="comment">' property.
  535. </span> (<span class="keyword">let</span> ((pos 0) (end (length string)) outlist)
  536. (<span class="keyword">while</span> (&lt; pos end)
  537. (<span class="keyword">let*</span> ((link (get-char-property pos 'htmlize-link string))
  538. (next-link-change (next-single-property-change
  539. pos 'htmlize-link string end))
  540. (chunk (substring string pos next-link-change)))
  541. (push
  542. (<span class="keyword">cond</span> (link
  543. (htmlize-format-link link chunk))
  544. ((get-char-property 0 'htmlize-literal chunk)
  545. chunk)
  546. (t
  547. (htmlize-protect-string chunk)))
  548. outlist)
  549. (setq pos next-link-change)))
  550. (htmlize-concat (nreverse outlist))))
  551. (<span class="keyword">defun</span> <span class="function-name">htmlize-display-prop-to-html</span> (display text)
  552. (<span class="keyword">let</span> (desc)
  553. (<span class="keyword">cond</span> ((stringp display)
  554. <span class="comment-delimiter">;; </span><span class="comment">Emacs ignores recursive display properties.
  555. </span> (htmlize-escape-or-link display))
  556. ((not (eq (car-safe display) 'image))
  557. (htmlize-protect-string text))
  558. ((null (setq desc (funcall htmlize-transform-image
  559. (cdr display) text)))
  560. (htmlize-escape-or-link text))
  561. ((stringp desc)
  562. (htmlize-escape-or-link desc))
  563. (t
  564. (htmlize-generate-image desc text)))))
  565. (<span class="keyword">defun</span> <span class="function-name">htmlize-string-to-html</span> (string)
  566. <span class="comment-delimiter">;; </span><span class="comment">Convert the string to HTML, including images attached as
  567. </span> <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">display</span></span><span class="comment">' property and links as `</span><span class="comment"><span class="constant">htmlize-link</span></span><span class="comment">' property. In a
  568. </span> <span class="comment-delimiter">;; </span><span class="comment">string without images or links, this is equivalent to
  569. </span> <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">htmlize-protect-string</span></span><span class="comment">'.
  570. </span> (<span class="keyword">let</span> ((pos 0) (end (length string)) outlist)
  571. (<span class="keyword">while</span> (&lt; pos end)
  572. (<span class="keyword">let*</span> ((display (get-char-property pos 'display string))
  573. (next-display-change (next-single-property-change
  574. pos 'display string end))
  575. (chunk (substring string pos next-display-change)))
  576. (push
  577. (<span class="keyword">if</span> display
  578. (htmlize-display-prop-to-html display chunk)
  579. (htmlize-escape-or-link chunk))
  580. outlist)
  581. (setq pos next-display-change)))
  582. (htmlize-concat (nreverse outlist))))
  583. (<span class="keyword">defun</span> <span class="function-name">htmlize-default-transform-image</span> (imgprops _text)
  584. <span class="doc">"Default transformation of image descriptor to something usable in HTML.
  585. If `</span><span class="doc"><span class="constant">htmlize-use-images</span></span><span class="doc">' is nil, the function always returns nil, meaning
  586. use original text. Otherwise, it tries to find the image for images that
  587. specify a file name. If `</span><span class="doc"><span class="constant">htmlize-force-inline-images</span></span><span class="doc">' is non-nil, it also
  588. converts the :file attribute to :data and returns the modified property
  589. list."</span>
  590. (<span class="keyword">when</span> htmlize-use-images
  591. (<span class="keyword">when</span> (plist-get imgprops <span class="builtin">:file</span>)
  592. (<span class="keyword">let</span> ((location (plist-get (cdr (find-image (list imgprops))) <span class="builtin">:file</span>)))
  593. (<span class="keyword">when</span> location
  594. (setq imgprops (plist-put (copy-list imgprops) <span class="builtin">:file</span> location)))))
  595. (<span class="keyword">if</span> htmlize-force-inline-images
  596. (<span class="keyword">let</span> ((location (plist-get imgprops <span class="builtin">:file</span>))
  597. data)
  598. (<span class="keyword">when</span> location
  599. (<span class="keyword">with-temp-buffer</span>
  600. (<span class="keyword">condition-case</span> nil
  601. (<span class="keyword">progn</span>
  602. (insert-file-contents-literally location)
  603. (setq data (buffer-string)))
  604. (<span class="warning">error</span> nil))))
  605. <span class="comment-delimiter">;; </span><span class="comment">if successful, return the new plist, otherwise return
  606. </span> <span class="comment-delimiter">;; </span><span class="comment">nil, which will use the original text
  607. </span> (and data
  608. (plist-put (plist-put imgprops <span class="builtin">:file</span> nil)
  609. <span class="builtin">:data</span> data)))
  610. imgprops)))
  611. (<span class="keyword">defun</span> <span class="function-name">htmlize-alt-text</span> (_imgprops origtext)
  612. (and (/= (length origtext) 0)
  613. (&lt;= (length origtext) htmlize-max-alt-text)
  614. (not (string-match <span class="string">"[\0-\x1f]"</span> origtext))
  615. origtext))
  616. (<span class="keyword">defun</span> <span class="function-name">htmlize-generate-image</span> (imgprops origtext)
  617. (<span class="keyword">let*</span> ((alt-text (htmlize-alt-text imgprops origtext))
  618. (alt-attr (<span class="keyword">if</span> alt-text
  619. (format <span class="string">" alt=\"%s\""</span> (htmlize-attr-escape alt-text))
  620. <span class="string">""</span>)))
  621. (<span class="keyword">cond</span> ((plist-get imgprops <span class="builtin">:file</span>)
  622. <span class="comment-delimiter">;; </span><span class="comment">Try to find the image in image-load-path
  623. </span> (<span class="keyword">let*</span> ((found-props (cdr (find-image (list imgprops))))
  624. (file (or (plist-get found-props <span class="builtin">:file</span>)
  625. (plist-get imgprops <span class="builtin">:file</span>))))
  626. (format <span class="string">"&lt;img src=\"%s\"%s /&gt;"</span>
  627. (htmlize-attr-escape (file-relative-name file))
  628. alt-attr)))
  629. ((plist-get imgprops <span class="builtin">:data</span>)
  630. (format <span class="string">"&lt;img src=\"data:image/%s;base64,%s\"%s /&gt;"</span>
  631. (or (plist-get imgprops <span class="builtin">:type</span>) <span class="string">""</span>)
  632. (base64-encode-string (plist-get imgprops <span class="builtin">:data</span>))
  633. alt-attr)))))
  634. (<span class="keyword">defconst</span> <span class="variable-name">htmlize-ellipsis</span> <span class="string">"..."</span>)
  635. (put-text-property 0 (length htmlize-ellipsis) 'htmlize-ellipsis t htmlize-ellipsis)
  636. (<span class="keyword">defun</span> <span class="function-name">htmlize-match-inv-spec</span> (inv)
  637. (member* inv buffer-invisibility-spec
  638. <span class="builtin">:key</span> (<span class="keyword">lambda</span> (i)
  639. (<span class="keyword">if</span> (symbolp i) i (car i)))))
  640. (<span class="keyword">defun</span> <span class="function-name">htmlize-decode-invisibility-spec</span> (invisible)
  641. <span class="comment-delimiter">;; </span><span class="comment">Return t, nil, or `</span><span class="comment"><span class="constant">ellipsis</span></span><span class="comment">', depending on how invisible text should be inserted.
  642. </span>
  643. (<span class="keyword">if</span> (not (listp buffer-invisibility-spec))
  644. <span class="comment-delimiter">;; </span><span class="comment">If buffer-invisibility-spec is not a list, then all
  645. </span> <span class="comment-delimiter">;; </span><span class="comment">characters with non-nil `</span><span class="comment"><span class="constant">invisible</span></span><span class="comment">' property are visible.
  646. </span> (not invisible)
  647. <span class="comment-delimiter">;; </span><span class="comment">Otherwise, the value of a non-nil `</span><span class="comment"><span class="constant">invisible</span></span><span class="comment">' property can be:
  648. </span> <span class="comment-delimiter">;; </span><span class="comment">1. a symbol -- make the text invisible if it matches
  649. </span> <span class="comment-delimiter">;; </span><span class="comment">buffer-invisibility-spec.
  650. </span> <span class="comment-delimiter">;; </span><span class="comment">2. a list of symbols -- make the text invisible if
  651. </span> <span class="comment-delimiter">;; </span><span class="comment">any symbol in the list matches
  652. </span> <span class="comment-delimiter">;; </span><span class="comment">buffer-invisibility-spec.
  653. </span> <span class="comment-delimiter">;; </span><span class="comment">If the match of buffer-invisibility-spec has a non-nil
  654. </span> <span class="comment-delimiter">;; </span><span class="comment">CDR, replace the invisible text with an ellipsis.
  655. </span> (<span class="keyword">let</span> ((match (<span class="keyword">if</span> (symbolp invisible)
  656. (htmlize-match-inv-spec invisible)
  657. (some #'htmlize-match-inv-spec invisible))))
  658. (<span class="keyword">cond</span> ((null match) t)
  659. ((cdr-safe (car match)) 'ellipsis)
  660. (t nil)))))
  661. (<span class="keyword">defun</span> <span class="function-name">htmlize-add-before-after-strings</span> (beg end text)
  662. <span class="comment-delimiter">;; </span><span class="comment">Find overlays specifying before-string and after-string in [beg,
  663. </span> <span class="comment-delimiter">;; </span><span class="comment">pos). If any are found, splice them into TEXT and return the new
  664. </span> <span class="comment-delimiter">;; </span><span class="comment">text.
  665. </span> (<span class="keyword">let</span> (additions)
  666. (<span class="keyword">dolist</span> (overlay (overlays-in beg end))
  667. (<span class="keyword">let</span> ((before (overlay-get overlay 'before-string))
  668. (after (overlay-get overlay 'after-string)))
  669. (<span class="keyword">when</span> after
  670. (push (cons (- (overlay-end overlay) beg)
  671. after)
  672. additions))
  673. (<span class="keyword">when</span> before
  674. (push (cons (- (overlay-start overlay) beg)
  675. before)
  676. additions))))
  677. (<span class="keyword">if</span> additions
  678. (<span class="keyword">let</span> ((textlist nil)
  679. (strpos 0))
  680. (<span class="keyword">dolist</span> (add (stable-sort additions #'&lt; <span class="builtin">:key</span> #'car))
  681. (<span class="keyword">let</span> ((addpos (car add))
  682. (addtext (cdr add)))
  683. (push (substring text strpos addpos) textlist)
  684. (push addtext textlist)
  685. (setq strpos addpos)))
  686. (push (substring text strpos) textlist)
  687. (apply #'concat (nreverse textlist)))
  688. text)))
  689. (<span class="keyword">defun</span> <span class="function-name">htmlize-copy-prop</span> (prop beg end string)
  690. <span class="comment-delimiter">;; </span><span class="comment">Copy the specified property from the specified region of the
  691. </span> <span class="comment-delimiter">;; </span><span class="comment">buffer to the target string. We cannot rely on Emacs to copy the
  692. </span> <span class="comment-delimiter">;; </span><span class="comment">property because we want to handle properties coming from both
  693. </span> <span class="comment-delimiter">;; </span><span class="comment">text properties and overlays.
  694. </span> (<span class="keyword">let</span> ((pos beg))
  695. (<span class="keyword">while</span> (&lt; pos end)
  696. (<span class="keyword">let</span> ((value (get-char-property pos prop))
  697. (next-change (htmlize-next-change pos prop end)))
  698. (<span class="keyword">when</span> value
  699. (put-text-property (- pos beg) (- next-change beg)
  700. prop value string))
  701. (setq pos next-change)))))
  702. (<span class="keyword">defun</span> <span class="function-name">htmlize-get-text-with-display</span> (beg end)
  703. <span class="comment-delimiter">;; </span><span class="comment">Like buffer-substring-no-properties, except it copies the
  704. </span> <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">display</span></span><span class="comment">' property from the buffer, if found.
  705. </span> (<span class="keyword">let</span> ((text (buffer-substring-no-properties beg end)))
  706. (htmlize-copy-prop 'display beg end text)
  707. (htmlize-copy-prop 'htmlize-link beg end text)
  708. (<span class="keyword">unless</span> htmlize-running-xemacs
  709. (setq text (htmlize-add-before-after-strings beg end text)))
  710. text))
  711. (<span class="keyword">defun</span> <span class="function-name">htmlize-buffer-substring-no-invisible</span> (beg end)
  712. <span class="comment-delimiter">;; </span><span class="comment">Like buffer-substring-no-properties, but don't copy invisible
  713. </span> <span class="comment-delimiter">;; </span><span class="comment">parts of the region. Where buffer-substring-no-properties
  714. </span> <span class="comment-delimiter">;; </span><span class="comment">mandates an ellipsis to be shown, htmlize-ellipsis is inserted.
  715. </span> (<span class="keyword">let</span> ((pos beg)
  716. visible-list invisible show last-show next-change)
  717. <span class="comment-delimiter">;; </span><span class="comment">Iterate over the changes in the `</span><span class="comment"><span class="constant">invisible</span></span><span class="comment">' property and filter
  718. </span> <span class="comment-delimiter">;; </span><span class="comment">out the portions where it's non-nil, i.e. where the text is
  719. </span> <span class="comment-delimiter">;; </span><span class="comment">invisible.
  720. </span> (<span class="keyword">while</span> (&lt; pos end)
  721. (setq invisible (get-char-property pos 'invisible)
  722. next-change (htmlize-next-change pos 'invisible end)
  723. show (htmlize-decode-invisibility-spec invisible))
  724. (<span class="keyword">cond</span> ((eq show t)
  725. (push (htmlize-get-text-with-display pos next-change)
  726. visible-list))
  727. ((and (eq show 'ellipsis)
  728. (not (eq last-show 'ellipsis))
  729. <span class="comment-delimiter">;; </span><span class="comment">Conflate successive ellipses.
  730. </span> (push htmlize-ellipsis visible-list))))
  731. (setq pos next-change last-show show))
  732. (htmlize-concat (nreverse visible-list))))
  733. (<span class="keyword">defun</span> <span class="function-name">htmlize-trim-ellipsis</span> (text)
  734. <span class="comment-delimiter">;; </span><span class="comment">Remove htmlize-ellipses ("...") from the beginning of TEXT if it
  735. </span> <span class="comment-delimiter">;; </span><span class="comment">starts with it. It checks for the special property of the
  736. </span> <span class="comment-delimiter">;; </span><span class="comment">ellipsis so it doesn't work on ordinary text that begins with
  737. </span> <span class="comment-delimiter">;; </span><span class="comment">"...".
  738. </span> (<span class="keyword">if</span> (get-text-property 0 'htmlize-ellipsis text)
  739. (substring text (length htmlize-ellipsis))
  740. text))
  741. (<span class="keyword">defconst</span> <span class="variable-name">htmlize-tab-spaces</span>
  742. <span class="comment-delimiter">;; </span><span class="comment">A table of strings with spaces. (aref htmlize-tab-spaces 5) is
  743. </span> <span class="comment-delimiter">;; </span><span class="comment">like (make-string 5 ?\ ), except it doesn't cons.
  744. </span> (<span class="keyword">let</span> ((v (make-vector 32 nil)))
  745. (<span class="keyword">dotimes</span> (i (length v))
  746. (setf (aref v i) (make-string i ?\ )))
  747. v))
  748. (<span class="keyword">defun</span> <span class="function-name">htmlize-untabify</span> (text start-column)
  749. <span class="doc">"Untabify TEXT, assuming it starts at START-COLUMN."</span>
  750. (<span class="keyword">let</span> ((column start-column)
  751. (last-match 0)
  752. (chunk-start 0)
  753. chunks match-pos tab-size)
  754. (<span class="keyword">while</span> (string-match <span class="string">"[\t\n]"</span> text last-match)
  755. (setq match-pos (match-beginning 0))
  756. (<span class="keyword">cond</span> ((eq (aref text match-pos) ?\t)
  757. <span class="comment-delimiter">;; </span><span class="comment">Encountered a tab: create a chunk of text followed by
  758. </span> <span class="comment-delimiter">;; </span><span class="comment">the expanded tab.
  759. </span> (push (substring text chunk-start match-pos) chunks)
  760. <span class="comment-delimiter">;; </span><span class="comment">Increase COLUMN by the length of the text we've
  761. </span> <span class="comment-delimiter">;; </span><span class="comment">skipped since last tab or newline. (Encountering
  762. </span> <span class="comment-delimiter">;; </span><span class="comment">newline resets it.)
  763. </span> (incf column (- match-pos last-match))
  764. <span class="comment-delimiter">;; </span><span class="comment">Calculate tab size based on tab-width and COLUMN.
  765. </span> (setq tab-size (- tab-width (% column tab-width)))
  766. <span class="comment-delimiter">;; </span><span class="comment">Expand the tab, carefully recreating the `</span><span class="comment"><span class="constant">display</span></span><span class="comment">'
  767. </span> <span class="comment-delimiter">;; </span><span class="comment">property if one was on the TAB.
  768. </span> (<span class="keyword">let</span> ((display (get-text-property match-pos 'display text))
  769. (expanded-tab (aref htmlize-tab-spaces tab-size)))
  770. (<span class="keyword">when</span> display
  771. (put-text-property 0 tab-size 'display display expanded-tab))
  772. (push expanded-tab chunks))
  773. (incf column tab-size)
  774. (setq chunk-start (1+ match-pos)))
  775. (t
  776. <span class="comment-delimiter">;; </span><span class="comment">Reset COLUMN at beginning of line.
  777. </span> (setq column 0)))
  778. (setq last-match (1+ match-pos)))
  779. <span class="comment-delimiter">;; </span><span class="comment">If no chunks have been allocated, it means there have been no
  780. </span> <span class="comment-delimiter">;; </span><span class="comment">tabs to expand. Return TEXT unmodified.
  781. </span> (<span class="keyword">if</span> (null chunks)
  782. text
  783. (<span class="keyword">when</span> (&lt; chunk-start (length text))
  784. <span class="comment-delimiter">;; </span><span class="comment">Push the remaining chunk.
  785. </span> (push (substring text chunk-start) chunks))
  786. <span class="comment-delimiter">;; </span><span class="comment">Generate the output from the available chunks.
  787. </span> (htmlize-concat (nreverse chunks)))))
  788. (<span class="keyword">defun</span> <span class="function-name">htmlize-extract-text</span> (beg end trailing-ellipsis)
  789. <span class="comment-delimiter">;; </span><span class="comment">Extract buffer text, sans the invisible parts. Then
  790. </span> <span class="comment-delimiter">;; </span><span class="comment">untabify it and escape the HTML metacharacters.
  791. </span> (<span class="keyword">let</span> ((text (htmlize-buffer-substring-no-invisible beg end)))
  792. (<span class="keyword">when</span> trailing-ellipsis
  793. (setq text (htmlize-trim-ellipsis text)))
  794. <span class="comment-delimiter">;; </span><span class="comment">If TEXT ends up empty, don't change trailing-ellipsis.
  795. </span> (<span class="keyword">when</span> (&gt; (length text) 0)
  796. (setq trailing-ellipsis
  797. (get-text-property (1- (length text))
  798. 'htmlize-ellipsis text)))
  799. (setq text (htmlize-untabify text (current-column)))
  800. (setq text (htmlize-string-to-html text))
  801. (values text trailing-ellipsis)))
  802. (<span class="keyword">defun</span> <span class="function-name">htmlize-despam-address</span> (string)
  803. <span class="doc">"Replace every occurrence of '@' in STRING with %40.
  804. This is used to protect mailto links without modifying their meaning."</span>
  805. <span class="comment-delimiter">;; </span><span class="comment">Suggested by Ville Skytta.
  806. </span> (<span class="keyword">while</span> (string-match <span class="string">"@"</span> string)
  807. (setq string (replace-match <span class="string">"%40"</span> nil t string)))
  808. string)
  809. (<span class="keyword">defun</span> <span class="function-name">htmlize-make-tmp-overlay</span> (beg end props)
  810. (<span class="keyword">let</span> ((overlay (htmlize-make-overlay beg end)))
  811. (htmlize-overlay-put overlay 'htmlize-tmp-overlay t)
  812. (<span class="keyword">while</span> props
  813. (htmlize-overlay-put overlay (pop props) (pop props)))
  814. overlay))
  815. (<span class="keyword">defun</span> <span class="function-name">htmlize-delete-tmp-overlays</span> ()
  816. (<span class="keyword">dolist</span> (overlay (htmlize-overlays-in (point-min) (point-max)))
  817. (<span class="keyword">when</span> (htmlize-overlay-get overlay 'htmlize-tmp-overlay)
  818. (htmlize-delete-overlay overlay))))
  819. (<span class="keyword">defun</span> <span class="function-name">htmlize-make-link-overlay</span> (beg end uri)
  820. (htmlize-make-tmp-overlay beg end `(htmlize-link (<span class="builtin">:uri</span> ,uri))))
  821. (<span class="keyword">defun</span> <span class="function-name">htmlize-create-auto-links</span> ()
  822. <span class="doc">"Add `</span><span class="doc"><span class="constant">htmlize-link</span></span><span class="doc">' property to all mailto links in the buffer."</span>
  823. (<span class="keyword">save-excursion</span>
  824. (goto-char (point-min))
  825. (<span class="keyword">while</span> (re-search-forward
  826. <span class="string">"&lt;</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">mailto:</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">?</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">[-=+_.a-zA-Z0-9]+@[-_.a-zA-Z0-9]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">&gt;"</span>
  827. nil t)
  828. (<span class="keyword">let*</span> ((address (match-string 3))
  829. (beg (match-beginning 0)) (end (match-end 0))
  830. (uri (concat <span class="string">"mailto:"</span> (htmlize-despam-address address))))
  831. (htmlize-make-link-overlay beg end uri)))
  832. (goto-char (point-min))
  833. (<span class="keyword">while</span> (re-search-forward <span class="string">"&lt;</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">URL:</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">?</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">[a-zA-Z]+://[</span><span class="string"><span class="negation-char">^</span></span><span class="string">;]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">&gt;"</span>
  834. nil t)
  835. (htmlize-make-link-overlay
  836. (match-beginning 0) (match-end 0) (match-string 3)))))
  837. <span class="comment-delimiter">;; </span><span class="comment">Tests for htmlize-create-auto-links:
  838. </span>
  839. <span class="comment-delimiter">;; </span><span class="comment"><a href="mailto:hniksic%40xemacs.org">&lt;mailto:hniksic@xemacs.org&gt;</a>
  840. </span><span class="comment-delimiter">;; </span><span class="comment"><a href="http://fly.srk.fer.hr">&lt;http://fly.srk.fer.hr&gt;</a>
  841. </span><span class="comment-delimiter">;; </span><span class="comment"><a href="http://www.xemacs.org">&lt;URL:http://www.xemacs.org&gt;</a>
  842. </span><span class="comment-delimiter">;; </span><span class="comment"><a href="http://www.mail-archive.com/bbdb-info@xemacs.org/">&lt;http://www.mail-archive.com/bbdb-info@xemacs.org/&gt;</a>
  843. </span><span class="comment-delimiter">;; </span><span class="comment"><a href="mailto:hniksic%40xemacs.org">&lt;hniksic@xemacs.org&gt;</a>
  844. </span><span class="comment-delimiter">;; </span><span class="comment"><a href="mailto:xalan-dev-sc.10148567319.hacuhiucknfgmpfnjcpg-john=doe.com%40xml.apache.org">&lt;xalan-dev-sc.10148567319.hacuhiucknfgmpfnjcpg-john=doe.com@xml.apache.org&gt;</a>
  845. </span>
  846. (<span class="keyword">defun</span> <span class="function-name">htmlize-shadow-form-feeds</span> ()
  847. (<span class="keyword">let</span> ((s <span class="string">"\n&lt;hr /&gt;"</span>))
  848. (put-text-property 0 (length s) 'htmlize-literal t s)
  849. (<span class="keyword">let</span> ((disp `(display ,s)))
  850. (<span class="keyword">while</span> (re-search-forward <span class="string">"\n\^L"</span> nil t)
  851. (htmlize-make-tmp-overlay (match-beginning 0) (match-end 0) disp)))))
  852. (<span class="keyword">defun</span> <span class="function-name">htmlize-defang-local-variables</span> ()
  853. <span class="comment-delimiter">;; </span><span class="comment">Juri Linkov reports that an HTML-ized "Local variables" can lead
  854. </span> <span class="comment-delimiter">;; </span><span class="comment">visiting the HTML to fail with "Local variables list is not
  855. </span> <span class="comment-delimiter">;; </span><span class="comment">properly terminated". He suggested changing the phrase to
  856. </span> <span class="comment-delimiter">;; </span><span class="comment">syntactically equivalent HTML that Emacs doesn't recognize.
  857. </span> (goto-char (point-min))
  858. (<span class="keyword">while</span> (search-forward <span class="string">"Local Variables&#58;"</span> nil t)
  859. (replace-match <span class="string">"Local Variables&amp;#58;"</span> nil t)))
  860. <hr />
  861. <span class="comment-delimiter">;;; </span><span class="comment">Color handling.
  862. </span>
  863. (<span class="keyword">defvar</span> <span class="variable-name">htmlize-x-library-search-path</span>
  864. `(,data-directory
  865. <span class="string">"/etc/X11/rgb.txt"</span>
  866. <span class="string">"/usr/share/X11/rgb.txt"</span>
  867. <span class="comment-delimiter">;; </span><span class="comment">the remainder of this list really belongs in a museum
  868. </span> <span class="string">"/usr/X11R6/lib/X11/"</span>
  869. <span class="string">"/usr/X11R5/lib/X11/"</span>
  870. <span class="string">"/usr/lib/X11R6/X11/"</span>
  871. <span class="string">"/usr/lib/X11R5/X11/"</span>
  872. <span class="string">"/usr/local/X11R6/lib/X11/"</span>
  873. <span class="string">"/usr/local/X11R5/lib/X11/"</span>
  874. <span class="string">"/usr/local/lib/X11R6/X11/"</span>
  875. <span class="string">"/usr/local/lib/X11R5/X11/"</span>
  876. <span class="string">"/usr/X11/lib/X11/"</span>
  877. <span class="string">"/usr/lib/X11/"</span>
  878. <span class="string">"/usr/local/lib/X11/"</span>
  879. <span class="string">"/usr/X386/lib/X11/"</span>
  880. <span class="string">"/usr/x386/lib/X11/"</span>
  881. <span class="string">"/usr/XFree86/lib/X11/"</span>
  882. <span class="string">"/usr/unsupported/lib/X11/"</span>
  883. <span class="string">"/usr/athena/lib/X11/"</span>
  884. <span class="string">"/usr/local/x11r5/lib/X11/"</span>
  885. <span class="string">"/usr/lpp/Xamples/lib/X11/"</span>
  886. <span class="string">"/usr/openwin/lib/X11/"</span>
  887. <span class="string">"/usr/openwin/share/lib/X11/"</span>))
  888. (<span class="keyword">defun</span> <span class="function-name">htmlize-get-color-rgb-hash</span> (<span class="type">&amp;optional</span> rgb-file)
  889. <span class="doc">"Return a hash table mapping X color names to RGB values.
  890. The keys in the hash table are X11 color names, and the values are the
  891. #rrggbb RGB specifications, extracted from `</span><span class="doc"><span class="constant">rgb.txt</span></span><span class="doc">'.
  892. If RGB-FILE is nil, the function will try hard to find a suitable file
  893. in the system directories.
  894. If no rgb.txt file is found, return nil."</span>
  895. (<span class="keyword">let</span> ((rgb-file (or rgb-file (locate-file
  896. <span class="string">"rgb.txt"</span>
  897. htmlize-x-library-search-path)))
  898. (hash nil))
  899. (<span class="keyword">when</span> rgb-file
  900. (<span class="keyword">with-temp-buffer</span>
  901. (insert-file-contents rgb-file)
  902. (setq hash (make-hash-table <span class="builtin">:test</span> 'equal))
  903. (<span class="keyword">while</span> (not (eobp))
  904. (<span class="keyword">cond</span> ((looking-at <span class="string">"^\\s-*</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">[!#]</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">|</span></span><span class="string">$</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">"</span>)
  905. <span class="comment-delimiter">;; </span><span class="comment">Skip comments and empty lines.
  906. </span> )
  907. ((looking-at
  908. <span class="string">"[ \t]*</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">[0-9]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">[ \t]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">[0-9]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">[ \t]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">[0-9]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">[ \t]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">.*</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">"</span>)
  909. (setf (gethash (downcase (match-string 4)) hash)
  910. (format <span class="string">"#%02x%02x%02x"</span>
  911. (string-to-number (match-string 1))
  912. (string-to-number (match-string 2))
  913. (string-to-number (match-string 3)))))
  914. (t
  915. (<span class="warning">error</span>
  916. <span class="string">"Unrecognized line in %s: %s"</span>
  917. rgb-file
  918. (buffer-substring (point) (<span class="keyword">progn</span> (end-of-line) (point))))))
  919. (forward-line 1))))
  920. hash))
  921. <span class="comment-delimiter">;; </span><span class="comment">Compile the RGB map when loaded. On systems where rgb.txt is
  922. </span><span class="comment-delimiter">;; </span><span class="comment">missing, the value of the variable will be nil, and rgb.txt will
  923. </span><span class="comment-delimiter">;; </span><span class="comment">not be used.
  924. </span>(<span class="keyword">defvar</span> <span class="variable-name">htmlize-color-rgb-hash</span> (htmlize-get-color-rgb-hash))
  925. <hr />
  926. <span class="comment-delimiter">;;; </span><span class="comment">Face handling.
  927. </span>
  928. (<span class="keyword">defun</span> <span class="function-name">htmlize-face-specifies-property</span> (face prop)
  929. <span class="comment-delimiter">;; </span><span class="comment">Return t if face specifies PROP, as opposed to it being inherited
  930. </span> <span class="comment-delimiter">;; </span><span class="comment">from the default face. The problem with e.g.
  931. </span> <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">face-foreground-instance</span></span><span class="comment">' is that it returns an instance for
  932. </span> <span class="comment-delimiter">;; </span><span class="comment">EVERY face because every face inherits from the default face.
  933. </span> <span class="comment-delimiter">;; </span><span class="comment">However, we'd like htmlize-face-{fore,back}ground to return nil
  934. </span> <span class="comment-delimiter">;; </span><span class="comment">when called with a face that doesn't specify its own foreground
  935. </span> <span class="comment-delimiter">;; </span><span class="comment">or background.
  936. </span> (or (eq face 'default)
  937. (assq 'global (specifier-spec-list (face-property face prop)))))
  938. (<span class="keyword">defun</span> <span class="function-name">htmlize-face-color-internal</span> (face fg)
  939. <span class="comment-delimiter">;; </span><span class="comment">Used only under GNU Emacs. Return the color of FACE, but don't
  940. </span> <span class="comment-delimiter">;; </span><span class="comment">return "unspecified-fg" or "unspecified-bg". If the face is
  941. </span> <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">default</span></span><span class="comment">' and the color is unspecified, look up the color in
  942. </span> <span class="comment-delimiter">;; </span><span class="comment">frame parameters.
  943. </span> (<span class="keyword">let*</span> ((function (<span class="keyword">if</span> fg #'face-foreground #'face-background))
  944. color)
  945. (<span class="keyword">if</span> (&gt;= emacs-major-version 22)
  946. <span class="comment-delimiter">;; </span><span class="comment">For GNU Emacs 22+ set INHERIT to get the inherited values.
  947. </span> (setq color (funcall function face nil t))
  948. (setq color (funcall function face))
  949. <span class="comment-delimiter">;; </span><span class="comment">For GNU Emacs 21 (which has `</span><span class="comment"><span class="constant">face-attribute</span></span><span class="comment">'): if the color
  950. </span> <span class="comment-delimiter">;; </span><span class="comment">is nil, recursively check for the face's parent.
  951. </span> (<span class="keyword">when</span> (and (null color)
  952. (fboundp 'face-attribute)
  953. (face-attribute face <span class="builtin">:inherit</span>)
  954. (not (eq (face-attribute face <span class="builtin">:inherit</span>) 'unspecified)))
  955. (setq color (htmlize-face-color-internal
  956. (face-attribute face <span class="builtin">:inherit</span>) fg))))
  957. (<span class="keyword">when</span> (and (eq face 'default) (null color))
  958. (setq color (cdr (assq (<span class="keyword">if</span> fg 'foreground-color 'background-color)
  959. (frame-parameters)))))
  960. (<span class="keyword">when</span> (or (eq color 'unspecified)
  961. (equal color <span class="string">"unspecified-fg"</span>)
  962. (equal color <span class="string">"unspecified-bg"</span>))
  963. (setq color nil))
  964. (<span class="keyword">when</span> (and (eq face 'default)
  965. (null color))
  966. <span class="comment-delimiter">;; </span><span class="comment">Assuming black on white doesn't seem right, but I can't think
  967. </span> <span class="comment-delimiter">;; </span><span class="comment">of anything better to do.
  968. </span> (setq color (<span class="keyword">if</span> fg <span class="string">"black"</span> <span class="string">"white"</span>)))
  969. color))
  970. (<span class="keyword">defun</span> <span class="function-name">htmlize-face-foreground</span> (face)
  971. <span class="comment-delimiter">;; </span><span class="comment">Return the name of the foreground color of FACE. If FACE does
  972. </span> <span class="comment-delimiter">;; </span><span class="comment">not specify a foreground color, return nil.
  973. </span> (<span class="keyword">cond</span> (htmlize-running-xemacs
  974. <span class="comment-delimiter">;; </span><span class="comment">XEmacs.
  975. </span> (and (htmlize-face-specifies-property face 'foreground)
  976. (color-instance-name (face-foreground-instance face))))
  977. (t
  978. <span class="comment-delimiter">;; </span><span class="comment">GNU Emacs.
  979. </span> (htmlize-face-color-internal face t))))
  980. (<span class="keyword">defun</span> <span class="function-name">htmlize-face-background</span> (face)
  981. <span class="comment-delimiter">;; </span><span class="comment">Return the name of the background color of FACE. If FACE does
  982. </span> <span class="comment-delimiter">;; </span><span class="comment">not specify a background color, return nil.
  983. </span> (<span class="keyword">cond</span> (htmlize-running-xemacs
  984. <span class="comment-delimiter">;; </span><span class="comment">XEmacs.
  985. </span> (and (htmlize-face-specifies-property face 'background)
  986. (color-instance-name (face-background-instance face))))
  987. (t
  988. <span class="comment-delimiter">;; </span><span class="comment">GNU Emacs.
  989. </span> (htmlize-face-color-internal face nil))))
  990. <span class="comment-delimiter">;; </span><span class="comment">Convert COLOR to the #RRGGBB string. If COLOR is already in that
  991. </span><span class="comment-delimiter">;; </span><span class="comment">format, it's left unchanged.
  992. </span>
  993. (<span class="keyword">defun</span> <span class="function-name">htmlize-color-to-rgb</span> (color)
  994. (<span class="keyword">let</span> ((rgb-string nil))
  995. (<span class="keyword">cond</span> ((null color)
  996. <span class="comment-delimiter">;; </span><span class="comment">Ignore nil COLOR because it means that the face is not
  997. </span> <span class="comment-delimiter">;; </span><span class="comment">specifying any color. Hence (htmlize-color-to-rgb nil)
  998. </span> <span class="comment-delimiter">;; </span><span class="comment">returns nil.
  999. </span> )
  1000. ((string-match <span class="string">"\\`#"</span> color)
  1001. <span class="comment-delimiter">;; </span><span class="comment">The color is already in #rrggbb format.
  1002. </span> (setq rgb-string color))
  1003. ((and htmlize-use-rgb-txt
  1004. htmlize-color-rgb-hash)
  1005. <span class="comment-delimiter">;; </span><span class="comment">Use of rgb.txt is requested, and it's available on the
  1006. </span> <span class="comment-delimiter">;; </span><span class="comment">system. Use it.
  1007. </span> (setq rgb-string (gethash (downcase color) htmlize-color-rgb-hash)))
  1008. (t
  1009. <span class="comment-delimiter">;; </span><span class="comment">We're getting the RGB components from Emacs.
  1010. </span> (<span class="keyword">let</span> ((rgb
  1011. (<span class="keyword">if</span> (fboundp 'color-instance-rgb-components)
  1012. (mapcar (<span class="keyword">lambda</span> (arg)
  1013. (/ arg 256))
  1014. (color-instance-rgb-components
  1015. (make-color-instance color)))
  1016. (mapcar (<span class="keyword">lambda</span> (arg)
  1017. (/ arg 256))
  1018. (color-values color)))))
  1019. (<span class="keyword">when</span> rgb
  1020. (setq rgb-string (apply #'format <span class="string">"#%02x%02x%02x"</span> rgb))))))
  1021. <span class="comment-delimiter">;; </span><span class="comment">If RGB-STRING is still nil, it means the color cannot be found,
  1022. </span> <span class="comment-delimiter">;; </span><span class="comment">for whatever reason. In that case just punt and return COLOR.
  1023. </span> <span class="comment-delimiter">;; </span><span class="comment">Most browsers support a decent set of color names anyway.
  1024. </span> (or rgb-string color)))
  1025. <span class="comment-delimiter">;; </span><span class="comment">We store the face properties we care about into an
  1026. </span><span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">htmlize-fstruct</span></span><span class="comment">' type. That way we only have to analyze face
  1027. </span><span class="comment-delimiter">;; </span><span class="comment">properties, which can be time consuming, once per each face. The
  1028. </span><span class="comment-delimiter">;; </span><span class="comment">mapping between Emacs faces and htmlize-fstructs is established by
  1029. </span><span class="comment-delimiter">;; </span><span class="comment">htmlize-make-face-map. The name "fstruct" refers to variables of
  1030. </span><span class="comment-delimiter">;; </span><span class="comment">type `</span><span class="comment"><span class="constant">htmlize-fstruct</span></span><span class="comment">', while the term "face" is reserved for Emacs
  1031. </span><span class="comment-delimiter">;; </span><span class="comment">faces.
  1032. </span>
  1033. (<span class="keyword">defstruct</span> <span class="type">htmlize-fstruct</span>
  1034. foreground <span class="comment-delimiter">; </span><span class="comment">foreground color, #rrggbb
  1035. </span> background <span class="comment-delimiter">; </span><span class="comment">background color, #rrggbb
  1036. </span> size <span class="comment-delimiter">; </span><span class="comment">size
  1037. </span> boldp <span class="comment-delimiter">; </span><span class="comment">whether face is bold
  1038. </span> italicp <span class="comment-delimiter">; </span><span class="comment">whether face is italic
  1039. </span> underlinep <span class="comment-delimiter">; </span><span class="comment">whether face is underlined
  1040. </span> overlinep <span class="comment-delimiter">; </span><span class="comment">whether face is overlined
  1041. </span> strikep <span class="comment-delimiter">; </span><span class="comment">whether face is struck through
  1042. </span> css-name <span class="comment-delimiter">; </span><span class="comment">CSS name of face
  1043. </span> )
  1044. (<span class="keyword">defun</span> <span class="function-name">htmlize-face-emacs21-attr</span> (fstruct attr value)
  1045. <span class="comment-delimiter">;; </span><span class="comment">For ATTR and VALUE, set the equivalent value in FSTRUCT.
  1046. </span> (<span class="keyword">case</span> attr
  1047. (<span class="builtin">:foreground</span>
  1048. (setf (htmlize-fstruct-foreground fstruct) (htmlize-color-to-rgb value)))
  1049. (<span class="builtin">:background</span>
  1050. (setf (htmlize-fstruct-background fstruct) (htmlize-color-to-rgb value)))
  1051. (<span class="builtin">:height</span>
  1052. (setf (htmlize-fstruct-size fstruct) value))
  1053. (<span class="builtin">:weight</span>
  1054. (<span class="keyword">when</span> (string-match (symbol-name value) <span class="string">"bold"</span>)
  1055. (setf (htmlize-fstruct-boldp fstruct) t)))
  1056. (<span class="builtin">:slant</span>
  1057. (setf (htmlize-fstruct-italicp fstruct) (or (eq value 'italic)
  1058. (eq value 'oblique))))
  1059. (<span class="builtin">:bold</span>
  1060. (setf (htmlize-fstruct-boldp fstruct) value))
  1061. (<span class="builtin">:italic</span>
  1062. (setf (htmlize-fstruct-italicp fstruct) value))
  1063. (<span class="builtin">:underline</span>
  1064. (setf (htmlize-fstruct-underlinep fstruct) value))
  1065. (<span class="builtin">:overline</span>
  1066. (setf (htmlize-fstruct-overlinep fstruct) value))
  1067. (<span class="builtin">:strike-through</span>
  1068. (setf (htmlize-fstruct-strikep fstruct) value))))
  1069. (<span class="keyword">defun</span> <span class="function-name">htmlize-face-size</span> (face)
  1070. <span class="comment-delimiter">;; </span><span class="comment">The size (height) of FACE, taking inheritance into account.
  1071. </span> <span class="comment-delimiter">;; </span><span class="comment">Only works in Emacs 21 and later.
  1072. </span> (<span class="keyword">let</span> ((size-list
  1073. (<span class="keyword">loop</span>
  1074. for f = face then (face-attribute f <span class="builtin">:inherit</span>)
  1075. until (or (not f) (eq f 'unspecified))
  1076. for h = (face-attribute f <span class="builtin">:height</span>)
  1077. collect (<span class="keyword">if</span> (eq h 'unspecified) nil h))))
  1078. (reduce 'htmlize-merge-size (cons nil size-list))))
  1079. (<span class="keyword">defun</span> <span class="function-name">htmlize-face-css-name</span> (face)
  1080. <span class="comment-delimiter">;; </span><span class="comment">Generate the css-name property for the given face. Emacs places
  1081. </span> <span class="comment-delimiter">;; </span><span class="comment">no restrictions on the names of symbols that represent faces --
  1082. </span> <span class="comment-delimiter">;; </span><span class="comment">any characters may be in the name, even control chars. We try
  1083. </span> <span class="comment-delimiter">;; </span><span class="comment">hard to beat the face name into shape, both esthetically and
  1084. </span> <span class="comment-delimiter">;; </span><span class="comment">according to CSS1 specs.
  1085. </span> (<span class="keyword">let</span> ((name (downcase (symbol-name face))))
  1086. (<span class="keyword">when</span> (string-match <span class="string">"\\`font-lock-"</span> name)
  1087. <span class="comment-delimiter">;; </span><span class="comment">font-lock-FOO-face -&gt; FOO.
  1088. </span> (setq name (replace-match <span class="string">""</span> t t name)))
  1089. (<span class="keyword">when</span> (string-match <span class="string">"-face\\'"</span> name)
  1090. <span class="comment-delimiter">;; </span><span class="comment">Drop the redundant "-face" suffix.
  1091. </span> (setq name (replace-match <span class="string">""</span> t t name)))
  1092. (<span class="keyword">while</span> (string-match <span class="string">"[</span><span class="string"><span class="negation-char">^</span></span><span class="string">-a-zA-Z0-9]"</span> name)
  1093. <span class="comment-delimiter">;; </span><span class="comment">Drop the non-alphanumerics.
  1094. </span> (setq name (replace-match <span class="string">"X"</span> t t name)))
  1095. (<span class="keyword">when</span> (string-match <span class="string">"\\`[-0-9]"</span> name)
  1096. <span class="comment-delimiter">;; </span><span class="comment">CSS identifiers may not start with a digit.
  1097. </span> (setq name (concat <span class="string">"X"</span> name)))
  1098. <span class="comment-delimiter">;; </span><span class="comment">After these transformations, the face could come out empty.
  1099. </span> (<span class="keyword">when</span> (equal name <span class="string">""</span>)
  1100. (setq name <span class="string">"face"</span>))
  1101. <span class="comment-delimiter">;; </span><span class="comment">Apply the prefix.
  1102. </span> (concat htmlize-css-name-prefix name)))
  1103. (<span class="keyword">defun</span> <span class="function-name">htmlize-face-to-fstruct</span> (face)
  1104. <span class="doc">"Convert Emacs face FACE to fstruct."</span>
  1105. (<span class="keyword">let</span> ((fstruct (make-htmlize-fstruct
  1106. <span class="builtin">:foreground</span> (htmlize-color-to-rgb
  1107. (htmlize-face-foreground face))
  1108. <span class="builtin">:background</span> (htmlize-color-to-rgb
  1109. (htmlize-face-background face)))))
  1110. (<span class="keyword">if</span> htmlize-running-xemacs
  1111. <span class="comment-delimiter">;; </span><span class="comment">XEmacs doesn't provide a way to detect whether a face is
  1112. </span> <span class="comment-delimiter">;; </span><span class="comment">bold or italic, so we need to examine the font instance.
  1113. </span> (<span class="keyword">let*</span> ((font-instance (face-font-instance face))
  1114. (props (font-instance-properties font-instance)))
  1115. (<span class="keyword">when</span> (equalp (cdr (assq 'WEIGHT_NAME props)) <span class="string">"bold"</span>)
  1116. (setf (htmlize-fstruct-boldp fstruct) t))
  1117. (<span class="keyword">when</span> (or (equalp (cdr (assq 'SLANT props)) <span class="string">"i"</span>)
  1118. (equalp (cdr (assq 'SLANT props)) <span class="string">"o"</span>))
  1119. (setf (htmlize-fstruct-italicp fstruct) t))
  1120. (setf (htmlize-fstruct-strikep fstruct)
  1121. (face-strikethru-p face))
  1122. (setf (htmlize-fstruct-underlinep fstruct)
  1123. (face-underline-p face)))
  1124. <span class="comment-delimiter">;; </span><span class="comment">GNU Emacs
  1125. </span> (<span class="keyword">dolist</span> (attr '(<span class="builtin">:weight</span> <span class="builtin">:slant</span> <span class="builtin">:underline</span> <span class="builtin">:overline</span> <span class="builtin">:strike-through</span>))
  1126. (<span class="keyword">let</span> ((value (<span class="keyword">if</span> (&gt;= emacs-major-version 22)
  1127. <span class="comment-delimiter">;; </span><span class="comment">Use the INHERIT arg in GNU Emacs 22.
  1128. </span> (face-attribute face attr nil t)
  1129. <span class="comment-delimiter">;; </span><span class="comment">Otherwise, fake it.
  1130. </span> (<span class="keyword">let</span> ((face face))
  1131. (<span class="keyword">while</span> (and (eq (face-attribute face attr)
  1132. 'unspecified)
  1133. (not (eq (face-attribute face <span class="builtin">:inherit</span>)
  1134. 'unspecified)))
  1135. (setq face (face-attribute face <span class="builtin">:inherit</span>)))
  1136. (face-attribute face attr)))))
  1137. (<span class="keyword">when</span> (and value (not (eq value 'unspecified)))
  1138. (htmlize-face-emacs21-attr fstruct attr value))))
  1139. (<span class="keyword">let</span> ((size (htmlize-face-size face)))
  1140. (<span class="keyword">unless</span> (eql size 1.0) <span class="comment-delimiter">; </span><span class="comment">ignore non-spec
  1141. </span> (setf (htmlize-fstruct-size fstruct) size))))
  1142. (setf (htmlize-fstruct-css-name fstruct) (htmlize-face-css-name face))
  1143. fstruct))
  1144. (<span class="keyword">defmacro</span> <span class="function-name">htmlize-copy-attr-if-set</span> (attr-list dest source)
  1145. <span class="comment-delimiter">;; </span><span class="comment">Generate code with the following pattern:
  1146. </span> <span class="comment-delimiter">;; </span><span class="comment">(progn
  1147. </span> <span class="comment-delimiter">;; </span><span class="comment">(when (htmlize-fstruct-ATTR source)
  1148. </span> <span class="comment-delimiter">;; </span><span class="comment">(setf (htmlize-fstruct-ATTR dest) (htmlize-fstruct-ATTR source)))
  1149. </span> <span class="comment-delimiter">;; </span><span class="comment">...)
  1150. </span> <span class="comment-delimiter">;; </span><span class="comment">for the given list of boolean attributes.
  1151. </span> (cons 'progn
  1152. (<span class="keyword">loop</span> for attr in attr-list
  1153. for attr-sym = (intern (format <span class="string">"htmlize-fstruct-%s"</span> attr))
  1154. collect `(<span class="keyword">when</span> (,attr-sym ,source)
  1155. (setf (,attr-sym ,dest) (,attr-sym ,source))))))
  1156. (<span class="keyword">defun</span> <span class="function-name">htmlize-merge-size</span> (merged next)
  1157. <span class="comment-delimiter">;; </span><span class="comment">Calculate the size of the merge of MERGED and NEXT.
  1158. </span> (<span class="keyword">cond</span> ((null merged) next)
  1159. ((integerp next) next)
  1160. ((null next) merged)
  1161. ((floatp merged) (* merged next))
  1162. ((integerp merged) (round (* merged next)))))
  1163. (<span class="keyword">defun</span> <span class="function-name">htmlize-merge-two-faces</span> (merged next)
  1164. (htmlize-copy-attr-if-set
  1165. (foreground background boldp italicp underlinep overlinep strikep)
  1166. merged next)
  1167. (setf (htmlize-fstruct-size merged)
  1168. (htmlize-merge-size (htmlize-fstruct-size merged)
  1169. (htmlize-fstruct-size next)))
  1170. merged)
  1171. (<span class="keyword">defun</span> <span class="function-name">htmlize-merge-faces</span> (fstruct-list)
  1172. (<span class="keyword">cond</span> ((null fstruct-list)
  1173. <span class="comment-delimiter">;; </span><span class="comment">Nothing to do, return a dummy face.
  1174. </span> (make-htmlize-fstruct))
  1175. ((null (cdr fstruct-list))
  1176. <span class="comment-delimiter">;; </span><span class="comment">Optimize for the common case of a single face, simply
  1177. </span> <span class="comment-delimiter">;; </span><span class="comment">return it.
  1178. </span> (car fstruct-list))
  1179. (t
  1180. (reduce #'htmlize-merge-two-faces
  1181. (cons (make-htmlize-fstruct) fstruct-list)))))
  1182. <span class="comment-delimiter">;; </span><span class="comment">GNU Emacs 20+ supports attribute lists in `</span><span class="comment"><span class="constant">face</span></span><span class="comment">' properties. For
  1183. </span><span class="comment-delimiter">;; </span><span class="comment">example, you can use `(:foreground "red" :weight bold)' as an
  1184. </span><span class="comment-delimiter">;; </span><span class="comment">overlay's "face", or you can even use a list of such lists, etc.
  1185. </span><span class="comment-delimiter">;; </span><span class="comment">We call those "attrlists".
  1186. </span><span class="comment-delimiter">;;</span><span class="comment">
  1187. </span><span class="comment-delimiter">;; </span><span class="comment">htmlize supports attrlist by converting them to fstructs, the same
  1188. </span><span class="comment-delimiter">;; </span><span class="comment">as with regular faces.
  1189. </span>
  1190. (<span class="keyword">defun</span> <span class="function-name">htmlize-attrlist-to-fstruct</span> (attrlist)
  1191. <span class="comment-delimiter">;; </span><span class="comment">Like htmlize-face-to-fstruct, but accepts an ATTRLIST as input.
  1192. </span> (<span class="keyword">let</span> ((fstruct (make-htmlize-fstruct)))
  1193. (<span class="keyword">cond</span> ((eq (car attrlist) 'foreground-color)
  1194. <span class="comment-delimiter">;; </span><span class="comment">ATTRLIST is (foreground-color . COLOR)
  1195. </span> (setf (htmlize-fstruct-foreground fstruct)
  1196. (htmlize-color-to-rgb (cdr attrlist))))
  1197. ((eq (car attrlist) 'background-color)
  1198. <span class="comment-delimiter">;; </span><span class="comment">ATTRLIST is (background-color . COLOR)
  1199. </span> (setf (htmlize-fstruct-background fstruct)
  1200. (htmlize-color-to-rgb (cdr attrlist))))
  1201. (t
  1202. <span class="comment-delimiter">;; </span><span class="comment">ATTRLIST is a plist.
  1203. </span> (<span class="keyword">while</span> attrlist
  1204. (<span class="keyword">let</span> ((attr (pop attrlist))
  1205. (value (pop attrlist)))
  1206. (<span class="keyword">when</span> (and value (not (eq value 'unspecified)))
  1207. (htmlize-face-emacs21-attr fstruct attr value))))))
  1208. (setf (htmlize-fstruct-css-name fstruct) <span class="string">"ATTRLIST"</span>)
  1209. fstruct))
  1210. (<span class="keyword">defun</span> <span class="function-name">htmlize-decode-face-prop</span> (prop)
  1211. <span class="doc">"Turn face property PROP into a list of face-like objects."</span>
  1212. <span class="comment-delimiter">;; </span><span class="comment">PROP can be a symbol naming a face, a string naming such a
  1213. </span> <span class="comment-delimiter">;; </span><span class="comment">symbol, a cons (foreground-color . COLOR) or (background-color
  1214. </span> <span class="comment-delimiter">;; </span><span class="comment">COLOR), a property list (:attr1 val1 :attr2 val2 ...), or a list
  1215. </span> <span class="comment-delimiter">;; </span><span class="comment">of any of those.
  1216. </span> <span class="comment-delimiter">;;</span><span class="comment">
  1217. </span> <span class="comment-delimiter">;; </span><span class="comment">(htmlize-decode-face-prop 'face) -&gt; (face)
  1218. </span> <span class="comment-delimiter">;; </span><span class="comment">(htmlize-decode-face-prop '(face1 face2)) -&gt; (face1 face2)
  1219. </span> <span class="comment-delimiter">;; </span><span class="comment">(htmlize-decode-face-prop '(:attr "val")) -&gt; ((:attr "val"))
  1220. </span> <span class="comment-delimiter">;; </span><span class="comment">(htmlize-decode-face-prop '((:attr "val") face (foreground-color "red")))
  1221. </span> <span class="comment-delimiter">;; </span><span class="comment">-&gt; ((:attr "val") face (foreground-color "red"))
  1222. </span> <span class="comment-delimiter">;;</span><span class="comment">
  1223. </span> <span class="comment-delimiter">;; </span><span class="comment">Unrecognized atoms or non-face symbols/strings are silently
  1224. </span> <span class="comment-delimiter">;; </span><span class="comment">stripped away.
  1225. </span> (<span class="keyword">cond</span> ((null prop)
  1226. nil)
  1227. ((symbolp prop)
  1228. (and (facep prop)
  1229. (list prop)))
  1230. ((stringp prop)
  1231. (and (facep (intern-soft prop))
  1232. (list prop)))
  1233. ((atom prop)
  1234. nil)
  1235. ((and (symbolp (car prop))
  1236. (eq ?: (aref (symbol-name (car prop)) 0)))
  1237. (list prop))
  1238. ((or (eq (car prop) 'foreground-color)
  1239. (eq (car prop) 'background-color))
  1240. (list prop))
  1241. (t
  1242. (apply #'nconc (mapcar #'htmlize-decode-face-prop prop)))))
  1243. (<span class="keyword">defun</span> <span class="function-name">htmlize-make-face-map</span> (faces)
  1244. <span class="comment-delimiter">;; </span><span class="comment">Return a hash table mapping Emacs faces to htmlize's fstructs.
  1245. </span> <span class="comment-delimiter">;; </span><span class="comment">The keys are either face symbols or attrlists, so the test
  1246. </span> <span class="comment-delimiter">;; </span><span class="comment">function must be `</span><span class="comment"><span class="constant">equal</span></span><span class="comment">'.
  1247. </span> (<span class="keyword">let</span> ((face-map (make-hash-table <span class="builtin">:test</span> 'equal))
  1248. css-names)
  1249. (<span class="keyword">dolist</span> (face faces)
  1250. (<span class="keyword">unless</span> (gethash face face-map)
  1251. <span class="comment-delimiter">;; </span><span class="comment">Haven't seen FACE yet; convert it to an fstruct and cache
  1252. </span> <span class="comment-delimiter">;; </span><span class="comment">it.
  1253. </span> (<span class="keyword">let</span> ((fstruct (<span class="keyword">if</span> (symbolp face)
  1254. (htmlize-face-to-fstruct face)
  1255. (htmlize-attrlist-to-fstruct face))))
  1256. (setf (gethash face face-map) fstruct)
  1257. (<span class="keyword">let*</span> ((css-name (htmlize-fstruct-css-name fstruct))
  1258. (new-name css-name)
  1259. (i 0))
  1260. <span class="comment-delimiter">;; </span><span class="comment">Uniquify the face's css-name by using NAME-1, NAME-2,
  1261. </span> <span class="comment-delimiter">;; </span><span class="comment">etc.
  1262. </span> (<span class="keyword">while</span> (member new-name css-names)
  1263. (setq new-name (format <span class="string">"%s-%s"</span> css-name (incf i))))
  1264. (<span class="keyword">unless</span> (equal new-name css-name)
  1265. (setf (htmlize-fstruct-css-name fstruct) new-name))
  1266. (push new-name css-names)))))
  1267. face-map))
  1268. (<span class="keyword">defun</span> <span class="function-name">htmlize-unstringify-face</span> (face)
  1269. <span class="doc">"If FACE is a string, return it interned, otherwise return it unchanged."</span>
  1270. (<span class="keyword">if</span> (stringp face)
  1271. (intern face)
  1272. face))
  1273. (<span class="keyword">defun</span> <span class="function-name">htmlize-faces-in-buffer</span> ()
  1274. <span class="doc">"Return a list of faces used in the current buffer.
  1275. Under XEmacs, this returns the set of faces specified by the extents
  1276. with the `</span><span class="doc"><span class="constant">face</span></span><span class="doc">' property. (This covers text properties as well.) Under
  1277. GNU Emacs, it returns the set of faces specified by the `</span><span class="doc"><span class="constant">face</span></span><span class="doc">' text
  1278. property and by buffer overlays that specify `</span><span class="doc"><span class="constant">face</span></span><span class="doc">'."</span>
  1279. (<span class="keyword">let</span> (faces)
  1280. <span class="comment-delimiter">;; </span><span class="comment">Testing for (fboundp 'map-extents) doesn't work because W3
  1281. </span> <span class="comment-delimiter">;; </span><span class="comment">defines `</span><span class="comment"><span class="constant">map-extents</span></span><span class="comment">' under FSF.
  1282. </span> (<span class="keyword">if</span> htmlize-running-xemacs
  1283. (<span class="keyword">let</span> (face-prop)
  1284. (map-extents (<span class="keyword">lambda</span> (extent ignored)
  1285. (setq face-prop (extent-face extent)
  1286. <span class="comment-delimiter">;; </span><span class="comment">FACE-PROP can be a face or a list of
  1287. </span> <span class="comment-delimiter">;; </span><span class="comment">faces.
  1288. </span> faces (<span class="keyword">if</span> (listp face-prop)
  1289. (union face-prop faces)
  1290. (adjoin face-prop faces)))
  1291. nil)
  1292. nil
  1293. <span class="comment-delimiter">;; </span><span class="comment">Specify endpoints explicitly to respect
  1294. </span> <span class="comment-delimiter">;; </span><span class="comment">narrowing.
  1295. </span> (point-min) (point-max) nil nil 'face))
  1296. <span class="comment-delimiter">;; </span><span class="comment">FSF Emacs code.
  1297. </span> <span class="comment-delimiter">;; </span><span class="comment">Faces used by text properties.
  1298. </span> (<span class="keyword">let</span> ((pos (point-min)) face-prop next)
  1299. (<span class="keyword">while</span> (&lt; pos (point-max))
  1300. (setq face-prop (get-text-property pos 'face)
  1301. next (or (next-single-property-change pos 'face) (point-max)))
  1302. (setq faces (nunion (htmlize-decode-face-prop face-prop)
  1303. faces <span class="builtin">:test</span> 'equal))
  1304. (setq pos next)))
  1305. <span class="comment-delimiter">;; </span><span class="comment">Faces used by overlays.
  1306. </span> (<span class="keyword">dolist</span> (overlay (overlays-in (point-min) (point-max)))
  1307. (<span class="keyword">let</span> ((face-prop (overlay-get overlay 'face)))
  1308. (setq faces (nunion (htmlize-decode-face-prop face-prop)
  1309. faces <span class="builtin">:test</span> 'equal)))))
  1310. faces))
  1311. <span class="comment-delimiter">;; </span><span class="comment">htmlize-faces-at-point returns the faces in use at point. The
  1312. </span><span class="comment-delimiter">;; </span><span class="comment">faces are sorted by increasing priority, i.e. the last face takes
  1313. </span><span class="comment-delimiter">;; </span><span class="comment">precedence.
  1314. </span><span class="comment-delimiter">;;</span><span class="comment">
  1315. </span><span class="comment-delimiter">;; </span><span class="comment">Under XEmacs, this returns all the faces in all the extents at
  1316. </span><span class="comment-delimiter">;; </span><span class="comment">point. Under GNU Emacs, this returns all the faces in the `</span><span class="comment"><span class="constant">face</span></span><span class="comment">'
  1317. </span><span class="comment-delimiter">;; </span><span class="comment">property and all the faces in the overlays at point.
  1318. </span>
  1319. (<span class="keyword">cond</span> (htmlize-running-xemacs
  1320. (<span class="keyword">defun</span> <span class="function-name">htmlize-faces-at-point</span> ()
  1321. (<span class="keyword">let</span> (extent extent-list face-list face-prop)
  1322. (<span class="keyword">while</span> (setq extent (extent-at (point) nil 'face extent))
  1323. (push extent extent-list))
  1324. <span class="comment-delimiter">;; </span><span class="comment">extent-list is in reverse display order, meaning that
  1325. </span> <span class="comment-delimiter">;; </span><span class="comment">smallest ones come last. That is the order we want,
  1326. </span> <span class="comment-delimiter">;; </span><span class="comment">except it can be overridden by the `</span><span class="comment"><span class="constant">priority</span></span><span class="comment">' property.
  1327. </span> (setq extent-list (stable-sort extent-list #'&lt;
  1328. <span class="builtin">:key</span> #'extent-priority))
  1329. (<span class="keyword">dolist</span> (extent extent-list)
  1330. (setq face-prop (extent-face extent))
  1331. <span class="comment-delimiter">;; </span><span class="comment">extent's face-list is in reverse order from what we
  1332. </span> <span class="comment-delimiter">;; </span><span class="comment">want, but the `</span><span class="comment"><span class="constant">nreverse</span></span><span class="comment">' below will take care of it.
  1333. </span> (setq face-list (<span class="keyword">if</span> (listp face-prop)
  1334. (append face-prop face-list)
  1335. (cons face-prop face-list))))
  1336. (nreverse face-list))))
  1337. (t
  1338. (<span class="keyword">defun</span> <span class="function-name">htmlize-faces-at-point</span> ()
  1339. (<span class="keyword">let</span> (all-faces)
  1340. <span class="comment-delimiter">;; </span><span class="comment">Faces from text properties.
  1341. </span> (<span class="keyword">let</span> ((face-prop (get-text-property (point) 'face)))
  1342. <span class="comment-delimiter">;; </span><span class="comment">we need to reverse the `</span><span class="comment"><span class="constant">face</span></span><span class="comment">' prop because we want
  1343. </span> <span class="comment-delimiter">;; </span><span class="comment">more specific faces to come later
  1344. </span> (setq all-faces (nreverse (htmlize-decode-face-prop face-prop))))
  1345. <span class="comment-delimiter">;; </span><span class="comment">Faces from overlays.
  1346. </span> (<span class="keyword">let</span> ((overlays
  1347. <span class="comment-delimiter">;; </span><span class="comment">Collect overlays at point that specify `</span><span class="comment"><span class="constant">face</span></span><span class="comment">'.
  1348. </span> (delete-if-not (<span class="keyword">lambda</span> (o)
  1349. (overlay-get o 'face))
  1350. (overlays-at (point))))
  1351. list face-prop)
  1352. <span class="comment-delimiter">;; </span><span class="comment">Sort the overlays so the smaller (more specific) ones
  1353. </span> <span class="comment-delimiter">;; </span><span class="comment">come later. The number of overlays at each one
  1354. </span> <span class="comment-delimiter">;; </span><span class="comment">position should be very small, so the sort shouldn't
  1355. </span> <span class="comment-delimiter">;; </span><span class="comment">slow things down.
  1356. </span> (setq overlays (sort* overlays
  1357. <span class="comment-delimiter">;; </span><span class="comment">Sort by ascending...
  1358. </span> #'&lt;
  1359. <span class="comment-delimiter">;; </span><span class="comment">...overlay size.
  1360. </span> <span class="builtin">:key</span> (<span class="keyword">lambda</span> (o)
  1361. (- (overlay-end o)
  1362. (overlay-start o)))))
  1363. <span class="comment-delimiter">;; </span><span class="comment">Overlay priorities, if present, override the above
  1364. </span> <span class="comment-delimiter">;; </span><span class="comment">established order. Larger overlay priority takes
  1365. </span> <span class="comment-delimiter">;; </span><span class="comment">precedence and therefore comes later in the list.
  1366. </span> (setq overlays (stable-sort
  1367. overlays
  1368. <span class="comment-delimiter">;; </span><span class="comment">Reorder (stably) by acending...
  1369. </span> #'&lt;
  1370. <span class="comment-delimiter">;; </span><span class="comment">...overlay priority.
  1371. </span> <span class="builtin">:key</span> (<span class="keyword">lambda</span> (o)
  1372. (or (overlay-get o 'priority) 0))))
  1373. (<span class="keyword">dolist</span> (overlay overlays)
  1374. (setq face-prop (overlay-get overlay 'face)
  1375. list (nconc (htmlize-decode-face-prop face-prop) list)))
  1376. <span class="comment-delimiter">;; </span><span class="comment">Under "Merging Faces" the manual explicitly states
  1377. </span> <span class="comment-delimiter">;; </span><span class="comment">that faces specified by overlays take precedence over
  1378. </span> <span class="comment-delimiter">;; </span><span class="comment">faces specified by text properties.
  1379. </span> (setq all-faces (nconc all-faces list)))
  1380. all-faces))))
  1381. <hr />
  1382. <span class="comment-delimiter">;; </span><span class="comment">htmlize supports generating HTML in several flavors, some of which
  1383. </span><span class="comment-delimiter">;; </span><span class="comment">use CSS, and others the &lt;font&gt; element. We take an OO approach and
  1384. </span><span class="comment-delimiter">;; </span><span class="comment">define "methods" that indirect to the functions that depend on
  1385. </span><span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">htmlize-output-type</span></span><span class="comment">'. The currently used methods are `</span><span class="comment"><span class="constant">doctype</span></span><span class="comment">',
  1386. </span><span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">insert-head</span></span><span class="comment">', `</span><span class="comment"><span class="constant">body-tag</span></span><span class="comment">', and `</span><span class="comment"><span class="constant">text-markup</span></span><span class="comment">'. Not all output types
  1387. </span><span class="comment-delimiter">;; </span><span class="comment">define all methods.
  1388. </span><span class="comment-delimiter">;;</span><span class="comment">
  1389. </span><span class="comment-delimiter">;; </span><span class="comment">Methods are called either with (htmlize-method METHOD ARGS...)
  1390. </span><span class="comment-delimiter">;; </span><span class="comment">special form, or by accessing the function with
  1391. </span><span class="comment-delimiter">;; </span><span class="comment">(htmlize-method-function 'METHOD) and calling (funcall FUNCTION).
  1392. </span><span class="comment-delimiter">;; </span><span class="comment">The latter form is useful in tight loops because `</span><span class="comment"><span class="constant">htmlize-method</span></span><span class="comment">'
  1393. </span><span class="comment-delimiter">;; </span><span class="comment">conses.
  1394. </span>
  1395. (<span class="keyword">defmacro</span> <span class="function-name">htmlize-method</span> (method <span class="type">&amp;rest</span> args)
  1396. <span class="comment-delimiter">;; </span><span class="comment">Expand to (htmlize-TYPE-METHOD ...ARGS...). TYPE is the value of
  1397. </span> <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">htmlize-output-type</span></span><span class="comment">' at run time.
  1398. </span> `(funcall (htmlize-method-function ',method) ,@args))
  1399. (<span class="keyword">defun</span> <span class="function-name">htmlize-method-function</span> (method)
  1400. <span class="comment-delimiter">;; </span><span class="comment">Return METHOD's function definition for the current output type.
  1401. </span> <span class="comment-delimiter">;; </span><span class="comment">The returned object can be safely funcalled.
  1402. </span> (<span class="keyword">let</span> ((sym (intern (format <span class="string">"htmlize-%s-%s"</span> htmlize-output-type method))))
  1403. (indirect-function (<span class="keyword">if</span> (fboundp sym)
  1404. sym
  1405. (<span class="keyword">let</span> ((default (intern (concat <span class="string">"htmlize-default-"</span>
  1406. (symbol-name method)))))
  1407. (<span class="keyword">if</span> (fboundp default)
  1408. default
  1409. 'ignore))))))
  1410. (<span class="keyword">defvar</span> <span class="variable-name">htmlize-memoization-table</span> (make-hash-table <span class="builtin">:test</span> 'equal))
  1411. (<span class="keyword">defmacro</span> <span class="function-name">htmlize-memoize</span> (key generator)
  1412. <span class="doc">"Return the value of GENERATOR, memoized as KEY.
  1413. That means that GENERATOR will be evaluated and returned the first time
  1414. it's called with the same value of KEY. All other times, the cached
  1415. \(memoized) value will be returned."</span>
  1416. (<span class="keyword">let</span> ((value (gensym)))
  1417. `(<span class="keyword">let</span> ((,value (gethash ,key htmlize-memoization-table)))
  1418. (<span class="keyword">unless</span> ,value
  1419. (setq ,value ,generator)
  1420. (setf (gethash ,key htmlize-memoization-table) ,value))
  1421. ,value)))
  1422. <hr />
  1423. <span class="comment-delimiter">;;; </span><span class="comment">Default methods.
  1424. </span>
  1425. (<span class="keyword">defun</span> <span class="function-name">htmlize-default-doctype</span> ()
  1426. nil <span class="comment-delimiter">; </span><span class="comment">no doc-string
  1427. </span> <span class="comment-delimiter">;; </span><span class="comment">Note that the `</span><span class="comment"><span class="constant">font</span></span><span class="comment">' output is technically invalid under this DTD
  1428. </span> <span class="comment-delimiter">;; </span><span class="comment">because the DTD doesn't allow embedding &lt;font&gt; in &lt;pre&gt;.
  1429. </span> <span class="string">"&lt;!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"&gt;"</span>
  1430. )
  1431. (<span class="keyword">defun</span> <span class="function-name">htmlize-default-body-tag</span> (face-map)
  1432. nil <span class="comment-delimiter">; </span><span class="comment">no doc-string
  1433. </span> face-map <span class="comment-delimiter">; </span><span class="comment">shut up the byte-compiler
  1434. </span> <span class="string">"&lt;body&gt;"</span>)
  1435. <hr />
  1436. <span class="comment-delimiter">;;; </span><span class="comment">CSS based output support.
  1437. </span>
  1438. <span class="comment-delimiter">;; </span><span class="comment">Internal function; not a method.
  1439. </span>(<span class="keyword">defun</span> <span class="function-name">htmlize-css-specs</span> (fstruct)
  1440. (<span class="keyword">let</span> (result)
  1441. (<span class="keyword">when</span> (htmlize-fstruct-foreground fstruct)
  1442. (push (format <span class="string">"color: %s;"</span> (htmlize-fstruct-foreground fstruct))
  1443. result))
  1444. (<span class="keyword">when</span> (htmlize-fstruct-background fstruct)
  1445. (push (format <span class="string">"background-color: %s;"</span>
  1446. (htmlize-fstruct-background fstruct))
  1447. result))
  1448. (<span class="keyword">let</span> ((size (htmlize-fstruct-size fstruct)))
  1449. (<span class="keyword">when</span> (and size (not (eq htmlize-ignore-face-size t)))
  1450. (<span class="keyword">cond</span> ((floatp size)
  1451. (push (format <span class="string">"font-size: %d%%;"</span> (* 100 size)) result))
  1452. ((not (eq htmlize-ignore-face-size 'absolute))
  1453. (push (format <span class="string">"font-size: %spt;"</span> (/ size 10.0)) result)))))
  1454. (<span class="keyword">when</span> (htmlize-fstruct-boldp fstruct)
  1455. (push <span class="string">"font-weight: bold;"</span> result))
  1456. (<span class="keyword">when</span> (htmlize-fstruct-italicp fstruct)
  1457. (push <span class="string">"font-style: italic;"</span> result))
  1458. (<span class="keyword">when</span> (htmlize-fstruct-underlinep fstruct)
  1459. (push <span class="string">"text-decoration: underline;"</span> result))
  1460. (<span class="keyword">when</span> (htmlize-fstruct-overlinep fstruct)
  1461. (push <span class="string">"text-decoration: overline;"</span> result))
  1462. (<span class="keyword">when</span> (htmlize-fstruct-strikep fstruct)
  1463. (push <span class="string">"text-decoration: line-through;"</span> result))
  1464. (nreverse result)))
  1465. (<span class="keyword">defun</span> <span class="function-name">htmlize-css-insert-head</span> (buffer-faces face-map)
  1466. (insert <span class="string">" &lt;style type=\"text/css\"&gt;\n &lt;!--\n"</span>)
  1467. (insert <span class="string">" body {\n "</span>
  1468. (mapconcat #'identity
  1469. (htmlize-css-specs (gethash 'default face-map))
  1470. <span class="string">"\n "</span>)
  1471. <span class="string">"\n }\n"</span>)
  1472. (<span class="keyword">dolist</span> (face (sort* (copy-list buffer-faces) #'string-lessp
  1473. <span class="builtin">:key</span> (<span class="keyword">lambda</span> (f)
  1474. (htmlize-fstruct-css-name (gethash f face-map)))))
  1475. (<span class="keyword">let*</span> ((fstruct (gethash face face-map))
  1476. (cleaned-up-face-name
  1477. (<span class="keyword">let</span> ((s
  1478. <span class="comment-delimiter">;; </span><span class="comment">Use `</span><span class="comment"><span class="constant">prin1-to-string</span></span><span class="comment">' rather than `</span><span class="comment"><span class="constant">symbol-name</span></span><span class="comment">'
  1479. </span> <span class="comment-delimiter">;; </span><span class="comment">to get the face name because the "face" can also
  1480. </span> <span class="comment-delimiter">;; </span><span class="comment">be an attrlist, which is not a symbol.
  1481. </span> (prin1-to-string face)))
  1482. <span class="comment-delimiter">;; </span><span class="comment">If the name contains `</span><span class="comment"><span class="constant">--</span></span><span class="comment">' or `</span><span class="comment"><span class="constant">*/</span></span><span class="comment">', remove them.
  1483. </span> (<span class="keyword">while</span> (string-match <span class="string">"--"</span> s)
  1484. (setq s (replace-match <span class="string">"-"</span> t t s)))
  1485. (<span class="keyword">while</span> (string-match <span class="string">"\\*/"</span> s)
  1486. (setq s (replace-match <span class="string">"XX"</span> t t s)))
  1487. s))
  1488. (specs (htmlize-css-specs fstruct)))
  1489. (insert <span class="string">" ."</span> (htmlize-fstruct-css-name fstruct))
  1490. (<span class="keyword">if</span> (null specs)
  1491. (insert <span class="string">" {"</span>)
  1492. (insert <span class="string">" {\n /* "</span> cleaned-up-face-name <span class="string">" */\n "</span>
  1493. (mapconcat #'identity specs <span class="string">"\n "</span>)))
  1494. (insert <span class="string">"\n }\n"</span>)))
  1495. (insert htmlize-hyperlink-style
  1496. <span class="string">" --&gt;\n &lt;/style&gt;\n"</span>))
  1497. (<span class="keyword">defun</span> <span class="function-name">htmlize-css-text-markup</span> (fstruct-list buffer)
  1498. <span class="comment-delimiter">;; </span><span class="comment">Open the markup needed to insert text colored with FACES into
  1499. </span> <span class="comment-delimiter">;; </span><span class="comment">BUFFER. Return the function that closes the markup.
  1500. </span>
  1501. <span class="comment-delimiter">;; </span><span class="comment">In CSS mode, this is easy: just nest the text in one &lt;span
  1502. </span> <span class="comment-delimiter">;; </span><span class="comment">class=...&gt; tag for each face in FSTRUCT-LIST.
  1503. </span> (<span class="keyword">dolist</span> (fstruct fstruct-list)
  1504. (princ <span class="string">"&lt;span class=\""</span> buffer)
  1505. (princ (htmlize-fstruct-css-name fstruct) buffer)
  1506. (princ <span class="string">"\"&gt;"</span> buffer))
  1507. (htmlize-lexlet ((fstruct-list fstruct-list) (buffer buffer))
  1508. (<span class="keyword">lambda</span> ()
  1509. (<span class="keyword">dolist</span> (fstruct fstruct-list)
  1510. (ignore fstruct) <span class="comment-delimiter">; </span><span class="comment">shut up the byte-compiler
  1511. </span> (princ <span class="string">"&lt;/span&gt;"</span> buffer)))))
  1512. <hr />
  1513. <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">inline-css</span></span><span class="comment">' output support.
  1514. </span>
  1515. (<span class="keyword">defun</span> <span class="function-name">htmlize-inline-css-body-tag</span> (face-map)
  1516. (format <span class="string">"&lt;body style=\"%s\"&gt;"</span>
  1517. (mapconcat #'identity (htmlize-css-specs (gethash 'default face-map))
  1518. <span class="string">" "</span>)))
  1519. (<span class="keyword">defun</span> <span class="function-name">htmlize-inline-css-text-markup</span> (fstruct-list buffer)
  1520. (<span class="keyword">let*</span> ((merged (htmlize-merge-faces fstruct-list))
  1521. (style (htmlize-memoize
  1522. merged
  1523. (<span class="keyword">let</span> ((specs (htmlize-css-specs merged)))
  1524. (and specs
  1525. (mapconcat #'identity (htmlize-css-specs merged) <span class="string">" "</span>))))))
  1526. (<span class="keyword">when</span> style
  1527. (princ <span class="string">"&lt;span style=\""</span> buffer)
  1528. (princ style buffer)
  1529. (princ <span class="string">"\"&gt;"</span> buffer))
  1530. (htmlize-lexlet ((style style) (buffer buffer))
  1531. (<span class="keyword">lambda</span> ()
  1532. (<span class="keyword">when</span> style
  1533. (princ <span class="string">"&lt;/span&gt;"</span> buffer))))))
  1534. <hr />
  1535. <span class="comment-delimiter">;;; </span><span class="comment">`</span><span class="comment"><span class="constant">font</span></span><span class="comment">' tag based output support.
  1536. </span>
  1537. (<span class="keyword">defun</span> <span class="function-name">htmlize-font-body-tag</span> (face-map)
  1538. (<span class="keyword">let</span> ((fstruct (gethash 'default face-map)))
  1539. (format <span class="string">"&lt;body text=\"%s\" bgcolor=\"%s\"&gt;"</span>
  1540. (htmlize-fstruct-foreground fstruct)
  1541. (htmlize-fstruct-background fstruct))))
  1542. (<span class="keyword">defun</span> <span class="function-name">htmlize-font-text-markup</span> (fstruct-list buffer)
  1543. <span class="comment-delimiter">;; </span><span class="comment">In `</span><span class="comment"><span class="constant">font</span></span><span class="comment">' mode, we use the traditional HTML means of altering
  1544. </span> <span class="comment-delimiter">;; </span><span class="comment">presentation: &lt;font&gt; tag for colors, &lt;b&gt; for bold, &lt;u&gt; for
  1545. </span> <span class="comment-delimiter">;; </span><span class="comment">underline, and &lt;strike&gt; for strike-through.
  1546. </span> (<span class="keyword">let*</span> ((merged (htmlize-merge-faces fstruct-list))
  1547. (markup (htmlize-memoize
  1548. merged
  1549. (cons (concat
  1550. (and (htmlize-fstruct-foreground merged)
  1551. (format <span class="string">"&lt;font color=\"%s\"&gt;"</span> (htmlize-fstruct-foreground merged)))
  1552. (and (htmlize-fstruct-boldp merged) <span class="string">"&lt;b&gt;"</span>)
  1553. (and (htmlize-fstruct-italicp merged) <span class="string">"&lt;i&gt;"</span>)
  1554. (and (htmlize-fstruct-underlinep merged) <span class="string">"&lt;u&gt;"</span>)
  1555. (and (htmlize-fstruct-strikep merged) <span class="string">"&lt;strike&gt;"</span>))
  1556. (concat
  1557. (and (htmlize-fstruct-strikep merged) <span class="string">"&lt;/strike&gt;"</span>)
  1558. (and (htmlize-fstruct-underlinep merged) <span class="string">"&lt;/u&gt;"</span>)
  1559. (and (htmlize-fstruct-italicp merged) <span class="string">"&lt;/i&gt;"</span>)
  1560. (and (htmlize-fstruct-boldp merged) <span class="string">"&lt;/b&gt;"</span>)
  1561. (and (htmlize-fstruct-foreground merged) <span class="string">"&lt;/font&gt;"</span>))))))
  1562. (princ (car markup) buffer)
  1563. (htmlize-lexlet ((markup markup) (buffer buffer))
  1564. (<span class="keyword">lambda</span> ()
  1565. (princ (cdr markup) buffer)))))
  1566. <hr />
  1567. (<span class="keyword">defun</span> <span class="function-name">htmlize-buffer-1</span> ()
  1568. <span class="comment-delimiter">;; </span><span class="comment">Internal function; don't call it from outside this file. Htmlize
  1569. </span> <span class="comment-delimiter">;; </span><span class="comment">current buffer, writing the resulting HTML to a new buffer, and
  1570. </span> <span class="comment-delimiter">;; </span><span class="comment">return it. Unlike htmlize-buffer, this doesn't change current
  1571. </span> <span class="comment-delimiter">;; </span><span class="comment">buffer or use switch-to-buffer.
  1572. </span> (<span class="keyword">save-excursion</span>
  1573. <span class="comment-delimiter">;; </span><span class="comment">Protect against the hook changing the current buffer.
  1574. </span> (<span class="keyword">save-excursion</span>
  1575. (run-hooks 'htmlize-before-hook))
  1576. <span class="comment-delimiter">;; </span><span class="comment">Convince font-lock support modes to fontify the entire buffer
  1577. </span> <span class="comment-delimiter">;; </span><span class="comment">in advance.
  1578. </span> (htmlize-ensure-fontified)
  1579. (clrhash htmlize-extended-character-cache)
  1580. (clrhash htmlize-memoization-table)
  1581. <span class="comment-delimiter">;; </span><span class="comment">It's important that the new buffer inherits default-directory
  1582. </span> <span class="comment-delimiter">;; </span><span class="comment">from the current buffer.
  1583. </span> (<span class="keyword">let</span> ((htmlbuf (generate-new-buffer (<span class="keyword">if</span> (buffer-file-name)
  1584. (htmlize-make-file-name
  1585. (file-name-nondirectory
  1586. (buffer-file-name)))
  1587. <span class="string">"*html*"</span>)))
  1588. (completed nil))
  1589. (<span class="keyword">unwind-protect</span>
  1590. (<span class="keyword">let*</span> ((buffer-faces (htmlize-faces-in-buffer))
  1591. (face-map (htmlize-make-face-map (adjoin 'default buffer-faces)))
  1592. (places (gensym))
  1593. (title (<span class="keyword">if</span> (buffer-file-name)
  1594. (file-name-nondirectory (buffer-file-name))
  1595. (buffer-name))))
  1596. (<span class="keyword">when</span> htmlize-generate-hyperlinks
  1597. (htmlize-create-auto-links))
  1598. (<span class="keyword">when</span> htmlize-replace-form-feeds
  1599. (htmlize-shadow-form-feeds))
  1600. <span class="comment-delimiter">;; </span><span class="comment">Initialize HTMLBUF and insert the HTML prolog.
  1601. </span> (<span class="keyword">with-current-buffer</span> htmlbuf
  1602. (buffer-disable-undo)
  1603. (insert (htmlize-method doctype) ?\n
  1604. (format <span class="string">"&lt;!-- Created by htmlize-%s in %s mode. --&gt;\n"</span>
  1605. htmlize-version htmlize-output-type)
  1606. <span class="string">"&lt;html&gt;\n "</span>)
  1607. (put places 'head-start (point-marker))
  1608. (insert <span class="string">"&lt;head&gt;\n"</span>
  1609. <span class="string">" &lt;title&gt;"</span> (htmlize-protect-string title) <span class="string">"&lt;/title&gt;\n"</span>
  1610. (<span class="keyword">if</span> htmlize-html-charset
  1611. (format (concat <span class="string">" &lt;meta http-equiv=\"Content-Type\" "</span>
  1612. <span class="string">"content=\"text/html; charset=%s\"&gt;\n"</span>)
  1613. htmlize-html-charset)
  1614. <span class="string">""</span>)
  1615. htmlize-head-tags)
  1616. (htmlize-method insert-head buffer-faces face-map)
  1617. (insert <span class="string">" &lt;/head&gt;"</span>)
  1618. (put places 'head-end (point-marker))
  1619. (insert <span class="string">"\n "</span>)
  1620. (put places 'body-start (point-marker))
  1621. (insert (htmlize-method body-tag face-map)
  1622. <span class="string">"\n "</span>)
  1623. (put places 'content-start (point-marker))
  1624. (insert <span class="string">"&lt;pre&gt;\n"</span>))
  1625. (<span class="keyword">let</span> ((text-markup
  1626. <span class="comment-delimiter">;; </span><span class="comment">Get the inserter method, so we can funcall it inside
  1627. </span> <span class="comment-delimiter">;; </span><span class="comment">the loop. Not calling `</span><span class="comment"><span class="constant">htmlize-method</span></span><span class="comment">' in the loop
  1628. </span> <span class="comment-delimiter">;; </span><span class="comment">body yields a measurable speed increase.
  1629. </span> (htmlize-method-function 'text-markup))
  1630. <span class="comment-delimiter">;; </span><span class="comment">Declare variables used in loop body outside the loop
  1631. </span> <span class="comment-delimiter">;; </span><span class="comment">because it's faster to establish `</span><span class="comment"><span class="constant">let</span></span><span class="comment">' bindings only
  1632. </span> <span class="comment-delimiter">;; </span><span class="comment">once.
  1633. </span> next-change text face-list trailing-ellipsis
  1634. fstruct-list last-fstruct-list
  1635. (close-markup (<span class="keyword">lambda</span> ())))
  1636. <span class="comment-delimiter">;; </span><span class="comment">This loop traverses and reads the source buffer, appending
  1637. </span> <span class="comment-delimiter">;; </span><span class="comment">the resulting HTML to HTMLBUF. This method is fast
  1638. </span> <span class="comment-delimiter">;; </span><span class="comment">because: 1) it doesn't require examining the text
  1639. </span> <span class="comment-delimiter">;; </span><span class="comment">properties char by char (htmlize-next-face-change is used
  1640. </span> <span class="comment-delimiter">;; </span><span class="comment">to move between runs with the same face), and 2) it doesn't
  1641. </span> <span class="comment-delimiter">;; </span><span class="comment">require frequent buffer switches, which are slow because
  1642. </span> <span class="comment-delimiter">;; </span><span class="comment">they rebind all buffer-local vars.
  1643. </span> (goto-char (point-min))
  1644. (<span class="keyword">while</span> (not (eobp))
  1645. (setq next-change (htmlize-next-face-change (point)))
  1646. <span class="comment-delimiter">;; </span><span class="comment">Get faces in use between (point) and NEXT-CHANGE, and
  1647. </span> <span class="comment-delimiter">;; </span><span class="comment">convert them to fstructs.
  1648. </span> (setq face-list (htmlize-faces-at-point)
  1649. fstruct-list (delq nil (mapcar (<span class="keyword">lambda</span> (f)
  1650. (gethash f face-map))
  1651. face-list)))
  1652. (multiple-value-setq (text trailing-ellipsis)
  1653. (htmlize-extract-text (point) next-change trailing-ellipsis))
  1654. <span class="comment-delimiter">;; </span><span class="comment">Don't bother writing anything if there's no text (this
  1655. </span> <span class="comment-delimiter">;; </span><span class="comment">happens in invisible regions).
  1656. </span> (<span class="keyword">when</span> (&gt; (length text) 0)
  1657. <span class="comment-delimiter">;; </span><span class="comment">Open the new markup if necessary and insert the text.
  1658. </span> (<span class="keyword">when</span> (not (equalp fstruct-list last-fstruct-list))
  1659. (funcall close-markup)
  1660. (setq last-fstruct-list fstruct-list
  1661. close-markup (funcall text-markup fstruct-list htmlbuf)))
  1662. (princ text htmlbuf))
  1663. (goto-char next-change))
  1664. <span class="comment-delimiter">;; </span><span class="comment">We've gone through the buffer; close the markup from
  1665. </span> <span class="comment-delimiter">;; </span><span class="comment">the last run, if any.
  1666. </span> (funcall close-markup))
  1667. <span class="comment-delimiter">;; </span><span class="comment">Insert the epilog and post-process the buffer.
  1668. </span> (<span class="keyword">with-current-buffer</span> htmlbuf
  1669. (insert <span class="string">"&lt;/pre&gt;"</span>)
  1670. (put places 'content-end (point-marker))
  1671. (insert <span class="string">"\n &lt;/body&gt;"</span>)
  1672. (put places 'body-end (point-marker))
  1673. (insert <span class="string">"\n&lt;/html&gt;\n"</span>)
  1674. (htmlize-defang-local-variables)
  1675. (goto-char (point-min))
  1676. (<span class="keyword">when</span> htmlize-html-major-mode
  1677. <span class="comment-delimiter">;; </span><span class="comment">What sucks about this is that the minor modes, most notably
  1678. </span> <span class="comment-delimiter">;; </span><span class="comment">font-lock-mode, won't be initialized. Oh well.
  1679. </span> (funcall htmlize-html-major-mode))
  1680. (set (make-local-variable 'htmlize-buffer-places)
  1681. (symbol-plist places))
  1682. (run-hooks 'htmlize-after-hook)
  1683. (buffer-enable-undo))
  1684. (setq completed t)
  1685. htmlbuf)
  1686. (<span class="keyword">when</span> (not completed)
  1687. (kill-buffer htmlbuf))
  1688. (htmlize-delete-tmp-overlays)))))
  1689. <span class="comment-delimiter">;; </span><span class="comment">Utility functions.
  1690. </span>
  1691. (<span class="keyword">defmacro</span> <span class="function-name">htmlize-with-fontify-message</span> (<span class="type">&amp;rest</span> body)
  1692. <span class="comment-delimiter">;; </span><span class="comment">When forcing fontification of large buffers in
  1693. </span> <span class="comment-delimiter">;; </span><span class="comment">htmlize-ensure-fontified, inform the user that he is waiting for
  1694. </span> <span class="comment-delimiter">;; </span><span class="comment">font-lock, not for htmlize to finish.
  1695. </span> `(<span class="keyword">progn</span>
  1696. (<span class="keyword">if</span> (&gt; (buffer-size) 65536)
  1697. (message <span class="string">"Forcing fontification of %s..."</span>
  1698. (buffer-name (current-buffer))))
  1699. ,@body
  1700. (<span class="keyword">if</span> (&gt; (buffer-size) 65536)
  1701. (message <span class="string">"Forcing fontification of %s...done"</span>
  1702. (buffer-name (current-buffer))))))
  1703. (<span class="keyword">defun</span> <span class="function-name">htmlize-ensure-fontified</span> ()
  1704. <span class="comment-delimiter">;; </span><span class="comment">If font-lock is being used, ensure that the "support" modes
  1705. </span> <span class="comment-delimiter">;; </span><span class="comment">actually fontify the buffer. If font-lock is not in use, we
  1706. </span> <span class="comment-delimiter">;; </span><span class="comment">don't care because, except in htmlize-file, we don't force
  1707. </span> <span class="comment-delimiter">;; </span><span class="comment">font-lock on the user.
  1708. </span> (<span class="keyword">when</span> (and (boundp 'font-lock-mode)
  1709. font-lock-mode)
  1710. <span class="comment-delimiter">;; </span><span class="comment">In part taken from ps-print-ensure-fontified in GNU Emacs 21.
  1711. </span> (<span class="keyword">cond</span>
  1712. ((and (boundp 'jit-lock-mode)
  1713. (symbol-value 'jit-lock-mode))
  1714. (htmlize-with-fontify-message
  1715. (jit-lock-fontify-now (point-min) (point-max))))
  1716. ((and (boundp 'lazy-lock-mode)
  1717. (symbol-value 'lazy-lock-mode))
  1718. (htmlize-with-fontify-message
  1719. (lazy-lock-fontify-region (point-min) (point-max))))
  1720. ((and (boundp 'lazy-shot-mode)
  1721. (symbol-value 'lazy-shot-mode))
  1722. (htmlize-with-fontify-message
  1723. <span class="comment-delimiter">;; </span><span class="comment">lazy-shot is amazing in that it must *refontify* the region,
  1724. </span> <span class="comment-delimiter">;; </span><span class="comment">even if the whole buffer has already been fontified. &lt;sigh&gt;
  1725. </span> (lazy-shot-fontify-region (point-min) (point-max))))
  1726. <span class="comment-delimiter">;; </span><span class="comment">There's also fast-lock, but we don't need to handle specially,
  1727. </span> <span class="comment-delimiter">;; </span><span class="comment">I think. fast-lock doesn't really defer fontification, it
  1728. </span> <span class="comment-delimiter">;; </span><span class="comment">just saves it to an external cache so it's not done twice.
  1729. </span> )))
  1730. <hr />
  1731. <span class="comment-delimiter">;;;</span><span class="comment">###</span><span class="comment"><span class="warning">autoload</span></span><span class="comment">
  1732. </span>(<span class="keyword">defun</span> <span class="function-name">htmlize-buffer</span> (<span class="type">&amp;optional</span> buffer)
  1733. <span class="doc">"Convert BUFFER to HTML, preserving colors and decorations.
  1734. The generated HTML is available in a new buffer, which is returned.
  1735. When invoked interactively, the new buffer is selected in the current
  1736. window. The title of the generated document will be set to the buffer's
  1737. file name or, if that's not available, to the buffer's name.
  1738. Note that htmlize doesn't fontify your buffers, it only uses the
  1739. decorations that are already present. If you don't set up font-lock or
  1740. something else to fontify your buffers, the resulting HTML will be
  1741. plain. Likewise, if you don't like the choice of colors, fix the mode
  1742. that created them, or simply alter the faces it uses."</span>
  1743. (interactive)
  1744. (<span class="keyword">let</span> ((htmlbuf (<span class="keyword">with-current-buffer</span> (or buffer (current-buffer))
  1745. (htmlize-buffer-1))))
  1746. (<span class="keyword">when</span> (interactive-p)
  1747. (switch-to-buffer htmlbuf))
  1748. htmlbuf))
  1749. <span class="comment-delimiter">;;;</span><span class="comment">###</span><span class="comment"><span class="warning">autoload</span></span><span class="comment">
  1750. </span>(<span class="keyword">defun</span> <span class="function-name">htmlize-region</span> (beg end)
  1751. <span class="doc">"Convert the region to HTML, preserving colors and decorations.
  1752. See `</span><span class="doc"><span class="constant">htmlize-buffer</span></span><span class="doc">' for details."</span>
  1753. (interactive <span class="string">"r"</span>)
  1754. <span class="comment-delimiter">;; </span><span class="comment">Don't let zmacs region highlighting end up in HTML.
  1755. </span> (<span class="keyword">when</span> (fboundp 'zmacs-deactivate-region)
  1756. (zmacs-deactivate-region))
  1757. (<span class="keyword">let</span> ((htmlbuf (<span class="keyword">save-restriction</span>
  1758. (narrow-to-region beg end)
  1759. (htmlize-buffer-1))))
  1760. (<span class="keyword">when</span> (interactive-p)
  1761. (switch-to-buffer htmlbuf))
  1762. htmlbuf))
  1763. (<span class="keyword">defun</span> <span class="function-name">htmlize-region-for-paste</span> (beg end)
  1764. <span class="doc">"Htmlize the region and return just the HTML as a string.
  1765. This forces the `</span><span class="doc"><span class="constant">inline-css</span></span><span class="doc">' style and only returns the HTML body,
  1766. but without the BODY tag. This should make it useful for inserting
  1767. the text to another HTML buffer."</span>
  1768. (<span class="keyword">let*</span> ((htmlize-output-type 'inline-css)
  1769. (htmlbuf (htmlize-region beg end)))
  1770. (<span class="keyword">unwind-protect</span>
  1771. (<span class="keyword">with-current-buffer</span> htmlbuf
  1772. (buffer-substring (plist-get htmlize-buffer-places 'content-start)
  1773. (plist-get htmlize-buffer-places 'content-end)))
  1774. (kill-buffer htmlbuf))))
  1775. (<span class="keyword">defun</span> <span class="function-name">htmlize-make-file-name</span> (file)
  1776. <span class="doc">"Make an HTML file name from FILE.
  1777. In its default implementation, this simply appends `</span><span class="doc"><span class="constant">.html</span></span><span class="doc">' to FILE.
  1778. This function is called by htmlize to create the buffer file name, and
  1779. by `</span><span class="doc"><span class="constant">htmlize-file</span></span><span class="doc">' to create the target file name.
  1780. More elaborate transformations are conceivable, such as changing FILE's
  1781. extension to `</span><span class="doc"><span class="constant">.html</span></span><span class="doc">' (\"file.c\" -&gt; \"file.html\"). If you want them,
  1782. overload this function to do it and htmlize will comply."</span>
  1783. (concat file <span class="string">".html"</span>))
  1784. <span class="comment-delimiter">;; </span><span class="comment">Older implementation of htmlize-make-file-name that changes FILE's
  1785. </span><span class="comment-delimiter">;; </span><span class="comment">extension to ".html".
  1786. </span><span class="comment-delimiter">;</span><span class="comment">(defun htmlize-make-file-name (file)
  1787. </span><span class="comment-delimiter">; </span><span class="comment">(let ((extension (file-name-extension file))
  1788. </span><span class="comment-delimiter">;</span><span class="comment"> (sans-extension (file-name-sans-extension file)))
  1789. </span><span class="comment-delimiter">; </span><span class="comment">(if (or (equal extension "html")
  1790. </span><span class="comment-delimiter">;</span><span class="comment"> (equal extension "htm")
  1791. </span><span class="comment-delimiter">;</span><span class="comment"> (equal sans-extension ""))
  1792. </span><span class="comment-delimiter">;</span><span class="comment"> (concat file ".html")
  1793. </span><span class="comment-delimiter">; </span><span class="comment">(concat sans-extension ".html"))))
  1794. </span>
  1795. <span class="comment-delimiter">;;;</span><span class="comment">###</span><span class="comment"><span class="warning">autoload</span></span><span class="comment">
  1796. </span>(<span class="keyword">defun</span> <span class="function-name">htmlize-file</span> (file <span class="type">&amp;optional</span> target)
  1797. <span class="doc">"Load FILE, fontify it, convert it to HTML, and save the result.
  1798. Contents of FILE are inserted into a temporary buffer, whose major mode
  1799. is set with `</span><span class="doc"><span class="constant">normal-mode</span></span><span class="doc">' as appropriate for the file type. The buffer
  1800. is subsequently fontified with `</span><span class="doc"><span class="constant">font-lock</span></span><span class="doc">' and converted to HTML. Note
  1801. that, unlike `</span><span class="doc"><span class="constant">htmlize-buffer</span></span><span class="doc">', this function explicitly turns on
  1802. font-lock. If a form of highlighting other than font-lock is desired,
  1803. please use `</span><span class="doc"><span class="constant">htmlize-buffer</span></span><span class="doc">' directly on buffers so highlighted.
  1804. Buffers currently visiting FILE are unaffected by this function. The
  1805. function does not change current buffer or move the point.
  1806. If TARGET is specified and names a directory, the resulting file will be
  1807. saved there instead of to FILE's directory. If TARGET is specified and
  1808. does not name a directory, it will be used as output file name."</span>
  1809. (interactive (list (read-file-name
  1810. <span class="string">"HTML-ize file: "</span>
  1811. nil nil nil (and (buffer-file-name)
  1812. (file-name-nondirectory
  1813. (buffer-file-name))))))
  1814. (<span class="keyword">let</span> ((output-file (<span class="keyword">if</span> (and target (not (file-directory-p target)))
  1815. target
  1816. (expand-file-name
  1817. (htmlize-make-file-name (file-name-nondirectory file))
  1818. (or target (file-name-directory file)))))
  1819. <span class="comment-delimiter">;; </span><span class="comment">Try to prevent `</span><span class="comment"><span class="constant">find-file-noselect</span></span><span class="comment">' from triggering
  1820. </span> <span class="comment-delimiter">;; </span><span class="comment">font-lock because we'll fontify explicitly below.
  1821. </span> (font-lock-mode nil)
  1822. (font-lock-auto-fontify nil)
  1823. (global-font-lock-mode nil)
  1824. <span class="comment-delimiter">;; </span><span class="comment">Ignore the size limit for the purposes of htmlization.
  1825. </span> (font-lock-maximum-size nil)
  1826. <span class="comment-delimiter">;; </span><span class="comment">Disable font-lock support modes. This will only work in
  1827. </span> <span class="comment-delimiter">;; </span><span class="comment">more recent Emacs versions, so htmlize-buffer-1 still needs
  1828. </span> <span class="comment-delimiter">;; </span><span class="comment">to call htmlize-ensure-fontified.
  1829. </span> (font-lock-support-mode nil))
  1830. (<span class="keyword">with-temp-buffer</span>
  1831. <span class="comment-delimiter">;; </span><span class="comment">Insert FILE into the temporary buffer.
  1832. </span> (insert-file-contents file)
  1833. <span class="comment-delimiter">;; </span><span class="comment">Set the file name so normal-mode and htmlize-buffer-1 pick it
  1834. </span> <span class="comment-delimiter">;; </span><span class="comment">up. Restore it afterwards so with-temp-buffer's kill-buffer
  1835. </span> <span class="comment-delimiter">;; </span><span class="comment">doesn't complain about killing a modified buffer.
  1836. </span> (<span class="keyword">let</span> ((buffer-file-name file))
  1837. <span class="comment-delimiter">;; </span><span class="comment">Set the major mode for the sake of font-lock.
  1838. </span> (normal-mode)
  1839. (font-lock-mode 1)
  1840. (<span class="keyword">unless</span> font-lock-mode
  1841. <span class="comment-delimiter">;; </span><span class="comment">In GNU Emacs (font-lock-mode 1) doesn't force font-lock,
  1842. </span> <span class="comment-delimiter">;; </span><span class="comment">contrary to the documentation. This seems to work.
  1843. </span> (font-lock-fontify-buffer))
  1844. <span class="comment-delimiter">;; </span><span class="comment">htmlize the buffer and save the HTML.
  1845. </span> (<span class="keyword">with-current-buffer</span> (htmlize-buffer-1)
  1846. (<span class="keyword">unwind-protect</span>
  1847. (<span class="keyword">progn</span>
  1848. (run-hooks 'htmlize-file-hook)
  1849. (write-region (point-min) (point-max) output-file))
  1850. (kill-buffer (current-buffer)))))))
  1851. <span class="comment-delimiter">;; </span><span class="comment">I haven't decided on a useful return value yet, so just return
  1852. </span> <span class="comment-delimiter">;; </span><span class="comment">nil.
  1853. </span> nil)
  1854. <span class="comment-delimiter">;;;</span><span class="comment">###</span><span class="comment"><span class="warning">autoload</span></span><span class="comment">
  1855. </span>(<span class="keyword">defun</span> <span class="function-name">htmlize-many-files</span> (files <span class="type">&amp;optional</span> target-directory)
  1856. <span class="doc">"Convert FILES to HTML and save the corresponding HTML versions.
  1857. FILES should be a list of file names to convert. This function calls
  1858. `</span><span class="doc"><span class="constant">htmlize-file</span></span><span class="doc">' on each file; see that function for details. When
  1859. invoked interactively, you are prompted for a list of files to convert,
  1860. terminated with RET.
  1861. If TARGET-DIRECTORY is specified, the HTML files will be saved to that
  1862. directory. Normally, each HTML file is saved to the directory of the
  1863. corresponding source file."</span>
  1864. (interactive
  1865. (list
  1866. (<span class="keyword">let</span> (list file)
  1867. <span class="comment-delimiter">;; </span><span class="comment">Use empty string as DEFAULT because setting DEFAULT to nil
  1868. </span> <span class="comment-delimiter">;; </span><span class="comment">defaults to the directory name, which is not what we want.
  1869. </span> (<span class="keyword">while</span> (not (equal (setq file (read-file-name
  1870. <span class="string">"HTML-ize file (RET to finish): "</span>
  1871. (and list (file-name-directory
  1872. (car list)))
  1873. <span class="string">""</span> t))
  1874. <span class="string">""</span>))
  1875. (push file list))
  1876. (nreverse list))))
  1877. <span class="comment-delimiter">;; </span><span class="comment">Verify that TARGET-DIRECTORY is indeed a directory. If it's a
  1878. </span> <span class="comment-delimiter">;; </span><span class="comment">file, htmlize-file will use it as target, and that doesn't make
  1879. </span> <span class="comment-delimiter">;; </span><span class="comment">sense.
  1880. </span> (and target-directory
  1881. (not (file-directory-p target-directory))
  1882. (<span class="warning">error</span> <span class="string">"target-directory must name a directory: %s"</span> target-directory))
  1883. (<span class="keyword">dolist</span> (file files)
  1884. (htmlize-file file target-directory)))
  1885. <span class="comment-delimiter">;;;</span><span class="comment">###</span><span class="comment"><span class="warning">autoload</span></span><span class="comment">
  1886. </span>(<span class="keyword">defun</span> <span class="function-name">htmlize-many-files-dired</span> (arg <span class="type">&amp;optional</span> target-directory)
  1887. <span class="doc">"HTMLize dired-marked files."</span>
  1888. (interactive <span class="string">"P"</span>)
  1889. (htmlize-many-files (dired-get-marked-files nil arg) target-directory))
  1890. (<span class="keyword">provide</span> '<span class="constant">htmlize</span>)
  1891. <span class="comment-delimiter">;; </span><span class="comment">Local Variables&#58;
  1892. </span><span class="comment-delimiter">;; </span><span class="comment">byte-compile-warnings: (not cl-functions lexical unresolved obsolete)
  1893. </span><span class="comment-delimiter">;; </span><span class="comment">lexical-binding: t
  1894. </span><span class="comment-delimiter">;; </span><span class="comment">End:
  1895. </span>
  1896. <span class="comment-delimiter">;;; </span><span class="comment">htmlize.el ends here
  1897. </span></pre>
  1898. </body>
  1899. </html>