cap-words.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ;;; cap-words.el --- minor mode for motion in CapitalizedWordIdentifiers
  2. ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
  3. ;; Author: Dave Love <fx@gnu.org>
  4. ;; Keywords: languages
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Provides Capitalized Words minor mode for word movement in
  18. ;; identifiers CapitalizedLikeThis.
  19. ;; Note that the same effect could be obtained by frobbing the
  20. ;; category of upper case characters to produce word boundaries, but
  21. ;; the necessary processing isn't done for ASCII characters.
  22. ;; Fixme: This doesn't work properly for mouse double clicks.
  23. ;;; Code:
  24. (defun capitalized-find-word-boundary (pos limit)
  25. "Function for use in `find-word-boundary-function-table'.
  26. Looks for word boundaries before capitals."
  27. (save-excursion
  28. (goto-char pos)
  29. (let (case-fold-search)
  30. (if (<= pos limit)
  31. ;; Fixme: Are these regexps the best?
  32. (or (and (re-search-forward "\\=.\\w*[[:upper:]]"
  33. limit t)
  34. (progn (backward-char)
  35. t))
  36. (re-search-forward "\\>" limit t))
  37. (or (re-search-backward "[[:upper:]]\\w*\\=" limit t)
  38. (re-search-backward "\\<" limit t))))
  39. (point)))
  40. (defconst capitalized-find-word-boundary-function-table
  41. (let ((tab (make-char-table nil)))
  42. (set-char-table-range tab t #'capitalized-find-word-boundary)
  43. tab)
  44. "Assigned to `find-word-boundary-function-table' in Capitalized Words mode.")
  45. ;;;###autoload
  46. (define-minor-mode capitalized-words-mode
  47. "Toggle Capitalized Words mode.
  48. With a prefix argument ARG, enable Capitalized Words mode if ARG
  49. is positive, and disable it otherwise. If called from Lisp,
  50. enable the mode if ARG is omitted or nil.
  51. Capitalized Words mode is a buffer-local minor mode. When
  52. enabled, a word boundary occurs immediately before an uppercase
  53. letter in a symbol. This is in addition to all the normal
  54. boundaries given by the syntax and category tables. There is no
  55. restriction to ASCII.
  56. E.g. the beginning of words in the following identifier are as marked:
  57. capitalizedWorDD
  58. ^ ^ ^^
  59. Note that these word boundaries only apply for word motion and
  60. marking commands such as \\[forward-word]. This mode does not affect word
  61. boundaries found by regexp matching (`\\>', `\\w' &c).
  62. This style of identifiers is common in environments like Java ones,
  63. where underscores aren't trendy enough. Capitalization rules are
  64. sometimes part of the language, e.g. Haskell, which may thus encourage
  65. such a style. It is appropriate to add `capitalized-words-mode' to
  66. the mode hook for programming language modes in which you encounter
  67. variables like this, e.g. `java-mode-hook'. It's unlikely to cause
  68. trouble if such identifiers aren't used.
  69. See also `glasses-mode' and `studlify-word'.
  70. Obsoletes `c-forward-into-nomenclature'."
  71. nil " Caps" nil :group 'programming
  72. (set (make-local-variable 'find-word-boundary-function-table)
  73. capitalized-find-word-boundary-function-table))
  74. (provide 'cap-words)
  75. ;;; cap-words.el ends here