Emacs config utilizing prelude as a base
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.0 KiB

  1. ;;; ruby-end.el --- Automatic insertion of end blocks for Ruby
  2. ;; Copyright (C) 2010 Johan Andersson
  3. ;; Author: Johan Andersson <johan.rejeep@gmail.com>
  4. ;; Maintainer: Johan Andersson <johan.rejeep@gmail.com>
  5. ;; Version: 0.1.0
  6. ;; Keywords: speed, convenience
  7. ;; URL: http://github.com/rejeep/ruby-end
  8. ;; This file is NOT part of GNU Emacs.
  9. ;;; License:
  10. ;; This program 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, or (at your option)
  13. ;; any later version.
  14. ;; This program 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; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; ruby-end is a minor mode for Emacs that can be used with ruby-mode
  24. ;; to automatically close blocks by inserting "end" when typing a
  25. ;; block-keyword, followed by a space.
  26. ;;
  27. ;; To use ruby-end-mode, make sure that this file is in Emacs load-path:
  28. ;; (add-to-list 'load-path "/path/to/directory/or/file")
  29. ;;
  30. ;; Then require ruby-end:
  31. ;; (require 'ruby-end)
  32. ;;
  33. ;; ruby-end-mode is automatically started in ruby-mode.
  34. ;;; Code:
  35. (defvar ruby-end-expand-key "SPC"
  36. "Space key name.")
  37. (defvar ruby-end-mode-map
  38. (let ((map (make-sparse-keymap))
  39. (key (read-kbd-macro ruby-end-expand-key)))
  40. (define-key map key 'ruby-end-space)
  41. map)
  42. "Keymap for `ruby-end-mode'.")
  43. (defcustom ruby-end-check-statement-modifiers t
  44. "*Disable or enable expansion (insertion of end) for statement modifiers"
  45. :type 'boolean
  46. :group 'ruby)
  47. (defconst ruby-end-expand-postfix-modifiers-before-re
  48. "\\(?:if\\|unless\\|while\\)"
  49. "Regular expression matching statements before point.")
  50. (defconst ruby-end-expand-prefix-check-modifiers-re
  51. "^\\s-*"
  52. "Prefix for regular expression to prevent expansion with statement modifiers")
  53. (defconst ruby-end-expand-prefix-re
  54. "\\(?:^\\|\\s-+\\)"
  55. "Prefix for regular expression")
  56. (defconst ruby-end-expand-keywords-before-re
  57. "\\(?:^\\|\\s-+\\)\\(?:do\\|def\\|class\\|module\\|case\\|for\\|begin\\)"
  58. "Regular expression matching blocks before point.")
  59. (defconst ruby-end-expand-after-re
  60. "\\s-*$"
  61. "Regular expression matching after point.")
  62. (defun ruby-end-space ()
  63. "Called when SPC-key is pressed."
  64. (interactive)
  65. (cond
  66. ((ruby-end-expand-p)
  67. (ruby-end-insert-end)
  68. (insert " "))
  69. (t
  70. (let ((ruby-end-mode nil))
  71. (call-interactively
  72. (key-binding
  73. (read-kbd-macro ruby-end-expand-key)))))))
  74. (defun ruby-end-insert-end ()
  75. "Closes block by inserting end."
  76. (save-excursion
  77. (newline)
  78. (insert "end")
  79. (indent-according-to-mode)))
  80. (defun ruby-end-expand-p ()
  81. "Checks if expansion (insertion of end) should be done."
  82. (let ((ruby-end-expand-statement-modifiers-before-re
  83. (concat
  84. (if ruby-end-check-statement-modifiers
  85. ruby-end-expand-prefix-check-modifiers-re
  86. ruby-end-expand-prefix-re)
  87. ruby-end-expand-postfix-modifiers-before-re)))
  88. (and
  89. (ruby-end-code-at-point-p)
  90. (or
  91. (looking-back ruby-end-expand-statement-modifiers-before-re)
  92. (looking-back ruby-end-expand-keywords-before-re))
  93. (looking-at ruby-end-expand-after-re))))
  94. (defun ruby-end-code-at-point-p ()
  95. "Checks if point is code, or comment or string."
  96. (let ((properties (text-properties-at (point))))
  97. (and
  98. (null (memq 'font-lock-string-face properties))
  99. (null (memq 'font-lock-comment-face properties)))))
  100. ;;;###autoload
  101. (define-minor-mode ruby-end-mode
  102. "Automatic insertion of end blocks for Ruby."
  103. :init-value nil
  104. :lighter " end"
  105. :keymap ruby-end-mode-map)
  106. (add-hook 'ruby-mode-hook 'ruby-end-mode)
  107. (provide 'ruby-end)
  108. ;;; ruby-end.el ends here