annotate .elisp/yaml-mode.el @ 77:45d7441d0cf2

Modularize .emacs
author Augie Fackler <durin42@gmail.com>
date Thu, 09 Apr 2009 13:39:20 -0500
parents d6ea7c108453
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
1 ;;; yaml-mode.el --- Major mode for editing YAML files
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
2
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
3 ;; Copyright (C) 2006 Yoshiki Kurihara
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
4
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
5 ;; Author: Yoshiki Kurihara <kurihara@cpan.org>
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
6 ;; Marshall T. Vandegrift <llasram@gmail.com>
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
7 ;; Keywords: data yaml
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
8 ;; Version: 0.0.3
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
9
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
10 ;; This file is not part of Emacs
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
11
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
12 ;; This file is free software; you can redistribute it and/or modify
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
13 ;; it under the terms of the GNU General Public License as published by
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
15 ;; any later version.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
16
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
17 ;; This file is distributed in the hope that it will be useful,
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
20 ;; GNU General Public License for more details.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
21
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
24 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
25 ;; Boston, MA 02111-1307, USA.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
26
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
27 ;;; Commentary:
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
28
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
29 ;; This is a major mode for editing files in the YAML data
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
30 ;; serialization format. It was initially developed by Yoshiki
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
31 ;; Kurihara and many features were added by Marshall Vandegrift. As
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
32 ;; YAML and Python share the fact that indentation determines
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
33 ;; structure, this mode provides indentation and indentation command
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
34 ;; behavior very similar to that of python-mode.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
35
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
36 ;;; Installation:
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
37
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
38 ;; To install, just drop this file into a directory in your
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
39 ;; `load-path' and (optionally) byte-compile it. To automatically
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
40 ;; handle files ending in '.yml', add something like:
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
41 ;;
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
42 ;; (require 'yaml-mode)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
43 ;; (add-to-list 'auto-mode-alist '("\\.yml$" . yaml-mode))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
44 ;;
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
45 ;; to your .emacs file.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
46 ;;
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
47 ;; Unlike python-mode, this mode follows the Emacs convention of not
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
48 ;; binding the ENTER key to `newline-and-indent'. To get this
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
49 ;; behavior, add the key definition to `yaml-mode-hook':
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
50 ;;
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
51 ;; (add-hook 'yaml-mode-hook
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
52 ;; '(lambda ()
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
53 ;; (define-key yaml-mode-map "\C-m" 'newline-and-indent)))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
54
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
55 ;;; Known Bugs:
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
56
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
57 ;; YAML is easy to write but complex to parse, and this mode doesn't
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
58 ;; even really try. Indentation and highlighting will break on
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
59 ;; abnormally complicated structures.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
60
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
61 ;;; Code:
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
62
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
63
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
64 ;; User definable variables
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
65
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
66 (defgroup yaml nil
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
67 "Support for the YAML serialization format"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
68 :group 'languages
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
69 :prefix "yaml-")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
70
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
71 (defcustom yaml-mode-hook nil
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
72 "*Hook run by `yaml-mode'."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
73 :type 'hook
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
74 :group 'yaml)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
75
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
76 (defcustom yaml-indent-offset 2
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
77 "*Amount of offset per level of indentation."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
78 :type 'integer
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
79 :group 'yaml)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
80
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
81 (defcustom yaml-backspace-function 'backward-delete-char-untabify
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
82 "*Function called by `yaml-electric-backspace' when deleting backwards."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
83 :type 'function
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
84 :group 'yaml)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
85
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
86 (defcustom yaml-block-literal-search-lines 100
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
87 "*Maximum number of lines to search for start of block literals."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
88 :type 'integer
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
89 :group 'yaml)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
90
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
91 (defcustom yaml-block-literal-electric-alist
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
92 '((?| . "") (?> . "-"))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
93 "*Characters for which to provide electric behavior.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
94 The association list key should be a key code and the associated value
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
95 should be a string containing additional characters to insert when
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
96 that key is pressed to begin a block literal."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
97 :type 'alist
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
98 :group 'yaml)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
99
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
100 (defface yaml-tab-face
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
101 '((((class color)) (:background "red" :foreground "red" :bold t))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
102 (t (:reverse-video t)))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
103 "Face to use for highlighting tabs in YAML files."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
104 :group 'faces
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
105 :group 'yaml)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
106
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
107
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
108 ;; Constants
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
109
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
110 (defconst yaml-mode-version "0.0.3" "Version of `yaml-mode.'")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
111
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
112 (defconst yaml-blank-line-re "^ *$"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
113 "Regexp matching a line containing only (valid) whitespace.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
114
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
115 (defconst yaml-comment-re "\\(#*.*\\)"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
116 "Regexp matching a line containing a YAML comment or delimiter.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
117
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
118 (defconst yaml-directive-re "^\\(?:--- \\)? *%\\(\\w+\\)"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
119 "Regexp matching a line contatining a YAML directive.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
120
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
121 (defconst yaml-document-delimiter-re "^ *\\(?:---\\|[.][.][.]\\)"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
122 "Rexexp matching a YAML document delimiter line.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
123
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
124 (defconst yaml-node-anchor-alias-re "[&*]\\w+"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
125 "Regexp matching a YAML node anchor or alias.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
126
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
127 (defconst yaml-tag-re "!!?[^ \n]+"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
128 "Rexexp matching a YAML tag.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
129
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
130 (defconst yaml-bare-scalar-re
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
131 "\\(?:[^-:,#!\n{\\[ ]\\|[^#!\n{\\[ ]\\S-\\)[^#\n]*?"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
132 "Rexexp matching a YAML bare scalar.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
133
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
134 (defconst yaml-hash-key-re
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
135 (concat "\\(?:^\\(?:--- \\)?\\|{\\|\\(?:[-,] +\\)+\\) *"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
136 "\\(?:" yaml-tag-re " +\\)?"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
137 "\\(" yaml-bare-scalar-re "\\) *:"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
138 "\\(?: +\\|$\\)")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
139 "Regexp matching a single YAML hash key.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
140
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
141 (defconst yaml-scalar-context-re
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
142 (concat "\\(?:^\\(?:--- \\)?\\|{\\|\\(?:[-,] +\\)+\\) *"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
143 "\\(?:" yaml-bare-scalar-re " *: \\)?")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
144 "Regexp indicating the begininng of a scalar context.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
145
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
146 (defconst yaml-nested-map-re
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
147 (concat ".*: *\\(?:&.*\\|{ *\\|" yaml-tag-re " *\\)?$")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
148 "Regexp matching a line beginning a YAML nested structure.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
149
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
150 (defconst yaml-block-literal-base-re " *[>|][-+0-9]* *\\(?:\n\\|\\'\\)"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
151 "Regexp matching the substring start of a block literal.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
152
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
153 (defconst yaml-block-literal-re
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
154 (concat yaml-scalar-context-re
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
155 "\\(?:" yaml-tag-re "\\)?"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
156 yaml-block-literal-base-re)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
157 "Regexp matching a line beginning a YAML block literal")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
158
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
159 (defconst yaml-nested-sequence-re
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
160 (concat "^\\(?: *- +\\)+"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
161 "\\(?:" yaml-bare-scalar-re " *:\\(?: +.*\\)?\\)?$")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
162 "Regexp matching a line containing one or more nested YAML sequences")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
163
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
164 (defconst yaml-constant-scalars-re
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
165 (concat "\\(?:^\\|\\(?::\\|-\\|,\\|{\\|\\[\\) +\\) *"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
166 (regexp-opt
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
167 '("~" "null" "Null" "NULL"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
168 ".nan" ".NaN" ".NAN"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
169 ".inf" ".Inf" ".INF"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
170 "-.inf" "-.Inf" "-.INF"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
171 "y" "Y" "yes" "Yes" "YES" "n" "N" "no" "No" "NO"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
172 "true" "True" "TRUE" "false" "False" "FALSE"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
173 "on" "On" "ON" "off" "Off" "OFF") t)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
174 " *$")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
175 "Regexp matching certain scalar constants in scalar context")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
176
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
177
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
178 ;; Mode setup
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
179
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
180 (defvar yaml-mode-map ()
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
181 "Keymap used in `yaml-mode' buffers.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
182 (if yaml-mode-map
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
183 nil
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
184 (setq yaml-mode-map (make-sparse-keymap))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
185 (define-key yaml-mode-map "|" 'yaml-electric-bar-and-angle)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
186 (define-key yaml-mode-map ">" 'yaml-electric-bar-and-angle)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
187 (define-key yaml-mode-map "-" 'yaml-electric-dash-and-dot)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
188 (define-key yaml-mode-map "." 'yaml-electric-dash-and-dot)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
189 (define-key yaml-mode-map [backspace] 'yaml-electric-backspace)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
190 (define-key yaml-mode-map "\C-j" 'newline-and-indent))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
191
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
192 (defvar yaml-mode-syntax-table nil
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
193 "Syntax table in use in yaml-mode buffers.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
194 (if yaml-mode-syntax-table
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
195 nil
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
196 (setq yaml-mode-syntax-table (make-syntax-table))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
197 (modify-syntax-entry ?\' "\"" yaml-mode-syntax-table)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
198 (modify-syntax-entry ?\" "\"" yaml-mode-syntax-table)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
199 (modify-syntax-entry ?# "<" yaml-mode-syntax-table)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
200 (modify-syntax-entry ?\n ">" yaml-mode-syntax-table)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
201 (modify-syntax-entry ?\\ "\\" yaml-mode-syntax-table)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
202 (modify-syntax-entry ?- "." yaml-mode-syntax-table)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
203 (modify-syntax-entry ?_ "_" yaml-mode-syntax-table)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
204 (modify-syntax-entry ?\( "." yaml-mode-syntax-table)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
205 (modify-syntax-entry ?\) "." yaml-mode-syntax-table)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
206 (modify-syntax-entry ?\{ "(}" yaml-mode-syntax-table)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
207 (modify-syntax-entry ?\} "){" yaml-mode-syntax-table)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
208 (modify-syntax-entry ?\[ "(]" yaml-mode-syntax-table)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
209 (modify-syntax-entry ?\] ")[" yaml-mode-syntax-table))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
210
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
211 (define-derived-mode yaml-mode fundamental-mode "YAML"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
212 "Simple mode to edit YAML.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
213
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
214 \\{yaml-mode-map}"
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
215 (set (make-local-variable 'comment-start) "# ")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
216 (set (make-local-variable 'comment-start-skip) "#+ *")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
217 (set (make-local-variable 'indent-line-function) 'yaml-indent-line)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
218 (set (make-local-variable 'font-lock-defaults)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
219 '(yaml-font-lock-keywords
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
220 nil nil nil nil
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
221 (font-lock-syntactic-keywords . yaml-font-lock-syntactic-keywords))))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
222
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
223
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
224 ;; Font-lock support
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
225
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
226 (defvar yaml-font-lock-keywords
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
227 (list
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
228 (cons yaml-comment-re '(1 font-lock-comment-face))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
229 (cons yaml-constant-scalars-re '(1 font-lock-constant-face))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
230 (cons yaml-tag-re '(0 font-lock-type-face))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
231 (cons yaml-node-anchor-alias-re '(0 font-lock-function-name-face t))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
232 (cons yaml-hash-key-re '(1 font-lock-variable-name-face t))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
233 (cons yaml-document-delimiter-re '(0 font-lock-comment-face))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
234 (cons yaml-directive-re '(1 font-lock-builtin-face))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
235 '(yaml-font-lock-block-literals 0 font-lock-string-face t)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
236 '("^[\t]+" 0 'yaml-tab-face t))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
237 "Additional expressions to highlight in YAML mode.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
238
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
239 (defvar yaml-font-lock-syntactic-keywords
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
240 (list '(yaml-syntactic-block-literals 0 "." t))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
241 "Additional syntax features to highlight in YAML mode.")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
242
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
243
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
244 (defun yaml-font-lock-block-literals (bound)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
245 "Find lines within block literals.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
246 Find the next line of the first (if any) block literal after point and
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
247 prior to BOUND. Returns the beginning and end of the block literal
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
248 line in the match data, as consumed by `font-lock-keywords' matcher
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
249 functions. The function begins by searching backwards to determine
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
250 whether or not the current line is within a block literal. This could
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
251 be time-consuming in large buffers, so the number of lines searched is
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
252 artificially limitted to the value of
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
253 `yaml-block-literal-search-lines'."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
254 (if (eolp) (goto-char (1+ (point))))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
255 (unless (or (eobp) (>= (point) bound))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
256 (let ((begin (point))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
257 (end (min (1+ (point-at-eol)) bound)))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
258 (goto-char (point-at-bol))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
259 (while (and (looking-at yaml-blank-line-re) (not (bobp)))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
260 (forward-line -1))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
261 (let ((nlines yaml-block-literal-search-lines)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
262 (min-level (current-indentation)))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
263 (forward-line -1)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
264 (while (and (/= nlines 0)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
265 (/= min-level 0)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
266 (not (looking-at yaml-block-literal-re))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
267 (not (bobp)))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
268 (set 'nlines (1- nlines))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
269 (unless (looking-at yaml-blank-line-re)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
270 (set 'min-level (min min-level (current-indentation))))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
271 (forward-line -1))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
272 (cond
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
273 ((and (< (current-indentation) min-level)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
274 (looking-at yaml-block-literal-re))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
275 (goto-char end) (set-match-data (list begin end)) t)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
276 ((progn
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
277 (goto-char begin)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
278 (re-search-forward (concat yaml-block-literal-re
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
279 " *\\(.*\\)\n")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
280 bound t))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
281 (set-match-data (nthcdr 2 (match-data))) t))))))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
282
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
283 (defun yaml-syntactic-block-literals (bound)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
284 "Find quote characters within block literals.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
285 Finds the first quote character within a block literal (if any) after
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
286 point and prior to BOUND. Returns the position of the quote character
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
287 in the match data, as consumed by matcher functions in
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
288 `font-lock-syntactic-keywords'. This allows the mode to treat ['\"]
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
289 characters in block literals as punctuation syntax instead of string
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
290 syntax, preventing unmatched quotes in block literals from painting
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
291 the entire buffer in `font-lock-string-face'."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
292 (let ((found nil))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
293 (while (and (not found)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
294 (/= (point) bound)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
295 (yaml-font-lock-block-literals bound))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
296 (let ((begin (match-beginning 0)) (end (match-end 0)))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
297 (goto-char begin)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
298 (cond
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
299 ((re-search-forward "['\"]" end t) (setq found t))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
300 ((goto-char end)))))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
301 found))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
302
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
303
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
304 ;; Indentation and electric keys
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
305
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
306 (defun yaml-compute-indentation ()
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
307 "Calculate the maximum sensible indentation for the current line."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
308 (save-excursion
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
309 (beginning-of-line)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
310 (if (looking-at yaml-document-delimiter-re) 0
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
311 (forward-line -1)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
312 (while (and (looking-at yaml-blank-line-re)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
313 (> (point) (point-min)))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
314 (forward-line -1))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
315 (+ (current-indentation)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
316 (if (looking-at yaml-nested-map-re) yaml-indent-offset 0)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
317 (if (looking-at yaml-nested-sequence-re) yaml-indent-offset 0)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
318 (if (looking-at yaml-block-literal-re) yaml-indent-offset 0)))))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
319
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
320 (defun yaml-indent-line ()
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
321 "Indent the current line.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
322 The first time this command is used, the line will be indented to the
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
323 maximum sensible indentation. Each immediately subsequent usage will
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
324 back-dent the line by `yaml-indent-offset' spaces. On reaching column
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
325 0, it will cycle back to the maximum sensible indentation."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
326 (interactive "*")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
327 (let ((ci (current-indentation))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
328 (cc (current-column))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
329 (need (yaml-compute-indentation)))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
330 (save-excursion
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
331 (beginning-of-line)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
332 (delete-horizontal-space)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
333 (if (and (equal last-command this-command) (/= ci 0))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
334 (indent-to (* (/ (- ci 1) yaml-indent-offset) yaml-indent-offset))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
335 (indent-to need)))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
336 (if (< (current-column) (current-indentation))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
337 (forward-to-indentation 0))))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
338
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
339 (defun yaml-electric-backspace (arg)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
340 "Delete characters or back-dent the current line.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
341 If invoked following only whitespace on a line, will back-dent to the
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
342 immediately previous multiple of `yaml-indent-offset' spaces."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
343 (interactive "*p")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
344 (if (or (/= (current-indentation) (current-column)) (bolp))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
345 (funcall yaml-backspace-function arg)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
346 (let ((ci (current-column)))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
347 (beginning-of-line)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
348 (delete-horizontal-space)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
349 (indent-to (* (/ (- ci (* arg yaml-indent-offset))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
350 yaml-indent-offset)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
351 yaml-indent-offset)))))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
352
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
353 (defun yaml-electric-bar-and-angle (arg)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
354 "Insert the bound key and possibly begin a block literal.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
355 Inserts the bound key. If inserting the bound key causes the current
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
356 line to match the initial line of a block literal, then inserts the
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
357 matching string from `yaml-block-literal-electric-alist', a newline,
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
358 and indents appropriately."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
359 (interactive "*P")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
360 (self-insert-command (prefix-numeric-value arg))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
361 (let ((extra-chars
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
362 (assoc last-command-char
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
363 yaml-block-literal-electric-alist)))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
364 (cond
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
365 ((and extra-chars (not arg) (eolp)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
366 (save-excursion
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
367 (beginning-of-line)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
368 (looking-at yaml-block-literal-re)))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
369 (insert (cdr extra-chars))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
370 (newline-and-indent)))))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
371
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
372 (defun yaml-electric-dash-and-dot (arg)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
373 "Insert the bound key and possibly de-dent line.
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
374 Inserts the bound key. If inserting the bound key causes the current
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
375 line to match a document delimiter, de-dent the line to the left
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
376 margin."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
377 (interactive "*P")
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
378 (self-insert-command (prefix-numeric-value arg))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
379 (save-excursion
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
380 (beginning-of-line)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
381 (if (and (not arg) (looking-at yaml-document-delimiter-re))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
382 (delete-horizontal-space))))
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
383
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
384 (defun yaml-mode-version ()
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
385 "Diplay version of `yaml-mode'."
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
386 (interactive)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
387 (message "yaml-mode %s" yaml-mode-version)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
388 yaml-mode-version)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
389
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
390 (provide 'yaml-mode)
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
391
d6ea7c108453 Add yaml mode.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
392 ;;; yaml-mode.el ends here