(setq load-path (cons "~/dlisp/" load-path))

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

(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-comment-or-string ()
  (or (d-latexize--inside-comment)
      (d-latexize--inside-string)
      (d-latexize--inside-func)
      (d-latexize--inside-hash)
      (d-latexize--inside-prvt)
      ))

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

(let ((case-fold-search nil))

  (setq debug-on-error t)

  (checkpoint "checkpoint:1")

  (when (not (boundp 'target))
    ;;(setq target "d:/home/3-libd/lisp++2c++-lisp++2c++.el")
    (setq target "d:/home/3-libd/lisp++2c++-cclass.el")
    ;;(setq target "d:/home/3-libd/2012/Arithmetickles/arithmetickles.lisp++")
    ;;(setq target "~/3-libd/2012/Arithmetickles/glyph_a.lisp++")
    )

  (checkpoint "checkpoint:2")

  (assert (boundp 'target))
  (assert 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))
      (setq mode-c++ t))

  (checkpoint "checkpoint:4")

  (setq *buf* (find-file target))
  (read-only-mode -1)
  (write-file (concat target ".tex"))

  ;;(fundamental-mode)
  ;;(message "**** mode-emacs-lisp++=%s" mode-emacs-lisp++)

  (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 "*** d-latexize found file=%s" target)
  (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 "^[ \t]*#" nil t)
      (replace-match "(Zero)#"))
    ;;(d-beeps "found #")
    )

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

  (checkpoint "checkpoint:5")

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

  (checkpoint "checkpoint:6")

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

  (checkpoint "checkpoint:7")

  (let ((bcount 0))
    (goto-char (point-min))
    (while (re-search-forward "$\\\\backslash$" nil t)
      (replace-match "$\\\\forwardslash$")
      (incf bcount)
      (message "bcount=%d" bcount)))

  (when mode-emacs-lisp
    (goto-char (point-min))
    (while (re-search-forward ";;" nil t)
      (save-excursion
        (forward-char -2)
        ;;(delete-region (point) (1+ (point-at-eol)))
        (insert "(CXXCOMM){(Zero)color{comm}{")
        (end-of-line)
        ;;(forward-char 1)
        (insert "}}(CXXMMOC)")))
    )

  (checkpoint "checkpoint:8")

  (when (or mode-emacs-lisp++ mode-c++)
    (goto-char (point-min))
    (while (re-search-forward "//" nil t)
      (forward-char -2)
      (insert "(CXXCOMM){(Zero)color{comm}{")
      (end-of-line)
      ;;(forward-char 1)
      (insert "}}(CXXMMOC)"))

    (checkpoint "checkpoint:9")

    (goto-char (point-min))
    (while (re-search-forward "/[*]" nil t)
      (save-excursion
        (forward-char -2)
        (insert "(CXXCOMM){(Zero)color{comm}{")
        (re-search-forward "*/" nil t)
        (insert "}}(CXXMMOC)")))
    )

  (checkpoint "checkpoint:10")

  (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){(Zero)color{blue}{\\2}}(CXXIRTS)" 'fixedcase)
          (replace-match "\\1(CXXSTRI){(Zero)colorbox{blue}{(Zero)color{white}{\\2}}}(CXXIRTS)" 'fixedcase)
        )))

  (checkpoint "checkpoint:11")

  (if (eq major-mode 'c++-mode)
      (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){(Zero)color{blue}{\\2}}(CXXIRTS)" 'fixedcase)
        (replace-match "\\1(CXXSTRI){(Zero)colorbox{blue}{(Zero)color{white}{\\2}}}(CXXIRTS)" 'fixedcase)
        )))

  (checkpoint "checkpoint:12")

  (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){(Zero)colorbox{incl}{#include}} (CXXSTRI){(Zero)colorbox{blue}{(Zero)color{white}{\\2}}}(CXXIRTS)(CXXHSAH)" 'fixedcase)
      ))

  (checkpoint "checkpoint:13")

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

  (checkpoint "checkpoint:14")

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

  (checkpoint "checkpoint:16")

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

  (checkpoint "checkpoint:17")

  (when (eq major-mode 'c++-mode) ;;mode-emacs-lisp++
    (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_<]*[ >]*\\)[&*]*\\)\\( +\\)"
                                      "\\([A-Za-z0-9_]*\\)") nil t)
      (when (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{(Zero)color{clss}{\\2}}\\4{(Zero)color{vari}{\\5}}" 'fixedcase)))))

  (checkpoint "checkpoint:18")

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

  (checkpoint "checkpoint:19")

  (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 "({(Z)bf{}defun} {(Z)color{func}{(CXXFUNC)\\1(CXXCNUF)}}" 'fixedcase)
        (replace-match "({(Zero)bf{}defun} {(Zero)colorbox{func}{(CXXFUNC)\\1(CXXCNUF)}}" 'fixedcase)
        ))

    (checkpoint "checkpoint:20")

    (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 "({(Zero)bf{}defmacro} {(Zero)color{func}{(CXXFUNC)\\1(CXXCNUF)}}" 'fixedcase)
        (replace-match "({(Zero)bf{}defmacro} {(Zero)colorbox{func}{(CXXFUNC)\\1(CXXCNUF)}}" 'fixedcase)
        ))
    )

  (checkpoint "checkpoint:21")

  (when (eq major-mode 'c++-mode)
    (goto-char (point-min))
    (while (re-search-forward "class \\([A-Za-z0-9_]+\\)" nil t)
      (replace-match "{(Zero)bf{}class} {(Zero)color{clss}{\\1}}" 'fixedcase)))

  (checkpoint "checkpoint:22")

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

  (checkpoint "checkpoint:23")

  (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{(Zero)color{red}{\\2}}::" 'fixedcase))))

  (checkpoint "checkpoint:24")

  (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{(Zero)color{glbl}{\\2}}\\3" 'fixedcase))))

  (checkpoint "checkpoint:25")

  (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_<]*[> ]*\\)[&*]* *\\)(") nil t)
      (if (not (d-latexize--inside-comment-or-string))
          (replace-match "\\1{(Zero)color{clss}{\\2}}(" 'fixedcase))))

  (checkpoint "checkpoint:26")

  (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]+\\)"
                                      "\\([a-zA-Z0-9_]+\\)") 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= "operator" operator))
            (replace-match "\\1{(Zero)color{clss}{\\2}}{(Zero)color{vari}{\\4}}" 'fixedcase)))))

  (checkpoint "checkpoint:27")

  (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{(Zero)color{clss}{\\2}}\\3" 'fixedcase))))

  (checkpoint "checkpoint:28")

  (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-PR-Y]\\|"
                                      "_?[A-Z]+[a-z][a-zA-Z0-9_]*\\)\\([&*]*\\))") nil t)
      (replace-match "({(Zero)color{clss}{\\1\\2}})" 'fixedcase)))

  (checkpoint "checkpoint:29")

  (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]+\\)\\([_a-zA-Z0-9]+\\)") nil t)
      (when (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= variable "operator"))
            (replace-match "\\1{(Zero)color{clss}{\\2}(Zero)color{vari}{\\4\\5}}" 'fixedcase)))))

  (checkpoint "checkpoint:30")

  (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 "{(Zero)color{green}\\1}" 'fixedcase)
        ;;(replace-match "{(Zero)colorbox{green}{(Zero)color{white}\\1}}" 'fixedcase)
        )
      )
    )

  (checkpoint "checkpoint:31")

  (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 "{(Zero)color{clss}{\\1}}\\2" 'fixedcase))))

  (checkpoint "checkpoint:32")

  (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{(Zero)color{clss}{\\2}}" 'fixedcase))))

  (checkpoint "checkpoint:33")

  (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
            (forward-char -2)
            (not (d-latexize--inside-comment-or-string)))
          (replace-match "\\1{(Zero)color{blue}{\\2}}" 'fixedcase))))

  (checkpoint "checkpoint:34")

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

  (checkpoint "checkpoint:35")

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

  (checkpoint "checkpoint:36")

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

  (checkpoint "checkpoint:37")

  (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{(Zero)color{glbl}{\\2}}\\4" 'fixedcase))))

  (checkpoint "checkpoint:38")

  (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{(Zero)color{prop}{\\2}}\\4" 'fixedcase))))

  (checkpoint "checkpoint:39")

  (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{(Zero)color{prop}{\\2}}\\4" 'fixedcase))))

  (checkpoint "checkpoint:40")

  ;;(message "**** major-mode=%s" major-mode)

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

  (checkpoint "checkpoint:41")

  (when (eq major-mode 'c++-mode) ;;(or mode-emacs-lisp++ mode-c++)
    (goto-char (point-min))
    (while (re-search-forward (concat "\\([^a-zA-Z0-9_-]\\)\\(SHOULD_NEVER_HAPPEN\\|ASSERT_INFO\\|"
                                      "EXIT_SUCCESS\\|EXIT_FAILURE\\|ERROR\\|should_never_happen\\|"
                                      "assert\\|ASSERT\\|true\\|false\\|null\\)\\([^a-zA-Z0-9_-]\\)") nil t)
      (if (not (d-latexize--inside-comment-or-string))
          (replace-match "\\1{(Zero)color{red}{\\2}}\\3" 'fixedcase))))

  (checkpoint "checkpoint:42")

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

  (checkpoint "checkpoint:43")

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

  (save-buffer)
  (kill-emacs)

  (checkpoint "checkpoint:44")

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

  (checkpoint "checkpoint:45")

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

  (checkpoint "checkpoint:46")

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

  (checkpoint "checkpoint:47")

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

  (checkpoint "checkpoint:48")

  (checkpoint "checkpoint:49")

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

  (checkpoint "checkpoint:50")

  (when mode-emacs-lisp++

    (checkpoint "checkpoint:51")

    (progn
      (goto-char (point-min))
      (while (re-search-forward "(\\(cmethod\\|c-static-method\\|cfunction\\)" nil t)
        (when (re-search-forward "(cret \\([^()]*\\))" (point-at-eol) t)
          (replace-match "(cret {\\\\color{clss}{\\1}})" 'fixedcase))
        (when (re-search-forward "(cname \\([^()]*\\))" (point-at-eol) t)
          ;;(replace-match "(cname {\\\\color{func}(CXXFUNC){\\1}(CXXCNUF)})" 'fixedcase)
          (replace-match "(cname {\\\\colorbox{func}(CXXFUNC){\\1}(CXXCNUF)})" 'fixedcase)
          )))

    (checkpoint "checkpoint:52")

    (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 {\\\\color{clss}{\\1}})" 'fixedcase))))

    (checkpoint "checkpoint:53")

    (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 {\\\\color{clss}{\\1}})" 'fixedcase))
        (when (re-search-forward "(cname \\([^()]*\\))" (point-at-eol) t)
          (replace-match "(cname {\\\\color{vari}{\\1}})" 'fixedcase))))

    (checkpoint "checkpoint:54")

    )

  (checkpoint "checkpoint:55")

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

  (checkpoint "checkpoint:56")

  (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 "{\\\\color{red}{\\\\bf{}\\1}}::\\2" 'fixedcase))))

  (checkpoint "checkpoint:57")

  (progn
    (goto-char (point-min))
    (while (re-search-forward (concat
                               "\\([^a-zA-Z0-9_.-]\\)\\(c-[a-zA-Z0-9_+-]*\\|cret\\|ctype\\|"
                               "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++\\|"
                               "cfriend\\|ctypedef\\|cpublic\\|"
                               "cprivate\\|cprotected\\)\\([^a-zA-Z0-9_.-]\\)") nil t)
      (if (not (d-latexize--inside-comment-or-string))
          (replace-match "\\1{\\\\color{greenkeyw}{\\2}}\\3" 'fixedcase))))

  (checkpoint "checkpoint:58")

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

  (checkpoint "checkpoint:59")

  (progn
    (goto-char (point-min))
    (while (re-search-forward "\\*[a-z-]+\\*" nil t)
      (if (not (d-latexize--inside-comment-or-string))
          (replace-match "{\\\\color{glbl}{\\&}}" 'fixedcase))))

  (checkpoint "checkpoint:60")

  (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
            (forward-char -2)
            (not (d-latexize--inside-comment-or-string)))
      (replace-match "\\1{\\\\color{numb}{\\2}}\\4" 'fixedcase)
      (forward-char -1)))

  (checkpoint "checkpoint:61")

  (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
            (forward-char -2)
            (not (d-latexize--inside-comment-or-string)))
      (replace-match "\\1{\\\\color{numb}{\\2}}\\3" 'fixedcase)))

  (checkpoint "checkpoint:62")

  (d-quote progn
    (goto-char (point-min))
    (while (re-search-forward "(FROBNICATE)" nil t)
      (replace-match "#")))

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

  (checkpoint "checkpoint:63")

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

  (checkpoint "checkpoint:64")

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

  (checkpoint "checkpoint:65")

  (checkpoint "checkpoint:66")

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

  (checkpoint "checkpoint:67")

  (progn
    (goto-char (point-min))
    (setq quoted-dollar (regexp-quote "$"))
    (while (re-search-forward " " nil t)
      (if (save-match-data
            (and (save-excursion
                   (forward-char -1)
                   (not (looking-at quoted-dollar)))
                 (not (looking-at quoted-dollar))))
          (replace-match "\\\\phantom{a}" 'fixedcase))))

  (checkpoint "checkpoint:68")
  ;;(save-buffer)

  (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:69")

  (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:70")

  (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:71")

  (let (count)
    (setq hash-count 0)
    (goto-char (point-min))
    (while (search-forward "(CXXHASH)" nil t)
      (replace-match "" 'fixedcase)
      (incf hash-count)
      (message "hash-count=%d" hash-count)
      )
    (goto-char (point-min))
    (while (search-forward "(CXXHSAH)" nil t)
      (replace-match "" 'fixedcase)
      (incf hash-count)
      (message "hash-count=%d" hash-count)))

  (checkpoint "checkpoint:72")

  (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:73")

  (progn
    (goto-char (point-min))
    (while (re-search-forward "<" nil t)
      (replace-match "$<$"))
    (goto-char (point-min))
    (while (re-search-forward ">" nil t)
      (replace-match "$>$"))
    )

  (checkpoint "checkpoint:74")

  (save-buffer)
  (kill-buffer *buf*)

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

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