;;; markdown-mode.el --- Major mode to edit Markdown files in Emacs ;; Copyright (C) 2007, 2008, 2009 Jason Blevins ;; Version: 1.7 ;; Keywords: Markdown major mode ;; Author: Jason Blevins ;; URL: http://jblevins.org/projects/markdown-mode/ ;; This file is not part of GNU Emacs. ;; 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; if not, write to the Free Software ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ;;; Commentary: ;; markdown-mode is a major mode for editing [Markdown][]-formatted ;; text files in GNU Emacs. markdown-mode is free software, licensed ;; under the GNU GPL. ;; ;; [Markdown]: http://daringfireball.net/projects/markdown/ ;; ;; The latest stable version is markdown-mode 1.7, released on October 1, 2009: ;; ;; * [markdown-mode.el][] ;; * [Screenshot][] ;; * [Release notes][] ;; ;; markdown-mode is also available in the Debian ;; [emacs-goodies-el](http://packages.debian.org/emacs-goodies-el) ;; package (beginning with revision 27.0-1) and the OpenBSD ;; [textproc/markdown-mode](http://pkgsrc.se/textproc/markdown-mode) package. ;; ;; [markdown-mode.el]: http://jblevins.org/projects/markdown-mode/markdown-mode.el ;; [screenshot]: http://jblevins.org/projects/markdown-mode/screenshots/20080604-001.png ;; [release notes]: http://jblevins.org/projects/markdown-mode/rev-1-7 ;; The latest development version can be downloaded directly ;; ([markdown-mode.el][devel.el]) or it can be obtained from the ;; (browsable and clonable) Git repository at ;; . The entire repository, ;; including the full project history, can be cloned via the Git protocol ;; by running ;; ;; git clone git://jblevins.org/git/markdown-mode.git ;; ;; [devel.el]: http://jblevins.org/git/markdown-mode.git/plain/markdown-mode.el ;;; Dependencies: ;; markdown-mode requires easymenu, a standard package since GNU Emacs ;; 19 and XEmacs 19, which provides a uniform interface for creating ;; menus in GNU Emacs and XEmacs. ;;; Installation: ;; Make sure to place `markdown-mode.el` somewhere in the load-path and add ;; the following lines to your `.emacs` file to associate markdown-mode ;; with `.text` files: ;; ;; (autoload 'markdown-mode "markdown-mode.el" ;; "Major mode for editing Markdown files" t) ;; (setq auto-mode-alist ;; (cons '("\\.text" . markdown-mode) auto-mode-alist)) ;; ;; There is no consensus on an official file extension so change `.text` to ;; `.mdwn`, `.md`, `.mdt`, or whatever you call your markdown files. ;;; Customization: ;; Although no configuration is *necessary* there are a few things ;; that can be customized. The `M-x customize-mode` command ;; provides an interface to all of the possible customizations: ;; ;; * `markdown-command` - the command used to run Markdown ;; (default: `markdown`). ;; ;; * `markdown-hr-length` - the length of horizontal rules ;; (default: `5`). ;; ;; * `markdown-bold-underscore` - set to a non-nil value to use two ;; underscores for bold instead of two asterisks (default: `nil`). ;; ;; * `markdown-italic-underscore` - set to a non-nil value to use ;; underscores for italic instead of asterisks (default: `nil`). ;; ;; * `markdown-indent-function` - the function to use for automatic ;; indentation (default: `markdown-indent-line`). ;; ;; * `markdown-indent-on-enter` - set to a non-nil value to ;; automatically indent new lines when the enter key is pressed ;; (default: `t`) ;; ;; * `markdown-uri-types` - a list of protocols for URIs that ;; `markdown-mode' should highlight. ;; ;; * `markdown-enable-math` - syntax highlighting for ;; LaTeX fragments (default: `nil`). ;; ;; Additionally, the faces used for syntax highlighting can be modified to ;; your liking by issuing `M-x customize-group RET markdown-faces` ;; or by using the "Markdown Faces" link at the bottom of the mode ;; customization screen. ;;; Usage: ;; Keybindings are grouped by prefixes based on their function. For ;; example, commands dealing with headers begin with `C-c C-t`. The ;; primary commands in each group will are described below. You can ;; obtain a list of all keybindings by pressing `C-c C-h`. ;; ;; * Anchors: `C-c C-a` ;; ;; `C-c C-a l` inserts inline links of the form `[text](url)`. If ;; there is an active region, text in the region is used for the ;; link text. `C-c C-a w` acts similarly for wiki links of the ;; form `[[WikiLink]]`. ;; ;; * Commands: `C-c C-c` ;; ;; `C-c C-c m` will run Markdown on the current buffer and preview ;; the output in another buffer while `C-c C-c p` runs Markdown on ;; the current buffer and previews the output in a browser. ;; ;; `C-c C-c c` will check for undefined references. If there are ;; any, a small buffer will open with a list of undefined ;; references and the line numbers on which they appear. In Emacs ;; 22 and greater, selecting a reference from this list and ;; pressing `RET` will insert an empty reference definition at the ;; end of the buffer. Similarly, selecting the line number will ;; jump to the corresponding line. ;; ;; * Images: `C-c C-i` ;; ;; `C-c C-i i` inserts an image, using the active region (if any) ;; as the alt text. ;; ;; * Physical styles: `C-c C-p` ;; ;; These commands all act on text in the active region, if any, ;; and insert empty markup fragments otherwise. `C-c C-p b` makes ;; the selected text bold, `C-c C-p f` formats the region as ;; fixed-width text, and `C-c C-p i` is used for italic text. ;; ;; * Logical styles: `C-c C-s` ;; ;; These commands all act on text in the active region, if any, ;; and insert empty markup fragments otherwise. Logical styles ;; include blockquote (`C-c C-s b`), preformatted (`C-c C-s p`), ;; code (`C-c C-s c`), emphasis (`C-c C-s e`), and strong (`C-c ;; C-s s`). ;; ;; * Headers: `C-c C-t` ;; ;; All header commands use text in the active region, if any, as ;; the header text. To insert an atx or hash style level-n ;; header, press `C-c C-t n` where n is between 1 and 6. For a ;; top-level setext or underline style header press `C-c C-t t` ;; (mnemonic: title) and for a second-level underline-style header ;; press `C-c C-t s` (mnemonic: section). ;; ;; * Other commands ;; ;; `C-c -` inserts a horizontal rule. ;; ;; Many of the commands described above behave differently depending on ;; whether Transient Mark mode is enabled or not. When it makes sense, ;; if Transient Mark mode is on and a region is active, the command ;; applies to the text in the region (e.g., `C-c C-p b` makes the region ;; bold). For users who prefer to work outside of Transient Mark mode, ;; in Emacs 22 it can be enabled temporarily by pressing `C-SPC C-SPC`. ;; ;; When applicable, commands that specifically act on the region even ;; outside of Transient Mark mode have the same keybinding as the with ;; the exception of an additional `C-` prefix. For example, ;; `markdown-insert-blockquote` is bound to `C-c C-s b` and only acts on ;; the region in Transient Mark mode while `markdown-blockquote-region` ;; is bound to `C-c C-s C-b` and always applies to the region (when ;; nonempty). ;; ;; markdown-mode supports outline-minor-mode as well as org-mode-style ;; visibility cycling for atx- or hash-style headers. There are two ;; types of visibility cycling: Pressing `S-TAB` cycles globally between ;; the table of contents view (headers only), outline view (top-level ;; headers only), and the full document view. Pressing `TAB` while the ;; point is at a header will cycle through levels of visibility for the ;; subtree: completely folded, visible children, and fully visible. ;; Note that mixing hash and underline style headers will give undesired ;; results. ;;; Extensions: ;; Besides supporting the basic Markdown syntax, markdown-mode also ;; includes syntax highlighting for `[[Wiki Links]]` by default. ;; ;; [SmartyPants][] support is possible by customizing `markdown-command`. ;; If you install `SmartyPants.pl` at, say, `/usr/local/bin/smartypants`, ;; then you can set `markdown-command` to `"markdown | smartypants"`. ;; You can do this either by using `M-x customize-group markdown` ;; or by placing the following in your `.emacs` file: ;; ;; (defun markdown-custom () ;; "markdown-mode-hook" ;; (setq markdown-command "markdown | smartypants")) ;; (add-hook 'markdown-mode-hook '(lambda() (markdown-custom))) ;; ;; [SmartyPants]: http://daringfireball.net/projects/smartypants/ ;; ;; Experimental syntax highlighting for mathematical expressions written ;; in LaTeX (only expressions denoted by `$..$`, `$$..$$`, or `\[..\]`) ;; can be enabled by setting `markdown-enable-math` to a non-nil value, ;; either via customize or by placing `(setq markdown-enable-itex t)` ;; in `.emacs`, and restarting Emacs. ;;; Acknowledgments: ;; markdown-mode has benefited greatly from the efforts of the ;; following people: ;; ;; * Cyril Brulebois for Debian packaging. ;; * Conal Elliott for a font-lock regexp patch. ;; * Edward O'Connor for a font-lock regexp fix. ;; * Greg Bognar for menus and a patch. ;; * Daniel Burrows for filing Debian bug #456592. ;; * Peter S. Galbraith for maintaining emacs-goodies-el. ;; * Dmitry Dzhus for reference checking functions. ;; * Bryan Kyle for indentation code. ;; * intrigeri for face customizations. ;; * Ankit Solanki for longlines.el compatibility. ;; * Hilko Bengen for proper XHTML output. ;; * Jose A. Ortega Ruiz for Emacs 23 fixes. ;; * Alec Resnick for bug reports. ;; * Peter Williams for fill-paragraph enhancements. ;;; Bugs: ;; Although markdown-mode is developed and tested primarily using ;; GNU Emacs 23, compatibility with GNU Emacs 21 and 22 is also a ;; priority. ;; ;; markdown-mode's syntax highlighting is accomplished using the ;; search-based fontification features of Emacs through a series of ;; regular expressions. Unfortunately, Emacs has trouble highlighting ;; multi-line constructs using regular expressions and this creates ;; several syntax-highlighting quirks such as mistaking indented ;; lists for preformatted text, etc. Making markdown-mode's syntax ;; highlighting more robust through the use of matching functions ;; or syntactic font lock is a high-priority item for future work. ;; ;; If you find any bugs not mentioned here, please construct a test ;; case and/or a patch and email me at . ;;; History: ;; markdown-mode was written and is maintained by Jason Blevins. The ;; first version was released on May 24, 2007. ;; ;; * 2007-05-24: Version 1.1 ;; * 2007-05-25: Version 1.2 ;; * 2007-06-05: [Version 1.3][] ;; * 2007-06-29: Version 1.4 ;; * 2008-05-24: [Version 1.5][] ;; * 2008-06-04: [Version 1.6][] ;; * 2008-10-01: [Version 1.7][] ;; ;; [Version 1.3]: http://jblevins.org/projects/markdown-mode/rev-1-3 ;; [Version 1.5]: http://jblevins.org/projects/markdown-mode/rev-1-5 ;; [Version 1.6]: http://jblevins.org/projects/markdown-mode/rev-1-6 ;; [Version 1.7]: http://jblevins.org/projects/markdown-mode/rev-1-7 ;;; Code: (require 'easymenu) (require 'outline) ;;; Customizable variables ==================================================== ;; Current revision (defconst markdown-mode-version "1.7-dev") ;; A hook for users to run their own code when the mode is loaded. (defvar markdown-mode-hook nil) ;;; Customizable variables ==================================================== (defgroup markdown nil "Major mode for editing text files in Markdown format." :prefix "markdown-" :group 'wp :link '(url-link "http://jblevins.org/projects/markdown-mode/")) (defcustom markdown-command "markdown" "Command to run markdown." :group 'markdown :type 'string) (defcustom markdown-hr-length 5 "Length of horizonal rules." :group 'markdown :type 'integer) (defcustom markdown-bold-underscore nil "Use two underscores for bold instead of two asterisks." :group 'markdown :type 'boolean) (defcustom markdown-italic-underscore nil "Use underscores for italic instead of asterisks." :group 'markdown :type 'boolean) (defcustom markdown-indent-function 'markdown-indent-line "Function to use to indent." :group 'markdown :type 'function) (defcustom markdown-indent-on-enter t "Automatically indent new lines when enter key is pressed." :group 'markdown :type 'boolean) (defcustom markdown-uri-types '("acap" "cid" "data" "dav" "fax" "file" "ftp" "gopher" "http" "https" "imap" "ldap" "mailto" "mid" "modem" "news" "nfs" "nntp" "pop" "prospero" "rtsp" "service" "sip" "tel" "telnet" "tip" "urn" "vemmi" "wais") "Link types for syntax highlighting of URIs." :group 'markdown :type 'list) (defcustom markdown-enable-math nil "Syntax highlighting for inline LaTeX expressions. This will not take effect until Emacs is restarted." :group 'markdown :type 'boolean) (defcustom markdown-css-path nil "CSS file to include in the output XHTML" :group 'markdown :type 'string) ;;; Font lock ================================================================= (require 'font-lock) (defvar markdown-italic-face 'markdown-italic-face "Face name to use for italic text.") (defvar markdown-bold-face 'markdown-bold-face "Face name to use for bold text.") (defvar markdown-header-face 'markdown-header-face "Face name to use as a base for headers.") (defvar markdown-header-face-1 'markdown-header-face-1 "Face name to use for level-1 headers.") (defvar markdown-header-face-2 'markdown-header-face-2 "Face name to use for level-2 headers.") (defvar markdown-header-face-3 'markdown-header-face-3 "Face name to use for level-3 headers.") (defvar markdown-header-face-4 'markdown-header-face-4 "Face name to use for level-4 headers.") (defvar markdown-header-face-5 'markdown-header-face-5 "Face name to use for level-5 headers.") (defvar markdown-header-face-6 'markdown-header-face-6 "Face name to use for level-6 headers.") (defvar markdown-inline-code-face 'markdown-inline-code-face "Face name to use for inline code.") (defvar markdown-list-face 'markdown-list-face "Face name to use for list markers.") (defvar markdown-blockquote-face 'markdown-blockquote-face "Face name to use for blockquote.") (defvar markdown-pre-face 'markdown-pre-face "Face name to use for preformatted text.") (defvar markdown-link-face 'markdown-link-face "Face name to use for links.") (defvar markdown-reference-face 'markdown-reference-face "Face name to use for reference.") (defvar markdown-url-face 'markdown-url-face "Face name to use for URLs.") (defvar markdown-link-title-face 'markdown-link-title-face "Face name to use for reference link titles.") (defvar markdown-comment-face 'markdown-comment-face "Face name to use for HTML comments.") (defvar markdown-math-face 'markdown-math-face "Face name to use for LaTeX expressions.") (defgroup markdown-faces nil "Faces used in Markdown Mode" :group 'markdown :group 'faces) (defface markdown-italic-face '((t :inherit font-lock-variable-name-face :italic t)) "Face for italic text." :group 'markdown-faces) (defface markdown-bold-face '((t :inherit font-lock-variable-name-face :bold t)) "Face for bold text." :group 'markdown-faces) (defface markdown-header-face '((t :inherit font-lock-function-name-face :weight bold)) "Base face for headers." :group 'markdown-faces) (defface markdown-header-face-1 '((t :inherit markdown-header-face)) "Face for level-1 headers." :group 'markdown-faces) (defface markdown-header-face-2 '((t :inherit markdown-header-face)) "Face for level-2 headers." :group 'markdown-faces) (defface markdown-header-face-3 '((t :inherit markdown-header-face)) "Face for level-3 headers." :group 'markdown-faces) (defface markdown-header-face-4 '((t :inherit markdown-header-face)) "Face for level-4 headers." :group 'markdown-faces) (defface markdown-header-face-5 '((t :inherit markdown-header-face)) "Face for level-5 headers." :group 'markdown-faces) (defface markdown-header-face-6 '((t :inherit markdown-header-face)) "Face for level-6 headers." :group 'markdown-faces) (defface markdown-inline-code-face '((t :inherit font-lock-constant-face)) "Face for inline code." :group 'markdown-faces) (defface markdown-list-face '((t :inherit font-lock-builtin-face)) "Face for list item markers." :group 'markdown-faces) (defface markdown-blockquote-face '((t :inherit font-lock-doc-face)) "Face for blockquote sections." :group 'markdown-faces) (defface markdown-pre-face '((t :inherit font-lock-constant-face)) "Face for preformatted text." :group 'markdown-faces) (defface markdown-link-face '((t :inherit font-lock-keyword-face)) "Face for links." :group 'markdown-faces) (defface markdown-reference-face '((t :inherit font-lock-type-face)) "Face for link references." :group 'markdown-faces) (defface markdown-url-face '((t :inherit font-lock-string-face)) "Face for URLs." :group 'markdown-faces) (defface markdown-link-title-face '((t :inherit font-lock-comment-face)) "Face for reference link titles." :group 'markdown-faces) (defface markdown-comment-face '((t :inherit font-lock-comment-face)) "Face for HTML comments." :group 'markdown-faces) (defface markdown-math-face '((t :inherit font-lock-string-face)) "Face for LaTeX expressions." :group 'markdown-faces) (defconst markdown-regex-link-inline "\\(!?\\[[^]]*?\\]\\)\\(([^\\)]*)\\)" "Regular expression for a [text](file) or an image link ![text](file).") (defconst markdown-regex-link-reference "\\(!?\\[[^]]+?\\]\\)[ ]?\\(\\[[^]]*?\\]\\)" "Regular expression for a reference link [text][id].") (defconst markdown-regex-reference-definition "^ \\{0,3\\}\\(\\[.+?\\]\\):\\s *\\(.*?\\)\\s *\\( \"[^\"]*\"$\\|$\\)" "Regular expression for a link definition [id]: ...") (defconst markdown-regex-header-1-atx "^\\(# \\)\\(.*?\\)\\($\\| #+$\\)" "Regular expression for level 1 atx-style (hash mark) headers.") (defconst markdown-regex-header-2-atx "^\\(## \\)\\(.*?\\)\\($\\| #+$\\)" "Regular expression for level 2 atx-style (hash mark) headers.") (defconst markdown-regex-header-3-atx "^\\(### \\)\\(.*?\\)\\($\\| #+$\\)" "Regular expression for level 3 atx-style (hash mark) headers.") (defconst markdown-regex-header-4-atx "^\\(#### \\)\\(.*?\\)\\($\\| #+$\\)" "Regular expression for level 4 atx-style (hash mark) headers.") (defconst markdown-regex-header-5-atx "^\\(##### \\)\\(.*?\\)\\($\\| #+$\\)" "Regular expression for level 5 atx-style (hash mark) headers.") (defconst markdown-regex-header-6-atx "^\\(###### \\)\\(.*?\\)\\($\\| #+$\\)" "Regular expression for level 6 atx-style (hash mark) headers.") (defconst markdown-regex-header-1-setext "^\\(.*\\)\n\\(===+\\)$" "Regular expression for level 1 setext-style (underline) headers.") (defconst markdown-regex-header-2-setext "^\\(.*\\)\n\\(---+\\)$" "Regular expression for level 2 setext-style (underline) headers.") (defconst markdown-regex-hr "^\\(\\*[ ]?\\*[ ]?\\*[ ]?[\\* ]*\\|-[ ]?-[ ]?-[--- ]*\\)$" "Regular expression for matching Markdown horizontal rules.") (defconst markdown-regex-code "\\(^\\|[^\\]\\)\\(\\(`\\{1,2\\}\\)\\([^ \\]\\|[^ ].*?[^ \\]\\)\\3\\)" "Regular expression for matching inline code fragments.") (defconst markdown-regex-pre "^\\( \\|\t\\).*$" "Regular expression for matching preformatted text sections.") (defconst markdown-regex-list "^[ \t]*\\([0-9]+\\.\\|[\\*\\+-]\\) " "Regular expression for matching list markers.") (defconst markdown-regex-bold "\\(^\\|[^\\]\\)\\(\\([*_]\\{2\\}\\)\\(.\\|\n\\)*?[^\\ ]\\3\\)" "Regular expression for matching bold text.") (defconst markdown-regex-italic "\\(^\\|[^\\]\\)\\(\\([*_]\\)\\([^ \\]\\3\\|[^ ]\\(.\\|\n\\)*?[^\\ ]\\3\\)\\)" "Regular expression for matching italic text.") (defconst markdown-regex-blockquote "^>.*$" "Regular expression for matching blockquote lines.") (defconst markdown-regex-line-break " $" "Regular expression for matching line breaks.") (defconst markdown-regex-wiki-link "\\[\\[[^]]+\\]\\]" "Regular expression for matching wiki links.") (defconst markdown-regex-uri (concat "\\(" (mapconcat 'identity markdown-uri-types "\\|") "\\):[^]\t\n\r<>,;() ]+") "Regular expression for matching inline URIs.") (defconst markdown-regex-angle-uri (concat "\\(<\\)\\(" (mapconcat 'identity markdown-uri-types "\\|") "\\):[^]\t\n\r<>,;()]+\\(>\\)") "Regular expression for matching inline URIs in angle brackets.") (defconst markdown-regex-email "<\\(\\sw\\|\\s_\\|\\s.\\)+@\\(\\sw\\|\\s_\\|\\s.\\)+>" "Regular expression for matching inline email addresses.") (defconst markdown-regex-latex-expression "\\(^\\|[^\\]\\)\\(\\$\\($\\([^\\$]\\|\\\\.\\)*\\$\\|\\([^\\$]\\|\\\\.\\)*\\)\\$\\)" "Regular expression for itex $..$ or $$..$$ math mode expressions.") (defconst markdown-regex-latex-display "^\\\\\\[\\(.\\|\n\\)*?\\\\\\]$" "Regular expression for itex \[..\] display mode expressions.") (defconst markdown-regex-list-indent "^\\(\\s *\\)\\([0-9]+\\.\\|[\\*\\+-]\\)\\(\\s +\\)" "Regular expression for matching indentation of list items.") ; From html-helper-mode (defun markdown-match-comments (last) "Matches HTML comments from the point to LAST" (cond ((search-forward "") (make-local-variable 'comment-start-skip) (setq comment-start-skip "