public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* Start of literate programming emacs mode
@ 2010-05-02 14:04 Ivan Lazar Miljenovic
  2010-05-02 16:06 ` John Velman
       [not found] ` <874oiq9z85.fsf-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
  0 siblings, 2 replies; 5+ messages in thread
From: Ivan Lazar Miljenovic @ 2010-05-02 14:04 UTC (permalink / raw)
  To: pandoc-discuss


--=-=-=


Hi all,

I've wasted my weekend hacking on a multi-mode for Emacs for use with
markdown-mode and literate-haskell-mode along with other modes so that
we can take advantage of the various syntax highlighting, completion,
etc. when writing markdown with code chunks.

The mode uses [multi-mode] and is based upon haskell-latex.el in the
same directory.  I first tried mmm-mode, but found out that it doesn't
support syntax highlighting with recent versions of emacs; I then
switched to mumamo-mode at the advice of quicksilver which was a pain to
use: first I got it working as long as you had at most one code block
[any more and it barfed], then realised I had an old version and updated
it only to find that now it couldn't tell when the code block ended,
probably because it got confused between the starts and ends.  In the
end, multi-mode does some weird things (see below) but seems to work.

[multi-mode]: http://www.loveshack.ukfsn.org/emacs/multi-mode.el

Some comments:

* Only works on named code blocks.  However, it picks up the name of the
  language used and if you set it up (via customisation) then it will
  automatically choose the correct major mode for that code block.  Only
  Haskell (using literate-haskell so that I can do bird-style code
  blocks) is predefined.  Unnamed code blocks will probably cause
  problems (how do you tell whether the given region is an un-named code
  block or is just between two un-named code blocks).

* Cannot accept extra lines of tildas inside code blocks.

* multi-mode seems to create extra virtual buffers for each code block,
  (e.g. not only is there Foo.text but also Foo.text<2>, Foo.text<3>,
  etc.).

* Documentation, etc. isn't complete yet (hasn't been touched really).

You just need to do (require 'markdown-code-mode) and then create
associations with markdown-code-mode.  To get it working with other
languages, do customize-group on markdown-code and add a new name/mode
pair.

This is my first emacs mode, so be gentle.


--=-=-=


-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic@gmail.com
IvanMiljenovic.wordpress.com

--=-=-=
Content-Type: application/emacs-lisp
Content-Disposition: attachment; filename=markdown-code-mode.el
Content-Transfer-Encoding: quoted-printable

;;; markdown-code-mode.el

;; Copyright (C) 2003, 2007, 2009  Free Software Foundation, Inc.

;; This file 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 3 of the License, or
;; (at your option) any later version.

;; This file 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 GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This provides a mode for editing literate Haskell with the LaTeX
;; \begin{code}...\end{code} convention (but not the `Bird tracks'
;; `inverse comment' convention).
;;
;; It originally was meant to use my simple Haskell mode rather than
;; the one from haskell.org.  Versions of the haskell.org
;; haskell-indent.el prior to v2.1 (?) need to be modified to work
;; with this, due to the narrowing used by multi-mode around the
;; indentation command.  That has been fixed subsequently, so the
;; current version can be used with this code.  However, note that the
;; recommended way of installing it adds `literate-haskell-mode' to
;; `auto-mode-alist' for .lhs files, whereas we want `markdown-code'.
;; Thus you probably want to do something like this, assuming
;; markdown-code is on `load-path' and $haskell_mode_path is the path
;; name of the markdown-code directory:
;;
;;   (load "$haskell_mode_path/haskell-site-file.el")
;;   (add-to-list 'auto-mode-alist '("\\.l[gh]s\\'" . markdown-code-mode))
;;   (autoload 'markdown-code-mode "markdown-code")
;;
;; Alternatively, you can customize `haskell-mode-hook' by adding
;; `haskell-markdown-maybe' to it.
;;
;; You can use the Emacs Markdown mode or AUCTeX.  The latter causes
;; trouble which is hacked around in the multi-mode.el (revision
;; 1.6+).  There are problems with font-locking in Emacs 21, both with
;; AUCTeX and the built-in mode, which seem to be fixed in Emacs 22 --
;; single `$'s in Haskell parts cause subsequent Markdown to be fontified
;; as maths.

;;; Code:

(require 'multi-mode)
(require 'markdown-mode)
(require 'haskell-mode)

(defcustom markdown-code-modes
  '(
    ("haskell" . literate-haskell-mode)
    )
  "Mapping from name in markdown code block to major mode."
  :type '(alist :key-type (string :tag "Block name") :value-type function)
  :group 'markdown-code)

(defvar markdown-code-block-start-re "^~~~+"
  "Regular expression to detect a code block in markdown.")

(defvar markdown-code-block-re
  (concat markdown-code-block-start-re "[ \\t]*$")
  "Regular expression to detect a code block on its own")

(defvar markdown-end-code-block-re ; "^~~~+[ \\t]*$"
  (concat markdown-code-block-start-re "[ \\t]*$")
  "blah")

(defvar markdown-code-block-name-spec-re
  "[ \\t]*{\\.\\(\\w+\\)\\(?:[ \\t]+\\.\\w+\\)*}[ \\t]*$"
  "Regular expression to determine the name for a named code block in markd=
own.")

(defvar markdown-named-code-block-re
  (concat markdown-code-block-start-re markdown-code-block-name-spec-re)
  "Regular expression to detect a named code block in markdown.")

(defvar markdown-code-block-boundary-re
  ; have to do named one first
  ; FIXME: can be expressed better.
  (concat "\\(?:" markdown-named-code-block-re
          "\\|\\(" markdown-end-code-block-re "\\)\\)")
  "Regular expression to detect either the beginning or an end of a code bl=
ock.")

(defun markdown-code-chunk-region (pos)
  "Determine type and limit of current chunk at POS.
Return (MODE START END), where MODE is `haskell-mode' or `markdown-mode'
and START and END are the limits of the chunk."
  (let ((mode 'markdown-mode)
        (start (point-min))
        (end (point-max)))
    (save-excursion
      (save-restriction
        (widen)
        (goto-char pos)
        ;; Look for the beginning or end of a code block.
        (cond
         ((save-excursion
            (beginning-of-line)
            (looking-at markdown-code-block-boundary-re))
          (if (match-beginning 2)	; end of block
              (progn
                (setq start (point))
                (if (re-search-forward markdown-named-code-block-re nil t)
                    (setq end (line-end-position))))
            ;; beginning of code block
            (setq end (1- (line-beginning-position 2)))
            (if (re-search-backward markdown-end-code-block-re nil t)
                (setq start (match-beginning 0)))))
         ;; Between \begin and \end (in either order).
         ((re-search-backward markdown-code-block-boundary-re
                              nil t)
          (if (match-beginning 2)	; end of block
              (progn
                (setq start (match-beginning 0))
                (if (re-search-forward markdown-named-code-block-re nil t)
                    (setq end (line-end-position))))
            ;; beginning of line
            (let ((exc-mode 'markdown-mode))
              (let ((lang (match-string 1)))
                (when lang
                  (setq exc-mode (catch 'major
                                   (dolist (rec markdown-code-modes)
                                     (when (string-match (car rec) lang)
                                       (throw 'major (cdr rec))))
                                   nil))))
              (setq start (1- (line-beginning-position 2))
                    mode exc-mode)
              (if (re-search-forward markdown-end-code-block-re nil t)
                  (setq end (1- (match-beginning 0)))))))
         ;; Doc chunk at start.
         (t
          (beginning-of-line)
          (if (re-search-forward markdown-named-code-block-re nil t)
              (setq end (point))
            (setq end (point-max)
                  mode 'literate-haskell-mode))))
        (multi-make-list mode start end)))))

(defun add-chunk-mode (mode-list)
  "Takes a list of (ModeName . Mode) values, and converts them to
a form suitable for `multi-mode-alist'."
  (if (not mode-list)
      nil
    (cons
     (cons (cdar mode-list) 'markdown-code-chunk-region)
     (add-chunk-mode (cdr mode-list)))))

;;;###autoload
(defun markdown-code-mode ()
  "Mode for editing `literate Haskell' with Markdown conventions."
  (interactive)
  (set (make-local-variable 'multi-mode-alist)
       (cons '(markdown-mode . nil)
             (add-chunk-mode markdown-code-modes)))
  (multi-mode-install-modes))

(provide 'markdown-code-mode)
;;; markdown-code-mode.el ends here

--=-=-=
Content-Type: text/plain; charset=ISO-8859-1
Content-Disposition: inline

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To post to this group, send email to pandoc-discuss@googlegroups.com.
To unsubscribe from this group, send email to pandoc-discuss+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pandoc-discuss?hl=en.


--=-=-=--

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Start of literate programming emacs mode
  2010-05-02 14:04 Start of literate programming emacs mode Ivan Lazar Miljenovic
@ 2010-05-02 16:06 ` John Velman
  2010-05-03  0:13   ` Ivan Miljenovic
       [not found] ` <874oiq9z85.fsf-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
  1 sibling, 1 reply; 5+ messages in thread
From: John Velman @ 2010-05-02 16:06 UTC (permalink / raw)
  To: pandoc-discuss


Sounds complicated.  I've had a couple of brief flirtations with emacs,
including OS X Auamacs, but always go back to vim.  :-)

Best,

John V.

On Mon, May 03, 2010 at 12:04:26AM +1000, Ivan Lazar Miljenovic wrote:
> 
> Hi all,
> 
> I've wasted my weekend hacking on a multi-mode for Emacs for use with
> markdown-mode and literate-haskell-mode along with other modes so that
> we can take advantage of the various syntax highlighting, completion,
> etc. when writing markdown with code chunks.
> 
> The mode uses [multi-mode] and is based upon haskell-latex.el in the
> same directory.  I first tried mmm-mode, but found out that it doesn't
> support syntax highlighting with recent versions of emacs; I then
> switched to mumamo-mode at the advice of quicksilver which was a pain to
> use: first I got it working as long as you had at most one code block
> [any more and it barfed], then realised I had an old version and updated
> it only to find that now it couldn't tell when the code block ended,
> probably because it got confused between the starts and ends.  In the
> end, multi-mode does some weird things (see below) but seems to work.
> 
> [multi-mode]: http://www.loveshack.ukfsn.org/emacs/multi-mode.el
> 
> Some comments:
> 
> * Only works on named code blocks.  However, it picks up the name of the
>   language used and if you set it up (via customisation) then it will
>   automatically choose the correct major mode for that code block.  Only
>   Haskell (using literate-haskell so that I can do bird-style code
>   blocks) is predefined.  Unnamed code blocks will probably cause
>   problems (how do you tell whether the given region is an un-named code
>   block or is just between two un-named code blocks).
> 
> * Cannot accept extra lines of tildas inside code blocks.
> 
> * multi-mode seems to create extra virtual buffers for each code block,
>   (e.g. not only is there Foo.text but also Foo.text<2>, Foo.text<3>,
>   etc.).
> 
> * Documentation, etc. isn't complete yet (hasn't been touched really).
> 
> You just need to do (require 'markdown-code-mode) and then create
> associations with markdown-code-mode.  To get it working with other
> languages, do customize-group on markdown-code and add a new name/mode
> pair.
> 
> This is my first emacs mode, so be gentle.
> 

> 
> -- 
> Ivan Lazar Miljenovic
> Ivan.Miljenovic@gmail.com
> IvanMiljenovic.wordpress.com


> -- 
> You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
> To post to this group, send email to pandoc-discuss@googlegroups.com.
> To unsubscribe from this group, send email to pandoc-discuss+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pandoc-discuss?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To post to this group, send email to pandoc-discuss@googlegroups.com.
To unsubscribe from this group, send email to pandoc-discuss+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pandoc-discuss?hl=en.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Start of literate programming emacs mode
  2010-05-02 16:06 ` John Velman
@ 2010-05-03  0:13   ` Ivan Miljenovic
  0 siblings, 0 replies; 5+ messages in thread
From: Ivan Miljenovic @ 2010-05-03  0:13 UTC (permalink / raw)
  To: pandoc-discuss, John Velman


On 3 May 2010 02:06, John Velman <velman@cox.net> wrote:
> Sounds complicated. =A0I've had a couple of brief flirtations with emacs,
> including OS X Auamacs, but always go back to vim. =A0:-)

Yes, but can vim do multi-mode?  This is complicated only because of
the multi-mode stuff (which emacs doesn't support natively).

--=20
Ivan Lazar Miljenovic
Ivan.Miljenovic@gmail.com
IvanMiljenovic.wordpress.com

--=20
You received this message because you are subscribed to the Google Groups "=
pandoc-discuss" group.
To post to this group, send email to pandoc-discuss@googlegroups.com.
To unsubscribe from this group, send email to pandoc-discuss+unsubscribe@go=
oglegroups.com.
For more options, visit this group at http://groups.google.com/group/pandoc=
-discuss?hl=3Den.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Start of literate programming emacs mode
       [not found] ` <874oiq9z85.fsf-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
@ 2010-11-13 18:24   ` Conal Elliott
       [not found]     ` <AANLkTikku8DFsESZDG_3s9WB17+Er17p3Vv3a3h4BYvh-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: Conal Elliott @ 2010-11-13 18:24 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

[-- Attachment #1: Type: text/plain, Size: 3546 bytes --]

Hi Ivan,

How has your multi-mode developed since you posted in May? I've been using
mmm-mode with okay (but not stellar) results. And I do get syntax
highlighting in code blocks & code inlines. For the inlines, however, syntax
highlighting spills outside, contaminating the rest of the line.

Thanks, - Conal

On Sun, May 2, 2010 at 7:04 AM, Ivan Lazar Miljenovic <
ivan.miljenovic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:

>
> Hi all,
>
> I've wasted my weekend hacking on a multi-mode for Emacs for use with
> markdown-mode and literate-haskell-mode along with other modes so that
> we can take advantage of the various syntax highlighting, completion,
> etc. when writing markdown with code chunks.
>
> The mode uses [multi-mode] and is based upon haskell-latex.el in the
> same directory.  I first tried mmm-mode, but found out that it doesn't
> support syntax highlighting with recent versions of emacs; I then
> switched to mumamo-mode at the advice of quicksilver which was a pain to
> use: first I got it working as long as you had at most one code block
> [any more and it barfed], then realised I had an old version and updated
> it only to find that now it couldn't tell when the code block ended,
> probably because it got confused between the starts and ends.  In the
> end, multi-mode does some weird things (see below) but seems to work.
>
> [multi-mode]: http://www.loveshack.ukfsn.org/emacs/multi-mode.el
>
> Some comments:
>
> * Only works on named code blocks.  However, it picks up the name of the
>  language used and if you set it up (via customisation) then it will
>  automatically choose the correct major mode for that code block.  Only
>  Haskell (using literate-haskell so that I can do bird-style code
>  blocks) is predefined.  Unnamed code blocks will probably cause
>  problems (how do you tell whether the given region is an un-named code
>  block or is just between two un-named code blocks).
>
> * Cannot accept extra lines of tildas inside code blocks.
>
> * multi-mode seems to create extra virtual buffers for each code block,
>  (e.g. not only is there Foo.text but also Foo.text<2>, Foo.text<3>,
>  etc.).
>
> * Documentation, etc. isn't complete yet (hasn't been touched really).
>
> You just need to do (require 'markdown-code-mode) and then create
> associations with markdown-code-mode.  To get it working with other
> languages, do customize-group on markdown-code and add a new name/mode
> pair.
>
> This is my first emacs mode, so be gentle.
>
>
>
> --
> Ivan Lazar Miljenovic
> Ivan.Miljenovic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
> IvanMiljenovic.wordpress.com
>
> --
> You received this message because you are subscribed to the Google Groups
> "pandoc-discuss" group.
> To post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
> pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<pandoc-discuss%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> .
> For more options, visit this group at
> http://groups.google.com/group/pandoc-discuss?hl=en.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at http://groups.google.com/group/pandoc-discuss?hl=en.


[-- Attachment #2: Type: text/html, Size: 4464 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Start of literate programming emacs mode
       [not found]     ` <AANLkTikku8DFsESZDG_3s9WB17+Er17p3Vv3a3h4BYvh-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2010-11-13 19:26       ` Ivan Lazar Miljenovic
  0 siblings, 0 replies; 5+ messages in thread
From: Ivan Lazar Miljenovic @ 2010-11-13 19:26 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

On 14 November 2010 05:24, Conal Elliott <conal-R2YG1wQAgWFeoWH0uzbU5w@public.gmane.org> wrote:
> Hi Ivan,
>
> How has your multi-mode developed since you posted in May? I've been using
> mmm-mode with okay (but not stellar) results. And I do get syntax
> highlighting in code blocks & code inlines. For the inlines, however, syntax
> highlighting spills outside, contaminating the rest of the line.

I haven't really touched it since, but I've been meaning to.  From
memory, it currently works as it stands, but my biggest barrier was
that I wanted to have some way of pre-processing the file to get rid
of the Markdown such that you aren't constrained as much, but I never
really worked out how to do so.

If I remember, I'll try to put up a repo for it on patch-tag or
something during the next couple of days.

-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
IvanMiljenovic.wordpress.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-11-13 19:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-02 14:04 Start of literate programming emacs mode Ivan Lazar Miljenovic
2010-05-02 16:06 ` John Velman
2010-05-03  0:13   ` Ivan Miljenovic
     [not found] ` <874oiq9z85.fsf-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2010-11-13 18:24   ` Conal Elliott
     [not found]     ` <AANLkTikku8DFsESZDG_3s9WB17+Er17p3Vv3a3h4BYvh-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-11-13 19:26       ` Ivan Lazar Miljenovic

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).