Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: 0.1.1
Fetching contributors…

Cannot retrieve contributors at this time

file 154 lines (140 sloc) 4.364 kb
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
;;; elixir-mode.el -- Major mode for editing Elixir files

;; Copyright (c) 2011 secondplanet
;; Author: Humza Yaqoob
;; URL: https://github.com/secondplanet/elixir-mode
;; Created: Mon Nov 7 2011
;; Keywords: languages elixir
;; Version: 0.1.1

;; This file is not a part of GNU Emacs.
;; It is distributed under the MIT license.
;; See https://github.com/secondplanet/elixir-mode/tree/master/LICENSE

;;; Commentary:

;; Provides font-locking, indentation support, and navigation for Elixir programs.

;;; Code:

(defconst elixir-mode-version "0.1.1"
"Elixir mode version number.")
(defconst elixir-mode-date "2011-11-08"
"Elixir mode version date.")

(defvar elixir-mode-hook nil)

(defvar elixir-mode-keyword-names '(
  "->"
  "do"
  "after"
  "for"
  "module"
  "def"
  "if"
  "when"
  "case"
  "match"
  "then"
  "else"
  "elsif"
  "try"
  "catch"
  "end")
"Elixir mode keywords.")
(defvar elixir-mode-module-names '(
  "Atom"
  "BitString"
  "Code"
  "Date"
  "DateTime"
  "EEx"
  "ETS"
  "ExUnit"
  "File"
  "Float"
  "Function"
  "GenServer"
  "GenTCP"
  "IEX"
  "Integer"
  "IO"
  "List"
  "Math"
  "Method"
  "Module"
  "Numeric"
  "OrderedDict"
  "OS"
  "Port"
  "Process"
  "Record"
  "Reference"
  "Regexp"
  "Set"
  "String"
  "Timer"
  "Tuple"
  "UnboundMethod")
"Elixir mode modules.")
(defvar elixir-mode-builtin-names '(
  "Erlang")
"Elixir mode builtins.")

(defconst elixir-mode-font-lock-defaults
  (list
    '("%.*$" . font-lock-comment-face)
    '("^\\s *def\\s +\\([^( \t\n]+\\)" . font-lock-function-name-face)
    `(,(concat "\\<" (regexp-opt elixir-mode-keyword-names t) "\\>") . font-lock-keyword-face)
    `(,(concat "\\<" (regexp-opt elixir-mode-builtin-names t) "\\>") . font-lock-builtin-face)
    `(,(concat "\\<" (regexp-opt elixir-mode-module-names t) "\\>") . font-lock-type-face)
    '("\\(\\w*\\)\\s-*:?=" . font-lock-variable-name-face)
    '("\\<\\(true\\|false\\|nil\\)\\>" . font-lock-constant-face))
"Highlighting for Elixir mode.")

(defun elixir-mode-indent-line ()
  "Indent current line as Elixir code."
  (interactive)
  (beginning-of-line)
  (if (bobp)
    (indent-line-to 0)
    (let ((not-indented t) cur-indent)
      (if (looking-at "^[ \t]*end$")
        (progn
          (save-excursion
            (forward-line -1)
            (setq cur-indent (- (current-indentation) default-tab-width)))
        (if (< cur-indent 0)
          (setq cur-indent 0)))
       (save-excursion
         (while not-indented
           (forward-line -1)
             (if (looking-at "^[ \t]*end$")
               (progn
                 (setq cur-indent (current-indentation))
                 (setq not-indented nil))
                 (if (looking-at "^[ \t]*\\(do\\|after\\|module\\|def\\|if\\|case\\|else\\|elsif\\|receive\\|after\\|try\\|catch\\)")
                   (progn
                     (setq cur-indent (+ (current-indentation) default-tab-width))
                     (setq not-idented nil))
                 (if (bobp)
                   (setq not-indented nil)))))))
      (if cur-indent
        (indent-line-to cur-indent)
        (indent-line-to 0)))))

(defvar elixir-mode-syntax-table
  (let ((elixir-mode-syntax-table (make-syntax-table)))
    (modify-syntax-entry ?_ "w" elixir-mode-syntax-table)
    (modify-syntax-entry ?% "<" elixir-mode-syntax-table)
    (modify-syntax-entry ?\n ">" elixir-mode-syntax-table)
    (modify-syntax-entry ?\( "()" elixir-mode-syntax-table)
    (modify-syntax-entry ?\) ")(" elixir-mode-syntax-table)
    (modify-syntax-entry ?\{ "(}" elixir-mode-syntax-table)
    (modify-syntax-entry ?\} "){" elixir-mode-syntax-table)
    (modify-syntax-entry ?\[ "(]" elixir-mode-syntax-table)
    (modify-syntax-entry ?\] ")[" elixir-mode-syntax-table)
    elixir-mode-syntax-table)
"Elixir mode syntax table.")

(defun elixir-mode ()
  "Major mode for editing Elixir files."
    (interactive)
    (kill-all-local-variables)
    (set-syntax-table elixir-mode-syntax-table)
    (set (make-local-variable 'indent-line-function) 'elixir-mode-indent-line)
    (set (make-local-variable 'font-lock-defaults) '(elixir-mode-font-lock-defaults))
    (setq major-mode 'elixir-mode)
    (setq mode-name "Elixir")
    (run-hooks 'elixir-mode-hook))

(add-to-list 'auto-mode-alist '("\\.ex\\'" . elixir-mode))
(add-to-list 'auto-mode-alist '("\\.exs\\'" . elixir-mode))

(provide 'elixir-mode)
Something went wrong with that request. Please try again.