ruby-mode.el 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  1. ;;; ruby-mode.el --- Major mode for editing Ruby files
  2. ;; Copyright (C) 1994-2012 Free Software Foundation, Inc.
  3. ;; Authors: Yukihiro Matsumoto
  4. ;; Nobuyoshi Nakada
  5. ;; URL: http://www.emacswiki.org/cgi-bin/wiki/RubyMode
  6. ;; Created: Fri Feb 4 14:49:13 JST 1994
  7. ;; Keywords: languages ruby
  8. ;; Version: 1.0
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; Provides font-locking, indentation support, and navigation for Ruby code.
  22. ;;
  23. ;; If you're installing manually, you should add this to your .emacs
  24. ;; file after putting it on your load path:
  25. ;;
  26. ;; (autoload 'ruby-mode "ruby-mode" "Major mode for ruby files" t)
  27. ;; (add-to-list 'auto-mode-alist '("\\.rb$" . ruby-mode))
  28. ;; (add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))
  29. ;;
  30. ;; Still needs more docstrings; search below for TODO.
  31. ;;; Code:
  32. (eval-when-compile (require 'cl))
  33. (defgroup ruby nil
  34. "Major mode for editing Ruby code."
  35. :prefix "ruby-"
  36. :group 'languages)
  37. (defconst ruby-keyword-end-re
  38. (if (string-match "\\_>" "ruby")
  39. "\\_>"
  40. "\\>"))
  41. (defconst ruby-block-beg-keywords
  42. '("class" "module" "def" "if" "unless" "case" "while" "until" "for" "begin" "do")
  43. "Keywords at the beginning of blocks.")
  44. (defconst ruby-block-beg-re
  45. (regexp-opt ruby-block-beg-keywords)
  46. "Regexp to match the beginning of blocks.")
  47. (defconst ruby-non-block-do-re
  48. (concat (regexp-opt '("while" "until" "for" "rescue") t) ruby-keyword-end-re)
  49. "Regexp to match keywords that nest without blocks.")
  50. (defconst ruby-indent-beg-re
  51. (concat "\\(\\s *" (regexp-opt '("class" "module" "def") t) "\\)\\|"
  52. (regexp-opt '("if" "unless" "case" "while" "until" "for" "begin")))
  53. "Regexp to match where the indentation gets deeper.")
  54. (defconst ruby-modifier-beg-keywords
  55. '("if" "unless" "while" "until")
  56. "Modifiers that are the same as the beginning of blocks.")
  57. (defconst ruby-modifier-beg-re
  58. (regexp-opt ruby-modifier-beg-keywords)
  59. "Regexp to match modifiers same as the beginning of blocks.")
  60. (defconst ruby-modifier-re
  61. (regexp-opt (cons "rescue" ruby-modifier-beg-keywords))
  62. "Regexp to match modifiers.")
  63. (defconst ruby-block-mid-keywords
  64. '("then" "else" "elsif" "when" "rescue" "ensure")
  65. "Keywords where the indentation gets shallower in middle of block statements.")
  66. (defconst ruby-block-mid-re
  67. (regexp-opt ruby-block-mid-keywords)
  68. "Regexp to match where the indentation gets shallower in middle of block statements.")
  69. (defconst ruby-block-op-keywords
  70. '("and" "or" "not")
  71. "Regexp to match boolean keywords.")
  72. (defconst ruby-block-hanging-re
  73. (regexp-opt (append ruby-modifier-beg-keywords ruby-block-op-keywords))
  74. "Regexp to match hanging block modifiers.")
  75. (defconst ruby-block-end-re "\\_<end\\_>")
  76. (eval-and-compile
  77. (defconst ruby-here-doc-beg-re
  78. "\\(<\\)<\\(-\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)"
  79. "Regexp to match the beginning of a heredoc."))
  80. (defun ruby-here-doc-end-match ()
  81. "Return a regexp to find the end of a heredoc.
  82. This should only be called after matching against `ruby-here-doc-beg-re'."
  83. (concat "^"
  84. (if (match-string 2) "[ \t]*" nil)
  85. (regexp-quote
  86. (or (match-string 4)
  87. (match-string 5)
  88. (match-string 6)))))
  89. (defconst ruby-delimiter
  90. (concat "[?$/%(){}#\"'`.:]\\|<<\\|\\[\\|\\]\\|\\_<\\("
  91. ruby-block-beg-re
  92. "\\)\\_>\\|" ruby-block-end-re
  93. "\\|^=begin\\|" ruby-here-doc-beg-re))
  94. (defconst ruby-negative
  95. (concat "^[ \t]*\\(\\(" ruby-block-mid-re "\\)\\>\\|"
  96. ruby-block-end-re "\\|}\\|\\]\\)")
  97. "Regexp to match where the indentation gets shallower.")
  98. (defconst ruby-operator-re "[-,.+*/%&|^~=<>:]"
  99. "Regexp to match operators.")
  100. (defconst ruby-symbol-chars "a-zA-Z0-9_"
  101. "List of characters that symbol names may contain.")
  102. (defconst ruby-symbol-re (concat "[" ruby-symbol-chars "]")
  103. "Regexp to match symbols.")
  104. (define-abbrev-table 'ruby-mode-abbrev-table ()
  105. "Abbrev table in use in Ruby mode buffers.")
  106. (defvar ruby-mode-map
  107. (let ((map (make-sparse-keymap)))
  108. (define-key map "{" 'ruby-electric-brace)
  109. (define-key map "}" 'ruby-electric-brace)
  110. (define-key map (kbd "M-C-a") 'ruby-beginning-of-defun)
  111. (define-key map (kbd "M-C-e") 'ruby-end-of-defun)
  112. (define-key map (kbd "M-C-b") 'ruby-backward-sexp)
  113. (define-key map (kbd "M-C-f") 'ruby-forward-sexp)
  114. (define-key map (kbd "M-C-p") 'ruby-beginning-of-block)
  115. (define-key map (kbd "M-C-n") 'ruby-end-of-block)
  116. (define-key map (kbd "M-C-h") 'ruby-mark-defun)
  117. (define-key map (kbd "M-C-q") 'ruby-indent-exp)
  118. (define-key map (kbd "C-M-h") 'backward-kill-word)
  119. (define-key map (kbd "C-j") 'reindent-then-newline-and-indent)
  120. (define-key map (kbd "C-m") 'newline)
  121. (define-key map (kbd "C-c C-c") 'comment-region)
  122. map)
  123. "Keymap used in Ruby mode.")
  124. (defvar ruby-mode-syntax-table
  125. (let ((table (make-syntax-table)))
  126. (modify-syntax-entry ?\' "\"" table)
  127. (modify-syntax-entry ?\" "\"" table)
  128. (modify-syntax-entry ?\` "\"" table)
  129. (modify-syntax-entry ?# "<" table)
  130. (modify-syntax-entry ?\n ">" table)
  131. (modify-syntax-entry ?\\ "\\" table)
  132. (modify-syntax-entry ?$ "." table)
  133. (modify-syntax-entry ?? "_" table)
  134. (modify-syntax-entry ?_ "_" table)
  135. (modify-syntax-entry ?: "_" table)
  136. (modify-syntax-entry ?< "." table)
  137. (modify-syntax-entry ?> "." table)
  138. (modify-syntax-entry ?& "." table)
  139. (modify-syntax-entry ?| "." table)
  140. (modify-syntax-entry ?% "." table)
  141. (modify-syntax-entry ?= "." table)
  142. (modify-syntax-entry ?/ "." table)
  143. (modify-syntax-entry ?+ "." table)
  144. (modify-syntax-entry ?* "." table)
  145. (modify-syntax-entry ?- "." table)
  146. (modify-syntax-entry ?\; "." table)
  147. (modify-syntax-entry ?\( "()" table)
  148. (modify-syntax-entry ?\) ")(" table)
  149. (modify-syntax-entry ?\{ "(}" table)
  150. (modify-syntax-entry ?\} "){" table)
  151. (modify-syntax-entry ?\[ "(]" table)
  152. (modify-syntax-entry ?\] ")[" table)
  153. table)
  154. "Syntax table to use in Ruby mode.")
  155. (defcustom ruby-indent-tabs-mode nil
  156. "Indentation can insert tabs in Ruby mode if this is non-nil."
  157. :type 'boolean :group 'ruby)
  158. (defcustom ruby-indent-level 2
  159. "Indentation of Ruby statements."
  160. :type 'integer :group 'ruby)
  161. (defcustom ruby-comment-column 32
  162. "Indentation column of comments."
  163. :type 'integer :group 'ruby)
  164. (defcustom ruby-deep-arglist t
  165. "Deep indent lists in parenthesis when non-nil.
  166. Also ignores spaces after parenthesis when 'space."
  167. :group 'ruby)
  168. (defcustom ruby-deep-indent-paren '(?\( ?\[ ?\] t)
  169. "Deep indent lists in parenthesis when non-nil.
  170. The value t means continuous line.
  171. Also ignores spaces after parenthesis when 'space."
  172. :group 'ruby)
  173. (defcustom ruby-deep-indent-paren-style 'space
  174. "Default deep indent style."
  175. :options '(t nil space) :group 'ruby)
  176. (defcustom ruby-encoding-map '((shift_jis . cp932) (shift-jis . cp932))
  177. "Alist to map encoding name from Emacs to Ruby."
  178. :group 'ruby)
  179. (defcustom ruby-insert-encoding-magic-comment t
  180. "Insert a magic Emacs 'coding' comment upon save if this is non-nil."
  181. :type 'boolean :group 'ruby)
  182. (defcustom ruby-use-encoding-map t
  183. "Use `ruby-encoding-map' to set encoding magic comment if this is non-nil."
  184. :type 'boolean :group 'ruby)
  185. ;; Safe file variables
  186. (put 'ruby-indent-tabs-mode 'safe-local-variable 'booleanp)
  187. (put 'ruby-indent-level 'safe-local-variable 'integerp)
  188. (put 'ruby-comment-column 'safe-local-variable 'integerp)
  189. (put 'ruby-deep-arglist 'safe-local-variable 'booleanp)
  190. (defun ruby-imenu-create-index-in-block (prefix beg end)
  191. "Create an imenu index of methods inside a block."
  192. (let ((index-alist '()) (case-fold-search nil)
  193. name next pos decl sing)
  194. (goto-char beg)
  195. (while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\)\\s +\\([^\(\n ]+\\)\\)" end t)
  196. (setq sing (match-beginning 3))
  197. (setq decl (match-string 5))
  198. (setq next (match-end 0))
  199. (setq name (or (match-string 4) (match-string 6)))
  200. (setq pos (match-beginning 0))
  201. (cond
  202. ((string= "alias" decl)
  203. (if prefix (setq name (concat prefix name)))
  204. (push (cons name pos) index-alist))
  205. ((string= "def" decl)
  206. (if prefix
  207. (setq name
  208. (cond
  209. ((string-match "^self\." name)
  210. (concat (substring prefix 0 -1) (substring name 4)))
  211. (t (concat prefix name)))))
  212. (push (cons name pos) index-alist)
  213. (ruby-accurate-end-of-block end))
  214. (t
  215. (if (string= "self" name)
  216. (if prefix (setq name (substring prefix 0 -1)))
  217. (if prefix (setq name (concat (substring prefix 0 -1) "::" name)))
  218. (push (cons name pos) index-alist))
  219. (ruby-accurate-end-of-block end)
  220. (setq beg (point))
  221. (setq index-alist
  222. (nconc (ruby-imenu-create-index-in-block
  223. (concat name (if sing "." "#"))
  224. next beg) index-alist))
  225. (goto-char beg))))
  226. index-alist))
  227. (defun ruby-imenu-create-index ()
  228. "Create an imenu index of all methods in the buffer."
  229. (nreverse (ruby-imenu-create-index-in-block nil (point-min) nil)))
  230. (defun ruby-accurate-end-of-block (&optional end)
  231. "TODO: document."
  232. (let (state
  233. (end (or end (point-max))))
  234. (while (and (setq state (apply 'ruby-parse-partial end state))
  235. (>= (nth 2 state) 0) (< (point) end)))))
  236. (defun ruby-mode-variables ()
  237. "Set up initial buffer-local variables for Ruby mode."
  238. (set-syntax-table ruby-mode-syntax-table)
  239. (setq local-abbrev-table ruby-mode-abbrev-table)
  240. (setq indent-tabs-mode ruby-indent-tabs-mode)
  241. (set (make-local-variable 'indent-line-function) 'ruby-indent-line)
  242. (set (make-local-variable 'require-final-newline) t)
  243. (set (make-local-variable 'comment-start) "# ")
  244. (set (make-local-variable 'comment-end) "")
  245. (set (make-local-variable 'comment-column) ruby-comment-column)
  246. (set (make-local-variable 'comment-start-skip) "#+ *")
  247. (set (make-local-variable 'parse-sexp-ignore-comments) t)
  248. (set (make-local-variable 'parse-sexp-lookup-properties) t)
  249. (set (make-local-variable 'paragraph-start) (concat "$\\|" page-delimiter))
  250. (set (make-local-variable 'paragraph-separate) paragraph-start)
  251. (set (make-local-variable 'paragraph-ignore-fill-prefix) t))
  252. (defun ruby-mode-set-encoding ()
  253. "Insert a magic comment header with the proper encoding if necessary."
  254. (save-excursion
  255. (widen)
  256. (goto-char (point-min))
  257. (when (re-search-forward "[^\0-\177]" nil t)
  258. (goto-char (point-min))
  259. (let ((coding-system
  260. (or coding-system-for-write
  261. buffer-file-coding-system)))
  262. (if coding-system
  263. (setq coding-system
  264. (or (coding-system-get coding-system 'mime-charset)
  265. (coding-system-change-eol-conversion coding-system nil))))
  266. (setq coding-system
  267. (if coding-system
  268. (symbol-name
  269. (or (and ruby-use-encoding-map
  270. (cdr (assq coding-system ruby-encoding-map)))
  271. coding-system))
  272. "ascii-8bit"))
  273. (if (looking-at "^#!") (beginning-of-line 2))
  274. (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
  275. (unless (string= (match-string 2) coding-system)
  276. (goto-char (match-beginning 2))
  277. (delete-region (point) (match-end 2))
  278. (and (looking-at "-\*-")
  279. (let ((n (skip-chars-backward " ")))
  280. (cond ((= n 0) (insert " ") (backward-char))
  281. ((= n -1) (insert " "))
  282. ((forward-char)))))
  283. (insert coding-system)))
  284. ((looking-at "\\s *#.*coding\\s *[:=]"))
  285. (t (when ruby-insert-encoding-magic-comment
  286. (insert "# -*- coding: " coding-system " -*-\n"))))))))
  287. (defun ruby-current-indentation ()
  288. "Return the indentation level of current line."
  289. (save-excursion
  290. (beginning-of-line)
  291. (back-to-indentation)
  292. (current-column)))
  293. (defun ruby-indent-line (&optional ignored)
  294. "Correct the indentation of the current Ruby line."
  295. (interactive)
  296. (ruby-indent-to (ruby-calculate-indent)))
  297. (defun ruby-indent-to (column)
  298. "Indent the current line to COLUMN."
  299. (when column
  300. (let (shift top beg)
  301. (and (< column 0) (error "invalid nest"))
  302. (setq shift (current-column))
  303. (beginning-of-line)
  304. (setq beg (point))
  305. (back-to-indentation)
  306. (setq top (current-column))
  307. (skip-chars-backward " \t")
  308. (if (>= shift top) (setq shift (- shift top))
  309. (setq shift 0))
  310. (if (and (bolp)
  311. (= column top))
  312. (move-to-column (+ column shift))
  313. (move-to-column top)
  314. (delete-region beg (point))
  315. (beginning-of-line)
  316. (indent-to column)
  317. (move-to-column (+ column shift))))))
  318. (defun ruby-special-char-p (&optional pos)
  319. "Return t if the character before POS is a special character.
  320. If omitted, POS defaults to the current point.
  321. Special characters are `?', `$', `:' when preceded by whitespace,
  322. and `\\' when preceded by `?'."
  323. (setq pos (or pos (point)))
  324. (let ((c (char-before pos)) (b (and (< (point-min) pos)
  325. (char-before (1- pos)))))
  326. (cond ((or (eq c ??) (eq c ?$)))
  327. ((and (eq c ?:) (or (not b) (eq (char-syntax b) ? ))))
  328. ((eq c ?\\) (eq b ??)))))
  329. (defun ruby-expr-beg (&optional option)
  330. "TODO: document."
  331. (save-excursion
  332. (store-match-data nil)
  333. (let ((space (skip-chars-backward " \t")))
  334. (cond
  335. ((bolp) t)
  336. ((progn
  337. (forward-char -1)
  338. (and (looking-at "\\?")
  339. (or (eq (char-syntax (char-before (point))) ?w)
  340. (ruby-special-char-p))))
  341. nil)
  342. ((and (eq option 'heredoc) (< space 0)) t)
  343. ((or (looking-at ruby-operator-re)
  344. (looking-at "[\\[({,;]")
  345. (and (looking-at "[!?]")
  346. (or (not (eq option 'modifier))
  347. (bolp)
  348. (save-excursion (forward-char -1) (looking-at "\\Sw$"))))
  349. (and (looking-at ruby-symbol-re)
  350. (skip-chars-backward ruby-symbol-chars)
  351. (cond
  352. ((looking-at (regexp-opt
  353. (append ruby-block-beg-keywords
  354. ruby-block-op-keywords
  355. ruby-block-mid-keywords)
  356. 'words))
  357. (goto-char (match-end 0))
  358. (not (looking-at "\\s_")))
  359. ((eq option 'expr-qstr)
  360. (looking-at "[a-zA-Z][a-zA-z0-9_]* +%[^ \t]"))
  361. ((eq option 'expr-re)
  362. (looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]"))
  363. (t nil)))))))))
  364. (defun ruby-forward-string (term &optional end no-error expand)
  365. "TODO: document."
  366. (let ((n 1) (c (string-to-char term))
  367. (re (if expand
  368. (concat "[^\\]\\(\\\\\\\\\\)*\\([" term "]\\|\\(#{\\)\\)")
  369. (concat "[^\\]\\(\\\\\\\\\\)*[" term "]"))))
  370. (while (and (re-search-forward re end no-error)
  371. (if (match-beginning 3)
  372. (ruby-forward-string "}{" end no-error nil)
  373. (> (setq n (if (eq (char-before (point)) c)
  374. (1- n) (1+ n))) 0)))
  375. (forward-char -1))
  376. (cond ((zerop n))
  377. (no-error nil)
  378. ((error "unterminated string")))))
  379. (defun ruby-deep-indent-paren-p (c)
  380. "TODO: document."
  381. (cond ((listp ruby-deep-indent-paren)
  382. (let ((deep (assoc c ruby-deep-indent-paren)))
  383. (cond (deep
  384. (or (cdr deep) ruby-deep-indent-paren-style))
  385. ((memq c ruby-deep-indent-paren)
  386. ruby-deep-indent-paren-style))))
  387. ((eq c ruby-deep-indent-paren) ruby-deep-indent-paren-style)
  388. ((eq c ?\( ) ruby-deep-arglist)))
  389. (defun ruby-parse-partial (&optional end in-string nest depth pcol indent)
  390. "TODO: document throughout function body."
  391. (or depth (setq depth 0))
  392. (or indent (setq indent 0))
  393. (when (re-search-forward ruby-delimiter end 'move)
  394. (let ((pnt (point)) w re expand)
  395. (goto-char (match-beginning 0))
  396. (cond
  397. ((and (memq (char-before) '(?@ ?$)) (looking-at "\\sw"))
  398. (goto-char pnt))
  399. ((looking-at "[\"`]") ;skip string
  400. (cond
  401. ((and (not (eobp))
  402. (ruby-forward-string (buffer-substring (point) (1+ (point))) end t t))
  403. nil)
  404. (t
  405. (setq in-string (point))
  406. (goto-char end))))
  407. ((looking-at "'")
  408. (cond
  409. ((and (not (eobp))
  410. (re-search-forward "[^\\]\\(\\\\\\\\\\)*'" end t))
  411. nil)
  412. (t
  413. (setq in-string (point))
  414. (goto-char end))))
  415. ((looking-at "/=")
  416. (goto-char pnt))
  417. ((looking-at "/")
  418. (cond
  419. ((and (not (eobp)) (ruby-expr-beg 'expr-re))
  420. (if (ruby-forward-string "/" end t t)
  421. nil
  422. (setq in-string (point))
  423. (goto-char end)))
  424. (t
  425. (goto-char pnt))))
  426. ((looking-at "%")
  427. (cond
  428. ((and (not (eobp))
  429. (ruby-expr-beg 'expr-qstr)
  430. (not (looking-at "%="))
  431. (looking-at "%[QqrxWw]?\\([^a-zA-Z0-9 \t\n]\\)"))
  432. (goto-char (match-beginning 1))
  433. (setq expand (not (memq (char-before) '(?q ?w))))
  434. (setq w (match-string 1))
  435. (cond
  436. ((string= w "[") (setq re "]["))
  437. ((string= w "{") (setq re "}{"))
  438. ((string= w "(") (setq re ")("))
  439. ((string= w "<") (setq re "><"))
  440. ((and expand (string= w "\\"))
  441. (setq w (concat "\\" w))))
  442. (unless (cond (re (ruby-forward-string re end t expand))
  443. (expand (ruby-forward-string w end t t))
  444. (t (re-search-forward
  445. (if (string= w "\\")
  446. "\\\\[^\\]*\\\\"
  447. (concat "[^\\]\\(\\\\\\\\\\)*" w))
  448. end t)))
  449. (setq in-string (point))
  450. (goto-char end)))
  451. (t
  452. (goto-char pnt))))
  453. ((looking-at "\\?") ;skip ?char
  454. (cond
  455. ((and (ruby-expr-beg)
  456. (looking-at "?\\(\\\\C-\\|\\\\M-\\)*\\\\?."))
  457. (goto-char (match-end 0)))
  458. (t
  459. (goto-char pnt))))
  460. ((looking-at "\\$") ;skip $char
  461. (goto-char pnt)
  462. (forward-char 1))
  463. ((looking-at "#") ;skip comment
  464. (forward-line 1)
  465. (goto-char (point))
  466. )
  467. ((looking-at "[\\[{(]")
  468. (let ((deep (ruby-deep-indent-paren-p (char-after))))
  469. (if (and deep (or (not (eq (char-after) ?\{)) (ruby-expr-beg)))
  470. (progn
  471. (and (eq deep 'space) (looking-at ".\\s +[^# \t\n]")
  472. (setq pnt (1- (match-end 0))))
  473. (setq nest (cons (cons (char-after (point)) pnt) nest))
  474. (setq pcol (cons (cons pnt depth) pcol))
  475. (setq depth 0))
  476. (setq nest (cons (cons (char-after (point)) pnt) nest))
  477. (setq depth (1+ depth))))
  478. (goto-char pnt)
  479. )
  480. ((looking-at "[])}]")
  481. (if (ruby-deep-indent-paren-p (matching-paren (char-after)))
  482. (setq depth (cdr (car pcol)) pcol (cdr pcol))
  483. (setq depth (1- depth)))
  484. (setq nest (cdr nest))
  485. (goto-char pnt))
  486. ((looking-at ruby-block-end-re)
  487. (if (or (and (not (bolp))
  488. (progn
  489. (forward-char -1)
  490. (setq w (char-after (point)))
  491. (or (eq ?_ w)
  492. (eq ?. w))))
  493. (progn
  494. (goto-char pnt)
  495. (setq w (char-after (point)))
  496. (or (eq ?_ w)
  497. (eq ?! w)
  498. (eq ?? w))))
  499. nil
  500. (setq nest (cdr nest))
  501. (setq depth (1- depth)))
  502. (goto-char pnt))
  503. ((looking-at "def\\s +[^(\n;]*")
  504. (if (or (bolp)
  505. (progn
  506. (forward-char -1)
  507. (not (eq ?_ (char-after (point))))))
  508. (progn
  509. (setq nest (cons (cons nil pnt) nest))
  510. (setq depth (1+ depth))))
  511. (goto-char (match-end 0)))
  512. ((looking-at (concat "\\_<\\(" ruby-block-beg-re "\\)\\_>"))
  513. (and
  514. (save-match-data
  515. (or (not (looking-at (concat "do" ruby-keyword-end-re)))
  516. (save-excursion
  517. (back-to-indentation)
  518. (not (looking-at ruby-non-block-do-re)))))
  519. (or (bolp)
  520. (progn
  521. (forward-char -1)
  522. (setq w (char-after (point)))
  523. (not (or (eq ?_ w)
  524. (eq ?. w)))))
  525. (goto-char pnt)
  526. (setq w (char-after (point)))
  527. (not (eq ?_ w))
  528. (not (eq ?! w))
  529. (not (eq ?? w))
  530. (skip-chars-forward " \t")
  531. (goto-char (match-beginning 0))
  532. (or (not (looking-at ruby-modifier-re))
  533. (ruby-expr-beg 'modifier))
  534. (goto-char pnt)
  535. (setq nest (cons (cons nil pnt) nest))
  536. (setq depth (1+ depth)))
  537. (goto-char pnt))
  538. ((looking-at ":\\(['\"]\\)")
  539. (goto-char (match-beginning 1))
  540. (ruby-forward-string (buffer-substring (match-beginning 1) (match-end 1)) end))
  541. ((looking-at ":\\([-,.+*/%&|^~<>]=?\\|===?\\|<=>\\|![~=]?\\)")
  542. (goto-char (match-end 0)))
  543. ((looking-at ":\\([a-zA-Z_][a-zA-Z_0-9]*[!?=]?\\)?")
  544. (goto-char (match-end 0)))
  545. ((or (looking-at "\\.\\.\\.?")
  546. (looking-at "\\.[0-9]+")
  547. (looking-at "\\.[a-zA-Z_0-9]+")
  548. (looking-at "\\."))
  549. (goto-char (match-end 0)))
  550. ((looking-at "^=begin")
  551. (if (re-search-forward "^=end" end t)
  552. (forward-line 1)
  553. (setq in-string (match-end 0))
  554. (goto-char end)))
  555. ((looking-at "<<")
  556. (cond
  557. ((and (ruby-expr-beg 'heredoc)
  558. (looking-at "<<\\(-\\)?\\(\\([\"'`]\\)\\([^\n]+?\\)\\3\\|\\(?:\\sw\\|\\s_\\)+\\)"))
  559. (setq re (regexp-quote (or (match-string 4) (match-string 2))))
  560. (if (match-beginning 1) (setq re (concat "\\s *" re)))
  561. (let* ((id-end (goto-char (match-end 0)))
  562. (line-end-position (point-at-eol))
  563. (state (list in-string nest depth pcol indent)))
  564. ;; parse the rest of the line
  565. (while (and (> line-end-position (point))
  566. (setq state (apply 'ruby-parse-partial
  567. line-end-position state))))
  568. (setq in-string (car state)
  569. nest (nth 1 state)
  570. depth (nth 2 state)
  571. pcol (nth 3 state)
  572. indent (nth 4 state))
  573. ;; skip heredoc section
  574. (if (re-search-forward (concat "^" re "$") end 'move)
  575. (forward-line 1)
  576. (setq in-string id-end)
  577. (goto-char end))))
  578. (t
  579. (goto-char pnt))))
  580. ((looking-at "^__END__$")
  581. (goto-char pnt))
  582. ((and (looking-at ruby-here-doc-beg-re)
  583. (boundp 'ruby-indent-point))
  584. (if (re-search-forward (ruby-here-doc-end-match)
  585. ruby-indent-point t)
  586. (forward-line 1)
  587. (setq in-string (match-end 0))
  588. (goto-char ruby-indent-point)))
  589. (t
  590. (error (format "bad string %s"
  591. (buffer-substring (point) pnt)
  592. ))))))
  593. (list in-string nest depth pcol))
  594. (defun ruby-parse-region (start end)
  595. "TODO: document."
  596. (let (state)
  597. (save-excursion
  598. (if start
  599. (goto-char start)
  600. (ruby-beginning-of-indent))
  601. (save-restriction
  602. (narrow-to-region (point) end)
  603. (while (and (> end (point))
  604. (setq state (apply 'ruby-parse-partial end state))))))
  605. (list (nth 0 state) ; in-string
  606. (car (nth 1 state)) ; nest
  607. (nth 2 state) ; depth
  608. (car (car (nth 3 state))) ; pcol
  609. ;(car (nth 5 state)) ; indent
  610. )))
  611. (defun ruby-indent-size (pos nest)
  612. "Return the indentation level in spaces NEST levels deeper than POS."
  613. (+ pos (* (or nest 1) ruby-indent-level)))
  614. (defun ruby-calculate-indent (&optional parse-start)
  615. "Return the proper indentation level of the current line."
  616. ;; TODO: Document body
  617. (save-excursion
  618. (beginning-of-line)
  619. (let ((ruby-indent-point (point))
  620. (case-fold-search nil)
  621. state eol begin op-end
  622. (paren (progn (skip-syntax-forward " ")
  623. (and (char-after) (matching-paren (char-after)))))
  624. (indent 0))
  625. (if parse-start
  626. (goto-char parse-start)
  627. (ruby-beginning-of-indent)
  628. (setq parse-start (point)))
  629. (back-to-indentation)
  630. (setq indent (current-column))
  631. (setq state (ruby-parse-region parse-start ruby-indent-point))
  632. (cond
  633. ((nth 0 state) ; within string
  634. (setq indent nil)) ; do nothing
  635. ((car (nth 1 state)) ; in paren
  636. (goto-char (setq begin (cdr (nth 1 state))))
  637. (let ((deep (ruby-deep-indent-paren-p (car (nth 1 state)))))
  638. (if deep
  639. (cond ((and (eq deep t) (eq (car (nth 1 state)) paren))
  640. (skip-syntax-backward " ")
  641. (setq indent (1- (current-column))))
  642. ((let ((s (ruby-parse-region (point) ruby-indent-point)))
  643. (and (nth 2 s) (> (nth 2 s) 0)
  644. (or (goto-char (cdr (nth 1 s))) t)))
  645. (forward-word -1)
  646. (setq indent (ruby-indent-size (current-column)
  647. (nth 2 state))))
  648. (t
  649. (setq indent (current-column))
  650. (cond ((eq deep 'space))
  651. (paren (setq indent (1- indent)))
  652. (t (setq indent (ruby-indent-size (1- indent) 1))))))
  653. (if (nth 3 state) (goto-char (nth 3 state))
  654. (goto-char parse-start) (back-to-indentation))
  655. (setq indent (ruby-indent-size (current-column) (nth 2 state))))
  656. (and (eq (car (nth 1 state)) paren)
  657. (ruby-deep-indent-paren-p (matching-paren paren))
  658. (search-backward (char-to-string paren))
  659. (setq indent (current-column)))))
  660. ((and (nth 2 state) (> (nth 2 state) 0)) ; in nest
  661. (if (null (cdr (nth 1 state)))
  662. (error "invalid nest"))
  663. (goto-char (cdr (nth 1 state)))
  664. (forward-word -1) ; skip back a keyword
  665. (setq begin (point))
  666. (cond
  667. ((looking-at "do\\>[^_]") ; iter block is a special case
  668. (if (nth 3 state) (goto-char (nth 3 state))
  669. (goto-char parse-start) (back-to-indentation))
  670. (setq indent (ruby-indent-size (current-column) (nth 2 state))))
  671. (t
  672. (setq indent (+ (current-column) ruby-indent-level)))))
  673. ((and (nth 2 state) (< (nth 2 state) 0)) ; in negative nest
  674. (setq indent (ruby-indent-size (current-column) (nth 2 state)))))
  675. (when indent
  676. (goto-char ruby-indent-point)
  677. (end-of-line)
  678. (setq eol (point))
  679. (beginning-of-line)
  680. (cond
  681. ((and (not (ruby-deep-indent-paren-p paren))
  682. (re-search-forward ruby-negative eol t))
  683. (and (not (eq ?_ (char-after (match-end 0))))
  684. (setq indent (- indent ruby-indent-level))))
  685. ((and
  686. (save-excursion
  687. (beginning-of-line)
  688. (not (bobp)))
  689. (or (ruby-deep-indent-paren-p t)
  690. (null (car (nth 1 state)))))
  691. ;; goto beginning of non-empty no-comment line
  692. (let (end done)
  693. (while (not done)
  694. (skip-chars-backward " \t\n")
  695. (setq end (point))
  696. (beginning-of-line)
  697. (if (re-search-forward "^\\s *#" end t)
  698. (beginning-of-line)
  699. (setq done t))))
  700. (end-of-line)
  701. ;; skip the comment at the end
  702. (skip-chars-backward " \t")
  703. (let (end (pos (point)))
  704. (beginning-of-line)
  705. (while (and (re-search-forward "#" pos t)
  706. (setq end (1- (point)))
  707. (or (ruby-special-char-p end)
  708. (and (setq state (ruby-parse-region parse-start end))
  709. (nth 0 state))))
  710. (setq end nil))
  711. (goto-char (or end pos))
  712. (skip-chars-backward " \t")
  713. (setq begin (if (and end (nth 0 state)) pos (cdr (nth 1 state))))
  714. (setq state (ruby-parse-region parse-start (point))))
  715. (or (bobp) (forward-char -1))
  716. (and
  717. (or (and (looking-at ruby-symbol-re)
  718. (skip-chars-backward ruby-symbol-chars)
  719. (looking-at (concat "\\<\\(" ruby-block-hanging-re "\\)\\>"))
  720. (not (eq (point) (nth 3 state)))
  721. (save-excursion
  722. (goto-char (match-end 0))
  723. (not (looking-at "[a-z_]"))))
  724. (and (looking-at ruby-operator-re)
  725. (not (ruby-special-char-p))
  726. ;; operator at the end of line
  727. (let ((c (char-after (point))))
  728. (and
  729. ;; (or (null begin)
  730. ;; (save-excursion
  731. ;; (goto-char begin)
  732. ;; (skip-chars-forward " \t")
  733. ;; (not (or (eolp) (looking-at "#")
  734. ;; (and (eq (car (nth 1 state)) ?{)
  735. ;; (looking-at "|"))))))
  736. (or (not (eq ?/ c))
  737. (null (nth 0 (ruby-parse-region (or begin parse-start) (point)))))
  738. (or (not (eq ?| (char-after (point))))
  739. (save-excursion
  740. (or (eolp) (forward-char -1))
  741. (cond
  742. ((search-backward "|" nil t)
  743. (skip-chars-backward " \t\n")
  744. (and (not (eolp))
  745. (progn
  746. (forward-char -1)
  747. (not (looking-at "{")))
  748. (progn
  749. (forward-word -1)
  750. (not (looking-at "do\\>[^_]")))))
  751. (t t))))
  752. (not (eq ?, c))
  753. (setq op-end t)))))
  754. (setq indent
  755. (cond
  756. ((and
  757. (null op-end)
  758. (not (looking-at (concat "\\<\\(" ruby-block-hanging-re "\\)\\>")))
  759. (eq (ruby-deep-indent-paren-p t) 'space)
  760. (not (bobp)))
  761. (widen)
  762. (goto-char (or begin parse-start))
  763. (skip-syntax-forward " ")
  764. (current-column))
  765. ((car (nth 1 state)) indent)
  766. (t
  767. (+ indent ruby-indent-level))))))))
  768. (goto-char ruby-indent-point)
  769. (beginning-of-line)
  770. (skip-syntax-forward " ")
  771. (if (looking-at "\\.[^.]")
  772. (+ indent ruby-indent-level)
  773. indent))))
  774. (defun ruby-electric-brace (arg)
  775. "Insert a brace and re-indent the current line."
  776. (interactive "P")
  777. (self-insert-command (prefix-numeric-value arg))
  778. (ruby-indent-line t))
  779. ;; TODO: Why isn't one ruby-*-of-defun written in terms of the other?
  780. (defun ruby-beginning-of-defun (&optional arg)
  781. "Move backward to the beginning of the current top-level defun.
  782. With ARG, move backward multiple defuns. Negative ARG means
  783. move forward."
  784. (interactive "p")
  785. (and (re-search-backward (concat "^\\(" ruby-block-beg-re "\\)\\b")
  786. nil 'move (or arg 1))
  787. (beginning-of-line)))
  788. (defun ruby-end-of-defun (&optional arg)
  789. "Move forward to the end of the current top-level defun.
  790. With ARG, move forward multiple defuns. Negative ARG means
  791. move backward."
  792. (interactive "p")
  793. (and (re-search-forward (concat "^\\(" ruby-block-end-re "\\)\\($\\|\\b[^_]\\)")
  794. nil 'move (or arg 1))
  795. (beginning-of-line))
  796. (forward-line 1))
  797. (defun ruby-beginning-of-indent ()
  798. "TODO: document"
  799. ;; I don't understand this function.
  800. ;; It seems like it should move to the line where indentation should deepen,
  801. ;; but ruby-indent-beg-re only accounts for whitespace before class, module and def,
  802. ;; so this will only match other block beginners at the beginning of the line.
  803. (and (re-search-backward (concat "^\\(" ruby-indent-beg-re "\\)\\_>") nil 'move)
  804. (beginning-of-line)))
  805. (defun ruby-move-to-block (n)
  806. "Move to the beginning (N < 0) or the end (N > 0) of the current block
  807. or blocks containing the current block."
  808. ;; TODO: Make this work for n > 1,
  809. ;; make it not loop for n = 0,
  810. ;; document body
  811. (let (start pos done down)
  812. (setq start (ruby-calculate-indent))
  813. (setq down (looking-at (if (< n 0) ruby-block-end-re
  814. (concat "\\<\\(" ruby-block-beg-re "\\)\\>"))))
  815. (while (and (not done) (not (if (< n 0) (bobp) (eobp))))
  816. (forward-line n)
  817. (cond
  818. ((looking-at "^\\s *$"))
  819. ((looking-at "^\\s *#"))
  820. ((and (> n 0) (looking-at "^=begin\\>"))
  821. (re-search-forward "^=end\\>"))
  822. ((and (< n 0) (looking-at "^=end\\>"))
  823. (re-search-backward "^=begin\\>"))
  824. (t
  825. (setq pos (current-indentation))
  826. (cond
  827. ((< start pos)
  828. (setq down t))
  829. ((and down (= pos start))
  830. (setq done t))
  831. ((> start pos)
  832. (setq done t)))))
  833. (if done
  834. (save-excursion
  835. (back-to-indentation)
  836. (if (looking-at (concat "\\<\\(" ruby-block-mid-re "\\)\\>"))
  837. (setq done nil))))))
  838. (back-to-indentation))
  839. (defun ruby-beginning-of-block (&optional arg)
  840. "Move backward to the beginning of the current block.
  841. With ARG, move up multiple blocks."
  842. (interactive "p")
  843. (ruby-move-to-block (- (or arg 1))))
  844. (defun ruby-end-of-block (&optional arg)
  845. "Move forward to the end of the current block.
  846. With ARG, move out of multiple blocks."
  847. ;; Passing a value > 1 to ruby-move-to-block currently doesn't work.
  848. (interactive)
  849. (ruby-move-to-block (or arg 1)))
  850. (defun ruby-forward-sexp (&optional arg)
  851. "Move forward across one balanced expression (sexp).
  852. With ARG, do it many times. Negative ARG means move backward."
  853. ;; TODO: Document body
  854. (interactive "p")
  855. (if (and (numberp arg) (< arg 0))
  856. (ruby-backward-sexp (- arg))
  857. (let ((i (or arg 1)))
  858. (condition-case nil
  859. (while (> i 0)
  860. (skip-syntax-forward " ")
  861. (if (looking-at ",\\s *") (goto-char (match-end 0)))
  862. (cond ((looking-at "\\?\\(\\\\[CM]-\\)*\\\\?\\S ")
  863. (goto-char (match-end 0)))
  864. ((progn
  865. (skip-chars-forward ",.:;|&^~=!?\\+\\-\\*")
  866. (looking-at "\\s("))
  867. (goto-char (scan-sexps (point) 1)))
  868. ((and (looking-at (concat "\\<\\(" ruby-block-beg-re "\\)\\>"))
  869. (not (eq (char-before (point)) ?.))
  870. (not (eq (char-before (point)) ?:)))
  871. (ruby-end-of-block)
  872. (forward-word 1))
  873. ((looking-at "\\(\\$\\|@@?\\)?\\sw")
  874. (while (progn
  875. (while (progn (forward-word 1) (looking-at "_")))
  876. (cond ((looking-at "::") (forward-char 2) t)
  877. ((> (skip-chars-forward ".") 0))
  878. ((looking-at "\\?\\|!\\(=[~=>]\\|[^~=]\\)")
  879. (forward-char 1) nil)))))
  880. ((let (state expr)
  881. (while
  882. (progn
  883. (setq expr (or expr (ruby-expr-beg)
  884. (looking-at "%\\sw?\\Sw\\|[\"'`/]")))
  885. (nth 1 (setq state (apply 'ruby-parse-partial nil state))))
  886. (setq expr t)
  887. (skip-chars-forward "<"))
  888. (not expr))))
  889. (setq i (1- i)))
  890. ((error) (forward-word 1)))
  891. i)))
  892. (defun ruby-backward-sexp (&optional arg)
  893. "Move backward across one balanced expression (sexp).
  894. With ARG, do it many times. Negative ARG means move forward."
  895. ;; TODO: Document body
  896. (interactive "p")
  897. (if (and (numberp arg) (< arg 0))
  898. (ruby-forward-sexp (- arg))
  899. (let ((i (or arg 1)))
  900. (condition-case nil
  901. (while (> i 0)
  902. (skip-chars-backward " \t\n,.:;|&^~=!?\\+\\-\\*")
  903. (forward-char -1)
  904. (cond ((looking-at "\\s)")
  905. (goto-char (scan-sexps (1+ (point)) -1))
  906. (case (char-before)
  907. (?% (forward-char -1))
  908. ((?q ?Q ?w ?W ?r ?x)
  909. (if (eq (char-before (1- (point))) ?%) (forward-char -2))))
  910. nil)
  911. ((looking-at "\\s\"\\|\\\\\\S_")
  912. (let ((c (char-to-string (char-before (match-end 0)))))
  913. (while (and (search-backward c)
  914. (eq (logand (skip-chars-backward "\\") 1)
  915. 1))))
  916. nil)
  917. ((looking-at "\\s.\\|\\s\\")
  918. (if (ruby-special-char-p) (forward-char -1)))
  919. ((looking-at "\\s(") nil)
  920. (t
  921. (forward-char 1)
  922. (while (progn (forward-word -1)
  923. (case (char-before)
  924. (?_ t)
  925. (?. (forward-char -1) t)
  926. ((?$ ?@)
  927. (forward-char -1)
  928. (and (eq (char-before) (char-after)) (forward-char -1)))
  929. (?:
  930. (forward-char -1)
  931. (eq (char-before) :)))))
  932. (if (looking-at ruby-block-end-re)
  933. (ruby-beginning-of-block))
  934. nil))
  935. (setq i (1- i)))
  936. ((error)))
  937. i)))
  938. (defun ruby-mark-defun ()
  939. "Put mark at end of this Ruby function, point at beginning."
  940. (interactive)
  941. (push-mark (point))
  942. (ruby-end-of-defun)
  943. (push-mark (point) nil t)
  944. (ruby-beginning-of-defun)
  945. (re-search-backward "^\n" (- (point) 1) t))
  946. (defun ruby-indent-exp (&optional ignored)
  947. "Indent each line in the balanced expression following the point."
  948. (interactive "*P")
  949. (let ((here (point-marker)) start top column (nest t))
  950. (set-marker-insertion-type here t)
  951. (unwind-protect
  952. (progn
  953. (beginning-of-line)
  954. (setq start (point) top (current-indentation))
  955. (while (and (not (eobp))
  956. (progn
  957. (setq column (ruby-calculate-indent start))
  958. (cond ((> column top)
  959. (setq nest t))
  960. ((and (= column top) nest)
  961. (setq nest nil) t))))
  962. (ruby-indent-to column)
  963. (beginning-of-line 2)))
  964. (goto-char here)
  965. (set-marker here nil))))
  966. (defun ruby-add-log-current-method ()
  967. "Return the current method name as a string.
  968. This string includes all namespaces.
  969. For example:
  970. #exit
  971. String#gsub
  972. Net::HTTP#active?
  973. File::open.
  974. See `add-log-current-defun-function'."
  975. ;; TODO: Document body
  976. ;; Why does this append a period to class methods?
  977. (condition-case nil
  978. (save-excursion
  979. (let (mname mlist (indent 0))
  980. ;; get current method (or class/module)
  981. (if (re-search-backward
  982. (concat "^[ \t]*\\(def\\|class\\|module\\)[ \t]+"
  983. "\\("
  984. ;; \\. and :: for class method
  985. "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)"
  986. "+\\)")
  987. nil t)
  988. (progn
  989. (setq mname (match-string 2))
  990. (unless (string-equal "def" (match-string 1))
  991. (setq mlist (list mname) mname nil))
  992. (goto-char (match-beginning 1))
  993. (setq indent (current-column))
  994. (beginning-of-line)))
  995. ;; nest class/module
  996. (while (and (> indent 0)
  997. (re-search-backward
  998. (concat
  999. "^[ \t]*\\(class\\|module\\)[ \t]+"
  1000. "\\([A-Z]" ruby-symbol-re "*\\)")
  1001. nil t))
  1002. (goto-char (match-beginning 1))
  1003. (if (< (current-column) indent)
  1004. (progn
  1005. (setq mlist (cons (match-string 2) mlist))
  1006. (setq indent (current-column))
  1007. (beginning-of-line))))
  1008. (when mname
  1009. (let ((mn (split-string mname "\\.\\|::")))
  1010. (if (cdr mn)
  1011. (progn
  1012. (cond
  1013. ((string-equal "" (car mn))
  1014. (setq mn (cdr mn) mlist nil))
  1015. ((string-equal "self" (car mn))
  1016. (setq mn (cdr mn)))
  1017. ((let ((ml (nreverse mlist)))
  1018. (while ml
  1019. (if (string-equal (car ml) (car mn))
  1020. (setq mlist (nreverse (cdr ml)) ml nil))
  1021. (or (setq ml (cdr ml)) (nreverse mlist))))))
  1022. (if mlist
  1023. (setcdr (last mlist) mn)
  1024. (setq mlist mn))
  1025. (setq mn (last mn 2))
  1026. (setq mname (concat "." (cadr mn)))
  1027. (setcdr mn nil))
  1028. (setq mname (concat "#" mname)))))
  1029. ;; generate string
  1030. (if (consp mlist)
  1031. (setq mlist (mapconcat (function identity) mlist "::")))
  1032. (if mname
  1033. (if mlist (concat mlist mname) mname)
  1034. mlist)))))
  1035. (declare-function ruby-syntax-propertize-heredoc "ruby-mode" (limit))
  1036. (if (eval-when-compile (fboundp #'syntax-propertize-rules))
  1037. ;; New code that works independently from font-lock.
  1038. (progn
  1039. (defun ruby-syntax-propertize-function (start end)
  1040. "Syntactic keywords for Ruby mode. See `syntax-propertize-function'."
  1041. (goto-char start)
  1042. (ruby-syntax-propertize-heredoc end)
  1043. (funcall
  1044. (syntax-propertize-rules
  1045. ;; #{ }, #$hoge, #@foo are not comments
  1046. ("\\(#\\)[{$@]" (1 "."))
  1047. ;; $' $" $` .... are variables
  1048. ;; ?' ?" ?` are ascii codes
  1049. ("\\([?$]\\)[#\"'`]"
  1050. (1 (unless (save-excursion
  1051. ;; Not within a string.
  1052. (nth 3 (syntax-ppss (match-beginning 0))))
  1053. (string-to-syntax "\\"))))
  1054. ;; regexps
  1055. ("\\(^\\|[[=(,~?:;<>]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
  1056. (4 "\"/")
  1057. (6 "\"/"))
  1058. ("^=en\\(d\\)\\_>" (1 "!"))
  1059. ("^\\(=\\)begin\\_>" (1 "!"))
  1060. ;; Handle here documents.
  1061. ((concat ruby-here-doc-beg-re ".*\\(\n\\)")
  1062. (7 (prog1 "\"" (ruby-syntax-propertize-heredoc end)))))
  1063. (point) end))
  1064. (defun ruby-syntax-propertize-heredoc (limit)
  1065. (let ((ppss (syntax-ppss))
  1066. (res '()))
  1067. (when (eq ?\n (nth 3 ppss))
  1068. (save-excursion
  1069. (goto-char (nth 8 ppss))
  1070. (beginning-of-line)
  1071. (while (re-search-forward ruby-here-doc-beg-re
  1072. (line-end-position) t)
  1073. (push (concat (ruby-here-doc-end-match) "\n") res)))
  1074. (let ((start (point)))
  1075. ;; With multiple openers on the same line, we don't know in which
  1076. ;; part `start' is, so we have to go back to the beginning.
  1077. (when (cdr res)
  1078. (goto-char (nth 8 ppss))
  1079. (setq res (nreverse res)))
  1080. (while (and res (re-search-forward (pop res) limit 'move))
  1081. (if (null res)
  1082. (put-text-property (1- (point)) (point)
  1083. 'syntax-table (string-to-syntax "\""))))
  1084. ;; Make extra sure we don't move back, lest we could fall into an
  1085. ;; inf-loop.
  1086. (if (< (point) start) (goto-char start))))))
  1087. )
  1088. ;; For Emacsen where syntax-propertize-rules is not (yet) available,
  1089. ;; fallback on the old font-lock-syntactic-keywords stuff.
  1090. (defconst ruby-here-doc-end-re
  1091. "^\\([ \t]+\\)?\\(.*\\)\\(\n\\)"
  1092. "Regexp to match the end of heredocs.
  1093. This will actually match any line with one or more characters.
  1094. It's useful in that it divides up the match string so that
  1095. `ruby-here-doc-beg-match' can search for the beginning of the heredoc.")
  1096. (defun ruby-here-doc-beg-match ()
  1097. "Return a regexp to find the beginning of a heredoc.
  1098. This should only be called after matching against `ruby-here-doc-end-re'."
  1099. (let ((contents (concat
  1100. (regexp-quote (concat (match-string 2) (match-string 3)))
  1101. (if (string= (match-string 3) "_") "\\B" "\\b"))))
  1102. (concat "<<"
  1103. (let ((match (match-string 1)))
  1104. (if (and match (> (length match) 0))
  1105. (concat "\\(?:-\\([\"']?\\)\\|\\([\"']\\)"
  1106. (match-string 1) "\\)"
  1107. contents "\\(\\1\\|\\2\\)")
  1108. (concat "-?\\([\"']\\|\\)" contents "\\1"))))))
  1109. (defconst ruby-font-lock-syntactic-keywords
  1110. `( ;; #{ }, #$hoge, #@foo are not comments
  1111. ("\\(#\\)[{$@]" 1 (1 . nil))
  1112. ;; the last $', $", $` in the respective string is not variable
  1113. ;; the last ?', ?", ?` in the respective string is not ascii code
  1114. ("\\(^\\|[\[ \t\n<+\(,=]\\)\\(['\"`]\\)\\(\\\\.\\|\\2\\|[^'\"`\n\\\\]\\)*?\\\\?[?$]\\(\\2\\)"
  1115. (2 (7 . nil))
  1116. (4 (7 . nil)))
  1117. ;; $' $" $` .... are variables
  1118. ;; ?' ?" ?` are ascii codes
  1119. ("\\(^\\|[^\\\\]\\)\\(\\\\\\\\\\)*[?$]\\([#\"'`]\\)" 3 (1 . nil))
  1120. ;; regexps
  1121. ("\\(^\\|[[=(,~?:;<>]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
  1122. (4 (7 . ?/))
  1123. (6 (7 . ?/)))
  1124. ("^=en\\(d\\)\\_>" 1 "!")
  1125. ("^\\(=\\)begin\\_>" 1 (ruby-comment-beg-syntax))
  1126. ;; Currently, the following case is highlighted incorrectly:
  1127. ;;
  1128. ;; <<FOO
  1129. ;; FOO
  1130. ;; <<BAR
  1131. ;; <<BAZ
  1132. ;; BAZ
  1133. ;; BAR
  1134. ;;
  1135. ;; This is because all here-doc beginnings are highlighted before any endings,
  1136. ;; so although <<BAR is properly marked as a beginning, when we get to <<BAZ
  1137. ;; it thinks <<BAR is part of a string so it's marked as well.
  1138. ;;
  1139. ;; This may be fixable by modifying ruby-in-here-doc-p to use
  1140. ;; ruby-in-non-here-doc-string-p rather than syntax-ppss-context,
  1141. ;; but I don't want to try that until we've got unit tests set up
  1142. ;; to make sure I don't break anything else.
  1143. (,(concat ruby-here-doc-beg-re ".*\\(\n\\)")
  1144. ,(+ 1 (regexp-opt-depth ruby-here-doc-beg-re))
  1145. (ruby-here-doc-beg-syntax))
  1146. (,ruby-here-doc-end-re 3 (ruby-here-doc-end-syntax)))
  1147. "Syntactic keywords for Ruby mode. See `font-lock-syntactic-keywords'.")
  1148. (defun ruby-comment-beg-syntax ()
  1149. "Return the syntax cell for a the first character of a =begin.
  1150. See the definition of `ruby-font-lock-syntactic-keywords'.
  1151. This returns a comment-delimiter cell as long as the =begin
  1152. isn't in a string or another comment."
  1153. (when (not (nth 3 (syntax-ppss)))
  1154. (string-to-syntax "!")))
  1155. (defun ruby-in-here-doc-p ()
  1156. "Return whether or not the point is in a heredoc."
  1157. (save-excursion
  1158. (let ((old-point (point)) (case-fold-search nil))
  1159. (beginning-of-line)
  1160. (catch 'found-beg
  1161. (while (re-search-backward ruby-here-doc-beg-re nil t)
  1162. (if (not (or (ruby-in-ppss-context-p 'anything)
  1163. (ruby-here-doc-find-end old-point)))
  1164. (throw 'found-beg t)))))))
  1165. (defun ruby-here-doc-find-end (&optional limit)
  1166. "Expects the point to be on a line with one or more heredoc openers.
  1167. Returns the buffer position at which all heredocs on the line
  1168. are terminated, or nil if they aren't terminated before the
  1169. buffer position `limit' or the end of the buffer."
  1170. (save-excursion
  1171. (beginning-of-line)
  1172. (catch 'done
  1173. (let ((eol (point-at-eol))
  1174. (case-fold-search nil)
  1175. ;; Fake match data such that (match-end 0) is at eol
  1176. (end-match-data (progn (looking-at ".*$") (match-data)))
  1177. beg-match-data end-re)
  1178. (while (re-search-forward ruby-here-doc-beg-re eol t)
  1179. (setq beg-match-data (match-data))
  1180. (setq end-re (ruby-here-doc-end-match))
  1181. (set-match-data end-match-data)
  1182. (goto-char (match-end 0))
  1183. (unless (re-search-forward end-re limit t) (throw 'done nil))
  1184. (setq end-match-data (match-data))
  1185. (set-match-data beg-match-data)
  1186. (goto-char (match-end 0)))
  1187. (set-match-data end-match-data)
  1188. (goto-char (match-end 0))
  1189. (point)))))
  1190. (defun ruby-here-doc-beg-syntax ()
  1191. "Return the syntax cell for a line that may begin a heredoc.
  1192. See the definition of `ruby-font-lock-syntactic-keywords'.
  1193. This sets the syntax cell for the newline ending the line
  1194. containing the heredoc beginning so that cases where multiple
  1195. heredocs are started on one line are handled correctly."
  1196. (save-excursion
  1197. (goto-char (match-beginning 0))
  1198. (unless (or (ruby-in-ppss-context-p 'non-heredoc)
  1199. (ruby-in-here-doc-p))
  1200. (string-to-syntax "\""))))
  1201. (defun ruby-here-doc-end-syntax ()
  1202. "Return the syntax cell for a line that may end a heredoc.
  1203. See the definition of `ruby-font-lock-syntactic-keywords'."
  1204. (let ((pss (syntax-ppss)) (case-fold-search nil))
  1205. ;; If we aren't in a string, we definitely aren't ending a heredoc,
  1206. ;; so we can just give up.
  1207. ;; This means we aren't doing a full-document search
  1208. ;; every time we enter a character.
  1209. (when (ruby-in-ppss-context-p 'heredoc pss)
  1210. (save-excursion
  1211. (goto-char (nth 8 pss)) ; Go to the beginning of heredoc.
  1212. (let ((eol (point)))
  1213. (beginning-of-line)
  1214. (if (and (re-search-forward (ruby-here-doc-beg-match) eol t) ; If there is a heredoc that matches this line...
  1215. (not (ruby-in-ppss-context-p 'anything)) ; And that's not inside a heredoc/string/comment...
  1216. (progn (goto-char (match-end 0)) ; And it's the last heredoc on its line...
  1217. (not (re-search-forward ruby-here-doc-beg-re eol t))))
  1218. (string-to-syntax "\"")))))))
  1219. (unless (functionp 'syntax-ppss)
  1220. (defun syntax-ppss (&optional pos)
  1221. (parse-partial-sexp (point-min) (or pos (point)))))
  1222. )
  1223. (defun ruby-in-ppss-context-p (context &optional ppss)
  1224. (let ((ppss (or ppss (syntax-ppss (point)))))
  1225. (if (cond
  1226. ((eq context 'anything)
  1227. (or (nth 3 ppss)
  1228. (nth 4 ppss)))
  1229. ((eq context 'string)
  1230. (nth 3 ppss))
  1231. ((eq context 'heredoc)
  1232. (eq ?\n (nth 3 ppss)))
  1233. ((eq context 'non-heredoc)
  1234. (and (ruby-in-ppss-context-p 'anything)
  1235. (not (ruby-in-ppss-context-p 'heredoc))))
  1236. ((eq context 'comment)
  1237. (nth 4 ppss))
  1238. (t
  1239. (error (concat
  1240. "Internal error on `ruby-in-ppss-context-p': "
  1241. "context name `" (symbol-name context) "' is unknown"))))
  1242. t)))
  1243. (if (featurep 'xemacs)
  1244. (put 'ruby-mode 'font-lock-defaults
  1245. '((ruby-font-lock-keywords)
  1246. nil nil nil
  1247. beginning-of-line
  1248. (font-lock-syntactic-keywords
  1249. . ruby-font-lock-syntactic-keywords))))
  1250. (defvar ruby-font-lock-syntax-table
  1251. (let ((tbl (copy-syntax-table ruby-mode-syntax-table)))
  1252. (modify-syntax-entry ?_ "w" tbl)
  1253. tbl)
  1254. "The syntax table to use for fontifying Ruby mode buffers.
  1255. See `font-lock-syntax-table'.")
  1256. (defconst ruby-font-lock-keywords
  1257. (list
  1258. ;; functions
  1259. '("^\\s *def\\s +\\([^( \t\n]+\\)"
  1260. 1 font-lock-function-name-face)
  1261. ;; keywords
  1262. (cons (concat
  1263. "\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(defined\\?\\|"
  1264. (regexp-opt
  1265. '("alias_method"
  1266. "alias"
  1267. "and"
  1268. "begin"
  1269. "break"
  1270. "case"
  1271. "catch"
  1272. "class"
  1273. "def"
  1274. "do"
  1275. "elsif"
  1276. "else"
  1277. "fail"
  1278. "ensure"
  1279. "for"
  1280. "end"
  1281. "if"
  1282. "in"
  1283. "module_function"
  1284. "module"
  1285. "next"
  1286. "not"
  1287. "or"
  1288. "public"
  1289. "private"
  1290. "protected"
  1291. "raise"
  1292. "redo"
  1293. "rescue"
  1294. "retry"
  1295. "return"
  1296. "then"
  1297. "throw"
  1298. "super"
  1299. "unless"
  1300. "undef"
  1301. "until"
  1302. "when"
  1303. "while"
  1304. "yield")
  1305. t)
  1306. "\\)"
  1307. ruby-keyword-end-re)
  1308. 2)
  1309. ;; here-doc beginnings
  1310. (list ruby-here-doc-beg-re 0 'font-lock-string-face)
  1311. ;; variables
  1312. '("\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(nil\\|self\\|true\\|false\\)\\>"
  1313. 2 font-lock-variable-name-face)
  1314. ;; variables
  1315. '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
  1316. 1 font-lock-variable-name-face)
  1317. '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
  1318. 0 font-lock-variable-name-face)
  1319. ;; general delimited string
  1320. '("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
  1321. (2 font-lock-string-face))
  1322. ;; constants
  1323. '("\\(^\\|[^_]\\)\\b\\([A-Z]+\\(\\w\\|_\\)*\\)"
  1324. 2 font-lock-type-face)
  1325. ;; symbols
  1326. '("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
  1327. 2 font-lock-reference-face)
  1328. '("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]" 2 font-lock-reference-face)
  1329. ;; expression expansion
  1330. '("#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)"
  1331. 0 font-lock-variable-name-face t)
  1332. ;; warn lower camel case
  1333. ;'("\\<[a-z]+[a-z0-9]*[A-Z][A-Za-z0-9]*\\([!?]?\\|\\>\\)"
  1334. ; 0 font-lock-warning-face)
  1335. )
  1336. "Additional expressions to highlight in Ruby mode.")
  1337. ;;;###autoload
  1338. (define-derived-mode ruby-mode prog-mode "Ruby"
  1339. "Major mode for editing Ruby scripts.
  1340. \\[ruby-indent-line] properly indents subexpressions of multi-line
  1341. class, module, def, if, while, for, do, and case statements, taking
  1342. nesting into account.
  1343. The variable `ruby-indent-level' controls the amount of indentation.
  1344. \\{ruby-mode-map}"
  1345. (ruby-mode-variables)
  1346. (set (make-local-variable 'imenu-create-index-function)
  1347. 'ruby-imenu-create-index)
  1348. (set (make-local-variable 'add-log-current-defun-function)
  1349. 'ruby-add-log-current-method)
  1350. (add-hook
  1351. (cond ((boundp 'before-save-hook) 'before-save-hook)
  1352. ((boundp 'write-contents-functions) 'write-contents-functions)
  1353. ((boundp 'write-contents-hooks) 'write-contents-hooks))
  1354. 'ruby-mode-set-encoding nil 'local)
  1355. (set (make-local-variable 'electric-indent-chars)
  1356. (append '(?\{ ?\}) electric-indent-chars))
  1357. (set (make-local-variable 'font-lock-defaults)
  1358. '((ruby-font-lock-keywords) nil nil))
  1359. (set (make-local-variable 'font-lock-keywords)
  1360. ruby-font-lock-keywords)
  1361. (set (make-local-variable 'font-lock-syntax-table)
  1362. ruby-font-lock-syntax-table)
  1363. (if (eval-when-compile (fboundp 'syntax-propertize-rules))
  1364. (set (make-local-variable 'syntax-propertize-function)
  1365. #'ruby-syntax-propertize-function)
  1366. (set (make-local-variable 'font-lock-syntactic-keywords)
  1367. ruby-font-lock-syntactic-keywords)))
  1368. ;;; Invoke ruby-mode when appropriate
  1369. ;;;###autoload
  1370. (add-to-list 'auto-mode-alist (cons (purecopy "\\.rb\\'") 'ruby-mode))
  1371. ;;;###autoload
  1372. (dolist (name (list "ruby" "rbx" "jruby" "ruby1.9" "ruby1.8"))
  1373. (add-to-list 'interpreter-mode-alist (cons (purecopy name) 'ruby-mode)))
  1374. (provide 'ruby-mode)
  1375. ;;; ruby-mode.el ends here