/ emacs.d / haskell / haskell-cabal.el
haskell-cabal.el
  1  ;;; haskell-cabal.el --- Support for Cabal packages
  2  
  3  ;; Copyright (C) 2007  Stefan Monnier
  4  
  5  ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
  6  
  7  ;; This file is free software; you can redistribute it and/or modify
  8  ;; it under the terms of the GNU General Public License as published by
  9  ;; the Free Software Foundation; either version 2, or (at your option)
 10  ;; any later version.
 11  
 12  ;; This file is distributed in the hope that it will be useful,
 13  ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  ;; GNU General Public License for more details.
 16  
 17  ;; You should have received a copy of the GNU General Public License
 18  ;; along with GNU Emacs; see the file COPYING.  If not, write to
 19  ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 20  ;; Boston, MA 02110-1301, USA.
 21  
 22  ;;; Commentary:
 23  
 24  ;; 
 25  
 26  ;;; Code:
 27  
 28  ;; (defun haskell-cabal-extract-fields-from-doc ()
 29  ;;   (require 'xml)
 30  ;;   (require 'cl)
 31  ;;   (let ((section (completing-read
 32  ;;                   "Section: "
 33  ;;                   '("general-fields" "library" "executable" "buildinfo"))))
 34  ;;     (goto-char (point-min))
 35  ;;     (search-forward (concat "<sect3 id=\"" section "\">")))
 36  ;;   (let* ((xml (xml-parse-region
 37  ;;                (progn (search-forward "<variablelist>") (match-beginning 0))
 38  ;;                (progn (search-forward "</variablelist>") (point))))
 39  ;;          (varlist (remove-if-not 'consp (cddar xml)))
 40  ;;          (syms (mapcar (lambda (entry) (caddr (assq 'literal (assq 'term entry))))
 41  ;;                        varlist))
 42  ;;          (fields (mapcar (lambda (sym) (substring-no-properties sym 0 -1)) syms)))
 43  ;;     fields))
 44  
 45  (defconst haskell-cabal-general-fields
 46    ;; Extracted with (haskell-cabal-extract-fields-from-doc "general-fields")
 47    '("name" "version" "cabal-version" "license" "license-file" "copyright"
 48      "author" "maintainer" "stability" "homepage" "package-url" "synopsis"
 49      "description" "category" "tested-with" "build-depends" "data-files"
 50      "extra-source-files" "extra-tmp-files"))
 51  
 52  (defconst haskell-cabal-library-fields
 53    ;; Extracted with (haskell-cabal-extract-fields-from-doc "library")
 54    '("exposed-modules"))
 55  
 56  (defconst haskell-cabal-executable-fields
 57    ;; Extracted with (haskell-cabal-extract-fields-from-doc "executable")
 58    '("executable" "main-is"))
 59  
 60  (defconst haskell-cabal-buildinfo-fields
 61    ;; Extracted with (haskell-cabal-extract-fields-from-doc "buildinfo")
 62    '("buildable" "other-modules" "hs-source-dirs" "extensions" "ghc-options"
 63      "ghc-prof-options" "hugs-options" "nhc-options" "includes"
 64      "install-includes" "include-dirs" "c-sources" "extra-libraries"
 65      "extra-lib-dirs" "cc-options" "ld-options" "frameworks"))
 66  
 67  (defvar haskell-cabal-mode-syntax-table
 68    (let ((st (make-syntax-table)))
 69      st))
 70  
 71  (defvar haskell-cabal-font-lock-keywords
 72    ;; The comment syntax can't be described simply in syntax-table.  We could
 73    ;; use font-lock-syntactic-keywords, but is it worth it?
 74    '(("^--.*" . font-lock-comment-face)
 75      ("^\\([^ :]+\\):" (1 font-lock-keyword-face))))
 76  
 77  (defvar haskell-cabal-buffers nil
 78    "List of Cabal buffers.")
 79  
 80  (defun haskell-cabal-buffers-clean (&optional buffer)
 81    (let ((bufs ()))
 82      (dolist (buf haskell-cabal-buffers)
 83        (if (and (buffer-live-p buf) (not (eq buf buffer))
 84                 (with-current-buffer buf (derived-mode-p 'haskell-cabal-mode)))
 85            (push buf bufs)))
 86      (setq haskell-cabal-buffers bufs)))
 87  
 88  (defun haskell-cabal-unregister-buffer ()
 89    (haskell-cabal-buffers-clean (current-buffer)))
 90  
 91  ;;;###autoload
 92  (add-to-list 'auto-mode-alist '("\\.cabal\\'" . haskell-cabal-mode))
 93  
 94  ;;;###autoload
 95  (define-derived-mode haskell-cabal-mode fundamental-mode "Haskell-Cabal"
 96    "Major mode for Cabal package description files."
 97    (set (make-local-variable 'font-lock-defaults)
 98         '(haskell-cabal-font-lock-keywords t t nil nil))
 99    (add-to-list 'haskell-cabal-buffers (current-buffer))
100    (add-hook 'change-major-mode-hook 'haskell-cabal-unregister-buffer nil 'local)
101    (add-hook 'kill-buffer-hook 'haskell-cabal-unregister-buffer nil 'local))
102  
103  (provide 'haskell-cabal)
104  
105  ;; arch-tag: d455f920-5e4d-42b6-a2c7-4a7e84a05c29
106  ;;; haskell-cabal.el ends here