Warn against using `length' to compute display width of a string.
[emacs.git] / lisp / htmlfontify.el
blobb94d4293fa724ef0303863e77c499acbc7f87698
1 ;;; htmlfontify.el --- htmlize a buffer/source tree with optional hyperlinks
3 ;; Copyright (C) 2002-2003, 2009-2012 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: htmlize 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 a string that 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 be large and take
170 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 flavor 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's 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 "Color" color )
436 (const :tag "Grayscale" 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 colors are no good - go fetch some X colors:
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 damnedest 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)
599 "Is option SYMBOL set."
600 (memq symbol hfy-optimisations))
602 (defun hfy-default-header (file style)
603 "Default value for `hfy-page-header'.
604 FILE is the name of the file.
605 STYLE is the inline CSS stylesheet (or tag referring to an external sheet)."
606 ;; (format "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
607 ;; <html>\n <head>\n <title>%s</title>\n %s\n </head>\n <body>\n" file style))
608 (format "<?xml version=\"1.0\" encoding=\"utf-8\"?>
609 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"
610 \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">
611 <html xmlns=\"http://www.w3.org/1999/xhtml\">
612 <head>
613 <title>%s</title>
615 <script type=\"text/javascript\"><!--
616 // this function is needed to work around
617 // a bug in IE related to element attributes
618 function hasClass(obj)
620 var result = false;
621 if (obj.getAttributeNode(\"class\") != null)
623 result = obj.getAttributeNode(\"class\").value;
625 return result;
628 function stripe(id)
630 // the flag we'll use to keep track of
631 // whether the current row is odd or even
632 var even = false;
634 // if arguments are provided to specify the colors
635 // of the even & odd rows, then use the them;
636 // otherwise use the following defaults:
637 var evenColor = arguments[1] ? arguments[1] : \"#fff\";
638 var oddColor = arguments[2] ? arguments[2] : \"#ddd\";
640 // obtain a reference to the desired table
641 // if no such table exists, abort
642 var table = document.getElementById(id);
643 if (! table) { return; }
645 // by definition, tables can have more than one tbody
646 // element, so we'll have to get the list of child
647 // &lt;tbody&gt;s
648 var tbodies = table.getElementsByTagName(\"tbody\");
650 // and iterate through them...
651 for (var h = 0; h < tbodies.length; h++)
653 // find all the &lt;tr&gt; elements...
654 var trs = tbodies[h].getElementsByTagName(\"tr\");
656 // ... and iterate through them
657 for (var i = 0; i < trs.length; i++)
659 // avoid rows that have a class attribute
660 // or backgroundColor style
661 if (! hasClass(trs[i]) &&
662 ! trs[i].style.backgroundColor)
664 // get all the cells in this row...
665 var tds = trs[i].getElementsByTagName(\"td\");
667 // and iterate through them...
668 for (var j = 0; j < tds.length; j++)
670 var mytd = tds[j];
672 // avoid cells that have a class attribute
673 // or backgroundColor style
674 if (! hasClass(mytd) &&
675 ! mytd.style.backgroundColor)
677 mytd.style.backgroundColor =
678 even ? evenColor : oddColor;
682 // flip from odd to even, or vice-versa
683 even = ! even;
688 function toggle_invis( name )
690 var filter =
691 { acceptNode:
692 function( node )
693 { var classname = node.id;
694 if( classname )
695 { var classbase = classname.substr( 0, name.length );
696 if( classbase == name ) { return NodeFilter.FILTER_ACCEPT; } }
697 return NodeFilter.FILTER_SKIP; } };
698 var walker = document.createTreeWalker( document.body ,
699 NodeFilter.SHOW_ELEMENT ,
700 filter ,
701 false );
702 while( walker.nextNode() )
704 var e = walker.currentNode;
705 if( e.style.display == \"none\" ) { e.style.display = \"inline\"; }
706 else { e.style.display = \"none\"; }
709 --> </script>
710 </head>
711 <body onload=\"stripe('index'); return true;\">\n"
712 file style))
714 (defun hfy-default-footer (_file)
715 "Default value for `hfy-page-footer'.
716 FILE is the name of the file being rendered, in case it is needed."
717 "\n </body>\n</html>\n")
719 (defun hfy-link-style-string (style-string)
720 "Replace the end of a CSS style declaration STYLE-STRING with the contents
721 of the variable `hfy-src-doc-link-style', removing text matching the regex
722 `hfy-src-doc-link-unstyle' first, if necessary."
723 ;;(message "hfy-colour-vals");;DBUG
724 (if (string-match hfy-src-doc-link-unstyle style-string)
725 (setq style-string (replace-match "" 'fixed-case 'literal style-string)))
726 (if (and (not (string-match hfy-src-doc-link-style style-string))
727 (string-match "} *$" style-string))
728 (concat (replace-match hfy-src-doc-link-style
729 'fixed-case
730 'literal
731 style-string) " }")
732 style-string))
734 ;; utility functions - cast emacs style specification values into their
735 ;; css2 equivalents:
736 (defun hfy-triplet (colour)
737 "Takes a COLOUR name (string) and return a CSS rgb(R, G, B) triplet string.
738 Uses the definition of \"white\" to map the numbers to the 0-255 range, so
739 if you've redefined white, (esp. if you've redefined it to have a triplet
740 member lower than that of the color you are processing) strange things
741 may happen."
742 ;;(message "hfy-colour-vals");;DBUG
743 (let ((white (mapcar (lambda (I) (float (1+ I))) (hfy-colour-vals "white")))
744 (rgb16 (mapcar (lambda (I) (float (1+ I))) (hfy-colour-vals colour))))
745 (if rgb16
746 ;;(apply 'format "rgb(%d, %d, %d)"
747 ;; Use #rrggbb instead, it is smaller
748 (apply 'format "#%02x%02x%02x"
749 (mapcar (lambda (X)
750 (* (/ (nth X rgb16)
751 (nth X white)) 255))
752 '(0 1 2))))))
754 (defun hfy-family (family) (list (cons "font-family" family)))
755 (defun hfy-bgcol (colour) (list (cons "background" (hfy-triplet colour))))
756 (defun hfy-colour (colour) (list (cons "color" (hfy-triplet colour))))
757 (defun hfy-width (width) (list (cons "font-stretch" (symbol-name width))))
759 (defcustom hfy-font-zoom 1.05
760 "Font scaling from Emacs to HTML."
761 :type 'float
762 :group 'htmlfontify)
764 (defun hfy-size (height)
765 "Derive a CSS font-size specifier from an Emacs font :height attribute HEIGHT.
766 Does not cope with the case where height is a function to be applied to
767 the height of the underlying font."
768 (list
769 (cond
770 ;;(t (cons "font-size" ": 1em"))
771 ((floatp height)
772 (cons "font-size" (format "%d%%" (* (* hfy-font-zoom height) 100))))
773 ((integerp height)
774 (cons "font-size" (format "%dpt" (/ (* hfy-font-zoom height) 10 )))) )) )
776 (defun hfy-slant (slant)
777 "Derive a font-style CSS specifier from the Emacs :slant attribute SLANT:
778 CSS does not define the reverse-* styles, so just maps those to the
779 regular specifiers."
780 (list (cons "font-style"
781 (or (cdr (assq slant '((italic . "italic")
782 (reverse-italic . "italic" )
783 (oblique . "oblique")
784 (reverse-oblique . "oblique"))))
785 "normal"))))
787 (defun hfy-weight (weight)
788 "Derive a font-weight CSS specifier from an Emacs weight spec symbol WEIGHT."
789 (list (cons "font-weight" (cdr (assq weight '((ultra-bold . "900")
790 (extra-bold . "800")
791 (bold . "700")
792 (semi-bold . "600")
793 (normal . "500")
794 (semi-light . "400")
795 (light . "300")
796 (extra-light . "200")
797 (ultra-light . "100")))))))
799 (defun hfy-box-to-border-assoc (spec)
800 (if spec
801 (let ((tag (car spec))
802 (val (cadr spec)))
803 (cons (case tag
804 (:color (cons "colour" val))
805 (:width (cons "width" val))
806 (:style (cons "style" val)))
807 (hfy-box-to-border-assoc (cddr spec))))))
809 (defun hfy-box-to-style (spec)
810 (let* ((css (hfy-box-to-border-assoc spec))
811 (col (cdr (assoc "colour" css)))
812 (s (cdr (assoc "style" css))))
813 (list
814 (if col (cons "border-color" (cdr (assoc "colour" css))))
815 (cons "border-width" (format "%dpx" (or (cdr (assoc "width" css)) 1)))
816 (cons "border-style" (case s
817 (released-button "outset")
818 (pressed-button "inset" )
819 (t "solid" ))))))
821 (defun hfy-box (box)
822 "Derive CSS border-* attributes from the Emacs :box attribute BOX."
823 (if box
824 (cond
825 ((integerp box) (list (cons "border-width" (format "%dpx" box))))
826 ((stringp box) (list (cons "border" (format "solid %s 1px" box))))
827 ((listp box) (hfy-box-to-style box) ))) )
829 (defun hfy-decor (tag _val)
830 "Derive CSS text-decoration specifiers from various Emacs font attributes.
831 TAG is an Emacs font attribute key (eg :underline).
832 VAL is ignored."
833 (list
834 ;; FIXME: Why not '("text-decoration" . "underline")? --Stef
835 (case tag
836 (:underline (cons "text-decoration" "underline" ))
837 (:overline (cons "text-decoration" "overline" ))
838 (:strike-through (cons "text-decoration" "line-through")))))
840 (defun hfy-invisible (&optional _val)
841 "This text should be invisible.
842 Do something in CSS to make that happen.
843 VAL is ignored here."
844 '(("display" . "none")))
846 (defun hfy-combined-face-spec (face)
847 "Return a `defface' style alist of possible specifications for FACE.
848 Entries resulting from customization (`custom-set-faces') will take
849 precedence."
850 (append
851 (if (and hfy-display-class hfy-default-face-def (eq face 'default))
852 hfy-default-face-def)
853 (get face 'saved-face)
854 (get face 'face-defface-spec)))
856 (defun hfy-face-attr-for-class (face &optional class)
857 "Return the face attributes for FACE.
858 If CLASS is set, it must be a `defface' alist key [see below],
859 in which case the first face specification returned by `hfy-combined-face-spec'
860 which *doesn't* clash with CLASS is returned.\n
861 \(A specification with a class of t is considered to match any class you
862 specify - this matches Emacs's behavior when deciding on which face attributes
863 to use, to the best of my understanding).\n
864 If CLASS is nil, then you just get whatever `face-attr-construct' returns,
865 ie the current specification in effect for FACE.\n
866 *NOTE*: This function forces any face that is not 'default and which has
867 no :inherit property to inherit from 'default (this is because 'default
868 is magical in that Emacs's fonts behave as if they inherit implicitly from
869 'default, but no such behavior exists in HTML/CSS).\n
870 See also `hfy-display-class' for details of valid values for CLASS."
871 (let ((face-spec
872 (if class
873 (let ((face-props (hfy-combined-face-spec face))
874 (face-specn nil)
875 (face-class nil)
876 (face-attrs nil)
877 (face-score -1)
878 (face-match nil))
879 (while face-props
880 (setq face-specn (car face-props)
881 face-class (car face-specn)
882 face-attrs (cdr face-specn)
883 face-props (cdr face-props))
884 ;; if the current element CEL of CLASS is t we match
885 ;; if the current face-class is t, we match
886 ;; if the cdr of CEL has a non-nil
887 ;; intersection with the cdr of the first member of
888 ;; the current face-class with the same car as CEL, we match
889 ;; if we actually clash, then we can't match
890 (let ((cbuf class)
891 (cel nil)
892 (key nil)
893 (val nil)
894 (x nil)
895 (next nil)
896 (score 0))
897 (while (and cbuf (not next))
898 (setq cel (car cbuf)
899 cbuf (cdr cbuf)
900 key (car cel)
901 val (cdr cel)
902 val (if (listp val) val (list val)))
903 (cond
904 ((or (eq cel t)
905 (memq face-class '(t default))) ;Default match.
906 (setq score 0) (ignore "t match"))
907 ((not (cdr (assq key face-class))) ;Neither good nor bad.
908 nil (ignore "non match, non collision"))
909 ((setq x (hfy-interq val (cdr (assq key face-class))))
910 (setq score (+ score (length x)))
911 (ignore "intersection"))
912 (t ;; nope.
913 (setq next t score -10) (ignore "collision")) ))
914 (if (> score face-score)
915 (progn
916 (setq face-match face-attrs
917 face-score score )
918 (ignore "%d << %S/%S" score face-class class))
919 (ignore "--- %d ---- (insufficient)" score)) ))
920 ;; matched ? last attrs : nil
921 (if face-match
922 (if (listp (car face-match)) (car face-match) face-match)
923 nil))
924 ;; Unfortunately the default face returns a
925 ;; :background. Fortunately we can remove it, but how do we do
926 ;; that in a non-system specific way?
927 (let ((spec (face-attr-construct face))
928 (new-spec nil))
929 (if (not (memq :background spec))
930 spec
931 (while spec
932 (let ((a (nth 0 spec))
933 (b (nth 1 spec)))
934 (unless (and (eq a :background)
935 (stringp b)
936 (string= b "SystemWindow"))
937 (setq new-spec (cons a (cons b new-spec)))))
938 (setq spec (cddr spec)))
939 new-spec)))))
940 (if (or (memq :inherit face-spec) (eq 'default face))
941 face-spec
942 (append face-spec (list :inherit 'default)))))
944 ;; construct an assoc of (css-tag-name . css-tag-value) pairs
945 ;; from a face or assoc of face attributes:
947 ;; Some tests etc:
948 ;; (mumamo-message-with-face "testing face" 'highlight)
949 ;; (mumamo-message-with-face "testing face" '(:foreground "red" :background "yellow"))
950 ;; (hfy-face-to-style-i '(:inherit default foreground-color "red"))
951 ;; default face=(:stipple nil :background "SystemWindow" :foreground
952 ;; "SystemWindowText" :inverse-video nil :box nil :strike-through
953 ;; nil :overline nil :underline nil :slant normal :weight normal
954 ;; :height 98 :width normal :family "outline-courier new")
955 (defun hfy-face-to-style-i (fn)
956 "The guts of `hfy-face-to-style': FN should be a `defface' font spec,
957 as returned by `face-attr-construct' or `hfy-face-attr-for-class'.
958 Note that this function does not get font-sizes right if they are based
959 on inherited modifiers (via the :inherit) attribute, and any other
960 modifiers that are cumulative if they appear multiple times need to be
961 merged by the user - `hfy-flatten-style' should do this."
962 ;;(message "hfy-face-to-style-i");;DBUG
964 ;; fn's value could be something like
965 ;; (:inherit
966 ;; ((foreground-color . "blue"))
967 ;; (foreground-color . "blue")
968 ;; nil)
970 (when fn
971 (let ((key (car fn))
972 (val (cadr fn))
973 (next (cddr fn))
974 (that nil)
975 (this nil)
976 (parent nil))
977 (if (eq key :inherit)
978 (let ((vs (if (listp val) val (list val))))
979 ;; (let ((x '(a b))) (setq x (append '(c d) x)))
980 ;; (let ((x '(a b))) (setq x (append '(c d) x)))
981 (dolist (v vs)
982 (setq parent
983 (append
984 parent
985 (hfy-face-to-style-i
986 (hfy-face-attr-for-class v hfy-display-class)) ))))
987 (setq this
988 (if val (case key
989 (:family (hfy-family val))
990 (:width (hfy-width val))
991 (:weight (hfy-weight val))
992 (:slant (hfy-slant val))
993 (:foreground (hfy-colour val))
994 (:background (hfy-bgcol val))
995 (:box (hfy-box val))
996 (:height (hfy-size val))
997 (:underline (hfy-decor key val))
998 (:overline (hfy-decor key val))
999 (:strike-through (hfy-decor key val))
1000 (:invisible (hfy-invisible val))
1001 (:bold (hfy-weight 'bold))
1002 (:italic (hfy-slant 'italic))))))
1003 (setq that (hfy-face-to-style-i next))
1004 ;;(lwarn t :warning "%S => %S" fn (nconc this that parent))
1005 (nconc this that parent))) )
1007 (defun hfy-size-to-int (spec)
1008 "Convert SPEC, a CSS font-size specifier, to an Emacs :height attribute value.
1009 Used while merging multiple font-size attributes."
1010 ;;(message "hfy-size-to-int");;DBUG
1011 (list
1012 (if (string-match "\\([0-9]+\\)\\(%\\|pt\\)" spec)
1013 (cond ((string= "%" (match-string 2 spec))
1014 (/ (string-to-number (match-string 1 spec)) 100.0))
1015 ((string= "pt" (match-string 2 spec))
1016 (* (string-to-number (match-string 1 spec)) 10)))
1017 (string-to-number spec))) )
1019 ;; size is different, in that in order to get it right at all,
1020 ;; we have to trawl the inheritance path, accumulating modifiers,
1021 ;; _until_ we get to an absolute (pt) specifier, then combine the lot
1022 (defun hfy-flatten-style (style)
1023 "Take STYLE (see `hfy-face-to-style-i', `hfy-face-to-style') and merge
1024 any multiple attributes appropriately. Currently only font-size is merged
1025 down to a single occurrence - others may need special handling, but I
1026 haven't encountered them yet. Returns a `hfy-style-assoc'."
1027 ;;(message "(hfy-flatten-style %S)" style) ;;DBUG
1028 (let ((n 0)
1029 (m (list 1))
1030 (x nil)
1031 (r nil))
1032 (dolist (css style)
1033 (if (string= (car css) "font-size")
1034 (progn
1035 (when (not x) (setq m (nconc m (hfy-size-to-int (cdr css)))))
1036 (when (string-match "pt" (cdr css)) (setq x t)))
1037 (setq r (nconc r (list css)))))
1038 ;;(message "r: %S" r)
1039 (setq n (apply '* m))
1040 (nconc r (hfy-size (if x (round n) (* n 1.0)))) ))
1042 (defun hfy-face-resolve-face (fn)
1043 (cond
1044 ((facep fn)
1045 (hfy-face-attr-for-class fn hfy-display-class))
1046 ((and (symbolp fn)
1047 (facep (symbol-value fn)))
1048 ;; Obsolete faces like `font-lock-reference-face' are defined as
1049 ;; aliases for another face.
1050 (hfy-face-attr-for-class (symbol-value fn) hfy-display-class))
1051 (t nil)))
1054 (defun hfy-face-to-style (fn)
1055 "Take FN, a font or `defface' style font specification,
1056 \(as returned by `face-attr-construct' or `hfy-face-attr-for-class')
1057 and return a `hfy-style-assoc'.\n
1058 See also `hfy-face-to-style-i', `hfy-flatten-style'."
1059 ;;(message "hfy-face-to-style");;DBUG
1060 (let* ((face-def (hfy-face-resolve-face fn))
1061 (final-style
1062 (hfy-flatten-style (hfy-face-to-style-i face-def))))
1063 ;;(message "%S" final-style)
1064 (if (not (assoc "text-decoration" final-style))
1065 (progn (setq final-style
1066 ;; Fix-me: there is no need for this since
1067 ;; text-decoration is not inherited.
1068 ;; but it's not wrong and if this ever changes it will
1069 ;; be needed, so I think it's better to leave it in? -- v
1070 (nconc final-style '(("text-decoration" . "none"))))))
1071 final-style))
1073 ;; strip redundant bits from a name. Technically, this could result in
1074 ;; a collision, but it is pretty unlikely - will fix later...
1075 ;; also handle ephemeral fonts created by overlays, which don't actually
1076 ;; have names:
1077 (defun hfy-face-or-def-to-name (fn)
1078 "Render a font symbol or `defface' font spec FN into a name (string)."
1079 ;;(message "generating name for %s" fn)
1080 (if (not (listp fn))
1081 (format "%s" fn)
1082 (let* ((key (format "%s" fn))
1083 (entry (assoc key hfy-tmpfont-stack))
1084 (base (cadr (memq :inherit fn)))
1085 (tag (cdr entry)))
1086 ;;(message "checking for key «%s» in font stack [%d]"
1087 ;; key (if entry 1 0))
1088 (if entry nil ;; noop
1089 (setq tag (format "%04d" (length hfy-tmpfont-stack))
1090 entry (cons key tag)
1091 hfy-tmpfont-stack (cons entry hfy-tmpfont-stack)))
1092 ;;(message " -> name: %s-%s" (or base 'default) tag)
1093 (format "%s-%s" (or base 'default) tag)) ))
1095 (defun hfy-css-name (fn)
1096 "Strip the boring bits from a font-name FN and return a CSS style name."
1097 ;;(message "hfy-css-name");;DBUG
1098 (let ((face-name (hfy-face-or-def-to-name fn)))
1099 (if (or (string-match "font-lock-\\(.*\\)" face-name)
1100 (string-match "cperl-\\(.*\\)" face-name)
1101 (string-match "^[Ii]nfo-\\(.*\\)" face-name))
1102 (progn
1103 (setq face-name (match-string 1 face-name))
1104 (if (string-match "\\(.*\\)-face\\'" face-name)
1105 (setq face-name (match-string 1 face-name)))
1106 face-name)
1107 face-name)) )
1109 ;; construct an assoc of (stripped-name . "{ css-stuff-here }") pairs
1110 ;; from a face:
1111 (defun hfy-face-to-css (fn)
1112 "Take FN, a font or `defface' specification (cf `face-attr-construct')
1113 and return a CSS style specification.\n
1114 See also `hfy-face-to-style'."
1115 ;;(message "hfy-face-to-css");;DBUG
1116 (let* ((css-list (hfy-face-to-style fn))
1117 (seen nil)
1118 (css-text
1119 (mapcar
1120 (lambda (E)
1121 (if (car E)
1122 (unless (member (car E) seen)
1123 (push (car E) seen)
1124 (format " %s: %s; " (car E) (cdr E)))))
1125 css-list)))
1126 (cons (hfy-css-name fn) (format "{%s}" (apply 'concat css-text)))) )
1128 (defalias 'hfy-prop-invisible-p
1129 (if (fboundp 'invisible-p) #'invisible-p
1130 (lambda (prop)
1131 "Is text property PROP an active invisibility property?"
1132 (or (and (eq buffer-invisibility-spec t) prop)
1133 (or (memq prop buffer-invisibility-spec)
1134 (assq prop buffer-invisibility-spec))))))
1136 (defun hfy-find-invisible-ranges ()
1137 "Return a list of (start-point . end-point) cons cells of invisible regions."
1138 (save-excursion
1139 (let (invisible p i s) ;; return-value pos invisible end start
1140 (setq p (goto-char (point-min)))
1141 (when (invisible-p p) (setq s p i t))
1142 (while (< p (point-max))
1143 (if i ;; currently invisible
1144 (when (not (invisible-p p)) ;; but became visible
1145 (setq i nil
1146 invisible (cons (cons s p) invisible)))
1147 ;; currently visible:
1148 (when (invisible-p p) ;; but have become invisible
1149 (setq s p i t)))
1150 (setq p (next-char-property-change p)))
1151 ;; still invisible at buffer end?
1152 (when i
1153 (setq invisible (cons (cons s (point-max)) invisible)))
1154 invisible)))
1156 (defun hfy-invisible-name (point map)
1157 "Generate a CSS style name for an invisible section of the buffer.
1158 POINT is the point inside the invisible region.
1159 MAP is the invisibility map as returned by `hfy-find-invisible-ranges'."
1160 ;;(message "(hfy-invisible-name %S %S)" point map)
1161 (let (name)
1162 (dolist (range map)
1163 (when (and (>= point (car range))
1164 (< point (cdr range)))
1165 (setq name (format "invisible-%S-%S" (car range) (cdr range)))))
1166 name))
1168 ;; Fix-me: This function needs some cleanup by someone who understand
1169 ;; all the formats that face properties can have.
1171 ;; overlay handling should be fine. haven't tested multiple stacked overlapping
1172 ;; overlays recently, but the common case of a text property face + an overlay
1173 ;; face produces the correct merged css style (or as close to it as css can get)
1174 ;; -- v
1175 (defun hfy-face-at (p)
1176 "Find face in effect at point P.
1177 If overlays are to be considered (see `hfy-optimisations') then this may
1178 return a `defface' style list of face properties instead of a face symbol."
1179 ;;(message "hfy-face-at");;DBUG
1180 ;; Fix-me: clean up, remove face-name etc
1181 ;; not sure why we'd want to remove face-name? -- v
1182 (let ((overlay-data nil)
1183 (base-face nil)
1184 (face-name (get-text-property p 'face))
1185 ;; (face-name (hfy-get-face-at p))
1186 (prop-seen nil)
1187 (extra-props nil)
1188 (text-props (text-properties-at p)))
1189 ;;(message "face-name: %S" face-name)
1190 (when (and face-name (listp face-name) (facep (car face-name)))
1191 ;;(message "face-name is a list %S" face-name)
1192 ;;(setq text-props (cons 'face face-name))
1193 (dolist (f face-name)
1194 (setq extra-props (if (listp f)
1195 ;; for things like (variable-pitch
1196 ;; (:foreground "red"))
1197 (cons f extra-props)
1198 (cons :inherit (cons f extra-props)))))
1199 (setq base-face (car face-name)
1200 face-name nil))
1201 ;; text-properties-at => (face (:foreground "red" ...))
1202 ;; or => (face (compilation-info underline)) list of faces
1203 ;; overlay-properties
1204 ;; format= (evaporate t face ((foreground-color . "red")))
1206 ;; SO: if we have turned overlays off,
1207 ;; or if there's no overlay data
1208 ;; just bail out and return whatever face data we've accumulated so far
1209 (if (or (not (hfy-opt 'keep-overlays))
1210 (not (setq overlay-data (hfy-overlay-props-at p))))
1211 (progn
1212 ;;(message "· %d: %s; %S; %s"
1213 ;; p face-name extra-props text-props)
1214 (or face-name base-face)) ;; no overlays or extra properties
1215 ;; collect any face data and any overlay data for processing:
1216 (when text-props
1217 (push text-props overlay-data))
1218 (setq overlay-data (nreverse overlay-data))
1219 ;;(message "- %d: %s; %S; %s; %s"
1220 ;; p face-name extra-props text-props overlay-data)
1221 ;; remember the basic face name so we don't keep repeating its specs:
1222 (when face-name (setq base-face face-name))
1223 (dolist (P overlay-data)
1224 (let ((iprops (cadr (memq 'invisible P)))) ;FIXME: plist-get?
1225 ;;(message "(hfy-prop-invisible-p %S)" iprops)
1226 (when (and iprops (hfy-prop-invisible-p iprops))
1227 (setq extra-props
1228 (cons :invisible (cons t extra-props))) ))
1229 (let ((fprops (cadr (or (memq 'face P)
1230 (memq 'font-lock-face P)))))
1231 ;;(message "overlay face: %s" fprops)
1232 (if (not (listp fprops))
1233 (let ((this-face (if (stringp fprops) (intern fprops) fprops)))
1234 (when (not (eq this-face base-face))
1235 (setq extra-props
1236 (cons :inherit
1237 (cons this-face extra-props))) ))
1238 (while fprops
1239 (if (facep (car fprops))
1240 (let ((face (car fprops)))
1241 (when (stringp face) (setq face (intern fprops)))
1242 (setq extra-props
1243 (cons :inherit
1244 (cons face
1245 extra-props)))
1246 (setq fprops (cdr fprops)))
1247 (let (p v)
1248 ;; Sigh.
1249 (if (listp (car fprops))
1250 (if (nlistp (cdr (car fprops)))
1251 (progn
1252 ;; ((prop . val))
1253 (setq p (caar fprops))
1254 (setq v (cdar fprops))
1255 (setq fprops (cdr fprops)))
1256 ;; ((prop val))
1257 (setq p (caar fprops))
1258 (setq v (cadar fprops))
1259 (setq fprops (cdr fprops)))
1260 (if (listp (cdr fprops))
1261 (progn
1262 ;; (:prop val :prop val ...)
1263 (setq p (car fprops))
1264 (setq v (cadr fprops))
1265 (setq fprops (cddr fprops)))
1266 (if (and (listp fprops)
1267 (not (listp (cdr fprops))))
1268 ;;(and (consp x) (cdr (last x)))
1269 (progn
1270 ;; (prop . val)
1271 (setq p (car fprops))
1272 (setq v (cdr fprops))
1273 (setq fprops nil))
1274 (error "Eh... another format! fprops=%s" fprops) )))
1275 (setq p (case p
1276 ;; These are all the properties handled
1277 ;; in `hfy-face-to-style-i'.
1279 ;; Are these translations right?
1280 ;; yes, they are -- v
1281 (family :family )
1282 (width :width )
1283 (height :height )
1284 (weight :weight )
1285 (slant :slant )
1286 (underline :underline )
1287 (overline :overline )
1288 (strike-through :strike-through)
1289 (box :box )
1290 (foreground-color :foreground)
1291 (background-color :background)
1292 (bold :bold )
1293 (italic :italic )
1294 (t p)))
1295 (if (memq p prop-seen) nil ;; noop
1296 (setq prop-seen (cons p prop-seen)
1297 extra-props (cons p (cons v extra-props))))))))))
1298 ;;(message "+ %d: %s; %S" p face-name extra-props)
1299 (if extra-props
1300 (nconc extra-props (if (listp face-name)
1301 face-name
1302 (face-attr-construct face-name)))
1303 face-name)) ))
1305 (defun hfy-overlay-props-at (p)
1306 "Grab overlay properties at point P.
1307 The plists are returned in descending priority order."
1308 (sort (mapcar #'overlay-properties (overlays-at p))
1309 (lambda (A B) (> (or (cadr (memq 'priority A)) 0) ;FIXME: plist-get?
1310 (or (cadr (memq 'priority B)) 0)))))
1312 ;; construct an assoc of (face-name . (css-name . "{ css-style }")) elements:
1313 (defun hfy-compile-stylesheet ()
1314 "Trawl the current buffer, construct and return a `hfy-sheet-assoc'."
1315 ;;(message "hfy-compile-stylesheet");;DBUG
1316 (let ((pt (point-min))
1317 ;; Make the font stack stay:
1318 ;;(hfy-tmpfont-stack nil)
1319 (fn nil)
1320 (style nil))
1321 (save-excursion
1322 (goto-char pt)
1323 (while (< pt (point-max))
1324 (if (and (setq fn (hfy-face-at pt)) (not (assoc fn style)))
1325 (push (cons fn (hfy-face-to-css fn)) style))
1326 (setq pt (next-char-property-change pt))) )
1327 (push (cons 'default (hfy-face-to-css 'default)) style)))
1329 (defun hfy-fontified-p ()
1330 "`font-lock' doesn't like to say it's been fontified when in batch
1331 mode, but we want to know if we should fontify or raw copy, so in batch
1332 mode we check for non-default face properties. Otherwise we test
1333 variable `font-lock-mode' and variable `font-lock-fontified' for truth."
1334 ;;(message "font-lock-fontified: %S" font-lock-fontified)
1335 ;;(message "noninteractive : %S" noninteractive)
1336 ;;(message "font-lock-mode : %S" font-lock-mode)
1337 (and font-lock-fontified
1338 (if noninteractive
1339 (let ((pt (point-min))
1340 (face-name nil))
1341 (save-excursion
1342 (goto-char pt)
1343 (while (and (< pt (point-max)) (not face-name))
1344 (setq face-name (hfy-face-at pt))
1345 (setq pt (next-char-property-change pt))))
1346 face-name)
1347 font-lock-mode)))
1349 ;; remember, the map is in reverse point order:
1350 ;; I wrote this while suffering the effects of a cold, and maybe a
1351 ;; mild fever - I think it's correct, but it might be a little warped
1352 ;; as my minfd keeps ... where was I? Oh yes, the bunnies...
1353 (defun hfy-merge-adjacent-spans (face-map)
1354 "Where FACE-MAP is a `hfy-facemap-assoc' for the current buffer,
1355 this function merges adjacent style blocks which are of the same value
1356 and are separated by nothing more interesting than whitespace.\n
1357 <span class=\"foo\">narf</span> <span class=\"foo\">brain</span>\n
1358 \(as interpreted from FACE-MAP) would become:\n
1359 <span class=\"foo\">narf brain</span>\n
1360 Returns a modified copy of FACE-MAP."
1361 (let ((tmp-map face-map)
1362 (map-buf nil)
1363 (first-start nil)
1364 (first-stop nil)
1365 (last-start nil)
1366 (last-stop nil)
1367 (span-stop nil)
1368 (span-start nil)
1369 (reduced-map nil))
1370 ;;(push (car tmp-map) reduced-map)
1371 ;;(push (cadr tmp-map) reduced-map)
1372 (while tmp-map
1373 (setq first-start (cadddr tmp-map)
1374 first-stop (caddr tmp-map)
1375 last-start (cadr tmp-map)
1376 last-stop (car tmp-map)
1377 map-buf tmp-map
1378 span-start last-start
1379 span-stop last-stop )
1380 (while (and (equal (cdr first-start)
1381 (cdr last-start))
1382 (save-excursion
1383 (goto-char (car first-stop))
1384 (not (re-search-forward "[^ \t\n\r]" (car last-start) t))))
1385 (setq map-buf (cddr map-buf)
1386 span-start first-start
1387 first-start (cadddr map-buf)
1388 first-stop (caddr map-buf)
1389 last-start (cadr map-buf)
1390 last-stop (car map-buf)))
1391 (push span-stop reduced-map)
1392 (push span-start reduced-map)
1393 (setq tmp-map (memq last-start tmp-map))
1394 (setq tmp-map (cdr tmp-map)))
1395 (setq reduced-map (nreverse reduced-map))))
1397 ;; remember to generate 'synthetic' </span> entries -
1398 ;; emacs copes by just having a stack of styles in effect
1399 ;; and only using the top one: html has a more simplistic approach -
1400 ;; we have to explicitly end a style, there's no way of temporarily
1401 ;; overriding it w. another one... (afaik)
1402 (defun hfy-compile-face-map ()
1403 ;; no need for special <a> version.
1404 ;; IME hyperlinks don't get underlined, esp when you htmlfontify a whole
1405 ;; source tree, so the <a> version is needed -- v
1406 ;; Fix-me: save table for multi-buffer
1407 "Compile and return a `hfy-facemap-assoc' for the current buffer."
1408 ;;(message "hfy-compile-face-map");;DBUG
1409 (let* ((pt (point-min))
1410 (pt-narrow (save-restriction (widen) (point-min)))
1411 (offset (- pt pt-narrow))
1412 (fn nil)
1413 (map nil)
1414 (prev-tag nil)) ;; t if the last tag-point was a span-start
1415 ;; nil if it was a span-stop
1416 (save-excursion
1417 (goto-char pt)
1418 (while (< pt (point-max))
1419 (if (setq fn (hfy-face-at pt))
1420 (progn (if prev-tag (push (cons pt-narrow 'end) map))
1421 (push (cons pt-narrow fn) map)
1422 (setq prev-tag t))
1423 (if prev-tag (push (cons pt-narrow 'end) map))
1424 (setq prev-tag nil))
1425 (setq pt (next-char-property-change pt))
1426 (setq pt-narrow (+ offset pt)))
1427 (if (and map (not (eq 'end (cdar map))))
1428 (push (cons (- (point-max) (point-min)) 'end) map)))
1429 (if (hfy-opt 'merge-adjacent-tags) (hfy-merge-adjacent-spans map) map)))
1431 (defun hfy-buffer ()
1432 "Generate a buffer to hold the HTML output.
1433 The filename of this buffer is derived from the source (current) buffer's
1434 variable `buffer-file-name', if it is set, plus `hfy-extn'.
1435 Otherwise a plausible filename is constructed from `default-directory',
1436 `buffer-name' and `hfy-extn'."
1437 (let* ((name (concat (buffer-name) hfy-extn))
1438 (src (buffer-file-name))
1439 (buf (get-buffer-create name)))
1440 (with-current-buffer buf
1441 (setq buffer-file-name
1442 (if src (concat src hfy-extn)
1443 (expand-file-name (if (string-match "^.*/\\([^/]*\\)\\'" name)
1444 (match-string 1 name)
1445 name))))
1446 buf)))
1448 (defun hfy-lookup (face style)
1449 "Get a CSS style name for FACE from STYLE."
1450 (cadr (assoc face style)))
1452 (defun hfy-link-style (style-string)
1453 "Copy, alter and return a STYLE-STRING to make it suitable for a hyperlink.
1454 Uses `hfy-link-style-fun' to do this."
1455 (if (functionp hfy-link-style-fun)
1456 (funcall hfy-link-style-fun style-string)
1457 style-string))
1459 (defun hfy-sprintf-stylesheet (css file)
1460 "Return the inline CSS style sheet for FILE as a string."
1461 (let ((stylesheet
1462 (concat
1463 hfy-meta-tags
1464 "\n<style type=\"text/css\"><!-- \n"
1465 ;; Fix-me: Add handling of page breaks here + scan for ^L
1466 ;; where appropriate.
1467 (format "body %s\n" (cddr (assq 'default css)))
1468 (apply 'concat
1469 (mapcar
1470 (lambda (style)
1471 (format
1472 "span.%s %s\nspan.%s a %s\n"
1473 (cadr style) (cddr style)
1474 (cadr style) (hfy-link-style (cddr style))))
1475 css))
1476 " --></style>\n")))
1477 (funcall hfy-page-header file stylesheet)))
1479 ;; tag all the dangerous characters we want to escape
1480 ;; (ie any "<> chars we _didn't_ put there explicitly for css markup)
1481 (defun hfy-html-enkludge-buffer ()
1482 "Mark dangerous [\"<>] characters with the `hfy-quoteme' property.\n
1483 See also `hfy-html-dekludge-buffer'."
1484 ;;(message "hfy-html-enkludge-buffer");;DBUG
1485 (save-excursion
1486 (goto-char (point-min))
1487 (while (re-search-forward hfy-html-quote-regex nil t)
1488 (put-text-property (match-beginning 0) (point) 'hfy-quoteme t))) )
1490 ;; dangerous char -> &entity;
1491 (defun hfy-html-quote (char-string)
1492 "Map CHAR-STRING to an HTML safe string (entity) if need be."
1493 ;;(message "hfy-html-quote");;DBUG
1494 (or (cadr (assoc char-string hfy-html-quote-map)) char-string) )
1496 ;; actually entity-ise dangerous chars.
1497 ;; note that we can't do this until _after_ we have inserted the css
1498 ;; markup, since we use a position-based map to insert this, and if we
1499 ;; enter any other text before we do this, we'd have to track another
1500 ;; map of offsets, which would be tedious...
1501 (defun hfy-html-dekludge-buffer ()
1502 "Transform all dangerous characters marked with the `hfy-quoteme' property
1503 using `hfy-html-quote'.\n
1504 See also `hfy-html-enkludge-buffer'."
1505 ;;(message "hfy-html-dekludge-buffer");;DBUG
1506 (save-excursion
1507 (goto-char (point-min))
1508 (while (re-search-forward hfy-html-quote-regex nil t)
1509 (if (get-text-property (match-beginning 0) 'hfy-quoteme)
1510 (replace-match (hfy-html-quote (match-string 1))) )) ))
1512 ;; Borrowed from font-lock.el
1513 (defmacro hfy-save-buffer-state (varlist &rest body)
1514 "Bind variables according to VARLIST and eval BODY restoring buffer state.
1515 Do not record undo information during evaluation of BODY."
1516 (declare (indent 1) (debug let))
1517 (let ((modified (make-symbol "modified")))
1518 `(let* ,(append varlist
1519 `((,modified (buffer-modified-p))
1520 (buffer-undo-list t)
1521 (inhibit-read-only t)
1522 (inhibit-point-motion-hooks t)
1523 (inhibit-modification-hooks t)
1524 deactivate-mark
1525 buffer-file-name
1526 buffer-file-truename))
1527 (progn
1528 ,@body)
1529 (unless ,modified
1530 (restore-buffer-modified-p nil)))))
1532 (defun hfy-mark-trailing-whitespace ()
1533 "Tag trailing whitespace with a hfy property if it is currently highlighted."
1534 (when show-trailing-whitespace
1535 (let ((inhibit-read-only t))
1536 (save-excursion
1537 (goto-char (point-min))
1538 (hfy-save-buffer-state nil
1539 (while (re-search-forward "[ \t]+$" nil t)
1540 (put-text-property (match-beginning 0) (match-end 0)
1541 'hfy-show-trailing-whitespace t)))))))
1543 (defun hfy-unmark-trailing-whitespace ()
1544 "Undo the effect of `hfy-mark-trailing-whitespace'."
1545 (when show-trailing-whitespace
1546 (hfy-save-buffer-state nil
1547 (remove-text-properties (point-min) (point-max)
1548 '(hfy-show-trailing-whitespace)))))
1550 (defun hfy-fontify-buffer (&optional srcdir file)
1551 "Implement the guts of `htmlfontify-buffer'.
1552 SRCDIR, if set, is the directory being htmlfontified.
1553 FILE, if set, is the file name."
1554 (if srcdir (setq srcdir (directory-file-name srcdir)))
1555 (let* ( (html-buffer (hfy-buffer))
1556 (css-sheet nil)
1557 (css-map nil)
1558 (invis-ranges nil)
1559 (rovl nil)
1560 (orig-ovls (overlays-in (point-min) (point-max)))
1561 (rmin (when mark-active (region-beginning)))
1562 (rmax (when mark-active (region-end ))) )
1563 (when (and mark-active
1564 transient-mark-mode)
1565 (unless (and (= rmin (point-min))
1566 (= rmax (point-max)))
1567 (setq rovl (make-overlay rmin rmax))
1568 (overlay-put rovl 'priority 1000)
1569 (overlay-put rovl 'face 'region)))
1570 ;; copy the buffer, including fontification, and switch to it:
1571 (hfy-mark-trailing-whitespace)
1572 (setq css-sheet (hfy-compile-stylesheet )
1573 css-map (hfy-compile-face-map )
1574 invis-ranges (hfy-find-invisible-ranges))
1575 (hfy-unmark-trailing-whitespace)
1576 (when rovl
1577 (delete-overlay rovl))
1578 (copy-to-buffer html-buffer (point-min) (point-max))
1579 (set-buffer html-buffer)
1580 ;; rip out props that could interfere with our htmlization of the buffer:
1581 (remove-text-properties (point-min) (point-max) hfy-ignored-properties)
1582 ;; Apply overlay invisible spec
1583 (setq orig-ovls
1584 (sort orig-ovls
1585 (lambda (A B)
1586 (> (or (cadr (memq 'priority (overlay-properties A))) 0)
1587 (or (cadr (memq 'priority (overlay-properties B))) 0)))))
1588 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1589 ;; at this point, html-buffer retains the fontification of the parent:
1590 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1591 ;; we don't really need or want text in the html buffer to be invisible, as
1592 ;; that can make it look like we've rendered invalid xhtml when all that's
1593 ;; happened is some tags are in the invisible portions of the buffer:
1594 (setq buffer-invisibility-spec nil)
1595 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1596 ;; #####################################################################
1597 ;; if we are in etags mode, add properties to mark the anchors and links
1598 (if (and srcdir file)
1599 (progn
1600 (hfy-mark-tag-names srcdir file) ;; mark anchors
1601 (hfy-mark-tag-hrefs srcdir file))) ;; mark links
1602 ;; #####################################################################
1603 ;; mark the 'dangerous' characters
1604 ;;(message "marking dangerous characters")
1605 (hfy-html-enkludge-buffer)
1606 ;; trawl the position-based face-map, inserting span tags as we go
1607 ;; note that we cannot change any character positions before this point
1608 ;; or we will invalidate the map:
1609 ;; NB: This also means we have to trawl the map in descending file-offset
1610 ;; order, obviously.
1611 ;; ---------------------------------------------------------------------
1612 ;; Remember, inserting pushes properties to the right, which we don't
1613 ;; actually want to happen for link properties, so we have to flag
1614 ;; them and move them by hand - if you don't, you end up with
1616 ;; <span class="foo"><a href="bar">texta</span><span class="bletch"></a>...
1618 ;; instead of:
1620 ;; <span class="foo"><a href="bar">texta</a></span><span class="bletch">...
1622 ;; If my analysis of the problem is correct, we can detect link-ness by
1623 ;; either hfy-linkp or hfy-endl properties at the insertion point, but I
1624 ;; think we only need to relocate the hfy-endl property, as the hfy-linkp
1625 ;; property has already served its main purpose by this point.
1626 ;;(message "mapcar over the CSS-MAP")
1627 (message "invis-ranges:\n%S" invis-ranges)
1628 (dolist (point-face css-map)
1629 (let ((pt (car point-face))
1630 (fn (cdr point-face))
1631 (move-link nil))
1632 (goto-char pt)
1633 (setq move-link
1634 (or (get-text-property pt 'hfy-linkp)
1635 (get-text-property pt 'hfy-endl )))
1636 (if (eq 'end fn)
1637 (insert "</span>")
1638 (if (not (and srcdir file))
1640 (when move-link
1641 (remove-text-properties (point) (1+ (point)) '(hfy-endl nil))
1642 (put-text-property pt (1+ pt) 'hfy-endl t) ))
1643 ;; if we have invisible blocks, we need to do some extra magic:
1644 (if invis-ranges
1645 (let ((iname (hfy-invisible-name pt invis-ranges))
1646 (fname (hfy-lookup fn css-sheet )))
1647 (when (assq pt invis-ranges)
1648 (insert
1649 (format "<span onclick=\"toggle_invis('%s');\">" iname))
1650 (insert "…</span>"))
1651 (insert
1652 (format "<span class=\"%s\" id=\"%s-%d\">" fname iname pt)))
1653 (insert (format "<span class=\"%s\">" (hfy-lookup fn css-sheet))))
1654 (if (not move-link) nil
1655 ;;(message "removing prop2 @ %d" (point))
1656 (if (remove-text-properties (point) (1+ (point)) '(hfy-endl nil))
1657 (put-text-property pt (1+ pt) 'hfy-endl t))))))
1658 ;; #####################################################################
1659 ;; Invisibility
1660 ;; Maybe just make the text invisible in XHTML?
1661 ;; DONE -- big block of obsolete invisibility code elided here -- v
1662 ;; #####################################################################
1663 ;; (message "checking to see whether we should link...")
1664 (if (and srcdir file)
1665 (let ((lp 'hfy-link)
1666 (pt (point-min))
1667 (pr nil)
1668 (rr nil))
1669 ;; (message " yes we should.")
1670 ;; translate 'hfy-anchor properties to anchors
1671 (while (setq pt (next-single-property-change pt 'hfy-anchor))
1672 (if (setq pr (get-text-property pt 'hfy-anchor))
1673 (progn (goto-char pt)
1674 (remove-text-properties pt (1+ pt) '(hfy-anchor nil))
1675 (insert (concat "<a name=\"" pr "\"></a>")))))
1676 ;; translate alternate 'hfy-link and 'hfy-endl props to opening
1677 ;; and closing links. (this should avoid those spurious closes
1678 ;; we sometimes get by generating only paired tags)
1679 (setq pt (point-min))
1680 (while (setq pt (next-single-property-change pt lp))
1681 (if (not (setq pr (get-text-property pt lp))) nil
1682 (goto-char pt)
1683 (remove-text-properties pt (1+ pt) (list lp nil))
1684 (case lp
1685 (hfy-link
1686 (if (setq rr (get-text-property pt 'hfy-inst))
1687 (insert (format "<a name=\"%s\"></a>" rr)))
1688 (insert (format "<a href=\"%s\">" pr))
1689 (setq lp 'hfy-endl))
1690 (hfy-endl
1691 (insert "</a>") (setq lp 'hfy-link)) ))) ))
1693 ;; #####################################################################
1694 ;; transform the dangerous chars. This changes character positions
1695 ;; since entities have > char length.
1696 ;; note that this deletes the dangerous characters, and therefore
1697 ;; destroys any properties they may contain (such as 'hfy-endl),
1698 ;; so we have to do this after we use said properties:
1699 ;; (message "munging dangerous characters")
1700 (hfy-html-dekludge-buffer)
1701 ;; insert the stylesheet at the top:
1702 (goto-char (point-min))
1703 ;;(message "inserting stylesheet")
1704 (insert (hfy-sprintf-stylesheet css-sheet file))
1705 (if (hfy-opt 'div-wrapper) (insert "<div class=\"default\">"))
1706 (insert "\n<pre>")
1707 (goto-char (point-max))
1708 (insert "</pre>\n")
1709 (if (hfy-opt 'div-wrapper) (insert "</div>"))
1710 ;;(message "inserting footer")
1711 (insert (funcall hfy-page-footer file))
1712 ;; call any post html-generation hooks:
1713 (run-hooks 'hfy-post-html-hooks)
1714 ;; return the html buffer
1715 (set-buffer-modified-p nil)
1716 html-buffer))
1718 (defun hfy-force-fontification ()
1719 "Try to force font-locking even when it is optimized away."
1720 (run-hooks 'hfy-init-kludge-hook)
1721 (eval-and-compile (require 'font-lock))
1722 (if (boundp 'font-lock-cache-position)
1723 (or font-lock-cache-position
1724 (set 'font-lock-cache-position (make-marker))))
1725 (if (not noninteractive)
1726 (progn
1727 (message "hfy interactive mode (%S %S)" window-system major-mode)
1728 (when (and font-lock-defaults
1729 font-lock-mode)
1730 (font-lock-fontify-region (point-min) (point-max) nil)))
1731 (message "hfy batch mode (%s:%S)"
1732 (or (buffer-file-name) (buffer-name)) major-mode)
1733 (when font-lock-defaults
1734 (font-lock-fontify-buffer)) ))
1736 ;;;###autoload
1737 (defun htmlfontify-buffer (&optional srcdir file)
1738 "Create a new buffer, named for the current buffer + a .html extension,
1739 containing an inline CSS-stylesheet and formatted CSS-markup HTML
1740 that reproduces the look of the current Emacs buffer as closely
1741 as possible.
1743 Dangerous characters in the existing buffer are turned into HTML
1744 entities, so you should even be able to do HTML-within-HTML
1745 fontified display.
1747 You should, however, note that random control or eight-bit
1748 characters such as ^L (\x0c) or ¤ (\xa4) won't get mapped yet.
1750 If the SRCDIR and FILE arguments are set, lookup etags derived
1751 entries in the `hfy-tags-cache' and add HTML anchors and
1752 hyperlinks as appropriate."
1753 (interactive)
1754 ;; pick up the file name in case we didn't receive it
1755 (if (not file)
1756 (progn (setq file (or (buffer-file-name) (buffer-name)))
1757 (if (string-match "/\\([^/]*\\)\\'" file)
1758 (setq file (match-string 1 file)))) )
1760 (if (not (hfy-opt 'skip-refontification))
1761 (save-excursion ;; Keep region
1762 (hfy-force-fontification)))
1763 (if (called-interactively-p 'any) ;; display the buffer in interactive mode:
1764 (switch-to-buffer (hfy-fontify-buffer srcdir file))
1765 (hfy-fontify-buffer srcdir file)))
1767 ;; recursive file listing
1768 (defun hfy-list-files (directory)
1769 "Return a list of files under DIRECTORY.
1770 Strips any leading \"./\" from each filename."
1771 ;;(message "hfy-list-files");;DBUG
1772 ;; FIXME: this changes the dir of the current buffer. Is that right??
1773 (cd directory)
1774 (mapcar (lambda (F) (if (string-match "^./\\(.*\\)" F) (match-string 1 F) F))
1775 (split-string (shell-command-to-string hfy-find-cmd))) )
1777 ;; strip the filename off, return a directory name
1778 ;; not a particularly thorough implementation, but it will be
1779 ;; fed pretty carefully, so it should be Ok:
1780 (defun hfy-dirname (file)
1781 "Return everything preceding the last \"/\" from a relative filename FILE,
1782 on the assumption that this will produce a relative directory name.
1783 Hardly bombproof, but good enough in the context in which it is being used."
1784 ;;(message "hfy-dirname");;DBUG
1785 (let ((f (directory-file-name file)))
1786 (and (string-match "^\\(.*\\)/" f) (match-string 1 f))))
1788 ;; create a directory, cf mkdir -p
1789 (defun hfy-make-directory (dir)
1790 "Approx. equivalent of mkdir -p DIR."
1791 ;;(message "hfy-make-directory");;DBUG
1792 (if (file-exists-p dir)
1793 (if (file-directory-p dir) t)
1794 (make-directory dir t)))
1796 (defun hfy-text-p (srcdir file)
1797 "Is SRCDIR/FILE text? Uses `hfy-istext-command' to determine this."
1798 (let* ((cmd (format hfy-istext-command (expand-file-name file srcdir)))
1799 (rsp (shell-command-to-string cmd)))
1800 (string-match "text" rsp)))
1802 ;; open a file, check fontification, if fontified, write a fontified copy
1803 ;; to the destination directory, otherwise just copy the file:
1804 (defun hfy-copy-and-fontify-file (srcdir dstdir file)
1805 "Open FILE in SRCDIR - if fontified, write a fontified copy to DSTDIR
1806 adding an extension of `hfy-extn'. Fontification is actually done by
1807 `htmlfontify-buffer'. If the buffer is not fontified, just copy it."
1808 ;;(message "hfy-copy-and-fontify-file");;DBUG
1809 (let (;;(fast-lock-minimum-size hfy-fast-lock-save)
1810 ;;(font-lock-support-mode 'fast-lock-mode)
1811 ;;(window-system (or window-system 'htmlfontify))
1812 (target nil)
1813 (source nil)
1814 (html nil))
1815 (cd srcdir)
1816 (with-current-buffer (setq source (find-file-noselect file))
1817 ;; FIXME: Shouldn't this use expand-file-name? --Stef
1818 (setq target (concat dstdir "/" file))
1819 (hfy-make-directory (hfy-dirname target))
1820 (if (not (hfy-opt 'skip-refontification)) (hfy-force-fontification))
1821 (if (or (hfy-fontified-p) (hfy-text-p srcdir file))
1822 (progn (setq html (hfy-fontify-buffer srcdir file))
1823 (set-buffer html)
1824 (write-file (concat target hfy-extn))
1825 (kill-buffer html))
1826 ;; #o0200 == 128, but emacs20 doesn't know that
1827 (if (and (file-exists-p target) (not (file-writable-p target)))
1828 (set-file-modes target (logior (file-modes target) 128)))
1829 (copy-file (buffer-file-name source) target 'overwrite))
1830 (kill-buffer source)) ))
1832 ;; list of tags in file in srcdir
1833 (defun hfy-tags-for-file (cache-hash file)
1834 "List of etags tags that have definitions in this FILE.
1835 CACHE-HASH is the tags cache."
1836 ;;(message "hfy-tags-for-file");;DBUG
1837 (let* ((tag-list nil))
1838 (if cache-hash
1839 (maphash
1840 (lambda (K V)
1841 (if (assoc file V)
1842 (setq tag-list (cons K tag-list))))
1843 cache-hash))
1844 tag-list))
1846 ;; mark the tags native to this file for anchors
1847 (defun hfy-mark-tag-names (srcdir file)
1848 "Mark tags in FILE (lookup SRCDIR in `hfy-tags-cache') with the `hfy-anchor'
1849 property, with a value of \"tag.line-number\"."
1850 ;;(message "(hfy-mark-tag-names %s %s)" srcdir file);;DBUG
1851 (let* ((cache-entry (assoc srcdir hfy-tags-cache))
1852 (cache-hash (cadr cache-entry)))
1853 (if cache-hash
1854 (mapcar
1855 (lambda (TAG)
1856 (mapcar
1857 (lambda (TLIST)
1858 (if (string= file (car TLIST))
1859 (let* ((line (cadr TLIST) )
1860 (chr (caddr TLIST) )
1861 (link (format "%s.%d" TAG line) ))
1862 (put-text-property (+ 1 chr)
1863 (+ 2 chr)
1864 'hfy-anchor link))))
1865 (gethash TAG cache-hash)))
1866 (hfy-tags-for-file cache-hash file)))))
1868 (defun hfy-relstub (file &optional start)
1869 "Return a \"../\" stub of the appropriate length for the current source
1870 tree depth, as determined from FILE (a filename).
1871 START is the offset at which to start looking for the / character in FILE."
1872 ;;(message "hfy-relstub");;DBUG
1873 (let ((c ""))
1874 (while (setq start (string-match "/" file start))
1875 (setq start (1+ start)) (setq c (concat c "../")))
1878 (defun hfy-href-stub (this-file def-files tag)
1879 "Return an href stub for a tag href in THIS-FILE.
1880 If DEF-FILES (list of files containing definitions for the tag in question)
1881 contains only one entry, the href should link straight to that file.
1882 Otherwise, the link should be to the index file.\n
1883 We are not yet concerned with the file extensions/tag line number and so on at
1884 this point.\n
1885 If `hfy-split-index' is set, and the href wil be to an index file rather than
1886 a source file, append a .X to `hfy-index-file', where X is the uppercased
1887 first character of TAG.\n
1888 See also `hfy-relstub', `hfy-index-file'."
1889 ;;(message "hfy-href-stub");;DBUG
1890 ;; FIXME: Why not use something like
1891 ;; (file-relative-name (if ...) (file-name-directory this-file)) ? --Stef
1892 (concat
1893 (hfy-relstub this-file)
1894 (if (= 1 (length def-files)) (car def-files)
1895 (if (not hfy-split-index) hfy-index-file
1896 (concat hfy-index-file "." (upcase (substring tag 0 1)))))) )
1898 (defun hfy-href (this-file def-files tag tag-map)
1899 "Return a relative href to the tag in question, based on\n
1900 THIS-FILE `hfy-link-extn' `hfy-extn' DEF-FILES TAG and TAG-MAP\n
1901 THIS-FILE is the current source file
1902 DEF-FILES is a list of file containing possible link endpoints for TAG
1903 TAG is the tag in question
1904 TAG-MAP is the entry in `hfy-tags-cache'."
1905 ;;(message "hfy-href");;DBUG
1906 (concat
1907 (hfy-href-stub this-file def-files tag)
1908 (or hfy-link-extn hfy-extn) "#" tag ;;(.src -> .html)
1909 (if (= 1 (length def-files))
1910 (concat "." (format "%d" (cadr (assoc (car def-files) tag-map)))))) )
1912 (defun hfy-word-regex (string)
1913 "Return a regex that matches STRING as the first `match-string', with non
1914 word characters on either side."
1915 ;; FIXME: Should this use [^$[:alnum:]_] instead? --Stef
1916 (concat "[^$A-Za-z_0-9]\\(" (regexp-quote string) "\\)[^A-Za-z_0-9]"))
1918 ;; mark all tags for hyperlinking, except the tags at
1919 ;; their own points of definition, iyswim:
1920 (defun hfy-mark-tag-hrefs (srcdir file)
1921 "Mark href start points with the `hfy-link' prop (value: href string).\n
1922 Mark href end points with the `hfy-endl' prop (value t).\n
1923 Avoid overlapping links, and mark links in descending length of
1924 tag name in order to prevent subtags from usurping supertags,
1925 \(eg \"term\" for \"terminal\").
1926 SRCDIR is the directory being \"published\".
1927 FILE is the specific file we are rendering."
1928 ;;(message "hfy-mark-tag-hrefs");;DBUG
1929 (let ((cache-entry (assoc srcdir hfy-tags-cache))
1930 (list-cache (assoc srcdir hfy-tags-sortl))
1931 (rmap-cache (assoc srcdir hfy-tags-rmap ))
1932 (no-comment (hfy-opt 'zap-comment-links))
1933 (no-strings (hfy-opt 'zap-string-links ))
1934 (cache-hash nil)
1935 (tags-list nil)
1936 (tags-rmap nil)
1937 (case-fold-search nil))
1938 ;; extract the tag mapping hashes (fwd and rev) and the tag list:
1939 (if (and (setq cache-hash (cadr cache-entry))
1940 (setq tags-rmap (cadr rmap-cache ))
1941 (setq tags-list (cadr list-cache )))
1942 (mapcar
1943 (lambda (TAG)
1944 (let* ((start nil)
1945 (stop nil)
1946 (href nil)
1947 (name nil)
1948 (case-fold-search nil)
1949 (tmp-point nil)
1950 (maybe-start nil)
1951 (face-at nil)
1952 (rmap-entry nil)
1953 (rnew-elt nil)
1954 (rmap-line nil)
1955 (tag-regex (hfy-word-regex TAG))
1956 (tag-map (gethash TAG cache-hash))
1957 (tag-files (mapcar #'car tag-map)))
1958 ;; find instances of TAG and do what needs to be done:
1959 (goto-char (point-min))
1960 (while (search-forward TAG nil 'NOERROR)
1961 (setq tmp-point (point)
1962 maybe-start (- (match-beginning 0) 1))
1963 (goto-char maybe-start)
1964 (if (not (looking-at tag-regex))
1966 (setq start (match-beginning 1))
1967 (setq stop (match-end 1))
1968 (setq face-at
1969 (and (or no-comment no-strings) (hfy-face-at start)))
1970 (if (listp face-at)
1971 (setq face-at (cadr (memq :inherit face-at))))
1972 (if (or (text-property-any start (1+ stop) 'hfy-linkp t)
1973 (and no-comment (eq 'font-lock-comment-face face-at))
1974 (and no-strings (eq 'font-lock-string-face face-at)))
1975 nil ;; already a link, NOOP
1977 ;; set a reverse map entry:
1978 (setq rmap-line (line-number-at-pos)
1979 rmap-entry (gethash TAG tags-rmap)
1980 rnew-elt (list file rmap-line start)
1981 rmap-entry (cons rnew-elt rmap-entry)
1982 name (format "%s.%d" TAG rmap-line))
1983 (put-text-property start (1+ start) 'hfy-inst name)
1984 (puthash TAG rmap-entry tags-rmap)
1986 ;; mark the link. link to index if the tag has > 1 def
1987 ;; add the line number to the #name if it does not:
1988 (setq href (hfy-href file tag-files TAG tag-map))
1989 (put-text-property start (1+ start) 'hfy-link href)
1990 (put-text-property stop (1+ stop ) 'hfy-endl t )
1991 (put-text-property start (1+ stop ) 'hfy-linkp t )))
1992 (goto-char tmp-point)) ))
1993 tags-list) )))
1995 (defun hfy-shell ()
1996 "Return `shell-file-name', or \"/bin/sh\" if it is a non-Bourne shell."
1997 (if (string-match "\\<bash\\>\\|\\<sh\\>\\|\\<dash\\>" shell-file-name)
1998 shell-file-name
1999 (or hfy-shell-file-name "/bin/sh")))
2001 ;; cache the #(tag => file line point) entries for files under srcdir
2002 ;; and cache the descending sorted list of tags in the relevant alist,
2003 ;; also keyed by srcdir:
2004 (defun hfy-load-tags-cache (srcdir)
2005 "Run `hfy-etags-cmd' on SRCDIR, then call `hfy-parse-tags-buffer'."
2006 ;;(message "hfy-load-tags-cache");;DBUG
2007 (let ((etags-buffer (get-buffer-create "*hfy-tags*"))
2008 (etags-command (format hfy-etags-cmd hfy-etags-bin))
2009 (shell-file-name (hfy-shell)))
2010 (cd srcdir)
2011 (shell-command etags-command etags-buffer)
2012 (hfy-parse-tags-buffer srcdir etags-buffer)) )
2014 ;; break this out from `hfy-load-tags-cache' to make the tar file
2015 ;; functionality easier to implement.
2016 ;; ( tar file functionality not merged here because it requires a
2017 ;; hacked copy of etags capable of tagging stdin: if Francesco
2018 ;; Potorti accepts a patch, or otherwise implements stdin tagging,
2019 ;; then I will provide a `htmlfontify-tar-file' defun )
2020 (defun hfy-parse-tags-buffer (srcdir buffer)
2021 "Parse a BUFFER containing etags formatted output, loading the
2022 `hfy-tags-cache' and `hfy-tags-sortl' entries for SRCDIR."
2023 (let ((cache-entry (assoc srcdir hfy-tags-cache))
2024 (tlist-cache (assoc srcdir hfy-tags-sortl))
2025 (trmap-cache (assoc srcdir hfy-tags-rmap ))
2026 (cache-hash nil) (trmap-hash nil) (tags-list nil)
2027 (hash-entry nil) (tag-string nil) (tag-line nil)
2028 (tag-point nil) (new-entry nil) (etags-file nil))
2030 ;; (re)initialize the tag reverse map:
2031 (if trmap-cache (setq trmap-hash (cadr trmap-cache))
2032 (setq trmap-hash (make-hash-table :test 'equal))
2033 (setq hfy-tags-rmap (list (list srcdir trmap-hash) hfy-tags-rmap)))
2034 (clrhash trmap-hash)
2036 ;; (re)initialize the tag cache:
2037 (if cache-entry (setq cache-hash (cadr cache-entry))
2038 (setq cache-hash (make-hash-table :test 'equal))
2039 (setq hfy-tags-cache (list (list srcdir cache-hash) hfy-tags-cache)))
2040 (clrhash cache-hash)
2042 ;; cache the TAG => ((file line point) (file line point) ... ) entries:
2043 (with-current-buffer buffer
2044 (goto-char (point-min))
2046 (while (and (looking-at "^\x0c") (= 0 (forward-line 1)))
2047 ;;(message "^L boundary")
2048 (if (and (looking-at "^\\(.+\\),\\([0-9]+\\)$")
2049 (= 0 (forward-line 1)))
2050 (progn
2051 (setq etags-file (match-string 1))
2052 ;;(message "TAGS for file: %s" etags-file)
2053 (while (and (looking-at hfy-etag-regex) (= 0 (forward-line 1)))
2054 (setq tag-string (match-string 1))
2055 (if (= 0 (length tag-string)) nil ;; noop
2056 (setq tag-line (round (string-to-number (match-string 2))))
2057 (setq tag-point (round (string-to-number (match-string 3))))
2058 (setq hash-entry (gethash tag-string cache-hash))
2059 (setq new-entry (list etags-file tag-line tag-point))
2060 (push new-entry hash-entry)
2061 ;;(message "HASH-ENTRY %s %S" tag-string new-entry)
2062 (puthash tag-string hash-entry cache-hash)))) )))
2064 ;; cache a list of tags in descending length order:
2065 (maphash (lambda (K _V) (push K tags-list)) cache-hash)
2066 (setq tags-list (sort tags-list (lambda (A B) (< (length B) (length A)))))
2068 ;; put the tag list into the cache:
2069 (if tlist-cache (setcar (cdr tlist-cache) tags-list)
2070 (push (list srcdir tags-list) hfy-tags-sortl))
2072 ;; return the number of tags found:
2073 (length tags-list) ))
2075 (defun hfy-prepare-index-i (srcdir dstdir filename &optional stub map)
2076 "Prepare a tags index buffer for SRCDIR.
2077 `hfy-tags-cache' must already have an entry for SRCDIR for this to work.
2078 `hfy-page-header', `hfy-page-footer', `hfy-link-extn' and `hfy-extn'
2079 all play a part here.\n
2080 If STUB is set, prepare an (appropriately named) index buffer
2081 specifically for entries beginning with STUB.\n
2082 If MAP is set, use that instead of `hfy-tags-cache'.
2083 FILENAME is the name of the file being indexed.
2084 DSTDIR is the output directory, where files will be written."
2085 ;;(message "hfy-write-index");;DBUG
2086 (let ((cache-entry (assoc srcdir (or map hfy-tags-cache)))
2087 (cache-hash nil)
2088 (tag-list nil)
2089 (index-file
2090 (concat filename (if stub (concat "." stub) "") hfy-extn))
2091 (index-buf nil))
2092 (if (not (and cache-entry
2093 (setq cache-hash (cadr cache-entry))
2094 (setq index-buf (get-buffer-create index-file))))
2095 nil ;; noop
2096 (maphash (lambda (K _V) (push K tag-list)) cache-hash)
2097 (setq tag-list (sort tag-list 'string<))
2098 (set-buffer index-buf)
2099 (erase-buffer)
2100 (insert (funcall hfy-page-header filename "<!-- CSS -->"))
2101 (insert "<table class=\"index\">\n")
2103 (dolist (TAG tag-list)
2104 (let ((tag-started nil))
2105 (dolist (DEF (gethash TAG cache-hash))
2106 (if (and stub (not (string-match (concat "^" stub) TAG)))
2107 nil ;; we have a stub and it didn't match: NOOP
2108 (let ((file (car DEF))
2109 (line (cadr DEF)))
2110 (insert
2111 (format
2112 (concat
2113 " <tr> \n"
2114 " <td>%s</td> \n"
2115 " <td><a href=\"%s%s\">%s</a></td> \n"
2116 " <td><a href=\"%s%s#%s.%d\">%d</a></td>\n"
2117 " </tr> \n")
2118 (if (string= TAG tag-started) "&nbsp;"
2119 (format "<a name=\"%s\">%s</a>" TAG TAG))
2120 file (or hfy-link-extn hfy-extn) file
2121 file (or hfy-link-extn hfy-extn) TAG line line))
2122 (setq tag-started TAG))))))
2123 (insert "</table>\n")
2124 (insert (funcall hfy-page-footer filename))
2125 (and dstdir (cd dstdir))
2126 (set-visited-file-name index-file)
2127 index-buf) ))
2129 (defun hfy-prepare-index (srcdir dstdir)
2130 "Return a list of index buffer(s), as determined by `hfy-split-index'.
2131 SRCDIR and DSTDIR are the source and output directories respectively."
2132 (if (not hfy-split-index)
2133 (list (hfy-prepare-index-i srcdir dstdir hfy-index-file nil))
2134 (let ((stub-list nil)
2135 (cache-hash nil)
2136 (index-list nil)
2137 (cache-entry (assoc srcdir hfy-tags-cache)))
2138 (if (and cache-entry (setq cache-hash (cadr cache-entry)))
2139 (maphash
2140 (lambda (K _V)
2141 (let ((stub (upcase (substring K 0 1))))
2142 (if (member stub stub-list)
2143 nil ;; seen this already: NOOP
2144 (setq
2145 stub-list (cons stub stub-list)
2146 index-list (cons (hfy-prepare-index-i srcdir
2147 dstdir
2148 hfy-index-file
2149 stub)
2150 index-list)) )))
2151 cache-hash) )
2152 index-list)))
2154 (defun hfy-prepare-tag-map (srcdir dstdir)
2155 "Prepare the counterpart(s) to the index buffer(s) - a list of buffers
2156 with the same structure, but listing (and linking to) instances of tags
2157 \(as opposed to their definitions).\n
2158 SRCDIR and DSTDIR are the source and output directories respectively.
2159 See also `hfy-prepare-index', `hfy-split-index'."
2160 (if (not hfy-split-index)
2161 (list (hfy-prepare-index-i srcdir
2162 dstdir
2163 hfy-instance-file
2165 hfy-tags-rmap))
2166 (let ((stub-list nil)
2167 (cache-hash nil)
2168 (index-list nil)
2169 (cache-entry (assoc srcdir hfy-tags-rmap)))
2171 (if (and cache-entry (setq cache-hash (cadr cache-entry)))
2172 (maphash
2173 (lambda (K _V)
2174 (let ((stub (upcase (substring K 0 1))))
2175 (if (member stub stub-list)
2176 nil ;; seen this already: NOOP
2177 (setq
2178 stub-list (cons stub stub-list)
2179 index-list (cons (hfy-prepare-index-i srcdir
2180 dstdir
2181 hfy-instance-file
2182 stub
2183 hfy-tags-rmap)
2184 index-list)) )))
2185 cache-hash) )
2186 index-list)))
2188 (defun hfy-subtract-maps (srcdir)
2189 "Internal function - strips definitions of tags from the instance map.
2190 SRCDIR is the directory being \"published\".
2191 See also `hfy-tags-cache', `hfy-tags-rmap'."
2192 (let ((new-list nil)
2193 (old-list nil)
2194 (def-list nil)
2195 (exc-list nil)
2196 (fwd-map (cadr (assoc srcdir hfy-tags-cache)))
2197 (rev-map (cadr (assoc srcdir hfy-tags-rmap )))
2198 (taglist (cadr (assoc srcdir hfy-tags-sortl))))
2199 (dolist (TAG taglist)
2200 (setq def-list (gethash TAG fwd-map)
2201 old-list (gethash TAG rev-map)
2202 exc-list (mapcar (lambda (P) (list (car P) (cadr P))) def-list)
2203 new-list nil)
2204 (dolist (P old-list)
2205 (or (member (list (car P) (cadr P)) exc-list)
2206 (push P new-list)))
2207 (puthash TAG new-list rev-map))))
2209 (defun htmlfontify-run-etags (srcdir)
2210 "Load the etags cache for SRCDIR.
2211 See also `hfy-load-tags-cache'."
2212 (interactive "D source directory: ")
2213 (hfy-load-tags-cache (directory-file-name srcdir)))
2215 ;;(defun hfy-test-read-args (foo bar)
2216 ;; (interactive "D source directory: \nD target directory: ")
2217 ;; (message "foo: %S\nbar: %S" foo bar))
2219 (defun hfy-save-kill-buffers (buffer-list &optional dstdir)
2220 (dolist (B buffer-list)
2221 (set-buffer B)
2222 (and dstdir (file-directory-p dstdir) (cd dstdir))
2223 (save-buffer)
2224 (kill-buffer B)))
2226 ;;;###autoload
2227 (defun htmlfontify-copy-and-link-dir (srcdir dstdir &optional f-ext l-ext)
2228 "Trawl SRCDIR and write fontified-and-hyperlinked output in DSTDIR.
2229 F-EXT and L-EXT specify values for `hfy-extn' and `hfy-link-extn'.\n
2230 You may also want to set `hfy-page-header' and `hfy-page-footer'."
2231 (interactive "D source directory: \nD output directory: ")
2232 ;;(message "htmlfontify-copy-and-link-dir")
2233 (setq srcdir (directory-file-name srcdir))
2234 (setq dstdir (directory-file-name dstdir))
2235 (let ((source-files "SETME: list of source files, relative to srcdir")
2236 (tr-cache (assoc srcdir hfy-tags-rmap))
2237 (hfy-extn (or f-ext ".html"))
2238 (hfy-link-extn (or l-ext ".html")))
2239 ;; oops, forgot to load etags for srcdir:
2240 (if tr-cache nil
2241 (message "autoload of tags cache")
2242 (hfy-load-tags-cache srcdir)
2243 (setq tr-cache (assoc srcdir hfy-tags-rmap)))
2244 ;; clear out the old cache:
2245 (clrhash (cadr tr-cache))
2246 (hfy-make-directory dstdir)
2247 (setq source-files (hfy-list-files srcdir))
2248 (dolist (file source-files)
2249 (hfy-copy-and-fontify-file srcdir dstdir file))
2250 (hfy-subtract-maps srcdir)
2251 (hfy-save-kill-buffers (hfy-prepare-index srcdir dstdir) dstdir)
2252 (hfy-save-kill-buffers (hfy-prepare-tag-map srcdir dstdir) dstdir) ))
2254 ;; name of the init file we want:
2255 (defun hfy-initfile ()
2256 "Return the expected location of the htmlfontify specific init/custom file."
2257 (let* ((file (or (getenv "HFY_INITFILE") ".hfy.el")))
2258 (expand-file-name file "~") ))
2261 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2262 ;; incomplete as yet : transfer hook settings to hfy init file:
2263 ;; (defalias 'hfy-set-hooks 'custom-set-variables)
2265 ;; (defun hfy-pp-hook (H)
2266 ;; (and (string-match "-hook\\'" (symbol-name H))
2267 ;; (boundp H)
2268 ;; (symbol-value H)
2269 ;; (insert (format "\n '(%S %S)" H (symbol-value H)))
2270 ;; )
2271 ;; )
2273 ;; (defun hfy-save-hooks ()
2274 ;; (let ((custom-file (hfy-initfile)))
2275 ;; (custom-save-delete 'hfy-set-hooks)
2276 ;; (let ((standard-output (current-buffer)))
2277 ;; (princ "(hfy-set-hooks\n;;auto-generated, only one copy allowed\n")
2278 ;; (mapatoms 'hfy-pp-hook)
2279 ;; (insert "\n)")
2280 ;; )
2281 ;; )
2282 ;; )
2283 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2285 (defalias 'hfy-init-progn 'progn)
2287 (defun hfy-save-initvar (sym)
2288 (princ (format "(setq %s\n '" sym))
2289 (pp (symbol-value sym))
2290 (princ ")\n"))
2292 (defun htmlfontify-save-initfile ()
2293 "Save the htmlfontify settings to the htmlfontify init file."
2294 (interactive)
2295 (let* ((start-pos nil)
2296 (custom-file (hfy-initfile))
2297 (standard-output (find-file-noselect custom-file 'nowarn)))
2298 (save-excursion
2299 (custom-save-delete 'hfy-init-progn)
2300 (setq start-pos (point))
2301 (princ "(hfy-init-progn\n;;auto-generated, only one copy allowed\n")
2302 ;; FIXME: This saving&restoring of global customization
2303 ;; variables can interfere with other customization settings for
2304 ;; those vars (in .emacs or in Customize).
2305 (mapc 'hfy-save-initvar
2306 '(auto-mode-alist interpreter-mode-alist))
2307 (princ ")\n")
2308 (indent-region start-pos (point) nil))
2309 (custom-save-all) ))
2311 (defun htmlfontify-load-initfile ()
2312 "Load the htmlfontify specific init/custom file."
2313 (interactive)
2314 (let ((file (hfy-initfile)))
2315 (load file 'NOERROR nil nil) ))
2318 ;;;### (autoloads (hfy-fallback-colour-values htmlfontify-load-rgb-file)
2319 ;;;;;; "hfy-cmap" "hfy-cmap.el" "ef24066922f1e27b7580d572f12fabbe")
2320 ;;; Generated autoloads from hfy-cmap.el
2322 (autoload 'htmlfontify-load-rgb-file "hfy-cmap" "\
2323 Load an X11 style rgb.txt FILE.
2324 Search `hfy-rgb-load-path' if FILE is not specified.
2325 Loads the variable `hfy-rgb-txt-colour-map', which is used by
2326 `hfy-fallback-colour-values'.
2328 \(fn &optional FILE)" t nil)
2330 (autoload 'hfy-fallback-colour-values "hfy-cmap" "\
2331 Use a fallback method for obtaining the rgb values for a color.
2333 \(fn COLOUR-STRING)" nil nil)
2335 ;;;***
2338 (provide 'htmlfontify)
2340 ;; Local Variables:
2341 ;; coding: utf-8
2342 ;; End:
2344 ;;; htmlfontify.el ends here