forked from zenspider/enhanced-ruby-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enh-ruby-mode.el
1207 lines (1031 loc) · 43.3 KB
/
enh-ruby-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; enh-ruby-mode.el --- Major mode for editing Ruby files
;; Copyright (C) 2012 -- Ryan Davis
;; Copyright (C) 2010, 2011, 2012
;; Geoff Jacobsen
;; Author: Geoff Jacobsen
;; URL: http://github.com/zenspider/Enhanced-Ruby-Mode
;; Created: Sep 18 2010
;; Keywords: languages elisp, ruby
;; Version: 1.0.1
;; This file is not part of GNU Emacs.
;; This file is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 2 of the License, or
;; (at your option) any later version.
;; It is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
;; License for more details.
;; You should have received a copy of the GNU General Public License
;; along with it. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This is a fork of http://http://github.com/jacott/Enhanced-Ruby-Mode
;; to provide further enhancements and bug fixes.
;;
;; It has been renamed to enh-ruby-mode.el to avoid name conflicts
;; with ruby-mode that ships with emacs. All symbols that started with
;; 'ruby now start with 'enh-ruby. This also makes it possible to
;; switch back and forth for testing purposes.
;; Provides fontification, indentation, syntax checking, and navigation for Ruby code.
;;
;; If you're installing manually, you should add this to your .emacs
;; file after putting it on your load path:
;;
;; (add-to-list 'load-path "(path-to)/Enhanced-Ruby-Mode") ; must be added after any path containing old ruby-mode
;; (setq enh-ruby-program "(path-to-ruby1.9)/bin/ruby") ; so that still works if ruby points to ruby1.8
;;
(require 'cl) ; for cdddr, caddr
;;; Variables:
(defcustom enh-ruby-program "ruby"
"The ruby program to parse the source."
:group 'enh-ruby)
(defcustom enh-ruby-check-syntax 'errors-and-warnings
"Highlight syntax errors and warnings."
:type '(radio (const :tag "None" nil)
(const :tag "Errors" errors)
(const :tag "Errors and warnings" errors-and-warnings))
:group 'enh-ruby)
(defcustom enh-ruby-extra-keywords
nil
"List of idents that will be fontified as keywords. `erm-reset'
will need to be called in order for any global changes to take effect.
This variable can also be buffer local in which case it will
override the global value for the buffer it is local
to. `ruby-local-enable-extra-keywords' needs to be called after
the value changes.
"
:group 'enh-ruby
:type '(repeat string))
(put 'enh-ruby-extra-keywords 'safe-local-variable 'listp)
(defcustom enh-ruby-indent-tabs-mode nil
"*Indentation can insert tabs in ruby mode if this is non-nil."
:type 'boolean :group 'enh-ruby)
(put 'enh-ruby-indent-tabs-mode 'safe-local-variable 'booleanp)
(defcustom enh-ruby-indent-level 2
"*Indentation of ruby statements."
:type 'integer :group 'enh-ruby)
(put 'enh-ruby-indent-level 'safe-local-variable 'integerp)
(defcustom enh-ruby-hanging-indent-level 2
"*Extra hanging Indentation for continued ruby statements."
:type 'integer :group 'enh-ruby)
(put 'enh-ruby-hanging-indent-level 'safe-local-variable 'integerp)
(defcustom enh-ruby-comment-column 32
"*Indentation column of comments."
:type 'integer :group 'enh-ruby)
(put 'enh-ruby-comment-column 'safe-local-variable 'integerp)
(defcustom enh-ruby-deep-arglist nil
"Ignored in enhanced ruby mode."
:group 'enh-ruby)
(put 'enh-ruby-deep-arglist 'safe-local-variable 'booleanp)
(defcustom enh-ruby-deep-indent-paren t
"*Deep indent lists in parenthesis when non-nil."
:group 'enh-ruby)
(put 'enh-ruby-deep-indent-paren 'safe-local-variable 'booleanp)
(defcustom enh-ruby-deep-indent-paren-style nil
"Ignored in enhanced ruby mode."
:options '(t nil space) :group 'enh-ruby)
(defcustom enh-ruby-bounce-deep-indent nil
"Bounce between normal indentation and deep indentation when non-nil."
:group 'enh-ruby)
(put 'enh-ruby-bounce-deep-indent 'safe-local-variable 'booleanp)
(defcustom enh-ruby-hanging-paren-indent-level 2
"*Extra hanging indentation for continued ruby parenthesis."
:type 'integer :group 'enh-ruby)
(put 'enh-ruby-hanging-paren-indent-level 'safe-local-variable 'integerp)
(defcustom enh-ruby-hanging-brace-indent-level 2
"*Extra hanging indentation for continued ruby curly braces."
:type 'integer :group 'enh-ruby)
(put 'enh-ruby-hanging-brace-indent-level 'safe-local-variable 'integerp)
(defcustom enh-ruby-hanging-paren-deep-indent-level 0
"*Extra hanging deep indentation for continued ruby parenthesis."
:type 'integer :group 'enh-ruby)
(put 'enh-ruby-hanging-paren-deep-indent-level 'safe-local-variable 'integerp)
(defcustom enh-ruby-hanging-brace-deep-indent-level 0
"*Extra hanging deep indentation for continued ruby curly braces."
:type 'integer :group 'enh-ruby)
(put 'enh-ruby-hanging-brace-deep-indent-level 'safe-local-variable 'integerp)
(defcustom enh-ruby-add-encoding-comment-on-save t
"Adds ruby magic encoding comment on save when non-nil."
:type 'boolean :group 'enh-ruby)
(defcustom enh-ruby-encoding-map '((shift_jis . cp932) (shift-jis . cp932))
"Alist to map encoding name from emacs to ruby."
:group 'enh-ruby)
(defcustom enh-ruby-use-encoding-map t
"*Use `ruby-encoding-map' to set encoding magic comment if this is non-nil."
:type 'boolean :group 'enh-ruby)
(defcustom enh-ruby-use-ruby-mode-show-parens-config nil
"If not nil show-parens functionality from ruby-mode in 24.4 will be enabled"
:type 'boolean :group 'enh-ruby)
(defconst enh-ruby-symbol-chars "a-zA-Z0-9_=?!")
(defconst enh-ruby-symbol-re (concat "[" enh-ruby-symbol-chars "]"))
(defconst enh-ruby-defun-beg-keywords
'("class" "module" "def")
"Keywords at the beginning of definitions.")
(defconst enh-ruby-defun-beg-re
(regexp-opt enh-ruby-defun-beg-keywords)
"Regexp to match the beginning of definitions.")
(defconst enh-ruby-defun-and-name-re
(concat "\\(" enh-ruby-defun-beg-re "\\)[ \t]+\\("
;; \\. and :: for class method
"\\([A-Za-z_]" enh-ruby-symbol-re "*\\|\\.\\|::" "\\)"
"+\\)")
"Regexp to match definitions and their name")
(defconst erm-process-delimiter
"\n\0\0\0\n")
(define-abbrev-table 'enh-ruby-mode-abbrev-table ()
"Abbrev table used by enhanced-ruby-mode.")
(define-abbrev enh-ruby-mode-abbrev-table "end" "end"
'indent-for-tab-command :system t)
(defvar enh-ruby-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "{" 'enh-ruby-electric-brace)
(define-key map "}" 'enh-ruby-electric-brace)
(define-key map (kbd "M-C-a") 'enh-ruby-beginning-of-defun)
(define-key map (kbd "M-C-e") 'enh-ruby-end-of-defun)
(define-key map (kbd "M-C-b") 'enh-ruby-backward-sexp)
(define-key map (kbd "M-C-f") 'enh-ruby-forward-sexp)
(define-key map (kbd "M-C-p") 'enh-ruby-beginning-of-block)
(define-key map (kbd "M-C-n") 'enh-ruby-end-of-block)
(define-key map (kbd "M-C-h") 'enh-ruby-mark-defun)
(define-key map (kbd "M-C-q") 'enh-ruby-indent-exp)
(define-key map (kbd "C-c C-e") 'enh-ruby-find-error)
(define-key map (kbd "C-c C-f") 'enh-ruby-insert-end)
(define-key map (kbd "C-c /") 'enh-ruby-insert-end)
(define-key map (kbd "M-C-u") 'enh-ruby-up-sexp)
(define-key map (kbd "C-j") 'reindent-then-newline-and-indent)
map)
"Syntax table in use in enh-ruby-mode buffers.")
(defvar enh-ruby-mode-syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?\' "\"" table)
(modify-syntax-entry ?\" "\"" table)
(modify-syntax-entry ?\` "\"" table)
(modify-syntax-entry ?# "<" table)
(modify-syntax-entry ?\n ">" table)
(modify-syntax-entry ?\\ "\\" table)
(modify-syntax-entry ?$ "." table)
(modify-syntax-entry ?? "_" table)
(modify-syntax-entry ?_ "_" table)
(modify-syntax-entry ?: "_" table)
(modify-syntax-entry ?< "." table)
(modify-syntax-entry ?> "." table)
(modify-syntax-entry ?& "." table)
(modify-syntax-entry ?| "." table)
(modify-syntax-entry ?% "." table)
(modify-syntax-entry ?= "." table)
(modify-syntax-entry ?/ "." table)
(modify-syntax-entry ?+ "." table)
(modify-syntax-entry ?* "." table)
(modify-syntax-entry ?- "." table)
(modify-syntax-entry ?\; "." table)
(modify-syntax-entry ?\( "()" table)
(modify-syntax-entry ?\) ")(" table)
(modify-syntax-entry ?\{ "(}" table)
(modify-syntax-entry ?\} "){" table)
(modify-syntax-entry ?\[ "(]" table)
(modify-syntax-entry ?\] ")[" table)
table)
"Syntax table used by enh-ruby-mode buffers.")
;;; Mode:
(defalias 'enh-ruby-parent-mode
(if (fboundp 'prog-mode) 'prog-mode 'fundamental-mode))
;;;###autoload
(define-derived-mode enh-ruby-mode enh-ruby-parent-mode "EnhRuby"
"Enhanced Major mode for editing Ruby code.
\\{enh-ruby-mode-map}"
(setq-local comment-column enh-ruby-comment-column)
(setq-local comment-end "")
(setq-local comment-start "#")
(setq-local comment-start-skip "#+ *")
(setq-local erm-buff-num nil)
(setq-local erm-e-w-status nil)
(setq-local erm-full-parse-p nil)
(setq-local indent-line-function 'enh-ruby-indent-line)
(setq-local need-syntax-check-p nil)
(setq-local paragraph-ignore-fill-prefix t)
(setq-local paragraph-separate paragraph-start)
(setq-local parse-sexp-ignore-comments t)
(setq-local parse-sexp-lookup-properties t)
(setq-local require-final-newline mode-require-final-newline)
(setq-local beginning-of-defun-function 'enh-ruby-beginning-of-defun)
(setq-local end-of-defun-function 'enh-ruby-end-of-defun)
(set (make-local-variable 'paragraph-start)
(concat "$\\|" page-delimiter))
(set (make-local-variable 'add-log-current-defun-function)
'enh-ruby-add-log-current-method)
(setq font-lock-defaults '(nil t))
(setq indent-tabs-mode enh-ruby-indent-tabs-mode)
(setq imenu-create-index-function 'enh-ruby-imenu-create-index)
(if enh-ruby-add-encoding-comment-on-save
(add-hook 'before-save-hook 'enh-ruby-mode-set-encoding nil t))
(add-hook 'change-major-mode-hook 'erm-major-mode-changed nil t)
(add-hook 'kill-buffer-hook 'erm-buffer-killed nil t)
(when enh-ruby-use-ruby-mode-show-parens-config
(require 'ruby-mode)
(smie-setup ruby-smie-grammar #'ruby-smie-rules
:forward-token #'ruby-smie--forward-token
:backward-token #'ruby-smie--backward-token))
(abbrev-mode)
(erm-reset-buffer))
;;; Faces:
(require 'color nil t)
(defun erm-darken-color (name)
(color-darken-name (face-attribute name :foreground) 20))
(defun erm-define-faces ()
(defface enh-ruby-string-delimiter-face
`((t :foreground ,(erm-darken-color font-lock-string-face)))
"Face used to highlight string delimiters like \" and %Q."
:group 'enh-ruby)
(defface enh-ruby-heredoc-delimiter-face
`((t :foreground ,(erm-darken-color font-lock-string-face)))
"Face used to highlight string heredoc anchor strings like <<END and END"
:group 'enh-ruby)
(defface enh-ruby-regexp-delimiter-face
`((t :foreground ,(erm-darken-color font-lock-string-face)))
"Face used to highlight regexp delimiters like / and %r."
:group 'enh-ruby)
(defface enh-ruby-regexp-face
`((t :foreground ,(face-attribute font-lock-string-face :foreground)))
"Face used to highlight the inside of regular expressions"
:group 'enh-ruby)
(defface enh-ruby-op-face
`((t :foreground ,(erm-darken-color font-lock-keyword-face)))
"Face used to highlight operators like + and ||"
:group 'enh-ruby)
(defface erm-syn-errline
'((t (:box (:line-width 1 :color "red"))))
"Face used for marking error lines."
:group 'enh-ruby)
(defface erm-syn-warnline
'((t (:box (:line-width 1 :color "orange"))))
"Face used for marking warning lines."
:group 'enh-ruby))
(add-hook 'enh-ruby-mode-hook 'erm-define-faces)
;;; Support Functions:
(defun enh-ruby-mode-set-encoding ()
(save-excursion
(widen)
(goto-char (point-min))
(when (re-search-forward "[^\0-\177]" nil t)
(goto-char (point-min))
(let ((coding-system
(or coding-system-for-write
buffer-file-coding-system)))
(if coding-system
(setq coding-system
(or (coding-system-get coding-system 'mime-charset)
(coding-system-change-eol-conversion coding-system nil))))
(setq coding-system
(if coding-system
(symbol-name
(or (and enh-ruby-use-encoding-map
(cdr (assq coding-system enh-ruby-encoding-map)))
coding-system))
"ascii-8bit"))
(if (looking-at "^#!") (beginning-of-line 2))
(cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
(unless (string= (match-string 2) coding-system)
(goto-char (match-beginning 2))
(delete-region (point) (match-end 2))
(and (looking-at "-\*-")
(let ((n (skip-chars-backward " ")))
(cond ((= n 0) (insert " ") (backward-char))
((= n -1) (insert " "))
((forward-char)))))
(insert coding-system)))
((looking-at "\\s *#.*coding\\s *[:=]"))
(t (insert "# -*- coding: " coding-system " -*-\n"))
)))))
(defun erm-ruby-get-process ()
(when (and erm-ruby-process (not (equal (process-status erm-ruby-process) 'run)))
(let ((message (and erm-parsing-p erm-response)))
(erm-reset)
(if message
(error "%s" message)
(throw 'interrupted t))))
(unless erm-ruby-process
(let ((process-connection-type nil))
(setq erm-ruby-process
(start-process "erm-ruby-process"
nil
enh-ruby-program (concat (erm-source-dir)
"ruby/erm.rb")))
(set-process-coding-system erm-ruby-process 'utf-8 'utf-8)
(set-process-filter erm-ruby-process 'erm-filter)
(set-process-query-on-exit-flag erm-ruby-process nil)
(process-send-string
(erm-ruby-get-process)
(concat "x0:"
(mapconcat 'identity (default-value 'enh-ruby-extra-keywords) " ")
":"
erm-process-delimiter))))
erm-ruby-process)
(defvar erm-response nil "Private variable.")
(defvar erm-parsing-p nil "Private variable.")
(defvar erm-no-parse-needed-p nil "Private variable.")
(defvar erm-source-dir nil "Private variable.")
(defun erm-source-dir ()
(or erm-source-dir
(setq erm-source-dir (file-name-directory (find-lisp-object-file-name
'erm-source-dir (symbol-function 'erm-source-dir))))))
(defvar erm-ruby-process nil
"The current erm process where emacs is interacting with")
(defvar erm-next-buff-num nil "Private variable.")
(defvar erm-parse-buff nil "Private variable.")
(defvar erm-reparse-list nil "Private variable.")
(defvar erm-syntax-check-list nil "Private variable.")
(defun erm-reset-syntax-buffers (list)
(let ((buffer (car list)))
(when buffer
(when (buffer-live-p buffer)
(with-current-buffer buffer (setq need-syntax-check-p nil)))
(erm-reset-syntax-buffers (cdr list)))))
(defun erm-reset ()
"Reset all enh-ruby-mode buffers and restart the ruby parser."
(interactive)
(erm-reset-syntax-buffers erm-syntax-check-list)
(setq erm-reparse-list nil
erm-syntax-check-list nil
erm-parsing-p nil
erm-parse-buff nil
erm-next-buff-num 1)
(when erm-ruby-process
(delete-process erm-ruby-process)
(setq erm-ruby-process nil))
(dolist (buf (buffer-list))
(with-current-buffer buf
(when (eq 'enh-ruby-mode major-mode)
(erm-reset-buffer)))))
(defun erm-major-mode-changed ()
(remove-hook 'kill-buffer-hook 'erm-buffer-killed t)
(erm-buffer-killed))
(defun erm-proc-string (prefix)
(concat prefix (number-to-string erm-buff-num) ":" erm-process-delimiter))
(defun erm-buffer-killed ()
(process-send-string (erm-ruby-get-process) (erm-proc-string "k")))
(defun erm-reset-buffer ()
(setq erm-buff-num erm-next-buff-num)
(setq erm-next-buff-num (1+ erm-buff-num))
(add-hook 'after-change-functions #'erm-req-parse nil t)
(unless
(enh-ruby-local-enable-extra-keywords)
(enh-ruby-fontify-buffer)))
(defun enh-ruby-local-enable-extra-keywords ()
"If the variable `ruby-extra-keywords' is buffer local then
enable the keywords for current buffer."
(when (local-variable-p 'enh-ruby-extra-keywords)
(process-send-string (erm-ruby-get-process)
(concat "x"
(number-to-string erm-buff-num) ":"
(mapconcat 'identity enh-ruby-extra-keywords " ")
":" erm-process-delimiter))
(enh-ruby-fontify-buffer)
t))
(defun enh-ruby-electric-brace (arg)
(interactive "P")
(insert-char last-command-event 1)
(enh-ruby-indent-line t)
(delete-char -1)
(self-insert-command (prefix-numeric-value arg)))
(defun enh-ruby-imenu-create-index-in-block (prefix beg end)
(let* ((index-alist '())
(pos beg)
(prop (get-text-property pos 'indent)))
(setq end (or end (point-max)))
(while (and pos (< pos end))
(goto-char pos)
(when (and (eq prop 'b) (looking-at enh-ruby-defun-and-name-re))
(push (cons (concat (match-string 1) " "(match-string 2)) pos) index-alist))
(setq prop (and (setq pos (enh-ruby-next-indent-change pos))
(get-text-property pos 'indent))))
index-alist))
(defun enh-ruby-imenu-create-index ()
(nreverse (enh-ruby-imenu-create-index-in-block nil (point-min) nil)))
(defun enh-ruby-add-log-current-method ()
"Return current method string."
;; We un-confuse `parse-partial-sexp' by setting syntax-table properties
;; for characters inside regexp literals.
(condition-case nil
(save-excursion
(enh-ruby-beginning-of-defun 1)
(when (looking-at enh-ruby-defun-and-name-re)
(concat (match-string 1) " " (match-string 2))))))
;; Stolen shamelessly from James Clark's nxml-mode.
(defmacro erm-with-unmodifying-text-property-changes (&rest body)
"Evaluate BODY without any text property changes modifying the buffer.
Any text properties changes happen as usual but the changes are not treated as
modifications to the buffer."
(let ((modified (make-symbol "modified")))
`(let ((,modified (buffer-modified-p))
(inhibit-read-only t)
(inhibit-modification-hooks t)
(buffer-undo-list t)
(deactivate-mark nil)
;; Apparently these avoid file locking problems.
(buffer-file-name nil)
(buffer-file-truename nil))
(unwind-protect
(progn ,@body)
(unless ,modified
(restore-buffer-modified-p nil))))))
(defun enh-ruby-fontify-buffer ()
"Fontify the current buffer. Useful if faces are out of sync"
(interactive)
(if (and erm-parsing-p (not (eq erm-parse-buff (current-buffer))))
(erm-reparse-diff-buf)
(setq erm-full-parse-p t)
(erm-req-parse nil nil nil)))
(defun erm-reparse-diff-buf ()
(setq erm-reparse-list (cons (current-buffer) erm-reparse-list)))
(defun erm-req-parse (min max len)
(when (and enh-ruby-check-syntax (not need-syntax-check-p))
(setq need-syntax-check-p t)
(setq erm-syntax-check-list (cons (current-buffer) erm-syntax-check-list)))
(let ((pc (if erm-parsing-p
(if (eq erm-parse-buff (current-buffer))
(setq erm-parsing-p 'a)
'dbuf)
(setq erm-response "")
(setq erm-parsing-p t)
(if (not erm-full-parse-p)
(if erm-no-parse-needed-p (progn (setq erm-parsing-p nil) 'a) 'p)
(setq min (point-min)
max (point-max)
len 0
erm-full-parse-p nil)
'r)))
interrupted-p)
(setq interrupted-p
(catch 'interrupted
(if (eq pc 'dbuf)
(erm-reparse-diff-buf)
(setq erm-parse-buff (current-buffer))
(process-send-string (erm-ruby-get-process)
(format "%s%d:%d:%d:%d:%d:"
pc
erm-buff-num
(point-min)
(point-max)
min
len))
(process-send-region erm-ruby-process min max)
(process-send-string erm-ruby-process erm-process-delimiter))
nil))
(when interrupted-p
(setq erm-full-parse-p t))))
(defun erm-wait-for-parse ()
(while erm-parsing-p
(accept-process-output (erm-ruby-get-process) 0.5)))
(defun erm-filter (proc response)
(setq erm-response (concat erm-response response))
(when (and (> (length erm-response) 5)
(string= erm-process-delimiter (substring erm-response -5 nil)))
(setq response (substring erm-response 0 -5))
(setq erm-response "")
(with-current-buffer erm-parse-buff
(erm-with-unmodifying-text-property-changes
(erm-parse response)))))
(defsubst erm-ready ()
(if erm-full-parse-p
(enh-ruby-fontify-buffer)
(setq erm-parsing-p t)
(process-send-string (erm-ruby-get-process) (erm-proc-string "g"))))
(setq enh-ruby-font-names
'(nil
font-lock-string-face
font-lock-type-face
font-lock-variable-name-face
font-lock-comment-face
font-lock-constant-face
font-lock-string-face
enh-ruby-string-delimiter-face
enh-ruby-regexp-delimiter-face
font-lock-function-name-face
font-lock-keyword-face
enh-ruby-heredoc-delimiter-face
enh-ruby-op-face
enh-ruby-regexp-face
))
(defun enh-ruby-calculate-indent (&optional start-point)
"Calculate the indentation of the previous line and its level."
(save-excursion
(when start-point (goto-char start-point))
(if (bobp)
0
(forward-line 0)
(skip-syntax-forward " " (line-end-position))
(let ((pos (line-beginning-position))
(prop (get-text-property (point) 'indent))
(face (get-text-property (point) 'font-lock-face)))
(cond
((or (eq 'e prop) (eq 's prop))
(when (eq 's prop) (forward-char))
(enh-ruby-backward-sexp)
(if (not (eq 'd (get-text-property (point) 'indent)))
(current-column)
(setq pos (point))
(enh-ruby-skip-non-indentable)
(enh-ruby-calculate-indent-1 pos (line-beginning-position))))
((eq 'r prop)
(let ((opening-col
(save-excursion (enh-ruby-backward-sexp) (current-column))))
(if (and enh-ruby-deep-indent-paren
(not enh-ruby-bounce-deep-indent))
opening-col
(forward-line -1)
(enh-ruby-skip-non-indentable)
(let ((opening-char
(save-excursion (enh-ruby-backward-sexp) (char-after)))
(proposed-col
(enh-ruby-calculate-indent-1 pos
(line-beginning-position))))
(if (< proposed-col opening-col)
(- proposed-col
(if (char-equal opening-char ?{)
enh-ruby-hanging-brace-indent-level
enh-ruby-hanging-paren-indent-level))
opening-col)))))
((or (memq face '(font-lock-string-face enh-ruby-heredoc-delimiter-face))
(and (eq 'font-lock-variable-name-face face)
(looking-at "#")))
(current-column))
(t
(forward-line -1)
(enh-ruby-skip-non-indentable)
(enh-ruby-calculate-indent-1 pos (line-beginning-position))))))))
(defun erm-looking-at-not-indentable ()
(skip-syntax-forward " " (line-end-position))
(let ((face (get-text-property (point) 'font-lock-face)))
(or (= (point) (line-end-position))
(memq face '(font-lock-string-face font-lock-comment-face enh-ruby-heredoc-delimiter-face))
(and (eq 'font-lock-variable-name-face face)
(looking-at "#"))
(and (memq face '(enh-ruby-regexp-delimiter-face enh-ruby-string-delimiter-face))
(> (point) (point-min))
(eq (get-text-property (1- (point)) 'font-lock-face)
'font-lock-string-face)))))
(defun enh-ruby-skip-non-indentable ()
(forward-line 0)
(while (and (> (point) (point-min))
(erm-looking-at-not-indentable))
(skip-chars-backward " \n\t\r\v\f")
(forward-line 0)))
(defvar enh-ruby-last-bounce-line nil
"The last line that `erm-bounce-deep-indent-paren` was run against.")
(defvar enh-ruby-last-bounce-deep nil
"The last result from `erm-bounce-deep-indent-paren`.")
(defun enh-ruby-calculate-indent-1 (limit pos)
(goto-char pos)
(let* ((start-pos pos)
(prop (get-text-property pos 'indent))
(indent (- (current-indentation)
(if (eq 'c prop) enh-ruby-hanging-indent-level 0)))
(nbc 0)
(npc 0)
col max bc pc)
(setq enh-ruby-last-bounce-deep
(if (eq enh-ruby-last-bounce-line (line-number-at-pos))
(not enh-ruby-last-bounce-deep)
t))
(setq enh-ruby-last-bounce-line (line-number-at-pos))
(while (< pos limit)
(unless prop
(setq pos (next-single-property-change pos 'indent (current-buffer) limit))
(when (< pos limit)
(setq prop (get-text-property pos 'indent))))
(setq col (- pos start-pos -1))
(cond
((eq prop 'l)
(let ((shallow-indent
(if (char-equal (char-after pos) ?{)
(+ enh-ruby-hanging-brace-indent-level indent)
(+ enh-ruby-hanging-paren-indent-level indent)))
(deep-indent
(if (char-equal (char-after pos) ?{)
(+ enh-ruby-hanging-brace-deep-indent-level col)
(+ enh-ruby-hanging-paren-deep-indent-level col))))
(if enh-ruby-bounce-deep-indent
(setq pc (cons (if enh-ruby-last-bounce-deep shallow-indent deep-indent) pc))
(setq pc (cons (if enh-ruby-deep-indent-paren deep-indent shallow-indent) pc)))))
((eq prop 'r)
(if pc (setq pc (cdr pc)) (setq npc col)))
((memq prop '(b d s))
(setq bc (cons col bc)))
((eq prop 'e)
(if bc
(setq bc (cdr bc))
(setq nbc col))))
(when (< (setq pos (1+ pos)) limit)
(setq prop (get-text-property pos 'indent))))
;;(prin1 (list indent nbc bc npc pc))
(setq pc (or (car pc) 0))
(setq bc (or (car bc) 0))
(setq max (max pc bc nbc npc))
(+
(if (eq 'c (get-text-property limit 'indent)) enh-ruby-hanging-indent-level 0)
(cond
((= max 0)
(if (not (memq (get-text-property start-pos 'font-lock-face)
'(enh-ruby-heredoc-delimiter-face font-lock-string-face)))
indent
(goto-char (or (enh-ruby-string-start-pos start-pos) limit))
(current-indentation)))
((= max pc) (if (eq 'c (get-text-property limit 'indent))
(- pc enh-ruby-hanging-indent-level)
pc))
((= max bc)
(if (eq 'd (get-text-property (+ start-pos bc -1) 'indent))
(+ (enh-ruby-calculate-indent-1 (+ start-pos bc -1) start-pos)
enh-ruby-indent-level)
(+ bc enh-ruby-indent-level -1)))
((= max npc)
(goto-char (+ start-pos npc))
(enh-ruby-backward-sexp)
(enh-ruby-calculate-indent-1 (point) (line-beginning-position)))
((= max nbc)
(goto-char (+ start-pos nbc -1))
(enh-ruby-backward-sexp)
(enh-ruby-calculate-indent-1 (point) (line-beginning-position)))
(t 0)))))
(defun enh-ruby-string-start-pos (pos)
(when (< 0 (or (setq pos (previous-single-property-change pos 'font-lock-face)) 0))
(previous-single-property-change pos 'font-lock-face)))
(defun enh-ruby-show-errors-at (pos face)
(let ((overlays (overlays-at pos))
overlay
messages)
;; TODO:
;; (-map (lambda (o) (overlay-get o 'help-echo))
;; (-filter (lambda (o) (and (overlay-get o 'erm-syn-overlay)
;; (eq (overlay-get o 'font-lock-face) face)))))
(while overlays
(setq overlay (car overlays))
(when (and (overlay-get overlay 'erm-syn-overlay)
(eq (overlay-get overlay 'font-lock-face) face))
(setq messages (cons (overlay-get overlay 'help-echo) messages)))
(setq overlays (cdr overlays)))
(message "%s" (mapconcat 'identity messages "\n"))
messages))
(defun enh-ruby-find-error (&optional arg)
"Search back, then forward for a syntax error/warning.
Display contents in mini-buffer."
(interactive "^P")
(let (overlays
overlay
(face (if arg 'erm-syn-warnline 'erm-syn-errline))
messages
(pos (point)))
(unless (eq last-command 'enh-ruby-find-error)
(while (and (not messages) (> pos (point-min)))
(setq messages (enh-ruby-show-errors-at (setq pos (previous-overlay-change pos)) face))))
(unless messages
(while (and (not messages) (< pos (point-max)))
(setq messages (enh-ruby-show-errors-at (setq pos (next-overlay-change pos)) face))))
(if messages
(goto-char pos)
(unless arg
(enh-ruby-find-error t)))))
(defun enh-ruby-up-sexp (&optional arg)
"Move up one balanced expression (sexp).
With ARG, do it that many times."
(interactive "^p")
(unless arg (setq arg 1))
(while (>= (setq arg (1- arg)) 0)
(let* ((count 1)
prop)
(goto-char
(save-excursion
(while (and (not (= (point) (point-min)))
(< 0 count))
(goto-char (enh-ruby-previous-indent-change (point)))
(setq prop (get-text-property (point) 'indent))
(setq count (cond
((or (eq prop 'l) (eq prop 'b) (eq prop 'd)) (1- count))
((or (eq prop 'r) (eq prop 'e)) (1+ count))
(t count))))
(point))))))
(defun enh-ruby-beginning-of-defun (&optional arg)
"Move backward across one balanced expression (sexp) looking for a definition begining.
With ARG, do it that many times."
(interactive "^p")
(unless arg (setq arg 1))
(let ((cont t)
prop)
(goto-char
(save-excursion
(while (>= (setq arg (1- arg)) 0)
(while (and
(> (point) (point-min))
(progn
(enh-ruby-backward-sexp 1)
(setq prop (get-text-property (point) 'indent))
(not (and (eq prop 'b) (looking-at enh-ruby-defun-beg-re)))))))
(point)))))
(defun enh-ruby-mark-defun ()
"Put mark at end of this Ruby definition, point at beginning."
(interactive)
(push-mark (point))
(enh-ruby-beginning-of-defun 1)
(enh-ruby-forward-sexp 1)
(forward-line 1)
(push-mark (point) nil t)
(forward-line -1)
(end-of-line)
(enh-ruby-backward-sexp 1)
(forward-line 0))
(defun enh-ruby-indent-exp (&optional shutup-p)
"Indent each line in the balanced expression following point syntactically.
If optional SHUTUP-P is non-nil, no errors are signalled if no
balanced expression is found."
(interactive "*P")
(erm-wait-for-parse)
(let ((end-pos (save-excursion (enh-ruby-forward-sexp 1) (point))))
(indent-region (point) end-pos)))
(defun enh-ruby-beginning-of-block (&optional arg)
"Move backward across one balanced expression (sexp) looking for a block begining.
With ARG, do it that many times."
(interactive "^p")
(unless arg (setq arg 1))
(let ((cont t)
prop)
(goto-char
(save-excursion
(while (>= (setq arg (1- arg)) 0)
(while (progn
(enh-ruby-backward-sexp 1)
(setq prop (get-text-property (point) 'indent))
(not (or (eq prop 'b) (eq prop 'd))))))
(point)))))
(defun enh-ruby-end-of-defun (&optional arg)
"Move forwards across one balanced expression (sexp) looking for a definition end.
With ARG, do it that many times."
(interactive "^p")
(unless arg (setq arg 1))
(let ((cont t)
prop)
(while (>= (setq arg (1- arg)) 0)
(while (and
(< (point) (point-max))
(progn
(enh-ruby-forward-sexp 1)
(setq prop (get-text-property (- (point) 3) 'indent))
(not (and (eq prop 'e)
(save-excursion
(enh-ruby-backward-sexp 1)
(looking-at enh-ruby-defun-beg-re))))))))
(point)))
(defun enh-ruby-end-of-block (&optional arg)
"Move forwards across one balanced expression (sexp) looking for a block end.
With ARG, do it that many times."
(interactive "^p")
(unless arg (setq arg 1))
(let ((cont t)
prop)
(goto-char
(save-excursion
(while (>= (setq arg (1- arg)) 0)
(while (progn
(enh-ruby-forward-sexp 1)
(setq prop (get-text-property (- (point) 3) 'indent))
(not (eq prop 'e)))))
(point)))))
(defun enh-ruby-backward-sexp (&optional arg)
"Move backward across one balanced expression (sexp).
With ARG, do it that many times."
(interactive "^p")
(unless arg (setq arg 1))
(while (>= (setq arg (1- arg)) 0)
(let* ((pos (point))
(prop (get-text-property pos 'indent))
(count 0))
(unless (or (eq prop 'r) (eq prop 'e))
(setq prop (and (setq pos (enh-ruby-previous-indent-change pos))
(get-text-property pos 'indent))))
(while (< 0 (setq count
(cond
((or (eq prop 'l) (eq prop 'b) (eq prop 'd)) (1- count))
((or (eq prop 'r) (eq prop 'e)) (1+ count))
((eq prop 'c) count)
((eq prop 's) (if (= 0 count) 1 count))
(t 0))))
(goto-char pos)
(setq prop (and (setq pos (enh-ruby-previous-indent-change pos))
(get-text-property pos 'indent))))
(goto-char (if prop pos (point-min))))))
(defun enh-ruby-forward-sexp (&optional arg)
"Move backward across one balanced expression (sexp).
With ARG, do it that many times."
(interactive "^p")
(unless arg (setq arg 1))
(let ((i (or arg 1)))
(cond
((< i 0)
(enh-ruby-backward-sexp (- i)))
(t
(skip-syntax-forward " ")
(while (> i 0)
(let* ((pos (point))
(prop (get-text-property pos 'indent))
(count 0))
(unless (memq prop '(l b d))
(setq prop (and (setq pos (enh-ruby-next-indent-change pos))
(get-text-property pos 'indent))))
(while (< 0 (setq count
(cond
((memq prop '(l b d)) (1+ count))
((memq prop '(r e)) (1- count))
((memq prop '(c)) count)
((memq prop '(s)) (if (= 0 count) 1 count))
(t 0))))
(goto-char pos)
(setq prop (and (setq pos (enh-ruby-next-indent-change pos))
(get-text-property pos 'indent))))
(goto-char (if prop pos (point-max)))
(cond ((looking-at "end") ; move past end/}/]/)
(forward-word 1))
((looking-at "}\\|)\\|]")
(forward-char 1))))
(setq i (1- i)))))))
(defun enh-ruby-insert-end ()
(interactive)
(let ((text (save-excursion
(forward-line 0)
(if (looking-at "^[ \t]*$")