
(if (not (string-match "~/dlisp/" (car load-path)))
  (setq load-path (cons "~/dlisp/" load-path)))

(if noninteractive
    (load-file (concat (car load-path) "diagnose.el")))

(require 'd-flock-lisp++)

(defun d-latexize--inside-comment ()
  (save-excursion
    (save-match-data
      (let ((p (point)))
        (when (re-search-backward "(CXXCOMM)" nil t)
          (when (re-search-forward "(CXXMMOC)" nil t)
            (> (point) p)))))))

(defun d-latexize--inside-string ()
  (save-excursion
    (save-match-data
      (let ((p (point)))
        (when (re-search-backward "(CXXSTRI)" nil t)
          (when (re-search-forward "(CXXIRTS)" nil t)
            (> (point) p)))))))

(defun d-latexize--inside-func ()
  (save-excursion
    (save-match-data
      (let ((p (point)))
        (when (re-search-backward "(CXXFUNC)" nil t)
          (when (re-search-forward "(CXXCNUF)" nil t)
            (> (point) p)))))))

(defun d-latexize--inside-hash ()
  (save-excursion
    (save-match-data
      (let ((p (point)))
        (when (re-search-backward "(CXXHASH)" nil t)
          (when (re-search-forward "(CXXHSAH)" nil t)
            (> (point) p)))))))

(defun d-latexize--inside-prvt ()
  (save-excursion
    (save-match-data
      (let ((p (point)))
        (when (re-search-backward "(CXXPRVT)" nil t)
          (when (re-search-forward "(CXXTVRP)" nil t)
            (> (point) p)))))))

(defun d-latexize--inside-glbl ()
  (save-excursion
    (save-match-data
      (let ((p (point)))
        (when (re-search-backward "(CXXGLBL)" nil t)
          (when (re-search-forward "(CXXLBLG)" nil t)
            (> (point) p)))))))

(defun d-latexize--inside-jtw ()
  (save-excursion
    (save-match-data
      (let ((p (point)))
        (when (re-search-backward "(CXXJTW)" nil t)
          (when (re-search-forward "(CXXWTJ)" nil t)
            (> (point) p)))))))

(defun d-latexize--inside-clss ()
  (save-excursion
    (save-match-data
      (let ((p (point)))
        (when (re-search-backward "(CXXCLSS)" nil t)
          (when (re-search-forward "(CXXSSLC)" nil t)
            (> (point) p)))))))

(defun d-latexize--inside-vari ()
  (save-excursion
    (save-match-data
      (let ((p (point)))
        (when (re-search-backward "(CXXVARI)" nil t)
          (when (re-search-forward "(CXXIRAV)" nil t)
            (> (point) p)))))))

(defun d-latexize--inside-comment-or-string ()
  (or (d-latexize--inside-comment)
      (d-latexize--inside-string)
      (d-latexize--inside-func)
      (d-latexize--inside-hash)
      (d-latexize--inside-prvt)
      (d-latexize--inside-glbl)
      (d-latexize--inside-jtw)
      (d-latexize--inside-clss)
      (d-latexize--inside-vari)
      ))

(defun checkpoint (&rest args) (message "*** checkpoint %s" args))

(defun doit ()
  (interactive)
  (let ((case-fold-search nil)
        (debug-on-error   t)
        mode-emacs-lisp mode-emacs-lisp++ mode-c++)

    (checkpoint "checkpoint:1")

    (when (or (not (boundp '*target*)) (not *target*))
      ;;(setq *target* "d:/home/c++-projects/lisp++2c++-lisp++2c++.el")
      ;;(setq *target* "/media/C1TB/home/c++-projects/lisp++2c++-cclass.el")
      ;;(setq *target* "d:/home/c++-projects/2012/Arithmetickles/arithmetickles.lisp++")
      ;;(setq *target* "~/c++-projects/2012/Arithmetickles/glyph_a.lisp++")
      (setq *target* "~/c++-projects/2012/cpause/my_time_sharing_system.h++")
      (setq *target* "~/c++-projects/2006/libd/ptr.hh")
      )

    (checkpoint "checkpoint:2")

    (assert (boundp '*target*))
    (assert *target*)
    (message "*** d-latexize found file=%s" *target*)

    (checkpoint "checkpoint:3")

    (setq mode-emacs-lisp nil)
    (setq mode-emacs-lisp++ nil)
    (setq mode-c++ nil)

    ;;(message "**** *target*=%s" *target*)

    (if (string-match "\\.el$" *target*)
        (setq mode-emacs-lisp t))

    (if (string-match "\\.lisp\\+\\+$" *target*)
        (setq mode-emacs-lisp++ t))

    (if (or (string-match "\\.hh$" *target*)
            (string-match "\\.ch$" *target*)
            (string-match "\\.cc$" *target*)
            (string-match "\\.h\\+\\+$" *target*)
            (string-match "\\.ch\\+\\+$" *target*)
            (string-match "\\.c\\+\\+$" *target*))
        (setq mode-c++ t))

    (checkpoint "checkpoint:4")

    (setq *buf* (find-file *target*))
    (read-only-mode -1)

    (assert (or mode-emacs-lisp mode-emacs-lisp++ mode-c++))

    (if mode-emacs-lisp
        (emacs-lisp-mode))
    (if mode-emacs-lisp++
        (c++-mode))
    (if mode-c++
        (c++-mode))

    ;;(emacs-lisp-mode)
    ;;(message "*** debug-on-error=%s" debug-on-error)
    ;;(message "*** case-fold-search=%s" case-fold-search)

    (progn
      (goto-char (point-min))
      (while (re-search-forward "" nil t)
        (replace-match "^L" 'fixedcase)))

    (checkpoint "checkpoint:5")

    (goto-char (point-min))
    (while (re-search-forward "{" nil t)
      (replace-match "(ZZ){" 'fixedcase))

    (checkpoint "checkpoint:6")

    (goto-char (point-min))
    (while (re-search-forward "}" nil t)
      (replace-match "(ZZ)}" 'fixedcase))

    (checkpoint "checkpoint:7")

    (when mode-emacs-lisp
      (goto-char (point-min))
      (while (and (re-search-forward ";;;+" nil t)
                  (not (d-latexize--inside-comment)))
        (save-excursion
          ;;(goto-char (max 0 (- (point) 3)))
          ;;(assert (looking-at ";;;"))
          (while (d-delta-looking-at ";" -1)
            (forward-char -1))
          (while (d-delta-looking-at "[ \t]" -1)
            (forward-char -1))
          ;;(message "*** (char-after (point))=%c" (char-after (point)))
          (insert "(CXXCOMM){(ZZ)color{red}{")
          (end-of-line)
          (setq line (d-current-line-as-string))
          (message "**** before line=%s" line)
          (forward-char 1)
          (insert "}}(CXXMMOC)")
          (setq line (d-current-line-as-string))
          (message "**** after  line=%s" line)
          )))

    (when mode-emacs-lisp
      (goto-char (point-min))
      (while (and (re-search-forward ";;" nil t)
                  (not (d-latexize--inside-comment)))
        (save-excursion
          (goto-char (max 0 (- (point) 2)))
          (assert (looking-at ";;"))
          (while (d-delta-looking-at ";" -1)
            (forward-char -1))
          (while (d-delta-looking-at "[ \t]" -1)
            (forward-char -1))
          ;;(assert (looking-at ";;"))
          (insert "(CXXCOMM){(ZZ)color{comm}{")
          (end-of-line)
          ;;(setq line (d-current-line-as-string))
          ;;(message "**** before line=%s" line)
          (forward-char 1)
          ;;(insert "}}(CXXMMOC)")
          ;;(setq line (d-current-line-as-string))
          (message "**** after  line=%s" line)
          )))

    (checkpoint "checkpoint:8")

    (when (or mode-emacs-lisp++ mode-c++)
      (goto-char (point-min))
      (while (and (re-search-forward "///" nil t)
                  (not (d-latexize--inside-comment)))
        (goto-char (max 0 (- (point) 3)))
        (insert "(CXXCOMM){(ZZ)color{red}{")
        (end-of-line)
        (insert "}}(CXXMMOC)")))

    (checkpoint "checkpoint:9")

    (when (or mode-emacs-lisp++ mode-c++)
      (goto-char (point-min))
      (while (and (re-search-forward "//" nil t)
                  (not (d-latexize--inside-comment)))
        (save-excursion
          (goto-char (max 0 (- (point) 2)))
          (when (not (d-latexize--inside-comment))
            (insert "(CXXCOMM){(ZZ)color{comm}{")
            (end-of-line)
            (insert "}}(CXXMMOC)")))))

    (checkpoint "checkpoint:10")

    (goto-char (point-min))
    (while (re-search-forward "/\\*\\*" nil t)
      (save-excursion
        (goto-char (max 0 (- (point) 2)))
        (when (not (d-latexize--inside-comment))
          (insert "(CXXCOMM){(ZZ)color{red}{")
          (re-search-forward "*/" nil t)
          (insert "}}(CXXMMOC)"))))

    (checkpoint "checkpoint:11")

    (goto-char (point-min))
    (while (and (re-search-forward "/\\*" nil t)
                (not (d-latexize--inside-comment)))
      (save-excursion
        (goto-char (max 0 (- (point) 2)))
        (insert "(CXXCOMM){(ZZ)color{comm}{")
        (re-search-forward "*/" nil t)
        (insert "}}(CXXMMOC)")))

    (checkpoint "checkpoint:12")

    (progn
      (goto-char (point-min)) ;;                            12345678
      (while (re-search-forward (concat "\\([^\\]\\)\\(\"\\(\\\\\\\\\\|"
                                        "\\\\^\\|\\\\\'\\|\\\\\"\\|"
                                        "\\\\t\\|\\\\r\\|\\\\n\\|"
                                        "\\\\$\\|[^\"\\\r\n\t]\\)*\"\\)") nil t)
        (if (not (d-latexize--inside-comment))
            (replace-match "\\1(CXXSTRI){(ZZ)bf(ZZ)colorbox{blue}{(ZZ)color{white}{\\2}}}(CXXIRTS)" 'fixedcase)
          )))

    (checkpoint "checkpoint:13")

    (when mode-c++
      (goto-char (point-min))
      (while (re-search-forward "\\([A-Z][a-zA-Z0-9_]*\\)::\\([a-z][a-zA-Z0-9_]*\\)(" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "(CXXFUNC){(ZZ)color{clss}{\\1}}::{(ZZ)bf(ZZ)colorbox{func}{\\2}}(CXXCNUF)(" 'fixedcase))))

    (checkpoint "checkpoint:13a")

    (when mode-c++
      (goto-char (point-min))
      (while (re-search-forward "\\([A-Z][a-zA-Z0-9_]*\\)::\\([a-z][a-zA-Z0-9_]*\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "(CXXCLSS){(ZZ)color{clss}{\\1}}::{(ZZ)color{vari}{\\2}}(CXXSSLC)" 'fixedcase))))

    (checkpoint "checkpoint:13b")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "\\<class \\([A-Za-z_][A-Za-z0-9_]*\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "{(CXXCLSS)(ZZ)bf{}class} {(ZZ)color{clss}{\\1}}(CXXSSLC)" 'fixedcase))))

    (checkpoint "checkpoint:13c")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (progn
               (goto-char (max 0 (- (point) 1)))
               (re-search-forward (concat
                                   "\\([^-a-zA-Z0-9_]\\)\\(new\\|delete\\|static\\|virtual\\|"
                                   "this\\|const_cast\\|const\\|return\\|namespace\\|"
                                   "switch\\|case\\|break\\|using namespace\\|"
                                   "if\\|else\\|for\\|while\\|do\\|"
                                   "class\\|this\\|template\\|friend\\)\\([^-a-zA-Z0-9_]\\)") nil t))
        (if (save-excursion
              (goto-char (max 0 (- (point) 2)))
              (not (d-latexize--inside-comment-or-string)))
            (replace-match "\\1{(ZZ)color{black}(ZZ)bf{\\2}}\\3" 'fixedcase))))

    (checkpoint "checkpoint:13d")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "\\([a-zA-Z0-9_]+[&*]*\\) operator \\([^ ()]+\\)[ \t]*(" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "(CXXFUNC){(ZZ)color{clss}{\\1}} {(ZZ)bf{operator}} {(ZZ)colorbox{func}{\\2}}(CXXCNUF)(" 'fixedcase))))

    (checkpoint "checkpoint:13e")

    (when mode-c++
      (goto-char (point-min))
      (while (re-search-forward "\\([a-zA-Z0-9<_]+[> ]*[&*]*\\) \\(~?[a-z][a-zA-Z0-9_]*\\)[ \t]*(" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "(CXXFUNC){(ZZ)color{clss}{\\1}} {(ZZ)bf(ZZ)colorbox{func}{\\2}}(CXXCNUF)(" 'fixedcase))))

    (checkpoint "checkpoint:13f")

    (when mode-c++
      (goto-char (point-min))
      (while (re-search-forward "\\([ ][ ]\\)\\(~?m?ptr[a-zA-Z0-9_<]*[> ]*\\)(" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1(CXXFUNC){(ZZ)bf(ZZ)colorbox{func}{\\2}}(CXXCNUF)(" 'fixedcase))))

    (when mode-c++
      (goto-char (point-min))
      (while (re-search-forward "\\([(),]\\)\\(~?m?ptr[a-zA-Z0-9_<]*[> ]*\\)(" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1(CXXFUNC){(ZZ)bf(ZZ)colorbox{func}{\\2}}(CXXCNUF)(" 'fixedcase))))

    (when mode-c++
      (goto-char (point-min))
      (while (re-search-forward "~?Sprite[&*]*" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "(CXXCLSS){(ZZ)color{clss}\\&}(CXXSSLC)" 'fixedcase))))

    (checkpoint "checkpoint:13e")

    (when mode-c++
      (goto-char (point-min))
      (while (re-search-forward "\\([(, ]\\)\\(m?ptr[a-zA-Z0-9_<]*[> ]*[&*]*\\) \\([a-z][a-zA-Z0-9_]*\\)\\([ ,)]\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1{(ZZ)color{clss}{\\2}} {(ZZ)color{vari}{\\3}}\\4" 'fixedcase))))

    (checkpoint "checkpoint:13f")

    (when mode-c++
      (goto-char (point-min))
      (while (re-search-forward "\\([(, ]\\)\\([A-Z][a-zA-Z0-9_<]*[> ]*[&*]*\\) \\([a-z][a-zA-Z0-9_]*\\)\\([ ,)]\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1{(ZZ)color{clss}{\\2}} {(ZZ)color{vari}{\\3}}\\4" 'fixedcase))))

    (checkpoint "checkpoint:13g")

    (when mode-c++
      (goto-char (point-min))
      (while (re-search-forward "\\(\\(dmp::\\)?\\(int\\|string\\|string_buffer\\)[&*]*\\)\\([ \t]+\\)\\([a-z][a-zA-Z0-9_]*\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "{(ZZ)color{clss}{\\1}}\\4{(ZZ)color{vari}{\\5}}" 'fixedcase))))

    (checkpoint "checkpoint:13h")

    (when (or (eq major-mode 'emacs-lisp-mode)
              (eq major-mode 'c++-mode))
      (goto-char (point-min))
      (while (re-search-forward (concat "\\<\\(SHOULD_NEVER_HAPPEN\\|ASSERT_INFO\\|"
                                        "EXIT_SUCCESS\\|EXIT_FAILURE\\|ERROR\\|should_never_happen\\|"
                                        "assert\\|ASSERT\\|true\\|false\\|null\\)\\>") nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "{(ZZ)color{red}{\\&}}" 'fixedcase))))

    (checkpoint "checkpoint:13i")

    (d-quote when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "friend \\(dmp::\\)?\\(\\(bool\\|string\\|string_buffer\\|int\\|Writer\\|Reader\\)[&*]*\\) operator \\([^ ]+\\)[ \t]*(" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "{(ZZ)bf{friend}} {(ZZ)color{clss}{\\1\\2}} {(ZZ)bf{operator} {(ZZ)colorbox{func}{\\4}}}(" 'fixedcase))))

    (checkpoint "checkpoint:13j")

    (d-quote when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "\\(\\(Reader\\|Writer\\)[&*]*\\)[ \t]+\\([a-z][a-zA-Z0-9_]*\\)" nil t)
        (replace-match "(CXXCLSS){(ZZ)color{clss}{\\1}}(CXXSSLC) (CXXVARI){(ZZ)color{vari}{\\3}}(CXXIRAV)" 'fixedcase)))

    (checkpoint "checkpoint:13k")

    (d-quote progn
      (goto-char (point-min))
      (while (re-search-forward "\\<\\(m?ptr[a-zA-Z0-9<]*[> ]*[&*]*\\)[ \t]+\\([a-z][a-zA-Z0-9_]*\\)" nil t)
        (if (save-excursion
              (goto-char (max 0 (- (point) 2)))
              (not (d-latexize--inside-comment-or-string)))
            (replace-match "(CXXCLSS){(ZZ)color{clss}\\1}(CXXCLSS) (CXXVARI){(ZZ)color{vari}\\2}(CXXIRAV)" 'fixedcase))))

    (checkpoint "checkpoint:13l")

    (d-quote progn
      (goto-char (point-min))
      (while (re-search-forward "\\<\\(\\(string\\|string_buffer\\)[&*]*\\)[ \t]+\\([a-z][a-zA-Z0-9_]*[ \t]*[^ ()]*\\)(" nil t)
        (if (save-excursion
              (goto-char (max 0 (- (point) 2)))
              (not (d-latexize--inside-comment-or-string)))
            (replace-match "(CXXCLSS){(ZZ)color{clss}\\1}(CXXCLSS) (CXXFUNC){(ZZ)colorbox{func}\\3}(CXXCNUF)" 'fixedcase))))

    (checkpoint "checkpoint:13m")

    (d-quote progn
      (goto-char (point-min))
      (while (re-search-forward "\\<\\(\\(string\\|string_buffer\\)[&*]*\\)[ \t]+\\([a-z][a-zA-Z0-9_]*[ \t]*[^ ()]*\\)" nil t)
        (if (save-excursion
              (goto-char (max 0 (- (point) 2)))
              (not (d-latexize--inside-comment-or-string)))
            (replace-match "(CXXCLSS){(ZZ)color{clss}\\1}(CXXCLSS) (CXXVARI){(ZZ)color{vari}\\3}(CXXIRAV)" 'fixedcase))))

    (checkpoint "checkpoint:13n")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "^[ \t]*\\(#include[ \t]+\\)\\(<[A-Za-z0-9_]+>\\)" nil t)
        ;;(replace-match "(CXXHASH){(ZZ)color{incl}{#include}} (CXXSTRI){(ZZ)color{blue}{\\2}}(CXXIRTS)(CXXHSAH)" 'fixedcase)
        (replace-match "(CXXHASH){(ZZ)colorbox{incl}{#include}} (CXXSTRI){(ZZ)colorbox{blue}{(ZZ)color{white}{\\2}}}(CXXIRTS)(CXXHSAH)" 'fixedcase)
        ))

    (checkpoint "checkpoint:14")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "^[ \t]*\\(#include[ \t]+\\)" nil t)
        (replace-match "(CXXHASH){(ZZ)colorbox{incl}{#include}} (CXXHSAH)" 'fixedcase)))

    (checkpoint "checkpoint:15")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "^[ \t]*\\(#[a-z]+\\)[ \t]+\\([a-zA-Z_][A-Za-z0-9_]*\\)(" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "(CXXHASH){(ZZ)colorbox{incl}{\\1}} {(ZZ)colorbox{func}{\\2}}(CXXHSAH)(" 'fixedcase)
          )))

    (checkpoint "checkpoint:16")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "^[ \t]*\\(#[a-z]+\\)[ \t]+\\([a-zA-Z_][A-Za-z0-9_]*\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "(CXXHASH){(ZZ)colorbox{incl}{\\1}} {(ZZ)color{vari}{\\2}}(CXXHSAH)" 'fixedcase))))

    (checkpoint "checkpoint:17")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "^\\([ \t]*\\)\\(#endif\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1(CXXHASH){(ZZ)colorbox{incl}{#endif}}(CXXHSAH)" 'fixedcase))))

    (checkpoint "checkpoint:17a")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "\\(\\<RLE_SPRITE[&*]*\\)[ \t]+\\([a-z][a-zA-Z0-9_]*\\)" nil t)
        (if (save-excursion
              (not (d-latexize--inside-comment-or-string)))
            (replace-match "(CXXCLSS){(ZZ)color{clss}{\\1}} {(ZZ)color{vari}{\\2}}(CXXSSLC)" 'fixedcase))))

    (checkpoint "checkpoint:17b")

    (d-quote when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "^[ \t]*\\(\\(unsigned\\|signed\\)?[ \t]*\\(void\\|short\\|int\\|long\\|double\\|m?ptr[a-zA-Z0-9_<]*[ >]*\\|[A-Za-z0-9_<]+[ >]*\\)[&*]*[ \t]*\\)?\\([A-Z][a-zA-Z0-9_]*\\)::\\([a-z][a-zA-Z0-9_]*\\)(" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "(CXXCLSS){(ZZ)color{clss}\\1\\4}(CXXSSLC)::(CXXFUNC){(ZZ)bf(ZZ)colorbox{func}{FORT\\5}}(CXXCNUF)(" 'fixedcase))))

    (checkpoint "checkpoint 17c")

    (d-quote when (and (eq major-mode 'c++-mode) mode-c++)
      (goto-char (point-min))
      (while (re-search-forward (concat "\\(^[ \t]*\\(\\(static\\|friend\\|virtual\\)[ \t]+\\)*\\)"
                                        "\\([a-zA-Z0-9_<][> ]*[&*]* \\)operator \\([^ ]+\\)+(") nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1{(CXXFUNC)(ZZ)color{clss}{\\4}}{(ZZ)bf(ZZ)colorbox{func}{{(ZZ)bf{KOPFoperator}}FORT\\5(CXXCNUF)}}(" 'fixedcase))
        ))

    (checkpoint "checkpoint:17d")

    (when (and (eq major-mode 'c++-mode) mode-c++)
      (goto-char (point-min))
      (while (re-search-forward (concat "\\(^[ \t]*\\(\\(static\\|friend\\|virtual\\)[ \t]+\\)+\\)"
                                        "\\(\\([^ (){},=]+ \\)+\\)\\([a-z][a-zA-Z0-9_]*[ \t]*\\)(") nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1{(ZZ)color{clss}{\\4}}{(ZZ)bf(ZZ)colorbox{func}{(CXXFUNC)TORT\\6(CXXCNUF)}}(" 'fixedcase))
        ))

    (when mode-emacs-lisp++

      (checkpoint "checkpoint:18")

      (let (result str)
        (goto-char (point-min))
        ;;(setq count1 0)
        ;;(setq count2 0)
        (while (re-search-forward "(\\(cmethod\\|c-static-method\\|cfunction\\|cfriend\\)" nil t)
          (when (re-search-forward "(cret \\([^()]*\\))" (point-at-eol) t)
            (replace-match "(cret {(ZZ)color{clss}(CXXCLSS){\\1}(CXXSSLC)})" 'fixedcase))
          (when (and (re-search-forward "(cname \\([^()]*\\))" (point-at-eol) t)
                     (not (d-latexize--inside-comment-or-string)))
            (setq str (buffer-substring-no-properties (match-beginning 1) (match-end 1)))
            ;;(save-match-data
            ;;  (if (string-match "operator" str) (message "**** string-match operator str=%s" str)))
            ;;(debug (format "str=%s" str)))
            ;; (setq str "operator<<")
            ;; (setq str "operator>>")
            (cond
             ((save-match-data
                (string-match "operator[ \t]*<<" str))
              (replace-match (format "(cname (CXXFUNC){(ZZ)bf{operator}(ZZ)colorbox{func}{<<}}(CXXCNUF))") 'fixedcase)
              ;;(debug "Calisthenics")
              )
             ((save-match-data
                (string-match "operator[ \t]*>>" str))
              (replace-match (format "(cname (CXXFUNC){(ZZ)bf{operator}(ZZ)colorbox{func}{>>}}(CXXCNUF))") 'fixedcase)
              ;;(debug "Both sides now")
              )
             (t
              (save-match-data
                (if (string-match "operator \\([^ ]*\\)" str)
                    (progn
                      (setq str (substring str (match-beginning 1) (match-end 1)))
                      (setq result t))
                  (setq result nil)
                  (assert (not (string-match "operator" str)))
                  ))
              (if result
                  (replace-match (format "(cname {{(ZZ)bf{operator}}(ZZ)colorbox{func}{(CXXFUNC)%s(CXXCNUF)}})" str) 'fixedcase)
                (replace-match   (format "(cname {(ZZ)bf(ZZ)colorbox{func}{(CXXFUNC)%s(CXXCNUF)}})" str) 'fixedcase)))
              ) ;; end cond
            )))

      (checkpoint "checkpoint:19")

      (progn
        (goto-char (point-min))
        (while (re-search-forward "(\\(c-constructor-method\\|c-destructor-method\\)" nil t)
          (when (re-search-forward "(cname \\([^()]*\\))" (point-at-eol) t)
            (replace-match "(cname {(ZZ)color{clss}(CXXCLSS){\\1}(CXXSSLC)})" 'fixedcase))))

      (checkpoint "checkpoint:20")

      (progn
        (goto-char (point-min))
        (while (re-search-forward "(\\(cglobal\\|cproperty\\|c-static-property\\)" nil t)
          (when (re-search-forward "(ctype \\([^()]*\\))" (point-at-eol) t)
            (replace-match "(cret {(ZZ)color{clss}(CXXCLSS){\\1}(CXXSSLC)})" 'fixedcase))
          (when (re-search-forward "(cname \\([^()]*\\))" (point-at-eol) t)
            (replace-match "(cname {(ZZ)color{vari}(CXXVARI){\\1}(CXXIRAV)})" 'fixedcase))))

      (checkpoint "checkpoint:21")

      ;;(checkpoint "checkpoint:21a")
      ;;(checkpoint "checkpoint:21b")

      (when (eq major-mode 'c++-mode)
        (goto-char (point-min))
        (while (re-search-forward (concat "\\([^a-zA-Z0-9_-]\\)"
                                          "\\(\\(void\\|bool\\|char\\|int\\|"
                                          "float\\|double\\|string\\|string_buffer\\|"
                                          "SAMPLE\\|RLE_SPRITE\\|BITMAP\\|"
                                          "[A-Z]\\|[A-Z]+[a-z][a-zA-Z0-9_]\\|"
                                          "m?ptr[a-zA-Z0-9_<]*[ >]*\\)[&*]*\\)\\([ \t]+\\)"
                                          "\\([^() :,]*\\)(?") nil t)
          (when (save-excursion
                  (goto-char (max 0 (- (point) 2)))
                  (not (d-latexize--inside-comment-or-string)))
            (setq operator (buffer-substring-no-properties (match-beginning 5) (match-end 5)))
            ;;(if (not (string= operator "operator"))
            (replace-match "\\1{(ZZ)color{clss}{(CXXCLSS)\\2(CXXSSLC)}}\\4{(ZZ)color{vari}{(CXXVARI)\\5(CXXIRAV)}}" 'fixedcase))))

    )

    (d-quote when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "\\([^a-zA-Z0-9_{}]\\)\\(_[a-zA-Z0-9_]+\\)" nil t)
        (if (and (not (d-latexize--inside-comment))
                 (not (d-latexize--inside-string)))
            (replace-match "\\1{(ZZ)color{black}(ZZ)colorbox{prvt}{\\2}}" 'fixedcase)
          )
        )
      )

    (checkpoint "checkpoint:17b")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "<" nil t)
        (replace-match "(QQ)<(QQ)" 'fixedcase)))

    (checkpoint "checkpoint:17c")

    (progn
      (goto-char (point-min))
      (while (re-search-forward ">" nil t)
        (replace-match "(QQ)>(QQ)" 'fixedcase)))

    (checkpoint "checkpoint:17d")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "|" nil t)
        (replace-match "(QQ)|(QQ)" 'fixedcase)))

    (checkpoint "checkpoint:22")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "::\\(ctor\\)(" nil t)
        (when (save-excursion
                (goto-char (max 0 (- (point) 2)))
                (not (d-latexize--inside-comment-or-string)))
          (replace-match "::{(ZZ)color{numb}{ctor}}(" 'fixedcase))))

    (checkpoint "checkpoint:22a")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "\\<destroy_\\([a-z][a-zA-Z0-9_]*\\)" nil t)
        (replace-match "(CXXVARI){(ZZ)color{numb}{\\&}}(CXXVARI)" 'fixedcase)))

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "::\\([a-z][a-zA-Z0-9_]*\\)" nil t)
        (setq str (buffer-substring-no-properties (match-beginning 1) (match-end 1)))
        (if (and (not (d-latexize--inside-comment-or-string))
                 (or  (string= str "draw_rle_sprite")
                      (string= str "draw_sprite")
                      (string= str "putpixel")
                      (string= str "getpixel")
                      (string= str "create_rle_sprite")))
            (replace-match "::{(ZZ)color{brown}{\\1}}" 'fixedcase))))

    (checkpoint "checkpoint:22b")
    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "\\<allegro_[a-z][a-zA-Z0-9_]*" nil t)
        (replace-match "::{(ZZ)color{brown}{\\&}}" 'fixedcase)))

    (checkpoint "checkpoint:23")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "\\<delete_current(" nil t)
        (when (not (d-latexize--inside-comment-or-string))
          (replace-match "{(ZZ)color{numb}{delete_current}}(" 'fixedcase))))

    (checkpoint "checkpoint:24")
    (checkpoint "checkpoint:25")
    (checkpoint "checkpoint:26")

    (when mode-emacs-lisp++
      (goto-char (point-min))
      (while (re-search-forward "(cclass \\([a-zA-Z0-9_]*\\)" nil t)
        (replace-match "(cclass {(ZZ)color{clss}{\\1}}" 'fixedcase)))

    (checkpoint "checkpoint:27")

    (when mode-emacs-lisp
      (goto-char (point-min))
      (while (re-search-forward "(defun \\([-a-zA-Z0-9_+*<>:!]*\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "({(ZZ)bf{defun}}{(ZZ)bf(ZZ)colorbox{func}{(CXXFUNC)\\1(CXXCNUF)}}" 'fixedcase)
          ))

      (checkpoint "checkpoint:28")

      (goto-char (point-min))
      (while (re-search-forward "(\\(provide\\|require\\) '\\([-a-zA-Z0-9_+*<>:!]+\\))" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "(BO(\\1){}'{(ZZ)color{red}\\2})")))

      (checkpoint "checkpoint:29")

      (goto-char (point-min))
      (while (re-search-forward "(defmacro \\([-a-zA-Z0-9_+*<>:!]*\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "({(ZZ)bf{}defmacro }{(ZZ)bf(ZZ)colorbox{func}{(CXXFUNC)\\1(CXXCNUF)}}" 'fixedcase)
          ))
      )

    (checkpoint "checkpoint:29a")

    (checkpoint "checkpoint:30")

    (when (eq major-mode 'emacs-lisp-mode)
      (goto-char (point-min))
      (while (re-search-forward "\\<\\(jtw\\|warn\\|d\\)-[-a-zA-Z0-9_+<>:]*" nil t)
        (save-excursion
          (goto-char (max 0 (- (point) 3)))
          (when (not (d-latexize--inside-comment-or-string))
            (replace-match "{(ZZ)color{clss}{\\&}}" 'fixedcase)))))

    (checkpoint "checkpoint:30a")

    (when (eq major-mode 'emacs-lisp-mode)
      (goto-char (point-min))
      (while (re-search-forward "\\<\\(read-str\\)\\>" nil t)
        (save-excursion
          (goto-char (max 0 (- (point) 3)))
          (when (not (d-latexize--inside-comment-or-string))
            (replace-match "{(ZZ)color{clss}{\\&}}" 'fixedcase)))))

    (checkpoint "checkpoint:31")
    (checkpoint "checkpoint:32")

    (when mode-emacs-lisp++
      (while (re-search-forward "(carg\\([^(){}]*\\)[ \t]+\\([^(){}]*\\))" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "(carg{(ZZ)color{clss}{(CXXCLSS)\\1(CXXSSLC)}} {(ZZ)color{vari}{(CXXVARI)\\2(CXXIRAV)}})" 'fixedcase))))

    (checkpoint "checkpoint:33")

    (when (eq major-mode 'c++-mode)
      (while (re-search-forward "\\([^a-zA-Z0-9_{}]\\)\\([A-Z]\\)::" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1{(ZZ)color{red}{\\2}}::" 'fixedcase))))

    (checkpoint "checkpoint:34")

    (when (eq major-mode 'c++-mode)
      (while (re-search-forward "\\([^a-zA-Z0-9_{}]\\)\\(PRINT\\)\\((.*)\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1{(ZZ)color{glbl}{\\2}}\\3" 'fixedcase))))

    (checkpoint "checkpoint:35")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward (concat "\\([^a-zA-Z0-9_{}]\\)\\(\\(void\\|bool\\|char\\|int\\|float\\|"
                                        "double\\|string\\|string_buffer\\|BITMAP\\|RLE_SPRITE\\|SAMPLE\\|"
                                        "m?ptr[a-zA-Z0-9_<]*[> ]*\\)[&*]*[ \t]*\\)(") nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1{(ZZ)color{clss}{(CXXCLSS)\\2(CXXSSLC)}}(" 'fixedcase))))

    (checkpoint "checkpoint:36")

    (when mode-emacs-lisp
      (goto-char (point-min))
      (while (re-search-forward "\\*[a-zA-Z_][-a-zA-Z0-9_]*\\*" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "(CXXGLBL){(ZZ)color{glbl}{\\&}}(CXXLBLG)" 'fixedcase))))

    (checkpoint "checkpoint:37")

    (when mode-emacs-lisp
      (goto-char (point-min))
      (while (re-search-forward "\\<debug\\>" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "{(ZZ)color{numb}{debug}}" 'fixedcase))))

    (checkpoint "checkpoint:38")

    (when (eq major-mode 'c++-mode) ;; \\([(,][ \t\r\n]*\\)
      (goto-char (point-min))       ;; \\([ \t\r\n]*[,)]\\)
      (while (re-search-forward (concat "\\([^a-zA-Z0-9_{}]\\)\\(\\(void\\|bool\\|char\\|int\\|float\\|"
                                        "double\\|string\\|string_buffer\\|BITMAP\\|RLE_SPRITE\\|SAMPLE\\|"
                                        "m?ptr<[A-Za-z0-9_<]*[> ]*\\|[A-Z]\\|"
                                        "[A-Z]+[a-z][a-zA-Z0-9_]*\\)[&*]*[ \t\r\n]+\\)"
                                        "\\([^(),]+\\)") nil t)
        (when (not (d-latexize--inside-comment-or-string))
          (setq operator (buffer-substring-no-properties (match-beginning 4) (match-end 4)))
          (if (not (string-match "^operator" operator))
              (replace-match "\\1{(ZZ)color{clss}{(CXXCLSS)\\2(CXXSSLC)}}{(ZZ)color{vari}{(CXXVARI)\\4(CXXIRAV)}}" 'fixedcase)))))

    (checkpoint "checkpoint:39")

    (when (eq major-mode 'c++-mode) ;; \\([(,][ \t\r\n]*\\)
      (goto-char (point-min))       ;; \\([ \t\r\n]*[,)]\\)
      (while (re-search-forward (concat "\\([^a-zA-Z0-9_{}]\\)\\(~?[A-Z]\\)\\([^a-zA-Z0-9_{}]\\)") nil t)
        (when (not (d-latexize--inside-comment-or-string))
          (replace-match "\\1{(ZZ)color{clss}{\\2}}\\3" 'fixedcase))))

    (checkpoint "checkpoint:40")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward (concat "(\\(void\\|bool\\|char\\|int\\|float\\|double\\|"
                                        "string\\|string_buffer\\|SAMPLE\\|RLE_SPRITE\\|BITMAP\\|[A-Z]\\|"
                                        "_?[A-Z]+[a-z][a-zA-Z0-9_]*\\)\\([&*]*\\))") nil t)
        (replace-match "({(ZZ)color{clss}{(CXXCLSS)\\1\\2(CXXSSLC)}})" 'fixedcase)))

    (checkpoint "checkpoint:41")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward (concat "\\([^a-zA-Z0-9_-{}]\\)\\(\\(void\\|bool\\|"
                                        "char\\|int\\|float\\|double\\|string\\|BITMAP\\|"
                                        "SAMPLE\\|string_buffer\\|"
                                        "RLE_SPRITE\\|m?ptr<[A-Za-z0-9_<>]*[> ]*\\)[*&]*\\)"
                                        "\\([ \t\r\n]+\\)\\([^(),]+\\)") nil t)
        (when (save-excursion
                (goto-char (max 0 (- (point) 3)))
                (not (d-latexize--inside-comment-or-string)))
          (setq variable (buffer-substring-no-properties (match-beginning 5) (match-end 5)))
          ;;(if (string= variable "operator") (debug "Xenophobe"))
          (if (not (string-match "^operator" variable))
              (replace-match "\\1{(ZZ)color{clss}{(CXXCLSS)\\2(CXXSSLC)}(ZZ)color{vari}{(CXXVARI)\\4\\5(CXXIRAV)}}" 'fixedcase)))))

    (checkpoint "checkpoint:42")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "\\(\\<DCODE_[A-Z0-9_]*\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "{(ZZ)color{green}\\1}" 'fixedcase)
          ;;(replace-match "{(ZZ)colorbox{green}{(ZZ)color{white}\\1}}" 'fixedcase)
          )
        )
      )

    (checkpoint "checkpoint:42a")
    (checkpoint "checkpoint:43")

    (when mode-emacs-lisp++
      (goto-char (point-min))
      (while (re-search-forward "\\<\\([A-Z]\\)\\([:(]\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "{(ZZ)color{clss}{\\1}}\\2" 'fixedcase))))

    (checkpoint "checkpoint:44")

    (when (or mode-emacs-lisp++ mode-c++)
      (goto-char (point-min))
      (while (re-search-forward "\\([^a-zA-Z0-9_]\\)\\(~?[A-Z]+[a-z][A-Za-z0-9_<]*[ >]*[&*]*\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1{(ZZ)color{clss}{\\2}}" 'fixedcase))))

    (checkpoint "checkpoint:45")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "\\^" nil t)
        (replace-match "(ZZ)^{}" 'fixedcase)))

    (checkpoint "checkpoint:45a")

    (when mode-emacs-lisp
      (goto-char (point-min))
      (while (re-search-forward "\\([^-a-zA-Z0-9{}_]\\)\\(ptr[-a-zA-Z0-9_+<>]*[ >]*\\)" nil t)
        (if (save-excursion
              (goto-char (max 0 (- (point) 2)))
              (not (d-latexize--inside-comment-or-string)))
            (replace-match "\\1{(ZZ)color{blue}(CXXCLSS){\\2}(CXXSSLC)}" 'fixedcase))))

    (checkpoint "checkpoint:46")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "\\([^a-zA-Z0-9_{}]\\)\\(_[a-zA-Z0-9_]*\\)" nil t)
        (if (and (not (d-latexize--inside-comment))
                 (not (d-latexize--inside-string)))
            (replace-match "\\1{(CXXPRVT)(ZZ)colorbox{prvt}{(ZZ)color{black}{\\2}}(CXXTVRP)}" 'fixedcase)
          )))

    (checkpoint "checkpoint:47")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "\\([^a-zA-Z0-9_{}]\\)\\(private_[A-za-z0-9_]*\\)" nil t)
        (if (and (not (d-latexize--inside-comment))
                 (not (d-latexize--inside-string)))
            (replace-match "\\1{(CXXPRVT)(ZZ)colorbox{prvt}{(ZZ)color{black}{\\2}}(CXXTVRP)}" 'fixedcase)
          )))

    (checkpoint "checkpoint:48")

    (when mode-emacs-lisp
      (goto-char (point-min))
      (while (progn
               (goto-char (max 0 (- (point) 2)))
               (re-search-forward "\\([^-a-zA-Z0-9_]\\)\\(\\(global\\|GLOBAL\\)[a-zA-Z0-9_+-]*\\)\\([^a-zA-Z0-9_-]\\)"
                                  nil t))
        (if (not (save-excursion
                   (goto-char (max 0 (- (point) 4)))
                   (d-latexize--inside-comment-or-string)))
            (replace-match "\\1{(ZZ)color{glbl}{\\2}}\\4" 'fixedcase))))

    (checkpoint "checkpoint:49")

    (when (or mode-emacs-lisp++ mode-c++)
      (goto-char (point-min))
      (while (re-search-forward "\\([^-a-zA-Z0-9_]\\)\\(\\(global\\|GLOBAL\\)[a-zA-Z0-9_]*\\)\\([^a-zA-Z0-9_]\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1{(ZZ)color{glbl}{\\2}}\\4" 'fixedcase))))

    (checkpoint "checkpoint:50")

    (when mode-emacs-lisp
      (goto-char (point-min))
      (while (re-search-forward "\\([^-a-zA-Z0-9_]\\)\\(\\(prop\\|PROP\\)[-a-zA-Z0-9_]*\\)\\([^-a-zA-Z0-9_]\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1{(ZZ)color{prop}{\\2}}\\4" 'fixedcase))))

    (checkpoint "checkpoint:51")

    (when (eq major-mode 'c++-mode) ;;or mode-emacs-lisp++ mode-c++)
      (goto-char (point-min))
      (while (re-search-forward "\\([^a-zA-Z0-9_-]\\)\\(\\(prop\\|PROP\\)[a-zA-Z0-9_]*\\)\\([^a-zA-Z0-9_-]\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1{(ZZ)color{prop}{\\2}}\\4" 'fixedcase))))

    (checkpoint "checkpoint:52")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "\\([^{}]*\\)\\(operator\\) *\\([^ (){},]*\\) *(" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "{(ZZ)color{clss}{\\1}}{(ZZ)bf{}operator}{(ZZ)colorbox{func}{(CXXFUNC)\\3(CXXCNUF)}}(" 'fixedcase)
          )))

    (checkpoint "checkpoint:53")
    (checkpoint "checkpoint:54")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "namespace \\([a-zA-Z0-9_]+\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "namespace {(ZZ)color{red}{\\1}}" 'fixedcase))))

    (checkpoint "checkpoint:55")

    (when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "\\(^\\|[^a-zA-Z0-9_]\\)\\(dmp\\|std\\)::" nil t)
        (if (and (not (d-latexize--inside-comment))
                 (not (d-latexize--inside-string)))
            (replace-match "\\1{(ZZ)color{red}{\\2}}::" 'fixedcase))))

    (checkpoint "checkpoint:56")

    (when (eq major-mode 'c++-mode) ;;mode-emacs-lisp++ mode-c++)

      (progn
        (goto-char (point-min))
        (while (re-search-forward "^[ \t]*\\(public\\|private\\|protected\\):" nil t)
          (replace-match "{(ZZ)color{black}(ZZ)bf{\\&}}")))

      (checkpoint "checkpoint:56a")

      (checkpoint "checkpoint:56b")

      (progn
        (goto-char (point-min))
        (while (re-search-forward "virtual" nil t)
          (if (and (not (d-latexize--inside-comment))
                   (not (d-latexize--inside-string)))
              (replace-match "{(ZZ)color{black}(ZZ)bf{virtual}}" 'fixedcase))))

      (checkpoint "checkpoint:56c")

      )

    (checkpoint "checkpoint:57")

    (goto-char (point-min))
    (while (re-search-forward "\\\\" nil t)
      (replace-match "(QQ)(ZZ)backslash(QQ)" 'fixedcase))

    (checkpoint "checkpoint:58")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "\\$" nil t)
        (replace-match "(ZZ)$" 'fixedcase)))

    (checkpoint "checkpoint:59")

    (progn
      (goto-char (point-min))
      (while (search-forward "(QQ)" nil t)
        (replace-match "$" 'fixedcase)))

    (checkpoint "checkpoint:60")

    (progn
      (goto-char (point-min))
      (while (re-search-forward (concat
                                 "\\([^a-zA-Z0-9_.-]\\)\\(*?c-[-a-zA-Z0-9_+*<>]*\\|cret\\|ctype\\|"
                                 "\\*[a-zA-Z0-9]*\\*\\|cname\\|cargs?\\|cinit\\|cprogn\\|"
                                 "cmethod\\|cproperty\\|cpause\\|"
                                 "cfor\\|cforever\\|cwhile\\|cdo\\|cfunction\\|"
                                 "c-public-extends\\|c-private-extends\\|"
                                 "cautogc\\|cif[?]?\\|celseif\\|celse\\|"
                                 "cswitch\\|cbrackets\\|d-quote\\|"
                                 "cenum\\|citems\\|citem\\|"
                                 "cdefine\\|cglobal\\|cclass\\|"
                                 "s\\|f\\|sc\\|clet[*]?\\|cassert\\|"
                                 "c-raw-c++\\|c-raw-h++\\|c-raw-global-h++\\|"
                                 "cnamespace\\|cfriend\\|ctypedef\\|cpublic\\|"
                                 "cprivate\\|cprotected\\)\\([^a-zA-Z0-9_.-]\\)") nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "\\1{(ZZ)color{greenkeyw}{\\2}}\\3" 'fixedcase))))

    (checkpoint "checkpoint:61")

    (when mode-emacs-lisp
      (goto-char (point-min))
      (while (re-search-forward "&[a-zA-Z_][a-zA-Z0-9_]*" nil t)
        (when (not (d-latexize--inside-comment-or-string))
          (replace-match "{(ZZ)color{red}{\\&}}"))))

    (checkpoint "checkpoint:62")

    (when mode-emacs-lisp
      (goto-char (point-min))
      (while (re-search-forward "\\<\\(defvar\\|defmacro\\|defun\\|let\\|let\\*\\|if\\|when\\|while\\|save-excursion\\|progn\\|unwind-protect\\|condition-case\\|save-match-data\\)\\>" nil t)
        (when (not (d-latexize--inside-comment-or-string))
          (replace-match "{(ZZ)bf{\\&}}"))))

    (checkpoint "checkpoint:63")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "\\([a-z]*\\)::\\(cout\\|cerr\\)" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "{(ZZ)color{red}{(ZZ)bf{}\\1}}::\\2" 'fixedcase))))

    (checkpoint "checkpoint:64")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "_" nil t)
        (replace-match "(ZZ)_" 'fixedcase)))

    (checkpoint "checkpoint:65")
    (checkpoint "checkpoint:66")

    (goto-char (point-min))
    (while (re-search-forward "\\([^-a-zA-Z0-9_]\\)\\(-?[0-9]+\\(\\.[0-9]+\\)?\\)\\([^a-zA-Z0-9_]\\)" nil t)
      (when (save-excursion
              (goto-char (max 0 (- (point) 2)))
              (not (d-latexize--inside-comment-or-string)))
        (replace-match "\\1{(ZZ)color{numb}{\\2}}\\4" 'fixedcase)
        (goto-char (max 0 (- (point) 2)))))

    (checkpoint "checkpoint:67")

    (goto-char (point-min))
    (while (re-search-forward "\\([^-a-zA-Z0-9_]\\)\\(0[Xx][0-9A-Fa-f]+\\)\\([^-a-zA-Z0-9_]\\)" nil t)
      (when (save-excursion ;;0xDEADBEEF
              (goto-char (max 0 (- (point) 2)))
              (not (d-latexize--inside-comment-or-string)))
        (replace-match "\\1{(ZZ)color{numb}{\\2}}\\3" 'fixedcase)))

    (checkpoint "checkpoint:68")

    (goto-char (point-min))
    (while (re-search-forward "~" nil t)
      (replace-match "(ZZ)~{}" 'fixedcase))

    (checkpoint "checkpoint:69")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "&" nil t)
        (replace-match "\\\\&" 'fixedcase)))

    (checkpoint "checkpoint:70")
    (checkpoint "checkpoint:71")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "#" nil t)
        (replace-match "\\\\#" 'fixedcase)))

    (checkpoint "checkpoint:72")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "%" nil t)
        (replace-match "\\\\%" 'fixedcase)))

    (checkpoint "checkpoint:73")

    (let (count)
      (setq count 0)
      (goto-char (point-min))
      (setq quoted-dollar (regexp-quote "$"))
      (setq count-plus-100 (+ count 100))
      (while (re-search-forward " " nil t)
        (when (save-match-data
                (and (save-excursion
                       (goto-char (max 0 (- (point) 1)))
                       (not (looking-at quoted-dollar)))
                     (not (looking-at quoted-dollar))))
          (replace-match "\\\\p{}" 'fixedcase)
          (when (> count count-plus-100)
            (checkpoint (format "checkpoint:73a %s%%" (/ (* 100 (point)) (point-max))))
            (setq count-plus-100 (+ count 100)))
          (incf count)
          )))

    (checkpoint "checkpoint:74")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "(ZZ)" nil t)
        (replace-match "\\\\" 'fixedcase)))

    (checkpoint "checkpoint:75")

    (progn
      (goto-char (point-min))
      (while (and (re-search-forward "^.+$" nil t) (not (eobp)))
        (replace-match "\\\\noindent \\& \\\\\\\\" 'fixedcase)
        (goto-char (max 0 (- (point) 1)))))

    (checkpoint "checkpoint:76")

    (progn
      (goto-char (point-min))
      (while (search-forward "(CXXCOMM)" nil t)
        (replace-match "" 'fixedcase))
      (goto-char (point-min))
      (while (search-forward "(CXXMMOC)" nil t)
        (replace-match "" 'fixedcase)))

    (checkpoint "checkpoint:77")

    (progn
      (goto-char (point-min))
      (while (search-forward "(CXXSTRI)" nil t)
        (replace-match "" 'fixedcase))
      (goto-char (point-min))
      (while (search-forward "(CXXIRTS)" nil t)
        (replace-match "" 'fixedcase)))

    (checkpoint "checkpoint:78")

    (progn
      (goto-char (point-min))
      (while (search-forward "(CXXFUNC)" nil t)
        (replace-match "" 'fixedcase))
      (goto-char (point-min))
      (while (search-forward "(CXXCNUF)" nil t)
        (replace-match "" 'fixedcase)))

    (checkpoint "checkpoint:79")

    (progn
      (goto-char (point-min))
      (while (search-forward "(CXXHASH)" nil t)
        (replace-match "" 'fixedcase))
      (goto-char (point-min))
      (while (search-forward "(CXXHSAH)" nil t)
        (replace-match "" 'fixedcase)))

    (checkpoint "checkpoint:80")

    (progn
      (goto-char (point-min))
      (while (search-forward "(CXXPRVT)" nil t)
        (replace-match "" 'fixedcase))
      (goto-char (point-min))
      (while (search-forward "(CXXTVRP)" nil t)
        (replace-match "" 'fixedcase)))

    (checkpoint "checkpoint:81")

    (progn
      (goto-char (point-min))
      (while (search-forward "(CXXGLBL)" nil t)
        (replace-match "" 'fixedcase))
      (goto-char (point-min))
      (while (search-forward "(CXXLBLG)" nil t)
        (replace-match "" 'fixedcase)))

    (checkpoint "checkpoint:82")

    (d-quote progn
      (goto-char (point-min))
      (while (search-forward "(CXXJTW)" nil t)
        (replace-match "" 'fixedcase))
      (goto-char (point-min))
      (while (search-forward "(CXXWTJ)" nil t)
        (replace-match "" 'fixedcase)))

    (checkpoint "checkpoint:83")

    (progn
      (goto-char (point-min))
      (while (search-forward "(CXXVARI)" nil t)
        (replace-match "" 'fixedcase))
      (goto-char (point-min))
      (while (search-forward "(CXXIRAV)" nil t)
        (replace-match "" 'fixedcase)))

    (checkpoint "checkpoint:84")

    (progn
      (goto-char (point-min))
      (while (search-forward "(CXXCLSS)" nil t)
        (replace-match "" 'fixedcase))
      (goto-char (point-min))
      (while (search-forward "(CXXSSLC)" nil t)
        (replace-match "" 'fixedcase)))

    (checkpoint "checkpoint:85")

    (write-file (concat *target* ".tex"))
    (kill-buffer *buf*)

    (message "*** end of file")
    ))

(provide 'd-latexize)
;;; d-latexize.el ends here
-

(d-quote when (eq major-mode 'c++-mode)
      (goto-char (point-min))
      (while (re-search-forward "friend \\(dmp::\\)?\\(\\(bool\\|string\\|string_buffer\\|int\\|Writer\\|Reader\\)[&*]*\\) operator \\([^ ]+\\)[ \t]*(" nil t)
        (if (not (d-latexize--inside-comment-or-string))
            (replace-match "{(ZZ)bf{friend}} {(ZZ)color{red}{\\1}}{(ZZ)color{clss}{\\2}} {(ZZ)bf{operator} {(ZZ)colorbox{func}{\\4}}}(" 'fixedcase))))
