   :group 'markdown
   :type 'string)
 
-(defcustom markdown-hr-length 5
-  "Length of horizonal rules."
+(defcustom markdown-command-needs-filename nil
+  "Set to non-nil if `markdown-command' does not accept input from stdin.
+Instead, it will be passed a filename as the final command-line
+option.  As a result, you will only be able to run Markdown from
+buffers which are visiting a file."
   :group 'markdown
-  :type 'integer)
+  :type 'boolean)
+
+(defcustom markdown-hr-string "* * * * *"
+  "String to use for horizonal rules."
+  :group 'markdown
+  :type 'string)
 
 (defcustom markdown-bold-underscore nil
   "Use two underscores for bold instead of two asterisks."

  :group 'markdown
  :type 'boolean)


;;; Font lock =================================================================

(require 'font-lock)

(defgroup markdown-faces nil
  "Faces used in Markdown Mode"
  :group 'markdown
  :group 'faces)

(defcustom markdown-italic-face 'font-lock-variable-name-face
  "Italic text."
  :group 'markdown-faces
  :type '(face))

(defcustom markdown-bold-face 'font-lock-type-face
  "Bold text"
  :group 'markdown-faces
  :type '(face))

(defcustom markdown-header-face 'font-lock-function-name-face
  "Headers"
  :group 'markdown-faces
  :type '(face))

(defcustom markdown-inline-code-face 'font-lock-builtin-face
  "Inline code"
  :group 'markdown-faces
  :type '(face))

(defcustom markdown-list-face 'font-lock-variable-name-face
  "List item markers"
  :group 'markdown-faces
  :type '(face))

(defcustom markdown-blockquote-face 'font-lock-comment-face
  "Blockquote sections and preformatted text"
  :group 'markdown-faces
  :type '(face))

(defcustom markdown-link-face 'font-lock-constant-face
  "Link text"
  :group 'markdown-faces
  :type '(face))

(defcustom markdown-reference-face 'font-lock-type-face
  "Link references"
  :group 'markdown-faces
  :type '(face))

(defcustom markdown-url-face 'font-lock-string-face
  "URLs"
  :group 'markdown-faces
  :type '(face))

(defcustom markdown-math-face 'font-lock-builtin-face
  "LaTeX expressions"
  :group 'markdown-faces
  :type '(face))

(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-atx
  "^\\(#+ \\)\\(.*?\\)\\($\\| #+$\\)"
  "Regular expression for atx-style (hash mark) headers")

(defconst markdown-regex-header-setext
  "^\\(.*\\)\n\\(===+\\|---+\\)$"
  "Regular expression for 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
  "^    .*$"
  "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
  "<\\(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\\)://[^>]*>"
  "Regular expression for matching inline URIs")

(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-mode-font-lock-keywords-basic
  (list
   (cons markdown-regex-code '(2 markdown-inline-code-face))
   (cons markdown-regex-pre markdown-blockquote-face)
   (cons markdown-regex-header-setext markdown-header-face)
   (cons markdown-regex-header-atx markdown-header-face)
   (cons markdown-regex-list markdown-list-face)
   (cons markdown-regex-hr markdown-header-face)
   (cons markdown-regex-link-inline
         '((1 markdown-link-face t)
           (2 markdown-url-face t)))

the blockquote text."
  (interactive)
  (if (and (boundp 'transient-mark-mode) transient-mark-mode mark-active)
      (markdown-blockquote-region (region-beginning) (region-end))
    (insert "> ")))

(defun markdown-block-region (beg end prefix)
  "Format the region using a block prefix.
Arguments BEG and END specify the beginning and end of the
region.  The characters PREFIX will appear at the beginning
of each line."
  (if mark-active
      (save-excursion
        ;; Ensure that there is a leading blank line
        (goto-char beg)
        (when (and (>= (point) (+ (point-min) 2))
                   (not (looking-back "\n\n" 2)))
          (insert "\n")
          (setq beg (1+ beg))
          (setq end (1+ end)))
        ;; Move back before any blank lines at the end
        (goto-char end)
        (while (and (looking-back "\n" 1)
                    (not (equal (point) (point-min))))
          (backward-char)
          (setq end (1- end)))
        ;; Ensure that there is a trailing blank line
        (goto-char end)
        (if (not (or (looking-at "\n\n")
                     (and (equal (1+ end) (point-max)) (looking-at "\n"))))
          (insert "\n"))
        ;; Insert PREFIX
        (goto-char beg)
        (beginning-of-line)
        (while (< (point-at-bol) end)
          (insert prefix)
          (setq end (+ (length prefix) end))
          (forward-line)))))

(defun markdown-blockquote-region (beg end)
  "Blockquote the region.
Arguments BEG and END specify the beginning and end of the region."
  (interactive "*r")
  (markdown-block-region beg end "> "))

(defun markdown-insert-pre ()
  "Start a preformatted section (or apply to the region).
