haml 2.0.10 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of haml might be problematic. Click here for more details.

Files changed (107) hide show
  1. data/.yardopts +5 -0
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +347 -0
  4. data/Rakefile +124 -19
  5. data/VERSION +1 -1
  6. data/VERSION_NAME +1 -0
  7. data/extra/haml-mode.el +397 -78
  8. data/extra/sass-mode.el +148 -36
  9. data/extra/update_watch.rb +13 -0
  10. data/lib/haml.rb +15 -993
  11. data/lib/haml/buffer.rb +131 -84
  12. data/lib/haml/engine.rb +129 -97
  13. data/lib/haml/error.rb +7 -7
  14. data/lib/haml/exec.rb +127 -42
  15. data/lib/haml/filters.rb +107 -42
  16. data/lib/haml/helpers.rb +210 -156
  17. data/lib/haml/helpers/action_view_extensions.rb +34 -39
  18. data/lib/haml/helpers/action_view_mods.rb +132 -139
  19. data/lib/haml/html.rb +77 -65
  20. data/lib/haml/precompiler.rb +404 -213
  21. data/lib/haml/shared.rb +78 -0
  22. data/lib/haml/template.rb +14 -14
  23. data/lib/haml/template/patch.rb +2 -2
  24. data/lib/haml/template/plugin.rb +2 -3
  25. data/lib/haml/util.rb +211 -6
  26. data/lib/haml/version.rb +30 -13
  27. data/lib/sass.rb +7 -856
  28. data/lib/sass/css.rb +169 -161
  29. data/lib/sass/engine.rb +344 -328
  30. data/lib/sass/environment.rb +79 -0
  31. data/lib/sass/error.rb +33 -11
  32. data/lib/sass/files.rb +139 -0
  33. data/lib/sass/plugin.rb +160 -117
  34. data/lib/sass/plugin/merb.rb +7 -6
  35. data/lib/sass/plugin/rails.rb +5 -6
  36. data/lib/sass/repl.rb +58 -0
  37. data/lib/sass/script.rb +59 -0
  38. data/lib/sass/script/bool.rb +17 -0
  39. data/lib/sass/script/color.rb +183 -0
  40. data/lib/sass/script/funcall.rb +50 -0
  41. data/lib/sass/script/functions.rb +198 -0
  42. data/lib/sass/script/lexer.rb +178 -0
  43. data/lib/sass/script/literal.rb +177 -0
  44. data/lib/sass/script/node.rb +14 -0
  45. data/lib/sass/script/number.rb +381 -0
  46. data/lib/sass/script/operation.rb +45 -0
  47. data/lib/sass/script/parser.rb +172 -0
  48. data/lib/sass/script/string.rb +12 -0
  49. data/lib/sass/script/unary_operation.rb +34 -0
  50. data/lib/sass/script/variable.rb +31 -0
  51. data/lib/sass/tree/comment_node.rb +73 -10
  52. data/lib/sass/tree/debug_node.rb +30 -0
  53. data/lib/sass/tree/directive_node.rb +42 -17
  54. data/lib/sass/tree/file_node.rb +41 -0
  55. data/lib/sass/tree/for_node.rb +48 -0
  56. data/lib/sass/tree/if_node.rb +54 -0
  57. data/lib/sass/tree/mixin_def_node.rb +29 -0
  58. data/lib/sass/tree/mixin_node.rb +48 -0
  59. data/lib/sass/tree/node.rb +214 -11
  60. data/lib/sass/tree/prop_node.rb +109 -0
  61. data/lib/sass/tree/rule_node.rb +178 -51
  62. data/lib/sass/tree/variable_node.rb +34 -0
  63. data/lib/sass/tree/while_node.rb +31 -0
  64. data/test/haml/engine_test.rb +331 -36
  65. data/test/haml/helper_test.rb +12 -1
  66. data/test/haml/results/content_for_layout.xhtml +0 -3
  67. data/test/haml/results/filters.xhtml +2 -0
  68. data/test/haml/results/list.xhtml +1 -1
  69. data/test/haml/template_test.rb +7 -2
  70. data/test/haml/templates/content_for_layout.haml +0 -2
  71. data/test/haml/templates/list.haml +1 -1
  72. data/test/haml/util_test.rb +92 -0
  73. data/test/sass/css2sass_test.rb +69 -24
  74. data/test/sass/engine_test.rb +586 -64
  75. data/test/sass/functions_test.rb +125 -0
  76. data/test/sass/more_results/more1.css +9 -0
  77. data/test/sass/more_results/more1_with_line_comments.css +26 -0
  78. data/test/sass/more_results/more_import.css +29 -0
  79. data/test/sass/more_templates/_more_partial.sass +2 -0
  80. data/test/sass/more_templates/more1.sass +23 -0
  81. data/test/sass/more_templates/more_import.sass +11 -0
  82. data/test/sass/plugin_test.rb +81 -28
  83. data/test/sass/results/line_numbers.css +49 -0
  84. data/test/sass/results/{constants.css → script.css} +4 -4
  85. data/test/sass/results/subdir/subdir.css +2 -0
  86. data/test/sass/results/units.css +11 -0
  87. data/test/sass/script_test.rb +258 -0
  88. data/test/sass/templates/import.sass +1 -1
  89. data/test/sass/templates/importee.sass +7 -2
  90. data/test/sass/templates/line_numbers.sass +13 -0
  91. data/test/sass/templates/{constants.sass → script.sass} +11 -10
  92. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  93. data/test/sass/templates/subdir/subdir.sass +2 -2
  94. data/test/sass/templates/units.sass +11 -0
  95. data/test/test_helper.rb +14 -0
  96. metadata +77 -19
  97. data/FAQ +0 -138
  98. data/README.rdoc +0 -319
  99. data/lib/sass/constant.rb +0 -216
  100. data/lib/sass/constant/color.rb +0 -101
  101. data/lib/sass/constant/literal.rb +0 -54
  102. data/lib/sass/constant/nil.rb +0 -9
  103. data/lib/sass/constant/number.rb +0 -87
  104. data/lib/sass/constant/operation.rb +0 -30
  105. data/lib/sass/constant/string.rb +0 -22
  106. data/lib/sass/tree/attr_node.rb +0 -57
  107. data/lib/sass/tree/value_node.rb +0 -20
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.10
1
+ 2.2.0
@@ -0,0 +1 @@
1
+ Powerful Penny
@@ -1,20 +1,27 @@
1
- ;;; haml-mode.el -- Major mode for editing Haml files
2
- ;;; Written by Nathan Weizenbaum
1
+ ;;; haml-mode.el --- Major mode for editing Haml files
3
2
 
4
- ;;; Because Haml's indentation schema is similar
5
- ;;; to that of YAML and Python, many indentation-related
6
- ;;; functions are similar to those in yaml-mode and python-mode.
3
+ ;; Copyright (c) 2007, 2008 Nathan Weizenbaum
7
4
 
8
- ;;; To install, save this somewhere and add the following to your .emacs file:
9
- ;;;
10
- ;;; (add-to-list 'load-path "/path/to/haml-mode.el")
11
- ;;; (require 'haml-mode nil 't)
12
- ;;; (add-to-list 'auto-mode-alist '("\\.haml$" . haml-mode))
13
- ;;;
5
+ ;; Author: Nathan Weizenbaum
6
+ ;; URL: http://github.com/nex3/haml/tree/master
7
+ ;; Version: 1.0
8
+ ;; Keywords: markup, language
9
+
10
+ ;;; Commentary:
11
+
12
+ ;; Because Haml's indentation schema is similar
13
+ ;; to that of YAML and Python, many indentation-related
14
+ ;; functions are similar to those in yaml-mode and python-mode.
15
+
16
+ ;; To install, save this on your load path and add the following to
17
+ ;; your .emacs file:
18
+ ;;
19
+ ;; (require 'haml-mode)
14
20
 
15
21
  ;;; Code:
16
22
 
17
23
  (eval-when-compile (require 'cl))
24
+ (require 'ruby-mode)
18
25
 
19
26
  ;; User definable variables
20
27
 
@@ -48,14 +55,22 @@ line itself."
48
55
  :group 'haml)
49
56
 
50
57
  (defvar haml-indent-function 'haml-indent-p
51
- "This function should look at the current line and return true
52
- if the next line could be nested within this line.")
58
+ "This function should look at the current line and return t
59
+ if the next line could be nested within this line.
60
+
61
+ The function can also return a positive integer to indicate
62
+ a specific level to which the current line could be indented.")
63
+
64
+ (defconst haml-tag-beg-re
65
+ "^ *\\(?:[%\\.#][a-z0-9_:\\-]*\\)+\\(?:(.*)\\|{.*}\\|\\[.*\\]\\)*"
66
+ "A regexp matching the beginning of a Haml tag, through (), {}, and [].")
53
67
 
54
68
  (defvar haml-block-openers
55
- `("^ *\\([%\\.#][^ \t]*\\)\\(\\[.*\\]\\)?\\({.*}\\)?\\(\\[.*\\]\\)?[ \t]*$"
56
- "^ *[-=].*do[ \t]*\\(|.*|[ \t]*\\)?$"
57
- ,(concat "^ *-[ \t]*\\("
58
- (regexp-opt '("else" "elsif" "rescue" "ensure" "when"))
69
+ `(,(concat haml-tag-beg-re "[><]*[ \t]*$")
70
+ "^ *[&!]?[-=~].*do[ \t]*\\(|.*|[ \t]*\\)?$"
71
+ ,(concat "^ *[&!]?[-=~][ \t]*\\("
72
+ (regexp-opt '("if" "unless" "while" "until" "else"
73
+ "begin" "elsif" "rescue" "ensure" "when"))
59
74
  "\\)")
60
75
  "^ */\\(\\[.*\\]\\)?[ \t]*$"
61
76
  "^ *-#"
@@ -65,32 +80,196 @@ text nested beneath them.")
65
80
 
66
81
  ;; Font lock
67
82
 
83
+ (defun haml-nested-regexp (re)
84
+ (concat "^\\( *\\)" re "\\(\n\\(?:\\(?:\\1 .*\\| *\\)\n\\)*\\(?:\\1 .*\\| *\\)?\\)?"))
85
+
68
86
  (defconst haml-font-lock-keywords
69
- '(("^ *\\(\t\\)" 1 'haml-tab-face)
70
- ("^!!!.*" 0 font-lock-constant-face)
71
- ("\\('[^']*'\\)" 1 font-lock-string-face append)
72
- ("\\(\"[^\"]*\"\\)" 1 font-lock-string-face append)
73
- ("&?:\\w+" 0 font-lock-constant-face append)
74
- ("@[a-z0-9_]+" 0 font-lock-variable-name-face append)
75
- ("| *$" 0 font-lock-string-face)
76
- ("^[ \t]*\\(/.*\\)$" 1 font-lock-comment-face append)
77
- ("^ *\\(#[a-z0-9_]+\/?\\)" 1 font-lock-keyword-face)
78
- ("^ *\\(\\.[a-z0-9_]+\/?\\)" 1 font-lock-type-face)
79
- ("^ *\\(%[a-z0-9_]+\/?\\)" 1 font-lock-function-name-face)
80
- ("^ *\\(#[a-z0-9_]+\/?\\)" (1 font-lock-keyword-face)
81
- ("\\.[a-z0-9_]+" nil nil (0 font-lock-type-face)))
82
- ("^ *\\(\\.[a-z0-9_]+\/?\\)" (1 font-lock-type-face)
83
- ("\\.[a-z0-9_]+" nil nil (0 font-lock-type-face)))
84
- ("^ *\\(\\.[a-z0-9_]+\/?\\)" (1 font-lock-type-face)
85
- ("\\#[a-z0-9_]+" nil nil (0 font-lock-keyword-face)))
86
- ("^ *\\(%[a-z0-9_]+\/?\\)" (1 font-lock-function-name-face)
87
- ("\\.[a-z0-9_]+" nil nil (0 font-lock-type-face)))
88
- ("^ *\\(%[a-z0-9_]+\/?\\)" (1 font-lock-function-name-face)
89
- ("\\#[a-z0-9_]+" nil nil (0 font-lock-keyword-face)))
90
- ("^ *\\([~=-] .*\\)" 1 font-lock-preprocessor-face prepend)
91
- ("^ *[\\.#%a-z0-9_]+\\([~=-] .*\\)" 1 font-lock-preprocessor-face prepend)
92
- ("^ *[\\.#%a-z0-9_]+\\({[^}]+}\\)" 1 font-lock-preprocessor-face prepend)
93
- ("^ *[\\.#%a-z0-9_]+\\(\\[[^]]+\\]\\)" 1 font-lock-preprocessor-face prepend)))
87
+ `((,(haml-nested-regexp "\\(?:-#\\|/\\).*") 0 font-lock-comment-face)
88
+ (,(haml-nested-regexp ":\\w+") 0 font-lock-string-face)
89
+ (haml-highlight-interpolation 1 font-lock-variable-name-face prepend)
90
+ (haml-highlight-ruby-tag 1 font-lock-preprocessor-face)
91
+ (haml-highlight-ruby-script 1 font-lock-preprocessor-face)
92
+ ("^ *\\(\t\\)" 1 'haml-tab-face)
93
+ ("^!!!.*" 0 font-lock-constant-face)
94
+ ("| *$" 0 font-lock-string-face)))
95
+
96
+ (defconst haml-filter-re "^ *:\\w+")
97
+ (defconst haml-comment-re "^ *\\(?:-\\#\\|/\\)")
98
+
99
+ (defun haml-fontify-region-as-ruby (beg end)
100
+ "Use Ruby's font-lock variables to fontify the region between BEG and END."
101
+ (save-excursion
102
+ (save-match-data
103
+ (let ((font-lock-keywords ruby-font-lock-keywords)
104
+ (font-lock-syntactic-keywords ruby-font-lock-syntactic-keywords)
105
+ font-lock-keywords-only
106
+ font-lock-extend-region-functions
107
+ font-lock-keywords-case-fold-search)
108
+ ;; font-lock-fontify-region apparently isn't inclusive,
109
+ ;; so we have to move the beginning back one char
110
+ (font-lock-fontify-region (- beg 1) end)))))
111
+
112
+ (defun haml-highlight-ruby-script (limit)
113
+ "Highlight a Ruby script expression (-, =, or ~)."
114
+ (when (re-search-forward "^ *\\(-\\|[&!]?[=~]\\) \\(.*\\)$" limit t)
115
+ (haml-fontify-region-as-ruby (match-beginning 2) (match-end 2))))
116
+
117
+ (defun haml-highlight-ruby-tag (limit)
118
+ "Highlight Ruby code within a Haml tag.
119
+
120
+ This highlights the tag attributes and object refs of the tag,
121
+ as well as the script expression (-, =, or ~) following the tag.
122
+
123
+ For example, this will highlight all of the following:
124
+ %p{:foo => 'bar'}
125
+ %p[@bar]
126
+ %p= 'baz'
127
+ %p{:foo => 'bar'}[@bar]= 'baz'"
128
+ (when (re-search-forward "^ *[%.#]" limit t)
129
+ (forward-char -1)
130
+
131
+ ;; Highlight tag, classes, and ids
132
+ (while (haml-move "\\([.#%]\\)[a-z0-9_:\\-]*")
133
+ (put-text-property (match-beginning 0) (match-end 0) 'face
134
+ (case (char-after (match-beginning 1))
135
+ (?% font-lock-function-name-face)
136
+ (?# font-lock-keyword-face)
137
+ (?. font-lock-type-face))))
138
+
139
+ (block loop
140
+ (while t
141
+ (let ((eol (save-excursion (end-of-line) (point))))
142
+ (case (char-after)
143
+ ;; Highlight obj refs
144
+ (?\[
145
+ (let ((beg (point)))
146
+ (haml-limited-forward-sexp eol)
147
+ (haml-fontify-region-as-ruby beg (point))))
148
+ ;; Highlight new attr hashes
149
+ (?\(
150
+ (forward-char 1)
151
+ (while
152
+ (and (haml-parse-new-attr-hash
153
+ (lambda (type beg end)
154
+ (case type
155
+ (name (put-text-property beg end 'face font-lock-constant-face))
156
+ (value (haml-fontify-region-as-ruby beg end)))))
157
+ (not (eobp)))
158
+ (forward-line 1)
159
+ (beginning-of-line)))
160
+ ;; Highlight old attr hashes
161
+ (?\{
162
+ (let ((beg (point)))
163
+ (haml-limited-forward-sexp eol)
164
+
165
+ ;; Check for multiline
166
+ (while (and (eolp) (eq (char-before) ?,) (not (eobp)))
167
+ (forward-line)
168
+ (let ((eol (save-excursion (end-of-line) (point))))
169
+ ;; If no sexps are closed,
170
+ ;; we're still continuing a multiline hash
171
+ (if (>= (car (parse-partial-sexp (point) eol)) 0)
172
+ (end-of-line)
173
+ ;; If sexps have been closed,
174
+ ;; set the point at the end of the total sexp
175
+ (goto-char beg)
176
+ (haml-limited-forward-sexp eol))))
177
+
178
+ (haml-fontify-region-as-ruby (+ 1 beg) (point))))
179
+ (t (return-from loop))))))
180
+
181
+ ;; Move past end chars
182
+ (when (looking-at "[<>&!]+") (goto-char (match-end 0)))
183
+ ;; Highlight script
184
+ (if (looking-at "\\([=~]\\) \\(.*\\)$")
185
+ (haml-fontify-region-as-ruby (match-beginning 2) (match-end 2))
186
+ ;; Give font-lock something to highlight
187
+ (forward-char -1)
188
+ (looking-at "\\(\\)"))
189
+ t))
190
+
191
+ (defun haml-move (re)
192
+ "Try matching and moving to the end of a regular expression."
193
+ (when (looking-at re)
194
+ (goto-char (match-end 0))
195
+ t))
196
+
197
+ (defun haml-highlight-interpolation (limit)
198
+ "Highlight Ruby interpolation (#{foo})."
199
+ (when (re-search-forward "\\(#{\\)" limit t)
200
+ (save-match-data
201
+ (forward-char -1)
202
+ (let ((beg (point)))
203
+ (haml-limited-forward-sexp limit)
204
+ (haml-fontify-region-as-ruby (+ 1 beg) (point)))
205
+
206
+ (when (eq (char-before) ?})
207
+ (put-text-property (- (point) 1) (point)
208
+ 'face font-lock-variable-name-face))
209
+ t)))
210
+
211
+ (defun haml-limited-forward-sexp (limit &optional arg)
212
+ "Move forward using `forward-sexp' or to limit,
213
+ whichever comes first."
214
+ (let (forward-sexp-function)
215
+ (condition-case err
216
+ (save-restriction
217
+ (narrow-to-region (point) limit)
218
+ (forward-sexp arg))
219
+ (scan-error
220
+ (unless (equal (nth 1 err) "Unbalanced parentheses")
221
+ (signal 'scan-error (cdr err)))
222
+ (goto-char limit)))))
223
+
224
+ (defun* haml-extend-region-filters-comments ()
225
+ "Extend the font-lock region to encompass filters and comments."
226
+ (let ((old-beg font-lock-beg)
227
+ (old-end font-lock-end))
228
+ (save-excursion
229
+ (goto-char font-lock-beg)
230
+ (beginning-of-line)
231
+ (unless (or (looking-at haml-filter-re)
232
+ (looking-at haml-comment-re))
233
+ (return-from haml-extend-region-filters-comments))
234
+ (setq font-lock-beg (point))
235
+ (haml-forward-sexp)
236
+ (beginning-of-line)
237
+ (setq font-lock-end (max font-lock-end (point))))
238
+ (or (/= old-beg font-lock-beg)
239
+ (/= old-end font-lock-end))))
240
+
241
+ (defun* haml-extend-region-multiline-hashes ()
242
+ "Extend the font-lock region to encompass multiline attribute hashes."
243
+ (let ((old-beg font-lock-beg)
244
+ (old-end font-lock-end))
245
+ (save-excursion
246
+ (goto-char font-lock-beg)
247
+ (let ((attr-props (haml-parse-multiline-attr-hash))
248
+ multiline-end)
249
+ (when attr-props
250
+ (setq font-lock-beg (cdr (assq 'point attr-props)))
251
+
252
+ (end-of-line)
253
+ ;; Move through multiline attrs
254
+ (when (eq (char-before) ?,)
255
+ (save-excursion
256
+ (while (progn (end-of-line) (eq (char-before) ?,) (not (eobp)))
257
+ (forward-line))
258
+
259
+ (forward-line -1)
260
+ (end-of-line)
261
+ (setq multiline-end (point))))
262
+
263
+ (goto-char (+ (cdr (assq 'point attr-props))
264
+ (cdr (assq 'hash-indent attr-props))
265
+ -1))
266
+ (haml-limited-forward-sexp
267
+ (or multiline-end
268
+ (save-excursion (end-of-line) (point))))
269
+ (setq font-lock-end (max font-lock-end (point))))))
270
+ (or (/= old-beg font-lock-beg)
271
+ (/= old-end font-lock-end))))
272
+
94
273
 
95
274
  ;; Mode setup
96
275
 
@@ -105,11 +284,13 @@ text nested beneath them.")
105
284
  (let ((map (make-sparse-keymap)))
106
285
  (define-key map [backspace] 'haml-electric-backspace)
107
286
  (define-key map "\C-?" 'haml-electric-backspace)
108
- (define-key map "\C-\M-f" 'haml-forward-sexp)
109
- (define-key map "\C-\M-b" 'haml-backward-sexp)
110
- (define-key map "\C-\M-u" 'haml-up-list)
111
- (define-key map "\C-\M-d" 'haml-down-list)
112
- (define-key map "\C-C\C-k" 'haml-kill-line-and-indent)
287
+ (define-key map "\C-c\C-f" 'haml-forward-sexp)
288
+ (define-key map "\C-c\C-b" 'haml-backward-sexp)
289
+ (define-key map "\C-c\C-u" 'haml-up-list)
290
+ (define-key map "\C-c\C-d" 'haml-down-list)
291
+ (define-key map "\C-c\C-k" 'haml-kill-line-and-indent)
292
+ (define-key map "\C-c\C-r" 'haml-output-region)
293
+ (define-key map "\C-c\C-l" 'haml-output-buffer)
113
294
  map))
114
295
 
115
296
  ;;;###autoload
@@ -118,11 +299,68 @@ text nested beneath them.")
118
299
 
119
300
  \\{haml-mode-map}"
120
301
  (set-syntax-table haml-mode-syntax-table)
302
+ (add-to-list 'font-lock-extend-region-functions 'haml-extend-region-filters-comments)
303
+ (add-to-list 'font-lock-extend-region-functions 'haml-extend-region-multiline-hashes)
304
+ (set (make-local-variable 'font-lock-multiline) t)
121
305
  (set (make-local-variable 'indent-line-function) 'haml-indent-line)
122
306
  (set (make-local-variable 'indent-region-function) 'haml-indent-region)
123
- (set (make-local-variable 'forward-sexp-function) 'haml-forward-sexp)
307
+ (set (make-local-variable 'parse-sexp-lookup-properties) t)
308
+ (setq comment-start "-#")
124
309
  (setq indent-tabs-mode nil)
125
- (setq font-lock-defaults '((haml-font-lock-keywords) nil t)))
310
+ (setq font-lock-defaults '((haml-font-lock-keywords) t t)))
311
+
312
+ ;; Useful functions
313
+
314
+ (defun haml-comment-block ()
315
+ "Comment the current block of Haml code."
316
+ (interactive)
317
+ (save-excursion
318
+ (let ((indent (current-indentation)))
319
+ (back-to-indentation)
320
+ (insert "-#")
321
+ (newline)
322
+ (indent-to indent)
323
+ (beginning-of-line)
324
+ (haml-mark-sexp)
325
+ (haml-reindent-region-by haml-indent-offset))))
326
+
327
+ (defun haml-uncomment-block ()
328
+ "Uncomment the current block of Haml code."
329
+ (interactive)
330
+ (save-excursion
331
+ (beginning-of-line)
332
+ (while (not (looking-at haml-comment-re))
333
+ (haml-up-list)
334
+ (beginning-of-line))
335
+ (haml-mark-sexp)
336
+ (kill-line 1)
337
+ (haml-reindent-region-by (- haml-indent-offset))))
338
+
339
+ (defun haml-replace-region (start end)
340
+ "Replaces the current block of Haml code with the HTML equivalent."
341
+ (interactive "r")
342
+ (save-excursion
343
+ (goto-char end)
344
+ (setq end (point-marker))
345
+ (goto-char start)
346
+ (let ((ci (current-indentation)))
347
+ (while (re-search-forward "^ +" end t)
348
+ (replace-match (make-string (- (current-indentation) ci) ? ))))
349
+ (shell-command-on-region start end "haml" "haml-output" t)))
350
+
351
+ (defun haml-output-region (start end)
352
+ "Displays the HTML output for the current block of Haml code."
353
+ (interactive "r")
354
+ (kill-new (buffer-substring start end))
355
+ (with-temp-buffer
356
+ (yank)
357
+ (haml-indent-region (point-min) (point-max))
358
+ (shell-command-on-region (point-min) (point-max) "haml" "haml-output")))
359
+
360
+ (defun haml-output-buffer ()
361
+ "Displays the HTML output for entire buffer."
362
+ (interactive)
363
+ (haml-output-region (point-min) (point-max)))
126
364
 
127
365
  ;; Navigation
128
366
 
@@ -164,7 +402,7 @@ lines nested beneath it."
164
402
  (not (bobp))
165
403
  (> (current-indentation) indent)))
166
404
  (back-to-indentation)
167
- (setq arg (+ arg (if (> arg 0) -1 1)))))))
405
+ (setq arg (+ arg (if (> arg 0) -1 1)))))))
168
406
 
169
407
  (defun haml-backward-sexp (&optional arg)
170
408
  "Move backward across one nested expression.
@@ -204,11 +442,16 @@ With ARG, do this that many times."
204
442
  (setq arg (- arg 1))))
205
443
  (back-to-indentation))
206
444
 
445
+ (defun haml-mark-sexp ()
446
+ "Marks the next Haml block."
447
+ (let ((forward-sexp-function 'haml-forward-sexp))
448
+ (mark-sexp)))
449
+
207
450
  (defun haml-mark-sexp-but-not-next-line ()
208
- "Marks the next Haml sexp, but puts the mark at the end of the
451
+ "Marks the next Haml block, but puts the mark at the end of the
209
452
  last line of the sexp rather than the first non-whitespace
210
453
  character of the next line."
211
- (mark-sexp)
454
+ (haml-mark-sexp)
212
455
  (set-mark
213
456
  (save-excursion
214
457
  (goto-char (mark))
@@ -218,21 +461,89 @@ character of the next line."
218
461
 
219
462
  ;; Indentation and electric keys
220
463
 
221
- (defun haml-indent-p ()
222
- "Returns true if the current line can have lines nested beneath it."
464
+ (defun* haml-indent-p ()
465
+ "Returns t if the current line can have lines nested beneath it."
466
+ (let ((attr-props (haml-parse-multiline-attr-hash)))
467
+ (when attr-props
468
+ (return-from haml-indent-p
469
+ (if (haml-unclosed-attr-hash-p) (cdr (assq 'hash-indent attr-props))
470
+ (list (+ (cdr (assq 'indent attr-props)) haml-indent-offset) nil)))))
223
471
  (loop for opener in haml-block-openers
224
472
  if (looking-at opener) return t
225
473
  finally return nil))
226
474
 
475
+ (defun* haml-parse-multiline-attr-hash ()
476
+ "Parses a multiline attribute hash, and returns
477
+ an alist with the following keys:
478
+
479
+ INDENT is the indentation of the line beginning the hash.
480
+
481
+ HASH-INDENT is the indentation of the first character
482
+ within the attribute hash.
483
+
484
+ POINT is the character position at the beginning of the line
485
+ beginning the hash."
486
+ (save-excursion
487
+ (while t
488
+ (beginning-of-line)
489
+ (if (looking-at (eval-when-compile (concat haml-tag-beg-re "\\([{(]\\)")))
490
+ (progn
491
+ (goto-char (- (match-end 0) 1))
492
+ (haml-limited-forward-sexp (save-excursion (end-of-line) (point)))
493
+ (return-from haml-parse-multiline-attr-hash
494
+ (when (or (string-equal (match-string 1) "(") (eq (char-before) ?,))
495
+ `((indent . ,(current-indentation))
496
+ (hash-indent . ,(- (match-end 0) (match-beginning 0)))
497
+ (point . ,(match-beginning 0))))))
498
+ (when (bobp) (return-from haml-parse-multiline-attr-hash))
499
+ (forward-line -1)
500
+ (unless (haml-unclosed-attr-hash-p)
501
+ (return-from haml-parse-multiline-attr-hash))))))
502
+
503
+ (defun* haml-unclosed-attr-hash-p ()
504
+ "Return t if this line has an unclosed attribute hash, new or old."
505
+ (save-excursion
506
+ (end-of-line)
507
+ (when (eq (char-before) ?,) (return-from haml-unclosed-attr-hash-p t))
508
+ (re-search-backward "(\\|^")
509
+ (haml-move "(")
510
+ (haml-parse-new-attr-hash)))
511
+
512
+ (defun* haml-parse-new-attr-hash (&optional (fn (lambda (type beg end) ())))
513
+ "Parse a new-style attribute hash on this line, and returns
514
+ t if it's not finished on the current line.
515
+
516
+ FN should take three parameters: TYPE, BEG, and END.
517
+ TYPE is the type of text parsed ('name or 'value)
518
+ and BEG and END delimit that text in the buffer."
519
+ (let ((eol (save-excursion (end-of-line) (point))))
520
+ (while (not (haml-move ")"))
521
+ (haml-move " *")
522
+ (unless (haml-move "[a-z0-9_:\\-]+")
523
+ (return-from haml-parse-new-attr-hash (haml-move " *$")))
524
+ (funcall fn 'name (match-beginning 0) (match-end 0))
525
+ (haml-move " *")
526
+ (when (haml-move "=")
527
+ (haml-move " *")
528
+ (unless (looking-at "[\"'@a-z]") (return-from haml-parse-new-attr-hash))
529
+ (let ((beg (point)))
530
+ (haml-limited-forward-sexp eol)
531
+ (funcall fn 'value beg (point)))
532
+ (haml-move " *")))
533
+ nil))
534
+
227
535
  (defun haml-compute-indentation ()
228
536
  "Calculate the maximum sensible indentation for the current line."
229
537
  (save-excursion
230
538
  (beginning-of-line)
231
- (if (bobp) 0
539
+ (if (bobp) (list 0 nil)
232
540
  (haml-forward-through-whitespace t)
233
- (+ (current-indentation)
234
- (if (funcall haml-indent-function) haml-indent-offset
235
- 0)))))
541
+ (let ((indent (funcall haml-indent-function)))
542
+ (cond
543
+ ((consp indent) indent)
544
+ ((integerp indent) (list indent t))
545
+ (indent (list (+ (current-indentation) haml-indent-offset) nil))
546
+ (t (list (current-indentation) nil)))))))
236
547
 
237
548
  (defun haml-indent-region (start end)
238
549
  "Indent each nonblank line in the region.
@@ -250,7 +561,7 @@ between possible indentations."
250
561
  (next-line-column
251
562
  (if (and (equal last-command this-command) (/= (current-indentation) 0))
252
563
  (* (/ (- (current-indentation) 1) haml-indent-offset) haml-indent-offset)
253
- (haml-compute-indentation))))
564
+ (car (haml-compute-indentation)))))
254
565
  (while (< (point) end)
255
566
  (setq this-line-column next-line-column
256
567
  current-column (current-indentation))
@@ -275,16 +586,16 @@ back-dent the line by `haml-indent-offset' spaces. On reaching column
275
586
  0, it will cycle back to the maximum sensible indentation."
276
587
  (interactive "*")
277
588
  (let ((ci (current-indentation))
278
- (cc (current-column))
279
- (need (haml-compute-indentation)))
280
- (save-excursion
281
- (beginning-of-line)
282
- (delete-horizontal-space)
283
- (if (and (equal last-command this-command) (/= ci 0))
284
- (indent-to (* (/ (- ci 1) haml-indent-offset) haml-indent-offset))
285
- (indent-to need)))
286
- (if (< (current-column) (current-indentation))
287
- (forward-to-indentation 0))))
589
+ (cc (current-column)))
590
+ (destructuring-bind (need strict) (haml-compute-indentation)
591
+ (save-excursion
592
+ (beginning-of-line)
593
+ (delete-horizontal-space)
594
+ (if (and (not strict) (equal last-command this-command) (/= ci 0))
595
+ (indent-to (* (/ (- ci 1) haml-indent-offset) haml-indent-offset))
596
+ (indent-to need))))
597
+ (when (< (current-column) (current-indentation))
598
+ (forward-to-indentation 0))))
288
599
 
289
600
  (defun haml-reindent-region-by (n)
290
601
  "Add N spaces to the beginning of each line in the region.
@@ -309,14 +620,15 @@ the current line."
309
620
  (bolp)
310
621
  (looking-at "^[ \t]+$"))
311
622
  (backward-delete-char arg)
312
- (let ((ci (current-column)))
313
- (beginning-of-line)
314
- (if haml-backspace-backdents-nesting
315
- (haml-mark-sexp-but-not-next-line)
316
- (set-mark (save-excursion (end-of-line) (point))))
317
- (haml-reindent-region-by (* (- arg) haml-indent-offset))
318
- (back-to-indentation)
319
- (pop-mark))))
623
+ (save-excursion
624
+ (let ((ci (current-column)))
625
+ (beginning-of-line)
626
+ (if haml-backspace-backdents-nesting
627
+ (haml-mark-sexp-but-not-next-line)
628
+ (set-mark (save-excursion (end-of-line) (point))))
629
+ (haml-reindent-region-by (* (- arg) haml-indent-offset))
630
+ (back-to-indentation)
631
+ (pop-mark)))))
320
632
 
321
633
  (defun haml-kill-line-and-indent ()
322
634
  "Kill the current line, and re-indent all lines nested beneath it."
@@ -326,6 +638,13 @@ the current line."
326
638
  (kill-line 1)
327
639
  (haml-reindent-region-by (* -1 haml-indent-offset)))
328
640
 
329
- ;; Setup/Activation
641
+ (defun haml-indent-string ()
642
+ "Return the indentation string for `haml-indent-offset'."
643
+ (mapconcat 'identity (make-list haml-indent-offset " ") ""))
330
644
 
645
+ ;;;###autoload
646
+ (add-to-list 'auto-mode-alist '("\\.haml$" . haml-mode))
647
+
648
+ ;; Setup/Activation
331
649
  (provide 'haml-mode)
650
+ ;;; haml-mode.el ends here