From 518e22edfb3705e1c65b4a5cd3aed96cca1a4cc7 Mon Sep 17 00:00:00 2001 From: scemama Date: Sat, 6 Mar 2021 22:27:29 +0000 Subject: [PATCH] =?UTF-8?q?Deploying=20to=20gh-pages=20from=20@=20TREX-CoE?= =?UTF-8?q?/qmckl@a948aa6c4b58a2f202583278e1ce1d8f2fb97eab=20=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmodules | 3 + LICENSE | 29 + README.md | 28 + TODO.org | 21 + .gitignore => docs/.gitignore | 0 config.el => docs/config.el | 0 emacs-htmlize/.gitignore | 1 + emacs-htmlize/NEWS | 48 + emacs-htmlize/README.md | 36 + emacs-htmlize/htmlize.el | 1882 ++++++++++++++++++++++++++++++ emacs-htmlize/htmlize.el.html | 2032 +++++++++++++++++++++++++++++++++ src/.gitignore | 11 + src/Makefile | 63 + src/README.org | 246 ++++ src/qmckl.org | 36 + src/qmckl_ao.org | 751 ++++++++++++ src/qmckl_context.org | 922 +++++++++++++++ src/qmckl_distance.org | 356 ++++++ src/qmckl_error.org | 193 ++++ src/qmckl_footer.org | 18 + src/qmckl_memory.org | 117 ++ src/qmckl_precision.org | 21 + src/table_of_contents | 9 + src/test_qmckl.org | 85 ++ to_be_processed/.gitignore | 0 tools/create_doc.sh | 47 + tools/create_makefile.sh | 98 ++ tools/init.el | 80 ++ tools/merge_org.sh | 8 + tools/nb_to_org.sh | 11 + tools/rename.py | 44 + 31 files changed, 7196 insertions(+) create mode 100644 .gitmodules create mode 100644 LICENSE create mode 100644 README.md create mode 100644 TODO.org rename .gitignore => docs/.gitignore (100%) rename config.el => docs/config.el (100%) create mode 100644 emacs-htmlize/.gitignore create mode 100644 emacs-htmlize/NEWS create mode 100644 emacs-htmlize/README.md create mode 100644 emacs-htmlize/htmlize.el create mode 100644 emacs-htmlize/htmlize.el.html create mode 100644 src/.gitignore create mode 100644 src/Makefile create mode 100644 src/README.org create mode 100644 src/qmckl.org create mode 100644 src/qmckl_ao.org create mode 100644 src/qmckl_context.org create mode 100644 src/qmckl_distance.org create mode 100644 src/qmckl_error.org create mode 100644 src/qmckl_footer.org create mode 100644 src/qmckl_memory.org create mode 100644 src/qmckl_precision.org create mode 100644 src/table_of_contents create mode 100644 src/test_qmckl.org create mode 100644 to_be_processed/.gitignore create mode 100755 tools/create_doc.sh create mode 100755 tools/create_makefile.sh create mode 100644 tools/init.el create mode 100755 tools/merge_org.sh create mode 100755 tools/nb_to_org.sh create mode 100755 tools/rename.py diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..8ad4907 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "munit"] + path = munit + url = https://github.com/nemequ/munit/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2ad479f --- /dev/null +++ b/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2020, TREX Center of Excellence +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e64cfb3 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# QMCkl: Quantum Monte Carlo Kernel Library + +![Build Status](https://github.com/TREX-CoE/qmckl/workflows/test-build/badge.svg?branch=main) + +The domain of quantum chemistry needs a library in which the main +kernels of Quantum Monte Carlo (QMC) methods are implemented. In the +library proposed in this project, we expose the main algorithms in a +simple language and provide a standard API and tests to enable the +development of high-performance QMCkl implementations taking +advantage of modern hardware. + +See the [source code](https://github.com/TREX-CoE/qmckl/tree/main/src) +to read the documentation. + + +To clone the repository, use: +``` +git clone --recursive https://github.com/TREX-CoE/qmckl.git +``` +to dowload also the [munit](https://github.com/nemequ/munit) unit testing +framework. + + +------------------------------ + +![European flag](https://trex-coe.eu/sites/default/files/inline-images/euflag.jpg) +[TREX: Targeting Real Chemical Accuracy at the Exascale](https://trex-coe.eu) project has received funding from the European Union’s Horizon 2020 - Research and Innovation program - under grant agreement no. 952165. The content of this document does not represent the opinion of the European Union, and the European Union is not responsible for any use that might be made of such content. + diff --git a/TODO.org b/TODO.org new file mode 100644 index 0000000..b565b64 --- /dev/null +++ b/TODO.org @@ -0,0 +1,21 @@ +* Set up CI on Travis +* Write tests + +* malloc/free : Parameters for accelerators? +We should define qmckl_malloc and qmckl_free just to give the +possibility of the HPC implementations to define how they allocate the +memory (on CPU or GPU, using alternatives to malloc/free, etc). +A possibility could be to pass the id of a NUMA domain as a parameter of +qmckl_malloc, where the domain id is something obtained from the +context. + + +* TRANSA, TRANSB +* Performance info +* Benchmark interpolation of basis functions +* Complex numbers +* Adjustable number for derivatives (1,2,3) + +* Put pictures +* Make the Makefile part of the documented code ? +* Put the data-flow graph in the code. diff --git a/.gitignore b/docs/.gitignore similarity index 100% rename from .gitignore rename to docs/.gitignore diff --git a/config.el b/docs/config.el similarity index 100% rename from config.el rename to docs/config.el diff --git a/emacs-htmlize/.gitignore b/emacs-htmlize/.gitignore new file mode 100644 index 0000000..c531d98 --- /dev/null +++ b/emacs-htmlize/.gitignore @@ -0,0 +1 @@ +*.elc diff --git a/emacs-htmlize/NEWS b/emacs-htmlize/NEWS new file mode 100644 index 0000000..ad49e85 --- /dev/null +++ b/emacs-htmlize/NEWS @@ -0,0 +1,48 @@ +htmlize NEWS -- history of user-visible changes. + +* Changes in htmlize 1.51 + +** `htmlize-face-overrides' can be used to override Emacs's face +definitions. + + +* Changes in htmlize 1.47 + +** GNU Emacs 21 is no longer supported. + + +* Changes in htmlize 1.45 + +** Correctly handle :inherit specifying a list of faces. (This bug +would cause an error in AUX TeX buffers.) + + +* Changes in htmlize 1.44 + +** Faces specified in the `face' property are now prioritized the same +way that Emacs redisplay does it: faces that appear earlier have +precedence over those that appear later. + + +* Changes in htmlize 1.41 + +** `before-string' and `after-string' overlay properties are now +recognized by htmlize and inserted into the HTML. + +** Images specified by `display' property are recognized and inserted +into the HTML as . + +*** If the image data comes from a file, the image will be rendered as +a relative URI that would resolve to that file. Images whose data +comes from a string will be rendered inline as data: URIs. The flag +`htmlize-force-inline-images' can be used to force inserting *all* +images inline as data: URIs. + +** The image's ALT text will be the text that the `display' property is +replacing, if non-empty. + +** Arbitrary links can now be added to the generated HTML. If htmlize +encounters buffer text with `htmlize-link' property, it will wrap the +text in .... If the property value is a string, it +is interpreted as the URI. If it is a list, it should be a property +list whose currently only defined key is :uri. diff --git a/emacs-htmlize/README.md b/emacs-htmlize/README.md new file mode 100644 index 0000000..12d060f --- /dev/null +++ b/emacs-htmlize/README.md @@ -0,0 +1,36 @@ +# htmlize --- Convert buffer text and decorations to HTML + +[![MELPA](https://melpa.org/packages/htmlize-badge.svg)](https://melpa.org/#/htmlize) + +This package converts the buffer text and the associated +decorations to HTML. Mail to to discuss +features and additions. All suggestions are more than welcome. + +To use it, just switch to the buffer you want HTML-ized and type +M-x htmlize-buffer. You will be switched to a new buffer +that contains the resulting HTML code. You can edit and inspect this +buffer, or you can just save it with C-x C-w. M-x +htmlize-file will find a file, fontify it, and save the HTML +version in `FILE.html`, without any additional intervention. M-x +htmlize-many-files allows you to htmlize any number of files in +the same manner. M-x htmlize-many-files-dired does the +same for files marked in a dired buffer. + +htmlize supports three types of HTML output, selected by setting +`htmlize-output-type`: `css`, `inline-css` (optimized for code +snippets), and `font` (simpler output, doesn't rely on CSS). See +[`htmlize.el.html`][1] for an example of generated HTML. + +You can also use htmlize from your Emacs Lisp code. When called +non-interactively, `htmlize-buffer` and `htmlize-region` will +return the resulting HTML buffer, but will not change current +buffer or move the point. htmlize will do its best to work on +non-windowing Emacs sessions but the result will be limited to +colors supported by the terminal. + +htmlize aims for compatibility with older Emacs versions. Please +let me know if it doesn't work on the version of GNU Emacs that you +are using. + + +[1]: http://htmlpreview.github.io/?https://github.com/hniksic/emacs-htmlize/blob/master/htmlize.el.html diff --git a/emacs-htmlize/htmlize.el b/emacs-htmlize/htmlize.el new file mode 100644 index 0000000..f5c6d8a --- /dev/null +++ b/emacs-htmlize/htmlize.el @@ -0,0 +1,1882 @@ +;;; htmlize.el --- Convert buffer text and decorations to HTML. -*- lexical-binding: t -*- + +;; Copyright (C) 1997-2003,2005,2006,2009,2011,2012,2014,2017,2018 Hrvoje Niksic + +;; Author: Hrvoje Niksic +;; Homepage: https://github.com/hniksic/emacs-htmlize +;; Keywords: hypermedia, extensions +;; Version: 1.56 + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 2, or (at your option) +;; any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program; see the file COPYING. If not, write to the +;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, +;; Boston, MA 02111-1307, USA. + +;;; Commentary: + +;; This package converts the buffer text and the associated +;; decorations to HTML. Mail to to discuss +;; features and additions. All suggestions are more than welcome. + +;; To use it, just switch to the buffer you want HTML-ized and type +;; `M-x htmlize-buffer'. You will be switched to a new buffer that +;; contains the resulting HTML code. You can edit and inspect this +;; buffer, or you can just save it with C-x C-w. `M-x htmlize-file' +;; will find a file, fontify it, and save the HTML version in +;; FILE.html, without any additional intervention. `M-x +;; htmlize-many-files' allows you to htmlize any number of files in +;; the same manner. `M-x htmlize-many-files-dired' does the same for +;; files marked in a dired buffer. + +;; htmlize supports three types of HTML output, selected by setting +;; `htmlize-output-type': `css', `inline-css', and `font'. In `css' +;; mode, htmlize uses cascading style sheets to specify colors; it +;; generates classes that correspond to Emacs faces and uses ... to color parts of text. In this mode, the +;; produced HTML is valid under the 4.01 strict DTD, as confirmed by +;; the W3C validator. `inline-css' is like `css', except the CSS is +;; put directly in the STYLE attribute of the SPAN element, making it +;; possible to paste the generated HTML into existing HTML documents. +;; In `font' mode, htmlize uses ... to +;; colorize HTML, which is not standard-compliant, but works better in +;; older browsers. `css' mode is the default. + +;; You can also use htmlize from your Emacs Lisp code. When called +;; non-interactively, `htmlize-buffer' and `htmlize-region' will +;; return the resulting HTML buffer, but will not change current +;; buffer or move the point. htmlize will do its best to work on +;; non-windowing Emacs sessions but the result will be limited to +;; colors supported by the terminal. + +;; htmlize aims for compatibility with older Emacs versions. Please +;; let me know if it doesn't work on the version of GNU Emacs that you +;; are using. The package relies on the presence of CL extensions; +;; please don't try to remove that dependency. I see no practical +;; problems with using the full power of the CL extensions, except +;; that one might learn to like them too much. + +;; The latest version is available at: +;; +;; +;; +;; + +;; Thanks go to the many people who have sent reports and contributed +;; comments, suggestions, and fixes. They include Ron Gut, Bob +;; Weiner, Toni Drabik, Peter Breton, Ville Skytta, Thomas Vogels, +;; Juri Linkov, Maciek Pasternacki, and many others. + +;; User quotes: "You sir, are a sick, sick, _sick_ person. :)" +;; -- Bill Perry, author of Emacs/W3 + + +;;; Code: + +(require 'cl-lib) +(eval-when-compile + (defvar font-lock-auto-fontify) + (defvar font-lock-support-mode) + (defvar global-font-lock-mode)) + +(defconst htmlize-version "1.56") + +(defgroup htmlize nil + "Convert buffer text and faces to HTML." + :group 'hypermedia) + +(defcustom htmlize-head-tags "" + "Additional tags to insert within HEAD of the generated document." + :type 'string + :group 'htmlize) + +(defcustom htmlize-output-type 'css + "Output type of generated HTML, one of `css', `inline-css', or `font'. +When set to `css' (the default), htmlize will generate a style sheet +with description of faces, and use it in the HTML document, specifying +the faces in the actual text with . + +When set to `inline-css', the style will be generated as above, but +placed directly in the STYLE attribute of the span ELEMENT: . This makes it easier to paste the resulting HTML to +other documents. + +When set to `font', the properties will be set using layout tags +, , , , and . + +`css' output is normally preferred, but `font' is still useful for +supporting old, pre-CSS browsers, and both `inline-css' and `font' for +easier embedding of colorized text in foreign HTML documents (no style +sheet to carry around)." + :type '(choice (const css) (const inline-css) (const font)) + :group 'htmlize) + +(defcustom htmlize-use-images t + "Whether htmlize generates `img' for images attached to buffer contents." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-force-inline-images nil + "Non-nil means generate all images inline using data URLs. +Normally htmlize converts image descriptors with :file properties to +relative URIs, and those with :data properties to data URIs. With this +flag set, the images specified as a file name are loaded into memory and +embedded in the HTML as data URIs." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-max-alt-text 100 + "Maximum size of text to use as ALT text in images. + +Normally when htmlize encounters text covered by the `display' property +that specifies an image, it generates an `alt' attribute containing the +original text. If the text is larger than `htmlize-max-alt-text' characters, +this will not be done." + :type 'integer + :group 'htmlize) + +(defcustom htmlize-transform-image 'htmlize-default-transform-image + "Function called to modify the image descriptor. + +The function is called with the image descriptor found in the buffer and +the text the image is supposed to replace. It should return a (possibly +different) image descriptor property list or a replacement string to use +instead of of the original buffer text. + +Returning nil is the same as returning the original text." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-generate-hyperlinks t + "Non-nil means auto-generate the links from URLs and mail addresses in buffer. + +This is on by default; set it to nil if you don't want htmlize to +autogenerate such links. Note that this option only turns off automatic +search for contents that looks like URLs and converting them to links. +It has no effect on whether htmlize respects the `htmlize-link' property." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-hyperlink-style " + a { + color: inherit; + background-color: inherit; + font: inherit; + text-decoration: inherit; + } + a:hover { + text-decoration: underline; + } +" + "The CSS style used for hyperlinks when in CSS mode." + :type 'string + :group 'htmlize) + +(defcustom htmlize-replace-form-feeds t + "Non-nil means replace form feeds in source code with HTML separators. +Form feeds are the ^L characters at line beginnings that are sometimes +used to separate sections of source code. If this variable is set to +`t', form feed characters are replaced with the
separator. If this +is a string, it specifies the replacement to use. Note that
 is
+temporarily closed before the separator is inserted, so the default
+replacement is effectively \"

\".  If you specify
+another replacement, don't forget to close and reopen the 
 if you
+want the output to remain valid HTML.
+
+If you need more elaborate processing, set this to nil and use
+htmlize-after-hook."
+  :type 'boolean
+  :group 'htmlize)
+
+(defcustom htmlize-html-charset nil
+  "The charset declared by the resulting HTML documents.
+When non-nil, causes htmlize to insert the following in the HEAD section
+of the generated HTML:
+
+  
+
+where CHARSET is the value you've set for htmlize-html-charset.  Valid
+charsets are defined by MIME and include strings like \"iso-8859-1\",
+\"iso-8859-15\", \"utf-8\", etc.
+
+If you are using non-Latin-1 charsets, you might need to set this for
+your documents to render correctly.  Also, the W3C validator requires
+submitted HTML documents to declare a charset.  So if you care about
+validation, you can use this to prevent the validator from bitching.
+
+Needless to say, if you set this, you should actually make sure that
+the buffer is in the encoding you're claiming it is in.  (This is
+normally achieved by using the correct file coding system for the
+buffer.)  If you don't understand what that means, you should probably
+leave this option in its default setting."
+  :type '(choice (const :tag "Unset" nil)
+		 string)
+  :group 'htmlize)
+
+(defcustom htmlize-convert-nonascii-to-entities t
+  "Whether non-ASCII characters should be converted to HTML entities.
+
+When this is non-nil, characters with codes in the 128-255 range will be
+considered Latin 1 and rewritten as \"&#CODE;\".  Characters with codes
+above 255 will be converted to \"&#UCS;\", where UCS denotes the Unicode
+code point of the character.  If the code point cannot be determined,
+the character will be copied unchanged, as would be the case if the
+option were nil.
+
+When the option is nil, the non-ASCII characters are copied to HTML
+without modification.  In that case, the web server and/or the browser
+must be set to understand the encoding that was used when saving the
+buffer.  (You might also want to specify it by setting
+`htmlize-html-charset'.)
+
+Note that in an HTML entity \"&#CODE;\", CODE is always a UCS code point,
+which has nothing to do with the charset the page is in.  For example,
+\"©\" *always* refers to the copyright symbol, regardless of charset
+specified by the META tag or the charset sent by the HTTP server.  In
+other words, \"©\" is exactly equivalent to \"©\".
+
+For most people htmlize will work fine with this option left at the
+default setting; don't change it unless you know what you're doing."
+  :type 'sexp
+  :group 'htmlize)
+
+(defcustom htmlize-ignore-face-size 'absolute
+  "Whether face size should be ignored when generating HTML.
+If this is nil, face sizes are used.  If set to t, sizes are ignored
+If set to `absolute', only absolute size specifications are ignored.
+Please note that font sizes only work with CSS-based output types."
+  :type '(choice (const :tag "Don't ignore" nil)
+		 (const :tag "Ignore all" t)
+		 (const :tag "Ignore absolute" absolute))
+  :group 'htmlize)
+
+(defcustom htmlize-css-name-prefix ""
+  "The prefix used for CSS names.
+The CSS names that htmlize generates from face names are often too
+generic for CSS files; for example, `font-lock-type-face' is transformed
+to `type'.  Use this variable to add a prefix to the generated names.
+The string \"htmlize-\" is an example of a reasonable prefix."
+  :type 'string
+  :group 'htmlize)
+
+(defcustom htmlize-use-rgb-txt t
+  "Whether `rgb.txt' should be used to convert color names to RGB.
+
+This conversion means determining, for instance, that the color
+\"IndianRed\" corresponds to the (205, 92, 92) RGB triple.  `rgb.txt'
+is the X color database that maps hundreds of color names to such RGB
+triples.  When this variable is non-nil, `htmlize' uses `rgb.txt' to
+look up color names.
+
+If this variable is nil, htmlize queries Emacs for RGB components of
+colors using `color-instance-rgb-components' and `color-values'.
+This can yield incorrect results on non-true-color displays.
+
+If the `rgb.txt' file is not found (which will be the case if you're
+running Emacs on non-X11 systems), this option is ignored."
+  :type 'boolean
+  :group 'htmlize)
+
+(defvar htmlize-face-overrides nil
+  "Overrides for face definitions.
+
+Normally face definitions are taken from Emacs settings for fonts
+in the current frame.  For faces present in this plist, the
+definitions will be used instead.  Keys in the plist are symbols
+naming the face and values are the overriding definitions.  For
+example:
+
+  (setq htmlize-face-overrides
+        '(font-lock-warning-face \"black\"
+          font-lock-function-name-face \"red\"
+          font-lock-comment-face \"blue\"
+          default (:foreground \"dark-green\" :background \"yellow\")))
+
+This variable can be also be `let' bound when running `htmlize-buffer'.")
+
+(defcustom htmlize-untabify t
+  "Non-nil means untabify buffer contents during htmlization."
+  :type 'boolean
+  :group 'htmlize)
+
+(defcustom htmlize-html-major-mode nil
+  "The mode the newly created HTML buffer will be put in.
+Set this to nil if you prefer the default (fundamental) mode."
+  :type '(radio (const :tag "No mode (fundamental)" nil)
+		 (function-item html-mode)
+		 (function :tag "User-defined major mode"))
+  :group 'htmlize)
+
+(defcustom htmlize-pre-style nil
+  "When non-nil, `
' tags will be decorated with style
+information in `font' and `inline-css' modes. This allows a
+consistent background for captures of regions."
+  :type 'boolean
+  :group 'htmlize)
+
+(defvar htmlize-before-hook nil
+  "Hook run before htmlizing a buffer.
+The hook functions are run in the source buffer (not the resulting HTML
+buffer).")
+
+(defvar htmlize-after-hook nil
+  "Hook run after htmlizing a buffer.
+Unlike `htmlize-before-hook', these functions are run in the generated
+HTML buffer.  You may use them to modify the outlook of the final HTML
+output.")
+
+(defvar htmlize-file-hook nil
+  "Hook run by `htmlize-file' after htmlizing a file, but before saving it.")
+
+(defvar htmlize-buffer-places)
+
+;;; Some cross-Emacs compatibility.
+
+;; We need a function that efficiently finds the next change of a
+;; property regardless of whether the change occurred because of a
+;; text property or an extent/overlay.
+(defun htmlize-next-change (pos prop &optional limit)
+  (if prop
+      (next-single-char-property-change pos prop nil limit)
+    (next-char-property-change pos limit)))
+
+(defun htmlize-overlay-faces-at (pos)
+  (delq nil (mapcar (lambda (o) (overlay-get o 'face)) (overlays-at pos))))
+
+(defun htmlize-next-face-change (pos &optional limit)
+  ;; (htmlize-next-change pos 'face limit) would skip over entire
+  ;; overlays that specify the `face' property, even when they
+  ;; contain smaller text properties that also specify `face'.
+  ;; Emacs display engine merges those faces, and so must we.
+  (or limit
+      (setq limit (point-max)))
+  (let ((next-prop (next-single-property-change pos 'face nil limit))
+        (overlay-faces (htmlize-overlay-faces-at pos)))
+    (while (progn
+             (setq pos (next-overlay-change pos))
+             (and (< pos next-prop)
+                  (equal overlay-faces (htmlize-overlay-faces-at pos)))))
+    (setq pos (min pos next-prop))
+    ;; Additionally, we include the entire region that specifies the
+    ;; `display' property.
+    (when (get-char-property pos 'display)
+      (setq pos (next-single-char-property-change pos 'display nil limit)))
+    pos))
+
+(defmacro htmlize-lexlet (&rest letforms)
+  (declare (indent 1) (debug let))
+  (if (and (boundp 'lexical-binding)
+           lexical-binding)
+      `(let ,@letforms)
+    ;; cl extensions have a macro implementing lexical let
+    `(lexical-let ,@letforms)))
+
+
+;;; Transformation of buffer text: HTML escapes, untabification, etc.
+
+(defvar htmlize-basic-character-table
+  ;; Map characters in the 0-127 range to either one-character strings
+  ;; or to numeric entities.
+  (let ((table (make-vector 128 ?\0)))
+    ;; Map characters in the 32-126 range to themselves, others to
+    ;; &#CODE entities;
+    (dotimes (i 128)
+      (setf (aref table i) (if (and (>= i 32) (<= i 126))
+			       (char-to-string i)
+			     (format "&#%d;" i))))
+    ;; Set exceptions manually.
+    (setf
+     ;; Don't escape newline, carriage return, and TAB.
+     (aref table ?\n) "\n"
+     (aref table ?\r) "\r"
+     (aref table ?\t) "\t"
+     ;; Escape &, <, and >.
+     (aref table ?&) "&"
+     (aref table ?<) "<"
+     (aref table ?>) ">"
+     ;; Not escaping '"' buys us a measurable speedup.  It's only
+     ;; necessary to quote it for strings used in attribute values,
+     ;; which htmlize doesn't typically do.
+     ;(aref table ?\") """
+     )
+    table))
+
+;; A cache of HTML representation of non-ASCII characters.  Depending
+;; on the setting of `htmlize-convert-nonascii-to-entities', this maps
+;; non-ASCII characters to either "&#;" or "" (mapconcat's
+;; mapper must always return strings).  It's only filled as characters
+;; are encountered, so that in a buffer with e.g. French text, it will
+;; only ever contain French accented characters as keys.  It's cleared
+;; on each entry to htmlize-buffer-1 to allow modifications of
+;; `htmlize-convert-nonascii-to-entities' to take effect.
+(defvar htmlize-extended-character-cache (make-hash-table :test 'eq))
+
+(defun htmlize-protect-string (string)
+  "HTML-protect string, escaping HTML metacharacters and I18N chars."
+  ;; Only protecting strings that actually contain unsafe or non-ASCII
+  ;; chars removes a lot of unnecessary funcalls and consing.
+  (if (not (string-match "[^\r\n\t -%'-;=?-~]" string))
+      string
+    (mapconcat (lambda (char)
+		 (cond
+		  ((< char 128)
+		   ;; ASCII: use htmlize-basic-character-table.
+		   (aref htmlize-basic-character-table char))
+		  ((gethash char htmlize-extended-character-cache)
+		   ;; We've already seen this char; return the cached
+		   ;; string.
+		   )
+		  ((not htmlize-convert-nonascii-to-entities)
+		   ;; If conversion to entities is not desired, always
+		   ;; copy the char literally.
+		   (setf (gethash char htmlize-extended-character-cache)
+			 (char-to-string char)))
+		  ((< char 256)
+		   ;; Latin 1: no need to call encode-char.
+		   (setf (gethash char htmlize-extended-character-cache)
+			 (format "&#%d;" char)))
+		  ((encode-char char 'ucs)
+                   ;; Must check if encode-char works for CHAR;
+                   ;; it fails for Arabic and possibly elsewhere.
+		   (setf (gethash char htmlize-extended-character-cache)
+			 (format "&#%d;" (encode-char char 'ucs))))
+		  (t
+		   ;; encode-char doesn't work for this char.  Copy it
+		   ;; unchanged and hope for the best.
+		   (setf (gethash char htmlize-extended-character-cache)
+			 (char-to-string char)))))
+	       string "")))
+
+(defun htmlize-attr-escape (string)
+  ;; Like htmlize-protect-string, but also escapes double-quoted
+  ;; strings to make it usable in attribute values.
+  (setq string (htmlize-protect-string string))
+  (if (not (string-match "\"" string))
+      string
+    (mapconcat (lambda (char)
+                 (if (eql char ?\")
+                     """
+                   (char-to-string char)))
+               string "")))
+
+(defsubst htmlize-concat (list)
+  (if (and (consp list) (null (cdr list)))
+      ;; Don't create a new string in the common case where the list only
+      ;; consists of one element.
+      (car list)
+    (apply #'concat list)))
+
+(defun htmlize-format-link (linkprops text)
+  (let ((uri (if (stringp linkprops)
+                 linkprops
+               (plist-get linkprops :uri)))
+        (escaped-text (htmlize-protect-string text)))
+    (if uri
+        (format "%s" (htmlize-attr-escape uri) escaped-text)
+      escaped-text)))
+
+(defun htmlize-escape-or-link (string)
+  ;; Escape STRING and/or add hyperlinks.  STRING comes from a
+  ;; `display' property.
+  (let ((pos 0) (end (length string)) outlist)
+    (while (< pos end)
+      (let* ((link (get-char-property pos 'htmlize-link string))
+             (next-link-change (next-single-property-change
+                                pos 'htmlize-link string end))
+             (chunk (substring string pos next-link-change)))
+        (push
+         (cond (link
+                (htmlize-format-link link chunk))
+               ((get-char-property 0 'htmlize-literal chunk)
+                chunk)
+               (t
+                (htmlize-protect-string chunk)))
+         outlist)
+        (setq pos next-link-change)))
+    (htmlize-concat (nreverse outlist))))
+
+(defun htmlize-display-prop-to-html (display text)
+  (let (desc)
+    (cond ((stringp display)
+           ;; Emacs ignores recursive display properties.
+           (htmlize-escape-or-link display))
+          ((not (eq (car-safe display) 'image))
+           (htmlize-protect-string text))
+          ((null (setq desc (funcall htmlize-transform-image
+                                     (cdr display) text)))
+           (htmlize-escape-or-link text))
+          ((stringp desc)
+           (htmlize-escape-or-link desc))
+          (t
+           (htmlize-generate-image desc text)))))
+
+(defun htmlize-string-to-html (string)
+  ;; Convert the string to HTML, including images attached as
+  ;; `display' property and links as `htmlize-link' property.  In a
+  ;; string without images or links, this is equivalent to
+  ;; `htmlize-protect-string'.
+  (let ((pos 0) (end (length string)) outlist)
+    (while (< pos end)
+      (let* ((display (get-char-property pos 'display string))
+             (next-display-change (next-single-property-change
+                                   pos 'display string end))
+             (chunk (substring string pos next-display-change)))
+        (push
+         (if display
+             (htmlize-display-prop-to-html display chunk)
+           (htmlize-escape-or-link chunk))
+         outlist)
+        (setq pos next-display-change)))
+    (htmlize-concat (nreverse outlist))))
+
+(defun htmlize-default-transform-image (imgprops _text)
+  "Default transformation of image descriptor to something usable in HTML.
+
+If `htmlize-use-images' is nil, the function always returns nil, meaning
+use original text.  Otherwise, it tries to find the image for images that
+specify a file name.  If `htmlize-force-inline-images' is non-nil, it also
+converts the :file attribute to :data and returns the modified property
+list."
+  (when htmlize-use-images
+    (when (plist-get imgprops :file)
+      (let ((location (plist-get (cdr (find-image (list imgprops))) :file)))
+        (when location
+          (setq imgprops (plist-put (cl-copy-list imgprops) :file location)))))
+    (if htmlize-force-inline-images
+        (let ((location (plist-get imgprops :file))
+              data)
+          (when location
+            (with-temp-buffer
+              (condition-case nil
+                  (progn
+                    (insert-file-contents-literally location)
+                    (setq data (buffer-string)))
+                (error nil))))
+          ;; if successful, return the new plist, otherwise return
+          ;; nil, which will use the original text
+          (and data
+               (plist-put (plist-put imgprops :file nil)
+                          :data data)))
+      imgprops)))
+
+(defun htmlize-alt-text (_imgprops origtext)
+  (and (/= (length origtext) 0)
+       (<= (length origtext) htmlize-max-alt-text)
+       (not (string-match "[\0-\x1f]" origtext))
+       origtext))
+
+(defun htmlize-generate-image (imgprops origtext)
+  (let* ((alt-text (htmlize-alt-text imgprops origtext))
+         (alt-attr (if alt-text
+                       (format " alt=\"%s\"" (htmlize-attr-escape alt-text))
+                     "")))
+    (cond ((plist-get imgprops :file)
+           ;; Try to find the image in image-load-path
+           (let* ((found-props (cdr (find-image (list imgprops))))
+                  (file (or (plist-get found-props :file)
+                            (plist-get imgprops :file))))
+             (format ""
+                     (htmlize-attr-escape (file-relative-name file))
+                     alt-attr)))
+          ((plist-get imgprops :data)
+           (format ""
+                   (or (plist-get imgprops :type) "")
+                   (base64-encode-string (plist-get imgprops :data))
+                   alt-attr)))))
+
+(defconst htmlize-ellipsis "...")
+(put-text-property 0 (length htmlize-ellipsis) 'htmlize-ellipsis t htmlize-ellipsis)
+
+(defun htmlize-match-inv-spec (inv)
+  (cl-member inv buffer-invisibility-spec
+             :key (lambda (i)
+                    (if (symbolp i) i (car i)))))
+
+(defun htmlize-decode-invisibility-spec (invisible)
+  ;; Return t, nil, or `ellipsis', depending on how invisible text should be inserted.
+
+  (if (not (listp buffer-invisibility-spec))
+      ;; If buffer-invisibility-spec is not a list, then all
+      ;; characters with non-nil `invisible' property are visible.
+      (not invisible)
+
+    ;; Otherwise, the value of a non-nil `invisible' property can be:
+    ;; 1. a symbol -- make the text invisible if it matches
+    ;;    buffer-invisibility-spec.
+    ;; 2. a list of symbols -- make the text invisible if
+    ;;    any symbol in the list matches
+    ;;    buffer-invisibility-spec.
+    ;; If the match of buffer-invisibility-spec has a non-nil
+    ;; CDR, replace the invisible text with an ellipsis.
+    (let ((match (if (symbolp invisible)
+                     (htmlize-match-inv-spec invisible)
+                   (cl-some #'htmlize-match-inv-spec invisible))))
+      (cond ((null match) t)
+            ((cdr-safe (car match)) 'ellipsis)
+            (t nil)))))
+
+(defun htmlize-add-before-after-strings (beg end text)
+  ;; Find overlays specifying before-string and after-string in [beg,
+  ;; pos).  If any are found, splice them into TEXT and return the new
+  ;; text.
+  (let (additions)
+    (dolist (overlay (overlays-in beg end))
+      (let ((before (overlay-get overlay 'before-string))
+            (after (overlay-get overlay 'after-string)))
+        (when after
+          (push (cons (- (overlay-end overlay) beg)
+                      after)
+                additions))
+        (when before
+          (push (cons (- (overlay-start overlay) beg)
+                      before)
+                additions))))
+    (if additions
+        (let ((textlist nil)
+              (strpos 0))
+          (dolist (add (cl-stable-sort additions #'< :key #'car))
+            (let ((addpos (car add))
+                  (addtext (cdr add)))
+              (push (substring text strpos addpos) textlist)
+              (push addtext textlist)
+              (setq strpos addpos)))
+          (push (substring text strpos) textlist)
+          (apply #'concat (nreverse textlist)))
+      text)))
+
+(defun htmlize-copy-prop (prop beg end string)
+  ;; Copy the specified property from the specified region of the
+  ;; buffer to the target string.  We cannot rely on Emacs to copy the
+  ;; property because we want to handle properties coming from both
+  ;; text properties and overlays.
+  (let ((pos beg))
+    (while (< pos end)
+      (let ((value (get-char-property pos prop))
+            (next-change (htmlize-next-change pos prop end)))
+        (when value
+          (put-text-property (- pos beg) (- next-change beg)
+                             prop value string))
+        (setq pos next-change)))))
+
+(defun htmlize-get-text-with-display (beg end)
+  ;; Like buffer-substring-no-properties, except it copies the
+  ;; `display' property from the buffer, if found.
+  (let ((text (buffer-substring-no-properties beg end)))
+    (htmlize-copy-prop 'display beg end text)
+    (htmlize-copy-prop 'htmlize-link beg end text)
+    (setq text (htmlize-add-before-after-strings beg end text))
+    text))
+
+(defun htmlize-buffer-substring-no-invisible (beg end)
+  ;; Like buffer-substring-no-properties, but don't copy invisible
+  ;; parts of the region.  Where buffer-substring-no-properties
+  ;; mandates an ellipsis to be shown, htmlize-ellipsis is inserted.
+  (let ((pos beg)
+	visible-list invisible show last-show next-change)
+    ;; Iterate over the changes in the `invisible' property and filter
+    ;; out the portions where it's non-nil, i.e. where the text is
+    ;; invisible.
+    (while (< pos end)
+      (setq invisible (get-char-property pos 'invisible)
+	    next-change (htmlize-next-change pos 'invisible end)
+            show (htmlize-decode-invisibility-spec invisible))
+      (cond ((eq show t)
+	     (push (htmlize-get-text-with-display pos next-change)
+                   visible-list))
+            ((and (eq show 'ellipsis)
+                  (not (eq last-show 'ellipsis))
+                  ;; Conflate successive ellipses.
+                  (push htmlize-ellipsis visible-list))))
+      (setq pos next-change last-show show))
+    (htmlize-concat (nreverse visible-list))))
+
+(defun htmlize-trim-ellipsis (text)
+  ;; Remove htmlize-ellipses ("...") from the beginning of TEXT if it
+  ;; starts with it.  It checks for the special property of the
+  ;; ellipsis so it doesn't work on ordinary text that begins with
+  ;; "...".
+  (if (get-text-property 0 'htmlize-ellipsis text)
+      (substring text (length htmlize-ellipsis))
+    text))
+
+(defconst htmlize-tab-spaces
+  ;; A table of strings with spaces.  (aref htmlize-tab-spaces 5) is
+  ;; like (make-string 5 ?\ ), except it doesn't cons.
+  (let ((v (make-vector 32 nil)))
+    (dotimes (i (length v))
+      (setf (aref v i) (make-string i ?\ )))
+    v))
+
+(defun htmlize-untabify-string (text start-column)
+  "Untabify TEXT, assuming it starts at START-COLUMN."
+  (let ((column start-column)
+	(last-match 0)
+	(chunk-start 0)
+	chunks match-pos tab-size)
+    (while (string-match "[\t\n]" text last-match)
+      (setq match-pos (match-beginning 0))
+      (cond ((eq (aref text match-pos) ?\t)
+	     ;; Encountered a tab: create a chunk of text followed by
+	     ;; the expanded tab.
+	     (push (substring text chunk-start match-pos) chunks)
+	     ;; Increase COLUMN by the length of the text we've
+	     ;; skipped since last tab or newline.  (Encountering
+	     ;; newline resets it.)
+	     (cl-incf column (- match-pos last-match))
+	     ;; Calculate tab size based on tab-width and COLUMN.
+	     (setq tab-size (- tab-width (% column tab-width)))
+	     ;; Expand the tab, carefully recreating the `display'
+	     ;; property if one was on the TAB.
+             (let ((display (get-text-property match-pos 'display text))
+                   (expanded-tab (aref htmlize-tab-spaces tab-size)))
+               (when display
+                 (put-text-property 0 tab-size 'display display expanded-tab))
+               (push expanded-tab chunks))
+	     (cl-incf column tab-size)
+	     (setq chunk-start (1+ match-pos)))
+	    (t
+	     ;; Reset COLUMN at beginning of line.
+	     (setq column 0)))
+      (setq last-match (1+ match-pos)))
+    ;; If no chunks have been allocated, it means there have been no
+    ;; tabs to expand.  Return TEXT unmodified.
+    (if (null chunks)
+	text
+      (when (< chunk-start (length text))
+	;; Push the remaining chunk.
+	(push (substring text chunk-start) chunks))
+      ;; Generate the output from the available chunks.
+      (htmlize-concat (nreverse chunks)))))
+
+(defun htmlize-extract-text (beg end trailing-ellipsis)
+  ;; Extract buffer text, sans the invisible parts.  Then
+  ;; untabify it and escape the HTML metacharacters.
+  (let ((text (htmlize-buffer-substring-no-invisible beg end)))
+    (when trailing-ellipsis
+      (setq text (htmlize-trim-ellipsis text)))
+    ;; If TEXT ends up empty, don't change trailing-ellipsis.
+    (when (> (length text) 0)
+      (setq trailing-ellipsis
+            (get-text-property (1- (length text))
+                               'htmlize-ellipsis text)))
+    (when htmlize-untabify
+      (setq text (htmlize-untabify-string text (current-column))))
+    (setq text (htmlize-string-to-html text))
+    (cl-values text trailing-ellipsis)))
+
+(defun htmlize-despam-address (string)
+  "Replace every occurrence of '@' in STRING with %40.
+This is used to protect mailto links without modifying their meaning."
+  ;; Suggested by Ville Skytta.
+  (while (string-match "@" string)
+    (setq string (replace-match "%40" nil t string)))
+  string)
+
+(defun htmlize-make-tmp-overlay (beg end props)
+  (let ((overlay (make-overlay beg end)))
+    (overlay-put overlay 'htmlize-tmp-overlay t)
+    (while props
+      (overlay-put overlay (pop props) (pop props)))
+    overlay))
+
+(defun htmlize-delete-tmp-overlays ()
+  (dolist (overlay (overlays-in (point-min) (point-max)))
+    (when (overlay-get overlay 'htmlize-tmp-overlay)
+      (delete-overlay overlay))))
+
+(defun htmlize-make-link-overlay (beg end uri)
+  (htmlize-make-tmp-overlay beg end `(htmlize-link (:uri ,uri))))
+
+(defun htmlize-create-auto-links ()
+  "Add `htmlize-link' property to all mailto links in the buffer."
+  (save-excursion
+    (goto-char (point-min))
+    (while (re-search-forward
+            "<\\(\\(mailto:\\)?\\([-=+_.a-zA-Z0-9]+@[-_.a-zA-Z0-9]+\\)\\)>"
+            nil t)
+      (let* ((address (match-string 3))
+             (beg (match-beginning 0)) (end (match-end 0))
+             (uri (concat "mailto:" (htmlize-despam-address address))))
+        (htmlize-make-link-overlay beg end uri)))
+    (goto-char (point-min))
+    (while (re-search-forward "<\\(\\(URL:\\)?\\([a-zA-Z]+://[^;]+\\)\\)>"
+                              nil t)
+      (htmlize-make-link-overlay
+       (match-beginning 0) (match-end 0) (match-string 3)))))
+
+;; Tests for htmlize-create-auto-links:
+
+;; 
+;; 
+;; 
+;; 
+;; 
+;; 
+
+(defun htmlize-shadow-form-feeds ()
+  (let ((s "\n
")) + (put-text-property 0 (length s) 'htmlize-literal t s) + (let ((disp `(display ,s))) + (while (re-search-forward "\n\^L" nil t) + (let* ((beg (match-beginning 0)) + (end (match-end 0)) + (form-feed-pos (1+ beg)) + ;; don't process ^L if invisible or covered by `display' + (show (and (htmlize-decode-invisibility-spec + (get-char-property form-feed-pos 'invisible)) + (not (get-char-property form-feed-pos 'display))))) + (when show + (htmlize-make-tmp-overlay beg end disp))))))) + +(defun htmlize-defang-local-variables () + ;; Juri Linkov reports that an HTML-ized "Local variables" can lead + ;; visiting the HTML to fail with "Local variables list is not + ;; properly terminated". He suggested changing the phrase to + ;; syntactically equivalent HTML that Emacs doesn't recognize. + (goto-char (point-min)) + (while (search-forward "Local Variables:" nil t) + (replace-match "Local Variables:" nil t))) + + +;;; Color handling. + +(defvar htmlize-x-library-search-path + `(,data-directory + "/etc/X11/rgb.txt" + "/usr/share/X11/rgb.txt" + ;; the remainder of this list really belongs in a museum + "/usr/X11R6/lib/X11/" + "/usr/X11R5/lib/X11/" + "/usr/lib/X11R6/X11/" + "/usr/lib/X11R5/X11/" + "/usr/local/X11R6/lib/X11/" + "/usr/local/X11R5/lib/X11/" + "/usr/local/lib/X11R6/X11/" + "/usr/local/lib/X11R5/X11/" + "/usr/X11/lib/X11/" + "/usr/lib/X11/" + "/usr/local/lib/X11/" + "/usr/X386/lib/X11/" + "/usr/x386/lib/X11/" + "/usr/XFree86/lib/X11/" + "/usr/unsupported/lib/X11/" + "/usr/athena/lib/X11/" + "/usr/local/x11r5/lib/X11/" + "/usr/lpp/Xamples/lib/X11/" + "/usr/openwin/lib/X11/" + "/usr/openwin/share/lib/X11/")) + +(defun htmlize-get-color-rgb-hash (&optional rgb-file) + "Return a hash table mapping X color names to RGB values. +The keys in the hash table are X11 color names, and the values are the +#rrggbb RGB specifications, extracted from `rgb.txt'. + +If RGB-FILE is nil, the function will try hard to find a suitable file +in the system directories. + +If no rgb.txt file is found, return nil." + (let ((rgb-file (or rgb-file (locate-file + "rgb.txt" + htmlize-x-library-search-path))) + (hash nil)) + (when rgb-file + (with-temp-buffer + (insert-file-contents rgb-file) + (setq hash (make-hash-table :test 'equal)) + (while (not (eobp)) + (cond ((looking-at "^\\s-*\\([!#]\\|$\\)") + ;; Skip comments and empty lines. + ) + ((looking-at + "[ \t]*\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\(.*\\)") + (setf (gethash (downcase (match-string 4)) hash) + (format "#%02x%02x%02x" + (string-to-number (match-string 1)) + (string-to-number (match-string 2)) + (string-to-number (match-string 3))))) + (t + (error + "Unrecognized line in %s: %s" + rgb-file + (buffer-substring (point) (progn (end-of-line) (point)))))) + (forward-line 1)))) + hash)) + +;; Compile the RGB map when loaded. On systems where rgb.txt is +;; missing, the value of the variable will be nil, and rgb.txt will +;; not be used. +(defvar htmlize-color-rgb-hash (htmlize-get-color-rgb-hash)) + +;;; Face handling. + +(defun htmlize-face-color-internal (face fg) + ;; Used only under GNU Emacs. Return the color of FACE, but don't + ;; return "unspecified-fg" or "unspecified-bg". If the face is + ;; `default' and the color is unspecified, look up the color in + ;; frame parameters. + (let* ((function (if fg #'face-foreground #'face-background)) + (color (funcall function face nil t))) + (when (and (eq face 'default) (null color)) + (setq color (cdr (assq (if fg 'foreground-color 'background-color) + (frame-parameters))))) + (when (or (eq color 'unspecified) + (equal color "unspecified-fg") + (equal color "unspecified-bg")) + (setq color nil)) + (when (and (eq face 'default) + (null color)) + ;; Assuming black on white doesn't seem right, but I can't think + ;; of anything better to do. + (setq color (if fg "black" "white"))) + color)) + +(defun htmlize-face-foreground (face) + ;; Return the name of the foreground color of FACE. If FACE does + ;; not specify a foreground color, return nil. + (htmlize-face-color-internal face t)) + +(defun htmlize-face-background (face) + ;; Return the name of the background color of FACE. If FACE does + ;; not specify a background color, return nil. + ;; GNU Emacs. + (htmlize-face-color-internal face nil)) + +;; Convert COLOR to the #RRGGBB string. If COLOR is already in that +;; format, it's left unchanged. + +(defun htmlize-color-to-rgb (color) + (let ((rgb-string nil)) + (cond ((null color) + ;; Ignore nil COLOR because it means that the face is not + ;; specifying any color. Hence (htmlize-color-to-rgb nil) + ;; returns nil. + ) + ((string-match "\\`#" color) + ;; The color is already in #rrggbb format. + (setq rgb-string color)) + ((and htmlize-use-rgb-txt + htmlize-color-rgb-hash) + ;; Use of rgb.txt is requested, and it's available on the + ;; system. Use it. + (setq rgb-string (gethash (downcase color) htmlize-color-rgb-hash))) + (t + ;; We're getting the RGB components from Emacs. + (let ((rgb (mapcar (lambda (arg) + (/ arg 256)) + (color-values color)))) + (when rgb + (setq rgb-string (apply #'format "#%02x%02x%02x" rgb)))))) + ;; If RGB-STRING is still nil, it means the color cannot be found, + ;; for whatever reason. In that case just punt and return COLOR. + ;; Most browsers support a decent set of color names anyway. + (or rgb-string color))) + +;; We store the face properties we care about into an +;; `htmlize-fstruct' type. That way we only have to analyze face +;; properties, which can be time consuming, once per each face. The +;; mapping between Emacs faces and htmlize-fstructs is established by +;; htmlize-make-face-map. The name "fstruct" refers to variables of +;; type `htmlize-fstruct', while the term "face" is reserved for Emacs +;; faces. + +(cl-defstruct htmlize-fstruct + foreground ; foreground color, #rrggbb + background ; background color, #rrggbb + size ; size + boldp ; whether face is bold + italicp ; whether face is italic + underlinep ; whether face is underlined + overlinep ; whether face is overlined + strikep ; whether face is struck through + css-name ; CSS name of face + ) + +(defun htmlize-face-set-from-keyword-attr (fstruct attr value) + ;; For ATTR and VALUE, set the equivalent value in FSTRUCT. + (cl-case attr + (:foreground + (setf (htmlize-fstruct-foreground fstruct) (htmlize-color-to-rgb value))) + (:background + (setf (htmlize-fstruct-background fstruct) (htmlize-color-to-rgb value))) + (:height + (setf (htmlize-fstruct-size fstruct) value)) + (:weight + (when (string-match (symbol-name value) "bold") + (setf (htmlize-fstruct-boldp fstruct) t))) + (:slant + (setf (htmlize-fstruct-italicp fstruct) (or (eq value 'italic) + (eq value 'oblique)))) + (:bold + (setf (htmlize-fstruct-boldp fstruct) value)) + (:italic + (setf (htmlize-fstruct-italicp fstruct) value)) + (:underline + (setf (htmlize-fstruct-underlinep fstruct) value)) + (:overline + (setf (htmlize-fstruct-overlinep fstruct) value)) + (:strike-through + (setf (htmlize-fstruct-strikep fstruct) value)))) + +(defun htmlize-face-size (face) + ;; The size (height) of FACE, taking inheritance into account. + ;; Only works in Emacs 21 and later. + (let* ((face-list (list face)) + (head face-list) + (tail face-list)) + (while head + (let ((inherit (face-attribute (car head) :inherit))) + (cond ((listp inherit) + (setcdr tail (cl-copy-list inherit)) + (setq tail (last tail))) + ((eq inherit 'unspecified)) + (t + (setcdr tail (list inherit)) + (setq tail (cdr tail))))) + (pop head)) + (let ((size-list + (cl-loop + for f in face-list + for h = (face-attribute f :height) + collect (if (eq h 'unspecified) nil h)))) + (cl-reduce 'htmlize-merge-size (cons nil size-list))))) + +(defun htmlize-face-css-name (face) + ;; Generate the css-name property for the given face. Emacs places + ;; no restrictions on the names of symbols that represent faces -- + ;; any characters may be in the name, even control chars. We try + ;; hard to beat the face name into shape, both esthetically and + ;; according to CSS1 specs. + (let ((name (downcase (symbol-name face)))) + (when (string-match "\\`font-lock-" name) + ;; font-lock-FOO-face -> FOO. + (setq name (replace-match "" t t name))) + (when (string-match "-face\\'" name) + ;; Drop the redundant "-face" suffix. + (setq name (replace-match "" t t name))) + (while (string-match "[^-a-zA-Z0-9]" name) + ;; Drop the non-alphanumerics. + (setq name (replace-match "X" t t name))) + (when (string-match "\\`[-0-9]" name) + ;; CSS identifiers may not start with a digit. + (setq name (concat "X" name))) + ;; After these transformations, the face could come out empty. + (when (equal name "") + (setq name "face")) + ;; Apply the prefix. + (concat htmlize-css-name-prefix name))) + +(defun htmlize-face-to-fstruct-1 (face) + "Convert Emacs face FACE to fstruct, internal." + (let ((fstruct (make-htmlize-fstruct + :foreground (htmlize-color-to-rgb + (htmlize-face-foreground face)) + :background (htmlize-color-to-rgb + (htmlize-face-background face))))) + ;; GNU Emacs + (dolist (attr '(:weight :slant :underline :overline :strike-through)) + (let ((value (face-attribute face attr nil t))) + (when (and value (not (eq value 'unspecified))) + (htmlize-face-set-from-keyword-attr fstruct attr value)))) + (let ((size (htmlize-face-size face))) + (unless (eql size 1.0) ; ignore non-spec + (setf (htmlize-fstruct-size fstruct) size))) + (setf (htmlize-fstruct-css-name fstruct) (htmlize-face-css-name face)) + fstruct)) + +(defun htmlize-face-to-fstruct (face) + (let* ((face-list (or (and (symbolp face) + (cdr (assq face face-remapping-alist))) + (list face))) + (fstruct (htmlize-merge-faces + (mapcar (lambda (face) + (if (symbolp face) + (or (htmlize-get-override-fstruct face) + (htmlize-face-to-fstruct-1 face)) + (htmlize-attrlist-to-fstruct face))) + (nreverse face-list))))) + (when (symbolp face) + (setf (htmlize-fstruct-css-name fstruct) (htmlize-face-css-name face))) + fstruct)) + +(defmacro htmlize-copy-attr-if-set (attr-list dest source) + ;; Generate code with the following pattern: + ;; (progn + ;; (when (htmlize-fstruct-ATTR source) + ;; (setf (htmlize-fstruct-ATTR dest) (htmlize-fstruct-ATTR source))) + ;; ...) + ;; for the given list of boolean attributes. + (cons 'progn + (cl-loop for attr in attr-list + for attr-sym = (intern (format "htmlize-fstruct-%s" attr)) + collect `(when (,attr-sym ,source) + (setf (,attr-sym ,dest) (,attr-sym ,source)))))) + +(defun htmlize-merge-size (merged next) + ;; Calculate the size of the merge of MERGED and NEXT. + (cond ((null merged) next) + ((integerp next) next) + ((null next) merged) + ((floatp merged) (* merged next)) + ((integerp merged) (round (* merged next))))) + +(defun htmlize-merge-two-faces (merged next) + (htmlize-copy-attr-if-set + (foreground background boldp italicp underlinep overlinep strikep) + merged next) + (setf (htmlize-fstruct-size merged) + (htmlize-merge-size (htmlize-fstruct-size merged) + (htmlize-fstruct-size next))) + merged) + +(defun htmlize-merge-faces (fstruct-list) + (cond ((null fstruct-list) + ;; Nothing to do, return a dummy face. + (make-htmlize-fstruct)) + ((null (cdr fstruct-list)) + ;; Optimize for the common case of a single face, simply + ;; return it. + (car fstruct-list)) + (t + (cl-reduce #'htmlize-merge-two-faces + (cons (make-htmlize-fstruct) fstruct-list))))) + +;; GNU Emacs 20+ supports attribute lists in `face' properties. For +;; example, you can use `(:foreground "red" :weight bold)' as an +;; overlay's "face", or you can even use a list of such lists, etc. +;; We call those "attrlists". +;; +;; htmlize supports attrlist by converting them to fstructs, the same +;; as with regular faces. + +(defun htmlize-attrlist-to-fstruct (attrlist &optional name) + ;; Like htmlize-face-to-fstruct, but accepts an ATTRLIST as input. + (let ((fstruct (make-htmlize-fstruct))) + (cond ((eq (car attrlist) 'foreground-color) + ;; ATTRLIST is (foreground-color . COLOR) + (setf (htmlize-fstruct-foreground fstruct) + (htmlize-color-to-rgb (cdr attrlist)))) + ((eq (car attrlist) 'background-color) + ;; ATTRLIST is (background-color . COLOR) + (setf (htmlize-fstruct-background fstruct) + (htmlize-color-to-rgb (cdr attrlist)))) + (t + ;; ATTRLIST is a plist. + (while attrlist + (let ((attr (pop attrlist)) + (value (pop attrlist))) + (when (and value (not (eq value 'unspecified))) + (htmlize-face-set-from-keyword-attr fstruct attr value)))))) + (setf (htmlize-fstruct-css-name fstruct) (or name "custom")) + fstruct)) + +(defun htmlize-decode-face-prop (prop) + "Turn face property PROP into a list of face-like objects." + ;; PROP can be a symbol naming a face, a string naming such a + ;; symbol, a cons (foreground-color . COLOR) or (background-color + ;; COLOR), a property list (:attr1 val1 :attr2 val2 ...), or a list + ;; of any of those. + ;; + ;; (htmlize-decode-face-prop 'face) -> (face) + ;; (htmlize-decode-face-prop '(face1 face2)) -> (face1 face2) + ;; (htmlize-decode-face-prop '(:attr "val")) -> ((:attr "val")) + ;; (htmlize-decode-face-prop '((:attr "val") face (foreground-color "red"))) + ;; -> ((:attr "val") face (foreground-color "red")) + ;; + ;; Unrecognized atoms or non-face symbols/strings are silently + ;; stripped away. + (cond ((null prop) + nil) + ((symbolp prop) + (and (facep prop) + (list prop))) + ((stringp prop) + (and (facep (intern-soft prop)) + (list prop))) + ((atom prop) + nil) + ((and (symbolp (car prop)) + (eq ?: (aref (symbol-name (car prop)) 0))) + (list prop)) + ((or (eq (car prop) 'foreground-color) + (eq (car prop) 'background-color)) + (list prop)) + (t + (apply #'nconc (mapcar #'htmlize-decode-face-prop prop))))) + +(defun htmlize-get-override-fstruct (face) + (let* ((raw-def (plist-get htmlize-face-overrides face)) + (def (cond ((stringp raw-def) (list :foreground raw-def)) + ((listp raw-def) raw-def) + (t + (error (format (concat "face override must be an " + "attribute list or string, got %s") + raw-def)))))) + (and def + (htmlize-attrlist-to-fstruct def (symbol-name face))))) + +(defun htmlize-make-face-map (faces) + ;; Return a hash table mapping Emacs faces to htmlize's fstructs. + ;; The keys are either face symbols or attrlists, so the test + ;; function must be `equal'. + (let ((face-map (make-hash-table :test 'equal)) + css-names) + (dolist (face faces) + (unless (gethash face face-map) + ;; Haven't seen FACE yet; convert it to an fstruct and cache + ;; it. + (let ((fstruct (htmlize-face-to-fstruct face))) + (setf (gethash face face-map) fstruct) + (let* ((css-name (htmlize-fstruct-css-name fstruct)) + (new-name css-name) + (i 0)) + ;; Uniquify the face's css-name by using NAME-1, NAME-2, + ;; etc. + (while (member new-name css-names) + (setq new-name (format "%s-%s" css-name (cl-incf i)))) + (unless (equal new-name css-name) + (setf (htmlize-fstruct-css-name fstruct) new-name)) + (push new-name css-names))))) + face-map)) + +(defun htmlize-unstringify-face (face) + "If FACE is a string, return it interned, otherwise return it unchanged." + (if (stringp face) + (intern face) + face)) + +(defun htmlize-faces-in-buffer () + "Return a list of faces used in the current buffer. +This is the set of faces specified by the `face' text property and by buffer +overlays that specify `face'." + (let (faces) + ;; Faces used by text properties. + (let ((pos (point-min)) face-prop next) + (while (< pos (point-max)) + (setq face-prop (get-text-property pos 'face) + next (or (next-single-property-change pos 'face) (point-max))) + (setq faces (cl-nunion (htmlize-decode-face-prop face-prop) + faces :test 'equal)) + (setq pos next))) + ;; Faces used by overlays. + (dolist (overlay (overlays-in (point-min) (point-max))) + (let ((face-prop (overlay-get overlay 'face))) + (setq faces (cl-nunion (htmlize-decode-face-prop face-prop) + faces :test 'equal)))) + faces)) + +(if (>= emacs-major-version 25) + (defun htmlize-sorted-overlays-at (pos) + (overlays-at pos t)) + + (defun htmlize-sorted-overlays-at (pos) + ;; Like OVERLAYS-AT with the SORTED argument, for older Emacsen. + (let ((overlays (overlays-at pos))) + (setq overlays (cl-sort overlays #'< + :key (lambda (o) + (- (overlay-end o) (overlay-start o))))) + (setq overlays + (cl-stable-sort overlays #'< + :key (lambda (o) + (let ((prio (overlay-get o 'priority))) + (if (numberp prio) prio 0))))) + (nreverse overlays)))) + + +;; htmlize-faces-at-point returns the faces in use at point. The +;; faces are sorted by increasing priority, i.e. the last face takes +;; precedence. +;; +;; This returns all the faces in the `face' property and all the faces +;; in the overlays at point. + +(defun htmlize-faces-at-point () + (let (all-faces) + ;; Faces from text properties. + (let ((face-prop (get-text-property (point) 'face))) + ;; we need to reverse the `face' prop because we want + ;; more specific faces to come later + (setq all-faces (nreverse (htmlize-decode-face-prop face-prop)))) + ;; Faces from overlays. + (let ((overlays + ;; Collect overlays at point that specify `face'. + (cl-delete-if-not (lambda (o) + (overlay-get o 'face)) + (nreverse (htmlize-sorted-overlays-at (point))))) + list face-prop) + (dolist (overlay overlays) + (setq face-prop (overlay-get overlay 'face) + list (nconc (htmlize-decode-face-prop face-prop) list))) + ;; Under "Merging Faces" the manual explicitly states + ;; that faces specified by overlays take precedence over + ;; faces specified by text properties. + (setq all-faces (nconc all-faces list))) + all-faces)) + +;; htmlize supports generating HTML in several flavors, some of which +;; use CSS, and others the element. We take an OO approach and +;; define "methods" that indirect to the functions that depend on +;; `htmlize-output-type'. The currently used methods are `doctype', +;; `insert-head', `body-tag', `pre-tag', and `text-markup'. Not all +;; output types define all methods. +;; +;; Methods are called either with (htmlize-method METHOD ARGS...) +;; special form, or by accessing the function with +;; (htmlize-method-function 'METHOD) and calling (funcall FUNCTION). +;; The latter form is useful in tight loops because `htmlize-method' +;; conses. + +(defmacro htmlize-method (method &rest args) + ;; Expand to (htmlize-TYPE-METHOD ...ARGS...). TYPE is the value of + ;; `htmlize-output-type' at run time. + `(funcall (htmlize-method-function ',method) ,@args)) + +(defun htmlize-method-function (method) + ;; Return METHOD's function definition for the current output type. + ;; The returned object can be safely funcalled. + (let ((sym (intern (format "htmlize-%s-%s" htmlize-output-type method)))) + (indirect-function (if (fboundp sym) + sym + (let ((default (intern (concat "htmlize-default-" + (symbol-name method))))) + (if (fboundp default) + default + 'ignore)))))) + +(defvar htmlize-memoization-table (make-hash-table :test 'equal)) + +(defmacro htmlize-memoize (key generator) + "Return the value of GENERATOR, memoized as KEY. +That means that GENERATOR will be evaluated and returned the first time +it's called with the same value of KEY. All other times, the cached +\(memoized) value will be returned." + (let ((value (cl-gensym))) + `(let ((,value (gethash ,key htmlize-memoization-table))) + (unless ,value + (setq ,value ,generator) + (setf (gethash ,key htmlize-memoization-table) ,value)) + ,value))) + +;;; Default methods. + +(defun htmlize-default-doctype () + nil ; no doc-string + ;; Note that the `font' output is technically invalid under this DTD + ;; because the DTD doesn't allow embedding in
.
+  ""
+  )
+
+(defun htmlize-default-body-tag (face-map)
+  nil					; no doc-string
+  face-map ; shut up the byte-compiler
+  "")
+
+(defun htmlize-default-pre-tag (face-map)
+  nil					; no doc-string
+  face-map ; shut up the byte-compiler
+  "
")
+
+
+;;; CSS based output support.
+
+;; Internal function; not a method.
+(defun htmlize-css-specs (fstruct)
+  (let (result)
+    (when (htmlize-fstruct-foreground fstruct)
+      (push (format "color: %s;" (htmlize-fstruct-foreground fstruct))
+	    result))
+    (when (htmlize-fstruct-background fstruct)
+      (push (format "background-color: %s;"
+		    (htmlize-fstruct-background fstruct))
+	    result))
+    (let ((size (htmlize-fstruct-size fstruct)))
+      (when (and size (not (eq htmlize-ignore-face-size t)))
+	(cond ((floatp size)
+	       (push (format "font-size: %d%%;" (* 100 size)) result))
+	      ((not (eq htmlize-ignore-face-size 'absolute))
+	       (push (format "font-size: %spt;" (/ size 10.0)) result)))))
+    (when (htmlize-fstruct-boldp fstruct)
+      (push "font-weight: bold;" result))
+    (when (htmlize-fstruct-italicp fstruct)
+      (push "font-style: italic;" result))
+    (when (htmlize-fstruct-underlinep fstruct)
+      (push "text-decoration: underline;" result))
+    (when (htmlize-fstruct-overlinep fstruct)
+      (push "text-decoration: overline;" result))
+    (when (htmlize-fstruct-strikep fstruct)
+      (push "text-decoration: line-through;" result))
+    (nreverse result)))
+
+(defun htmlize-css-insert-head (buffer-faces face-map)
+  (insert "    \n"))
+
+(defun htmlize-css-text-markup (fstruct-list buffer)
+  ;; Open the markup needed to insert text colored with FACES into
+  ;; BUFFER.  Return the function that closes the markup.
+
+  ;; In CSS mode, this is easy: just nest the text in one  tag for each face in FSTRUCT-LIST.
+  (dolist (fstruct fstruct-list)
+    (princ "" buffer))
+  (htmlize-lexlet ((fstruct-list fstruct-list) (buffer buffer))
+    (lambda ()
+      (dolist (fstruct fstruct-list)
+        (ignore fstruct)                ; shut up the byte-compiler
+        (princ "" buffer)))))
+
+;; `inline-css' output support.
+
+(defun htmlize-inline-css-body-tag (face-map)
+  (format ""
+	  (mapconcat #'identity (htmlize-css-specs (gethash 'default face-map))
+		     " ")))
+
+(defun htmlize-inline-css-pre-tag (face-map)
+  (if htmlize-pre-style
+      (format "
"
+              (mapconcat #'identity (htmlize-css-specs (gethash 'default face-map))
+                         " "))
+    (format "
")))
+
+(defun htmlize-inline-css-text-markup (fstruct-list buffer)
+  (let* ((merged (htmlize-merge-faces fstruct-list))
+	 (style (htmlize-memoize
+		 merged
+		 (let ((specs (htmlize-css-specs merged)))
+		   (and specs
+			(mapconcat #'identity (htmlize-css-specs merged) " "))))))
+    (when style
+      (princ "" buffer))
+    (htmlize-lexlet ((style style) (buffer buffer))
+      (lambda ()
+        (when style
+          (princ "" buffer))))))
+
+;;; `font' tag based output support.
+
+(defun htmlize-font-body-tag (face-map)
+  (let ((fstruct (gethash 'default face-map)))
+    (format ""
+	    (htmlize-fstruct-foreground fstruct)
+	    (htmlize-fstruct-background fstruct))))
+
+(defun htmlize-font-pre-tag (face-map)
+  (if htmlize-pre-style
+      (let ((fstruct (gethash 'default face-map)))
+        (format "
"
+                (htmlize-fstruct-foreground fstruct)
+                (htmlize-fstruct-background fstruct)))
+    (format "
")))
+       
+(defun htmlize-font-text-markup (fstruct-list buffer)
+  ;; In `font' mode, we use the traditional HTML means of altering
+  ;; presentation:  tag for colors,  for bold,  for
+  ;; underline, and  for strike-through.
+  (let* ((merged (htmlize-merge-faces fstruct-list))
+	 (markup (htmlize-memoize
+		  merged
+		  (cons (concat
+			 (and (htmlize-fstruct-foreground merged)
+			      (format "" (htmlize-fstruct-foreground merged)))
+			 (and (htmlize-fstruct-boldp merged)      "")
+			 (and (htmlize-fstruct-italicp merged)    "")
+			 (and (htmlize-fstruct-underlinep merged) "")
+			 (and (htmlize-fstruct-strikep merged)    ""))
+			(concat
+			 (and (htmlize-fstruct-strikep merged)    "")
+			 (and (htmlize-fstruct-underlinep merged) "")
+			 (and (htmlize-fstruct-italicp merged)    "")
+			 (and (htmlize-fstruct-boldp merged)      "")
+			 (and (htmlize-fstruct-foreground merged) ""))))))
+    (princ (car markup) buffer)
+    (htmlize-lexlet ((markup markup) (buffer buffer))
+      (lambda ()
+        (princ (cdr markup) buffer)))))
+
+(defun htmlize-buffer-1 ()
+  ;; Internal function; don't call it from outside this file.  Htmlize
+  ;; current buffer, writing the resulting HTML to a new buffer, and
+  ;; return it.  Unlike htmlize-buffer, this doesn't change current
+  ;; buffer or use switch-to-buffer.
+  (save-excursion
+    ;; Protect against the hook changing the current buffer.
+    (save-excursion
+      (run-hooks 'htmlize-before-hook))
+    ;; Convince font-lock support modes to fontify the entire buffer
+    ;; in advance.
+    (htmlize-ensure-fontified)
+    (clrhash htmlize-extended-character-cache)
+    (clrhash htmlize-memoization-table)
+    ;; It's important that the new buffer inherits default-directory
+    ;; from the current buffer.
+    (let ((htmlbuf (generate-new-buffer (if (buffer-file-name)
+                                            (htmlize-make-file-name
+                                             (file-name-nondirectory
+                                              (buffer-file-name)))
+                                          "*html*")))
+          (completed nil))
+      (unwind-protect
+          (let* ((buffer-faces (htmlize-faces-in-buffer))
+                 (face-map (htmlize-make-face-map (cl-adjoin 'default buffer-faces)))
+                 (places (cl-gensym))
+                 (title (if (buffer-file-name)
+                            (file-name-nondirectory (buffer-file-name))
+                          (buffer-name))))
+            (when htmlize-generate-hyperlinks
+              (htmlize-create-auto-links))
+            (when htmlize-replace-form-feeds
+              (htmlize-shadow-form-feeds))
+
+            ;; Initialize HTMLBUF and insert the HTML prolog.
+            (with-current-buffer htmlbuf
+              (buffer-disable-undo)
+              (insert (htmlize-method doctype) ?\n
+                      (format "\n"
+                              htmlize-version htmlize-output-type)
+                      "\n  ")
+              (put places 'head-start (point-marker))
+              (insert "\n"
+                      "    " (htmlize-protect-string title) "\n"
+                      (if htmlize-html-charset
+                          (format (concat "    \n")
+                                  htmlize-html-charset)
+                        "")
+                      htmlize-head-tags)
+              (htmlize-method insert-head buffer-faces face-map)
+              (insert "  ")
+              (put places 'head-end (point-marker))
+              (insert "\n  ")
+              (put places 'body-start (point-marker))
+              (insert (htmlize-method body-tag face-map)
+                      "\n    ")
+              (put places 'content-start (point-marker))
+              (insert (htmlize-method pre-tag face-map) "\n"))
+            (let ((text-markup
+                   ;; Get the inserter method, so we can funcall it inside
+                   ;; the loop.  Not calling `htmlize-method' in the loop
+                   ;; body yields a measurable speed increase.
+                   (htmlize-method-function 'text-markup))
+                  ;; Declare variables used in loop body outside the loop
+                  ;; because it's faster to establish `let' bindings only
+                  ;; once.
+                  next-change text face-list trailing-ellipsis
+                  fstruct-list last-fstruct-list
+                  (close-markup (lambda ())))
+              ;; This loop traverses and reads the source buffer, appending
+              ;; the resulting HTML to HTMLBUF.  This method is fast
+              ;; because: 1) it doesn't require examining the text
+              ;; properties char by char (htmlize-next-face-change is used
+              ;; to move between runs with the same face), and 2) it doesn't
+              ;; require frequent buffer switches, which are slow because
+              ;; they rebind all buffer-local vars.
+              (goto-char (point-min))
+              (while (not (eobp))
+                (setq next-change (htmlize-next-face-change (point)))
+                ;; Get faces in use between (point) and NEXT-CHANGE, and
+                ;; convert them to fstructs.
+                (setq face-list (htmlize-faces-at-point)
+                      fstruct-list (delq nil (mapcar (lambda (f)
+                                                       (gethash f face-map))
+                                                     face-list)))
+                (cl-multiple-value-setq (text trailing-ellipsis)
+                  (htmlize-extract-text (point) next-change trailing-ellipsis))
+                ;; Don't bother writing anything if there's no text (this
+                ;; happens in invisible regions).
+                (when (> (length text) 0)
+                  ;; Open the new markup if necessary and insert the text.
+                  (when (not (cl-equalp fstruct-list last-fstruct-list))
+                    (funcall close-markup)
+                    (setq last-fstruct-list fstruct-list
+                          close-markup (funcall text-markup fstruct-list htmlbuf)))
+                  (princ text htmlbuf))
+                (goto-char next-change))
+
+              ;; We've gone through the buffer; close the markup from
+              ;; the last run, if any.
+              (funcall close-markup))
+
+            ;; Insert the epilog and post-process the buffer.
+            (with-current-buffer htmlbuf
+              (insert "
") + (put places 'content-end (point-marker)) + (insert "\n ") + (put places 'body-end (point-marker)) + (insert "\n\n") + (htmlize-defang-local-variables) + (goto-char (point-min)) + (when htmlize-html-major-mode + ;; What sucks about this is that the minor modes, most notably + ;; font-lock-mode, won't be initialized. Oh well. + (funcall htmlize-html-major-mode)) + (set (make-local-variable 'htmlize-buffer-places) + (symbol-plist places)) + (run-hooks 'htmlize-after-hook) + (buffer-enable-undo)) + (setq completed t) + htmlbuf) + + (when (not completed) + (kill-buffer htmlbuf)) + (htmlize-delete-tmp-overlays))))) + +;; Utility functions. + +(defmacro htmlize-with-fontify-message (&rest body) + ;; When forcing fontification of large buffers in + ;; htmlize-ensure-fontified, inform the user that he is waiting for + ;; font-lock, not for htmlize to finish. + `(progn + (if (> (buffer-size) 65536) + (message "Forcing fontification of %s..." + (buffer-name (current-buffer)))) + ,@body + (if (> (buffer-size) 65536) + (message "Forcing fontification of %s...done" + (buffer-name (current-buffer)))))) + +(defun htmlize-ensure-fontified () + ;; If font-lock is being used, ensure that the "support" modes + ;; actually fontify the buffer. If font-lock is not in use, we + ;; don't care because, except in htmlize-file, we don't force + ;; font-lock on the user. + (when font-lock-mode + ;; In part taken from ps-print-ensure-fontified in GNU Emacs 21. + (when (and (boundp 'jit-lock-mode) + (symbol-value 'jit-lock-mode)) + (htmlize-with-fontify-message + (jit-lock-fontify-now (point-min) (point-max)))) + + (if (fboundp 'font-lock-ensure) + (font-lock-ensure) + ;; Emacs prior to 25.1 + (with-no-warnings + (font-lock-mode 1) + (font-lock-fontify-buffer))))) + + +;;;###autoload +(defun htmlize-buffer (&optional buffer) + "Convert BUFFER to HTML, preserving colors and decorations. + +The generated HTML is available in a new buffer, which is returned. +When invoked interactively, the new buffer is selected in the current +window. The title of the generated document will be set to the buffer's +file name or, if that's not available, to the buffer's name. + +Note that htmlize doesn't fontify your buffers, it only uses the +decorations that are already present. If you don't set up font-lock or +something else to fontify your buffers, the resulting HTML will be +plain. Likewise, if you don't like the choice of colors, fix the mode +that created them, or simply alter the faces it uses." + (interactive) + (let ((htmlbuf (with-current-buffer (or buffer (current-buffer)) + (htmlize-buffer-1)))) + (when (interactive-p) + (switch-to-buffer htmlbuf)) + htmlbuf)) + +;;;###autoload +(defun htmlize-region (beg end) + "Convert the region to HTML, preserving colors and decorations. +See `htmlize-buffer' for details." + (interactive "r") + ;; Don't let zmacs region highlighting end up in HTML. + (when (fboundp 'zmacs-deactivate-region) + (zmacs-deactivate-region)) + (let ((htmlbuf (save-restriction + (narrow-to-region beg end) + (htmlize-buffer-1)))) + (when (interactive-p) + (switch-to-buffer htmlbuf)) + htmlbuf)) + +(defun htmlize-region-for-paste (beg end) + "Htmlize the region and return just the HTML as a string. +This forces the `inline-css' style and only returns the HTML body, +but without the BODY tag. This should make it useful for inserting +the text to another HTML buffer." + (let* ((htmlize-output-type 'inline-css) + (htmlbuf (htmlize-region beg end))) + (unwind-protect + (with-current-buffer htmlbuf + (buffer-substring (plist-get htmlize-buffer-places 'content-start) + (plist-get htmlize-buffer-places 'content-end))) + (kill-buffer htmlbuf)))) + +(defun htmlize-region-save-screenshot (beg end) + "Save the htmlized (see `htmlize-region-for-paste') region in +the kill ring. Uses `inline-css', with style information in +`
' tags, so that the rendering of the marked up text
+approximates the buffer as closely as possible."
+  (interactive "r")
+  (let ((htmlize-pre-style t))
+    (kill-new (htmlize-region-for-paste beg end)))
+  (deactivate-mark))
+
+(defun htmlize-make-file-name (file)
+  "Make an HTML file name from FILE.
+
+In its default implementation, this simply appends `.html' to FILE.
+This function is called by htmlize to create the buffer file name, and
+by `htmlize-file' to create the target file name.
+
+More elaborate transformations are conceivable, such as changing FILE's
+extension to `.html' (\"file.c\" -> \"file.html\").  If you want them,
+overload this function to do it and htmlize will comply."
+  (concat file ".html"))
+
+;; Older implementation of htmlize-make-file-name that changes FILE's
+;; extension to ".html".
+;(defun htmlize-make-file-name (file)
+;  (let ((extension (file-name-extension file))
+;	(sans-extension (file-name-sans-extension file)))
+;    (if (or (equal extension "html")
+;	    (equal extension "htm")
+;	    (equal sans-extension ""))
+;	(concat file ".html")
+;      (concat sans-extension ".html"))))
+
+;;;###autoload
+(defun htmlize-file (file &optional target)
+  "Load FILE, fontify it, convert it to HTML, and save the result.
+
+Contents of FILE are inserted into a temporary buffer, whose major mode
+is set with `normal-mode' as appropriate for the file type.  The buffer
+is subsequently fontified with `font-lock' and converted to HTML.  Note
+that, unlike `htmlize-buffer', this function explicitly turns on
+font-lock.  If a form of highlighting other than font-lock is desired,
+please use `htmlize-buffer' directly on buffers so highlighted.
+
+Buffers currently visiting FILE are unaffected by this function.  The
+function does not change current buffer or move the point.
+
+If TARGET is specified and names a directory, the resulting file will be
+saved there instead of to FILE's directory.  If TARGET is specified and
+does not name a directory, it will be used as output file name."
+  (interactive (list (read-file-name
+		      "HTML-ize file: "
+		      nil nil nil (and (buffer-file-name)
+				       (file-name-nondirectory
+					(buffer-file-name))))))
+  (let ((output-file (if (and target (not (file-directory-p target)))
+			 target
+		       (expand-file-name
+			(htmlize-make-file-name (file-name-nondirectory file))
+			(or target (file-name-directory file)))))
+	;; Try to prevent `find-file-noselect' from triggering
+	;; font-lock because we'll fontify explicitly below.
+	(font-lock-mode nil)
+	(font-lock-auto-fontify nil)
+	(global-font-lock-mode nil)
+	;; Ignore the size limit for the purposes of htmlization.
+	(font-lock-maximum-size nil))
+    (with-temp-buffer
+      ;; Insert FILE into the temporary buffer.
+      (insert-file-contents file)
+      ;; Set the file name so normal-mode and htmlize-buffer-1 pick it
+      ;; up.  Restore it afterwards so with-temp-buffer's kill-buffer
+      ;; doesn't complain about killing a modified buffer.
+      (let ((buffer-file-name file))
+	;; Set the major mode for the sake of font-lock.
+	(normal-mode)
+	;; htmlize the buffer and save the HTML.
+	(with-current-buffer (htmlize-buffer-1)
+	  (unwind-protect
+	      (progn
+		(run-hooks 'htmlize-file-hook)
+		(write-region (point-min) (point-max) output-file))
+	    (kill-buffer (current-buffer)))))))
+  ;; I haven't decided on a useful return value yet, so just return
+  ;; nil.
+  nil)
+
+;;;###autoload
+(defun htmlize-many-files (files &optional target-directory)
+  "Convert FILES to HTML and save the corresponding HTML versions.
+
+FILES should be a list of file names to convert.  This function calls
+`htmlize-file' on each file; see that function for details.  When
+invoked interactively, you are prompted for a list of files to convert,
+terminated with RET.
+
+If TARGET-DIRECTORY is specified, the HTML files will be saved to that
+directory.  Normally, each HTML file is saved to the directory of the
+corresponding source file."
+  (interactive
+   (list
+    (let (list file)
+      ;; Use empty string as DEFAULT because setting DEFAULT to nil
+      ;; defaults to the directory name, which is not what we want.
+      (while (not (equal (setq file (read-file-name
+				     "HTML-ize file (RET to finish): "
+				     (and list (file-name-directory
+						(car list)))
+				     "" t))
+			 ""))
+	(push file list))
+      (nreverse list))))
+  ;; Verify that TARGET-DIRECTORY is indeed a directory.  If it's a
+  ;; file, htmlize-file will use it as target, and that doesn't make
+  ;; sense.
+  (and target-directory
+       (not (file-directory-p target-directory))
+       (error "target-directory must name a directory: %s" target-directory))
+  (dolist (file files)
+    (htmlize-file file target-directory)))
+
+;;;###autoload
+(defun htmlize-many-files-dired (arg &optional target-directory)
+  "HTMLize dired-marked files."
+  (interactive "P")
+  (htmlize-many-files (dired-get-marked-files nil arg) target-directory))
+
+(provide 'htmlize)
+
+;; Local Variables:
+;; byte-compile-warnings: (not unresolved obsolete)
+;; End:
+
+;;; htmlize.el ends here
diff --git a/emacs-htmlize/htmlize.el.html b/emacs-htmlize/htmlize.el.html
new file mode 100644
index 0000000..a5e94e1
--- /dev/null
+++ b/emacs-htmlize/htmlize.el.html
@@ -0,0 +1,2032 @@
+
+
+
+  
+    htmlize.el
+    
+  
+  
+    
+;;; htmlize.el --- Convert buffer text and decorations to HTML.
+
+;; Copyright (C) 1997-2003,2005,2006,2009,2011,2012 Hrvoje Niksic
+
+;; Author: Hrvoje Niksic <hniksic@xemacs.org>
+;; Keywords: hypermedia, extensions
+;; Version: 1.45
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;; This package converts the buffer text and the associated
+;; decorations to HTML.  Mail to <hniksic@xemacs.org> to discuss
+;; features and additions.  All suggestions are more than welcome.
+
+;; To use it, just switch to the buffer you want HTML-ized and type
+;; `M-x htmlize-buffer'.  You will be switched to a new buffer that
+;; contains the resulting HTML code.  You can edit and inspect this
+;; buffer, or you can just save it with C-x C-w.  `M-x htmlize-file'
+;; will find a file, fontify it, and save the HTML version in
+;; FILE.html, without any additional intervention.  `M-x
+;; htmlize-many-files' allows you to htmlize any number of files in
+;; the same manner.  `M-x htmlize-many-files-dired' does the same for
+;; files marked in a dired buffer.
+
+;; htmlize supports three types of HTML output, selected by setting
+;; `htmlize-output-type': `css', `inline-css', and `font'.  In `css'
+;; mode, htmlize uses cascading style sheets to specify colors; it
+;; generates classes that correspond to Emacs faces and uses <span
+;; class=FACE>...</span> to color parts of text.  In this mode, the
+;; produced HTML is valid under the 4.01 strict DTD, as confirmed by
+;; the W3C validator.  `inline-css' is like `css', except the CSS is
+;; put directly in the STYLE attribute of the SPAN element, making it
+;; possible to paste the generated HTML into existing HTML documents.
+;; In `font' mode, htmlize uses <font color="...">...</font> to
+;; colorize HTML, which is not standard-compliant, but works better in
+;; older browsers.  `css' mode is the default.
+
+;; You can also use htmlize from your Emacs Lisp code.  When called
+;; non-interactively, `htmlize-buffer' and `htmlize-region' will
+;; return the resulting HTML buffer, but will not change current
+;; buffer or move the point.  htmlize will do its best to work on
+;; non-windowing Emacs sessions but the result will be limited to
+;; colors supported by the terminal.
+
+;; htmlize aims for compatibility with Emacsen version 21 and later.
+;; Please let me know if it doesn't work on the version of XEmacs or
+;; GNU Emacs that you are using.  The package relies on the presence
+;; of CL extensions, especially for cross-emacs compatibility; please
+;; don't try to remove that dependency.  I see no practical problems
+;; with using the full power of the CL extensions, except that one
+;; might learn to like them too much.
+
+;; The latest version is available as a git repository at:
+;;
+;;        <http://fly.srk.fer.hr/~hniksic/emacs/htmlize.git>
+;;
+;; The snapshot of the latest release can be obtained at:
+;;
+;;        <http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el.cgi>
+;;
+;; You can find a sample of htmlize's output (possibly generated with
+;; an older version) at:
+;;
+;;        <http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el.html>
+
+;; Thanks go to the many people who have sent reports and contributed
+;; comments, suggestions, and fixes.  They include Ron Gut, Bob
+;; Weiner, Toni Drabik, Peter Breton, Ville Skytta, Thomas Vogels,
+;; Juri Linkov, Maciek Pasternacki, and many others.
+
+;; User quotes: "You sir, are a sick, sick, _sick_ person. :)"
+;;                  -- Bill Perry, author of Emacs/W3
+
+
+;;; Code: + +(require 'cl) +(eval-when-compile + (defvar unresolved) + (if (string-match "XEmacs" emacs-version) + (byte-compiler-options + (warnings (- unresolved)))) + (defvar font-lock-auto-fontify) + (defvar font-lock-support-mode) + (defvar global-font-lock-mode)) + +(defconst htmlize-version "1.45") + +(defgroup htmlize nil + "Convert buffer text and faces to HTML." + :group 'hypermedia) + +(defcustom htmlize-head-tags "" + "Additional tags to insert within HEAD of the generated document." + :type 'string + :group 'htmlize) + +(defcustom htmlize-output-type 'css + "Output type of generated HTML, one of `css', `inline-css', or `font'. +When set to `css' (the default), htmlize will generate a style sheet +with description of faces, and use it in the HTML document, specifying +the faces in the actual text with <span class=\"FACE\">. + +When set to `inline-css', the style will be generated as above, but +placed directly in the STYLE attribute of the span ELEMENT: <span +style=\"STYLE\">. This makes it easier to paste the resulting HTML to +other documents. + +When set to `font', the properties will be set using layout tags +<font>, <b>, <i>, <u>, and <strike>. + +`css' output is normally preferred, but `font' is still useful for +supporting old, pre-CSS browsers, and both `inline-css' and `font' for +easier embedding of colorized text in foreign HTML documents (no style +sheet to carry around)." + :type '(choice (const css) (const inline-css) (const font)) + :group 'htmlize) + +(defcustom htmlize-use-images t + "Whether htmlize generates `img' for images attached to buffer contents." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-force-inline-images nil + "Non-nil means generate all images inline using data URLs. +Normally htmlize converts image descriptors with :file properties to +relative URIs, and those with :data properties to data URIs. With this +flag set, the images specified as a file name are loaded into memory and +embedded in the HTML as data URIs." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-max-alt-text 100 + "Maximum size of text to use as ALT text in images. + +Normally when htmlize encounters text covered by the `display' property +that specifies an image, it generates an `alt' attribute containing the +original text. If the text is larger than `htmlize-max-alt-text' characters, +this will not be done.") + +(defcustom htmlize-transform-image 'htmlize-default-transform-image + "Function called to modify the image descriptor. + +The function is called with the image descriptor found in the buffer and +the text the image is supposed to replace. It should return a (possibly +different) image descriptor property list or a replacement string to use +instead of of the original buffer text. + +Returning nil is the same as returning the original text." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-generate-hyperlinks t + "Non-nil means auto-generate the links from URLs and mail addresses in buffer. + +This is on by default; set it to nil if you don't want htmlize to +autogenerate such links. Note that this option only turns off automatic +search for contents that looks like URLs and converting them to links. +It has no effect on whether htmlize respects the `htmlize-link' property." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-hyperlink-style " + a { + color: inherit; + background-color: inherit; + font: inherit; + text-decoration: inherit; + } + a:hover { + text-decoration: underline; + } +" + "The CSS style used for hyperlinks when in CSS mode." + :type 'string + :group 'htmlize) + +(defcustom htmlize-replace-form-feeds t + "Non-nil means replace form feeds in source code with HTML separators. +Form feeds are the ^L characters at line beginnings that are sometimes +used to separate sections of source code. If this variable is set to +`t', form feed characters are replaced with the <hr> separator. If this +is a string, it specifies the replacement to use. Note that <pre> is +temporarily closed before the separator is inserted, so the default +replacement is effectively \"</pre><hr /><pre>\". If you specify +another replacement, don't forget to close and reopen the <pre> if you +want the output to remain valid HTML. + +If you need more elaborate processing, set this to nil and use +htmlize-after-hook." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-html-charset nil + "The charset declared by the resulting HTML documents. +When non-nil, causes htmlize to insert the following in the HEAD section +of the generated HTML: + + <meta http-equiv=\"Content-Type\" content=\"text/html; charset=CHARSET\"> + +where CHARSET is the value you've set for htmlize-html-charset. Valid +charsets are defined by MIME and include strings like \"iso-8859-1\", +\"iso-8859-15\", \"utf-8\", etc. + +If you are using non-Latin-1 charsets, you might need to set this for +your documents to render correctly. Also, the W3C validator requires +submitted HTML documents to declare a charset. So if you care about +validation, you can use this to prevent the validator from bitching. + +Needless to say, if you set this, you should actually make sure that +the buffer is in the encoding you're claiming it is in. (This is +normally achieved by using the correct file coding system for the +buffer.) If you don't understand what that means, you should probably +leave this option in its default setting." + :type '(choice (const :tag "Unset" nil) + string) + :group 'htmlize) + +(defcustom htmlize-convert-nonascii-to-entities t + "Whether non-ASCII characters should be converted to HTML entities. + +When this is non-nil, characters with codes in the 128-255 range will be +considered Latin 1 and rewritten as \"&#CODE;\". Characters with codes +above 255 will be converted to \"&#UCS;\", where UCS denotes the Unicode +code point of the character. If the code point cannot be determined, +the character will be copied unchanged, as would be the case if the +option were nil. + +When the option is nil, the non-ASCII characters are copied to HTML +without modification. In that case, the web server and/or the browser +must be set to understand the encoding that was used when saving the +buffer. (You might also want to specify it by setting +`htmlize-html-charset'.) + +Note that in an HTML entity \"&#CODE;\", CODE is always a UCS code point, +which has nothing to do with the charset the page is in. For example, +\"&#169;\" *always* refers to the copyright symbol, regardless of charset +specified by the META tag or the charset sent by the HTTP server. In +other words, \"&#169;\" is exactly equivalent to \"&copy;\". + +For most people htmlize will work fine with this option left at the +default setting; don't change it unless you know what you're doing." + :type 'sexp + :group 'htmlize) + +(defcustom htmlize-ignore-face-size 'absolute + "Whether face size should be ignored when generating HTML. +If this is nil, face sizes are used. If set to t, sizes are ignored +If set to `absolute', only absolute size specifications are ignored. +Please note that font sizes only work with CSS-based output types." + :type '(choice (const :tag "Don't ignore" nil) + (const :tag "Ignore all" t) + (const :tag "Ignore absolute" absolute)) + :group 'htmlize) + +(defcustom htmlize-css-name-prefix "" + "The prefix used for CSS names. +The CSS names that htmlize generates from face names are often too +generic for CSS files; for example, `font-lock-type-face' is transformed +to `type'. Use this variable to add a prefix to the generated names. +The string \"htmlize-\" is an example of a reasonable prefix." + :type 'string + :group 'htmlize) + +(defcustom htmlize-use-rgb-txt t + "Whether `rgb.txt' should be used to convert color names to RGB. + +This conversion means determining, for instance, that the color +\"IndianRed\" corresponds to the (205, 92, 92) RGB triple. `rgb.txt' +is the X color database that maps hundreds of color names to such RGB +triples. When this variable is non-nil, `htmlize' uses `rgb.txt' to +look up color names. + +If this variable is nil, htmlize queries Emacs for RGB components of +colors using `color-instance-rgb-components' and `color-values'. +This can yield incorrect results on non-true-color displays. + +If the `rgb.txt' file is not found (which will be the case if you're +running Emacs on non-X11 systems), this option is ignored." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-html-major-mode nil + "The mode the newly created HTML buffer will be put in. +Set this to nil if you prefer the default (fundamental) mode." + :type '(radio (const :tag "No mode (fundamental)" nil) + (function-item html-mode) + (function :tag "User-defined major mode")) + :group 'htmlize) + +(defvar htmlize-before-hook nil + "Hook run before htmlizing a buffer. +The hook functions are run in the source buffer (not the resulting HTML +buffer).") + +(defvar htmlize-after-hook nil + "Hook run after htmlizing a buffer. +Unlike `htmlize-before-hook', these functions are run in the generated +HTML buffer. You may use them to modify the outlook of the final HTML +output.") + +(defvar htmlize-file-hook nil + "Hook run by `htmlize-file' after htmlizing a file, but before saving it.") + +(defvar htmlize-buffer-places) +
+;;; Some cross-Emacs compatibility. + +;; I try to conditionalize on features rather than Emacs version, but +;; in some cases checking against the version *is* necessary. +(defconst htmlize-running-xemacs (string-match "XEmacs" emacs-version)) + +;; We need a function that efficiently finds the next change of a +;; property regardless of whether the change occurred because of a +;; text property or an extent/overlay. +(cond + (htmlize-running-xemacs + (defun htmlize-next-change (pos prop &optional limit) + (if prop + (next-single-char-property-change pos prop nil (or limit (point-max))) + (next-property-change pos nil (or limit (point-max))))) + (defun htmlize-next-face-change (pos &optional limit) + (htmlize-next-change pos 'face limit))) + ((fboundp 'next-single-char-property-change) + ;; GNU Emacs 21+ + (defun htmlize-next-change (pos prop &optional limit) + (if prop + (next-single-char-property-change pos prop nil limit) + (next-char-property-change pos limit))) + (defun htmlize-overlay-faces-at (pos) + (delq nil (mapcar (lambda (o) (overlay-get o 'face)) (overlays-at pos)))) + (defun htmlize-next-face-change (pos &optional limit) + ;; (htmlize-next-change pos 'face limit) would skip over entire + ;; overlays that specify the `face' property, even when they + ;; contain smaller text properties that also specify `face'. + ;; Emacs display engine merges those faces, and so must we. + (or limit + (setq limit (point-max))) + (let ((next-prop (next-single-property-change pos 'face nil limit)) + (overlay-faces (htmlize-overlay-faces-at pos))) + (while (progn + (setq pos (next-overlay-change pos)) + (and (< pos next-prop) + (equal overlay-faces (htmlize-overlay-faces-at pos))))) + (setq pos (min pos next-prop)) + ;; Additionally, we include the entire region that specifies the + ;; `display' property. + (when (get-char-property pos 'display) + (setq pos (next-single-char-property-change pos 'display nil limit))) + pos))) + (t + (error "htmlize requires next-single-property-change or \ +next-single-char-property-change"))) + +(defmacro htmlize-lexlet (&rest letforms) + (declare (indent 1) (debug let)) + (if (and (boundp 'lexical-binding) + lexical-binding) + `(let ,@letforms) + ;; cl extensions have a macro implementing lexical let + `(lexical-let ,@letforms))) + +;; Simple overlay emulation for XEmacs + +(cond + (htmlize-running-xemacs + (defalias 'htmlize-make-overlay 'make-extent) + (defalias 'htmlize-overlay-put 'set-extent-property) + (defalias 'htmlize-overlay-get 'extent-property) + (defun htmlize-overlays-in (beg end) (extent-list nil beg end)) + (defalias 'htmlize-delete-overlay 'detach-extent)) + (t + (defalias 'htmlize-make-overlay 'make-overlay) + (defalias 'htmlize-overlay-put 'overlay-put) + (defalias 'htmlize-overlay-get 'overlay-get) + (defalias 'htmlize-overlays-in 'overlays-in) + (defalias 'htmlize-delete-overlay 'delete-overlay))) + +
+;;; Transformation of buffer text: HTML escapes, untabification, etc. + +(defvar htmlize-basic-character-table + ;; Map characters in the 0-127 range to either one-character strings + ;; or to numeric entities. + (let ((table (make-vector 128 ?\0))) + ;; Map characters in the 32-126 range to themselves, others to + ;; &#CODE entities; + (dotimes (i 128) + (setf (aref table i) (if (and (>= i 32) (<= i 126)) + (char-to-string i) + (format "&#%d;" i)))) + ;; Set exceptions manually. + (setf + ;; Don't escape newline, carriage return, and TAB. + (aref table ?\n) "\n" + (aref table ?\r) "\r" + (aref table ?\t) "\t" + ;; Escape &, <, and >. + (aref table ?&) "&amp;" + (aref table ?<) "&lt;" + (aref table ?>) "&gt;" + ;; Not escaping '"' buys us a measurable speedup. It's only + ;; necessary to quote it for strings used in attribute values, + ;; which htmlize doesn't typically do. + ;(aref table ?\") "&quot;" + ) + table)) + +;; A cache of HTML representation of non-ASCII characters. Depending +;; on the setting of `htmlize-convert-nonascii-to-entities', this maps +;; non-ASCII characters to either "&#<code>;" or "<char>" (mapconcat's +;; mapper must always return strings). It's only filled as characters +;; are encountered, so that in a buffer with e.g. French text, it will +;; only ever contain French accented characters as keys. It's cleared +;; on each entry to htmlize-buffer-1 to allow modifications of +;; `htmlize-convert-nonascii-to-entities' to take effect. +(defvar htmlize-extended-character-cache (make-hash-table :test 'eq)) + +(defun htmlize-protect-string (string) + "HTML-protect string, escaping HTML metacharacters and I18N chars." + ;; Only protecting strings that actually contain unsafe or non-ASCII + ;; chars removes a lot of unnecessary funcalls and consing. + (if (not (string-match "[^\r\n\t -%'-;=?-~]" string)) + string + (mapconcat (lambda (char) + (cond + ((< char 128) + ;; ASCII: use htmlize-basic-character-table. + (aref htmlize-basic-character-table char)) + ((gethash char htmlize-extended-character-cache) + ;; We've already seen this char; return the cached + ;; string. + ) + ((not htmlize-convert-nonascii-to-entities) + ;; If conversion to entities is not desired, always + ;; copy the char literally. + (setf (gethash char htmlize-extended-character-cache) + (char-to-string char))) + ((< char 256) + ;; Latin 1: no need to call encode-char. + (setf (gethash char htmlize-extended-character-cache) + (format "&#%d;" char))) + ((encode-char char 'ucs) + ;; Must check if encode-char works for CHAR; + ;; it fails for Arabic and possibly elsewhere. + (setf (gethash char htmlize-extended-character-cache) + (format "&#%d;" (encode-char char 'ucs)))) + (t + ;; encode-char doesn't work for this char. Copy it + ;; unchanged and hope for the best. + (setf (gethash char htmlize-extended-character-cache) + (char-to-string char))))) + string ""))) + +(defun htmlize-attr-escape (string) + ;; Like htmlize-protect-string, but also escapes double-quoted + ;; strings to make it usable in attribute values. + (setq string (htmlize-protect-string string)) + (if (not (string-match "\"" string)) + string + (mapconcat (lambda (char) + (if (eql char ?\") + "&quot;" + (char-to-string char))) + string ""))) + +(defsubst htmlize-concat (list) + (if (and (consp list) (null (cdr list))) + ;; Don't create a new string in the common case where the list only + ;; consists of one element. + (car list) + (apply #'concat list))) + +(defun htmlize-format-link (linkprops text) + (let ((uri (if (stringp linkprops) + linkprops + (plist-get linkprops :uri))) + (escaped-text (htmlize-protect-string text))) + (if uri + (format "<a href=\"%s\">%s</a>" (htmlize-attr-escape uri) escaped-text) + escaped-text))) + +(defun htmlize-escape-or-link (string) + ;; Escape STRING and/or add hyperlinks. STRING comes from a + ;; `display' property. + (let ((pos 0) (end (length string)) outlist) + (while (< pos end) + (let* ((link (get-char-property pos 'htmlize-link string)) + (next-link-change (next-single-property-change + pos 'htmlize-link string end)) + (chunk (substring string pos next-link-change))) + (push + (cond (link + (htmlize-format-link link chunk)) + ((get-char-property 0 'htmlize-literal chunk) + chunk) + (t + (htmlize-protect-string chunk))) + outlist) + (setq pos next-link-change))) + (htmlize-concat (nreverse outlist)))) + +(defun htmlize-display-prop-to-html (display text) + (let (desc) + (cond ((stringp display) + ;; Emacs ignores recursive display properties. + (htmlize-escape-or-link display)) + ((not (eq (car-safe display) 'image)) + (htmlize-protect-string text)) + ((null (setq desc (funcall htmlize-transform-image + (cdr display) text))) + (htmlize-escape-or-link text)) + ((stringp desc) + (htmlize-escape-or-link desc)) + (t + (htmlize-generate-image desc text))))) + +(defun htmlize-string-to-html (string) + ;; Convert the string to HTML, including images attached as + ;; `display' property and links as `htmlize-link' property. In a + ;; string without images or links, this is equivalent to + ;; `htmlize-protect-string'. + (let ((pos 0) (end (length string)) outlist) + (while (< pos end) + (let* ((display (get-char-property pos 'display string)) + (next-display-change (next-single-property-change + pos 'display string end)) + (chunk (substring string pos next-display-change))) + (push + (if display + (htmlize-display-prop-to-html display chunk) + (htmlize-escape-or-link chunk)) + outlist) + (setq pos next-display-change))) + (htmlize-concat (nreverse outlist)))) + +(defun htmlize-default-transform-image (imgprops _text) + "Default transformation of image descriptor to something usable in HTML. + +If `htmlize-use-images' is nil, the function always returns nil, meaning +use original text. Otherwise, it tries to find the image for images that +specify a file name. If `htmlize-force-inline-images' is non-nil, it also +converts the :file attribute to :data and returns the modified property +list." + (when htmlize-use-images + (when (plist-get imgprops :file) + (let ((location (plist-get (cdr (find-image (list imgprops))) :file))) + (when location + (setq imgprops (plist-put (copy-list imgprops) :file location))))) + (if htmlize-force-inline-images + (let ((location (plist-get imgprops :file)) + data) + (when location + (with-temp-buffer + (condition-case nil + (progn + (insert-file-contents-literally location) + (setq data (buffer-string))) + (error nil)))) + ;; if successful, return the new plist, otherwise return + ;; nil, which will use the original text + (and data + (plist-put (plist-put imgprops :file nil) + :data data))) + imgprops))) + +(defun htmlize-alt-text (_imgprops origtext) + (and (/= (length origtext) 0) + (<= (length origtext) htmlize-max-alt-text) + (not (string-match "[\0-\x1f]" origtext)) + origtext)) + +(defun htmlize-generate-image (imgprops origtext) + (let* ((alt-text (htmlize-alt-text imgprops origtext)) + (alt-attr (if alt-text + (format " alt=\"%s\"" (htmlize-attr-escape alt-text)) + ""))) + (cond ((plist-get imgprops :file) + ;; Try to find the image in image-load-path + (let* ((found-props (cdr (find-image (list imgprops)))) + (file (or (plist-get found-props :file) + (plist-get imgprops :file)))) + (format "<img src=\"%s\"%s />" + (htmlize-attr-escape (file-relative-name file)) + alt-attr))) + ((plist-get imgprops :data) + (format "<img src=\"data:image/%s;base64,%s\"%s />" + (or (plist-get imgprops :type) "") + (base64-encode-string (plist-get imgprops :data)) + alt-attr))))) + +(defconst htmlize-ellipsis "...") +(put-text-property 0 (length htmlize-ellipsis) 'htmlize-ellipsis t htmlize-ellipsis) + +(defun htmlize-match-inv-spec (inv) + (member* inv buffer-invisibility-spec + :key (lambda (i) + (if (symbolp i) i (car i))))) + +(defun htmlize-decode-invisibility-spec (invisible) + ;; Return t, nil, or `ellipsis', depending on how invisible text should be inserted. + + (if (not (listp buffer-invisibility-spec)) + ;; If buffer-invisibility-spec is not a list, then all + ;; characters with non-nil `invisible' property are visible. + (not invisible) + + ;; Otherwise, the value of a non-nil `invisible' property can be: + ;; 1. a symbol -- make the text invisible if it matches + ;; buffer-invisibility-spec. + ;; 2. a list of symbols -- make the text invisible if + ;; any symbol in the list matches + ;; buffer-invisibility-spec. + ;; If the match of buffer-invisibility-spec has a non-nil + ;; CDR, replace the invisible text with an ellipsis. + (let ((match (if (symbolp invisible) + (htmlize-match-inv-spec invisible) + (some #'htmlize-match-inv-spec invisible)))) + (cond ((null match) t) + ((cdr-safe (car match)) 'ellipsis) + (t nil))))) + +(defun htmlize-add-before-after-strings (beg end text) + ;; Find overlays specifying before-string and after-string in [beg, + ;; pos). If any are found, splice them into TEXT and return the new + ;; text. + (let (additions) + (dolist (overlay (overlays-in beg end)) + (let ((before (overlay-get overlay 'before-string)) + (after (overlay-get overlay 'after-string))) + (when after + (push (cons (- (overlay-end overlay) beg) + after) + additions)) + (when before + (push (cons (- (overlay-start overlay) beg) + before) + additions)))) + (if additions + (let ((textlist nil) + (strpos 0)) + (dolist (add (stable-sort additions #'< :key #'car)) + (let ((addpos (car add)) + (addtext (cdr add))) + (push (substring text strpos addpos) textlist) + (push addtext textlist) + (setq strpos addpos))) + (push (substring text strpos) textlist) + (apply #'concat (nreverse textlist))) + text))) + +(defun htmlize-copy-prop (prop beg end string) + ;; Copy the specified property from the specified region of the + ;; buffer to the target string. We cannot rely on Emacs to copy the + ;; property because we want to handle properties coming from both + ;; text properties and overlays. + (let ((pos beg)) + (while (< pos end) + (let ((value (get-char-property pos prop)) + (next-change (htmlize-next-change pos prop end))) + (when value + (put-text-property (- pos beg) (- next-change beg) + prop value string)) + (setq pos next-change))))) + +(defun htmlize-get-text-with-display (beg end) + ;; Like buffer-substring-no-properties, except it copies the + ;; `display' property from the buffer, if found. + (let ((text (buffer-substring-no-properties beg end))) + (htmlize-copy-prop 'display beg end text) + (htmlize-copy-prop 'htmlize-link beg end text) + (unless htmlize-running-xemacs + (setq text (htmlize-add-before-after-strings beg end text))) + text)) + +(defun htmlize-buffer-substring-no-invisible (beg end) + ;; Like buffer-substring-no-properties, but don't copy invisible + ;; parts of the region. Where buffer-substring-no-properties + ;; mandates an ellipsis to be shown, htmlize-ellipsis is inserted. + (let ((pos beg) + visible-list invisible show last-show next-change) + ;; Iterate over the changes in the `invisible' property and filter + ;; out the portions where it's non-nil, i.e. where the text is + ;; invisible. + (while (< pos end) + (setq invisible (get-char-property pos 'invisible) + next-change (htmlize-next-change pos 'invisible end) + show (htmlize-decode-invisibility-spec invisible)) + (cond ((eq show t) + (push (htmlize-get-text-with-display pos next-change) + visible-list)) + ((and (eq show 'ellipsis) + (not (eq last-show 'ellipsis)) + ;; Conflate successive ellipses. + (push htmlize-ellipsis visible-list)))) + (setq pos next-change last-show show)) + (htmlize-concat (nreverse visible-list)))) + +(defun htmlize-trim-ellipsis (text) + ;; Remove htmlize-ellipses ("...") from the beginning of TEXT if it + ;; starts with it. It checks for the special property of the + ;; ellipsis so it doesn't work on ordinary text that begins with + ;; "...". + (if (get-text-property 0 'htmlize-ellipsis text) + (substring text (length htmlize-ellipsis)) + text)) + +(defconst htmlize-tab-spaces + ;; A table of strings with spaces. (aref htmlize-tab-spaces 5) is + ;; like (make-string 5 ?\ ), except it doesn't cons. + (let ((v (make-vector 32 nil))) + (dotimes (i (length v)) + (setf (aref v i) (make-string i ?\ ))) + v)) + +(defun htmlize-untabify (text start-column) + "Untabify TEXT, assuming it starts at START-COLUMN." + (let ((column start-column) + (last-match 0) + (chunk-start 0) + chunks match-pos tab-size) + (while (string-match "[\t\n]" text last-match) + (setq match-pos (match-beginning 0)) + (cond ((eq (aref text match-pos) ?\t) + ;; Encountered a tab: create a chunk of text followed by + ;; the expanded tab. + (push (substring text chunk-start match-pos) chunks) + ;; Increase COLUMN by the length of the text we've + ;; skipped since last tab or newline. (Encountering + ;; newline resets it.) + (incf column (- match-pos last-match)) + ;; Calculate tab size based on tab-width and COLUMN. + (setq tab-size (- tab-width (% column tab-width))) + ;; Expand the tab, carefully recreating the `display' + ;; property if one was on the TAB. + (let ((display (get-text-property match-pos 'display text)) + (expanded-tab (aref htmlize-tab-spaces tab-size))) + (when display + (put-text-property 0 tab-size 'display display expanded-tab)) + (push expanded-tab chunks)) + (incf column tab-size) + (setq chunk-start (1+ match-pos))) + (t + ;; Reset COLUMN at beginning of line. + (setq column 0))) + (setq last-match (1+ match-pos))) + ;; If no chunks have been allocated, it means there have been no + ;; tabs to expand. Return TEXT unmodified. + (if (null chunks) + text + (when (< chunk-start (length text)) + ;; Push the remaining chunk. + (push (substring text chunk-start) chunks)) + ;; Generate the output from the available chunks. + (htmlize-concat (nreverse chunks))))) + +(defun htmlize-extract-text (beg end trailing-ellipsis) + ;; Extract buffer text, sans the invisible parts. Then + ;; untabify it and escape the HTML metacharacters. + (let ((text (htmlize-buffer-substring-no-invisible beg end))) + (when trailing-ellipsis + (setq text (htmlize-trim-ellipsis text))) + ;; If TEXT ends up empty, don't change trailing-ellipsis. + (when (> (length text) 0) + (setq trailing-ellipsis + (get-text-property (1- (length text)) + 'htmlize-ellipsis text))) + (setq text (htmlize-untabify text (current-column))) + (setq text (htmlize-string-to-html text)) + (values text trailing-ellipsis))) + +(defun htmlize-despam-address (string) + "Replace every occurrence of '@' in STRING with %40. +This is used to protect mailto links without modifying their meaning." + ;; Suggested by Ville Skytta. + (while (string-match "@" string) + (setq string (replace-match "%40" nil t string))) + string) + +(defun htmlize-make-tmp-overlay (beg end props) + (let ((overlay (htmlize-make-overlay beg end))) + (htmlize-overlay-put overlay 'htmlize-tmp-overlay t) + (while props + (htmlize-overlay-put overlay (pop props) (pop props))) + overlay)) + +(defun htmlize-delete-tmp-overlays () + (dolist (overlay (htmlize-overlays-in (point-min) (point-max))) + (when (htmlize-overlay-get overlay 'htmlize-tmp-overlay) + (htmlize-delete-overlay overlay)))) + +(defun htmlize-make-link-overlay (beg end uri) + (htmlize-make-tmp-overlay beg end `(htmlize-link (:uri ,uri)))) + +(defun htmlize-create-auto-links () + "Add `htmlize-link' property to all mailto links in the buffer." + (save-excursion + (goto-char (point-min)) + (while (re-search-forward + "<\\(\\(mailto:\\)?\\([-=+_.a-zA-Z0-9]+@[-_.a-zA-Z0-9]+\\)\\)>" + nil t) + (let* ((address (match-string 3)) + (beg (match-beginning 0)) (end (match-end 0)) + (uri (concat "mailto:" (htmlize-despam-address address)))) + (htmlize-make-link-overlay beg end uri))) + (goto-char (point-min)) + (while (re-search-forward "<\\(\\(URL:\\)?\\([a-zA-Z]+://[^;]+\\)\\)>" + nil t) + (htmlize-make-link-overlay + (match-beginning 0) (match-end 0) (match-string 3))))) + +;; Tests for htmlize-create-auto-links: + +;; <mailto:hniksic@xemacs.org> +;; <http://fly.srk.fer.hr> +;; <URL:http://www.xemacs.org> +;; <http://www.mail-archive.com/bbdb-info@xemacs.org/> +;; <hniksic@xemacs.org> +;; <xalan-dev-sc.10148567319.hacuhiucknfgmpfnjcpg-john=doe.com@xml.apache.org> + +(defun htmlize-shadow-form-feeds () + (let ((s "\n<hr />")) + (put-text-property 0 (length s) 'htmlize-literal t s) + (let ((disp `(display ,s))) + (while (re-search-forward "\n\^L" nil t) + (htmlize-make-tmp-overlay (match-beginning 0) (match-end 0) disp))))) + +(defun htmlize-defang-local-variables () + ;; Juri Linkov reports that an HTML-ized "Local variables" can lead + ;; visiting the HTML to fail with "Local variables list is not + ;; properly terminated". He suggested changing the phrase to + ;; syntactically equivalent HTML that Emacs doesn't recognize. + (goto-char (point-min)) + (while (search-forward "Local Variables:" nil t) + (replace-match "Local Variables&#58;" nil t))) + +
+;;; Color handling. + +(defvar htmlize-x-library-search-path + `(,data-directory + "/etc/X11/rgb.txt" + "/usr/share/X11/rgb.txt" + ;; the remainder of this list really belongs in a museum + "/usr/X11R6/lib/X11/" + "/usr/X11R5/lib/X11/" + "/usr/lib/X11R6/X11/" + "/usr/lib/X11R5/X11/" + "/usr/local/X11R6/lib/X11/" + "/usr/local/X11R5/lib/X11/" + "/usr/local/lib/X11R6/X11/" + "/usr/local/lib/X11R5/X11/" + "/usr/X11/lib/X11/" + "/usr/lib/X11/" + "/usr/local/lib/X11/" + "/usr/X386/lib/X11/" + "/usr/x386/lib/X11/" + "/usr/XFree86/lib/X11/" + "/usr/unsupported/lib/X11/" + "/usr/athena/lib/X11/" + "/usr/local/x11r5/lib/X11/" + "/usr/lpp/Xamples/lib/X11/" + "/usr/openwin/lib/X11/" + "/usr/openwin/share/lib/X11/")) + +(defun htmlize-get-color-rgb-hash (&optional rgb-file) + "Return a hash table mapping X color names to RGB values. +The keys in the hash table are X11 color names, and the values are the +#rrggbb RGB specifications, extracted from `rgb.txt'. + +If RGB-FILE is nil, the function will try hard to find a suitable file +in the system directories. + +If no rgb.txt file is found, return nil." + (let ((rgb-file (or rgb-file (locate-file + "rgb.txt" + htmlize-x-library-search-path))) + (hash nil)) + (when rgb-file + (with-temp-buffer + (insert-file-contents rgb-file) + (setq hash (make-hash-table :test 'equal)) + (while (not (eobp)) + (cond ((looking-at "^\\s-*\\([!#]\\|$\\)") + ;; Skip comments and empty lines. + ) + ((looking-at + "[ \t]*\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\(.*\\)") + (setf (gethash (downcase (match-string 4)) hash) + (format "#%02x%02x%02x" + (string-to-number (match-string 1)) + (string-to-number (match-string 2)) + (string-to-number (match-string 3))))) + (t + (error + "Unrecognized line in %s: %s" + rgb-file + (buffer-substring (point) (progn (end-of-line) (point)))))) + (forward-line 1)))) + hash)) + +;; Compile the RGB map when loaded. On systems where rgb.txt is +;; missing, the value of the variable will be nil, and rgb.txt will +;; not be used. +(defvar htmlize-color-rgb-hash (htmlize-get-color-rgb-hash)) +
+;;; Face handling. + +(defun htmlize-face-specifies-property (face prop) + ;; Return t if face specifies PROP, as opposed to it being inherited + ;; from the default face. The problem with e.g. + ;; `face-foreground-instance' is that it returns an instance for + ;; EVERY face because every face inherits from the default face. + ;; However, we'd like htmlize-face-{fore,back}ground to return nil + ;; when called with a face that doesn't specify its own foreground + ;; or background. + (or (eq face 'default) + (assq 'global (specifier-spec-list (face-property face prop))))) + +(defun htmlize-face-color-internal (face fg) + ;; Used only under GNU Emacs. Return the color of FACE, but don't + ;; return "unspecified-fg" or "unspecified-bg". If the face is + ;; `default' and the color is unspecified, look up the color in + ;; frame parameters. + (let* ((function (if fg #'face-foreground #'face-background)) + color) + (if (>= emacs-major-version 22) + ;; For GNU Emacs 22+ set INHERIT to get the inherited values. + (setq color (funcall function face nil t)) + (setq color (funcall function face)) + ;; For GNU Emacs 21 (which has `face-attribute'): if the color + ;; is nil, recursively check for the face's parent. + (when (and (null color) + (fboundp 'face-attribute) + (face-attribute face :inherit) + (not (eq (face-attribute face :inherit) 'unspecified))) + (setq color (htmlize-face-color-internal + (face-attribute face :inherit) fg)))) + (when (and (eq face 'default) (null color)) + (setq color (cdr (assq (if fg 'foreground-color 'background-color) + (frame-parameters))))) + (when (or (eq color 'unspecified) + (equal color "unspecified-fg") + (equal color "unspecified-bg")) + (setq color nil)) + (when (and (eq face 'default) + (null color)) + ;; Assuming black on white doesn't seem right, but I can't think + ;; of anything better to do. + (setq color (if fg "black" "white"))) + color)) + +(defun htmlize-face-foreground (face) + ;; Return the name of the foreground color of FACE. If FACE does + ;; not specify a foreground color, return nil. + (cond (htmlize-running-xemacs + ;; XEmacs. + (and (htmlize-face-specifies-property face 'foreground) + (color-instance-name (face-foreground-instance face)))) + (t + ;; GNU Emacs. + (htmlize-face-color-internal face t)))) + +(defun htmlize-face-background (face) + ;; Return the name of the background color of FACE. If FACE does + ;; not specify a background color, return nil. + (cond (htmlize-running-xemacs + ;; XEmacs. + (and (htmlize-face-specifies-property face 'background) + (color-instance-name (face-background-instance face)))) + (t + ;; GNU Emacs. + (htmlize-face-color-internal face nil)))) + +;; Convert COLOR to the #RRGGBB string. If COLOR is already in that +;; format, it's left unchanged. + +(defun htmlize-color-to-rgb (color) + (let ((rgb-string nil)) + (cond ((null color) + ;; Ignore nil COLOR because it means that the face is not + ;; specifying any color. Hence (htmlize-color-to-rgb nil) + ;; returns nil. + ) + ((string-match "\\`#" color) + ;; The color is already in #rrggbb format. + (setq rgb-string color)) + ((and htmlize-use-rgb-txt + htmlize-color-rgb-hash) + ;; Use of rgb.txt is requested, and it's available on the + ;; system. Use it. + (setq rgb-string (gethash (downcase color) htmlize-color-rgb-hash))) + (t + ;; We're getting the RGB components from Emacs. + (let ((rgb + (if (fboundp 'color-instance-rgb-components) + (mapcar (lambda (arg) + (/ arg 256)) + (color-instance-rgb-components + (make-color-instance color))) + (mapcar (lambda (arg) + (/ arg 256)) + (color-values color))))) + (when rgb + (setq rgb-string (apply #'format "#%02x%02x%02x" rgb)))))) + ;; If RGB-STRING is still nil, it means the color cannot be found, + ;; for whatever reason. In that case just punt and return COLOR. + ;; Most browsers support a decent set of color names anyway. + (or rgb-string color))) + +;; We store the face properties we care about into an +;; `htmlize-fstruct' type. That way we only have to analyze face +;; properties, which can be time consuming, once per each face. The +;; mapping between Emacs faces and htmlize-fstructs is established by +;; htmlize-make-face-map. The name "fstruct" refers to variables of +;; type `htmlize-fstruct', while the term "face" is reserved for Emacs +;; faces. + +(defstruct htmlize-fstruct + foreground ; foreground color, #rrggbb + background ; background color, #rrggbb + size ; size + boldp ; whether face is bold + italicp ; whether face is italic + underlinep ; whether face is underlined + overlinep ; whether face is overlined + strikep ; whether face is struck through + css-name ; CSS name of face + ) + +(defun htmlize-face-emacs21-attr (fstruct attr value) + ;; For ATTR and VALUE, set the equivalent value in FSTRUCT. + (case attr + (:foreground + (setf (htmlize-fstruct-foreground fstruct) (htmlize-color-to-rgb value))) + (:background + (setf (htmlize-fstruct-background fstruct) (htmlize-color-to-rgb value))) + (:height + (setf (htmlize-fstruct-size fstruct) value)) + (:weight + (when (string-match (symbol-name value) "bold") + (setf (htmlize-fstruct-boldp fstruct) t))) + (:slant + (setf (htmlize-fstruct-italicp fstruct) (or (eq value 'italic) + (eq value 'oblique)))) + (:bold + (setf (htmlize-fstruct-boldp fstruct) value)) + (:italic + (setf (htmlize-fstruct-italicp fstruct) value)) + (:underline + (setf (htmlize-fstruct-underlinep fstruct) value)) + (:overline + (setf (htmlize-fstruct-overlinep fstruct) value)) + (:strike-through + (setf (htmlize-fstruct-strikep fstruct) value)))) + +(defun htmlize-face-size (face) + ;; The size (height) of FACE, taking inheritance into account. + ;; Only works in Emacs 21 and later. + (let ((size-list + (loop + for f = face then (face-attribute f :inherit) + until (or (not f) (eq f 'unspecified)) + for h = (face-attribute f :height) + collect (if (eq h 'unspecified) nil h)))) + (reduce 'htmlize-merge-size (cons nil size-list)))) + +(defun htmlize-face-css-name (face) + ;; Generate the css-name property for the given face. Emacs places + ;; no restrictions on the names of symbols that represent faces -- + ;; any characters may be in the name, even control chars. We try + ;; hard to beat the face name into shape, both esthetically and + ;; according to CSS1 specs. + (let ((name (downcase (symbol-name face)))) + (when (string-match "\\`font-lock-" name) + ;; font-lock-FOO-face -> FOO. + (setq name (replace-match "" t t name))) + (when (string-match "-face\\'" name) + ;; Drop the redundant "-face" suffix. + (setq name (replace-match "" t t name))) + (while (string-match "[^-a-zA-Z0-9]" name) + ;; Drop the non-alphanumerics. + (setq name (replace-match "X" t t name))) + (when (string-match "\\`[-0-9]" name) + ;; CSS identifiers may not start with a digit. + (setq name (concat "X" name))) + ;; After these transformations, the face could come out empty. + (when (equal name "") + (setq name "face")) + ;; Apply the prefix. + (concat htmlize-css-name-prefix name))) + +(defun htmlize-face-to-fstruct (face) + "Convert Emacs face FACE to fstruct." + (let ((fstruct (make-htmlize-fstruct + :foreground (htmlize-color-to-rgb + (htmlize-face-foreground face)) + :background (htmlize-color-to-rgb + (htmlize-face-background face))))) + (if htmlize-running-xemacs + ;; XEmacs doesn't provide a way to detect whether a face is + ;; bold or italic, so we need to examine the font instance. + (let* ((font-instance (face-font-instance face)) + (props (font-instance-properties font-instance))) + (when (equalp (cdr (assq 'WEIGHT_NAME props)) "bold") + (setf (htmlize-fstruct-boldp fstruct) t)) + (when (or (equalp (cdr (assq 'SLANT props)) "i") + (equalp (cdr (assq 'SLANT props)) "o")) + (setf (htmlize-fstruct-italicp fstruct) t)) + (setf (htmlize-fstruct-strikep fstruct) + (face-strikethru-p face)) + (setf (htmlize-fstruct-underlinep fstruct) + (face-underline-p face))) + ;; GNU Emacs + (dolist (attr '(:weight :slant :underline :overline :strike-through)) + (let ((value (if (>= emacs-major-version 22) + ;; Use the INHERIT arg in GNU Emacs 22. + (face-attribute face attr nil t) + ;; Otherwise, fake it. + (let ((face face)) + (while (and (eq (face-attribute face attr) + 'unspecified) + (not (eq (face-attribute face :inherit) + 'unspecified))) + (setq face (face-attribute face :inherit))) + (face-attribute face attr))))) + (when (and value (not (eq value 'unspecified))) + (htmlize-face-emacs21-attr fstruct attr value)))) + (let ((size (htmlize-face-size face))) + (unless (eql size 1.0) ; ignore non-spec + (setf (htmlize-fstruct-size fstruct) size)))) + (setf (htmlize-fstruct-css-name fstruct) (htmlize-face-css-name face)) + fstruct)) + +(defmacro htmlize-copy-attr-if-set (attr-list dest source) + ;; Generate code with the following pattern: + ;; (progn + ;; (when (htmlize-fstruct-ATTR source) + ;; (setf (htmlize-fstruct-ATTR dest) (htmlize-fstruct-ATTR source))) + ;; ...) + ;; for the given list of boolean attributes. + (cons 'progn + (loop for attr in attr-list + for attr-sym = (intern (format "htmlize-fstruct-%s" attr)) + collect `(when (,attr-sym ,source) + (setf (,attr-sym ,dest) (,attr-sym ,source)))))) + +(defun htmlize-merge-size (merged next) + ;; Calculate the size of the merge of MERGED and NEXT. + (cond ((null merged) next) + ((integerp next) next) + ((null next) merged) + ((floatp merged) (* merged next)) + ((integerp merged) (round (* merged next))))) + +(defun htmlize-merge-two-faces (merged next) + (htmlize-copy-attr-if-set + (foreground background boldp italicp underlinep overlinep strikep) + merged next) + (setf (htmlize-fstruct-size merged) + (htmlize-merge-size (htmlize-fstruct-size merged) + (htmlize-fstruct-size next))) + merged) + +(defun htmlize-merge-faces (fstruct-list) + (cond ((null fstruct-list) + ;; Nothing to do, return a dummy face. + (make-htmlize-fstruct)) + ((null (cdr fstruct-list)) + ;; Optimize for the common case of a single face, simply + ;; return it. + (car fstruct-list)) + (t + (reduce #'htmlize-merge-two-faces + (cons (make-htmlize-fstruct) fstruct-list))))) + +;; GNU Emacs 20+ supports attribute lists in `face' properties. For +;; example, you can use `(:foreground "red" :weight bold)' as an +;; overlay's "face", or you can even use a list of such lists, etc. +;; We call those "attrlists". +;; +;; htmlize supports attrlist by converting them to fstructs, the same +;; as with regular faces. + +(defun htmlize-attrlist-to-fstruct (attrlist) + ;; Like htmlize-face-to-fstruct, but accepts an ATTRLIST as input. + (let ((fstruct (make-htmlize-fstruct))) + (cond ((eq (car attrlist) 'foreground-color) + ;; ATTRLIST is (foreground-color . COLOR) + (setf (htmlize-fstruct-foreground fstruct) + (htmlize-color-to-rgb (cdr attrlist)))) + ((eq (car attrlist) 'background-color) + ;; ATTRLIST is (background-color . COLOR) + (setf (htmlize-fstruct-background fstruct) + (htmlize-color-to-rgb (cdr attrlist)))) + (t + ;; ATTRLIST is a plist. + (while attrlist + (let ((attr (pop attrlist)) + (value (pop attrlist))) + (when (and value (not (eq value 'unspecified))) + (htmlize-face-emacs21-attr fstruct attr value)))))) + (setf (htmlize-fstruct-css-name fstruct) "ATTRLIST") + fstruct)) + +(defun htmlize-decode-face-prop (prop) + "Turn face property PROP into a list of face-like objects." + ;; PROP can be a symbol naming a face, a string naming such a + ;; symbol, a cons (foreground-color . COLOR) or (background-color + ;; COLOR), a property list (:attr1 val1 :attr2 val2 ...), or a list + ;; of any of those. + ;; + ;; (htmlize-decode-face-prop 'face) -> (face) + ;; (htmlize-decode-face-prop '(face1 face2)) -> (face1 face2) + ;; (htmlize-decode-face-prop '(:attr "val")) -> ((:attr "val")) + ;; (htmlize-decode-face-prop '((:attr "val") face (foreground-color "red"))) + ;; -> ((:attr "val") face (foreground-color "red")) + ;; + ;; Unrecognized atoms or non-face symbols/strings are silently + ;; stripped away. + (cond ((null prop) + nil) + ((symbolp prop) + (and (facep prop) + (list prop))) + ((stringp prop) + (and (facep (intern-soft prop)) + (list prop))) + ((atom prop) + nil) + ((and (symbolp (car prop)) + (eq ?: (aref (symbol-name (car prop)) 0))) + (list prop)) + ((or (eq (car prop) 'foreground-color) + (eq (car prop) 'background-color)) + (list prop)) + (t + (apply #'nconc (mapcar #'htmlize-decode-face-prop prop))))) + +(defun htmlize-make-face-map (faces) + ;; Return a hash table mapping Emacs faces to htmlize's fstructs. + ;; The keys are either face symbols or attrlists, so the test + ;; function must be `equal'. + (let ((face-map (make-hash-table :test 'equal)) + css-names) + (dolist (face faces) + (unless (gethash face face-map) + ;; Haven't seen FACE yet; convert it to an fstruct and cache + ;; it. + (let ((fstruct (if (symbolp face) + (htmlize-face-to-fstruct face) + (htmlize-attrlist-to-fstruct face)))) + (setf (gethash face face-map) fstruct) + (let* ((css-name (htmlize-fstruct-css-name fstruct)) + (new-name css-name) + (i 0)) + ;; Uniquify the face's css-name by using NAME-1, NAME-2, + ;; etc. + (while (member new-name css-names) + (setq new-name (format "%s-%s" css-name (incf i)))) + (unless (equal new-name css-name) + (setf (htmlize-fstruct-css-name fstruct) new-name)) + (push new-name css-names))))) + face-map)) + +(defun htmlize-unstringify-face (face) + "If FACE is a string, return it interned, otherwise return it unchanged." + (if (stringp face) + (intern face) + face)) + +(defun htmlize-faces-in-buffer () + "Return a list of faces used in the current buffer. +Under XEmacs, this returns the set of faces specified by the extents +with the `face' property. (This covers text properties as well.) Under +GNU Emacs, it returns the set of faces specified by the `face' text +property and by buffer overlays that specify `face'." + (let (faces) + ;; Testing for (fboundp 'map-extents) doesn't work because W3 + ;; defines `map-extents' under FSF. + (if htmlize-running-xemacs + (let (face-prop) + (map-extents (lambda (extent ignored) + (setq face-prop (extent-face extent) + ;; FACE-PROP can be a face or a list of + ;; faces. + faces (if (listp face-prop) + (union face-prop faces) + (adjoin face-prop faces))) + nil) + nil + ;; Specify endpoints explicitly to respect + ;; narrowing. + (point-min) (point-max) nil nil 'face)) + ;; FSF Emacs code. + ;; Faces used by text properties. + (let ((pos (point-min)) face-prop next) + (while (< pos (point-max)) + (setq face-prop (get-text-property pos 'face) + next (or (next-single-property-change pos 'face) (point-max))) + (setq faces (nunion (htmlize-decode-face-prop face-prop) + faces :test 'equal)) + (setq pos next))) + ;; Faces used by overlays. + (dolist (overlay (overlays-in (point-min) (point-max))) + (let ((face-prop (overlay-get overlay 'face))) + (setq faces (nunion (htmlize-decode-face-prop face-prop) + faces :test 'equal))))) + faces)) + +;; htmlize-faces-at-point returns the faces in use at point. The +;; faces are sorted by increasing priority, i.e. the last face takes +;; precedence. +;; +;; Under XEmacs, this returns all the faces in all the extents at +;; point. Under GNU Emacs, this returns all the faces in the `face' +;; property and all the faces in the overlays at point. + +(cond (htmlize-running-xemacs + (defun htmlize-faces-at-point () + (let (extent extent-list face-list face-prop) + (while (setq extent (extent-at (point) nil 'face extent)) + (push extent extent-list)) + ;; extent-list is in reverse display order, meaning that + ;; smallest ones come last. That is the order we want, + ;; except it can be overridden by the `priority' property. + (setq extent-list (stable-sort extent-list #'< + :key #'extent-priority)) + (dolist (extent extent-list) + (setq face-prop (extent-face extent)) + ;; extent's face-list is in reverse order from what we + ;; want, but the `nreverse' below will take care of it. + (setq face-list (if (listp face-prop) + (append face-prop face-list) + (cons face-prop face-list)))) + (nreverse face-list)))) + (t + (defun htmlize-faces-at-point () + (let (all-faces) + ;; Faces from text properties. + (let ((face-prop (get-text-property (point) 'face))) + ;; we need to reverse the `face' prop because we want + ;; more specific faces to come later + (setq all-faces (nreverse (htmlize-decode-face-prop face-prop)))) + ;; Faces from overlays. + (let ((overlays + ;; Collect overlays at point that specify `face'. + (delete-if-not (lambda (o) + (overlay-get o 'face)) + (overlays-at (point)))) + list face-prop) + ;; Sort the overlays so the smaller (more specific) ones + ;; come later. The number of overlays at each one + ;; position should be very small, so the sort shouldn't + ;; slow things down. + (setq overlays (sort* overlays + ;; Sort by ascending... + #'< + ;; ...overlay size. + :key (lambda (o) + (- (overlay-end o) + (overlay-start o))))) + ;; Overlay priorities, if present, override the above + ;; established order. Larger overlay priority takes + ;; precedence and therefore comes later in the list. + (setq overlays (stable-sort + overlays + ;; Reorder (stably) by acending... + #'< + ;; ...overlay priority. + :key (lambda (o) + (or (overlay-get o 'priority) 0)))) + (dolist (overlay overlays) + (setq face-prop (overlay-get overlay 'face) + list (nconc (htmlize-decode-face-prop face-prop) list))) + ;; Under "Merging Faces" the manual explicitly states + ;; that faces specified by overlays take precedence over + ;; faces specified by text properties. + (setq all-faces (nconc all-faces list))) + all-faces)))) +
+;; htmlize supports generating HTML in several flavors, some of which +;; use CSS, and others the <font> element. We take an OO approach and +;; define "methods" that indirect to the functions that depend on +;; `htmlize-output-type'. The currently used methods are `doctype', +;; `insert-head', `body-tag', and `text-markup'. Not all output types +;; define all methods. +;; +;; Methods are called either with (htmlize-method METHOD ARGS...) +;; special form, or by accessing the function with +;; (htmlize-method-function 'METHOD) and calling (funcall FUNCTION). +;; The latter form is useful in tight loops because `htmlize-method' +;; conses. + +(defmacro htmlize-method (method &rest args) + ;; Expand to (htmlize-TYPE-METHOD ...ARGS...). TYPE is the value of + ;; `htmlize-output-type' at run time. + `(funcall (htmlize-method-function ',method) ,@args)) + +(defun htmlize-method-function (method) + ;; Return METHOD's function definition for the current output type. + ;; The returned object can be safely funcalled. + (let ((sym (intern (format "htmlize-%s-%s" htmlize-output-type method)))) + (indirect-function (if (fboundp sym) + sym + (let ((default (intern (concat "htmlize-default-" + (symbol-name method))))) + (if (fboundp default) + default + 'ignore)))))) + +(defvar htmlize-memoization-table (make-hash-table :test 'equal)) + +(defmacro htmlize-memoize (key generator) + "Return the value of GENERATOR, memoized as KEY. +That means that GENERATOR will be evaluated and returned the first time +it's called with the same value of KEY. All other times, the cached +\(memoized) value will be returned." + (let ((value (gensym))) + `(let ((,value (gethash ,key htmlize-memoization-table))) + (unless ,value + (setq ,value ,generator) + (setf (gethash ,key htmlize-memoization-table) ,value)) + ,value))) +
+;;; Default methods. + +(defun htmlize-default-doctype () + nil ; no doc-string + ;; Note that the `font' output is technically invalid under this DTD + ;; because the DTD doesn't allow embedding <font> in <pre>. + "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">" + ) + +(defun htmlize-default-body-tag (face-map) + nil ; no doc-string + face-map ; shut up the byte-compiler + "<body>") +
+;;; CSS based output support. + +;; Internal function; not a method. +(defun htmlize-css-specs (fstruct) + (let (result) + (when (htmlize-fstruct-foreground fstruct) + (push (format "color: %s;" (htmlize-fstruct-foreground fstruct)) + result)) + (when (htmlize-fstruct-background fstruct) + (push (format "background-color: %s;" + (htmlize-fstruct-background fstruct)) + result)) + (let ((size (htmlize-fstruct-size fstruct))) + (when (and size (not (eq htmlize-ignore-face-size t))) + (cond ((floatp size) + (push (format "font-size: %d%%;" (* 100 size)) result)) + ((not (eq htmlize-ignore-face-size 'absolute)) + (push (format "font-size: %spt;" (/ size 10.0)) result))))) + (when (htmlize-fstruct-boldp fstruct) + (push "font-weight: bold;" result)) + (when (htmlize-fstruct-italicp fstruct) + (push "font-style: italic;" result)) + (when (htmlize-fstruct-underlinep fstruct) + (push "text-decoration: underline;" result)) + (when (htmlize-fstruct-overlinep fstruct) + (push "text-decoration: overline;" result)) + (when (htmlize-fstruct-strikep fstruct) + (push "text-decoration: line-through;" result)) + (nreverse result))) + +(defun htmlize-css-insert-head (buffer-faces face-map) + (insert " <style type=\"text/css\">\n <!--\n") + (insert " body {\n " + (mapconcat #'identity + (htmlize-css-specs (gethash 'default face-map)) + "\n ") + "\n }\n") + (dolist (face (sort* (copy-list buffer-faces) #'string-lessp + :key (lambda (f) + (htmlize-fstruct-css-name (gethash f face-map))))) + (let* ((fstruct (gethash face face-map)) + (cleaned-up-face-name + (let ((s + ;; Use `prin1-to-string' rather than `symbol-name' + ;; to get the face name because the "face" can also + ;; be an attrlist, which is not a symbol. + (prin1-to-string face))) + ;; If the name contains `--' or `*/', remove them. + (while (string-match "--" s) + (setq s (replace-match "-" t t s))) + (while (string-match "\\*/" s) + (setq s (replace-match "XX" t t s))) + s)) + (specs (htmlize-css-specs fstruct))) + (insert " ." (htmlize-fstruct-css-name fstruct)) + (if (null specs) + (insert " {") + (insert " {\n /* " cleaned-up-face-name " */\n " + (mapconcat #'identity specs "\n "))) + (insert "\n }\n"))) + (insert htmlize-hyperlink-style + " -->\n </style>\n")) + +(defun htmlize-css-text-markup (fstruct-list buffer) + ;; Open the markup needed to insert text colored with FACES into + ;; BUFFER. Return the function that closes the markup. + + ;; In CSS mode, this is easy: just nest the text in one <span + ;; class=...> tag for each face in FSTRUCT-LIST. + (dolist (fstruct fstruct-list) + (princ "<span class=\"" buffer) + (princ (htmlize-fstruct-css-name fstruct) buffer) + (princ "\">" buffer)) + (htmlize-lexlet ((fstruct-list fstruct-list) (buffer buffer)) + (lambda () + (dolist (fstruct fstruct-list) + (ignore fstruct) ; shut up the byte-compiler + (princ "</span>" buffer))))) +
+;; `inline-css' output support. + +(defun htmlize-inline-css-body-tag (face-map) + (format "<body style=\"%s\">" + (mapconcat #'identity (htmlize-css-specs (gethash 'default face-map)) + " "))) + +(defun htmlize-inline-css-text-markup (fstruct-list buffer) + (let* ((merged (htmlize-merge-faces fstruct-list)) + (style (htmlize-memoize + merged + (let ((specs (htmlize-css-specs merged))) + (and specs + (mapconcat #'identity (htmlize-css-specs merged) " ")))))) + (when style + (princ "<span style=\"" buffer) + (princ style buffer) + (princ "\">" buffer)) + (htmlize-lexlet ((style style) (buffer buffer)) + (lambda () + (when style + (princ "</span>" buffer)))))) +
+;;; `font' tag based output support. + +(defun htmlize-font-body-tag (face-map) + (let ((fstruct (gethash 'default face-map))) + (format "<body text=\"%s\" bgcolor=\"%s\">" + (htmlize-fstruct-foreground fstruct) + (htmlize-fstruct-background fstruct)))) + +(defun htmlize-font-text-markup (fstruct-list buffer) + ;; In `font' mode, we use the traditional HTML means of altering + ;; presentation: <font> tag for colors, <b> for bold, <u> for + ;; underline, and <strike> for strike-through. + (let* ((merged (htmlize-merge-faces fstruct-list)) + (markup (htmlize-memoize + merged + (cons (concat + (and (htmlize-fstruct-foreground merged) + (format "<font color=\"%s\">" (htmlize-fstruct-foreground merged))) + (and (htmlize-fstruct-boldp merged) "<b>") + (and (htmlize-fstruct-italicp merged) "<i>") + (and (htmlize-fstruct-underlinep merged) "<u>") + (and (htmlize-fstruct-strikep merged) "<strike>")) + (concat + (and (htmlize-fstruct-strikep merged) "</strike>") + (and (htmlize-fstruct-underlinep merged) "</u>") + (and (htmlize-fstruct-italicp merged) "</i>") + (and (htmlize-fstruct-boldp merged) "</b>") + (and (htmlize-fstruct-foreground merged) "</font>")))))) + (princ (car markup) buffer) + (htmlize-lexlet ((markup markup) (buffer buffer)) + (lambda () + (princ (cdr markup) buffer))))) +
+(defun htmlize-buffer-1 () + ;; Internal function; don't call it from outside this file. Htmlize + ;; current buffer, writing the resulting HTML to a new buffer, and + ;; return it. Unlike htmlize-buffer, this doesn't change current + ;; buffer or use switch-to-buffer. + (save-excursion + ;; Protect against the hook changing the current buffer. + (save-excursion + (run-hooks 'htmlize-before-hook)) + ;; Convince font-lock support modes to fontify the entire buffer + ;; in advance. + (htmlize-ensure-fontified) + (clrhash htmlize-extended-character-cache) + (clrhash htmlize-memoization-table) + ;; It's important that the new buffer inherits default-directory + ;; from the current buffer. + (let ((htmlbuf (generate-new-buffer (if (buffer-file-name) + (htmlize-make-file-name + (file-name-nondirectory + (buffer-file-name))) + "*html*"))) + (completed nil)) + (unwind-protect + (let* ((buffer-faces (htmlize-faces-in-buffer)) + (face-map (htmlize-make-face-map (adjoin 'default buffer-faces))) + (places (gensym)) + (title (if (buffer-file-name) + (file-name-nondirectory (buffer-file-name)) + (buffer-name)))) + (when htmlize-generate-hyperlinks + (htmlize-create-auto-links)) + (when htmlize-replace-form-feeds + (htmlize-shadow-form-feeds)) + + ;; Initialize HTMLBUF and insert the HTML prolog. + (with-current-buffer htmlbuf + (buffer-disable-undo) + (insert (htmlize-method doctype) ?\n + (format "<!-- Created by htmlize-%s in %s mode. -->\n" + htmlize-version htmlize-output-type) + "<html>\n ") + (put places 'head-start (point-marker)) + (insert "<head>\n" + " <title>" (htmlize-protect-string title) "</title>\n" + (if htmlize-html-charset + (format (concat " <meta http-equiv=\"Content-Type\" " + "content=\"text/html; charset=%s\">\n") + htmlize-html-charset) + "") + htmlize-head-tags) + (htmlize-method insert-head buffer-faces face-map) + (insert " </head>") + (put places 'head-end (point-marker)) + (insert "\n ") + (put places 'body-start (point-marker)) + (insert (htmlize-method body-tag face-map) + "\n ") + (put places 'content-start (point-marker)) + (insert "<pre>\n")) + (let ((text-markup + ;; Get the inserter method, so we can funcall it inside + ;; the loop. Not calling `htmlize-method' in the loop + ;; body yields a measurable speed increase. + (htmlize-method-function 'text-markup)) + ;; Declare variables used in loop body outside the loop + ;; because it's faster to establish `let' bindings only + ;; once. + next-change text face-list trailing-ellipsis + fstruct-list last-fstruct-list + (close-markup (lambda ()))) + ;; This loop traverses and reads the source buffer, appending + ;; the resulting HTML to HTMLBUF. This method is fast + ;; because: 1) it doesn't require examining the text + ;; properties char by char (htmlize-next-face-change is used + ;; to move between runs with the same face), and 2) it doesn't + ;; require frequent buffer switches, which are slow because + ;; they rebind all buffer-local vars. + (goto-char (point-min)) + (while (not (eobp)) + (setq next-change (htmlize-next-face-change (point))) + ;; Get faces in use between (point) and NEXT-CHANGE, and + ;; convert them to fstructs. + (setq face-list (htmlize-faces-at-point) + fstruct-list (delq nil (mapcar (lambda (f) + (gethash f face-map)) + face-list))) + (multiple-value-setq (text trailing-ellipsis) + (htmlize-extract-text (point) next-change trailing-ellipsis)) + ;; Don't bother writing anything if there's no text (this + ;; happens in invisible regions). + (when (> (length text) 0) + ;; Open the new markup if necessary and insert the text. + (when (not (equalp fstruct-list last-fstruct-list)) + (funcall close-markup) + (setq last-fstruct-list fstruct-list + close-markup (funcall text-markup fstruct-list htmlbuf))) + (princ text htmlbuf)) + (goto-char next-change)) + + ;; We've gone through the buffer; close the markup from + ;; the last run, if any. + (funcall close-markup)) + + ;; Insert the epilog and post-process the buffer. + (with-current-buffer htmlbuf + (insert "</pre>") + (put places 'content-end (point-marker)) + (insert "\n </body>") + (put places 'body-end (point-marker)) + (insert "\n</html>\n") + (htmlize-defang-local-variables) + (goto-char (point-min)) + (when htmlize-html-major-mode + ;; What sucks about this is that the minor modes, most notably + ;; font-lock-mode, won't be initialized. Oh well. + (funcall htmlize-html-major-mode)) + (set (make-local-variable 'htmlize-buffer-places) + (symbol-plist places)) + (run-hooks 'htmlize-after-hook) + (buffer-enable-undo)) + (setq completed t) + htmlbuf) + + (when (not completed) + (kill-buffer htmlbuf)) + (htmlize-delete-tmp-overlays))))) + +;; Utility functions. + +(defmacro htmlize-with-fontify-message (&rest body) + ;; When forcing fontification of large buffers in + ;; htmlize-ensure-fontified, inform the user that he is waiting for + ;; font-lock, not for htmlize to finish. + `(progn + (if (> (buffer-size) 65536) + (message "Forcing fontification of %s..." + (buffer-name (current-buffer)))) + ,@body + (if (> (buffer-size) 65536) + (message "Forcing fontification of %s...done" + (buffer-name (current-buffer)))))) + +(defun htmlize-ensure-fontified () + ;; If font-lock is being used, ensure that the "support" modes + ;; actually fontify the buffer. If font-lock is not in use, we + ;; don't care because, except in htmlize-file, we don't force + ;; font-lock on the user. + (when (and (boundp 'font-lock-mode) + font-lock-mode) + ;; In part taken from ps-print-ensure-fontified in GNU Emacs 21. + (cond + ((and (boundp 'jit-lock-mode) + (symbol-value 'jit-lock-mode)) + (htmlize-with-fontify-message + (jit-lock-fontify-now (point-min) (point-max)))) + ((and (boundp 'lazy-lock-mode) + (symbol-value 'lazy-lock-mode)) + (htmlize-with-fontify-message + (lazy-lock-fontify-region (point-min) (point-max)))) + ((and (boundp 'lazy-shot-mode) + (symbol-value 'lazy-shot-mode)) + (htmlize-with-fontify-message + ;; lazy-shot is amazing in that it must *refontify* the region, + ;; even if the whole buffer has already been fontified. <sigh> + (lazy-shot-fontify-region (point-min) (point-max)))) + ;; There's also fast-lock, but we don't need to handle specially, + ;; I think. fast-lock doesn't really defer fontification, it + ;; just saves it to an external cache so it's not done twice. + ))) + +
+;;;###autoload +(defun htmlize-buffer (&optional buffer) + "Convert BUFFER to HTML, preserving colors and decorations. + +The generated HTML is available in a new buffer, which is returned. +When invoked interactively, the new buffer is selected in the current +window. The title of the generated document will be set to the buffer's +file name or, if that's not available, to the buffer's name. + +Note that htmlize doesn't fontify your buffers, it only uses the +decorations that are already present. If you don't set up font-lock or +something else to fontify your buffers, the resulting HTML will be +plain. Likewise, if you don't like the choice of colors, fix the mode +that created them, or simply alter the faces it uses." + (interactive) + (let ((htmlbuf (with-current-buffer (or buffer (current-buffer)) + (htmlize-buffer-1)))) + (when (interactive-p) + (switch-to-buffer htmlbuf)) + htmlbuf)) + +;;;###autoload +(defun htmlize-region (beg end) + "Convert the region to HTML, preserving colors and decorations. +See `htmlize-buffer' for details." + (interactive "r") + ;; Don't let zmacs region highlighting end up in HTML. + (when (fboundp 'zmacs-deactivate-region) + (zmacs-deactivate-region)) + (let ((htmlbuf (save-restriction + (narrow-to-region beg end) + (htmlize-buffer-1)))) + (when (interactive-p) + (switch-to-buffer htmlbuf)) + htmlbuf)) + +(defun htmlize-region-for-paste (beg end) + "Htmlize the region and return just the HTML as a string. +This forces the `inline-css' style and only returns the HTML body, +but without the BODY tag. This should make it useful for inserting +the text to another HTML buffer." + (let* ((htmlize-output-type 'inline-css) + (htmlbuf (htmlize-region beg end))) + (unwind-protect + (with-current-buffer htmlbuf + (buffer-substring (plist-get htmlize-buffer-places 'content-start) + (plist-get htmlize-buffer-places 'content-end))) + (kill-buffer htmlbuf)))) + +(defun htmlize-make-file-name (file) + "Make an HTML file name from FILE. + +In its default implementation, this simply appends `.html' to FILE. +This function is called by htmlize to create the buffer file name, and +by `htmlize-file' to create the target file name. + +More elaborate transformations are conceivable, such as changing FILE's +extension to `.html' (\"file.c\" -> \"file.html\"). If you want them, +overload this function to do it and htmlize will comply." + (concat file ".html")) + +;; Older implementation of htmlize-make-file-name that changes FILE's +;; extension to ".html". +;(defun htmlize-make-file-name (file) +; (let ((extension (file-name-extension file)) +; (sans-extension (file-name-sans-extension file))) +; (if (or (equal extension "html") +; (equal extension "htm") +; (equal sans-extension "")) +; (concat file ".html") +; (concat sans-extension ".html")))) + +;;;###autoload +(defun htmlize-file (file &optional target) + "Load FILE, fontify it, convert it to HTML, and save the result. + +Contents of FILE are inserted into a temporary buffer, whose major mode +is set with `normal-mode' as appropriate for the file type. The buffer +is subsequently fontified with `font-lock' and converted to HTML. Note +that, unlike `htmlize-buffer', this function explicitly turns on +font-lock. If a form of highlighting other than font-lock is desired, +please use `htmlize-buffer' directly on buffers so highlighted. + +Buffers currently visiting FILE are unaffected by this function. The +function does not change current buffer or move the point. + +If TARGET is specified and names a directory, the resulting file will be +saved there instead of to FILE's directory. If TARGET is specified and +does not name a directory, it will be used as output file name." + (interactive (list (read-file-name + "HTML-ize file: " + nil nil nil (and (buffer-file-name) + (file-name-nondirectory + (buffer-file-name)))))) + (let ((output-file (if (and target (not (file-directory-p target))) + target + (expand-file-name + (htmlize-make-file-name (file-name-nondirectory file)) + (or target (file-name-directory file))))) + ;; Try to prevent `find-file-noselect' from triggering + ;; font-lock because we'll fontify explicitly below. + (font-lock-mode nil) + (font-lock-auto-fontify nil) + (global-font-lock-mode nil) + ;; Ignore the size limit for the purposes of htmlization. + (font-lock-maximum-size nil) + ;; Disable font-lock support modes. This will only work in + ;; more recent Emacs versions, so htmlize-buffer-1 still needs + ;; to call htmlize-ensure-fontified. + (font-lock-support-mode nil)) + (with-temp-buffer + ;; Insert FILE into the temporary buffer. + (insert-file-contents file) + ;; Set the file name so normal-mode and htmlize-buffer-1 pick it + ;; up. Restore it afterwards so with-temp-buffer's kill-buffer + ;; doesn't complain about killing a modified buffer. + (let ((buffer-file-name file)) + ;; Set the major mode for the sake of font-lock. + (normal-mode) + (font-lock-mode 1) + (unless font-lock-mode + ;; In GNU Emacs (font-lock-mode 1) doesn't force font-lock, + ;; contrary to the documentation. This seems to work. + (font-lock-fontify-buffer)) + ;; htmlize the buffer and save the HTML. + (with-current-buffer (htmlize-buffer-1) + (unwind-protect + (progn + (run-hooks 'htmlize-file-hook) + (write-region (point-min) (point-max) output-file)) + (kill-buffer (current-buffer))))))) + ;; I haven't decided on a useful return value yet, so just return + ;; nil. + nil) + +;;;###autoload +(defun htmlize-many-files (files &optional target-directory) + "Convert FILES to HTML and save the corresponding HTML versions. + +FILES should be a list of file names to convert. This function calls +`htmlize-file' on each file; see that function for details. When +invoked interactively, you are prompted for a list of files to convert, +terminated with RET. + +If TARGET-DIRECTORY is specified, the HTML files will be saved to that +directory. Normally, each HTML file is saved to the directory of the +corresponding source file." + (interactive + (list + (let (list file) + ;; Use empty string as DEFAULT because setting DEFAULT to nil + ;; defaults to the directory name, which is not what we want. + (while (not (equal (setq file (read-file-name + "HTML-ize file (RET to finish): " + (and list (file-name-directory + (car list))) + "" t)) + "")) + (push file list)) + (nreverse list)))) + ;; Verify that TARGET-DIRECTORY is indeed a directory. If it's a + ;; file, htmlize-file will use it as target, and that doesn't make + ;; sense. + (and target-directory + (not (file-directory-p target-directory)) + (error "target-directory must name a directory: %s" target-directory)) + (dolist (file files) + (htmlize-file file target-directory))) + +;;;###autoload +(defun htmlize-many-files-dired (arg &optional target-directory) + "HTMLize dired-marked files." + (interactive "P") + (htmlize-many-files (dired-get-marked-files nil arg) target-directory)) + +(provide 'htmlize) + +;; Local Variables: +;; byte-compile-warnings: (not cl-functions lexical unresolved obsolete) +;; lexical-binding: t +;; End: + +;;; htmlize.el ends here +
+ + diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..aa0bfb8 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,11 @@ +*.o +*.c +*.f90 +*.h +*.fh +*.html +*~ +*.so +Makefile.generated +test_qmckl +merged_qmckl.org diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..8d9ff83 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,63 @@ +COMPILER=GNU +#COMPILER=INTEL +#COMPILER=LLVM + +ifeq ($(COMPILER),GNU) +CC=gcc -g +CFLAGS=-fPIC -fexceptions -Wall -Werror -Wpedantic -Wextra + +FC=gfortran -g +FFLAGS=-fPIC -fcheck=all -Waliasing -Wampersand -Wconversion -Wsurprising -Wintrinsics-std -Wno-tabs -Wintrinsic-shadow -Wline-truncation -Wreal-q-constant -Wuninitialized -fbacktrace -ffpe-trap=zero,overflow,underflow -finit-real=nan + +LIBS=-lgfortran -lm +endif + +ifeq ($(COMPILER),INTEL) +CC=icc -xHost +CFLAGS=-fPIC -g -O2 + +FC=ifort -xHost +FFLAGS=-fPIC -g -O2 + +LIBS=-lm -lifcore -lirc +endif + +#TODO +ifeq ($(COMPILER),LLVM) +CC=clang +CFLAGS=-fPIC -g -O2 + +FC=flang +FFLAGS=fPIC -g -O2 + +LIBS=-lm +endif + + +QMCKL_ROOT=$(shell dirname $(CURDIR)) + +export CC CFLAGS FC FFLAGS LIBS QMCKL_ROOT + +ORG_SOURCE_FILES=$(wildcard *.org) +OBJECT_FILES=$(filter-out $(EXCLUDED_OBJECTS), $(patsubst %.org,%.o,$(ORG_SOURCE_FILES))) + +.PHONY: clean +.SECONDARY: # Needed to keep the produced C and Fortran files + +libqmckl.so: Makefile.generated + $(MAKE) -f Makefile.generated + +test: Makefile.generated + $(MAKE) -f Makefile.generated test + + +doc: $(ORG_SOURCE_FILES) + $(QMCKL_ROOT)/tools/create_doc.sh + + +clean: + rm -f qmckl.h test_qmckl_* test_qmckl.c test_qmckl qmckl_*.f90 qmckl_*.c qmckl_*.o qmckl_*.h Makefile.generated libqmckl.so *.html *.fh *.mod + +Makefile.generated: Makefile $(QMCKL_ROOT)/tools/create_makefile.sh $(ORG_SOURCE_FILES) + $(QMCKL_ROOT)/tools/create_makefile.sh + diff --git a/src/README.org b/src/README.org new file mode 100644 index 0000000..a2c2c9e --- /dev/null +++ b/src/README.org @@ -0,0 +1,246 @@ +#+TITLE: QMCkl source code documentation +#+EXPORT_FILE_NAME: index.html +#+PROPERTY: comments org + +#+SETUPFILE: https://fniessen.github.io/org-html-themes/org/theme-readtheorg.setup + + +* Introduction + + + The ultimate goal of the QMCkl library is to provide a high-performance + implementation of the main kernels of QMC. In this particular + implementation of the library, we focus on the definition of the API and the tests, and + on a /pedagogical/ presentation of the algorithms. We expect the + HPC experts to use this repository as a reference for re-writing + optimized libraries. + + +** Literate programming + + In a traditional source code, most of the lines of source files of a program + are code, scripts, Makefiles, and only a few lines are comments explaining + parts of the code that are non-trivial to understand. The documentation of + the prorgam is usually written in a separate directory, and is often outdated + compared to the code. + + Literate programming is a different approach to programming, + where the program is considered as a publishable-quality document. Most of + the lines of the source files are text, mathematical formulas, tables, + figures, /etc/, and the lines of code are just the translation in a computer + language of the ideas and algorithms expressed in the text. More importantly, + the "document" is structured like a text document with sections, subsections, + a bibliography, a table of contents /etc/, and the place where pieces of code + appear are the places where they should belong for the reader to understand + the logic of the program, not the places where the compiler expects to find + them. Both the publishable-quality document and the binary executable are + produced from the same source files. + + Literate programming is particularly well adapted in this context, as the + central part of this project is the documentation of an API. The + implementation of the algorithms is just an expression of the algorithms in a + language that can be compiled, so that the correctness of the algorithms can + be tested. + + We have chosen to write the source files in [[https://karl-voit.at/2017/09/23/orgmode-as-markup-only/][org-mode]] format, + as any text editor can be used to edit org-mode files. To + produce the documentation, there exists multiple possibilities to convert + org-mode files into different formats such as HTML or PDF. The source code is + easily extracted from the org-mode files invoking the Emacs text editor from + the command-line in the =Makefile=, and then the produced files are compiled. + Moreover, within the Emacs text editor the source code blocks can be executed + interactively, in the same spirit as Jupyter notebooks. + + +** Source code editing + + For a tutorial on literate programming with org-mode, follow [[http://www.howardism.org/Technical/Emacs/literate-programming-tutorial.html][this link]]. + + Any text editor can be used to edit org-mode files. For a better + user experience Emacs is recommended. For users hating Emacs, it + is good to know that Emacs can behave like Vim when switched into + ``Evil'' mode. + + In the =tools/init.el= file, we provide a minimal Emacs configuration + file for vim users. This file should be copied into =.emacs.d/init.el=. + + For users with a preference for Jupyter notebooks, we also provide the + =tools/nb_to_org.sh= script can convert jupyter notebooks into org-mode + files. + + Note that pandoc can be used to convert multiple markdown formats into + org-mode. + + +** Choice of the programming language + + Most of the codes of the TREX CoE are written in Fortran with some scripts in + Bash and Python. Outside of the CoE, Fortran is also important (Casino, Amolqc), + and other important languages used by the community are C and C++ (QMCPack, + QWalk), and Julia is gaining in popularity. The library we design should be + compatible with all of these languages. The QMCkl API has to be compatible + with the C language since libraries with a C-compatible API can be used in + every other language. + + High-performance versions of the QMCkl, with the same API, will be rewritten by + the experts in HPC. These optimized libraries will be tuned for specific + architectures, among which we can cite x86 based processors, and GPU + accelerators. Nowadays, the most efficient software tools to take advantage of + low-level features of the processor (intrinsics) and of GPUs are for C++ + developers. It is highly probable that the optimized implementations will be + written in C++, and this is agreement with our choice to make the API + C-compatible. + + Fortran is one of the most common languages used by the community, and is simple + enough to make the algorithms readable both by experts in QMC, and experts in + HPC. Hence we propose in this pedagogical implementation of QMCkl to use Fortran + to express the QMC algorithms. As the main languages of the library is C, this + implies that the exposed C functions call the Fortran routine. However, for + internal functions related to system programming, the C language is more natural + than Fortran. + + The Fortran source files should provide a C interface using the + ~iso_c_binding~ module. The name of the Fortran source files should end with + =_f.f90= to be properly handled by the =Makefile=. The names of the functions + defined in Fortran should be the same as those exposed in the API suffixed by + =_f=. Fortran interfaces should also be written in the =qmckl_f.f90= file. + + For more guidelines on using Fortran to generate a C interface, see + [[http://fortranwiki.org/fortran/show/Generating+C+Interfaces][this link]]. + + +# Coding style +# # TODO: decide on a coding style + +# To improve readability, we maintain a consistent coding style in +# the library. + +# - For C source files, we will use __(decide on a coding style)__ +# - For Fortran source files, we will use __(decide on a coding +# style)__ + +# Coding style can be automatically checked with [[https://clang.llvm.org/docs/ClangFormat.html][clang-format]]. + +** Design of the library + + The proposed API should allow the library to: deal with memory transfers + between CPU and accelerators, and to use different levels of floating-point + precision. We chose a multi-layered design with low-level and high-level + functions (see below). + +*** Naming conventions + + To avoid namespace collisions, we use =qmckl_= as a prefix for all exported + functions and variables. All exported header files should have a file name + prefixed with =qmckl_=. + + If the name of the org-mode file is =xxx.org=, the name of the + produced C files should be =xxx.c= and =xxx.h= and the name of the + produced Fortran file should be =xxx.f90=. + + Arrays are in uppercase and scalars are in lowercase. + + In the names of the variables and functions, only the singular + form is allowed. + +*** Application programming interface + + In the C language, the number of bits used by the integer types can change + from one architecture to another one. To circumvent this problem, we choose to + use the integer types defined in ~~ where the number of bits used for + the integers are fixed. + + To ensure that the library will be easily usable in /any/ other language + than C, we restrict the data types in the interfaces to the following: + - 32-bit and 64-bit integers, scalars and and arrays (~int32_t~ and ~int64_t~) + - 32-bit and 64-bit floats, scalars and and arrays (~float~ and ~double~) + - Pointers are always casted into 64-bit integers, even on legacy 32-bit architectures + - ASCII strings are represented as a pointers to character arrays + and terminated by a ~'\0'~ character (C convention). + - Complex numbers can be represented by an array of 2 floats. + - Boolean variables are stored as integers, ~1~ for ~true~ and ~0~ for ~false~ + - Floating point variables should be by default + - ~double~ unless explicitly mentioned + - integers used for counting should always be ~int64_t~ + + To facilitate the use in other languages than C, we will provide some + bindings in other languages in other repositories. + + # TODO : Link to repositories for bindings + # To facilitate the use in other languages than C, we provide some + # bindings in other languages in other repositories. + +*** Global state + + Global variables should be avoided in the library, because it is + possible that one single program needs to use multiple instances + of the library. To solve this problem we propose to use a pointer + to a =context= variable, built by the library with the + =qmckl_context_create= function. The =context= contains the global + state of the library, and is used as the first argument of many + QMCkl functions. + + The internal structure of the context is not specified, to give a + maximum of freedom to the different implementations. Modifying + the state is done by setters and getters, prefixed by + =qmckl_context_set_= an =qmckl_context_get_=. When a context + variable is modified by a setter, a copy of the old data structure + is made and updated, and the pointer to the new data structure is + returned, such that the old contexts can still be accessed. It is + also possible to modify the state in an impure fashion, using the + =qmckl_context_update_= functions. The context and its old + versions can be destroyed with =qmckl_context_destroy=. + +*** Low-level functions + + Low-level functions are very simple functions which are leaves of + the function call tree (they don't call any other QMCkl function). + + These functions are /pure/, and unaware of the QMCkl + =context=. They are not allowed to allocate/deallocate memory, and + if they need temporary memory it should be provided in input. + +*** High-level functions + + High-level functions are at the top of the function call tree. + They are able to choose which lower-level function to call + depending on the required precision, and do the corresponding type + conversions. These functions are also responsible for allocating + temporary storage, to simplify the use of accelerators. + + The high-level functions should be pure, unless the introduction + of non-purity is justified. All the side effects should be made in + the =context= variable. + + # TODO : We need an identifier for impure functions + +*** Numerical precision + + The number of bits of precision required for a function should be + given as an input of low-level computational functions. This input + will be used to define the values of the different thresholds that + might be used to avoid computing unnecessary noise. High-level + functions will use the precision specified in the =context= + variable. + +** Algorithms + + Reducing the scaling of an algorithm usually implies also reducing + its arithmetic complexity (number of flops per byte). Therefore, + for small sizes \(\mathcal{O}(N^3)\) and \(\mathcal{O}(N^2)\) + algorithms are better adapted than linear scaling algorithms. As + QMCkl is a general purpose library, multiple algorithms should be + implemented adapted to different problem sizes. + +** Rules for the API + + - =stdint= should be used for integers (=int32_t=, =int64_t=) + - integers used for counting should always be =int64_t= + - floats should be by default =double=, unless explicitly mentioned + - pointers are converted to =int64_t= to increase portability + +* Documentation + + # The .org files will be appended here in the order specified in the + # table_of_contents file + diff --git a/src/qmckl.org b/src/qmckl.org new file mode 100644 index 0000000..985f848 --- /dev/null +++ b/src/qmckl.org @@ -0,0 +1,36 @@ + +** =qmckl.h= header file + + The =qmckl.h= header file has to be included in <<>> codes when + QMCkl functions are used: + #+BEGIN_SRC C :tangle none + #include "qmckl.h" + #+END_SRC f90 + + + In <<>> programs, the =qmckl_f.f90= interface file should be + included in the source code using the library, and the Fortran codes + should use the ~qmckl~ module as + #+BEGIN_SRC f90 :tangle none + use qmckl + #+END_SRC f90 + +*** Top of header files :noexport: + + #+BEGIN_SRC C :tangle qmckl.h :noweb yes +#ifndef QMCKL_H +#define QMCKL_H +#include +#include + +<> + + #+END_SRC + + #+BEGIN_SRC f90 :tangle qmckl_f.f90 +module qmckl + use, intrinsic :: iso_c_binding + #+END_SRC + + The bottoms of the files are located in the [[qmckl_footer.org]] file. + diff --git a/src/qmckl_ao.org b/src/qmckl_ao.org new file mode 100644 index 0000000..b0f9671 --- /dev/null +++ b/src/qmckl_ao.org @@ -0,0 +1,751 @@ +** Atomic Orbitals + :PROPERTIES: + :f: qmckl_ao.f90 + :c_test: test_qmckl_ao.c + :fh: qmckl_f.f90 + :h: qmckl.h + :f_test: test_qmckl_ao_f.f90 + :END: + + + This files contains all the routines for the computation of the + values, gradients and Laplacian of the atomic basis functions. + + 3 files are produced: + - a source file : =qmckl_ao.f90= + - a C test file : =test_qmckl_ao.c= + - a Fortran test file : =test_qmckl_ao_f.f90= + +*** Test :noexport: + #+BEGIN_SRC C :tangle (org-entry-get nil "c_test" t) +#include "qmckl.h" +#include "munit.h" +MunitResult test_qmckl_ao() { + qmckl_context context; + context = qmckl_context_create(); + #+END_SRC + +*** Polynomials + + \[ + P_l(\mathbf{r},\mathbf{R}_i) = (x-X_i)^a (y-Y_i)^b (z-Z_i)^c + \] + + \begin{eqnarray*} + \frac{\partial }{\partial x} P_l\left(\mathbf{r},\mathbf{R}_i \right) & + = & a (x-X_i)^{a-1} (y-Y_i)^b (z-Z_i)^c \\ + \frac{\partial }{\partial y} P_l\left(\mathbf{r},\mathbf{R}_i \right) & + = & b (x-X_i)^a (y-Y_i)^{b-1} (z-Z_i)^c \\ + \frac{\partial }{\partial z} P_l\left(\mathbf{r},\mathbf{R}_i \right) & + = & c (x-X_i)^a (y-Y_i)^b (z-Z_i)^{c-1} \\ + \end{eqnarray*} + + + \begin{eqnarray*} + \left( \frac{\partial }{\partial x^2} + + \frac{\partial }{\partial y^2} + + \frac{\partial }{\partial z^2} \right) P_l + \left(\mathbf{r},\mathbf{R}_i \right) & = & + a(a-1) (x-X_i)^{a-2} (y-Y_i)^b (z-Z_i)^c + \\ + && b(b-1) (x-X_i)^a (y-Y_i)^{b-1} (z-Z_i)^c + \\ + && c(c-1) (x-X_i)^a (y-Y_i)^b (z-Z_i)^{c-1} + \end{eqnarray*} + +**** ~qmckl_ao_power~ + + Computes all the powers of the ~n~ input data up to the given + maximum value given in input for each of the $n$ points: + + \[ P_{ij} = X_j^i \] + +***** Arguments + + | ~context~ | input | Global state | + | ~n~ | input | Number of values | + | ~X(n)~ | input | Array containing the input values | + | ~LMAX(n)~ | input | Array containing the maximum power for each value | + | ~P(LDP,n)~ | output | Array containing all the powers of ~X~ | + | ~LDP~ | input | Leading dimension of array ~P~ | + +***** Requirements + + - ~context~ is not 0 + - ~n~ > 0 + - ~X~ is allocated with at least $n \times 8$ bytes + - ~LMAX~ is allocated with at least $n \times 4$ bytes + - ~P~ is allocated with at least $n \times \max_i \text{LMAX}_i \times 8$ bytes + - ~LDP~ >= $\max_i$ ~LMAX[i]~ + +***** Header + #+BEGIN_SRC C :tangle (org-entry-get nil "h" t) +qmckl_exit_code qmckl_ao_power(const qmckl_context context, + const int64_t n, + const double *X, const int32_t *LMAX, + const double *P, const int64_t LDP); + #+END_SRC + +***** Source + #+BEGIN_SRC f90 :tangle (org-entry-get nil "f" t) +integer function qmckl_ao_power_f(context, n, X, LMAX, P, ldp) result(info) + implicit none + integer*8 , intent(in) :: context + integer*8 , intent(in) :: n + real*8 , intent(in) :: X(n) + integer , intent(in) :: LMAX(n) + real*8 , intent(out) :: P(ldp,n) + integer*8 , intent(in) :: ldp + + integer*8 :: i,j + + info = 0 + + if (context == 0_8) then + info = -1 + return + endif + + if (LDP < MAXVAL(LMAX)) then + info = -2 + return + endif + + do j=1,n + P(1,j) = X(j) + do i=2,LMAX(j) + P(i,j) = P(i-1,j) * X(j) + end do + end do + +end function qmckl_ao_power_f + #+END_SRC + +***** C interface :noexport: + #+BEGIN_SRC f90 :tangle (org-entry-get nil "f" t) +integer(c_int32_t) function qmckl_ao_power(context, n, X, LMAX, P, ldp) & + bind(C) result(info) + use, intrinsic :: iso_c_binding + implicit none + integer (c_int64_t) , intent(in) , value :: context + integer (c_int64_t) , intent(in) , value :: n + real (c_double) , intent(in) :: X(n) + integer (c_int32_t) , intent(in) :: LMAX(n) + real (c_double) , intent(out) :: P(ldp,n) + integer (c_int64_t) , intent(in) , value :: ldp + + integer, external :: qmckl_ao_power_f + info = qmckl_ao_power_f(context, n, X, LMAX, P, ldp) +end function qmckl_ao_power + #+END_SRC + + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer(c_int32_t) function qmckl_ao_power(context, n, X, LMAX, P, ldp) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t) , intent(in) , value :: context + integer (c_int64_t) , intent(in) , value :: n + integer (c_int64_t) , intent(in) , value :: ldp + real (c_double) , intent(in) :: X(n) + integer (c_int32_t) , intent(in) :: LMAX(n) + real (c_double) , intent(out) :: P(ldp,n) + end function qmckl_ao_power + end interface + #+END_SRC + +***** Test :noexport: + #+BEGIN_SRC f90 :tangle (org-entry-get nil "f_test" t) +integer(c_int32_t) function test_qmckl_ao_power(context) bind(C) + use qmckl + implicit none + + integer(c_int64_t), intent(in), value :: context + + integer*8 :: n, LDP + integer, allocatable :: LMAX(:) + double precision, allocatable :: X(:), P(:,:) + integer*8 :: i,j + double precision :: epsilon + + epsilon = qmckl_context_get_epsilon(context) + + n = 100; + LDP = 10; + + allocate(X(n), P(LDP,n), LMAX(n)) + + do j=1,n + X(j) = -5.d0 + 0.1d0 * dble(j) + LMAX(j) = 1 + int(mod(j, 5),4) + end do + + test_qmckl_ao_power = qmckl_ao_power(context, n, X, LMAX, P, LDP) + if (test_qmckl_ao_power /= 0) return + + test_qmckl_ao_power = -1 + + do j=1,n + do i=1,LMAX(j) + if ( X(j)**i == 0.d0 ) then + if ( P(i,j) /= 0.d0) return + else + if ( dabs(1.d0 - P(i,j) / (X(j)**i)) > epsilon ) return + end if + end do + end do + + test_qmckl_ao_power = 0 + deallocate(X,P,LMAX) +end function test_qmckl_ao_power + #+END_SRC + + #+BEGIN_SRC C :tangle (org-entry-get nil "c_test" t) +int test_qmckl_ao_power(qmckl_context context); +munit_assert_int(0, ==, test_qmckl_ao_power(context)); + #+END_SRC + + +**** ~qmckl_ao_polynomial_vgl~ + + Computes the values, gradients and Laplacians at a given point of + all polynomials with an angular momentum up to ~lmax~. + +***** Arguments + + | ~context~ | input | Global state | + | ~X(3)~ | input | Array containing the coordinates of the points | + | ~R(3)~ | input | Array containing the x,y,z coordinates of the center | + | ~lmax~ | input | Maximum angular momentum | + | ~n~ | output | Number of computed polynomials | + | ~L(ldl,n)~ | output | Contains a,b,c for all ~n~ results | + | ~ldl~ | input | Leading dimension of ~L~ | + | ~VGL(ldv,n)~ | output | Value, gradients and Laplacian of the polynomials | + | ~ldv~ | input | Leading dimension of array ~VGL~ | + +***** Requirements + + - ~context~ is not 0 + - ~n~ > 0 + - ~lmax~ >= 0 + - ~ldl~ >= 3 + - ~ldv~ >= 5 + - ~X~ is allocated with at least $3 \times 8$ bytes + - ~R~ is allocated with at least $3 \times 8$ bytes + - ~n~ >= ~(lmax+1)(lmax+2)(lmax+3)/6~ + - ~L~ is allocated with at least $3 \times n \times 4$ bytes + - ~VGL~ is allocated with at least $5 \times n \times 8$ bytes + - On output, ~n~ should be equal to ~(lmax+1)(lmax+2)(lmax+3)/6~ + - On output, the powers are given in the following order (l=a+b+c): + - Increase values of ~l~ + - Within a given value of ~l~, alphabetical order of the + string made by a*"x" + b*"y" + c*"z" (in Python notation). + For example, with a=0, b=2 and c=1 the string is "yyz" + +***** Error codes + + | -1 | Null context | + | -2 | Inconsistent ~ldl~ | + | -3 | Inconsistent ~ldv~ | + | -4 | Inconsistent ~lmax~ | + +***** Header + #+BEGIN_SRC C :tangle (org-entry-get nil "h" t) +qmckl_exit_code qmckl_ao_polynomial_vgl(const qmckl_context context, + const double *X, const double *R, + const int32_t lmax, const int64_t *n, + const int32_t *L, const int64_t ldl, + const double *VGL, const int64_t ldv); + #+END_SRC + +***** Source + #+BEGIN_SRC f90 :tangle (org-entry-get nil "f" t) +integer function qmckl_ao_polynomial_vgl_f(context, X, R, lmax, n, L, ldl, VGL, ldv) result(info) + implicit none + integer*8 , intent(in) :: context + real*8 , intent(in) :: X(3), R(3) + integer , intent(in) :: lmax + integer*8 , intent(out) :: n + integer , intent(out) :: L(ldl,(lmax+1)*(lmax+2)*(lmax+3)/6) + integer*8 , intent(in) :: ldl + real*8 , intent(out) :: VGL(ldv,(lmax+1)*(lmax+2)*(lmax+3)/6) + integer*8 , intent(in) :: ldv + + integer*8 :: i,j + integer :: a,b,c,d + real*8 :: Y(3) + integer :: lmax_array(3) + real*8 :: pows(-2:lmax,3) + integer, external :: qmckl_ao_power_f + double precision :: xy, yz, xz + double precision :: da, db, dc, dd + + info = 0 + + if (context == 0_8) then + info = -1 + return + endif + + if (ldl < 3) then + info = -2 + return + endif + + if (ldv < 5) then + info = -3 + return + endif + + if (lmax <= 0) then + info = -4 + return + endif + + + do i=1,3 + Y(i) = X(i) - R(i) + end do + + lmax_array(1:3) = lmax + if (lmax == 0) then + VGL(1,1) = 1.d0 + vgL(2:5,1) = 0.d0 + l(1:3,1) = 0 + n=1 + else if (lmax > 0) then + pows(-2:0,1:3) = 1.d0 + do i=1,lmax + pows(i,1) = pows(i-1,1) * Y(1) + pows(i,2) = pows(i-1,2) * Y(2) + pows(i,3) = pows(i-1,3) * Y(3) + end do + + VGL(1:5,1:4) = 0.d0 + l(1:3,1:4) = 0 + + VGL(1,1) = 1.d0 + vgl(1:5,2:4) = 0.d0 + + l(1,2) = 1 + vgl(1,2) = pows(1,1) + vgL(2,2) = 1.d0 + + l(2,3) = 1 + vgl(1,3) = pows(1,2) + vgL(3,3) = 1.d0 + + l(3,4) = 1 + vgl(1,4) = pows(1,3) + vgL(4,4) = 1.d0 + + n=4 + endif + + ! l>=2 + dd = 2.d0 + do d=2,lmax + da = dd + do a=d,0,-1 + db = dd-da + do b=d-a,0,-1 + c = d - a - b + dc = dd - da - db + n = n+1 + + l(1,n) = a + l(2,n) = b + l(3,n) = c + + xy = pows(a,1) * pows(b,2) + yz = pows(b,2) * pows(c,3) + xz = pows(a,1) * pows(c,3) + + vgl(1,n) = xy * pows(c,3) + + xy = dc * xy + xz = db * xz + yz = da * yz + + vgl(2,n) = pows(a-1,1) * yz + vgl(3,n) = pows(b-1,2) * xz + vgl(4,n) = pows(c-1,3) * xy + + vgl(5,n) = & + (da-1.d0) * pows(a-2,1) * yz + & + (db-1.d0) * pows(b-2,2) * xz + & + (dc-1.d0) * pows(c-2,3) * xy + + db = db - 1.d0 + end do + da = da - 1.d0 + end do + dd = dd + 1.d0 + end do + + info = 0 + +end function qmckl_ao_polynomial_vgl_f + #+END_SRC + +***** C interface :noexport: + #+BEGIN_SRC f90 :tangle (org-entry-get nil "f" t) +integer(c_int32_t) function qmckl_ao_polynomial_vgl(context, X, R, lmax, n, L, ldl, VGL, ldv) & + bind(C) result(info) + use, intrinsic :: iso_c_binding + implicit none + integer (c_int64_t) , intent(in) , value :: context + real (c_double) , intent(in) :: X(3), R(3) + integer (c_int32_t) , intent(in) , value :: lmax + integer (c_int64_t) , intent(out) :: n + integer (c_int32_t) , intent(out) :: L(ldl,(lmax+1)*(lmax+2)*(lmax+3)/6) + integer (c_int64_t) , intent(in) , value :: ldl + real (c_double) , intent(out) :: VGL(ldv,(lmax+1)*(lmax+2)*(lmax+3)/6) + integer (c_int64_t) , intent(in) , value :: ldv + + integer, external :: qmckl_ao_polynomial_vgl_f + info = qmckl_ao_polynomial_vgl_f(context, X, R, lmax, n, L, ldl, VGL, ldv) +end function qmckl_ao_polynomial_vgl + #+END_SRC + +***** Fortran interface :noexport: + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer(c_int32_t) function qmckl_ao_polynomial_vgl(context, X, R, lmax, n, L, ldl, VGL, ldv) & + bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t) , intent(in) , value :: context + integer (c_int32_t) , intent(in) , value :: lmax + integer (c_int64_t) , intent(in) , value :: ldl + integer (c_int64_t) , intent(in) , value :: ldv + real (c_double) , intent(in) :: X(3), R(3) + integer (c_int64_t) , intent(out) :: n + integer (c_int32_t) , intent(out) :: L(ldl,(lmax+1)*(lmax+2)*(lmax+3)/6) + real (c_double) , intent(out) :: VGL(ldv,(lmax+1)*(lmax+2)*(lmax+3)/6) + end function qmckl_ao_polynomial_vgl + end interface + #+END_SRC +***** Test :noexport: + #+BEGIN_SRC f90 :tangle (org-entry-get nil "f_test" t) +integer(c_int32_t) function test_qmckl_ao_polynomial_vgl(context) bind(C) + use qmckl + implicit none + + integer(c_int64_t), intent(in), value :: context + + integer :: lmax, d, i + integer, allocatable :: L(:,:) + integer*8 :: n, ldl, ldv, j + double precision :: X(3), R(3), Y(3) + double precision, allocatable :: VGL(:,:) + double precision :: w + double precision :: epsilon + + epsilon = qmckl_context_get_epsilon(context) + + X = (/ 1.1 , 2.2 , 3.3 /) + R = (/ 0.1 , 1.2 , -2.3 /) + Y(:) = X(:) - R(:) + + lmax = 4; + n = 0; + ldl = 3; + ldv = 100; + + d = (lmax+1)*(lmax+2)*(lmax+3)/6 + + allocate (L(ldl,d), VGL(ldv,d)) + + test_qmckl_ao_polynomial_vgl = & + qmckl_ao_polynomial_vgl(context, X, R, lmax, n, L, ldl, VGL, ldv) + if (test_qmckl_ao_polynomial_vgl /= 0) return + + test_qmckl_ao_polynomial_vgl = -1 + + if (n /= d) return + + do j=1,n + test_qmckl_ao_polynomial_vgl = -11 + do i=1,3 + if (L(i,j) < 0) return + end do + test_qmckl_ao_polynomial_vgl = -12 + if (dabs(1.d0 - VGL(1,j) / (& + Y(1)**L(1,j) * Y(2)**L(2,j) * Y(3)**L(3,j) & + )) > epsilon ) return + + test_qmckl_ao_polynomial_vgl = -13 + if (L(1,j) < 1) then + if (VGL(2,j) /= 0.d0) return + else + if (dabs(1.d0 - VGL(2,j) / (& + L(1,j) * Y(1)**(L(1,j)-1) * Y(2)**L(2,j) * Y(3)**L(3,j) & + )) > epsilon ) return + end if + + test_qmckl_ao_polynomial_vgl = -14 + if (L(2,j) < 1) then + if (VGL(3,j) /= 0.d0) return + else + if (dabs(1.d0 - VGL(3,j) / (& + L(2,j) * Y(1)**L(1,j) * Y(2)**(L(2,j)-1) * Y(3)**L(3,j) & + )) > epsilon ) return + end if + + test_qmckl_ao_polynomial_vgl = -15 + if (L(3,j) < 1) then + if (VGL(4,j) /= 0.d0) return + else + if (dabs(1.d0 - VGL(4,j) / (& + L(3,j) * Y(1)**L(1,j) * Y(2)**L(2,j) * Y(3)**(L(3,j)-1) & + )) > epsilon ) return + end if + + test_qmckl_ao_polynomial_vgl = -16 + w = 0.d0 + if (L(1,j) > 1) then + w = w + L(1,j) * (L(1,j)-1) * Y(1)**(L(1,j)-2) * Y(2)**L(2,j) * Y(3)**L(3,j) + end if + if (L(2,j) > 1) then + w = w + L(2,j) * (L(2,j)-1) * Y(1)**L(1,j) * Y(2)**(L(2,j)-2) * Y(3)**L(3,j) + end if + if (L(3,j) > 1) then + w = w + L(3,j) * (L(3,j)-1) * Y(1)**L(1,j) * Y(2)**L(2,j) * Y(3)**(L(3,j)-2) + end if + if (dabs(1.d0 - VGL(5,j) / w) > epsilon ) return + end do + + test_qmckl_ao_polynomial_vgl = 0 + + deallocate(L,VGL) +end function test_qmckl_ao_polynomial_vgl + #+END_SRC + + #+BEGIN_SRC C :tangle (org-entry-get nil "c_test" t) +int test_qmckl_ao_polynomial_vgl(qmckl_context context); +munit_assert_int(0, ==, test_qmckl_ao_polynomial_vgl(context)); + #+END_SRC + +*** Gaussian basis functions + +**** ~qmckl_ao_gaussian_vgl~ + + Computes the values, gradients and Laplacians at a given point of + ~n~ Gaussian functions centered at the same point: + + \[ v_i = \exp(-a_i |X-R|^2) \] + \[ \nabla_x v_i = -2 a_i (X_x - R_x) v_i \] + \[ \nabla_y v_i = -2 a_i (X_y - R_y) v_i \] + \[ \nabla_z v_i = -2 a_i (X_z - R_z) v_i \] + \[ \Delta v_i = a_i (4 |X-R|^2 a_i - 6) v_i \] + +***** Arguments + + | ~context~ | input | Global state | + | ~X(3)~ | input | Array containing the coordinates of the points | + | ~R(3)~ | input | Array containing the x,y,z coordinates of the center | + | ~n~ | input | Number of computed gaussians | + | ~A(n)~ | input | Exponents of the Gaussians | + | ~VGL(ldv,5)~ | output | Value, gradients and Laplacian of the Gaussians | + | ~ldv~ | input | Leading dimension of array ~VGL~ | + +***** Requirements + + - ~context~ is not 0 + - ~n~ > 0 + - ~ldv~ >= 5 + - ~A(i)~ > 0 for all ~i~ + - ~X~ is allocated with at least $3 \times 8$ bytes + - ~R~ is allocated with at least $3 \times 8$ bytes + - ~A~ is allocated with at least $n \times 8$ bytes + - ~VGL~ is allocated with at least $n \times 5 \times 8$ bytes + +***** Header + #+BEGIN_SRC C :tangle (org-entry-get nil "h" t) +qmckl_exit_code qmckl_ao_gaussian_vgl(const qmckl_context context, + const double *X, const double *R, + const int64_t *n, const int64_t *A, + const double *VGL, const int64_t ldv); + #+END_SRC + +***** Source + #+BEGIN_SRC f90 :tangle (org-entry-get nil "f" t) +integer function qmckl_ao_gaussian_vgl_f(context, X, R, n, A, VGL, ldv) result(info) + implicit none + integer*8 , intent(in) :: context + real*8 , intent(in) :: X(3), R(3) + integer*8 , intent(in) :: n + real*8 , intent(in) :: A(n) + real*8 , intent(out) :: VGL(ldv,5) + integer*8 , intent(in) :: ldv + + integer*8 :: i,j + real*8 :: Y(3), r2, t, u, v + + info = 0 + + if (context == 0_8) then + info = -1 + return + endif + + if (n <= 0) then + info = -2 + return + endif + + if (ldv < n) then + info = -3 + return + endif + + + do i=1,3 + Y(i) = X(i) - R(i) + end do + r2 = Y(1)*Y(1) + Y(2)*Y(2) + Y(3)*Y(3) + + do i=1,n + VGL(i,1) = dexp(-A(i) * r2) + end do + + do i=1,n + VGL(i,5) = A(i) * VGL(i,1) + end do + + t = -2.d0 * ( X(1) - R(1) ) + u = -2.d0 * ( X(2) - R(2) ) + v = -2.d0 * ( X(3) - R(3) ) + + do i=1,n + VGL(i,2) = t * VGL(i,5) + VGL(i,3) = u * VGL(i,5) + VGL(i,4) = v * VGL(i,5) + end do + + t = 4.d0 * r2 + do i=1,n + VGL(i,5) = (t * A(i) - 6.d0) * VGL(i,5) + end do + +end function qmckl_ao_gaussian_vgl_f + #+END_SRC + +***** C interface :noexport: + #+BEGIN_SRC f90 :tangle (org-entry-get nil "f" t) +integer(c_int32_t) function qmckl_ao_gaussian_vgl(context, X, R, n, A, VGL, ldv) & + bind(C) result(info) + use, intrinsic :: iso_c_binding + implicit none + integer (c_int64_t) , intent(in) , value :: context + real (c_double) , intent(in) :: X(3), R(3) + integer (c_int64_t) , intent(in) , value :: n + real (c_double) , intent(in) :: A(n) + real (c_double) , intent(out) :: VGL(ldv,5) + integer (c_int64_t) , intent(in) , value :: ldv + + integer, external :: qmckl_ao_gaussian_vgl_f + info = qmckl_ao_gaussian_vgl_f(context, X, R, n, A, VGL, ldv) +end function qmckl_ao_gaussian_vgl + #+END_SRC + + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer(c_int32_t) function qmckl_ao_gaussian_vgl(context, X, R, n, A, VGL, ldv) & + bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t) , intent(in) , value :: context + integer (c_int64_t) , intent(in) , value :: ldv + integer (c_int64_t) , intent(in) , value :: n + real (c_double) , intent(in) :: X(3), R(3), A(n) + real (c_double) , intent(out) :: VGL(ldv,5) + end function qmckl_ao_gaussian_vgl + end interface + #+END_SRC +***** Test :noexport: + #+BEGIN_SRC f90 :tangle (org-entry-get nil "f_test" t) +integer(c_int32_t) function test_qmckl_ao_gaussian_vgl(context) bind(C) + use qmckl + implicit none + + integer(c_int64_t), intent(in), value :: context + + integer*8 :: n, ldv, j, i + double precision :: X(3), R(3), Y(3), r2 + double precision, allocatable :: VGL(:,:), A(:) + double precision :: epsilon + + epsilon = qmckl_context_get_epsilon(context) + + X = (/ 1.1 , 2.2 , 3.3 /) + R = (/ 0.1 , 1.2 , -2.3 /) + Y(:) = X(:) - R(:) + r2 = Y(1)**2 + Y(2)**2 + Y(3)**2 + + n = 10; + ldv = 100; + + allocate (A(n), VGL(ldv,5)) + do i=1,n + A(i) = 0.0013 * dble(ishft(1,i)) + end do + + + test_qmckl_ao_gaussian_vgl = & + qmckl_ao_gaussian_vgl(context, X, R, n, A, VGL, ldv) + if (test_qmckl_ao_gaussian_vgl /= 0) return + + test_qmckl_ao_gaussian_vgl = -1 + + do i=1,n + test_qmckl_ao_gaussian_vgl = -11 + if (dabs(1.d0 - VGL(i,1) / (& + dexp(-A(i) * r2) & + )) > epsilon ) return + + test_qmckl_ao_gaussian_vgl = -12 + if (dabs(1.d0 - VGL(i,2) / (& + -2.d0 * A(i) * Y(1) * dexp(-A(i) * r2) & + )) > epsilon ) return + + test_qmckl_ao_gaussian_vgl = -13 + if (dabs(1.d0 - VGL(i,3) / (& + -2.d0 * A(i) * Y(2) * dexp(-A(i) * r2) & + )) > epsilon ) return + + test_qmckl_ao_gaussian_vgl = -14 + if (dabs(1.d0 - VGL(i,4) / (& + -2.d0 * A(i) * Y(3) * dexp(-A(i) * r2) & + )) > epsilon ) return + + test_qmckl_ao_gaussian_vgl = -15 + if (dabs(1.d0 - VGL(i,5) / (& + A(i) * (4.d0*r2*A(i) - 6.d0) * dexp(-A(i) * r2) & + )) > epsilon ) return + end do + + test_qmckl_ao_gaussian_vgl = 0 + + deallocate(VGL) +end function test_qmckl_ao_gaussian_vgl + #+END_SRC + + #+BEGIN_SRC C :tangle (org-entry-get nil "c_test" t) +int test_qmckl_ao_gaussian_vgl(qmckl_context context); +munit_assert_int(0, ==, test_qmckl_ao_gaussian_vgl(context)); + #+END_SRC + + +*** TODO Slater basis functions + +*** End of files :noexport: + +***** Test + #+BEGIN_SRC C :tangle (org-entry-get nil "c_test" t) + if (qmckl_context_destroy(context) != QMCKL_SUCCESS) + return QMCKL_FAILURE; + return MUNIT_OK; +} + #+END_SRC + + + # -*- mode: org -*- + # vim: syntax=c + diff --git a/src/qmckl_context.org b/src/qmckl_context.org new file mode 100644 index 0000000..fc8f0da --- /dev/null +++ b/src/qmckl_context.org @@ -0,0 +1,922 @@ +** Context + :PROPERTIES: + :c: qmckl_context.c + :c_test: test_qmckl_context.c + :fh: qmckl_f.f90 + :h: qmckl.h + :END: + + This file is written in C because it is more natural to express the + context in C than in Fortran. + + 2 files are produced: + - a source file : =qmckl_context.c= + - a test file : =test_qmckl_context.c= + +*** Headers :noexport: + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +#include "qmckl.h" +#include +#include +#include + #+END_SRC + + #+BEGIN_SRC C :tangle (org-entry-get nil "c_test" t) +#include "qmckl.h" +#include "munit.h" +MunitResult test_qmckl_context() { + #+END_SRC + +*** Context + + The <<>> variable is a handle for the state of the library, and + is stored in the following data structure, which can't be seen + outside of the library. To simplify compatibility with other + languages, the pointer to the internal data structure is converted + into a 64-bit signed integer, defined in the ~qmckl_context~ type. + A value of ~0~ for the context is equivalent to a ~NULL~ pointer. + + #+BEGIN_SRC C :comments org :tangle qmckl.h +typedef int64_t qmckl_context ; + #+END_SRC + +**** Data for error handling + + We define here the the data structure containing the strings + necessary for error handling. + + #+BEGIN_SRC C :comments org :tangle qmckl.h +#define QMCKL_MAX_FUN_LEN 256 +#define QMCKL_MAX_MSG_LEN 1024 + +typedef struct qmckl_error_struct { + + qmckl_exit_code exit_code; + char function[QMCKL_MAX_FUN_LEN]; + char message [QMCKL_MAX_MSG_LEN]; + +} qmckl_error_struct; + #+END_SRC + + +**** Basis set data structure + + Data structure for the info related to the atomic orbitals + basis set. + + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +typedef struct qmckl_ao_basis_struct { + + int64_t shell_num; + int64_t prim_num; + int64_t * shell_center; + int32_t * shell_ang_mom; + double * shell_factor; + double * exponent ; + double * coefficient ; + int64_t * shell_prim_num; + char type; + +} qmckl_ao_basis_struct; + #+END_SRC + +***** Source + + The tag is used internally to check if the memory domain pointed + by a pointer is a valid context. + + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +typedef struct qmckl_context_struct { + + struct qmckl_context_struct * prev; + + /* Molecular system */ + // struct qmckl_nucleus_struct * nucleus; + // struct qmckl_electron_struct * electron; + struct qmckl_ao_basis_struct * ao_basis; + // struct qmckl_mo_struct * mo; + // struct qmckl_determinant_struct * det; + + /* Numerical precision */ + uint32_t tag; + int32_t precision; + int32_t range; + + /* Error handling */ + struct qmckl_error_struct * error; + +} qmckl_context_struct; + +#define VALID_TAG 0xBEEFFACE +#define INVALID_TAG 0xDEADBEEF + #+END_SRC + +**** ~qmckl_context_update_error~ + + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +qmckl_exit_code +qmckl_context_update_error(qmckl_context context, const qmckl_exit_code exit_code, const char* function, const char* message); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +qmckl_exit_code +qmckl_context_update_error(qmckl_context context, const qmckl_exit_code exit_code, const char* function, const char* message) +{ + assert (context != 0); + assert (function != NULL); + assert (message != NULL); + assert (exit_code > 0); + assert (exit_code < QMCKL_INVALID_EXIT_CODE); + + qmckl_context_struct* ctx = (qmckl_context_struct*) context; + if (ctx == NULL) return QMCKL_FAILURE; + + if (ctx->error != NULL) { + free(ctx->error); + ctx->error = NULL; + } + + qmckl_error_struct* error = (qmckl_error_struct*) qmckl_malloc (context, sizeof(qmckl_error_struct)); + error->exit_code = exit_code; + strcpy(error->function, function); + strcpy(error->message, message); + + ctx->error = error; + + return QMCKL_SUCCESS; +} + #+END_SRC + +***** TODO Test + +**** ~qmckl_context_set_error~ + + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +qmckl_context +qmckl_context_set_error(qmckl_context context, const qmckl_exit_code exit_code, const char* function, const char* message); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +qmckl_context +qmckl_context_set_error(qmckl_context context, const qmckl_exit_code exit_code, const char* function, const char* message) +{ + assert (context != 0); + assert (function != NULL); + assert (message != NULL); + assert (exit_code > 0); + assert (exit_code < QMCKL_INVALID_EXIT_CODE); + + qmckl_context new_context = qmckl_context_copy(context); + if (new_context == 0) return context; + + if (qmckl_context_update_error(new_context, exit_code, + function, message) != QMCKL_SUCCESS) { + return context; + } + + return new_context; +} + #+END_SRC + +***** TODO Test + +***** Test :noexport: + + #+BEGIN_SRC C :tangle (org-entry-get nil "c_test" t) +qmckl_context context; +qmckl_context new_context; + #+END_SRC + + +**** ~qmckl_context_check~ + + Checks if the domain pointed by the pointer is a valid context. + Returns the input ~qmckl_context~ if the context is valid, 0 + otherwise. + + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +qmckl_context qmckl_context_check(const qmckl_context context) ; + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +qmckl_context qmckl_context_check(const qmckl_context context) { + + if (context == (qmckl_context) 0) return (qmckl_context) 0; + + const qmckl_context_struct * ctx = (qmckl_context_struct*) context; + + if (ctx->tag != VALID_TAG) return (qmckl_context) 0; + + return context; +} + #+END_SRC + +**** ~qmckl_context_create~ + + To create a new context, use ~qmckl_context_create()~. + - On success, returns a pointer to a context using the ~qmckl_context~ type + - Returns ~0~ upon failure to allocate the internal data structure + + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +qmckl_context qmckl_context_create(); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +qmckl_context qmckl_context_create() { + + qmckl_context_struct* context = + (qmckl_context_struct*) qmckl_malloc ((qmckl_context) 0, sizeof(qmckl_context_struct)); + if (context == NULL) { + return (qmckl_context) 0; + } + + context->prev = NULL; + context->ao_basis = NULL; + context->precision = QMCKL_DEFAULT_PRECISION; + context->range = QMCKL_DEFAULT_RANGE; + context->tag = VALID_TAG; + context->error = NULL; + + return (qmckl_context) context; +} + #+END_SRC + +***** Fortran interface + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer (c_int64_t) function qmckl_context_create() bind(C) + use, intrinsic :: iso_c_binding + end function qmckl_context_create + end interface + #+END_SRC + +***** Test :noexport: + #+BEGIN_SRC C :comments link :tangle (org-entry-get nil "c_test" t) +context = qmckl_context_create(); +munit_assert_int64( context, !=, (qmckl_context) 0); +munit_assert_int64( qmckl_context_check(context), ==, context); + #+END_SRC + +**** ~qmckl_context_copy~ + + This function makes a shallow copy of the current context. + - Copying the 0-valued context returns 0 + - On success, returns a pointer to the new context using the ~qmckl_context~ type + - Returns 0 upon failure to allocate the internal data structure + for the new context + + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +qmckl_context qmckl_context_copy(const qmckl_context context); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +qmckl_context qmckl_context_copy(const qmckl_context context) { + + const qmckl_context checked_context = qmckl_context_check(context); + + if (checked_context == (qmckl_context) 0) { + return (qmckl_context) 0; + } + + qmckl_context_struct* old_context = (qmckl_context_struct*) checked_context; + + qmckl_context_struct* new_context = + (qmckl_context_struct*) qmckl_malloc (context, sizeof(qmckl_context_struct)); + if (new_context == NULL) { + return (qmckl_context) 0; + } + + new_context->prev = old_context; + new_context->ao_basis = old_context->ao_basis; + new_context->precision = old_context->precision; + new_context->range = old_context->range; + new_context->tag = VALID_TAG; + new_context->error = old_context->error; + + return (qmckl_context) new_context; +} + + #+END_SRC + +***** Fortran interface + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer (c_int64_t) function qmckl_context_copy(context) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + end function qmckl_context_copy + end interface + #+END_SRC + +***** Test :noexport: + #+BEGIN_SRC C :comments link :tangle (org-entry-get nil "c_test" t) +new_context = qmckl_context_copy(context); +munit_assert_int64(new_context, !=, (qmckl_context) 0); +munit_assert_int64(new_context, !=, context); +munit_assert_int64(qmckl_context_check(new_context), ==, new_context); + #+END_SRC + +**** ~qmckl_context_previous~ + + Returns the previous context + - On success, returns the ancestor of the current context + - Returns 0 for the initial context + - Returns 0 for the 0-valued context + + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +qmckl_context qmckl_context_previous(const qmckl_context context); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +qmckl_context qmckl_context_previous(const qmckl_context context) { + + const qmckl_context checked_context = qmckl_context_check(context); + if (checked_context == (qmckl_context) 0) { + return (qmckl_context) 0; + } + + const qmckl_context_struct* ctx = (qmckl_context_struct*) checked_context; + return qmckl_context_check((qmckl_context) ctx->prev); +} + #+END_SRC + +***** Fortran interface + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer (c_int64_t) function qmckl_context_previous(context) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + end function qmckl_context_previous + end interface + #+END_SRC + +***** Test :noexport: + #+BEGIN_SRC C :comments link :tangle (org-entry-get nil "c_test" t) +munit_assert_int64(qmckl_context_previous(new_context), !=, (qmckl_context) 0); +munit_assert_int64(qmckl_context_previous(new_context), ==, context); +munit_assert_int64(qmckl_context_previous(context), ==, (qmckl_context) 0); +munit_assert_int64(qmckl_context_previous((qmckl_context) 0), ==, (qmckl_context) 0); + #+END_SRC + +**** ~qmckl_context_destroy~ + + Destroys the current context, leaving the ancestors untouched. + - Succeeds if the current context is properly destroyed + - Fails otherwise + - Fails if the 0-valued context is given in argument + - Fails if the the pointer is not a valid context + + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +qmckl_exit_code qmckl_context_destroy(qmckl_context context); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +qmckl_exit_code qmckl_context_destroy(const qmckl_context context) { + + const qmckl_context checked_context = qmckl_context_check(context); + if (checked_context == (qmckl_context) 0) return QMCKL_FAILURE; + + qmckl_context_struct* ctx = (qmckl_context_struct*) context; + if (ctx == NULL) return QMCKL_FAILURE; + + ctx->tag = INVALID_TAG; + return qmckl_free(context,ctx); +} + #+END_SRC + +***** Fortran interface + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer (c_int32_t) function qmckl_context_destroy(context) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + end function qmckl_context_destroy + end interface + #+END_SRC + +***** Test :noexport: + #+BEGIN_SRC C :tangle (org-entry-get nil "c_test" t) +munit_assert_int64(qmckl_context_check(new_context), ==, new_context); +munit_assert_int64(new_context, !=, (qmckl_context) 0); +munit_assert_int32(qmckl_context_destroy(new_context), ==, QMCKL_SUCCESS); +munit_assert_int64(qmckl_context_check(new_context), !=, new_context); +munit_assert_int64(qmckl_context_check(new_context), ==, (qmckl_context) 0); +munit_assert_int64(qmckl_context_destroy((qmckl_context) 0), ==, QMCKL_FAILURE); + #+END_SRC + +**** Basis set + + For H_2 with the following basis set, + + #+BEGIN_EXAMPLE +HYDROGEN +S 5 +1 3.387000E+01 6.068000E-03 +2 5.095000E+00 4.530800E-02 +3 1.159000E+00 2.028220E-01 +4 3.258000E-01 5.039030E-01 +5 1.027000E-01 3.834210E-01 +S 1 +1 3.258000E-01 1.000000E+00 +S 1 +1 1.027000E-01 1.000000E+00 +P 1 +1 1.407000E+00 1.000000E+00 +P 1 +1 3.880000E-01 1.000000E+00 +D 1 +1 1.057000E+00 1.0000000 + #+END_EXAMPLE + + we have: + + #+BEGIN_EXAMPLE +type = 'G' +shell_num = 12 +prim_num = 20 +SHELL_CENTER = [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2] +SHELL_ANG_MOM = ['S', 'S', 'S', 'P', 'P', 'D', 'S', 'S', 'S', 'P', 'P', 'D'] +SHELL_PRIM_NUM = [5, 1, 1, 1, 1, 1, 5, 1, 1, 1, 1, 1] +prim_index = [1, 6, 7, 8, 9, 10, 11, 16, 17, 18, 19, 20] +EXPONENT = [ 33.87, 5.095, 1.159, 0.3258, 0.1027, 0.3258, 0.1027, + 1.407, 0.388, 1.057, 33.87, 5.095, 1.159, 0.3258, 0.1027, + 0.3258, 0.1027, 1.407, 0.388, 1.057] +COEFFICIENT = [ 0.006068, 0.045308, 0.202822, 0.503903, 0.383421, + 1.0, 1.0, 1.0, 1.0, 1.0, 0.006068, 0.045308, 0.202822, + 0.503903, 0.383421, 1.0, 1.0, 1.0, 1.0, 1.0] + #+END_EXAMPLE + +**** ~qmckl_context_update_ao_basis~ + + Updates the data describing the AO basis set into the context. + + | ~type~ | Gaussian or Slater | + | ~shell_num~ | Number of shells | + | ~prim_num~ | Total number of primitives | + | ~SHELL_CENTER(shell_num)~ | Id of the nucleus on which the shell is centered | + | ~SHELL_ANG_MOM(shell_num)~ | Id of the nucleus on which the shell is centered | + | ~SHELL_FACTOR(shell_num)~ | Normalization factor for the shell | + | ~SHELL_PRIM_NUM(shell_num)~ | Number of primitives in the shell | + | ~SHELL_PRIM_INDEX(shell_num)~ | Address of the first primitive of the shelll in the ~EXPONENT~ array | + | ~EXPONENT(prim_num)~ | Array of exponents | + | ~COEFFICIENT(prim_num)~ | Array of coefficients | + + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +qmckl_exit_code +qmckl_context_update_ao_basis(qmckl_context context , const char type, + const int64_t shell_num , const int64_t prim_num, + const int64_t * SHELL_CENTER, const int32_t * SHELL_ANG_MOM, + const double * SHELL_FACTOR, const int64_t * SHELL_PRIM_NUM, + const int64_t * SHELL_PRIM_INDEX, + const double * EXPONENT , const double * COEFFICIENT); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +qmckl_exit_code +qmckl_context_update_ao_basis(qmckl_context context , const char type, + const int64_t shell_num , const int64_t prim_num, + const int64_t * SHELL_CENTER, const int32_t * SHELL_ANG_MOM, + const double * SHELL_FACTOR, const int64_t * SHELL_PRIM_NUM, + const int64_t * SHELL_PRIM_INDEX, + const double * EXPONENT , const double * COEFFICIENT) +{ + + int64_t i; + + /* Check input */ + + if (type != 'G' && type != 'S') return QMCKL_FAILURE; + if (shell_num <= 0) return QMCKL_FAILURE; + if (prim_num <= 0) return QMCKL_FAILURE; + if (prim_num < shell_num) return QMCKL_FAILURE; + + for (i=0 ; ishell_center = (int64_t*) malloc (shell_num * sizeof(int64_t)); + if (basis->shell_center == NULL) { + qmckl_free(context, basis); + return QMCKL_FAILURE; + } + + basis->shell_ang_mom = (int32_t*) malloc (shell_num * sizeof(int32_t)); + if (basis->shell_ang_mom == NULL) { + qmckl_free(context, basis->shell_center); + qmckl_free(context, basis); + return QMCKL_FAILURE; + } + + basis->shell_prim_num= (int64_t*) malloc (shell_num * sizeof(int64_t)); + if (basis->shell_prim_num == NULL) { + qmckl_free(context, basis->shell_ang_mom); + qmckl_free(context, basis->shell_center); + qmckl_free(context, basis); + return QMCKL_FAILURE; + } + + basis->shell_factor = (double *) malloc (shell_num * sizeof(double )); + if (basis->shell_factor == NULL) { + qmckl_free(context, basis->shell_prim_num); + qmckl_free(context, basis->shell_ang_mom); + qmckl_free(context, basis->shell_center); + qmckl_free(context, basis); + return QMCKL_FAILURE; + } + + basis->exponent = (double *) malloc (prim_num * sizeof(double )); + if (basis->exponent == NULL) { + qmckl_free(context, basis->shell_factor); + qmckl_free(context, basis->shell_prim_num); + qmckl_free(context, basis->shell_ang_mom); + qmckl_free(context, basis->shell_center); + qmckl_free(context, basis); + return QMCKL_FAILURE; + } + + basis->coefficient = (double *) malloc (prim_num * sizeof(double )); + if (basis->coefficient == NULL) { + qmckl_free(context, basis->exponent); + qmckl_free(context, basis->shell_factor); + qmckl_free(context, basis->shell_prim_num); + qmckl_free(context, basis->shell_ang_mom); + qmckl_free(context, basis->shell_center); + qmckl_free(context, basis); + return QMCKL_FAILURE; + } + + + /* Assign data */ + + basis->type = type; + basis->shell_num = shell_num; + basis->prim_num = prim_num; + + for (i=0 ; ishell_center [i] = SHELL_CENTER [i]; + basis->shell_ang_mom [i] = SHELL_ANG_MOM [i]; + basis->shell_prim_num[i] = SHELL_PRIM_NUM[i]; + basis->shell_factor [i] = SHELL_FACTOR [i]; + } + + for (i=0 ; iexponent [i] = EXPONENT[i]; + basis->coefficient[i] = COEFFICIENT[i]; + } + + ctx->ao_basis = basis; + return QMCKL_SUCCESS; +} + #+END_SRC + +***** Fortran interface + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer (c_int32_t) function qmckl_context_update_ao_basis(context, & + typ, shell_num, prim_num, SHELL_CENTER, SHELL_ANG_MOM, SHELL_FACTOR, & + SHELL_PRIM_NUM, SHELL_PRIM_INDEX, EXPONENT, COEFFICIENT) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + character(c_char) , intent(in), value :: typ + integer (c_int64_t), intent(in), value :: shell_num + integer (c_int64_t), intent(in), value :: prim_num + integer (c_int64_t), intent(in) :: SHELL_CENTER(shell_num) + integer (c_int32_t), intent(in) :: SHELL_ANG_MOM(shell_num) + double precision , intent(in) :: SHELL_FACTOR(shell_num) + integer (c_int64_t), intent(in) :: SHELL_PRIM_NUM(shell_num) + integer (c_int64_t), intent(in) :: SHELL_PRIM_INDEX(shell_num) + double precision , intent(in) :: EXPONENT(prim_num) + double precision , intent(in) :: COEFFICIENT(prim_num) + end function qmckl_context_update_ao_basis + end interface + #+END_SRC + +***** TODO Test + +**** ~qmckl_context_set_ao_basis~ + + Sets the data describing the AO basis set into the context. + + | ~type~ | Gaussian or Slater | + | ~shell_num~ | Number of shells | + | ~prim_num~ | Total number of primitives | + | ~SHELL_CENTER(shell_num)~ | Id of the nucleus on which the shell is centered | + | ~SHELL_ANG_MOM(shell_num)~ | Id of the nucleus on which the shell is centered | + | ~SHELL_FACTOR(shell_num)~ | Normalization factor for the shell | + | ~SHELL_PRIM_NUM(shell_num)~ | Number of primitives in the shell | + | ~SHELL_PRIM_INDEX(shell_num)~ | Address of the first primitive of the shelll in the ~EXPONENT~ array | + | ~EXPONENT(prim_num)~ | Array of exponents | + | ~COEFFICIENT(prim_num)~ | Array of coefficients | + + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +qmckl_context +qmckl_context_set_ao_basis(const qmckl_context context , const char type, + const int64_t shell_num , const int64_t prim_num, + const int64_t * SHELL_CENTER, const int32_t * SHELL_ANG_MOM, + const double * SHELL_FACTOR, const int64_t * SHELL_PRIM_NUM, + const int64_t * SHELL_PRIM_INDEX, + const double * EXPONENT , const double * COEFFICIENT); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +qmckl_context +qmckl_context_set_ao_basis(const qmckl_context context , const char type, + const int64_t shell_num , const int64_t prim_num, + const int64_t * SHELL_CENTER, const int32_t * SHELL_ANG_MOM, + const double * SHELL_FACTOR, const int64_t * SHELL_PRIM_NUM, + const int64_t * SHELL_PRIM_INDEX, + const double * EXPONENT , const double * COEFFICIENT) +{ + + qmckl_context new_context = qmckl_context_copy(context); + if (new_context == 0) return 0; + + if (qmckl_context_update_ao_basis(new_context, type, shell_num, prim_num, + SHELL_CENTER, SHELL_ANG_MOM, SHELL_FACTOR, + SHELL_PRIM_NUM, SHELL_PRIM_INDEX, EXPONENT, + COEFFICIENT + ) == QMCKL_FAILURE) + return 0; + + return new_context; +} + #+END_SRC + +***** Fortran interface + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer (c_int64_t) function qmckl_context_set_ao_basis(context, & + typ, shell_num, prim_num, SHELL_CENTER, SHELL_ANG_MOM, SHELL_FACTOR, & + SHELL_PRIM_NUM, SHELL_PRIM_INDEX, EXPONENT, COEFFICIENT) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + character(c_char) , intent(in), value :: typ + integer (c_int64_t), intent(in), value :: shell_num + integer (c_int64_t), intent(in), value :: prim_num + integer (c_int64_t), intent(in) :: SHELL_CENTER(shell_num) + integer (c_int32_t), intent(in) :: SHELL_ANG_MOM(shell_num) + double precision , intent(in) :: SHELL_FACTOR(shell_num) + integer (c_int64_t), intent(in) :: SHELL_PRIM_NUM(shell_num) + integer (c_int64_t), intent(in) :: SHELL_PRIM_INDEX(shell_num) + double precision , intent(in) :: EXPONENT(prim_num) + double precision , intent(in) :: COEFFICIENT(prim_num) + end function qmckl_context_set_ao_basis + end interface + #+END_SRC + +***** TODO Test + +**** Precision + + The following functions set and get the expected required + precision and range. ~precision~ should be an integer between 2 + and 53, and ~range~ should be an integer between 2 and 11. + + The setter functions functions return a new context as a 64-bit + integer. The getter functions return the value, as a 32-bit + integer. The update functions return ~QMCKL_SUCCESS~ or + ~QMCKL_FAILURE~. + +**** ~qmckl_context_update_precision~ + Modifies the parameter for the numerical precision in a given context. + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +qmckl_exit_code qmckl_context_update_precision(const qmckl_context context, const int precision); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +qmckl_exit_code qmckl_context_update_precision(const qmckl_context context, const int precision) { + + if (precision < 2) return QMCKL_FAILURE; + if (precision > 53) return QMCKL_FAILURE; + + qmckl_context_struct* ctx = (qmckl_context_struct*) context; + if (ctx == NULL) return QMCKL_FAILURE; + + ctx->precision = precision; + return QMCKL_SUCCESS; +} + #+END_SRC + +***** Fortran interface + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer (c_int32_t) function qmckl_context_update_precision(context, precision) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + integer (c_int32_t), intent(in), value :: precision + end function qmckl_context_update_precision + end interface + #+END_SRC + +***** TODO Tests :noexport: +**** ~qmckl_context_update_range~ + Modifies the parameter for the numerical range in a given context. + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +qmckl_exit_code qmckl_context_update_range(const qmckl_context context, const int range); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +qmckl_exit_code qmckl_context_update_range(const qmckl_context context, const int range) { + + if (range < 2) return QMCKL_FAILURE; + if (range > 11) return QMCKL_FAILURE; + + qmckl_context_struct* ctx = (qmckl_context_struct*) context; + if (ctx == NULL) return QMCKL_FAILURE; + + ctx->range = range; + return QMCKL_SUCCESS; +} + #+END_SRC + +***** Fortran interface + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer (c_int32_t) function qmckl_context_update_range(context, range) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + integer (c_int32_t), intent(in), value :: range + end function qmckl_context_update_range + end interface + #+END_SRC + +***** TODO Tests :noexport: +**** ~qmckl_context_set_precision~ + Returns a copy of the context with a different precision parameter. + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +qmckl_context qmckl_context_set_precision(const qmckl_context context, const int precision); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +qmckl_context qmckl_context_set_precision(const qmckl_context context, const int precision) { + qmckl_context new_context = qmckl_context_copy(context); + if (new_context == 0) return 0; + + if (qmckl_context_update_precision(new_context, precision) == QMCKL_FAILURE) return 0; + + return new_context; +} + #+END_SRC + +***** Fortran interface + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer (c_int64_t) function qmckl_context_set_precision(context, precision) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + integer (c_int32_t), intent(in), value :: precision + end function qmckl_context_set_precision + end interface + #+END_SRC + +***** TODO Tests :noexport: +**** ~qmckl_context_set_range~ + Returns a copy of the context with a different precision parameter. + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +qmckl_context qmckl_context_set_range(const qmckl_context context, const int range); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +qmckl_context qmckl_context_set_range(const qmckl_context context, const int range) { + qmckl_context new_context = qmckl_context_copy(context); + if (new_context == 0) return 0; + + if (qmckl_context_update_range(new_context, range) == QMCKL_FAILURE) return 0; + + return new_context; +} + #+END_SRC + +***** Fortran interface + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer (c_int64_t) function qmckl_context_set_range(context, range) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + integer (c_int32_t), intent(in), value :: range + end function qmckl_context_set_range + end interface + #+END_SRC + +***** TODO Tests :noexport: + +**** ~qmckl_context_get_precision~ + Returns the value of the numerical precision in the context + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +int32_t qmckl_context_get_precision(const qmckl_context context); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +int qmckl_context_get_precision(const qmckl_context context) { + const qmckl_context_struct* ctx = (qmckl_context_struct*) context; + return ctx->precision; +} + #+END_SRC + +***** Fortran interface + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer (c_int32_t) function qmckl_context_get_precision(context) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + end function qmckl_context_get_precision + end interface + #+END_SRC + +***** TODO Tests :noexport: +**** ~qmckl_context_get_range~ + Returns the value of the numerical range in the context + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +int32_t qmckl_context_get_range(const qmckl_context context); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +int qmckl_context_get_range(const qmckl_context context) { + const qmckl_context_struct* ctx = (qmckl_context_struct*) context; + return ctx->range; +} + #+END_SRC + +***** Fortran interface + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + integer (c_int32_t) function qmckl_context_get_range(context) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + end function qmckl_context_get_range + end interface + #+END_SRC + +***** TODO Tests :noexport: + +**** ~qmckl_context_get_epsilon~ + Returns $\epsilon = 2^{1-n}$ where ~n~ is the precision + #+BEGIN_SRC C :comments org :tangle (org-entry-get nil "h" t) +double qmckl_context_get_epsilon(const qmckl_context context); + #+END_SRC + +***** Source + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +double qmckl_context_get_epsilon(const qmckl_context context) { + const qmckl_context_struct* ctx = (qmckl_context_struct*) context; + return pow(2.0,(double) 1-ctx->precision); +} + #+END_SRC + +***** Fortran interface + #+BEGIN_SRC f90 :tangle (org-entry-get nil "fh" t) + interface + real (c_double) function qmckl_context_get_epsilon(context) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + end function qmckl_context_get_epsilon + end interface + #+END_SRC + +***** TODO Tests :noexport: + + + +*** End of files :noexport: + +***** Test + #+BEGIN_SRC C :comments link :tangle (org-entry-get nil "c_test" t) +return MUNIT_OK; +} + #+END_SRC + + + + # -*- mode: org -*- + # vim: syntax=c diff --git a/src/qmckl_distance.org b/src/qmckl_distance.org new file mode 100644 index 0000000..57e5531 --- /dev/null +++ b/src/qmckl_distance.org @@ -0,0 +1,356 @@ +** Computation of distances + + Function for the computation of distances between particles. + + 3 files are produced: + - a source file : =qmckl_distance.f90= + - a C test file : =test_qmckl_distance.c= + - a Fortran test file : =test_qmckl_distance_f.f90= + +**** Headers :noexport: + #+BEGIN_SRC C :comments link :tangle test_qmckl_distance.c +#include "qmckl.h" +#include "munit.h" +MunitResult test_qmckl_distance() { + qmckl_context context; + context = qmckl_context_create(); + + #+END_SRC + + +*** Squared distance + +**** ~qmckl_distance_sq~ + + Computes the matrix of the squared distances between all pairs of + points in two sets, one point within each set: + \[ + C_{ij} = \sum_{k=1}^3 (A_{k,i}-B_{k,j})^2 + \] + +***** Arguments + + | ~context~ | input | Global state | + | ~transa~ | input | Array ~A~ is ~N~: Normal, ~T~: Transposed | + | ~transb~ | input | Array ~B~ is ~N~: Normal, ~T~: Transposed | + | ~m~ | input | Number of points in the first set | + | ~n~ | input | Number of points in the second set | + | ~A(lda,3)~ | input | Array containing the $m \times 3$ matrix $A$ | + | ~lda~ | input | Leading dimension of array ~A~ | + | ~B(ldb,3)~ | input | Array containing the $n \times 3$ matrix $B$ | + | ~ldb~ | input | Leading dimension of array ~B~ | + | ~C(ldc,n)~ | output | Array containing the $m \times n$ matrix $C$ | + | ~ldc~ | input | Leading dimension of array ~C~ | + +***** Requirements + + - ~context~ is not 0 + - ~m~ > 0 + - ~n~ > 0 + - ~lda~ >= 3 if ~transa~ is ~N~ + - ~lda~ >= m if ~transa~ is ~T~ + - ~ldb~ >= 3 if ~transb~ is ~N~ + - ~ldb~ >= n if ~transb~ is ~T~ + - ~ldc~ >= m + - ~A~ is allocated with at least $3 \times m \times 8$ bytes + - ~B~ is allocated with at least $3 \times n \times 8$ bytes + - ~C~ is allocated with at least $m \times n \times 8$ bytes + +***** Performance + + This function might be more efficient when ~A~ and ~B~ are + transposed. + + #+BEGIN_SRC C :comments org :tangle qmckl.h +qmckl_exit_code qmckl_distance_sq(const qmckl_context context, + const char transa, const char transb, + const int64_t m, const int64_t n, + const double *A, const int64_t lda, + const double *B, const int64_t ldb, + const double *C, const int64_t ldc); + #+END_SRC + +***** Source + #+BEGIN_SRC f90 :tangle qmckl_distance.f90 +integer function qmckl_distance_sq_f(context, transa, transb, m, n, A, LDA, B, LDB, C, LDC) result(info) + implicit none + integer*8 , intent(in) :: context + character , intent(in) :: transa, transb + integer*8 , intent(in) :: m, n + integer*8 , intent(in) :: lda + real*8 , intent(in) :: A(lda,*) + integer*8 , intent(in) :: ldb + real*8 , intent(in) :: B(ldb,*) + integer*8 , intent(in) :: ldc + real*8 , intent(out) :: C(ldc,*) + + integer*8 :: i,j + real*8 :: x, y, z + integer :: transab + + info = 0 + + if (context == 0_8) then + info = -1 + return + endif + + if (m <= 0_8) then + info = -2 + return + endif + + if (n <= 0_8) then + info = -3 + return + endif + + if (transa == 'N' .or. transa == 'n') then + transab = 0 + else if (transa == 'T' .or. transa == 't') then + transab = 1 + else + transab = -100 + endif + + if (transb == 'N' .or. transb == 'n') then + continue + else if (transa == 'T' .or. transa == 't') then + transab = transab + 2 + else + transab = -100 + endif + + if (transab < 0) then + info = -4 + return + endif + + if (iand(transab,1) == 0 .and. LDA < 3) then + info = -5 + return + endif + + if (iand(transab,1) == 1 .and. LDA < m) then + info = -6 + return + endif + + if (iand(transab,2) == 0 .and. LDA < 3) then + info = -6 + return + endif + + if (iand(transab,2) == 2 .and. LDA < m) then + info = -7 + return + endif + + + select case (transab) + + case(0) + + do j=1,n + do i=1,m + x = A(1,i) - B(1,j) + y = A(2,i) - B(2,j) + z = A(3,i) - B(3,j) + C(i,j) = x*x + y*y + z*z + end do + end do + + case(1) + + do j=1,n + do i=1,m + x = A(i,1) - B(1,j) + y = A(i,2) - B(2,j) + z = A(i,3) - B(3,j) + C(i,j) = x*x + y*y + z*z + end do + end do + + case(2) + + do j=1,n + do i=1,m + x = A(1,i) - B(j,1) + y = A(2,i) - B(j,2) + z = A(3,i) - B(j,3) + C(i,j) = x*x + y*y + z*z + end do + end do + + case(3) + + do j=1,n + do i=1,m + x = A(i,1) - B(j,1) + y = A(i,2) - B(j,2) + z = A(i,3) - B(j,3) + C(i,j) = x*x + y*y + z*z + end do + end do + + end select + +end function qmckl_distance_sq_f + #+END_SRC + +***** C interface :noexport: + #+BEGIN_SRC f90 :tangle qmckl_distance.f90 +integer(c_int32_t) function qmckl_distance_sq(context, transa, transb, m, n, A, LDA, B, LDB, C, LDC) & + bind(C) result(info) + use, intrinsic :: iso_c_binding + implicit none + integer (c_int64_t) , intent(in) , value :: context + character (c_char) , intent(in) , value :: transa, transb + integer (c_int64_t) , intent(in) , value :: m, n + integer (c_int64_t) , intent(in) , value :: lda + real (c_double) , intent(in) :: A(lda,3) + integer (c_int64_t) , intent(in) , value :: ldb + real (c_double) , intent(in) :: B(ldb,3) + integer (c_int64_t) , intent(in) , value :: ldc + real (c_double) , intent(out) :: C(ldc,n) + + integer, external :: qmckl_distance_sq_f + info = qmckl_distance_sq_f(context, transa, transb, m, n, A, LDA, B, LDB, C, LDC) +end function qmckl_distance_sq + #+END_SRC + + #+BEGIN_SRC f90 :tangle qmckl_f.f90 + interface + integer(c_int32_t) function qmckl_distance_sq(context, transa, transb, m, n, A, LDA, B, LDB, C, LDC) & + bind(C) + use, intrinsic :: iso_c_binding + implicit none + integer (c_int64_t) , intent(in) , value :: context + character (c_char) , intent(in) , value :: transa, transb + integer (c_int64_t) , intent(in) , value :: m, n + integer (c_int64_t) , intent(in) , value :: lda + integer (c_int64_t) , intent(in) , value :: ldb + integer (c_int64_t) , intent(in) , value :: ldc + real (c_double) , intent(in) :: A(lda,3) + real (c_double) , intent(in) :: B(ldb,3) + real (c_double) , intent(out) :: C(ldc,n) + end function qmckl_distance_sq + end interface + #+END_SRC + +***** Test :noexport: + #+BEGIN_SRC f90 :tangle test_qmckl_distance_f.f90 +integer(c_int32_t) function test_qmckl_distance_sq(context) bind(C) + use qmckl + implicit none + integer(c_int64_t), intent(in), value :: context + + double precision, allocatable :: A(:,:), B(:,:), C(:,:) + integer*8 :: m, n, LDA, LDB, LDC + double precision :: x + integer*8 :: i,j + + m = 5 + n = 6 + LDA = m + LDB = n + LDC = 5 + + allocate( A(LDA,m), B(LDB,n), C(LDC,n) ) + + do j=1,m + do i=1,m + A(i,j) = -10.d0 + dble(i+j) + end do + end do + do j=1,n + do i=1,n + B(i,j) = -1.d0 + dble(i*j) + end do + end do + + test_qmckl_distance_sq = qmckl_distance_sq(context, 'X', 't', m, n, A, LDA, B, LDB, C, LDC) + if (test_qmckl_distance_sq == 0) return + + test_qmckl_distance_sq = qmckl_distance_sq(context, 't', 'X', m, n, A, LDA, B, LDB, C, LDC) + if (test_qmckl_distance_sq == 0) return + + test_qmckl_distance_sq = qmckl_distance_sq(context, 'T', 't', m, n, A, LDA, B, LDB, C, LDC) + if (test_qmckl_distance_sq /= 0) return + + test_qmckl_distance_sq = -1 + + do j=1,n + do i=1,m + x = (A(i,1)-B(j,1))**2 + & + (A(i,2)-B(j,2))**2 + & + (A(i,3)-B(j,3))**2 + if ( dabs(1.d0 - C(i,j)/x) > 1.d-14 ) return + end do + end do + + test_qmckl_distance_sq = qmckl_distance_sq(context, 'n', 'T', m, n, A, LDA, B, LDB, C, LDC) + if (test_qmckl_distance_sq /= 0) return + + test_qmckl_distance_sq = -1 + + do j=1,n + do i=1,m + x = (A(1,i)-B(j,1))**2 + & + (A(2,i)-B(j,2))**2 + & + (A(3,i)-B(j,3))**2 + if ( dabs(1.d0 - C(i,j)/x) > 1.d-14 ) return + end do + end do + + test_qmckl_distance_sq = qmckl_distance_sq(context, 'T', 'n', m, n, A, LDA, B, LDB, C, LDC) + if (test_qmckl_distance_sq /= 0) return + + test_qmckl_distance_sq = -1 + + do j=1,n + do i=1,m + x = (A(i,1)-B(1,j))**2 + & + (A(i,2)-B(2,j))**2 + & + (A(i,3)-B(3,j))**2 + if ( dabs(1.d0 - C(i,j)/x) > 1.d-14 ) return + end do + end do + + test_qmckl_distance_sq = qmckl_distance_sq(context, 'n', 'N', m, n, A, LDA, B, LDB, C, LDC) + if (test_qmckl_distance_sq /= 0) return + + test_qmckl_distance_sq = -1 + + do j=1,n + do i=1,m + x = (A(1,i)-B(1,j))**2 + & + (A(2,i)-B(2,j))**2 + & + (A(3,i)-B(3,j))**2 + if ( dabs(1.d0 - C(i,j)/x) > 1.d-14 ) return + end do + end do + + test_qmckl_distance_sq = 0 + + deallocate(A,B,C) +end function test_qmckl_distance_sq + #+END_SRC + + #+BEGIN_SRC C :comments link :tangle test_qmckl_distance.c +int test_qmckl_distance_sq(qmckl_context context); +munit_assert_int(0, ==, test_qmckl_distance_sq(context)); + #+END_SRC +*** End of files :noexport: + + #+BEGIN_SRC C :comments link :tangle test_qmckl_distance.c + if (qmckl_context_destroy(context) != QMCKL_SUCCESS) + return QMCKL_FAILURE; + return MUNIT_OK; +} + + #+END_SRC + + + # -*- mode: org -*- + # vim: syntax=c diff --git a/src/qmckl_error.org b/src/qmckl_error.org new file mode 100644 index 0000000..dda5996 --- /dev/null +++ b/src/qmckl_error.org @@ -0,0 +1,193 @@ +# This file is part of the qmckl.h file +** Error handling + :PROPERTIES: + :c: qmckl_error.c + :c_test: test_qmckl_error.c + :fh: qmckl_f.f90 + :h: qmckl.h + :END: + + This file is written in C because it is more natural to express the + error handling in C than in Fortran. + + 2 files are produced: + - a source file : =qmckl_error.c= + - a test file : =test_qmckl_error.c= + +*** Headers :noexport: + #+BEGIN_SRC C :tangle (org-entry-get nil "c" t) +#include "qmckl.h" +#include +#include +#include +#include + #+END_SRC + + #+BEGIN_SRC C :tangle (org-entry-get nil "c_test" t) +#include "qmckl.h" +#include "munit.h" +MunitResult test_qmckl_error() { + #+END_SRC + +*** Error handling + + The library should never make the calling programs abort, nor + perform any input/output operations. This decision has to be taken + by the developer of the code calling the library. + + All the functions return with an <<>>, defined as + #+NAME: type-exit-code + #+BEGIN_SRC C :comments org :tangle qmckl.h +typedef int32_t qmckl_exit_code; + #+END_SRC + + The exit code returns the completion status of the function to the + calling program. When a function call completed successfully, the + ~QMCKL_SUCCESS~ exit code is returned. If one of the functions of + the library fails to complete the requested task, an appropriate + error code is returned to the program. + + Here is the complete list of exit codes. + + #+NAME: table-exit-codes + | ~QMCKL_SUCCESS~ | 0 | + | ~QMCKL_INVALID_ARG_1~ | 1 | + | ~QMCKL_INVALID_ARG_2~ | 2 | + | ~QMCKL_INVALID_ARG_3~ | 3 | + | ~QMCKL_INVALID_ARG_4~ | 4 | + | ~QMCKL_INVALID_ARG_5~ | 5 | + | ~QMCKL_INVALID_ARG_6~ | 6 | + | ~QMCKL_INVALID_ARG_7~ | 7 | + | ~QMCKL_INVALID_ARG_8~ | 8 | + | ~QMCKL_INVALID_ARG_9~ | 9 | + | ~QMCKL_INVALID_ARG_10~ | 10 | + | ~QMCKL_NULL_CONTEXT~ | 101 | + | ~QMCKL_FAILURE~ | 102 | + | ~QMCKL_ERRNO~ | 103 | + | ~QMCKL_INVALID_EXIT_CODE~ | 104 | + + # We need to force Emacs not to indent the Python code: + # -*- org-src-preserve-indentation: t + #+BEGIN_SRC python :var table=table-exit-codes :results drawer :exports result +""" This script generates the C and Fortran constants for the error + codes from the org-mode table. +""" + +result = [ "#+BEGIN_SRC C :comments org :tangle qmckl.h" ] +for (text, code) in table: + text=text.replace("~","") + result += [ f"#define {text:30s} {code:d}" ] +result += [ "#+END_SRC" ] + +result += [ "" ] + +result += [ "#+BEGIN_SRC f90 :comments org :tangle qmckl_f.f90" ] +for (text, code) in table: + text=text.replace("~","") + result += [ f" integer, parameter :: {text:30s} = {code:d}" ] +result += [ "#+END_SRC" ] + +return '\n'.join(result) + + #+END_SRC + + #+RESULTS: + :results: + #+BEGIN_SRC C :comments org :tangle qmckl.h + #define QMCKL_SUCCESS 0 + #define QMCKL_INVALID_ARG_1 1 + #define QMCKL_INVALID_ARG_2 2 + #define QMCKL_INVALID_ARG_3 3 + #define QMCKL_INVALID_ARG_4 4 + #define QMCKL_INVALID_ARG_5 5 + #define QMCKL_INVALID_ARG_6 6 + #define QMCKL_INVALID_ARG_7 7 + #define QMCKL_INVALID_ARG_8 8 + #define QMCKL_INVALID_ARG_9 9 + #define QMCKL_INVALID_ARG_10 10 + #define QMCKL_NULL_CONTEXT 101 + #define QMCKL_FAILURE 102 + #define QMCKL_ERRNO 103 + #define QMCKL_INVALID_EXIT_CODE 104 + #+END_SRC + + #+BEGIN_SRC f90 :comments org :tangle qmckl_f.f90 + integer, parameter :: QMCKL_SUCCESS = 0 + integer, parameter :: QMCKL_INVALID_ARG_1 = 1 + integer, parameter :: QMCKL_INVALID_ARG_2 = 2 + integer, parameter :: QMCKL_INVALID_ARG_3 = 3 + integer, parameter :: QMCKL_INVALID_ARG_4 = 4 + integer, parameter :: QMCKL_INVALID_ARG_5 = 5 + integer, parameter :: QMCKL_INVALID_ARG_6 = 6 + integer, parameter :: QMCKL_INVALID_ARG_7 = 7 + integer, parameter :: QMCKL_INVALID_ARG_8 = 8 + integer, parameter :: QMCKL_INVALID_ARG_9 = 9 + integer, parameter :: QMCKL_INVALID_ARG_10 = 10 + integer, parameter :: QMCKL_NULL_CONTEXT = 101 + integer, parameter :: QMCKL_FAILURE = 102 + integer, parameter :: QMCKL_ERRNO = 103 + integer, parameter :: QMCKL_INVALID_EXIT_CODE = 104 + #+END_SRC + :end: + + To make a function fail, the <<<~qmckl_failwith~>>> function should be + called, such that information about the failure is stored in + the context. The desired exit code is given as an argument, as + well as the name of the function and an error message. The return + code of the function is the desired return code. + + #+BEGIN_SRC C :comments org :tangle qmckl.h +qmckl_exit_code qmckl_failwith(qmckl_context context, + const qmckl_exit_code exit_code, + const char* function, + const char* message) ; + #+END_SRC + + #+BEGIN_SRC C :comments org :tangle qmckl_error.c +qmckl_exit_code qmckl_failwith(qmckl_context context, + const qmckl_exit_code exit_code, + const char* function, + const char* message) { + if (context == 0) return QMCKL_NULL_CONTEXT; + assert (exit_code > 0); + assert (exit_code < QMCKL_INVALID_EXIT_CODE); + assert (function != NULL); + assert (message != NULL); + assert (strlen(function) < QMCKL_MAX_FUN_LEN); + assert (strlen(message) < QMCKL_MAX_MSG_LEN); + + context = qmckl_context_set_error(context, exit_code, function, message); + return exit_code; +} + + #+END_SRC + + For example, this function can be used as + #+BEGIN_SRC C :tangle no +if (x < 0) { + return qmckl_failwith(context, + QMCKL_INVALID_ARG_2, + "qmckl_function", + "Expected x >= 0"); + } + #+END_SRC + + + # To decode the error messages, the <<<~qmckl_strerror~>>> converts an + # error code into a string. + +*** End of files :noexport: + +***** Test + #+BEGIN_SRC C :comments link :tangle (org-entry-get nil "c_test" t) +return MUNIT_OK; +} + #+END_SRC + + + + # -*- mode: org -*- + # vim: syntax=c + + # -*- mode: org -*- + # vim: syntax=c diff --git a/src/qmckl_footer.org b/src/qmckl_footer.org new file mode 100644 index 0000000..5ed01c7 --- /dev/null +++ b/src/qmckl_footer.org @@ -0,0 +1,18 @@ +* Acknowledgments + + [[https://trex-coe.eu/sites/default/files/inline-images/euflag.jpg]] + [[https://trex-coe.eu][TREX: Targeting Real Chemical Accuracy at the Exascale]] project has received funding from the European Union’s Horizon 2020 - Research and Innovation program - under grant agreement no. 952165. The content of this document does not represent the opinion of the European Union, and the European Union is not responsible for any use that might be made of such content. + +* End of header files :noexport: + +#+BEGIN_SRC C :tangle qmckl.h +#endif +#+END_SRC + +#+BEGIN_SRC f90 :tangle qmckl_f.f90 +end module qmckl +#+END_SRC + + +# -*- mode: org -*- + diff --git a/src/qmckl_memory.org b/src/qmckl_memory.org new file mode 100644 index 0000000..d861c13 --- /dev/null +++ b/src/qmckl_memory.org @@ -0,0 +1,117 @@ +** Memory management + + We override the allocation functions to enable the possibility of + optimized libraries to fine-tune the memory allocation. + + 2 files are produced: + - a source file : =qmckl_memory.c= + - a test file : =test_qmckl_memory.c= + +*** Headers :noexport: + #+BEGIN_SRC C :tangle qmckl_memory.c +#include "qmckl.h" +#include + #+END_SRC + + #+BEGIN_SRC C :tangle test_qmckl_memory.c +#include "qmckl.h" +#include "munit.h" +MunitResult test_qmckl_memory() { + #+END_SRC + +*** ~qmckl_malloc~ + + Memory allocation function, letting the library choose how the + memory will be allocated, and a pointer is returned to the user. + The context is passed to let the library store data related to the + allocation inside the context. + + #+BEGIN_SRC C :tangle qmckl.h +void* qmckl_malloc(const qmckl_context ctx, const size_t size); + #+END_SRC + + #+BEGIN_SRC f90 :tangle qmckl_f.f90 + interface + type (c_ptr) function qmckl_malloc (context, size) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + integer (c_int64_t), intent(in), value :: size + end function qmckl_malloc + end interface + #+END_SRC + +**** Source + #+BEGIN_SRC C :tangle qmckl_memory.c +void* qmckl_malloc(const qmckl_context ctx, const size_t size) { + if (ctx == (qmckl_context) 0) {}; /* Avoid unused argument warning */ + void * result = malloc( (size_t) size ); + assert (result != NULL) ; + return result; +} + + #+END_SRC + +**** Test :noexport: + #+BEGIN_SRC C :tangle test_qmckl_memory.c +int *a = NULL; +munit_assert(a == NULL); +a = (int*) qmckl_malloc( (qmckl_context) 1, 3*sizeof(int)); +munit_assert(a != NULL); +a[0] = 1; +a[1] = 2; +a[2] = 3; +munit_assert_int(a[0], ==, 1); +munit_assert_int(a[1], ==, 2); +munit_assert_int(a[2], ==, 3); + #+END_SRC + +*** ~qmckl_free~ + + The context is passed, in case some important information has been + stored related to memory allocation and needs to be updated. + + #+BEGIN_SRC C :tangle qmckl.h +qmckl_exit_code qmckl_free(qmckl_context context, void *ptr); + #+END_SRC + + #+BEGIN_SRC f90 :tangle qmckl_f.f90 + interface + integer (c_int32_t) function qmckl_free (context, ptr) bind(C) + use, intrinsic :: iso_c_binding + integer (c_int64_t), intent(in), value :: context + type (c_ptr), intent(in), value :: ptr + end function qmckl_free + end interface + #+END_SRC + +**** Source + #+BEGIN_SRC C :tangle qmckl_memory.c +qmckl_exit_code qmckl_free(qmckl_context context, void *ptr) { + if (context == 0) return QMCKL_INVALID_ARG_1; + if (ptr == NULL) return QMCKL_INVALID_ARG_2; + free(ptr); + return QMCKL_SUCCESS; +} + #+END_SRC + +**** Test :noexport: + #+BEGIN_SRC C :tangle test_qmckl_memory.c +munit_assert(a != NULL); +qmckl_exit_code rc; +rc = qmckl_free( (qmckl_context) 1, a); +munit_assert(rc == QMCKL_SUCCESS); + + #+END_SRC + +*** End of files :noexport: + +**** Test + #+BEGIN_SRC C :comments org :tangle test_qmckl_memory.c + return MUNIT_OK; +} + + #+END_SRC + + + # -*- mode: org -*- + # vim: syntax=c diff --git a/src/qmckl_precision.org b/src/qmckl_precision.org new file mode 100644 index 0000000..60c8fd0 --- /dev/null +++ b/src/qmckl_precision.org @@ -0,0 +1,21 @@ +# This file is part of the qmckl.h file + +*** Multi-precision related constants + + Controlling numerical precision enables optimizations. Here, the + default parameters determining the target numerical precision and + range are defined. + + #+BEGIN_SRC C :comments org :tangle qmckl.h +#define QMCKL_DEFAULT_PRECISION 53 +#define QMCKL_DEFAULT_RANGE 11 + #+END_SRC + + #+BEGIN_SRC f90 :comments org :tangle qmckl_f.f90 + integer, parameter :: QMCKL_DEFAULT_PRECISION = 53 + integer, parameter :: QMCKL_DEFAULT_RANGE = 11 + #+END_SRC + + + # -*- mode: org -*- + # vim: syntax=c diff --git a/src/table_of_contents b/src/table_of_contents new file mode 100644 index 0000000..2fbbb05 --- /dev/null +++ b/src/table_of_contents @@ -0,0 +1,9 @@ +qmckl.org +qmckl_context.org +qmckl_error.org +qmckl_precision.org +qmckl_memory.org +qmckl_distance.org +qmckl_ao.org +test_qmckl.org +qmckl_footer.org diff --git a/src/test_qmckl.org b/src/test_qmckl.org new file mode 100644 index 0000000..cc0f169 --- /dev/null +++ b/src/test_qmckl.org @@ -0,0 +1,85 @@ +* QMCkl test :noexport: + + This file is the main program of the unit tests. The tests rely on the + $\mu$unit framework, which is provided as a git submodule. + + First, we use a script to find the list of all the produced test files: + #+NAME: test-files + #+BEGIN_SRC sh :exports none :results value +grep BEGIN_SRC *.org | \ + grep test_qmckl_ | \ + rev | \ + cut -d ' ' -f 1 | \ + rev | \ + sort | \ + uniq + #+END_SRC + + #+RESULTS: test-files + | test_qmckl_ao.c | + | test_qmckl_context.c | + | test_qmckl_distance.c | + | test_qmckl_memory.c | + + We generate the function headers + #+BEGIN_SRC sh :var files=test-files :exports output :results raw +echo "#+NAME: headers" +echo "#+BEGIN_SRC C :tangle no" +for file in $files +do + routine=${file%.c} + echo "MunitResult ${routine}();" +done +echo "#+END_SRC" + #+END_SRC + + #+RESULTS: + #+NAME: headers + #+BEGIN_SRC C :tangle no +MunitResult test_qmckl_ao(); +MunitResult test_qmckl_context(); +MunitResult test_qmckl_distance(); +MunitResult test_qmckl_memory(); + #+END_SRC + + and the required function calls: + #+BEGIN_SRC sh :var files=test-files :exports output :results raw +echo "#+NAME: calls" +echo "#+BEGIN_SRC C :tangle no" +for file in $files +do + routine=${file%.c} + echo " { (char*) \"${routine}\", ${routine}, NULL,NULL,MUNIT_TEST_OPTION_NONE,NULL}," +done +echo "#+END_SRC" + #+END_SRC + + #+RESULTS: + #+NAME: calls + #+BEGIN_SRC C :tangle no + { (char*) "test_qmckl_ao", test_qmckl_ao, NULL,NULL,MUNIT_TEST_OPTION_NONE,NULL}, + { (char*) "test_qmckl_context", test_qmckl_context, NULL,NULL,MUNIT_TEST_OPTION_NONE,NULL}, + { (char*) "test_qmckl_distance", test_qmckl_distance, NULL,NULL,MUNIT_TEST_OPTION_NONE,NULL}, + { (char*) "test_qmckl_memory", test_qmckl_memory, NULL,NULL,MUNIT_TEST_OPTION_NONE,NULL}, + #+END_SRC + + #+BEGIN_SRC C :comments link :noweb yes :tangle test_qmckl.c +#include "qmckl.h" +#include "munit.h" +<> + +int main(int argc, char* argv[MUNIT_ARRAY_PARAM(argc + 1)]) { + static MunitTest test_suite_tests[] = + { + <> + { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } + }; + + static const MunitSuite test_suite = + { + (char*) "", test_suite_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE + }; + + return munit_suite_main(&test_suite, (void*) "µnit", argc, argv); + } + #+END_SRC diff --git a/to_be_processed/.gitignore b/to_be_processed/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/tools/create_doc.sh b/tools/create_doc.sh new file mode 100755 index 0000000..ecb5d7b --- /dev/null +++ b/tools/create_doc.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +set -x + +INPUT=merged.org +if [[ -z $QMCKL_ROOT ]] +then + print "QMCKL_ROOT is not defined" + exit 1 +fi + + +# Install htmlize if needed +[[ -f ${QMCKL_ROOT}/docs/htmlize.el ]] || ( + cd ${QMCKL_ROOT}/docs/ + git clone https://github.com/hniksic/emacs-htmlize + cp emacs-htmlize/htmlize.el . + rm -rf emacs-htmlize + cd - +) + +[[ -f ${QMCKL_ROOT}/docs/htmlize.el ]] || exit 1 + + +# Switch to TMPDIR for easy cleanup +TMPDIR=$(mktemp -d) +${QMCKL_ROOT}/tools/merge_org.sh $TMPDIR/$INPUT +cd $TMPDIR + + +# Create documentation +emacs --batch \ + --load ${QMCKL_ROOT}/docs/htmlize.el \ + --load ${QMCKL_ROOT}/docs/config.el \ + $INPUT -f org-html-export-to-html + +if [[ $? -eq 0 ]] +then + mv index.html ${QMCKL_ROOT}/docs/ + rm -rf $TMPDIR + exit 0 +else + rm -rf $TMPDIR + exit 2 +fi + + diff --git a/tools/create_makefile.sh b/tools/create_makefile.sh new file mode 100755 index 0000000..3e81c7d --- /dev/null +++ b/tools/create_makefile.sh @@ -0,0 +1,98 @@ +#!/bin/bash + +MERGED=merged.org +${QMCKL_ROOT}/tools/merge_org.sh $MERGED + +OUTPUT=Makefile.generated + +# Tangle org files +emacs \ + $MERGED \ + --batch \ + -f org-babel-tangle \ + --kill + +rm $MERGED + + +# Create the list of *.o files to be created + +OBJECTS="" +for i in $(ls qmckl_*.c) ; do + FILE=${i%.c} + OBJECTS="${OBJECTS} ${FILE}.o" +done >> $OUTPUT + +for i in $(ls qmckl_*.f90) ; do + FILE=${i%.f90} + OBJECTS="${OBJECTS} ${FILE}.o" +done >> $OUTPUT + +TESTS="" +for i in $(ls test_qmckl_*.c) ; do + FILE=${i%.c}.o + TESTS="${TESTS} ${FILE}" +done >> $OUTPUT + +TESTS_F="" +for i in $(ls test_qmckl_*.f90) ; do + FILE=${i%.f90}.o + TESTS_F="${TESTS_F} ${FILE}" +done >> $OUTPUT + + +# Write the Makefile + +cat << EOF > $OUTPUT +CC=$CC +CFLAGS=$CFLAGS -I../munit/ + +FC=$FC +FFLAGS=$FFLAGS +OBJECT_FILES=$OBJECTS +TESTS=$TESTS +TESTS_F=$TESTS_F + +LIBS=$LIBS + +libqmckl.so: \$(OBJECT_FILES) + \$(CC) -shared \$(OBJECT_FILES) -o libqmckl.so + +%.o: %.c + \$(CC) \$(CFLAGS) -c \$*.c -o \$*.o + +%.o: %.f90 qmckl_f.o + \$(FC) \$(FFLAGS) -c \$*.f90 -o \$*.o + +test_qmckl: test_qmckl.c libqmckl.so \$(TESTS) \$(TESTS_F) + \$(CC) \$(CFLAGS) -Wl,-rpath,$PWD -L. \ + ../munit/munit.c \$(TESTS) \$(TESTS_F) -lqmckl \$(LIBS) test_qmckl.c -o test_qmckl + +test: test_qmckl + ./test_qmckl + +.PHONY: test +EOF + +for i in $(ls qmckl_*.c) ; do + FILE=${i%.c} + echo "${FILE}.o: ${FILE}.c " *.h +done >> $OUTPUT + +for i in $(ls qmckl_*.f90) ; do + FILE=${i%.f90} + echo "${FILE}.o: ${FILE}.f90" +done >> $OUTPUT + +for i in $(ls test_qmckl_*.c) ; do + FILE=${i%.c} + echo "${FILE}.o: ${FILE}.c qmckl.h" +done >> $OUTPUT + + +for i in $(ls test_qmckl*.f90) ; do + FILE=${i%.f90} + echo "${FILE}.o: ${FILE}.f90" +done >> $OUTPUT + + diff --git a/tools/init.el b/tools/init.el new file mode 100644 index 0000000..bc33d5e --- /dev/null +++ b/tools/init.el @@ -0,0 +1,80 @@ +(package-initialize) +(add-to-list 'package-archives + '("gnu" . "https://elpa.gnu.org/packages/")) +(add-to-list 'package-archives + '("melpa-stable" . "https://stable.melpa.org/packages/")) +(add-to-list 'package-archives + '("melpa" . "https://melpa.org/packages/")) +(setq package-archive-priorities '(("melpa-stable" . 100) + ("melpa" . 50) + ("gnu" . 10))) + +(require 'cl) +(let* ((required-packages + '(htmlize + evil + org-evil + org-bullets + )) + (missing-packages (remove-if #'package-installed-p required-packages))) + (when missing-packages + (message "Missing packages: %s" missing-packages) + (package-refresh-contents) + (dolist (pkg missing-packages) + (package-install pkg) + (message "Package %s has been installed" pkg)))) + +(setq backup-directory-alist + `(("." . ,(concat user-emacs-directory "backups")))) +(setq backup-by-copying t) + +(require 'org) +(setq org-format-latex-options (plist-put org-format-latex-options :scale 1.6)) + +(setq org-hide-leading-stars t) +(setq org-alphabetical-lists t) +(setq org-src-fontify-natively t) +(setq org-src-tab-acts-natively t) +(setq org-src-preserve-indentation t) +(setq org-hide-emphasis-markers nil) +(setq org-pretty-entities nil) +(setq org-confirm-babel-evaluate nil) ;; Do not ask for confirmation all the time!! + +(org-babel-do-load-languages + 'org-babel-load-languages + '( + (emacs-lisp . t) + (shell . t) + (python . t) + (C . t) + (org . t) + (makefile . t) + )) + +(add-hook 'org-babel-after-execute-hook 'org-display-inline-images) +'(indent-tabs-mode nil) + +(require 'evil) +(setq evil-want-C-i-jump nil) +(evil-mode 1) +(global-font-lock-mode t) +(global-superword-mode 1) + +(setq line-number-mode 1) +(setq column-number-mode 1) + +(evil-select-search-module 'evil-search-module 'evil-search) + +(global-set-key (kbd "C-+") 'text-scale-increase) +(global-set-key (kbd "C--") 'text-scale-decrease) + + +(custom-set-variables + ;; custom-set-variables was added by Custom. + ;; If you edit it by hand, you could mess it up, so be careful. + ;; Your init file should contain only one such instance. + ;; If there is more than one, they won't work right. + '(ansi-color-faces-vector + [default default default italic underline success warning error]) + '(custom-enabled-themes (quote (leuven))) +) diff --git a/tools/merge_org.sh b/tools/merge_org.sh new file mode 100755 index 0000000..97b749a --- /dev/null +++ b/tools/merge_org.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +OUTPUT=$1 + +for i in README.org $(cat $QMCKL_ROOT/src/table_of_contents) +do + cat $i >> $1 +done diff --git a/tools/nb_to_org.sh b/tools/nb_to_org.sh new file mode 100755 index 0000000..ab80ebe --- /dev/null +++ b/tools/nb_to_org.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# $ nb_to_org.sh notebook.ipynb +# produces the org-mode file notebook.org + +set -e + +nb=$(basename $1 .ipynb) +jupyter nbconvert --to markdown ${nb}.ipynb --output ${nb}.md +pandoc ${nb}.md -o ${nb}.org +rm ${nb}.md + diff --git a/tools/rename.py b/tools/rename.py new file mode 100755 index 0000000..51bb421 --- /dev/null +++ b/tools/rename.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +""" +Changes the name of a function into all the org files. +This script should be run in the src directory. +""" + +import sys +import os + + +def help(): + print("Syntax : {0} OLD_FUNC_NAME NEW_FUNC_NAME".format(sys.argv[0])) + + + +def replace_in_file(filename, old_func_name, new_func_name): + with open(filename,'r') as f: + text = f.read() + + new_text = text.replace(old_func_name, new_func_name) + + with open(filename,'w') as f: + f.write(new_text) + + +def main(): + if len(sys.argv) != 3: + help() + sys.exit(-1) + old_func_name = sys.argv[1] + new_func_name = sys.argv[2] + + for filename in os.listdir(os.getcwd()): + if filename.endswith(".org"): + replace_in_file(filename, old_func_name, new_func_name) + + print("Done. run git diff to check what has been changed.") + + + + +if __name__ == "__main__": + main()