Skip to content

Instantly share code, notes, and snippets.

@reetinder
Created November 6, 2012 06:30
Show Gist options
  • Save reetinder/4022989 to your computer and use it in GitHub Desktop.
Save reetinder/4022989 to your computer and use it in GitHub Desktop.
Literate Haskell with Org Mode Documentation
;;; haskell-org.el --- Literate Haskell with Org Mode Documentation
;; Copyright (C) 2003, 2007 Free Software Foundation, Inc.
;; Authors: Dave Love <fx@gnu.org>
;; Keywords: languages, wp
;; Created: Sept 2003
;; $Revision: 1.5 $
;; URL: http://www.loveshack.ukfsn.org/emacs
;; Modified by Reetinder Sidhu on oct. 30, 2012 for org-mode support.
;; URL: http://sidhusblog.wordpress.com/2012/10/05/haskell-org/
;; 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).
;;
;; Thus you probably want to do something like this, assuming
;; haskell-latex is on `load-path' and $haskell_mode_path is the path
;; name of the haskell-latex directory:
;;
;; (load "$haskell_mode_path/haskell-site-file.el")
;; (add-to-list 'auto-mode-alist '("\\.l[gh]s\\'" . haskell-latex-mode))
;; (autoload 'haskell-latex-mode "haskell-latex")
;;; Code:
;; TODO: remove interactive, comments above, figure out autoload, auto
;; colorize when buffer visited, relabel haskorg as debug.
(require 'haskell-mode)
(require 'org-mode)
(require 'multi-mode)
(add-hook 'haskell-mode-hook (lambda () (setq haskell-literate 'bird)))
(add-hook 'org-mode-hook (lambda () (setq truncate-lines nil) (setq fill-column 79)))
(defun haskell-org-chunk-region (pos)
"Determine type and limit of current chunk at POS.
Return (MODE START END), where MODE is `haskell-mode' or `org-mode'
and START and END are the limits of the chunk."
(interactive)
(save-excursion
(save-restriction
(widen)
(goto-char pos)
(beginning-of-line)
(if (looking-at ">")
(haskell-org-search-extent 'haskell-mode "^[^>]")
(haskell-org-search-extent 'org-mode "^>")))))
(defun haskell-org-search-extent (mode regex)
"Determines start and end of chunk (either haskell or org)."
(interactive)
(progn (setq ppos (point))
(setq back (re-search-backward regex nil t))
(goto-char ppos)
(setq front (re-search-forward regex nil t))
(multi-make-list mode
(if back (progn (goto-char back) (forward-line) (point)) (point-min))
(if front (progn (goto-char front) (forward-line -1) (line-end-position)) (point-max)))))
;;;###autoload
(defun haskell-org-mode ()
"Mode for editing `literate Haskell' with Org conventions."
(interactive)
(set (make-local-variable 'multi-alist)
'((haskell-mode . haskell-org-chunk-region)
(org-mode . nil)))
(multi-mode-install-modes))
(provide 'haskell-org)
;;; haskell-org.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment