* misc/ruby-mode.el (ruby-forward-string): forward a string. [new]

* misc/ruby-mode.el (ruby-parse-region): handle nested parentheses
  in a string and terminators in #{}.

* misc/ruby-mode.el (ruby-calculate-indent): ditto.

* misc/ruby-mode.el (ruby-font-lock-syntactic-keywords):
  fix font-lock problem [ruby-talk:29296].


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1965 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2002-01-05 03:25:02 +00:00
parent 0ad520443c
commit 8090c2f214
2 changed files with 97 additions and 77 deletions

View File

@ -1,3 +1,12 @@
Fri Jan 4 17:23:49 2002 Nobuyoshi Nakada <nobu.nakada@nifty.ne.jp>
* misc/ruby-mode.el (ruby-forward-string): forward a string. [new]
* misc/ruby-mode.el (ruby-parse-region): handle nested parentheses
in a string and terminators in #{}.
* misc/ruby-mode.el (ruby-calculate-indent): ditto.
Sat Jan 5 00:19:12 2002 Nobuyoshi Nakada <nobu.nakada@nifty.ne.jp> Sat Jan 5 00:19:12 2002 Nobuyoshi Nakada <nobu.nakada@nifty.ne.jp>
* parse.y (yycompile): strdup()'ed twice. * parse.y (yycompile): strdup()'ed twice.
@ -95,6 +104,11 @@ Tue Dec 25 02:11:17 2001 Yukihiro Matsumoto <matz@ruby-lang.org>
* object.c (rb_check_convert_type): ditto. * object.c (rb_check_convert_type): ditto.
Mon Dec 24 02:37:40 2001 Le Wang <lewang@bigfoot.com>
* misc/ruby-mode.el (ruby-font-lock-syntactic-keywords):
fix font-lock problem [ruby-talk:29296].
Sat Dec 22 22:52:14 2001 Yukihiro Matsumoto <matz@ruby-lang.org> Sat Dec 22 22:52:14 2001 Yukihiro Matsumoto <matz@ruby-lang.org>
* time.c (time_timeval): wrong cast to time_t. * time.c (time_timeval): wrong cast to time_t.

View File

@ -253,6 +253,21 @@ The variable ruby-indent-level controls the amount of indentation.
(and (not (eq option 'expr-arg)) (and (not (eq option 'expr-arg))
(looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]")))))))))) (looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]"))))))))))
(defun ruby-forward-string (term &optional end no-error expand)
(let ((n 1) (c (string-to-char term))
(re (if expand
(concat "[^\\]\\(\\\\\\\\\\)*\\([" term "]\\|\\(#{\\)\\)")
(concat "[^\\]\\(\\\\\\\\\\)*[" term "]"))))
(while (and (re-search-forward re end no-error)
(if (match-beginning 3)
(ruby-forward-string "}{" end no-error nil)
(> (setq n (if (eq (char-before (point)) c)
(1- n) (1+ n))) 0)))
(forward-char -1))
(cond ((zerop n))
(no-error nil)
(error "unterminated string"))))
(defun ruby-parse-region (start end) (defun ruby-parse-region (start end)
(let ((indent-point end) (let ((indent-point end)
(indent 0) (indent 0)
@ -270,16 +285,22 @@ The variable ruby-indent-level controls the amount of indentation.
(while (and (> indent-point (point)) (while (and (> indent-point (point))
(re-search-forward ruby-delimiter indent-point t)) (re-search-forward ruby-delimiter indent-point t))
(or depth (setq depth 0)) (or depth (setq depth 0))
(let ((pnt (point)) w re) (let ((pnt (point)) w re expand)
(goto-char (match-beginning 0)) (goto-char (match-beginning 0))
(cond (cond
((or (looking-at "\"") ;skip string ((or (looking-at "\"") ;skip string
(looking-at "'")
(looking-at "`")) (looking-at "`"))
(setq w (char-after (point)))
(cond (cond
((and (not (eobp)) ((and (not (eobp))
(re-search-forward (format "[^\\]\\(\\\\\\\\\\)*%c" w) indent-point t)) (ruby-forward-string (buffer-substring (point) (1+ (point))) indent-point t t))
nil)
(t
(setq in-string (point))
(goto-char indent-point))))
((looking-at "'")
(cond
((and (not (eobp))
(re-search-forward "[^\\]\\(\\\\\\\\\\)*'" indent-point t))
nil) nil)
(t (t
(setq in-string (point)) (setq in-string (point))
@ -287,7 +308,7 @@ The variable ruby-indent-level controls the amount of indentation.
((looking-at "/") ((looking-at "/")
(cond (cond
((and (not (eobp)) (ruby-expr-beg)) ((and (not (eobp)) (ruby-expr-beg))
(if (re-search-forward "[^\\]\\(\\\\\\\\\\)*/" indent-point t) (if (ruby-forward-string "/" indent-point t t)
nil nil
(setq in-string (point)) (setq in-string (point))
(goto-char indent-point))) (goto-char indent-point)))
@ -299,6 +320,7 @@ The variable ruby-indent-level controls the amount of indentation.
(not (looking-at "%=")) (not (looking-at "%="))
(looking-at "%[Qqrxw]?\\(.\\)")) (looking-at "%[Qqrxw]?\\(.\\)"))
(goto-char (match-beginning 1)) (goto-char (match-beginning 1))
(setq expand (not (eq (char-before) ?q)))
(setq w (buffer-substring (match-beginning 1) (setq w (buffer-substring (match-beginning 1)
(match-end 1))) (match-end 1)))
(cond (cond
@ -306,24 +328,16 @@ The variable ruby-indent-level controls the amount of indentation.
((string= w "{") (setq re "}{")) ((string= w "{") (setq re "}{"))
((string= w "(") (setq re ")(")) ((string= w "(") (setq re ")("))
((string= w "<") (setq re "><")) ((string= w "<") (setq re "><"))
((member w '("*" "." "+" "?" "^" "$")) ((or (and expand (string= w "\\"))
(member w '("*" "." "+" "?" "^" "$")))
(setq w (concat "\\" w)))) (setq w (concat "\\" w))))
(if (if re (unless (cond (re (ruby-forward-string re indent-point t expand))
(let ((n 1)) (expand (ruby-forward-string w indent-point t t))
(setq re (concat "[^\\]\\(\\\\\\\\\\)*[" re "]")) (t (re-search-forward
(while (and (re-search-forward re indent-point t) (if (string= w "\\")
(> (setq n (if (eq (char-before (point)) "\\\\[^\\]*\\\\"
(string-to-char w)) (concat "[^\\]\\(\\\\\\\\\\)*" w))
(1+ n) (1- n))) indent-point t)))
0))
(forward-char -1))
(zerop n))
(re-search-forward
(if (string= w "\\")
"\\\\[^\\]*\\\\"
(concat "[^\\]\\(\\\\\\\\\\)*" w))
indent-point t))
nil
(setq in-string (point)) (setq in-string (point))
(goto-char indent-point))) (goto-char indent-point)))
(t (t
@ -462,7 +476,7 @@ The variable ruby-indent-level controls the amount of indentation.
(goto-char (cdr (nth 1 s))) (goto-char (cdr (nth 1 s)))
(forward-word -1) (forward-word -1)
(setq indent (ruby-indent-size (current-column) (nth 2 state)))) (setq indent (ruby-indent-size (current-column) (nth 2 state))))
(t (t
(setq indent (current-column))))) (setq indent (current-column)))))
(cond (cond
((nth 3 state) ((nth 3 state)
@ -472,7 +486,7 @@ The variable ruby-indent-level controls the amount of indentation.
(goto-char parse-start) (goto-char parse-start)
(back-to-indentation) (back-to-indentation)
(setq indent (ruby-indent-size (current-column) (nth 2 state))))) (setq indent (ruby-indent-size (current-column) (nth 2 state)))))
)) ))
((and (nth 2 state)(> (nth 2 state) 0)) ; in nest ((and (nth 2 state)(> (nth 2 state) 0)) ; in nest
(if (null (cdr (nth 1 state))) (if (null (cdr (nth 1 state)))
(error "invalid nest")) (error "invalid nest"))
@ -500,7 +514,7 @@ The variable ruby-indent-level controls the amount of indentation.
(end-of-line) (end-of-line)
(setq eol (point)) (setq eol (point))
(beginning-of-line) (beginning-of-line)
(cond (cond
((re-search-forward ruby-negative eol t) ((re-search-forward ruby-negative eol t)
(and (not (eq ?_ (char-after (match-end 0)))) (and (not (eq ?_ (char-after (match-end 0))))
(setq indent (- indent ruby-indent-level)))) (setq indent (- indent ruby-indent-level))))
@ -528,19 +542,11 @@ The variable ruby-indent-level controls the amount of indentation.
(end-of-line) (end-of-line)
(skip-chars-backward " \t") (skip-chars-backward " \t")
(let ((pos (point))) (let ((pos (point)))
(and (while (and (re-search-backward "#" bol t)
(re-search-backward "#" (save-excursion (= (char-before) ??))
(beginning-of-line) (forward-char -1))
(point)) t) (skip-chars-backward " \t")
(if (not (= (point) (point-min))) (and
(save-excursion
(forward-char -1)
(not (looking-at "\\?"))))
(skip-chars-backward " \t")
(if (save-excursion
(forward-char -1)
(looking-at "\\?"))
(skip-chars-forward " \t"))
(setq state (ruby-parse-region parse-start (point))) (setq state (ruby-parse-region parse-start (point)))
(nth 0 state) (nth 0 state)
(goto-char pos))) (goto-char pos)))
@ -681,45 +687,45 @@ An end of a defun is found by moving forward from the beginning of one."
(or (boundp 'font-lock-variable-name-face) (or (boundp 'font-lock-variable-name-face)
(setq font-lock-variable-name-face font-lock-type-face)) (setq font-lock-variable-name-face font-lock-type-face))
(setq ruby-font-lock-syntactic-keywords
'(
;; #{ }, #$hoge, #@foo are not comments
("\\(#\\)[{$@]" 1 (1 . nil))
;; the last $' in the string ,'...$' is not variable
;; the last ?' in the string ,'...?' is not ascii code
("\\(^\\|[[\\s <+(,=]\\)\\('\\)[^'\n\\\\]*\\(\\\\.[^'\n\\\\]*\\)*[?$]\\('\\)"
(2 (7 . nil))
(4 (7 . nil)))
;; the last $` in the string ,`...$` is not variable
;; the last ?` in the string ,`...?` is not ascii code
("\\(^\\|[[\\s <+(,=]\\)\\(`\\)[^`\n\\\\]*\\(\\\\.[^`\n\\\\]*\\)*[?$]\\(`\\)"
(2 (7 . nil))
(4 (7 . nil)))
;; the last $" in the string ,"...$" is not variable
;; the last ?" in the string ,"...?" is not ascii code
("\\(^\\|[[\\s <+(,=]\\)\\(\"\\)[^\"\n\\\\]*\\(\\\\.[^\"\n\\\\]*\\)*[?$]\\(\"\\)"
(2 (7 . nil))
(4 (7 . nil)))
;; $' $" $` .... are variables
;; ?' ?" ?` are ascii codes
("[?$][#\"'`]" 0 (1 . nil))
;; regexps
("\\(^\\|[=(,~?:;]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
(4 (7 . ?/))
(6 (7 . ?/)))
;; %Q!...!
("\\(^\\|[[\\s <+(,=]\\)%[xrqQ]?\\([^a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\2\\)"
(2 (7 . nil))
(4 (7 . nil)))
("^\\(=\\)begin\\(\\s \\|$\\)" 1 (7 . nil))
("^\\(=\\)end\\(\\s \\|$\\)" 1 (7 . nil))))
(add-hook 'ruby-mode-hook (put 'ruby-mode 'font-lock-defaults
'(lambda () '((ruby-font-lock-keywords)
(make-local-variable 'ruby-font-lock-syntactic-keywords) nil nil nil
(setq ruby-font-lock-syntactic-keywords beginning-of-line
'( (font-lock-syntactic-keywords
;; #{ }, #$hoge, #@foo are not comments . ruby-font-lock-syntactic-keywords)))
("\\(#\\)[{$@]" 1 (1 . nil))
;; the last $' in the string ,'...$' is not variable
;; the last ?' in the string ,'...?' is not ascii code
("\\(^\\|[[\\s <+(,=]\\)\\('\\)[^'\n\\\\]*\\(\\\\.[^'\n\\\\]*\\)*[?$]\\('\\)"
(2 (7 . nil))
(4 (7 . nil)))
;; the last $` in the string ,`...$` is not variable
;; the last ?` in the string ,`...?` is not ascii code
("\\(^\\|[[\\s <+(,=]\\)\\(`\\)[^`\n\\\\]*\\(\\\\.[^`\n\\\\]*\\)*[?$]\\(`\\)"
(2 (7 . nil))
(4 (7 . nil)))
;; the last $" in the string ,"...$" is not variable
;; the last ?" in the string ,"...?" is not ascii code
("\\(^\\|[[\\s <+(,=]\\)\\(\"\\)[^\"\n\\\\]*\\(\\\\.[^\"\n\\\\]*\\)*[?$]\\(\"\\)"
(2 (7 . nil))
(4 (7 . nil)))
;; $' $" $` .... are variables
;; ?' ?" ?` are ascii codes
("[?$][#\"'`]" 0 (1 . nil))
;; regexps
("\\(^\\|[=(,~?:;]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
(4 (7 . ?/))
(6 (7 . ?/)))
;; %Q!...!
("\\(^\\|[[\\s <+(,=]\\)%[xrqQ]?\\([^a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\2\\)"
(2 (7 . nil))
(4 (7 . nil)))
("^\\(=\\)begin\\(\\s \\|$\\)" 1 (7 . nil))
("^\\(=\\)end\\(\\s \\|$\\)" 1 (7 . nil))))
(make-local-variable 'font-lock-defaults)
(setq font-lock-defaults '((ruby-font-lock-keywords) nil nil))
(setq font-lock-keywords ruby-font-lock-keywords)))
(defun ruby-font-lock-docs (limit) (defun ruby-font-lock-docs (limit)
(if (re-search-forward "^=begin\\(\\s \\|$\\)" limit t) (if (re-search-forward "^=begin\\(\\s \\|$\\)" limit t)
@ -797,7 +803,7 @@ An end of a defun is found by moving forward from the beginning of one."
2 font-lock-variable-name-face) 2 font-lock-variable-name-face)
;; variables ;; variables
'("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W" '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
1 font-lock-variable-name-face) 1 font-lock-variable-name-face)
'("\\(\\$\\|@\\|@@\\)\\(\\w\\(\\w\\|_\\)*\\|#{\\)" '("\\(\\$\\|@\\|@@\\)\\(\\w\\(\\w\\|_\\)*\\|#{\\)"
0 font-lock-variable-name-face) 0 font-lock-variable-name-face)
;; embedded document ;; embedded document