wok-current annotate emacs-pkg-vala-mode/stuff/vala-mode.el @ rev 8973

linmodem-hsfmodem: use variable
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Mar 02 10:41:40 2011 +0100 (2011-03-02)
parents
children
rev   line source
domcox@8443 1 ;;; vala-mode.el --- Vala mode derived mode
domcox@8443 2
domcox@8443 3 ;; Author: 2005 Dylan R. E. Moonfire
domcox@8443 4 ;; 2008 Étienne BERSAC
domcox@8443 5 ;; Maintainer: Étienne BERSAC <bersace03@laposte.net>
domcox@8443 6 ;; Created: 2008 May the 4th
domcox@8443 7 ;; Modified: May 2008
domcox@8443 8 ;; Version: 0.1
domcox@8443 9 ;; Keywords: vala languages oop
domcox@8443 10
domcox@8443 11 ;; This program is free software; you can redistribute it and/or modify
domcox@8443 12 ;; it under the terms of the GNU General Public License as published by
domcox@8443 13 ;; the Free Software Foundation; either version 2 of the License, or
domcox@8443 14 ;; (at your option) any later version.
domcox@8443 15 ;;
domcox@8443 16 ;; This program is distributed in the hope that it will be useful,
domcox@8443 17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
domcox@8443 18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
domcox@8443 19 ;; GNU General Public License for more details.
domcox@8443 20 ;;
domcox@8443 21 ;; You should have received a copy of the GNU General Public License
domcox@8443 22 ;; along with this program; see the file COPYING. If not, write to
domcox@8443 23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
domcox@8443 24 ;; Boston, MA 02111-1307, USA.
domcox@8443 25
domcox@8443 26 ;;; Commentary:
domcox@8443 27 ;;
domcox@8443 28 ;; See http://live.gnome.org/Vala for details about Vala language.
domcox@8443 29 ;;
domcox@8443 30 ;; This is a separate mode to implement the Vala constructs and
domcox@8443 31 ;; font-locking. It is mostly the csharp-mode from
domcox@8443 32 ;; http://mfgames.com/linux/csharp-mode with vala specific keywords
domcox@8443 33 ;; and filename suffixes.
domcox@8443 34 ;;
domcox@8443 35 ;; Note: The interface used in this file requires CC Mode 5.30 or
domcox@8443 36 ;; later.
domcox@8443 37
domcox@8443 38 ;;; .emacs (don't put in (require 'vala-mode))
domcox@8443 39 ;; (autoload 'vala-mode "vala-mode" "Major mode for editing Vala code." t)
domcox@8443 40 ;; (setq auto-mode-alist
domcox@8443 41 ;; (append '(("\\.vala$" . vala-mode)) auto-mode-alist))
domcox@8443 42
domcox@8443 43 ;;; Versions:
domcox@8443 44 ;;
domcox@8443 45 ;; 0.1 : Initial version based on csharp-mode
domcox@8443 46 ;;
domcox@8443 47
domcox@8443 48 ;; This is a copy of the function in cc-mode which is used to handle
domcox@8443 49 ;; the eval-when-compile which is needed during other times.
domcox@8443 50 (defun c-filter-ops (ops opgroup-filter op-filter &optional xlate)
domcox@8443 51 ;; See cc-langs.el, a direct copy.
domcox@8443 52 (unless (listp (car-safe ops))
domcox@8443 53 (setq ops (list ops)))
domcox@8443 54 (cond ((eq opgroup-filter t)
domcox@8443 55 (setq opgroup-filter (lambda (opgroup) t)))
domcox@8443 56 ((not (functionp opgroup-filter))
domcox@8443 57 (setq opgroup-filter `(lambda (opgroup)
domcox@8443 58 (memq opgroup ',opgroup-filter)))))
domcox@8443 59 (cond ((eq op-filter t)
domcox@8443 60 (setq op-filter (lambda (op) t)))
domcox@8443 61 ((stringp op-filter)
domcox@8443 62 (setq op-filter `(lambda (op)
domcox@8443 63 (string-match ,op-filter op)))))
domcox@8443 64 (unless xlate
domcox@8443 65 (setq xlate 'identity))
domcox@8443 66 (c-with-syntax-table (c-lang-const c-mode-syntax-table)
domcox@8443 67 (delete-duplicates
domcox@8443 68 (mapcan (lambda (opgroup)
domcox@8443 69 (when (if (symbolp (car opgroup))
domcox@8443 70 (when (funcall opgroup-filter (car opgroup))
domcox@8443 71 (setq opgroup (cdr opgroup))
domcox@8443 72 t)
domcox@8443 73 t)
domcox@8443 74 (mapcan (lambda (op)
domcox@8443 75 (when (funcall op-filter op)
domcox@8443 76 (let ((res (funcall xlate op)))
domcox@8443 77 (if (listp res) res (list res)))))
domcox@8443 78 opgroup)))
domcox@8443 79 ops)
domcox@8443 80 :test 'equal)))
domcox@8443 81
domcox@8443 82 ;; This inserts the bulk of the code.
domcox@8443 83 (require 'cc-mode)
domcox@8443 84
domcox@8443 85 ;; These are only required at compile time to get the sources for the
domcox@8443 86 ;; language constants. (The cc-fonts require and the font-lock
domcox@8443 87 ;; related constants could additionally be put inside an
domcox@8443 88 ;; (eval-after-load "font-lock" ...) but then some trickery is
domcox@8443 89 ;; necessary to get them compiled.)
domcox@8443 90 (eval-when-compile
domcox@8443 91 (let ((load-path
domcox@8443 92 (if (and (boundp 'byte-compile-dest-file)
domcox@8443 93 (stringp byte-compile-dest-file))
domcox@8443 94 (cons (file-name-directory byte-compile-dest-file) load-path)
domcox@8443 95 load-path)))
domcox@8443 96 (load "cc-mode" nil t)
domcox@8443 97 (load "cc-fonts" nil t)
domcox@8443 98 (load "cc-langs" nil t)))
domcox@8443 99
domcox@8443 100 (eval-and-compile
domcox@8443 101 ;; Make our mode known to the language constant system. Use Java
domcox@8443 102 ;; mode as the fallback for the constants we don't change here.
domcox@8443 103 ;; This needs to be done also at compile time since the language
domcox@8443 104 ;; constants are evaluated then.
domcox@8443 105 (c-add-language 'vala-mode 'java-mode))
domcox@8443 106
domcox@8443 107 ;; Java uses a series of regexes to change the font-lock for class
domcox@8443 108 ;; references. The problem comes in because Java uses Pascal (leading
domcox@8443 109 ;; space in names, SomeClass) for class and package names, but
domcox@8443 110 ;; Camel-casing (initial lowercase, upper case in words,
domcox@8443 111 ;; i.e. someVariable) for variables.
domcox@8443 112 ;;(error (byte-compile-dest-file))
domcox@8443 113 ;;(error (c-get-current-file))
domcox@8443 114 (c-lang-defconst c-opt-after-id-concat-key
domcox@8443 115 vala (if (c-lang-const c-opt-identifier-concat-key)
domcox@8443 116 (c-lang-const c-symbol-start)))
domcox@8443 117
domcox@8443 118 (c-lang-defconst c-basic-matchers-before
domcox@8443 119 vala `(
domcox@8443 120 ;;;; Font-lock the attributes by searching for the
domcox@8443 121 ;;;; appropriate regex and marking it as TODO.
domcox@8443 122 ;;,`(,(concat "\\(" vala-attribute-regex "\\)")
domcox@8443 123 ;; 0 font-lock-function-name-face)
domcox@8443 124
domcox@8443 125 ;; Put a warning face on the opener of unclosed strings that
domcox@8443 126 ;; can't span lines. Later font
domcox@8443 127 ;; lock packages have a `font-lock-syntactic-face-function' for
domcox@8443 128 ;; this, but it doesn't give the control we want since any
domcox@8443 129 ;; fontification done inside the function will be
domcox@8443 130 ;; unconditionally overridden.
domcox@8443 131 ,(c-make-font-lock-search-function
domcox@8443 132 ;; Match a char before the string starter to make
domcox@8443 133 ;; `c-skip-comments-and-strings' work correctly.
domcox@8443 134 (concat ".\\(" c-string-limit-regexp "\\)")
domcox@8443 135 '((c-font-lock-invalid-string)))
domcox@8443 136
domcox@8443 137 ;; Fontify keyword constants.
domcox@8443 138 ,@(when (c-lang-const c-constant-kwds)
domcox@8443 139 (let ((re (c-make-keywords-re nil
domcox@8443 140 (c-lang-const c-constant-kwds))))
domcox@8443 141 `((eval . (list ,(concat "\\<\\(" re "\\)\\>")
domcox@8443 142 1 c-constant-face-name)))))
domcox@8443 143
domcox@8443 144 ;; Fontify all keywords except the primitive types.
domcox@8443 145 ,`(,(concat "\\<" (c-lang-const c-regular-keywords-regexp))
domcox@8443 146 1 font-lock-keyword-face)
domcox@8443 147
domcox@8443 148 ;; Fontify leading identifiers in fully
domcox@8443 149 ;; qualified names like "Foo.Bar".
domcox@8443 150 ,@(when (c-lang-const c-opt-identifier-concat-key)
domcox@8443 151 `((,(byte-compile
domcox@8443 152 `(lambda (limit)
domcox@8443 153 (while (re-search-forward
domcox@8443 154 ,(concat "\\(\\<" ; 1
domcox@8443 155 "\\(" (c-lang-const c-symbol-key)
domcox@8443 156 "\\)" ; 2
domcox@8443 157 "[ \t\n\r\f\v]*"
domcox@8443 158 (c-lang-const
domcox@8443 159 c-opt-identifier-concat-key)
domcox@8443 160 "[ \t\n\r\f\v]*"
domcox@8443 161 "\\)"
domcox@8443 162 "\\("
domcox@8443 163 (c-lang-const
domcox@8443 164 c-opt-after-id-concat-key)
domcox@8443 165 "\\)")
domcox@8443 166 limit t)
domcox@8443 167 (unless (progn
domcox@8443 168 (goto-char (match-beginning 0))
domcox@8443 169 (c-skip-comments-and-strings limit))
domcox@8443 170 (or (get-text-property (match-beginning 2) 'face)
domcox@8443 171 (c-put-font-lock-face (match-beginning 2)
domcox@8443 172 (match-end 2)
domcox@8443 173 c-reference-face-name))
domcox@8443 174 (goto-char (match-end 1)))))))))
domcox@8443 175 ))
domcox@8443 176
domcox@8443 177 ;; Vala does not allow a leading qualifier operator. It also doesn't
domcox@8443 178 ;; allow the ".*" construct of Java. So, we redo this regex without
domcox@8443 179 ;; the "\\|\\*" regex.
domcox@8443 180 (c-lang-defconst c-identifier-key
domcox@8443 181 vala (concat "\\(" (c-lang-const c-symbol-key) "\\)" ; 1
domcox@8443 182 (concat "\\("
domcox@8443 183 "[ \t\n\r\f\v]*"
domcox@8443 184 (c-lang-const c-opt-identifier-concat-key)
domcox@8443 185 "[ \t\n\r\f\v]*"
domcox@8443 186 (concat "\\("
domcox@8443 187 "\\(" (c-lang-const c-symbol-key) "\\)"
domcox@8443 188 "\\)")
domcox@8443 189 "\\)*")))
domcox@8443 190
domcox@8443 191 ;; Vala has a few rules that are slightly different than Java for
domcox@8443 192 ;; operators. This also removed the Java's "super" and replaces it
domcox@8443 193 ;; with the Vala's "base".
domcox@8443 194 (c-lang-defconst c-operators
domcox@8443 195 vala `((prefix "base")))
domcox@8443 196
domcox@8443 197 ;; Vala directives ?
domcox@8443 198 ;; (c-lang-defconst c-opt-cpp-prefix
domcox@8443 199 ;; csharp "^\\s *#.*")
domcox@8443 200
domcox@8443 201
domcox@8443 202 ;; Vala uses the following assignment operators
domcox@8443 203 (c-lang-defconst c-assignment-operators
domcox@8443 204 vala '("=" "*=" "/=" "%=" "+=" "-=" ">>=" "<<="
domcox@8443 205 "&=" "^=" "|=" "++" "--"))
domcox@8443 206
domcox@8443 207 ;; This defines the primative types for Vala
domcox@8443 208 (c-lang-defconst c-primitive-type-kwds
domcox@8443 209 vala '("void" "char" "int" "float" "double" "string"))
domcox@8443 210
domcox@8443 211 ;; The keywords that define that the following is a type, such as a
domcox@8443 212 ;; class definition.
domcox@8443 213 (c-lang-defconst c-type-prefix-kwds
domcox@8443 214 vala '("class" "interface" "struct" "enum" "signal"))
domcox@8443 215
domcox@8443 216 ;; Type modifier keywords. They appear anywhere in types, but modifiy
domcox@8443 217 ;; instead create one.
domcox@8443 218 (c-lang-defconst c-type-modifier-kwds
domcox@8443 219 vala '("const"))
domcox@8443 220
domcox@8443 221 ;; Structures that are similiar to classes.
domcox@8443 222 (c-lang-defconst c-class-decl-kwds
domcox@8443 223 vala '("class" "interface"))
domcox@8443 224
domcox@8443 225 ;; The various modifiers used for class and method descriptions.
domcox@8443 226 (c-lang-defconst c-modifier-kwds
domcox@8443 227 vala '("public" "partial" "private" "const" "abstract"
domcox@8443 228 "protected" "ref" "in" "out" "static" "virtual"
domcox@8443 229 "override" "params" "internal" "weak" "owned"
domcox@8443 230 "unowned"))
domcox@8443 231
domcox@8443 232 ;; We don't use the protection level stuff because it breaks the
domcox@8443 233 ;; method indenting. Not sure why, though.
domcox@8443 234 (c-lang-defconst c-protection-kwds
domcox@8443 235 vala nil)
domcox@8443 236
domcox@8443 237 ;; Define the keywords that can have something following after them.
domcox@8443 238 (c-lang-defconst c-type-list-kwds
domcox@8443 239 vala '("struct" "class" "interface" "is" "as"
domcox@8443 240 "delegate" "event" "set" "get" "add" "remove"
domcox@8443 241 "callback" "signal" "var" "default"))
domcox@8443 242
domcox@8443 243 ;; This allows the classes after the : in the class declartion to be
domcox@8443 244 ;; fontified.
domcox@8443 245 (c-lang-defconst c-typeless-decl-kwds
domcox@8443 246 vala '(":"))
domcox@8443 247
domcox@8443 248 ;; Sets up the enum to handle the list properly
domcox@8443 249 (c-lang-defconst c-brace-list-decl-kwds
domcox@8443 250 vala '("enum" "errordomain"))
domcox@8443 251
domcox@8443 252 ;; We need to remove Java's package keyword
domcox@8443 253 (c-lang-defconst c-ref-list-kwds
domcox@8443 254 vala '("using" "namespace" "construct"))
domcox@8443 255
domcox@8443 256 ;; Follow-on blocks that don't require a brace
domcox@8443 257 (c-lang-defconst c-block-stmt-2-kwds
domcox@8443 258 vala '("for" "if" "switch" "while" "catch" "foreach" "lock"))
domcox@8443 259
domcox@8443 260 ;; Statements that break out of braces
domcox@8443 261 (c-lang-defconst c-simple-stmt-kwds
domcox@8443 262 vala '("return" "continue" "break" "throw"))
domcox@8443 263
domcox@8443 264 ;; Statements that allow a label
domcox@8443 265 ;; TODO?
domcox@8443 266 (c-lang-defconst c-before-label-kwds
domcox@8443 267 vala nil)
domcox@8443 268
domcox@8443 269 ;; Constant keywords
domcox@8443 270 (c-lang-defconst c-constant-kwds
domcox@8443 271 vala '("true" "false" "null"))
domcox@8443 272
domcox@8443 273 ;; Keywords that start "primary expressions."
domcox@8443 274 (c-lang-defconst c-primary-expr-kwds
domcox@8443 275 vala '("this" "base"))
domcox@8443 276
domcox@8443 277 ;; We need to treat namespace as an outer block to class indenting
domcox@8443 278 ;; works properly.
domcox@8443 279 (c-lang-defconst c-other-block-decl-kwds
domcox@8443 280 vala '("namespace"))
domcox@8443 281
domcox@8443 282 ;; We need to include the "in" for the foreach
domcox@8443 283 (c-lang-defconst c-other-kwds
domcox@8443 284 vala '("in" "sizeof" "typeof"))
domcox@8443 285
domcox@8443 286 (require 'cc-awk)
domcox@8443 287
domcox@8443 288 (c-lang-defconst c-at-vsemi-p-fn
domcox@8443 289 vala 'c-awk-at-vsemi-p)
domcox@8443 290
domcox@8443 291
domcox@8443 292 (defcustom vala-font-lock-extra-types nil
domcox@8443 293 "*List of extra types (aside from the type keywords) to recognize in Vala mode.
domcox@8443 294 Each list item should be a regexp matching a single identifier.")
domcox@8443 295
domcox@8443 296 (defconst vala-font-lock-keywords-1 (c-lang-const c-matchers-1 vala)
domcox@8443 297 "Minimal highlighting for Vala mode.")
domcox@8443 298
domcox@8443 299 (defconst vala-font-lock-keywords-2 (c-lang-const c-matchers-2 vala)
domcox@8443 300 "Fast normal highlighting for Vala mode.")
domcox@8443 301
domcox@8443 302 (defconst vala-font-lock-keywords-3 (c-lang-const c-matchers-3 vala)
domcox@8443 303 "Accurate normal highlighting for Vala mode.")
domcox@8443 304
domcox@8443 305 (defvar vala-font-lock-keywords vala-font-lock-keywords-3
domcox@8443 306 "Default expressions to highlight in Vala mode.")
domcox@8443 307
domcox@8443 308 (defvar vala-mode-syntax-table
domcox@8443 309 nil
domcox@8443 310 "Syntax table used in vala-mode buffers.")
domcox@8443 311 (or vala-mode-syntax-table
domcox@8443 312 (setq vala-mode-syntax-table
domcox@8443 313 (funcall (c-lang-const c-make-mode-syntax-table vala))))
domcox@8443 314
domcox@8443 315 (defvar vala-mode-abbrev-table nil
domcox@8443 316 "Abbreviation table used in vala-mode buffers.")
domcox@8443 317 (c-define-abbrev-table 'vala-mode-abbrev-table
domcox@8443 318 ;; Keywords that if they occur first on a line
domcox@8443 319 ;; might alter the syntactic context, and which
domcox@8443 320 ;; therefore should trig reindentation when
domcox@8443 321 ;; they are completed.
domcox@8443 322 '(("else" "else" c-electric-continued-statement 0)
domcox@8443 323 ("while" "while" c-electric-continued-statement 0)
domcox@8443 324 ("catch" "catch" c-electric-continued-statement 0)
domcox@8443 325 ("finally" "finally" c-electric-continued-statement 0)))
domcox@8443 326
domcox@8443 327 (defvar vala-mode-map (let ((map (c-make-inherited-keymap)))
domcox@8443 328 ;; Add bindings which are only useful for Vala
domcox@8443 329 map)
domcox@8443 330 "Keymap used in vala-mode buffers.")
domcox@8443 331
domcox@8443 332 ;;(easy-menu-define vala-menu vala-mode-map "Vala Mode Commands"
domcox@8443 333 ;; ;; Can use `vala' as the language for `c-mode-menu'
domcox@8443 334 ;; ;; since its definition covers any language. In
domcox@8443 335 ;; ;; this case the language is used to adapt to the
domcox@8443 336 ;; ;; nonexistence of a cpp pass and thus removing some
domcox@8443 337 ;; ;; irrelevant menu alternatives.
domcox@8443 338 ;; (cons "Vala" (c-lang-const c-mode-menu vala)))
domcox@8443 339
domcox@8443 340 ;;; Autoload mode trigger
domcox@8443 341 (add-to-list 'auto-mode-alist '("\\.vala$" . vala-mode))
domcox@8443 342 (add-to-list 'auto-mode-alist '("\\.vapi$" . vala-mode))
domcox@8443 343
domcox@8443 344 ;; Custom variables
domcox@8443 345 (defcustom vala-mode-hook nil
domcox@8443 346 "*Hook called by `vala-mode'."
domcox@8443 347 :type 'hook
domcox@8443 348 :group 'c)
domcox@8443 349
domcox@8443 350 ;;; The entry point into the mode
domcox@8443 351 ;;;###autoload
domcox@8443 352 (defun vala-mode ()
domcox@8443 353 "Major mode for editing Vala code.
domcox@8443 354 This is a simple example of a separate mode derived from CC Mode
domcox@8443 355 to support a language with syntax similar to
domcox@8443 356 C#/C/C++/ObjC/Java/IDL/Pike.
domcox@8443 357
domcox@8443 358 The hook `c-mode-common-hook' is run with no args at mode
domcox@8443 359 initialization, then `vala-mode-hook'.
domcox@8443 360
domcox@8443 361 Key bindings:
domcox@8443 362 \\{vala-mode-map}"
domcox@8443 363 (interactive)
domcox@8443 364 (kill-all-local-variables)
domcox@8443 365 (c-initialize-cc-mode t)
domcox@8443 366 (set-syntax-table vala-mode-syntax-table)
domcox@8443 367 (setq major-mode 'vala-mode
domcox@8443 368 mode-name "Vala"
domcox@8443 369 local-abbrev-table vala-mode-abbrev-table
domcox@8443 370 abbrev-mode t)
domcox@8443 371 (use-local-map c-mode-map)
domcox@8443 372 ;; `c-init-language-vars' is a macro that is expanded at compile
domcox@8443 373 ;; time to a large `setq' with all the language variables and their
domcox@8443 374 ;; customized values for our language.
domcox@8443 375 (c-init-language-vars vala-mode)
domcox@8443 376 ;; `c-common-init' initializes most of the components of a CC Mode
domcox@8443 377 ;; buffer, including setup of the mode menu, font-lock, etc.
domcox@8443 378 ;; There's also a lower level routine `c-basic-common-init' that
domcox@8443 379 ;; only makes the necessary initialization to get the syntactic
domcox@8443 380 ;; analysis and similar things working.
domcox@8443 381 (c-common-init 'vala-mode)
domcox@8443 382 ;;(easy-menu-add vala-menu)
domcox@8443 383 (c-set-style "linux")
domcox@8443 384 (setq indent-tabs-mode t)
domcox@8443 385 (setq c-basic-offset 4)
domcox@8443 386 (setq tab-width 4)
domcox@8443 387 (c-toggle-auto-newline -1)
domcox@8443 388 (c-toggle-hungry-state -1)
domcox@8443 389 (run-hooks 'c-mode-common-hook)
domcox@8443 390 (run-hooks 'vala-mode-hook)
domcox@8443 391 (c-update-modeline))
domcox@8443 392
domcox@8443 393 (provide 'vala-mode)
domcox@8443 394
domcox@8443 395 ;;; vala-mode.el ends here