Merge from trunk.
[emacs.git] / lisp / htmlfontify.el
blobc8e9558151061bc785b707de609903b2015cdee8
1 ;;; htmlfontify.el --- htmlise a buffer/source tree with optional hyperlinks
3 ;; Copyright (C) 2002-2003, 2009-2011 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Package: htmlfontify
7 ;; Filename: htmlfontify.el
8 ;; Version: 0.21
9 ;; Keywords: html, hypermedia, markup, etags
10 ;; Author: Vivek Dasmohapatra <vivek@etla.org>
11 ;; Maintainer: Vivek Dasmohapatra <vivek@etla.org>
12 ;; Created: 2002-01-05
13 ;; Description: htmlise a buffer/source tree with optional hyperlinks
14 ;; URL: http://rtfm.etla.org/emacs/htmlfontify/
15 ;; Compatibility: Emacs23, Emacs22
16 ;; Incompatibility: Emacs19, Emacs20, Emacs21
17 ;; Last Updated: Thu 2009-11-19 01:31:21 +0000
18 ;; Version: 0.21
20 ;; This file is part of GNU Emacs.
22 ;; GNU Emacs is free software: you can redistribute it and/or modify
23 ;; it under the terms of the GNU General Public License as published by
24 ;; the Free Software Foundation, either version 3 of the License, or
25 ;; (at your option) any later version.
27 ;; GNU Emacs is distributed in the hope that it will be useful,
28 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
29 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 ;; GNU General Public License for more details.
32 ;; You should have received a copy of the GNU General Public License
33 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
35 ;;; Commentary:
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;; I have made some changes to make it work for Emacs 22. A lot of
38 ;; small bug fixes related to the format of text and overlay
39 ;; properties (which might have changed since the beginning of 2003
40 ;; when this file was originally written).
42 ;; The function `hfy-face-at' currently carries much of the burden of
43 ;; my lacking understanding of the formats mentioned above and should
44 ;; need some knowledgeable help.
46 ;; Another thing that maybe could be fixed is that overlay background
47 ;; colors which are now only seen where there is text (in the XHTML
48 ;; output). A bit of CSS tweaking is necessary there.
50 ;; The face 'default has a value :background "SystemWindow" for the
51 ;; background color. There is no explicit notion that this should be
52 ;; considered transparent, but I have assumed that it could be handled
53 ;; like if it was here. (I am unsure that background and foreground
54 ;; priorities are handled ok, but it looks ok in my tests now.)
56 ;; 2007-12-27 Lennart Borgman
57 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
59 ;; Here's some elisp code to html-pretty-print an Emacs buffer, preserving
60 ;; the Emacs syntax/whatever highlighting. It also knows how to drive etags
61 ;; (exuberant-ctags or Emacs etags) and hyperlink the code according
62 ;; to its (etags') output.
64 ;; NOTE: Currently the hyperlinking code only knows how to drive GNU find
65 ;; and the exuberant and GNU variants of etags : I do not know of any other
66 ;; etags variants, but mechanisms have been provided to allow htmlfontify
67 ;; to be taught how to drive them. As long as your version of find has
68 ;; the -path test and is reasonably sane, you should be fine.
70 ;; A sample of the htmlfontified / hyperlinked output of this module can be
71 ;; found at http://rtfm.etla.org/sql/dbishell/src/ - it's not perfect, but
72 ;; it's a hell of a lot faster and more thorough than I could hope to be
73 ;; doing this by hand.
75 ;; some user / horrified onlooker comments:
76 ;; What? No! There's something deeply wrong here... (R. Shufflebotham)
77 ;; You're a freak. (D. Silverstone)
78 ;; Aren't we giving you enough to do? (J. Busuttil)
79 ;; You're almost as messed up as Lexx is! (N. Graves-Morris)
81 ;;; History:
82 ;; Changes: moved to changelog (CHANGES) file.
84 ;;; Code:
85 (eval-when-compile (require 'cl))
86 (require 'faces)
87 ;; (`facep' `face-attr-construct' `x-color-values' `color-values' `face-name')
88 (require 'custom)
89 ;; (`defgroup' `defcustom')
90 (require 'font-lock)
91 ;; (`font-lock-fontify-region')
92 (require 'cus-edit)
94 (defconst htmlfontify-version 0.21)
96 (defconst hfy-meta-tags
97 (format "<meta name=\"generator\" content=\"emacs %s; htmlfontify %0.2f\" />"
98 emacs-version htmlfontify-version)
99 "The generator meta tag for this version of htmlfontify.")
101 (defconst htmlfontify-manual "Htmlfontify Manual"
102 "Copy and convert buffers and files to HTML, adding hyperlinks between files
103 \(driven by etags) if requested.
104 \nInteractive functions:
105 `htmlfontify-buffer'
106 `htmlfontify-run-etags'
107 `htmlfontify-copy-and-link-dir'
108 `htmlfontify-load-rgb-file'
109 `htmlfontify-unload-rgb-file'\n
110 In order to:\n
111 fontify a file you have open: \\[htmlfontify-buffer]
112 prepare the etags map for a directory: \\[htmlfontify-run-etags]
113 copy a directory, fontifying as you go: \\[htmlfontify-copy-and-link-dir]\n
114 The following might be useful when running non-windowed or in batch mode:
115 \(note that they shouldn't be necessary - we have a built in map)\n
116 load an X11 style rgb.txt file: \\[htmlfontify-load-rgb-file]
117 unload the current rgb.txt file: \\[htmlfontify-unload-rgb-file]\n
118 And here's a programmatic example:\n
119 \(defun rtfm-build-page-header (file style)
120 (format \"#define TEMPLATE red+black.html
121 #define DEBUG 1
122 #include <build/menu-dirlist|>\\n
123 html-css-url := /css/red+black.css
124 title := rtfm.etla.org ( %s / src/%s )
125 bodytag :=
126 head <=STYLESHEET;\\n
128 STYLESHEET
129 main-title := rtfm / %s / src/%s\\n
130 main-content <=MAIN_CONTENT;\\n\" rtfm-section file style rtfm-section file))
132 \(defun rtfm-build-page-footer (file) \"\\nMAIN_CONTENT\\n\")
134 \(defun rtfm-build-source-docs (section srcdir destdir)
135 (interactive
136 \"s section[eg- emacs / p4-blame]:\\nD source-dir: \\nD output-dir: \")
137 (require 'htmlfontify)
138 (hfy-load-tags-cache srcdir)
139 (let ((hfy-page-header 'rtfm-build-page-header)
140 (hfy-page-footer 'rtfm-build-page-footer)
141 (rtfm-section section)
142 (hfy-index-file \"index\"))
143 (htmlfontify-run-etags srcdir)
144 (htmlfontify-copy-and-link-dir srcdir destdir \".src\" \".html\")))")
146 (defgroup htmlfontify nil
147 "Convert buffers and files to HTML."
148 :group 'applications
149 :link '(variable-link htmlfontify-manual)
150 :prefix "hfy-")
152 (defcustom hfy-page-header 'hfy-default-header
153 "Function called to build the header of the html source.
154 This is called with two arguments (the filename relative to the top
155 level source directory being etag'd and fontified), and a string containing
156 the <style>...</style> text to embed in the document.
157 It should return the string returned will be used as the header for the
158 htmlfontified version of the source file.\n
159 See also `hfy-page-footer'."
160 :group 'htmlfontify
161 ;; FIXME: Why place such a :tag everywhere? Isn't it imposing your
162 ;; own Custom preference on your users? --Stef
163 :tag "page-header"
164 :type '(function))
166 (defcustom hfy-split-index nil
167 "Whether or not to split the index `hfy-index-file' alphabetically.
168 If non-nil, the index is split on the first letter of each tag.
169 Useful when the index would otherwise
170 be large and take a long time to render or be difficult to navigate."
171 :group 'htmlfontify
172 :tag "split-index"
173 :type '(boolean))
175 (defcustom hfy-page-footer 'hfy-default-footer
176 "As `hfy-page-header', but generates the output footer.
177 It takes only one argument, the filename."
178 :group 'htmlfontify
179 :tag "page-footer"
180 :type '(function))
182 (defcustom hfy-extn ".html"
183 "File extension used for output files."
184 :group 'htmlfontify
185 :tag "extension"
186 :type '(string))
188 (defcustom hfy-src-doc-link-style "text-decoration: underline;"
189 "String to add to the '<style> a' variant of an htmlfontify CSS class."
190 :group 'htmlfontify
191 :tag "src-doc-link-style"
192 :type '(string))
194 (defcustom hfy-src-doc-link-unstyle " text-decoration: none;"
195 "Regex to remove from the <style> a variant of an htmlfontify CSS class."
196 :group 'htmlfontify
197 :tag "src-doc-link-unstyle"
198 :type '(string))
200 (defcustom hfy-link-extn nil
201 "File extension used for href links.
202 Useful where the htmlfontify output files are going to be processed
203 again, with a resulting change in file extension. If nil, then any
204 code using this should fall back to `hfy-extn'."
205 :group 'htmlfontify
206 :tag "link-extension"
207 :type '(choice string (const nil)))
209 (defcustom hfy-link-style-fun 'hfy-link-style-string
210 "Function to customize the appearance of hyperlinks.
211 Set this to a function, which will be called with one argument
212 \(a \"{ foo: bar; ...}\" CSS style-string) - it should return a copy of
213 its argument, altered so as to make any changes you want made for text which
214 is a hyperlink, in addition to being in the class to which that style would
215 normally be applied."
216 :group 'htmlfontify
217 :tag "link-style-function"
218 :type '(function))
220 (defcustom hfy-index-file "hfy-index"
221 "Name (sans extension) of the tag definition index file produced during
222 fontification-and-hyperlinking."
223 :group 'htmlfontify
224 :tag "index-file"
225 :type '(string))
227 (defcustom hfy-instance-file "hfy-instance"
228 "Name (sans extension) of the tag usage index file produced during
229 fontification-and-hyperlinking."
230 :group 'htmlfontify
231 :tag "instance-file"
232 :type '(string))
234 (defcustom hfy-html-quote-regex "\\([<\"&>]\\)"
235 "Regex to match (with a single back-reference per match) strings in HTML
236 which should be quoted with `hfy-html-quote' (and `hfy-html-quote-map')
237 to make them safe."
238 :group 'htmlfontify
239 :tag "html-quote-regex"
240 :type '(regexp))
242 (define-obsolete-variable-alias 'hfy-init-kludge-hooks 'hfy-init-kludge-hook
243 "23.2")
244 (defcustom hfy-init-kludge-hook '(hfy-kludge-cperl-mode)
245 "List of functions to call when starting `htmlfontify-buffer' to do any
246 kludging necessary to get highlighting modes to behave as you want, even
247 when not running under a window system."
248 :group 'htmlfontify
249 :tag "init-kludge-hooks"
250 :type '(hook))
252 (defcustom hfy-post-html-hooks nil
253 "List of functions to call after creating and filling the HTML buffer.
254 These functions will be called with the html buffer as the current buffer."
255 :group 'htmlfontify
256 :tag "post-html-hooks"
257 :options '(set-auto-mode)
258 :type '(hook))
260 (defcustom hfy-default-face-def nil
261 "Fallback `defface' specification for the face 'default, used when
262 `hfy-display-class' has been set (the normal htmlfontify way of extracting
263 potentially non-current face information doesn't necessarily work for
264 'default).\n
265 Example: I customize this to:\n
266 \((t :background \"black\" :foreground \"white\" :family \"misc-fixed\"))"
267 :group 'htmlfontify
268 :tag "default-face-definition"
269 :type '(alist))
271 (defcustom hfy-etag-regex (concat ".*"
272 "\x7f" "\\([^\x01\n]+\\)"
273 "\x01" "\\([0-9]+\\)"
274 "," "\\([0-9]+\\)$"
275 "\\|" ".*\x7f[0-9]+,[0-9]+$")
276 "Regex used to parse an etags entry: must have 3 subexps, corresponding,
277 in order, to:\n
278 1 - The tag
279 2 - The line
280 3 - The char (point) at which the tag occurs."
281 :group 'htmlfontify
282 :tag "etag-regex"
283 :type '(regexp))
285 (defcustom hfy-html-quote-map '(("\"" "&quot;")
286 ("<" "&lt;" )
287 ("&" "&amp;" )
288 (">" "&gt;" ))
289 "Alist of char -> entity mappings used to make the text HTML-safe."
290 :group 'htmlfontify
291 :tag "html-quote-map"
292 :type '(alist :key-type (string)))
293 (defconst hfy-e2x-etags-cmd "for src in `find . -type f`;
295 ETAGS=%s;
296 case ${src} in
297 *.ad[absm]|*.[CFHMSacfhlmpsty]|*.def|*.in[cs]|*.s[as]|*.src|*.cc|\\
298 *.hh|*.[chy]++|*.[ch]pp|*.[chy]xx|*.pdb|*.[ch]s|*.[Cc][Oo][Bb]|\\
299 *.[eh]rl|*.f90|*.for|*.java|*.[cem]l|*.clisp|*.lisp|*.[Ll][Ss][Pp]|\\
300 [Mm]akefile*|*.pas|*.[Pp][LlMm]|*.psw|*.lm|*.pc|*.prolog|*.oak|\\
301 *.p[sy]|*.sch|*.scheme|*.[Ss][Cc][Mm]|*.[Ss][Mm]|*.bib|*.cl[os]|\\
302 *.ltx|*.sty|*.TeX|*.tex|*.texi|*.texinfo|*.txi|*.x[bp]m|*.yy|\\
303 *.[Ss][Qq][Ll])
304 ${ETAGS} -o- ${src};
307 FTYPE=`file ${src}`;
308 case ${FTYPE} in
309 *script*text*)
310 ${ETAGS} -o- ${src};
312 *text*)
313 SHEBANG=`head -n1 ${src} | grep '#!' -c`;
314 if [ ${SHEBANG} -eq 1 ];
315 then
316 ${ETAGS} -o- ${src};
319 esac;
321 esac;
322 done;")
324 (defconst hfy-etags-cmd-alist-default
325 `(("emacs etags" . ,hfy-e2x-etags-cmd)
326 ("exuberant ctags" . "%s -R -f -" )))
328 (defcustom hfy-etags-cmd-alist
329 hfy-etags-cmd-alist-default
330 "Alist of possible shell commands that will generate etags output that
331 `htmlfontify' can use. '%s' will be replaced by `hfy-etags-bin'."
332 :group 'htmlfontify
333 :tag "etags-cmd-alist"
334 :type '(alist :key-type (string) :value-type (string)))
336 (defcustom hfy-etags-bin "etags"
337 "Location of etags binary (we begin by assuming it's in your path).\n
338 Note that if etags is not in your path, you will need to alter the shell
339 commands in `hfy-etags-cmd-alist'."
340 :group 'htmlfontify
341 :tag "etags-bin"
342 :type '(file))
344 (defcustom hfy-shell-file-name "/bin/sh"
345 "Shell (bourne or compatible) to invoke for complex shell operations."
346 :group 'htmlfontify
347 :tag "shell-file-name"
348 :type '(file))
350 (defcustom hfy-ignored-properties '(read-only
351 intangible
352 modification-hooks
353 insert-in-front-hooks
354 insert-behind-hooks
355 point-entered
356 point-left)
357 "Properties to omit when copying a fontified buffer for HTML transformation."
358 :group 'htmlfontify
359 :tag "ignored-properties"
360 :type '(repeat symbol))
362 (defun hfy-which-etags ()
363 "Return a string indicating which flavour of etags we are using."
364 (let ((v (shell-command-to-string (concat hfy-etags-bin " --version"))))
365 (cond ((string-match "exube" v) "exuberant ctags")
366 ((string-match "GNU E" v) "emacs etags" )) ))
368 (defcustom hfy-etags-cmd
369 ;; We used to wrap this in a `eval-and-compile', but:
370 ;; - it had no effect because this expression was not seen by the
371 ;; byte-compiler (defcustom used to quote this argument).
372 ;; - it signals an error (`hfy-which-etags' is not defined at compile-time).
373 ;; - we want this auto-detection to reflect the system on which Emacs is run
374 ;; rather than the one on which it's compiled.
375 (cdr (assoc (hfy-which-etags) hfy-etags-cmd-alist))
376 "The etags equivalent command to run in a source directory to generate a tags
377 file for the whole source tree from there on down. The command should emit
378 the etags output on stdout.\n
379 Two canned commands are provided - they drive Emacs' etags and
380 exuberant-ctags' etags respectively."
381 :group 'htmlfontify
382 :tag "etags-command"
383 :type (let ((clist (list '(string))))
384 (dolist (C hfy-etags-cmd-alist)
385 (push (list 'const :tag (car C) (cdr C)) clist))
386 (cons 'choice clist)))
388 (defcustom hfy-istext-command "file %s | sed -e 's@^[^:]*:[ \t]*@@'"
389 "Command to run with the name of a file, to see whether it is a text file
390 or not. The command should emit a string containing the word 'text' if
391 the file is a text file, and a string not containing 'text' otherwise."
392 :group 'htmlfontify
393 :tag "istext-command"
394 :type '(string))
396 (defcustom hfy-find-cmd
397 "find . -type f \\! -name \\*~ \\! -name \\*.flc \\! -path \\*/CVS/\\*"
398 "Find command used to harvest a list of files to attempt to fontify."
399 :group 'htmlfontify
400 :tag "find-command"
401 :type '(string))
403 (defcustom hfy-display-class nil
404 "Display class to use to determine which display class to use when
405 calculating a face's attributes. This is useful when, for example, you
406 are running Emacs on a tty or in batch mode, and want htmlfontify to have
407 access to the face spec you would use if you were connected to an X display.\n
408 Some valid class specification elements are:\n
409 '(class color)
410 '(class grayscale)
411 '(background dark)
412 '(background light)
413 '(type x-toolkit)
414 '(type tty)
415 '(type motif)
416 '(type lucid)
417 Multiple values for a tag may be combined, to indicate that any one or more
418 of these values in the specification key constitutes a match, eg:\n
419 '((class color grayscale) (type tty)) would match any of:\n
420 '((class color))
421 '((class grayscale))
422 '((class color grayscale))
423 '((class color foo))
424 '((type tty))
425 '((type tty) (class color))\n
426 and so on."
427 :type '(alist :key-type (symbol) :value-type (symbol))
428 :group 'htmlfontify
429 :tag "display-class"
430 :options '((type (choice (const :tag "X11" x-toolkit)
431 (const :tag "Terminal" tty )
432 (const :tag "Lucid Toolkit" lucid )
433 (const :tag "Motif Toolkit" motif )))
435 (class (choice (const :tag "Colour" color )
436 (const :tag "Greyscale" grayscale)))
438 (background (choice (const :tag "Dark" dark )
439 (const :tag "Bright" light ))) ))
441 (defcustom hfy-optimisations (list 'keep-overlays)
442 "Optimizations to turn on: So far, the following have been implemented:\n
443 merge-adjacent-tags: If two (or more) span tags are adjacent, identical and
444 separated by nothing more than whitespace, they will
445 be merged into one span.
446 zap-comment-links : Suppress hyperlinking of tags found in comments.
447 zap-string-links : Suppress hyperlinking of tags found in strings.
448 div-wrapper : Add <div class=\"default\"> </div> tags around the
449 output.
450 keep-overlays : More of a bell (or possibly whistle) than an
451 optimization - If on, preserve overlay highlighting
452 (cf ediff or goo-font-lock) as well as basic faces.\n
453 And the following are planned but not yet available:\n
454 kill-context-leak : Suppress hyperlinking between files highlighted by
455 different modes.\n
456 Note: like compiler optimizations, these optimize the _output_ of the code,
457 not the processing of the source itself, and are therefore likely to slow
458 htmlfontify down, at least a little. Except for skip-refontification,
459 which can never slow you down, but may result in incomplete fontification."
460 :type '(set (const :tag "merge-adjacent-tags" merge-adjacent-tags )
461 (const :tag "zap-comment-links" zap-comment-links )
462 (const :tag "zap-string-links" zap-string-links )
463 (const :tag "skip-refontification" skip-refontification)
464 (const :tag "kill-context-leak" kill-context-leak )
465 (const :tag "div-wrapper" div-wrapper )
466 (const :tag "keep-overlays" keep-overlays ))
467 :group 'htmlfontify
468 :tag "optimizations")
470 (defvar hfy-tags-cache nil
471 "Alist of the form:\n
472 \((\"/src/dir/0\" . tag-hash0) (\"/src/dir/1\" tag-hash1) ...)\n
473 Each tag hash entry then contains entries of the form:\n
474 \"tag_string\" => ((\"file/name.ext\" line char) ... )\n
475 ie an alist mapping (relative) file paths to line and character offsets.\n
476 See also `hfy-load-tags-cache'.")
478 (defvar hfy-tags-sortl nil
479 "Alist of the form ((\"/src/dir\" . (tag0 tag1 tag2)) ... )\n
480 where the tags are stored in descending order of length.\n
481 See also `hfy-load-tags-cache'.")
483 (defvar hfy-tags-rmap nil
484 "Alist of the form ((\"/src/dir\" . tag-rmap-hash))\n
485 where tag-rmap-hash has entries of the form:
486 \"tag_string\" => ( \"file/name.ext\" line char )
487 Unlike `hfy-tags-cache' these are the locations of occurrences of
488 tagged items, not the locations of their definitions.")
490 (defvar hfy-style-assoc 'please-ignore-this-line
491 "An assoc representing/describing an Emacs face.
492 Properties may be repeated, in which case later properties should be
493 treated as if they were inherited from a 'parent' font.
494 \(For some properties, only the first encountered value is of any importance,
495 for others the values might be cumulative, and for others they might be
496 cumulative in a complex way.)\n
497 Some examples:\n
498 \(hfy-face-to-style 'default) =>
499 ((\"background\" . \"rgb(0, 0, 0)\")
500 (\"color\" . \"rgb(255, 255, 255)\")
501 (\"font-style\" . \"normal\")
502 (\"font-weight\" . \"500\")
503 (\"font-stretch\" . \"normal\")
504 (\"font-family\" . \"misc-fixed\")
505 (\"font-size\" . \"13pt\")
506 (\"text-decoration\" . \"none\"))\n
507 \(hfy-face-to-style 'Info-title-3-face) =>
508 ((\"font-weight\" . \"700\")
509 (\"font-family\" . \"helv\")
510 (\"font-size\" . \"120%\")
511 (\"text-decoration\" . \"none\"))\n")
513 (defvar hfy-sheet-assoc 'please-ignore-this-line
514 "An assoc with elements of the form (face-name style-name . style-string):\n
515 '((default \"default\" . \"{background: black; color: white}\")
516 (font-lock-string-face \"string\" . \"{color: rgb(64,224,208)}\"))" )
518 (defvar hfy-facemap-assoc 'please-ignore-this-line
519 "An assoc of (point . FACE-SYMBOL) or (point . DEFFACE-LIST)
520 and (point . 'end) elements, in descending order of point value
521 \(ie from the file's end to its beginning).\n
522 The map is in reverse order because inserting a <style> tag (or any other
523 string) at `point' invalidates the map for all entries with a greater value of
524 point. By traversing the map from greatest to least point, we still invalidate
525 the map as we go, but only those points we have already dealt with (and
526 therefore no longer care about) will be invalid at any time.\n
527 '((64820 . end)
528 (64744 . font-lock-comment-face)
529 (64736 . end)
530 (64722 . font-lock-string-face)
531 (64630 . end)
532 (64623 . font-lock-string-face)
533 (64449 . end)
534 (64446 . font-lock-keyword-face)
535 (64406 . end)
536 (64395 . font-lock-constant-face)
537 (64393 . end)
538 (64386 . font-lock-keyword-face)
539 (64379 . end)
540 ;; big similar section elided. You get the idea.
541 (4285 . font-lock-constant-face)
542 (4285 . end)
543 (4221 . font-lock-comment-face)
544 (4221 . end)
545 (4197 . font-lock-constant-face)
546 (4197 . end)
547 (1 . font-lock-comment-face))")
549 (defvar hfy-tmpfont-stack nil
550 "An alist of derived fonts resulting from overlays.")
552 (defconst hfy-hex-regex "[0-9A-Fa-f]")
554 (defconst hfy-triplet-regex
555 (concat
556 "\\(" hfy-hex-regex hfy-hex-regex "\\)"
557 "\\(" hfy-hex-regex hfy-hex-regex "\\)"
558 "\\(" hfy-hex-regex hfy-hex-regex "\\)"))
560 (defun hfy-interq (set-a set-b)
561 "Return the intersection (using `eq') of two lists SET-A and SET-B."
562 (let ((sa set-a) (interq nil) (elt nil))
563 (while sa
564 (setq elt (car sa)
565 sa (cdr sa))
566 (if (memq elt set-b) (setq interq (cons elt interq))))
567 interq))
569 (defun hfy-colour-vals (colour)
570 "Where COLOUR is a color name or #XXXXXX style triplet, return a
571 list of three (16 bit) rgb values for said color.\n
572 If a window system is unavailable, calls `hfy-fallback-colour-values'."
573 (if (string-match hfy-triplet-regex colour)
574 (mapcar
575 (lambda (x) (* (string-to-number (match-string x colour) 16) 257))
576 '(1 2 3))
577 ;;(message ">> %s" colour)
578 (if window-system
579 (if (fboundp 'color-values)
580 (color-values colour)
581 ;;(message "[%S]" window-system)
582 (x-color-values colour))
583 ;; blarg - tty colours are no good - go fetch some X colours:
584 (hfy-fallback-colour-values colour))))
586 (defvar hfy-cperl-mode-kludged-p nil)
588 (defun hfy-kludge-cperl-mode ()
589 "CPerl mode does its damndest not to do some of its fontification when not
590 in a windowing system - try to trick it..."
591 (if (not hfy-cperl-mode-kludged-p)
592 (progn (if (not window-system)
593 (let ((window-system 'htmlfontify))
594 (eval-and-compile (require 'cperl-mode))
595 (setq cperl-syntaxify-by-font-lock t)))
596 (setq hfy-cperl-mode-kludged-p t))) )
598 (defun hfy-opt (symbol) "Is option SYMBOL set."
599 (memq symbol hfy-optimisations))
601 (defun hfy-default-header (file style)
602 "Default value for `hfy-page-header'.
603 FILE is the name of the file.
604 STYLE is the inline CSS stylesheet (or tag referring to an external sheet)."
605 ;; (format "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
606 ;; <html>\n <head>\n <title>%s</title>\n %s\n </head>\n <body>\n" file style))
607 (format "<?xml version=\"1.0\" encoding=\"utf-8\"?>
608 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"
609 \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">
610 <html xmlns=\"http://www.w3.org/1999/xhtml\">
611 <head>
612 <title>%s</title>
614 <script type=\"text/javascript\"><!--
615 // this function is needed to work around
616 // a bug in IE related to element attributes
617 function hasClass(obj)
619 var result = false;
620 if (obj.getAttributeNode(\"class\") != null)
622 result = obj.getAttributeNode(\"class\").value;
624 return result;
627 function stripe(id)
629 // the flag we'll use to keep track of
630 // whether the current row is odd or even
631 var even = false;
633 // if arguments are provided to specify the colors
634 // of the even & odd rows, then use the them;
635 // otherwise use the following defaults:
636 var evenColor = arguments[1] ? arguments[1] : \"#fff\";
637 var oddColor = arguments[2] ? arguments[2] : \"#ddd\";
639 // obtain a reference to the desired table
640 // if no such table exists, abort
641 var table = document.getElementById(id);
642 if (! table) { return; }
644 // by definition, tables can have more than one tbody
645 // element, so we'll have to get the list of child
646 // &lt;tbody&gt;s
647 var tbodies = table.getElementsByTagName(\"tbody\");
649 // and iterate through them...
650 for (var h = 0; h < tbodies.length; h++)
652 // find all the &lt;tr&gt; elements...
653 var trs = tbodies[h].getElementsByTagName(\"tr\");
655 // ... and iterate through them
656 for (var i = 0; i < trs.length; i++)
658 // avoid rows that have a class attribute
659 // or backgroundColor style
660 if (! hasClass(trs[i]) &&
661 ! trs[i].style.backgroundColor)
663 // get all the cells in this row...
664 var tds = trs[i].getElementsByTagName(\"td\");
666 // and iterate through them...
667 for (var j = 0; j < tds.length; j++)
669 var mytd = tds[j];
671 // avoid cells that have a class attribute
672 // or backgroundColor style
673 if (! hasClass(mytd) &&
674 ! mytd.style.backgroundColor)
676 mytd.style.backgroundColor =
677 even ? evenColor : oddColor;
681 // flip from odd to even, or vice-versa
682 even = ! even;
687 function toggle_invis( name )
689 var filter =
690 { acceptNode:
691 function( node )
692 { var classname = node.id;
693 if( classname )
694 { var classbase = classname.substr( 0, name.length );
695 if( classbase == name ) { return NodeFilter.FILTER_ACCEPT; } }
696 return NodeFilter.FILTER_SKIP; } };
697 var walker = document.createTreeWalker( document.body ,
698 NodeFilter.SHOW_ELEMENT ,
699 filter ,
700 false );
701 while( walker.nextNode() )
703 var e = walker.currentNode;
704 if( e.style.display == \"none\" ) { e.style.display = \"inline\"; }
705 else { e.style.display = \"none\"; }
708 --> </script>
709 </head>
710 <body onload=\"stripe('index'); return true;\">\n"
711 file style))
713 (defun hfy-default-footer (_file)
714 "Default value for `hfy-page-footer'.
715 FILE is the name of the file being rendered, in case it is needed."
716 "\n </body>\n</html>\n")
718 (defun hfy-link-style-string (style-string)
719 "Replace the end of a CSS style declaration STYLE-STRING with the contents
720 of the variable `hfy-src-doc-link-style', removing text matching the regex
721 `hfy-src-doc-link-unstyle' first, if necessary."
722 ;;(message "hfy-colour-vals");;DBUG
723 (if (string-match hfy-src-doc-link-unstyle style-string)
724 (setq style-string (replace-match "" 'fixed-case 'literal style-string)))
725 (if (and (not (string-match hfy-src-doc-link-style style-string))
726 (string-match "} *$" style-string))
727 (concat (replace-match hfy-src-doc-link-style
728 'fixed-case
729 'literal
730 style-string) " }")
731 style-string))
733 ;; utility functions - cast emacs style specification values into their
734 ;; css2 equivalents:
735 (defun hfy-triplet (colour)
736 "Takes a COLOUR name (string) and return a CSS rgb(R, G, B) triplet string.
737 Uses the definition of \"white\" to map the numbers to the 0-255 range, so
738 if you've redefined white, (esp. if you've redefined it to have a triplet
739 member lower than that of the color you are processing) strange things
740 may happen."
741 ;;(message "hfy-colour-vals");;DBUG
742 (let ((white (mapcar (lambda (I) (float (1+ I))) (hfy-colour-vals "white")))
743 (rgb16 (mapcar (lambda (I) (float (1+ I))) (hfy-colour-vals colour))))
744 (if rgb16
745 ;;(apply 'format "rgb(%d, %d, %d)"
746 ;; Use #rrggbb instead, it is smaller
747 (apply 'format "#%02x%02x%02x"
748 (mapcar (lambda (X)
749 (* (/ (nth X rgb16)
750 (nth X white)) 255))
751 '(0 1 2))))))
753 (defun hfy-family (family) (list (cons "font-family" family)))
754 (defun hfy-bgcol (colour) (list (cons "background" (hfy-triplet colour))))
755 (defun hfy-colour (colour) (list (cons "color" (hfy-triplet colour))))
756 (defun hfy-width (width) (list (cons "font-stretch" (symbol-name width))))
758 (defcustom hfy-font-zoom 1.05
759 "Font scaling from Emacs to HTML."
760 :type 'float
761 :group 'htmlfontify)
763 (defun hfy-size (height)
764 "Derive a CSS font-size specifier from an Emacs font :height attribute HEIGHT.
765 Does not cope with the case where height is a function to be applied to
766 the height of the underlying font."
767 (list
768 (cond
769 ;;(t (cons "font-size" ": 1em"))
770 ((floatp height)
771 (cons "font-size" (format "%d%%" (* (* hfy-font-zoom height) 100))))
772 ((integerp height)
773 (cons "font-size" (format "%dpt" (/ (* hfy-font-zoom height) 10 )))) )) )
775 (defun hfy-slant (slant)
776 "Derive a font-style CSS specifier from the Emacs :slant attribute SLANT:
777 CSS does not define the reverse-* styles, so just maps those to the
778 regular specifiers."
779 (list (cons "font-style"
780 (or (cdr (assq slant '((italic . "italic")
781 (reverse-italic . "italic" )
782 (oblique . "oblique")
783 (reverse-oblique . "oblique"))))
784 "normal"))))
786 (defun hfy-weight (weight)
787 "Derive a font-weight CSS specifier from an Emacs weight spec symbol WEIGHT."
788 (list (cons "font-weight" (cdr (assq weight '((ultra-bold . "900")
789 (extra-bold . "800")
790 (bold . "700")
791 (semi-bold . "600")
792 (normal . "500")
793 (semi-light . "400")
794 (light . "300")
795 (extra-light . "200")
796 (ultra-light . "100")))))))
798 (defun hfy-box-to-border-assoc (spec)
799 (if spec
800 (let ((tag (car spec))
801 (val (cadr spec)))
802 (cons (case tag
803 (:color (cons "colour" val))
804 (:width (cons "width" val))
805 (:style (cons "style" val)))
806 (hfy-box-to-border-assoc (cddr spec))))))
808 (defun hfy-box-to-style (spec)
809 (let* ((css (hfy-box-to-border-assoc spec))
810 (col (cdr (assoc "colour" css)))
811 (s (cdr (assoc "style" css))))
812 (list
813 (if col (cons "border-color" (cdr (assoc "colour" css))))
814 (cons "border-width" (format "%dpx" (or (cdr (assoc "width" css)) 1)))
815 (cons "border-style" (case s
816 (released-button "outset")
817 (pressed-button "inset" )
818 (t "solid" ))))))
820 (defun hfy-box (box)
821 "Derive CSS border-* attributes from the Emacs :box attribute BOX."
822 (if box
823 (cond
824 ((integerp box) (list (cons "border-width" (format "%dpx" box))))
825 ((stringp box) (list (cons "border" (format "solid %s 1px" box))))
826 ((listp box) (hfy-box-to-style box) ))) )
828 (defun hfy-decor (tag _val)
829 "Derive CSS text-decoration specifiers from various Emacs font attributes.
830 TAG is an Emacs font attribute key (eg :underline).
831 VAL is ignored."
832 (list
833 ;; FIXME: Why not '("text-decoration" . "underline")? --Stef
834 (case tag
835 (:underline (cons "text-decoration" "underline" ))
836 (:overline (cons "text-decoration" "overline" ))
837 (:strike-through (cons "text-decoration" "line-through")))))
839 (defun hfy-invisible (&optional _val)
840 "This text should be invisible.
841 Do something in CSS to make that happen.
842 VAL is ignored here."
843 '(("display" . "none")))
845 (defun hfy-combined-face-spec (face)
846 "Return a `defface' style alist of possible specifications for FACE.
847 Entries resulting from customization (`custom-set-faces') will take
848 precedence."
849 (append
850 (if (and hfy-display-class hfy-default-face-def (eq face 'default))
851 hfy-default-face-def)
852 (get face 'saved-face)
853 (get face 'face-defface-spec)))
855 (defun hfy-face-attr-for-class (face &optional class)
856 "Return the face attributes for FACE.
857 If CLASS is set, it must be a `defface' alist key [see below],
858 in which case the first face specification returned by `hfy-combined-face-spec'
859 which *doesn't* clash with CLASS is returned.\n
860 \(A specification with a class of t is considered to match any class you
861 specify - this matches Emacs' behavior when deciding on which face attributes
862 to use, to the best of my understanding).\n
863 If CLASS is nil, then you just get get whatever `face-attr-construct' returns,
864 ie the current specification in effect for FACE.\n
865 *NOTE*: This function forces any face that is not 'default and which has
866 no :inherit property to inherit from 'default (this is because 'default
867 is magical in that Emacs' fonts behave as if they inherit implicitly from
868 'default, but no such behavior exists in HTML/CSS).\n
869 See also `hfy-display-class' for details of valid values for CLASS."
870 (let ((face-spec
871 (if class
872 (let ((face-props (hfy-combined-face-spec face))
873 (face-specn nil)
874 (face-class nil)
875 (face-attrs nil)
876 (face-score -1)
877 (face-match nil))
878 (while face-props
879 (setq face-specn (car face-props)
880 face-class (car face-specn)
881 face-attrs (cdr face-specn)
882 face-props (cdr face-props))
883 ;; if the current element CEL of CLASS is t we match
884 ;; if the current face-class is t, we match
885 ;; if the cdr of CEL has a non-nil
886 ;; intersection with the cdr of the first member of
887 ;; the current face-class with the same car as CEL, we match
888 ;; if we actually clash, then we can't match
889 (let ((cbuf class)
890 (cel nil)
891 (key nil)
892 (val nil)
893 (x nil)
894 (next nil)
895 (score 0))
896 (while (and cbuf (not next))
897 (setq cel (car cbuf)
898 cbuf (cdr cbuf)
899 key (car cel)
900 val (cdr cel)
901 val (if (listp val) val (list val)))
902 (cond
903 ((or (eq cel t)
904 (memq face-class '(t default))) ;Default match.
905 (setq score 0) (ignore "t match"))
906 ((not (cdr (assq key face-class))) ;Neither good nor bad.
907 nil (ignore "non match, non collision"))
908 ((setq x (hfy-interq val (cdr (assq key face-class))))
909 (setq score (+ score (length x)))
910 (ignore "intersection"))
911 (t ;; nope.
912 (setq next t score -10) (ignore "collision")) ))
913 (if (> score face-score)
914 (progn
915 (setq face-match face-attrs
916 face-score score )
917 (ignore "%d << %S/%S" score face-class class))
918 (ignore "--- %d ---- (insufficient)" score)) ))
919 ;; matched ? last attrs : nil
920 (if face-match
921 (if (listp (car face-match)) (car face-match) face-match)
922 nil))
923 ;; Unfortunately the default face returns a
924 ;; :background. Fortunately we can remove it, but how do we do
925 ;; that in a non-system specific way?
926 (let ((spec (face-attr-construct face))
927 (new-spec nil))
928 (if (not (memq :background spec))
929 spec
930 (while spec
931 (let ((a (nth 0 spec))
932 (b (nth 1 spec)))
933 (unless (and (eq a :background)
934 (stringp b)
935 (string= b "SystemWindow"))
936 (setq new-spec (cons a (cons b new-spec)))))
937 (setq spec (cddr spec)))
938 new-spec)))))
939 (if (or (memq :inherit face-spec) (eq 'default face))
940 face-spec
941 (append face-spec (list :inherit 'default)))))
943 ;; construct an assoc of (css-tag-name . css-tag-value) pairs
944 ;; from a face or assoc of face attributes:
946 ;; Some tests etc:
947 ;; (mumamo-message-with-face "testing face" 'highlight)
948 ;; (mumamo-message-with-face "testing face" '(:foreground "red" :background "yellow"))
949 ;; (hfy-face-to-style-i '(:inherit default foreground-color "red"))
950 ;; default face=(:stipple nil :background "SystemWindow" :foreground
951 ;; "SystemWindowText" :inverse-video nil :box nil :strike-through
952 ;; nil :overline nil :underline nil :slant normal :weight normal
953 ;; :height 98 :width normal :family "outline-courier new")
954 (defun hfy-face-to-style-i (fn)
955 "The guts of `hfy-face-to-style': FN should be a `defface' font spec,
956 as returned by `face-attr-construct' or `hfy-face-attr-for-class'.
957 Note that this function does not get font-sizes right if they are based
958 on inherited modifiers (via the :inherit) attribute, and any other
959 modifiers that are cumulative if they appear multiple times need to be
960 merged by the user - `hfy-flatten-style' should do this."
961 ;;(message "hfy-face-to-style-i");;DBUG
963 ;; fn's value could be something like
964 ;; (:inherit
965 ;; ((foreground-color . "blue"))
966 ;; (foreground-color . "blue")
967 ;; nil)
969 (when fn
970 (let ((key (car fn))
971 (val (cadr fn))
972 (next (cddr fn))
973 (that nil)
974 (this nil)
975 (parent nil))
976 (if (eq key :inherit)
977 (let ((vs (if (listp val) val (list val))))
978 ;; (let ((x '(a b))) (setq x (append '(c d) x)))
979 ;; (let ((x '(a b))) (setq x (append '(c d) x)))
980 (dolist (v vs)
981 (setq parent
982 (append
983 parent
984 (hfy-face-to-style-i
985 (hfy-face-attr-for-class v hfy-display-class)) ))))
986 (setq this
987 (if val (case key
988 (:family (hfy-family val))
989 (:width (hfy-width val))
990 (:weight (hfy-weight val))
991 (:slant (hfy-slant val))
992 (:foreground (hfy-colour val))
993 (:background (hfy-bgcol val))
994 (:box (hfy-box val))
995 (:height (hfy-size val))
996 (:underline (hfy-decor key val))
997 (:overline (hfy-decor key val))
998 (:strike-through (hfy-decor key val))
999 (:invisible (hfy-invisible val))
1000 (:bold (hfy-weight 'bold))
1001 (:italic (hfy-slant 'italic))))))
1002 (setq that (hfy-face-to-style-i next))
1003 ;;(lwarn t :warning "%S => %S" fn (nconc this that parent))
1004 (nconc this that parent))) )
1006 (defun hfy-size-to-int (spec)
1007 "Convert SPEC, a CSS font-size specifier, to an Emacs :height attribute value.
1008 Used while merging multiple font-size attributes."
1009 ;;(message "hfy-size-to-int");;DBUG
1010 (list
1011 (if (string-match "\\([0-9]+\\)\\(%\\|pt\\)" spec)
1012 (cond ((string= "%" (match-string 2 spec))
1013 (/ (string-to-number (match-string 1 spec)) 100.0))
1014 ((string= "pt" (match-string 2 spec))
1015 (* (string-to-number (match-string 1 spec)) 10)))
1016 (string-to-number spec))) )
1018 ;; size is different, in that in order to get it right at all,
1019 ;; we have to trawl the inheritance path, accumulating modifiers,
1020 ;; _until_ we get to an absolute (pt) specifier, then combine the lot
1021 (defun hfy-flatten-style (style)
1022 "Take STYLE (see `hfy-face-to-style-i', `hfy-face-to-style') and merge
1023 any multiple attributes appropriately. Currently only font-size is merged
1024 down to a single occurrence - others may need special handling, but I
1025 haven't encountered them yet. Returns a `hfy-style-assoc'."
1026 ;;(message "(hfy-flatten-style %S)" style) ;;DBUG
1027 (let ((n 0)
1028 (m (list 1))
1029 (x nil)
1030 (r nil))
1031 (dolist (css style)
1032 (if (string= (car css) "font-size")
1033 (progn
1034 (when (not x) (setq m (nconc m (hfy-size-to-int (cdr css)))))
1035 (when (string-match "pt" (cdr css)) (setq x t)))
1036 (setq r (nconc r (list css)))))
1037 ;;(message "r: %S" r)
1038 (setq n (apply '* m))
1039 (nconc r (hfy-size (if x (round n) (* n 1.0)))) ))
1041 (defun hfy-face-resolve-face (fn)
1042 (cond
1043 ((facep fn)
1044 (hfy-face-attr-for-class fn hfy-display-class))
1045 ((and (symbolp fn)
1046 (facep (symbol-value fn)))
1047 ;; Obsolete faces like `font-lock-reference-face' are defined as
1048 ;; aliases for another face.
1049 (hfy-face-attr-for-class (symbol-value fn) hfy-display-class))
1050 (t nil)))
1053 (defun hfy-face-to-style (fn)
1054 "Take FN, a font or `defface' style font specification,
1055 \(as returned by `face-attr-construct' or `hfy-face-attr-for-class')
1056 and return a `hfy-style-assoc'.\n
1057 See also `hfy-face-to-style-i', `hfy-flatten-style'."
1058 ;;(message "hfy-face-to-style");;DBUG
1059 (let* ((face-def (hfy-face-resolve-face fn))
1060 (final-style
1061 (hfy-flatten-style (hfy-face-to-style-i face-def))))
1062 ;;(message "%S" final-style)
1063 (if (not (assoc "text-decoration" final-style))
1064 (progn (setq final-style
1065 ;; Fix-me: there is no need for this since
1066 ;; text-decoration is not inherited.
1067 ;; but it's not wrong and if this ever changes it will
1068 ;; be needed, so I think it's better to leave it in? -- v
1069 (nconc final-style '(("text-decoration"."none"))))))
1070 final-style))
1072 ;; strip redundant bits from a name. Technically, this could result in
1073 ;; a collision, but it is pretty unlikely - will fix later...
1074 ;; also handle ephemeral fonts created by overlays, which don't actually
1075 ;; have names:
1076 (defun hfy-face-or-def-to-name (fn)
1077 "Render a font symbol or `defface' font spec FN into a name (string)."
1078 ;;(message "generating name for %s" fn)
1079 (if (not (listp fn))
1080 (format "%s" fn)
1081 (let* ((key (format "%s" fn))
1082 (entry (assoc key hfy-tmpfont-stack))
1083 (base (cadr (memq :inherit fn)))
1084 (tag (cdr entry)))
1085 ;;(message "checking for key «%s» in font stack [%d]"
1086 ;; key (if entry 1 0))
1087 (if entry nil ;; noop
1088 (setq tag (format "%04d" (length hfy-tmpfont-stack))
1089 entry (cons key tag)
1090 hfy-tmpfont-stack (cons entry hfy-tmpfont-stack)))
1091 ;;(message " -> name: %s-%s" (or base 'default) tag)
1092 (format "%s-%s" (or base 'default) tag)) ))
1094 (defun hfy-css-name (fn)
1095 "Strip the boring bits from a font-name FN and return a CSS style name."
1096 ;;(message "hfy-css-name");;DBUG
1097 (let ((face-name (hfy-face-or-def-to-name fn)))
1098 (if (or (string-match "font-lock-\\(.*\\)" face-name)
1099 (string-match "cperl-\\(.*\\)" face-name)
1100 (string-match "^[Ii]nfo-\\(.*\\)" face-name))
1101 (progn
1102 (setq face-name (match-string 1 face-name))
1103 (if (string-match "\\(.*\\)-face\\'" face-name)
1104 (setq face-name (match-string 1 face-name)))
1105 face-name)
1106 face-name)) )
1108 ;; construct an assoc of (stripped-name . "{ css-stuff-here }") pairs
1109 ;; from a face:
1110 (defun hfy-face-to-css (fn)
1111 "Take FN, a font or `defface' specification (cf `face-attr-construct')
1112 and return a CSS style specification.\n
1113 See also `hfy-face-to-style'."
1114 ;;(message "hfy-face-to-css");;DBUG
1115 (let* ((css-list (hfy-face-to-style fn))
1116 (seen nil)
1117 (css-text
1118 (mapcar
1119 (lambda (E)
1120 (if (car E)
1121 (unless (member (car E) seen)
1122 (push (car E) seen)
1123 (format " %s: %s; " (car E) (cdr E)))))
1124 css-list)))
1125 (cons (hfy-css-name fn) (format "{%s}" (apply 'concat css-text)))) )
1127 (defalias 'hfy-prop-invisible-p
1128 (if (fboundp 'invisible-p) #'invisible-p
1129 (lambda (prop)
1130 "Is text property PROP an active invisibility property?"
1131 (or (and (eq buffer-invisibility-spec t) prop)
1132 (or (memq prop buffer-invisibility-spec)
1133 (assq prop buffer-invisibility-spec))))))
1135 (defun hfy-find-invisible-ranges ()
1136 "Return a list of (start-point . end-point) cons cells of invisible regions."
1137 (save-excursion
1138 (let (invisible p i s) ;; return-value pos invisible end start
1139 (setq p (goto-char (point-min)))
1140 (when (invisible-p p) (setq s p i t))
1141 (while (< p (point-max))
1142 (if i ;; currently invisible
1143 (when (not (invisible-p p)) ;; but became visible
1144 (setq i nil
1145 invisible (cons (cons s p) invisible)))
1146 ;; currently visible:
1147 (when (invisible-p p) ;; but have become invisible
1148 (setq s p i t)))
1149 (setq p (next-char-property-change p)))
1150 ;; still invisible at buffer end?
1151 (when i
1152 (setq invisible (cons (cons s (point-max)) invisible)))
1153 invisible)))
1155 (defun hfy-invisible-name (point map)
1156 "Generate a CSS style name for an invisible section of the buffer.
1157 POINT is the point inside the invisible region.
1158 MAP is the invisibility map as returned by `hfy-find-invisible-ranges'."
1159 ;;(message "(hfy-invisible-name %S %S)" point map)
1160 (let (name)
1161 (dolist (range map)
1162 (when (and (>= point (car range))
1163 (< point (cdr range)))
1164 (setq name (format "invisible-%S-%S" (car range) (cdr range)))))
1165 name))
1167 ;; Fix-me: This function needs some cleanup by someone who understand
1168 ;; all the formats that face properties can have.
1170 ;; overlay handling should be fine. haven't tested multiple stacked overlapping
1171 ;; overlays recently, but the common case of a text property face + an overlay
1172 ;; face produces the correct merged css style (or as close to it as css can get)
1173 ;; -- v
1174 (defun hfy-face-at (p)
1175 "Find face in effect at point P.
1176 If overlays are to be considered (see `hfy-optimisations') then this may
1177 return a `defface' style list of face properties instead of a face symbol."
1178 ;;(message "hfy-face-at");;DBUG
1179 ;; Fix-me: clean up, remove face-name etc
1180 ;; not sure why we'd want to remove face-name? -- v
1181 (let ((overlay-data nil)
1182 (base-face nil)
1183 (face-name (get-text-property p 'face))
1184 ;; (face-name (hfy-get-face-at p))
1185 (prop-seen nil)
1186 (extra-props nil)
1187 (text-props (text-properties-at p)))
1188 ;;(message "face-name: %S" face-name)
1189 (when (and face-name (listp face-name) (facep (car face-name)))
1190 ;;(message "face-name is a list %S" face-name)
1191 ;;(setq text-props (cons 'face face-name))
1192 (dolist (f face-name)
1193 (setq extra-props (if (listp f)
1194 ;; for things like (variable-pitch
1195 ;; (:foreground "red"))
1196 (cons f extra-props)
1197 (cons :inherit (cons f extra-props)))))
1198 (setq base-face (car face-name)
1199 face-name nil))
1200 ;; text-properties-at => (face (:foreground "red" ...))
1201 ;; or => (face (compilation-info underline)) list of faces
1202 ;; overlay-properties
1203 ;; format= (evaporate t face ((foreground-color . "red")))
1205 ;; SO: if we have turned overlays off,
1206 ;; or if there's no overlay data
1207 ;; just bail out and return whatever face data we've accumulated so far
1208 (if (or (not (hfy-opt 'keep-overlays))
1209 (not (setq overlay-data (hfy-overlay-props-at p))))
1210 (progn
1211 ;;(message "· %d: %s; %S; %s"
1212 ;; p face-name extra-props text-props)
1213 (or face-name base-face)) ;; no overlays or extra properties
1214 ;; collect any face data and any overlay data for processing:
1215 (when text-props
1216 (push text-props overlay-data))
1217 (setq overlay-data (nreverse overlay-data))
1218 ;;(message "- %d: %s; %S; %s; %s"
1219 ;; p face-name extra-props text-props overlay-data)
1220 ;; remember the basic face name so we don't keep repeating its specs:
1221 (when face-name (setq base-face face-name))
1222 (dolist (P overlay-data)
1223 (let ((iprops (cadr (memq 'invisible P)))) ;FIXME: plist-get?
1224 ;;(message "(hfy-prop-invisible-p %S)" iprops)
1225 (when (and iprops (hfy-prop-invisible-p iprops))
1226 (setq extra-props
1227 (cons :invisible (cons t extra-props))) ))
1228 (let ((fprops (cadr (or (memq 'face P)
1229 (memq 'font-lock-face P)))))
1230 ;;(message "overlay face: %s" fprops)
1231 (if (not (listp fprops))
1232 (let ((this-face (if (stringp fprops) (intern fprops) fprops)))
1233 (when (not (eq this-face base-face))
1234 (setq extra-props
1235 (cons :inherit
1236 (cons this-face extra-props))) ))
1237 (while fprops
1238 (if (facep (car fprops))
1239 (let ((face (car fprops)))
1240 (when (stringp face) (setq face (intern fprops)))
1241 (setq extra-props
1242 (cons :inherit
1243 (cons face
1244 extra-props)))
1245 (setq fprops (cdr fprops)))
1246 (let (p v)
1247 ;; Sigh.
1248 (if (listp (car fprops))
1249 (if (nlistp (cdr (car fprops)))
1250 (progn
1251 ;; ((prop . val))
1252 (setq p (caar fprops))
1253 (setq v (cdar fprops))
1254 (setq fprops (cdr fprops)))
1255 ;; ((prop val))
1256 (setq p (caar fprops))
1257 (setq v (cadar fprops))
1258 (setq fprops (cdr fprops)))
1259 (if (listp (cdr fprops))
1260 (progn
1261 ;; (:prop val :prop val ...)
1262 (setq p (car fprops))
1263 (setq v (cadr fprops))
1264 (setq fprops (cddr fprops)))
1265 (if (and (listp fprops)
1266 (not (listp (cdr fprops))))
1267 ;;(and (consp x) (cdr (last x)))
1268 (progn
1269 ;; (prop . val)
1270 (setq p (car fprops))
1271 (setq v (cdr fprops))
1272 (setq fprops nil))
1273 (error "Eh... another format! fprops=%s" fprops) )))
1274 (setq p (case p
1275 ;; These are all the properties handled
1276 ;; in `hfy-face-to-style-i'.
1278 ;; Are these translations right?
1279 ;; yes, they are -- v
1280 (family :family )
1281 (width :width )
1282 (height :height )
1283 (weight :weight )
1284 (slant :slant )
1285 (underline :underline )
1286 (overline :overline )
1287 (strike-through :strike-through)
1288 (box :box )
1289 (foreground-color :foreground)
1290 (background-color :background)
1291 (bold :bold )
1292 (italic :italic )
1293 (t p)))
1294 (if (memq p prop-seen) nil ;; noop
1295 (setq prop-seen (cons p prop-seen)
1296 extra-props (cons p (cons v extra-props))))))))))
1297 ;;(message "+ %d: %s; %S" p face-name extra-props)
1298 (if extra-props
1299 (nconc extra-props (if (listp face-name)
1300 face-name
1301 (face-attr-construct face-name)))
1302 face-name)) ))
1304 (defun hfy-overlay-props-at (p)
1305 "Grab overlay properties at point P.
1306 The plists are returned in descending priority order."
1307 (sort (mapcar #'overlay-properties (overlays-at p))
1308 (lambda (A B) (> (or (cadr (memq 'priority A)) 0) ;FIXME: plist-get?
1309 (or (cadr (memq 'priority B)) 0)))))
1311 ;; construct an assoc of (face-name . (css-name . "{ css-style }")) elements:
1312 (defun hfy-compile-stylesheet ()
1313 "Trawl the current buffer, construct and return a `hfy-sheet-assoc'."
1314 ;;(message "hfy-compile-stylesheet");;DBUG
1315 (let ((pt (point-min))
1316 ;; Make the font stack stay:
1317 ;;(hfy-tmpfont-stack nil)
1318 (fn nil)
1319 (style nil))
1320 (save-excursion
1321 (goto-char pt)
1322 (while (< pt (point-max))
1323 (if (and (setq fn (hfy-face-at pt)) (not (assoc fn style)))
1324 (push (cons fn (hfy-face-to-css fn)) style))
1325 (setq pt (next-char-property-change pt))) )
1326 (push (cons 'default (hfy-face-to-css 'default)) style)))
1328 (defun hfy-fontified-p ()
1329 "`font-lock' doesn't like to say it's been fontified when in batch
1330 mode, but we want to know if we should fontify or raw copy, so in batch
1331 mode we check for non-default face properties. Otherwise we test
1332 variable `font-lock-mode' and variable `font-lock-fontified' for truth."
1333 ;;(message "font-lock-fontified: %S" font-lock-fontified)
1334 ;;(message "noninteractive : %S" noninteractive)
1335 ;;(message "font-lock-mode : %S" font-lock-mode)
1336 (and font-lock-fontified
1337 (if noninteractive
1338 (let ((pt (point-min))
1339 (face-name nil))
1340 (save-excursion
1341 (goto-char pt)
1342 (while (and (< pt (point-max)) (not face-name))
1343 (setq face-name (hfy-face-at pt))
1344 (setq pt (next-char-property-change pt))))
1345 face-name)
1346 font-lock-mode)))
1348 ;; remember, the map is in reverse point order:
1349 ;; I wrote this while suffering the effects of a cold, and maybe a
1350 ;; mild fever - I think it's correct, but it might be a little warped
1351 ;; as my minfd keeps ... where was I? Oh yes, the bunnies...
1352 (defun hfy-merge-adjacent-spans (face-map)
1353 "Where FACE-MAP is a `hfy-facemap-assoc' for the current buffer,
1354 this function merges adjacent style blocks which are of the same value
1355 and are separated by nothing more interesting than whitespace.\n
1356 <span class=\"foo\">narf</span> <span class=\"foo\">brain</span>\n
1357 \(as interpreted from FACE-MAP) would become:\n
1358 <span class=\"foo\">narf brain</span>\n
1359 Returns a modified copy of FACE-MAP."
1360 (let ((tmp-map face-map)
1361 (map-buf nil)
1362 (first-start nil)
1363 (first-stop nil)
1364 (last-start nil)
1365 (last-stop nil)
1366 (span-stop nil)
1367 (span-start nil)
1368 (reduced-map nil))
1369 ;;(push (car tmp-map) reduced-map)
1370 ;;(push (cadr tmp-map) reduced-map)
1371 (while tmp-map
1372 (setq first-start (cadddr tmp-map)
1373 first-stop (caddr tmp-map)
1374 last-start (cadr tmp-map)
1375 last-stop (car tmp-map)
1376 map-buf tmp-map
1377 span-start last-start
1378 span-stop last-stop )
1379 (while (and (equal (cdr first-start)
1380 (cdr last-start))
1381 (save-excursion
1382 (goto-char (car first-stop))
1383 (not (re-search-forward "[^ \t\n\r]" (car last-start) t))))
1384 (setq map-buf (cddr map-buf)
1385 span-start first-start
1386 first-start (cadddr map-buf)
1387 first-stop (caddr map-buf)
1388 last-start (cadr map-buf)
1389 last-stop (car map-buf)))
1390 (push span-stop reduced-map)
1391 (push span-start reduced-map)
1392 (setq tmp-map (memq last-start tmp-map))
1393 (setq tmp-map (cdr tmp-map)))
1394 (setq reduced-map (nreverse reduced-map))))
1396 ;; remember to generate 'synthetic' </span> entries -
1397 ;; emacs copes by just having a stack of styles in effect
1398 ;; and only using the top one: html has a more simplistic approach -
1399 ;; we have to explicitly end a style, there's no way of temporarily
1400 ;; overriding it w. another one... (afaik)
1401 (defun hfy-compile-face-map ()
1402 ;; no need for special <a> version.
1403 ;; IME hyperlinks don't get underlined, esp when you htmlfontify a whole
1404 ;; source tree, so the <a> version is needed -- v
1405 ;; Fix-me: save table for multi-buffer
1406 "Compile and return a `hfy-facemap-assoc' for the current buffer."
1407 ;;(message "hfy-compile-face-map");;DBUG
1408 (let* ((pt (point-min))
1409 (pt-narrow (save-restriction (widen) (point-min)))
1410 (offset (- pt pt-narrow))
1411 (fn nil)
1412 (map nil)
1413 (prev-tag nil)) ;; t if the last tag-point was a span-start
1414 ;; nil if it was a span-stop
1415 (save-excursion
1416 (goto-char pt)
1417 (while (< pt (point-max))
1418 (if (setq fn (hfy-face-at pt))
1419 (progn (if prev-tag (push (cons pt-narrow 'end) map))
1420 (push (cons pt-narrow fn) map)
1421 (setq prev-tag t))
1422 (if prev-tag (push (cons pt-narrow 'end) map))
1423 (setq prev-tag nil))
1424 (setq pt (next-char-property-change pt))
1425 (setq pt-narrow (+ offset pt)))
1426 (if (and map (not (eq 'end (cdar map))))
1427 (push (cons (- (point-max) (point-min)) 'end) map)))
1428 (if (hfy-opt 'merge-adjacent-tags) (hfy-merge-adjacent-spans map) map)))
1430 (defun hfy-buffer ()
1431 "Generate a buffer to hold the HTML output.
1432 The filename of this buffer is derived from the source (current) buffer's
1433 variable `buffer-file-name', if it is set, plus `hfy-extn'.
1434 Otherwise a plausible filename is constructed from `default-directory',
1435 `buffer-name' and `hfy-extn'."
1436 (let* ((name (concat (buffer-name) hfy-extn))
1437 (src (buffer-file-name))
1438 (buf (get-buffer-create name)))
1439 (with-current-buffer buf
1440 (setq buffer-file-name
1441 (if src (concat src hfy-extn)
1442 (expand-file-name (if (string-match "^.*/\\([^/]*\\)\\'" name)
1443 (match-string 1 name)
1444 name))))
1445 buf)))
1447 (defun hfy-lookup (face style)
1448 "Get a CSS style name for FACE from STYLE."
1449 (cadr (assoc face style)))
1451 (defun hfy-link-style (style-string)
1452 "Copy, alter and return a STYLE-STRING to make it suitable for a hyperlink.
1453 Uses `hfy-link-style-fun' to do this."
1454 (if (functionp hfy-link-style-fun)
1455 (funcall hfy-link-style-fun style-string)
1456 style-string))
1458 (defun hfy-sprintf-stylesheet (css file)
1459 "Return the inline CSS style sheet for FILE as a string."
1460 (let ((stylesheet
1461 (concat
1462 hfy-meta-tags
1463 "\n<style type=\"text/css\"><!-- \n"
1464 ;; Fix-me: Add handling of page breaks here + scan for ^L
1465 ;; where appropriate.
1466 (format "body %s\n" (cddr (assq 'default css)))
1467 (apply 'concat
1468 (mapcar
1469 (lambda (style)
1470 (format
1471 "span.%s %s\nspan.%s a %s\n"
1472 (cadr style) (cddr style)
1473 (cadr style) (hfy-link-style (cddr style))))
1474 css))
1475 " --></style>\n")))
1476 (funcall hfy-page-header file stylesheet)))
1478 ;; tag all the dangerous characters we want to escape
1479 ;; (ie any "<> chars we _didn't_ put there explicitly for css markup)
1480 (defun hfy-html-enkludge-buffer ()
1481 "Mark dangerous [\"<>] characters with the `hfy-quoteme' property.\n
1482 See also `hfy-html-dekludge-buffer'."
1483 ;;(message "hfy-html-enkludge-buffer");;DBUG
1484 (save-excursion
1485 (goto-char (point-min))
1486 (while (re-search-forward hfy-html-quote-regex nil t)
1487 (put-text-property (match-beginning 0) (point) 'hfy-quoteme t))) )
1489 ;; dangerous char -> &entity;
1490 (defun hfy-html-quote (char-string)
1491 "Map CHAR-STRING to an HTML safe string (entity) if need be."
1492 ;;(message "hfy-html-quote");;DBUG
1493 (or (cadr (assoc char-string hfy-html-quote-map)) char-string) )
1495 ;; actually entity-ise dangerous chars.
1496 ;; note that we can't do this until _after_ we have inserted the css
1497 ;; markup, since we use a position-based map to insert this, and if we
1498 ;; enter any other text before we do this, we'd have to track another
1499 ;; map of offsets, which would be tedious...
1500 (defun hfy-html-dekludge-buffer ()
1501 "Transform all dangerous characters marked with the `hfy-quoteme' property
1502 using `hfy-html-quote'.\n
1503 See also `hfy-html-enkludge-buffer'."
1504 ;;(message "hfy-html-dekludge-buffer");;DBUG
1505 (save-excursion
1506 (goto-char (point-min))
1507 (while (re-search-forward hfy-html-quote-regex nil t)
1508 (if (get-text-property (match-beginning 0) 'hfy-quoteme)
1509 (replace-match (hfy-html-quote (match-string 1))) )) ))
1511 ;; Borrowed from font-lock.el
1512 (defmacro hfy-save-buffer-state (varlist &rest body)
1513 "Bind variables according to VARLIST and eval BODY restoring buffer state.
1514 Do not record undo information during evaluation of BODY."
1515 (declare (indent 1) (debug let))
1516 (let ((modified (make-symbol "modified")))
1517 `(let* ,(append varlist
1518 `((,modified (buffer-modified-p))
1519 (buffer-undo-list t)
1520 (inhibit-read-only t)
1521 (inhibit-point-motion-hooks t)
1522 (inhibit-modification-hooks t)
1523 deactivate-mark
1524 buffer-file-name
1525 buffer-file-truename))
1526 (progn
1527 ,@body)
1528 (unless ,modified
1529 (restore-buffer-modified-p nil)))))
1531 (defun hfy-mark-trailing-whitespace ()
1532 "Tag trailing whitespace with a hfy property if it is currently highlighted."
1533 (when show-trailing-whitespace
1534 (let ((inhibit-read-only t))
1535 (save-excursion
1536 (goto-char (point-min))
1537 (hfy-save-buffer-state nil
1538 (while (re-search-forward "[ \t]+$" nil t)
1539 (put-text-property (match-beginning 0) (match-end 0)
1540 'hfy-show-trailing-whitespace t)))))))
1542 (defun hfy-unmark-trailing-whitespace ()
1543 "Undo the effect of `hfy-mark-trailing-whitespace'."
1544 (when show-trailing-whitespace
1545 (hfy-save-buffer-state nil
1546 (remove-text-properties (point-min) (point-max)
1547 '(hfy-show-trailing-whitespace)))))
1549 (defun hfy-fontify-buffer (&optional srcdir file)
1550 "Implement the guts of `htmlfontify-buffer'.
1551 SRCDIR, if set, is the directory being htmlfontified.
1552 FILE, if set, is the file name."
1553 (if srcdir (setq srcdir (directory-file-name srcdir)))
1554 (let* ( (html-buffer (hfy-buffer))
1555 (css-sheet nil)
1556 (css-map nil)
1557 (invis-ranges nil)
1558 (rovl nil)
1559 (orig-ovls (overlays-in (point-min) (point-max)))
1560 (rmin (when mark-active (region-beginning)))
1561 (rmax (when mark-active (region-end ))) )
1562 (when (and mark-active
1563 transient-mark-mode)
1564 (unless (and (= rmin (point-min))
1565 (= rmax (point-max)))
1566 (setq rovl (make-overlay rmin rmax))
1567 (overlay-put rovl 'priority 1000)
1568 (overlay-put rovl 'face 'region)))
1569 ;; copy the buffer, including fontification, and switch to it:
1570 (hfy-mark-trailing-whitespace)
1571 (setq css-sheet (hfy-compile-stylesheet )
1572 css-map (hfy-compile-face-map )
1573 invis-ranges (hfy-find-invisible-ranges))
1574 (hfy-unmark-trailing-whitespace)
1575 (when rovl
1576 (delete-overlay rovl))
1577 (copy-to-buffer html-buffer (point-min) (point-max))
1578 (set-buffer html-buffer)
1579 ;; rip out props that could interfere with our htmlisation of the buffer:
1580 (remove-text-properties (point-min) (point-max) hfy-ignored-properties)
1581 ;; Apply overlay invisible spec
1582 (setq orig-ovls
1583 (sort orig-ovls
1584 (lambda (A B)
1585 (> (or (cadr (memq 'priority (overlay-properties A))) 0)
1586 (or (cadr (memq 'priority (overlay-properties B))) 0)))))
1587 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1588 ;; at this point, html-buffer retains the fontification of the parent:
1589 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1590 ;; we don't really need or want text in the html buffer to be invisible, as
1591 ;; that can make it look like we've rendered invalid xhtml when all that's
1592 ;; happened is some tags are in the invisible portions of the buffer:
1593 (setq buffer-invisibility-spec nil)
1594 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1595 ;; #####################################################################
1596 ;; if we are in etags mode, add properties to mark the anchors and links
1597 (if (and srcdir file)
1598 (progn
1599 (hfy-mark-tag-names srcdir file) ;; mark anchors
1600 (hfy-mark-tag-hrefs srcdir file))) ;; mark links
1601 ;; #####################################################################
1602 ;; mark the 'dangerous' characters
1603 ;;(message "marking dangerous characters")
1604 (hfy-html-enkludge-buffer)
1605 ;; trawl the position-based face-map, inserting span tags as we go
1606 ;; note that we cannot change any character positions before this point
1607 ;; or we will invalidate the map:
1608 ;; NB: This also means we have to trawl the map in descending file-offset
1609 ;; order, obviously.
1610 ;; ---------------------------------------------------------------------
1611 ;; Remember, inserting pushes properties to the right, which we don't
1612 ;; actually want to happen for link properties, so we have to flag
1613 ;; them and move them by hand - if you don't, you end up with
1615 ;; <span class="foo"><a href="bar">texta</span><span class="bletch"></a>...
1617 ;; instead of:
1619 ;; <span class="foo"><a href="bar">texta</a></span><span class="bletch">...
1621 ;; If my analysis of the problem is correct, we can detect link-ness by
1622 ;; either hfy-linkp or hfy-endl properties at the insertion point, but I
1623 ;; think we only need to relocate the hfy-endl property, as the hfy-linkp
1624 ;; property has already served its main purpose by this point.
1625 ;;(message "mapcar over the CSS-MAP")
1626 (message "invis-ranges:\n%S" invis-ranges)
1627 (dolist (point-face css-map)
1628 (let ((pt (car point-face))
1629 (fn (cdr point-face))
1630 (move-link nil))
1631 (goto-char pt)
1632 (setq move-link
1633 (or (get-text-property pt 'hfy-linkp)
1634 (get-text-property pt 'hfy-endl )))
1635 (if (eq 'end fn)
1636 (insert "</span>")
1637 (if (not (and srcdir file))
1639 (when move-link
1640 (remove-text-properties (point) (1+ (point)) '(hfy-endl nil))
1641 (put-text-property pt (1+ pt) 'hfy-endl t) ))
1642 ;; if we have invisible blocks, we need to do some extra magic:
1643 (if invis-ranges
1644 (let ((iname (hfy-invisible-name pt invis-ranges))
1645 (fname (hfy-lookup fn css-sheet )))
1646 (when (assq pt invis-ranges)
1647 (insert
1648 (format "<span onclick=\"toggle_invis('%s');\">" iname))
1649 (insert "…</span>"))
1650 (insert
1651 (format "<span class=\"%s\" id=\"%s-%d\">" fname iname pt)))
1652 (insert (format "<span class=\"%s\">" (hfy-lookup fn css-sheet))))
1653 (if (not move-link) nil
1654 ;;(message "removing prop2 @ %d" (point))
1655 (if (remove-text-properties (point) (1+ (point)) '(hfy-endl nil))
1656 (put-text-property pt (1+ pt) 'hfy-endl t))))))
1657 ;; #####################################################################
1658 ;; Invisibility
1659 ;; Maybe just make the text invisible in XHTML?
1660 ;; DONE -- big block of obsolete invisibility code elided here -- v
1661 ;; #####################################################################
1662 ;; (message "checking to see whether we should link...")
1663 (if (and srcdir file)
1664 (let ((lp 'hfy-link)
1665 (pt (point-min))
1666 (pr nil)
1667 (rr nil))
1668 ;; (message " yes we should.")
1669 ;; translate 'hfy-anchor properties to anchors
1670 (while (setq pt (next-single-property-change pt 'hfy-anchor))
1671 (if (setq pr (get-text-property pt 'hfy-anchor))
1672 (progn (goto-char pt)
1673 (remove-text-properties pt (1+ pt) '(hfy-anchor nil))
1674 (insert (concat "<a name=\"" pr "\"></a>")))))
1675 ;; translate alternate 'hfy-link and 'hfy-endl props to opening
1676 ;; and closing links. (this should avoid those spurious closes
1677 ;; we sometimes get by generating only paired tags)
1678 (setq pt (point-min))
1679 (while (setq pt (next-single-property-change pt lp))
1680 (if (not (setq pr (get-text-property pt lp))) nil
1681 (goto-char pt)
1682 (remove-text-properties pt (1+ pt) (list lp nil))
1683 (case lp
1684 (hfy-link
1685 (if (setq rr (get-text-property pt 'hfy-inst))
1686 (insert (format "<a name=\"%s\"></a>" rr)))
1687 (insert (format "<a href=\"%s\">" pr))
1688 (setq lp 'hfy-endl))
1689 (hfy-endl
1690 (insert "</a>") (setq lp 'hfy-link)) ))) ))
1692 ;; #####################################################################
1693 ;; transform the dangerous chars. This changes character positions
1694 ;; since entities have > char length.
1695 ;; note that this deletes the dangerous characters, and therefore
1696 ;; destroys any properties they may contain (such as 'hfy-endl),
1697 ;; so we have to do this after we use said properties:
1698 ;; (message "munging dangerous characters")
1699 (hfy-html-dekludge-buffer)
1700 ;; insert the stylesheet at the top:
1701 (goto-char (point-min))
1702 ;;(message "inserting stylesheet")
1703 (insert (hfy-sprintf-stylesheet css-sheet file))
1704 (if (hfy-opt 'div-wrapper) (insert "<div class=\"default\">"))
1705 (insert "\n<pre>")
1706 (goto-char (point-max))
1707 (insert "</pre>\n")
1708 (if (hfy-opt 'div-wrapper) (insert "</div>"))
1709 ;;(message "inserting footer")
1710 (insert (funcall hfy-page-footer file))
1711 ;; call any post html-generation hooks:
1712 (run-hooks 'hfy-post-html-hooks)
1713 ;; return the html buffer
1714 (set-buffer-modified-p nil)
1715 html-buffer))
1717 (defun hfy-force-fontification ()
1718 "Try to force font-locking even when it is optimized away."
1719 (run-hooks 'hfy-init-kludge-hook)
1720 (eval-and-compile (require 'font-lock))
1721 (if (boundp 'font-lock-cache-position)
1722 (or font-lock-cache-position
1723 (set 'font-lock-cache-position (make-marker))))
1724 (if (not noninteractive)
1725 (progn
1726 (message "hfy interactive mode (%S %S)" window-system major-mode)
1727 (when (and font-lock-defaults
1728 font-lock-mode)
1729 (font-lock-fontify-region (point-min) (point-max) nil)))
1730 (message "hfy batch mode (%s:%S)"
1731 (or (buffer-file-name) (buffer-name)) major-mode)
1732 (when font-lock-defaults
1733 (font-lock-fontify-buffer)) ))
1735 ;;;###autoload
1736 (defun htmlfontify-buffer (&optional srcdir file)
1737 "Create a new buffer, named for the current buffer + a .html extension,
1738 containing an inline CSS-stylesheet and formatted CSS-markup HTML
1739 that reproduces the look of the current Emacs buffer as closely
1740 as possible.
1742 Dangerous characters in the existing buffer are turned into HTML
1743 entities, so you should even be able to do HTML-within-HTML
1744 fontified display.
1746 You should, however, note that random control or eight-bit
1747 characters such as ^L (\x0c) or ¤ (\xa4) won't get mapped yet.
1749 If the SRCDIR and FILE arguments are set, lookup etags derived
1750 entries in the `hfy-tags-cache' and add HTML anchors and
1751 hyperlinks as appropriate."
1752 (interactive)
1753 ;; pick up the file name in case we didn't receive it
1754 (if (not file)
1755 (progn (setq file (or (buffer-file-name) (buffer-name)))
1756 (if (string-match "/\\([^/]*\\)\\'" file)
1757 (setq file (match-string 1 file)))) )
1759 (if (not (hfy-opt 'skip-refontification))
1760 (save-excursion ;; Keep region
1761 (hfy-force-fontification)))
1762 (if (called-interactively-p 'any) ;; display the buffer in interactive mode:
1763 (switch-to-buffer (hfy-fontify-buffer srcdir file))
1764 (hfy-fontify-buffer srcdir file)))
1766 ;; recursive file listing
1767 (defun hfy-list-files (directory)
1768 "Return a list of files under DIRECTORY.
1769 Strips any leading \"./\" from each filename."
1770 ;;(message "hfy-list-files");;DBUG
1771 ;; FIXME: this changes the dir of the currrent buffer. Is that right??
1772 (cd directory)
1773 (mapcar (lambda (F) (if (string-match "^./\\(.*\\)" F) (match-string 1 F) F))
1774 (split-string (shell-command-to-string hfy-find-cmd))) )
1776 ;; strip the filename off, return a directiry name
1777 ;; not a particularly thorough implementaion, but it will be
1778 ;; fed pretty carefully, so it should be Ok:
1779 (defun hfy-dirname (file)
1780 "Return everything preceding the last \"/\" from a relative filename FILE,
1781 on the assumption that this will produce a relative directory name.
1782 Hardly bombproof, but good enough in the context in which it is being used."
1783 ;;(message "hfy-dirname");;DBUG
1784 (let ((f (directory-file-name file)))
1785 (and (string-match "^\\(.*\\)/" f) (match-string 1 f))))
1787 ;; create a directory, cf mkdir -p
1788 (defun hfy-make-directory (dir)
1789 "Approx. equivalent of mkdir -p DIR."
1790 ;;(message "hfy-make-directory");;DBUG
1791 (if (file-exists-p dir)
1792 (if (file-directory-p dir) t)
1793 (make-directory dir t)))
1795 (defun hfy-text-p (srcdir file)
1796 "Is SRCDIR/FILE text? Uses `hfy-istext-command' to determine this."
1797 (let* ((cmd (format hfy-istext-command (expand-file-name file srcdir)))
1798 (rsp (shell-command-to-string cmd)))
1799 (string-match "text" rsp)))
1801 ;; open a file, check fontification, if fontified, write a fontified copy
1802 ;; to the destination directory, otherwise just copy the file:
1803 (defun hfy-copy-and-fontify-file (srcdir dstdir file)
1804 "Open FILE in SRCDIR - if fontified, write a fontified copy to DSTDIR
1805 adding an extension of `hfy-extn'. Fontification is actually done by
1806 `htmlfontify-buffer'. If the buffer is not fontified, just copy it."
1807 ;;(message "hfy-copy-and-fontify-file");;DBUG
1808 (let (;;(fast-lock-minimum-size hfy-fast-lock-save)
1809 ;;(font-lock-support-mode 'fast-lock-mode)
1810 ;;(window-system (or window-system 'htmlfontify))
1811 (target nil)
1812 (source nil)
1813 (html nil))
1814 (cd srcdir)
1815 (with-current-buffer (setq source (find-file-noselect file))
1816 ;; FIXME: Shouldn't this use expand-file-name? --Stef
1817 (setq target (concat dstdir "/" file))
1818 (hfy-make-directory (hfy-dirname target))
1819 (if (not (hfy-opt 'skip-refontification)) (hfy-force-fontification))
1820 (if (or (hfy-fontified-p) (hfy-text-p srcdir file))
1821 (progn (setq html (hfy-fontify-buffer srcdir file))
1822 (set-buffer html)
1823 (write-file (concat target hfy-extn))
1824 (kill-buffer html))
1825 ;; #o0200 == 128, but emacs20 doesn't know that
1826 (if (and (file-exists-p target) (not (file-writable-p target)))
1827 (set-file-modes target (logior (file-modes target) 128)))
1828 (copy-file (buffer-file-name source) target 'overwrite))
1829 (kill-buffer source)) ))
1831 ;; list of tags in file in srcdir
1832 (defun hfy-tags-for-file (cache-hash file)
1833 "List of etags tags that have definitions in this FILE.
1834 CACHE-HASH is the tags cache."
1835 ;;(message "hfy-tags-for-file");;DBUG
1836 (let* ((tag-list nil))
1837 (if cache-hash
1838 (maphash
1839 (lambda (K V)
1840 (if (assoc file V)
1841 (setq tag-list (cons K tag-list))))
1842 cache-hash))
1843 tag-list))
1845 ;; mark the tags native to this file for anchors
1846 (defun hfy-mark-tag-names (srcdir file)
1847 "Mark tags in FILE (lookup SRCDIR in `hfy-tags-cache') with the `hfy-anchor'
1848 property, with a value of \"tag.line-number\"."
1849 ;;(message "(hfy-mark-tag-names %s %s)" srcdir file);;DBUG
1850 (let* ((cache-entry (assoc srcdir hfy-tags-cache))
1851 (cache-hash (cadr cache-entry)))
1852 (if cache-hash
1853 (mapcar
1854 (lambda (TAG)
1855 (mapcar
1856 (lambda (TLIST)
1857 (if (string= file (car TLIST))
1858 (let* ((line (cadr TLIST) )
1859 (chr (caddr TLIST) )
1860 (link (format "%s.%d" TAG line) ))
1861 (put-text-property (+ 1 chr)
1862 (+ 2 chr)
1863 'hfy-anchor link))))
1864 (gethash TAG cache-hash)))
1865 (hfy-tags-for-file cache-hash file)))))
1867 (defun hfy-relstub (file &optional start)
1868 "Return a \"../\" stub of the appropriate length for the current source
1869 tree depth, as determined from FILE (a filename).
1870 START is the offset at which to start looking for the / character in FILE."
1871 ;;(message "hfy-relstub");;DBUG
1872 (let ((c ""))
1873 (while (setq start (string-match "/" file start))
1874 (setq start (1+ start)) (setq c (concat c "../")))
1877 (defun hfy-href-stub (this-file def-files tag)
1878 "Return an href stub for a tag href in THIS-FILE.
1879 If DEF-FILES (list of files containing definitions for the tag in question)
1880 contains only one entry, the href should link straight to that file.
1881 Otherwise, the link should be to the index file.\n
1882 We are not yet concerned with the file extensions/tag line number and so on at
1883 this point.\n
1884 If `hfy-split-index' is set, and the href wil be to an index file rather than
1885 a source file, append a .X to `hfy-index-file', where X is the uppercased
1886 first character of TAG.\n
1887 See also `hfy-relstub', `hfy-index-file'."
1888 ;;(message "hfy-href-stub");;DBUG
1889 ;; FIXME: Why not use something like
1890 ;; (file-relative-name (if ...) (file-name-directory this-file)) ? --Stef
1891 (concat
1892 (hfy-relstub this-file)
1893 (if (= 1 (length def-files)) (car def-files)
1894 (if (not hfy-split-index) hfy-index-file
1895 (concat hfy-index-file "." (upcase (substring tag 0 1)))))) )
1897 (defun hfy-href (this-file def-files tag tag-map)
1898 "Return a relative href to the tag in question, based on\n
1899 THIS-FILE `hfy-link-extn' `hfy-extn' DEF-FILES TAG and TAG-MAP\n
1900 THIS-FILE is the current source file
1901 DEF-FILES is a list of file containing possible link endpoints for TAG
1902 TAG is the tag in question
1903 TAG-MAP is the entry in `hfy-tags-cache'."
1904 ;;(message "hfy-href");;DBUG
1905 (concat
1906 (hfy-href-stub this-file def-files tag)
1907 (or hfy-link-extn hfy-extn) "#" tag ;;(.src -> .html)
1908 (if (= 1 (length def-files))
1909 (concat "." (format "%d" (cadr (assoc (car def-files) tag-map)))))) )
1911 (defun hfy-word-regex (string)
1912 "Return a regex that matches STRING as the first `match-string', with non
1913 word characters on either side."
1914 ;; FIXME: Should this use [^$[:alnum:]_] instead? --Stef
1915 (concat "[^$A-Za-z_0-9]\\(" (regexp-quote string) "\\)[^A-Za-z_0-9]"))
1917 ;; mark all tags for hyperlinking, except the tags at
1918 ;; their own points of definition, iyswim:
1919 (defun hfy-mark-tag-hrefs (srcdir file)
1920 "Mark href start points with the `hfy-link' prop (value: href string).\n
1921 Mark href end points with the `hfy-endl' prop (value t).\n
1922 Avoid overlapping links, and mark links in descending length of
1923 tag name in order to prevent subtags from usurping supertags,
1924 \(eg \"term\" for \"terminal\").
1925 SRCDIR is the directory being \"published\".
1926 FILE is the specific file we are rendering."
1927 ;;(message "hfy-mark-tag-hrefs");;DBUG
1928 (let ((cache-entry (assoc srcdir hfy-tags-cache))
1929 (list-cache (assoc srcdir hfy-tags-sortl))
1930 (rmap-cache (assoc srcdir hfy-tags-rmap ))
1931 (no-comment (hfy-opt 'zap-comment-links))
1932 (no-strings (hfy-opt 'zap-string-links ))
1933 (cache-hash nil)
1934 (tags-list nil)
1935 (tags-rmap nil)
1936 (case-fold-search nil))
1937 ;; extract the tag mapping hashes (fwd and rev) and the tag list:
1938 (if (and (setq cache-hash (cadr cache-entry))
1939 (setq tags-rmap (cadr rmap-cache ))
1940 (setq tags-list (cadr list-cache )))
1941 (mapcar
1942 (lambda (TAG)
1943 (let* ((start nil)
1944 (stop nil)
1945 (href nil)
1946 (name nil)
1947 (case-fold-search nil)
1948 (tmp-point nil)
1949 (maybe-start nil)
1950 (face-at nil)
1951 (rmap-entry nil)
1952 (rnew-elt nil)
1953 (rmap-line nil)
1954 (tag-regex (hfy-word-regex TAG))
1955 (tag-map (gethash TAG cache-hash))
1956 (tag-files (mapcar #'car tag-map)))
1957 ;; find instances of TAG and do what needs to be done:
1958 (goto-char (point-min))
1959 (while (search-forward TAG nil 'NOERROR)
1960 (setq tmp-point (point)
1961 maybe-start (- (match-beginning 0) 1))
1962 (goto-char maybe-start)
1963 (if (not (looking-at tag-regex))
1965 (setq start (match-beginning 1))
1966 (setq stop (match-end 1))
1967 (setq face-at
1968 (and (or no-comment no-strings) (hfy-face-at start)))
1969 (if (listp face-at)
1970 (setq face-at (cadr (memq :inherit face-at))))
1971 (if (or (text-property-any start (1+ stop) 'hfy-linkp t)
1972 (and no-comment (eq 'font-lock-comment-face face-at))
1973 (and no-strings (eq 'font-lock-string-face face-at)))
1974 nil ;; already a link, NOOP
1976 ;; set a reverse map entry:
1977 (setq rmap-line (line-number-at-pos)
1978 rmap-entry (gethash TAG tags-rmap)
1979 rnew-elt (list file rmap-line start)
1980 rmap-entry (cons rnew-elt rmap-entry)
1981 name (format "%s.%d" TAG rmap-line))
1982 (put-text-property start (1+ start) 'hfy-inst name)
1983 (puthash TAG rmap-entry tags-rmap)
1985 ;; mark the link. link to index if the tag has > 1 def
1986 ;; add the line number to the #name if it does not:
1987 (setq href (hfy-href file tag-files TAG tag-map))
1988 (put-text-property start (1+ start) 'hfy-link href)
1989 (put-text-property stop (1+ stop ) 'hfy-endl t )
1990 (put-text-property start (1+ stop ) 'hfy-linkp t )))
1991 (goto-char tmp-point)) ))
1992 tags-list) )))
1994 (defun hfy-shell ()
1995 "Return `shell-file-name', or \"/bin/sh\" if it is a non-bourne shell."
1996 (if (string-match "\\<bash\\>\\|\\<sh\\>\\|\\<dash\\>" shell-file-name)
1997 shell-file-name
1998 (or hfy-shell-file-name "/bin/sh")))
2000 ;; cache the #(tag => file line point) entries for files under srcdir
2001 ;; and cache the descending sorted list of tags in the relevant alist,
2002 ;; also keyed by srcdir:
2003 (defun hfy-load-tags-cache (srcdir)
2004 "Run `hfy-etags-cmd' on SRCDIR, then call `hfy-parse-tags-buffer'."
2005 ;;(message "hfy-load-tags-cache");;DBUG
2006 (let ((etags-buffer (get-buffer-create "*hfy-tags*"))
2007 (etags-command (format hfy-etags-cmd hfy-etags-bin))
2008 (shell-file-name (hfy-shell)))
2009 (cd srcdir)
2010 (shell-command etags-command etags-buffer)
2011 (hfy-parse-tags-buffer srcdir etags-buffer)) )
2013 ;; break this out from `hfy-load-tags-cache' to make the tar file
2014 ;; functionality easier to implement.
2015 ;; ( tar file functionality not merged here because it requires a
2016 ;; hacked copy of etags capable of tagging stdin: if Francesco
2017 ;; Potorti accepts a patch, or otherwise implements stdin tagging,
2018 ;; then I will provide a `htmlfontify-tar-file' defun )
2019 (defun hfy-parse-tags-buffer (srcdir buffer)
2020 "Parse a BUFFER containing etags formatted output, loading the
2021 `hfy-tags-cache' and `hfy-tags-sortl' entries for SRCDIR."
2022 (let ((cache-entry (assoc srcdir hfy-tags-cache))
2023 (tlist-cache (assoc srcdir hfy-tags-sortl))
2024 (trmap-cache (assoc srcdir hfy-tags-rmap ))
2025 (cache-hash nil) (trmap-hash nil) (tags-list nil)
2026 (hash-entry nil) (tag-string nil) (tag-line nil)
2027 (tag-point nil) (new-entry nil) (etags-file nil))
2029 ;; (re)initialise the tag reverse map:
2030 (if trmap-cache (setq trmap-hash (cadr trmap-cache))
2031 (setq trmap-hash (make-hash-table :test 'equal))
2032 (setq hfy-tags-rmap (list (list srcdir trmap-hash) hfy-tags-rmap)))
2033 (clrhash trmap-hash)
2035 ;; (re)initialise the tag cache:
2036 (if cache-entry (setq cache-hash (cadr cache-entry))
2037 (setq cache-hash (make-hash-table :test 'equal))
2038 (setq hfy-tags-cache (list (list srcdir cache-hash) hfy-tags-cache)))
2039 (clrhash cache-hash)
2041 ;; cache the TAG => ((file line point) (file line point) ... ) entries:
2042 (with-current-buffer buffer
2043 (goto-char (point-min))
2045 (while (and (looking-at "^\x0c") (= 0 (forward-line 1)))
2046 ;;(message "^L boundary")
2047 (if (and (looking-at "^\\(.+\\),\\([0-9]+\\)$")
2048 (= 0 (forward-line 1)))
2049 (progn
2050 (setq etags-file (match-string 1))
2051 ;;(message "TAGS for file: %s" etags-file)
2052 (while (and (looking-at hfy-etag-regex) (= 0 (forward-line 1)))
2053 (setq tag-string (match-string 1))
2054 (if (= 0 (length tag-string)) nil ;; noop
2055 (setq tag-line (round (string-to-number (match-string 2))))
2056 (setq tag-point (round (string-to-number (match-string 3))))
2057 (setq hash-entry (gethash tag-string cache-hash))
2058 (setq new-entry (list etags-file tag-line tag-point))
2059 (push new-entry hash-entry)
2060 ;;(message "HASH-ENTRY %s %S" tag-string new-entry)
2061 (puthash tag-string hash-entry cache-hash)))) )))
2063 ;; cache a list of tags in descending length order:
2064 (maphash (lambda (K _V) (push K tags-list)) cache-hash)
2065 (setq tags-list (sort tags-list (lambda (A B) (< (length B) (length A)))))
2067 ;; put the tag list into the cache:
2068 (if tlist-cache (setcar (cdr tlist-cache) tags-list)
2069 (push (list srcdir tags-list) hfy-tags-sortl))
2071 ;; return the number of tags found:
2072 (length tags-list) ))
2074 (defun hfy-prepare-index-i (srcdir dstdir filename &optional stub map)
2075 "Prepare a tags index buffer for SRCDIR.
2076 `hfy-tags-cache' must already have an entry for SRCDIR for this to work.
2077 `hfy-page-header', `hfy-page-footer', `hfy-link-extn' and `hfy-extn'
2078 all play a part here.\n
2079 If STUB is set, prepare an (appropriately named) index buffer
2080 specifically for entries beginning with STUB.\n
2081 If MAP is set, use that instead of `hfy-tags-cache'.
2082 FILENAME is the name of the file being indexed.
2083 DSTDIR is the output directory, where files will be written."
2084 ;;(message "hfy-write-index");;DBUG
2085 (let ((cache-entry (assoc srcdir (or map hfy-tags-cache)))
2086 (cache-hash nil)
2087 (tag-list nil)
2088 (index-file
2089 (concat filename (if stub (concat "." stub) "") hfy-extn))
2090 (index-buf nil))
2091 (if (not (and cache-entry
2092 (setq cache-hash (cadr cache-entry))
2093 (setq index-buf (get-buffer-create index-file))))
2094 nil ;; noop
2095 (maphash (lambda (K _V) (push K tag-list)) cache-hash)
2096 (setq tag-list (sort tag-list 'string<))
2097 (set-buffer index-buf)
2098 (erase-buffer)
2099 (insert (funcall hfy-page-header filename "<!-- CSS -->"))
2100 (insert "<table class=\"index\">\n")
2102 (dolist (TAG tag-list)
2103 (let ((tag-started nil))
2104 (dolist (DEF (gethash TAG cache-hash))
2105 (if (and stub (not (string-match (concat "^" stub) TAG)))
2106 nil ;; we have a stub and it didn't match: NOOP
2107 (let ((file (car DEF))
2108 (line (cadr DEF)))
2109 (insert
2110 (format
2111 (concat
2112 " <tr> \n"
2113 " <td>%s</td> \n"
2114 " <td><a href=\"%s%s\">%s</a></td> \n"
2115 " <td><a href=\"%s%s#%s.%d\">%d</a></td>\n"
2116 " </tr> \n")
2117 (if (string= TAG tag-started) "&nbsp;"
2118 (format "<a name=\"%s\">%s</a>" TAG TAG))
2119 file (or hfy-link-extn hfy-extn) file
2120 file (or hfy-link-extn hfy-extn) TAG line line))
2121 (setq tag-started TAG))))))
2122 (insert "</table>\n")
2123 (insert (funcall hfy-page-footer filename))
2124 (and dstdir (cd dstdir))
2125 (set-visited-file-name index-file)
2126 index-buf) ))
2128 (defun hfy-prepare-index (srcdir dstdir)
2129 "Return a list of index buffer(s), as determined by `hfy-split-index'.
2130 SRCDIR and DSTDIR are the source and output directories respectively."
2131 (if (not hfy-split-index)
2132 (list (hfy-prepare-index-i srcdir dstdir hfy-index-file nil))
2133 (let ((stub-list nil)
2134 (cache-hash nil)
2135 (index-list nil)
2136 (cache-entry (assoc srcdir hfy-tags-cache)))
2137 (if (and cache-entry (setq cache-hash (cadr cache-entry)))
2138 (maphash
2139 (lambda (K _V)
2140 (let ((stub (upcase (substring K 0 1))))
2141 (if (member stub stub-list)
2142 nil ;; seen this already: NOOP
2143 (setq
2144 stub-list (cons stub stub-list)
2145 index-list (cons (hfy-prepare-index-i srcdir
2146 dstdir
2147 hfy-index-file
2148 stub)
2149 index-list)) )))
2150 cache-hash) )
2151 index-list)))
2153 (defun hfy-prepare-tag-map (srcdir dstdir)
2154 "Prepare the counterpart(s) to the index buffer(s) - a list of buffers
2155 with the same structure, but listing (and linking to) instances of tags
2156 \(as opposed to their definitions).\n
2157 SRCDIR and DSTDIR are the source and output directories respectively.
2158 See also `hfy-prepare-index', `hfy-split-index'."
2159 (if (not hfy-split-index)
2160 (list (hfy-prepare-index-i srcdir
2161 dstdir
2162 hfy-instance-file
2164 hfy-tags-rmap))
2165 (let ((stub-list nil)
2166 (cache-hash nil)
2167 (index-list nil)
2168 (cache-entry (assoc srcdir hfy-tags-rmap)))
2170 (if (and cache-entry (setq cache-hash (cadr cache-entry)))
2171 (maphash
2172 (lambda (K _V)
2173 (let ((stub (upcase (substring K 0 1))))
2174 (if (member stub stub-list)
2175 nil ;; seen this already: NOOP
2176 (setq
2177 stub-list (cons stub stub-list)
2178 index-list (cons (hfy-prepare-index-i srcdir
2179 dstdir
2180 hfy-instance-file
2181 stub
2182 hfy-tags-rmap)
2183 index-list)) )))
2184 cache-hash) )
2185 index-list)))
2187 (defun hfy-subtract-maps (srcdir)
2188 "Internal function - strips definitions of tags from the instance map.
2189 SRCDIR is the directory being \"published\".
2190 See also `hfy-tags-cache', `hfy-tags-rmap'."
2191 (let ((new-list nil)
2192 (old-list nil)
2193 (def-list nil)
2194 (exc-list nil)
2195 (fwd-map (cadr (assoc srcdir hfy-tags-cache)))
2196 (rev-map (cadr (assoc srcdir hfy-tags-rmap )))
2197 (taglist (cadr (assoc srcdir hfy-tags-sortl))))
2198 (dolist (TAG taglist)
2199 (setq def-list (gethash TAG fwd-map)
2200 old-list (gethash TAG rev-map)
2201 exc-list (mapcar (lambda (P) (list (car P) (cadr P))) def-list)
2202 new-list nil)
2203 (dolist (P old-list)
2204 (or (member (list (car P) (cadr P)) exc-list)
2205 (push P new-list)))
2206 (puthash TAG new-list rev-map))))
2208 (defun htmlfontify-run-etags (srcdir)
2209 "Load the etags cache for SRCDIR.
2210 See also `hfy-load-tags-cache'."
2211 (interactive "D source directory: ")
2212 (hfy-load-tags-cache (directory-file-name srcdir)))
2214 ;;(defun hfy-test-read-args (foo bar)
2215 ;; (interactive "D source directory: \nD target directory: ")
2216 ;; (message "foo: %S\nbar: %S" foo bar))
2218 (defun hfy-save-kill-buffers (buffer-list &optional dstdir)
2219 (dolist (B buffer-list)
2220 (set-buffer B)
2221 (and dstdir (file-directory-p dstdir) (cd dstdir))
2222 (save-buffer)
2223 (kill-buffer B)))
2225 ;;;###autoload
2226 (defun htmlfontify-copy-and-link-dir (srcdir dstdir &optional f-ext l-ext)
2227 "Trawl SRCDIR and write fontified-and-hyperlinked output in DSTDIR.
2228 F-EXT and L-EXT specify values for `hfy-extn' and `hfy-link-extn'.\n
2229 You may also want to set `hfy-page-header' and `hfy-page-footer'."
2230 (interactive "D source directory: \nD output directory: ")
2231 ;;(message "htmlfontify-copy-and-link-dir")
2232 (setq srcdir (directory-file-name srcdir))
2233 (setq dstdir (directory-file-name dstdir))
2234 (let ((source-files "SETME: list of source files, relative to srcdir")
2235 (tr-cache (assoc srcdir hfy-tags-rmap))
2236 (hfy-extn (or f-ext ".html"))
2237 (hfy-link-extn (or l-ext ".html")))
2238 ;; oops, forgot to load etags for srcdir:
2239 (if tr-cache nil
2240 (message "autoload of tags cache")
2241 (hfy-load-tags-cache srcdir)
2242 (setq tr-cache (assoc srcdir hfy-tags-rmap)))
2243 ;; clear out the old cache:
2244 (clrhash (cadr tr-cache))
2245 (hfy-make-directory dstdir)
2246 (setq source-files (hfy-list-files srcdir))
2247 (dolist (file source-files)
2248 (hfy-copy-and-fontify-file srcdir dstdir file))
2249 (hfy-subtract-maps srcdir)
2250 (hfy-save-kill-buffers (hfy-prepare-index srcdir dstdir) dstdir)
2251 (hfy-save-kill-buffers (hfy-prepare-tag-map srcdir dstdir) dstdir) ))
2253 ;; name of the init file we want:
2254 (defun hfy-initfile ()
2255 "Return the expected location of the htmlfontify specific init/custom file."
2256 (let* ((file (or (getenv "HFY_INITFILE") ".hfy.el")))
2257 (expand-file-name file "~") ))
2260 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2261 ;; incomplete as yet : transfer hook settings to hfy init file:
2262 ;; (defalias 'hfy-set-hooks 'custom-set-variables)
2264 ;; (defun hfy-pp-hook (H)
2265 ;; (and (string-match "-hook\\'" (symbol-name H))
2266 ;; (boundp H)
2267 ;; (symbol-value H)
2268 ;; (insert (format "\n '(%S %S)" H (symbol-value H)))
2269 ;; )
2270 ;; )
2272 ;; (defun hfy-save-hooks ()
2273 ;; (let ((custom-file (hfy-initfile)))
2274 ;; (custom-save-delete 'hfy-set-hooks)
2275 ;; (let ((standard-output (current-buffer)))
2276 ;; (princ "(hfy-set-hooks\n;;auto-generated, only one copy allowed\n")
2277 ;; (mapatoms 'hfy-pp-hook)
2278 ;; (insert "\n)")
2279 ;; )
2280 ;; )
2281 ;; )
2282 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2284 (defalias 'hfy-init-progn 'progn)
2286 (defun hfy-save-initvar (sym)
2287 (princ (format "(setq %s\n '" sym))
2288 (pp (symbol-value sym))
2289 (princ ")\n"))
2291 (defun htmlfontify-save-initfile ()
2292 "Save the htmlfontify settings to the htmlfontify init file."
2293 (interactive)
2294 (let* ((start-pos nil)
2295 (custom-file (hfy-initfile))
2296 (standard-output (find-file-noselect custom-file 'nowarn)))
2297 (save-excursion
2298 (custom-save-delete 'hfy-init-progn)
2299 (setq start-pos (point))
2300 (princ "(hfy-init-progn\n;;auto-generated, only one copy allowed\n")
2301 ;; FIXME: This saving&restoring of global customization
2302 ;; variables can interfere with other customization settings for
2303 ;; those vars (in .emacs or in Customize).
2304 (mapc 'hfy-save-initvar
2305 '(auto-mode-alist interpreter-mode-alist))
2306 (princ ")\n")
2307 (indent-region start-pos (point) nil))
2308 (custom-save-all) ))
2310 (defun htmlfontify-load-initfile ()
2311 "Load the htmlfontify specific init/custom file."
2312 (interactive)
2313 (let ((file (hfy-initfile)))
2314 (load file 'NOERROR nil nil) ))
2317 ;;;### (autoloads (hfy-fallback-colour-values htmlfontify-load-rgb-file)
2318 ;;;;;; "hfy-cmap" "hfy-cmap.el" "8dce008297f15826cc6ab82203c46fa6")
2319 ;;; Generated autoloads from hfy-cmap.el
2321 (autoload 'htmlfontify-load-rgb-file "hfy-cmap" "\
2322 Load an X11 style rgb.txt FILE.
2323 Search `hfy-rgb-load-path' if FILE is not specified.
2324 Loads the variable `hfy-rgb-txt-colour-map', which is used by
2325 `hfy-fallback-colour-values'.
2327 \(fn &optional FILE)" t nil)
2329 (autoload 'hfy-fallback-colour-values "hfy-cmap" "\
2330 Use a fallback method for obtaining the rgb values for a color.
2332 \(fn COLOUR-STRING)" nil nil)
2334 ;;;***
2337 (provide 'htmlfontify)
2339 ;; Local Variables:
2340 ;; coding: utf-8
2341 ;; End:
2343 ;;; htmlfontify.el ends here