dynare.el 3.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
;;; dynare.el --- major mode to edit .mod files for dynare
;; Created: 2010 Sep 10
;; Version: 0.1

;; Copyright (C) 2010 Yannick Kalantzis
;;
;; 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 3 of the License, 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 GNU Emacs; see the file COPYING.  If not, see
;; <http://www.gnu.org/licenses/>.

;; Keywords: dynare

;; To comment/uncomment, use `ALT-;'. See `comment-dwim' for further details.

;;; Installation:
;;
;;   Put the this file as "dynare.el" somewhere on your load path, then
;;   add this to your .emacs or site-init.el file:
;;
;;   (require 'dynare)
;;   (autoload 'dynare-mode "dynare" "Enter dynare mode." t)
;;   (setq auto-mode-alist (cons '("\\.mod\\'" . dynare-mode) auto-mode-alist))

;;; Changelog
;;  2010-09-07 by Yannick Kalantzis
;;    Minor changes. Add `require'. Add builtin operators `;' and `='.
;;  2010-09-06 by Yannick Kalantzis 
;;    Created. 
;;    Reproduces Xah Lee's instructions. 
;;    See <http://xahlee.org/emacs/elisp_syntax_coloring.html>
;;    Very basic syntax highlighting: comments, some keywords.

;;; TODO
;;    - indentation 
;;    - blocks templates "model/end", "initval/end", etc.
;;    - functions to insert main keywords

;;; Code:

;; function to comment/uncomment text
(defun dynare-comment-dwim (arg)
"Comment or uncomment current line or region in a smart way.
For detail, see `comment-dwim'."
   (interactive "*P")
   (require 'newcomment)
   (let ((deactivate-mark nil) (comment-start "//") (comment-end ""))
     (comment-dwim arg)))

;; define several class of keywords
(defvar dynare-keywords
  '("var" "varexo" "parameters" "model" "initval" "endval" "end" "shocks" "periods" "values" "resid" "for" "endfor" "define" "in") 
  "dynare keywords.")

(defvar dynare-functions
  '("simul" "steady" "check" "rplot" "dynatype")
  "dynare functions.")

;; create the regex string for each class of keywords
(defvar dynare-keywords-regexp (regexp-opt dynare-keywords 'words))
(defvar dynare-functions-regexp (regexp-opt dynare-functions 'words))

;; clear memory
(setq dynare-keywords nil)
(setq dynare-functions nil)

;; create the list for font-lock.
;; each class of keyword is given a particular face
(setq dynare-font-lock-keywords
  `(
    (,dynare-functions-regexp . font-lock-function-name-face)
    (,dynare-keywords-regexp . font-lock-keyword-face)
    (";\\|=" . font-lock-builtin-face)
    ))

;; define the major mode
(define-derived-mode dynare-mode fundamental-mode
  "dynare mode"
  "dynare is a mode for editing mod files used by dynare."
  (setq mode-name "dynare mode")

  ;; modify the keymap
  (define-key dynare-mode-map [remap comment-dwim] 'dynare-comment-dwim)

  ;; define C++ style comment  “/* ... */” and “// ...” 
  (modify-syntax-entry ?\/ ". 124b" dynare-mode-syntax-table) 
  ;; "/" is the 1st and 2nd char of /* and */ (a-style) and the 2nd char of //
  ;; (b-style)
  (modify-syntax-entry ?* ". 23" dynare-mode-syntax-table)
  ;; "*" is the 2nd and 1st char of /* and */ (a-style only)
  (modify-syntax-entry ?\n "> b" dynare-mode-syntax-table)
  ;; newline is the comment-end sequence of b-style comments

  ;; syntax highlighting
  (setq font-lock-defaults '((dynare-font-lock-keywords)))

  ;; clear memory
  (setq dynare-keywords-regexp nil)
  (setq dynare-functions-regexp nil)
  )

(provide 'dynare)
;;; dynare.el ends here