forked from jacott/Enhanced-Ruby-Mode
-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
enh-ruby-mode.el
1722 lines (1507 loc) · 61.9 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 -*- lexical-binding: t; -*-
;; Copyright (C) 2012-2022+ -- Ryan Davis
;; Copyright (C) 2010-2012 Geoff Jacobsen
;; Author: Geoff Jacobsen
;; Maintainer: Ryan Davis
;; URL: https://github.com/zenspider/Enhanced-Ruby-Mode
;; Created: Sep 18 2010
;; Keywords: languages, elisp, ruby
;; Package-Requires: ((emacs "25.1"))
;; Version: 1.2.0
;; 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 https://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-ruby)/bin/ruby") ; so that still works if ruby points to ruby1.8
;;
(require 'cl-lib) ; for cdddr, caddr
(require 'files) ; for mode-require-final-newline
(require 'paren) ; show-paren-data-function
(require 'seq) ; seq-remove, seq-difference
(require 'subr-x) ; string-trim-right
(require 'cus-edit) ; custom-variable-state
(require 'find-func) ; find-library-name
;;; Properties & Other Bullshit Codes:
;; 'l - [, (, {, %w/%i open or | goalpost open
;; 'r - ], ), }, %w/%i close or | goalpost close
;; 'b - begin, def, case, if
;; 'd - do, {, embexpr (interpolation) start
;; 'e - end, embexpr (interpolation) end, close block }
;; 's - statement start on BACKDENT_KW else/when/rescue etc
;; 'c - continue - period followed by return (or other way around?)
;; pc
;; bc
;; nbc
;; npc
;;; Variables:
(defcustom enh-ruby-add-encoding-comment-on-save nil
"Adds ruby magic encoding comment on save when non-nil."
:type 'boolean
:safe #'booleanp
:group 'enh-ruby)
(defcustom enh-ruby-bounce-deep-indent nil
"Bounce between normal indentation and deep indentation when non-nil."
:type 'boolean
:safe #'booleanp
: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))
:safe #'enh-symbol-or-null-p
:group 'enh-ruby)
(defcustom enh-ruby-comment-column 32
"*Indentation column of comments."
:type 'integer
:safe #'integerp
:group 'enh-ruby)
(defcustom enh-ruby-deep-indent-construct t
"*Deep indent constructs such as if, def, class and module when non-nil."
:type 'boolean
:safe #'booleanp
:group 'enh-ruby)
(defcustom enh-ruby-deep-indent-paren t
"*Deep indent lists in parenthesis when non-nil."
;; FIX: this applies to square brackets as well
:type 'boolean
:safe #'booleanp
:group 'enh-ruby)
(defcustom enh-ruby-encoding-map
'((us-ascii . nil) ;; Do not put coding: us-ascii
(utf-8 . nil) ;; Do not put coding: utf-8
(shift-jis . cp932) ;; Emacs charset name of Shift_JIS
(shift_jis . cp932) ;; MIME charset name of Shift_JIS
(japanese-cp932 . cp932)) ;; Emacs charset name of CP932
"Alist to map encoding name from Emacs to ruby."
:type '(alist :key-type (symbol :tag "Encoding")
:value-type (choice (const :tag "Ignore" nil)
(symbol :tag "Charset")))
:safe (lambda (xs)
(and (listp xs)
(cl-every (lambda (x)
(and (symbolp (car x))
(enh-symbol-or-null-p (cdr x))))
xs)))
:group 'enh-ruby)
(defcustom enh-ruby-extra-keywords nil
"*A 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."
:type '(repeat string)
:safe #'listp
:group 'enh-ruby)
(defcustom enh-ruby-hanging-brace-deep-indent-level 0
"*Extra hanging deep indentation for continued ruby curly or square braces."
:type 'integer
:safe #'integerp
:group 'enh-ruby)
(defcustom enh-ruby-hanging-brace-indent-level 2
"*Extra hanging indentation for continued ruby curly braces."
:type 'integer
:safe #'integerp
:group 'enh-ruby)
(defcustom enh-ruby-hanging-indent-level 2
"*Extra hanging Indentation for continued ruby statements."
:type 'integer
:safe #'integerp
:group 'enh-ruby)
(defcustom enh-ruby-hanging-paren-deep-indent-level 0
"*Extra hanging deep indentation for continued ruby parenthesis."
:type 'integer
:safe #'integerp
:group 'enh-ruby)
(defcustom enh-ruby-hanging-paren-indent-level 2
"*Extra hanging indentation for continued ruby parenthesis."
:type 'integer
:safe #'integerp
:group 'enh-ruby)
(defcustom enh-ruby-indent-level 2
"*Indentation of ruby statements."
:type 'integer
:safe #'integerp
:group 'enh-ruby)
(defcustom enh-ruby-indent-tabs-mode nil
"*Indentation can insert tabs in ruby mode if this is non-nil."
:type 'boolean
:safe #'booleanp
:group 'enh-ruby)
(defcustom enh-ruby-preserve-indent-in-heredocs nil
"Indent heredocs and multiline strings like ‘text-mode’.
Warning: does not play well with command ‘electric-indent-mode’."
:type 'boolean
:safe #'booleanp
:group 'enh-ruby)
(defcustom enh-ruby-program "ruby"
"The ruby program to parse the source."
:type 'string
:safe #'stringp
:group 'enh-ruby)
(defcustom enh-ruby-use-encoding-map t
"*Use `enh-ruby-encoding-map' to set encoding magic comment if this is non-nil."
:type 'boolean
:safe #'booleanp
:group 'enh-ruby)
(defvar enh-ruby-use-ruby-mode-show-parens-config nil
"This flag has no effect anymore as ERM supports command
‘show-paren-mode’ directly.")
(make-obsolete-variable 'enh-ruby-use-ruby-mode-show-parens-config nil "2018-04-03")
;; TODO: renames:
;;
;; enh-ruby-indent-level: 2
;; enh-ruby-hanging-indent-level: 2
;; enh-ruby-hanging-brace-deep-indent-level: 0
;; enh-ruby-hanging-brace-indent-level: 2
;; enh-ruby-hanging-paren-deep-indent-level: 0
;; enh-ruby-hanging-paren-indent-level: 2
;;
;; Versus:
;;
;; enh-ruby-indent-level: 2
;; enh-ruby-indent-level-hanging: 2
;; enh-ruby-indent-level-hanging-paren: 2
;; enh-ruby-indent-level-hanging-paren-deep: 0
;; enh-ruby-indent-level-hanging-brace: 2
;; enh-ruby-indent-level-hanging-brace-deep: 0
(defvar need-syntax-check-p)
(defvar erm-buff-num)
(defvar erm-e-w-status)
(defvar erm-full-parse-p)
;;; Constants
(defconst enh-ruby-block-end-re "\\_<end\\_>")
(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-f") #'enh-ruby-find-file)
(define-key map (kbd "C-c C-e") #'enh-ruby-find-error)
(define-key map (kbd "C-c /") #'enh-ruby-insert-end)
(define-key map (kbd "C-c {") #'enh-ruby-toggle-block)
(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)
(modify-syntax-entry ?@ "'" table)
table)
"Syntax table used by ‘enh-ruby-mode’ buffers.")
(defconst enh-ruby-font-lock-keyword-beg-re "\\(?:^\\|[^.@$:]\\|\\.\\.\\)")
(defconst enh-ruby-font-lock-keywords
`(;; Core methods that have required arguments.
(,(concat
enh-ruby-font-lock-keyword-beg-re
(regexp-opt
'( ;; built-in methods on Kernel TODO: add more via reflection?
"at_exit"
"autoload"
"autoload?"
"callcc"
"catch"
"eval"
"exec"
"format"
"lambda"
"load"
"loop"
"open"
"p"
"print"
"printf"
"proc"
"putc"
"puts"
"require"
"require_relative"
"spawn"
"sprintf"
"syscall"
"system"
"throw"
"trace_var"
"trap"
"untrace_var"
"warn"
;; keyword-like private methods on Module
"alias_method"
"attr"
"attr_accessor"
"attr_reader"
"attr_writer"
"define_method"
"extend"
"include"
"module_function"
"prepend"
"private_class_method"
"private_constant"
"public_class_method"
"public_constant"
"refine"
"using")
'symbols))
(1 (unless (looking-at " *\\(?:[]|,.)}=]\\|$\\)")
font-lock-builtin-face)))
;; Kernel methods that have no required arguments.
(,(concat
enh-ruby-font-lock-keyword-beg-re
(regexp-opt
'("__callee__"
"__dir__"
"__method__"
"abort"
"binding"
"block_given?"
"caller"
"exit"
"exit!"
"fail"
"fork"
"global_variables"
"local_variables"
"private"
"protected"
"public"
"raise"
"rand"
"readline"
"readlines"
"sleep"
"srand")
'symbols))
(1 font-lock-builtin-face)))
"Additional expressions to highlight in ‘enh-ruby-mode’.")
(defconst 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)
"Font faces used by ‘enh-ruby-mode’.")
;;; Code:
;;;###autoload
(defun enh-symbol-or-null-p (x)
"Return true if X is either a symbol or null. Used for defcustom safe check."
(or (symbolp x)
(null x)))
(define-obsolete-variable-alias 'enh/symbol-or-null-p
'enh-symbol-or-null-p "2022-07-07")
;;;###autoload
(define-derived-mode enh-ruby-mode prog-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 forward-sexp-function #'enh-ruby-forward-sexp)
(setq-local need-syntax-check-p nil)
(setq-local paragraph-ignore-fill-prefix t)
(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)
(setq-local show-paren-data-function #'erm-show-paren-data-function)
(setq-local paragraph-start (concat "$\\|" page-delimiter))
(setq-local paragraph-separate paragraph-start)
(setq-local add-log-current-defun-function
'enh-ruby-add-log-current-method)
(setq-local font-lock-keywords enh-ruby-font-lock-keywords)
(setq font-lock-defaults '((enh-ruby-font-lock-keywords) 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)
(abbrev-mode)
(erm-reset-buffer))
;;; Faces:
(require 'color nil t)
(defun erm-darken-color (name)
"Return color NAME with foreground 20% darker."
(color-darken-name (face-attribute name :foreground) 20))
(defun erm-define-faces ()
"Define faces for ‘enh-ruby-mode’."
(defface enh-ruby-string-delimiter-face
`((t :foreground ,(erm-darken-color font-lock-string-face)))
"Face used to highlight string delimiters like quotes 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 ()
"Check encoding on save and create a magic comment if non-standard encoding."
(save-excursion
(widen)
(goto-char (point-min))
(when (re-search-forward "[^[:ascii:]]" 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 *[:=]"))
((equal "utf-8" coding-system) 'do-nothing) ; hack? should check version?
(t (insert "# -*- coding: " coding-system " -*-\n")))))))
(defvar erm-ruby-process nil "The current erm process where Emacs is interacting with.")
(defvar erm-response nil "Private variable.")
(defvar erm-parsing-p nil "Parsing: t, nil, 'a (all?), 'p (partial?).")
(defun erm-ruby-get-process ()
"Return (or create) the current ruby parser process."
(when (and erm-ruby-process (not (equal (process-status erm-ruby-process) 'run)))
(erm-reset)
(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-process
(concat "x0:"
(mapconcat #'identity (default-value 'enh-ruby-extra-keywords) " ")
":"
erm-process-delimiter))))
erm-ruby-process)
(defvar erm-no-parse-needed-p nil "Private variable.")
(defvar erm-source-dir nil "Private variable.")
(defun erm-source-dir ()
"Return the directory for enh-ruby-mode.el."
(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-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-ruby-program-version ()
"Display the current version of ERM-RUBY-PROGRAM"
(interactive)
(let* ((command (format "%s -v" enh-ruby-program))
(version (shell-command-to-string command))
(version (cl-second (split-string version))))
(message "erm-ruby-program -v = %s" version)))
(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 ()
(remove-hook 'kill-buffer-hook #'erm-buffer-killed t)
(catch 'interrupted
(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)
(delete-char -1)
(self-insert-command (prefix-numeric-value arg)))
(defun enh-ruby-brace-to-do-end (orig end)
(let (beg-marker end-marker)
(goto-char end)
(when (eq (char-before) ?\})
(delete-char -1)
(when (save-excursion
(skip-chars-backward " \t")
(not (bolp)))
(insert "\n"))
(insert "end")
(setq end-marker (point-marker))
(when (and (not (eobp)) (eq (char-syntax (char-after)) ?w))
(insert " "))
(goto-char orig)
(delete-char 1)
(when (eq (char-syntax (char-before)) ?w)
(insert " "))
(insert "do")
(setq beg-marker (point-marker))
(when (looking-at "\\(\\s \\)*|")
(unless (match-beginning 1)
(insert " "))
(goto-char (1+ (match-end 0)))
(search-forward "|"))
(unless (looking-at "\\s *$")
(insert "\n"))
(indent-region beg-marker end-marker)
(goto-char beg-marker))))
(defun enh-ruby-do-end-to-brace (orig end)
(let (beg-marker end-marker beg-pos end-pos)
(goto-char (- end 3))
(when (looking-at enh-ruby-block-end-re)
(delete-char 3)
(setq end-marker (point-marker))
(insert "}")
(goto-char orig)
(delete-char 2)
;; Maybe this should be customizable, let's see if anyone asks.
(insert "{ ")
(setq beg-marker (point-marker))
(when (looking-at "\\s +|")
(delete-char (- (match-end 0) (match-beginning 0) 1))
(forward-char)
(re-search-forward "|" (line-end-position) t))
(save-excursion
(skip-chars-forward " \t\n\r")
(setq beg-pos (point))
(goto-char end-marker)
(skip-chars-backward " \t\n\r")
(setq end-pos (point)))
(when (or
(< end-pos beg-pos)
(and (= (line-number-at-pos beg-pos) (line-number-at-pos end-pos))
(< (+ (current-column) (- end-pos beg-pos) 2) fill-column)))
(just-one-space -1)
(goto-char end-marker)
(just-one-space -1))
(goto-char beg-marker))))
(defun enh-ruby-toggle-block ()
"Toggle block type from do-end to braces or back.
The block must begin on the current line or above it and end after the point.
If the result is do-end block, it will always be multiline."
(interactive)
(let* ((pos (point))
(block-start (save-excursion
(end-of-line)
(while (and (not (bobp))
(not (enh-ruby-point-block-p)))
(backward-char))
(point)))
(block-end (save-excursion
(goto-char block-start)
(enh-ruby-forward-sexp)
(point))))
(if (< pos block-end)
(if (eq (char-after block-start) ?{)
(enh-ruby-brace-to-do-end block-start block-end)
(enh-ruby-do-end-to-brace block-start block-end)))))
(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."
(condition-case nil
(save-excursion
(enh-ruby-beginning-of-defun 1)
(when (looking-at enh-ruby-defun-and-name-re)
(let ((def-or-mod (match-string-no-properties 1))
(def-name (match-string-no-properties 2)))
(if (string= "def" def-or-mod)
(progn
(enh-ruby-up-sexp)
(when (looking-at enh-ruby-defun-and-name-re)
(let ((_mod-or-class (match-string-no-properties 1))
(mod-name (match-string-no-properties 2)))
(let* ((meth-name-re (concat
(regexp-opt (list "self" mod-name)
'words)
"\\.\\(.+\\)"))
(cls-meth (and (string-match meth-name-re def-name)
(match-string 2 def-name)))
(name (or cls-meth def-name))
(sep (if cls-meth "." "#")))
(concat mod-name sep name)))))
nil))))))
;; Stolen shamelessly from James Clark's nxml-mode.
(defmacro erm-with-unmodifying-text-property-changes (&rest body)
"Evaluate BODY without modifying the buffer's text properties.
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)
(condition-case nil
(erm-req-parse nil nil nil)
(error 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 "")
(unless (buffer-live-p erm-parse-buff)
(erm-reset))
(when (buffer-live-p erm-parse-buff)
(with-current-buffer erm-parse-buff
(erm-with-unmodifying-text-property-changes
(erm-parse response))))))
(defun 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"))))
(defun extra-col-% ()
"Return extra column adjustments in case we are ‘looking-at’ a % construct."
(or (and (looking-at "%\\([^[:alnum:]]\\|[QqWwIixrs].\\)")
(1- (length (match-string-no-properties 0))))
0))
(defun enh-ruby-continue-p (prop)
"Return whether PROP is a continue property."
(eq 'c prop))
(defun enh-ruby-block-p (prop)
"Return whether PROP is a block property."
(eq 'd prop))
(defun enh-ruby-point-continue-p (point)
"Return whether property at POINT is a continue property."
(enh-ruby-continue-p (get-text-property point 'indent)))
(defun enh-ruby-point-block-p (&optional point)
"Return whether property at POINT is a block property."
(or point (setq point (point)))
(enh-ruby-block-p (get-text-property point 'indent)))
(defun enh-ruby-calculate-indent (&optional start-point)
"Calculate the indentation of the previous line and its level at START-POINT."
(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)
(let ((bprop (get-text-property (point) 'indent)))
(cond ((eq 'd bprop)
(setq pos (point))
(enh-ruby-skip-non-indentable)
(let ((indent (enh-ruby-calculate-indent-1 pos (line-beginning-position)))
(chained-stmt-p (save-excursion
(forward-line 0)
(enh-ruby-point-continue-p (point)))))
(+ indent
(if chained-stmt-p enh-ruby-hanging-indent-level 0))))
((and (not enh-ruby-deep-indent-construct)
(eq 'b bprop))
(current-indentation))
(t
(current-column)))))
((eq 'r prop) ; TODO: make these consistent file-wide
(let (opening-col opening-is-last-thing-on-line)
(save-excursion
(enh-ruby-backward-sexp)
(setq opening-col (+ (current-column)
(extra-col-%)))
(forward-char 1)
(skip-syntax-forward " " (line-end-position))
(setq opening-is-last-thing-on-line (eolp)))
(if (and enh-ruby-deep-indent-paren
(not enh-ruby-bounce-deep-indent)
(not opening-is-last-thing-on-line))
opening-col ; deep + !bounce + !hanging = match open
(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)))
(chained-stmt-p (save-excursion (enh-ruby-backward-sexp)
(forward-line 0)
(enh-ruby-point-continue-p (point))))
(offset (if (char-equal opening-char ?{)
enh-ruby-hanging-brace-indent-level
enh-ruby-hanging-paren-indent-level)))
(cond ((and chained-stmt-p
(not enh-ruby-bounce-deep-indent))
(- proposed-col offset))
((< proposed-col opening-col)
(- proposed-col offset))
(t opening-col))))))
((or (memq face '(font-lock-string-face enh-ruby-heredoc-delimiter-face))
(and (eq 'font-lock-variable-name-face face)
(looking-at "#")))
(when enh-ruby-preserve-indent-in-heredocs
(forward-line -1)
(back-to-indentation))
(current-column))
(t
(forward-line -1)
(enh-ruby-skip-non-indentable)
(enh-ruby-calculate-indent-1 pos (line-beginning-position))))))))