glasses.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. ;;; glasses.el --- make cantReadThis readable
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Milan Zamazal <pdm@zamazal.org>
  4. ;; Maintainer: Milan Zamazal <pdm@zamazal.org>
  5. ;; Keywords: tools
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This file defines a minor mode for making unreadableIdentifiersLikeThis
  19. ;; readable. In some environments, for instance Java, it is common to use such
  20. ;; unreadable identifiers. It is not good to use underscores in identifiers of
  21. ;; your own project in such an environment to make your sources more readable,
  22. ;; since it introduces undesirable confusion, which is worse than the
  23. ;; unreadability. Fortunately, you use Emacs for the subproject, so the
  24. ;; problem can be solved some way.
  25. ;;
  26. ;; This file defines the `glasses-mode' minor mode, which displays underscores
  27. ;; between all the pairs of lower and upper English letters. (This only
  28. ;; displays underscores, the text is not changed actually.) Alternatively, you
  29. ;; can say you want the capitals in some given face (e.g. bold).
  30. ;;
  31. ;; The mode does something usable, though not perfect. Improvement suggestions
  32. ;; from Emacs experts are welcome.
  33. ;;
  34. ;; If you like in-identifier separators different from underscores, change the
  35. ;; value of the variable `glasses-separator' appropriately. See also the
  36. ;; variables `glasses-face' and `glasses-convert-on-write-p'. You can also use
  37. ;; the command `M-x customize-group RET glasses RET'.
  38. ;;
  39. ;; If you set any of the variables `glasses-separator' or `glasses-face' after
  40. ;; glasses.el is loaded in a different way than through customize, you
  41. ;; should call the function `glasses-set-overlay-properties' afterwards.
  42. ;;; Code:
  43. (eval-when-compile
  44. (require 'cl))
  45. ;;; User variables
  46. (defgroup glasses nil
  47. "Make unreadable code likeThis(one) readable."
  48. :version "21.1"
  49. :group 'tools)
  50. (defcustom glasses-separator "_"
  51. "String to be displayed as a visual separator in identifiers.
  52. It is used both for adding missing separators and for replacing separators
  53. defined by `glasses-original-separator'. If you don't want to add missing
  54. separators, set `glasses-separator' to an empty string. If you don't want to
  55. replace existent separators, set `glasses-original-separator' to an empty
  56. string."
  57. :group 'glasses
  58. :type 'string
  59. :set 'glasses-custom-set
  60. :initialize 'custom-initialize-default)
  61. (defcustom glasses-original-separator "_"
  62. "*String to be displayed as `glasses-separator' in separator positions.
  63. For instance, if you set it to \"_\" and set `glasses-separator' to \"-\",
  64. underscore separators are displayed as hyphens.
  65. If `glasses-original-separator' is an empty string, no such display change is
  66. performed."
  67. :group 'glasses
  68. :type 'string
  69. :set 'glasses-custom-set
  70. :initialize 'custom-initialize-default
  71. :version "22.1")
  72. (defcustom glasses-face nil
  73. "Face to be put on capitals of an identifier looked through glasses.
  74. If it is nil, no face is placed at the capitalized letter.
  75. For example, you can set `glasses-separator' to an empty string and
  76. `glasses-face' to `bold'. Then unreadable identifiers will have no separators,
  77. but will have their capitals in bold."
  78. :group 'glasses
  79. :type '(choice (const :tag "None" nil) face)
  80. :set 'glasses-custom-set
  81. :initialize 'custom-initialize-default)
  82. (defcustom glasses-separate-parentheses-p t
  83. "If non-nil, ensure space between an identifier and an opening parenthesis."
  84. :group 'glasses
  85. :type 'boolean)
  86. (defcustom glasses-separate-parentheses-exceptions
  87. '("^#[\t ]*define[\t ]*[A-Za-z0-9_-]* ?($")
  88. "List of regexp that are exceptions for `glasses-separate-parentheses-p'.
  89. They are matched to the current line truncated to the point where the
  90. parenthesis expression starts."
  91. :group 'glasses
  92. :type '(repeat regexp))
  93. (defcustom glasses-separate-capital-groups t
  94. "If non-nil, try to separate groups of capital letters.
  95. When the value is non-nil, HTMLSomething and IPv6 are displayed
  96. as HTML_Something and I_Pv6 respectively. Set the value to nil
  97. if you prefer to display them unchanged."
  98. :group 'glasses
  99. :type 'boolean
  100. :version "24.1")
  101. (defcustom glasses-uncapitalize-p nil
  102. "If non-nil, downcase embedded capital letters in identifiers.
  103. Only identifiers starting with lower case letters are affected, letters inside
  104. other identifiers are unchanged."
  105. :group 'glasses
  106. :type 'boolean
  107. :set 'glasses-custom-set
  108. :initialize 'custom-initialize-default)
  109. (defcustom glasses-uncapitalize-regexp "[a-z]"
  110. "Regexp matching beginnings of words to be uncapitalized.
  111. Only words starting with this regexp are uncapitalized.
  112. The regexp is case sensitive.
  113. It has any effect only when `glasses-uncapitalize-p' is non-nil."
  114. :group 'glasses
  115. :type 'regexp
  116. :set 'glasses-custom-set
  117. :initialize 'custom-initialize-default)
  118. (defcustom glasses-convert-on-write-p nil
  119. "If non-nil, remove separators when writing glasses buffer to a file.
  120. If you are confused by glasses so much, that you write the separators into code
  121. during coding, set this variable to t. The separators will be removed on each
  122. file write then.
  123. Note the removal action does not try to be much clever, so it can remove real
  124. separators too."
  125. :group 'glasses
  126. :type 'boolean)
  127. (defun glasses-custom-set (symbol value)
  128. "Set value of the variable SYMBOL to VALUE and update overlay categories.
  129. Used in :set parameter of some customized glasses variables."
  130. (set-default symbol value)
  131. (glasses-set-overlay-properties))
  132. ;;; Utility functions
  133. (defun glasses-parenthesis-exception-p (beg end)
  134. "Tell if (BEG, END) is an exception to `glasses-separate-parentheses-p'.
  135. See `glasses-separate-parentheses-exceptions'."
  136. (save-match-data
  137. (let ((str (buffer-substring beg end)))
  138. (catch 'match
  139. (dolist (re glasses-separate-parentheses-exceptions)
  140. (and (string-match re str) (throw 'match t)))))))
  141. (defun glasses-set-overlay-properties ()
  142. "Set properties of glasses overlays.
  143. Consider current setting of user variables."
  144. ;; In-identifier overlay
  145. (put 'glasses 'evaporate t)
  146. (put 'glasses 'before-string glasses-separator)
  147. (put 'glasses 'face glasses-face)
  148. ;; Beg-identifier overlay
  149. (put 'glasses-init 'evaporate t)
  150. (put 'glasses-init 'face glasses-face)
  151. ;; Parenthesis overlay
  152. (put 'glasses-parenthesis 'evaporate t)
  153. (put 'glasses-parenthesis 'before-string " "))
  154. (glasses-set-overlay-properties)
  155. (defun glasses-overlay-p (overlay)
  156. "Return whether OVERLAY is an overlay of glasses mode."
  157. (memq (overlay-get overlay 'category)
  158. '(glasses glasses-init glasses-parenthesis)))
  159. (defun glasses-make-overlay (beg end &optional category)
  160. "Create and return readability overlay over the region from BEG to END.
  161. CATEGORY is the overlay category. If it is nil, use the `glasses' category."
  162. (let ((overlay (make-overlay beg end)))
  163. (overlay-put overlay 'category (or category 'glasses))
  164. overlay))
  165. (defun glasses-make-readable (beg end)
  166. "Make identifiers in the region from BEG to END readable."
  167. (let ((case-fold-search nil))
  168. (save-excursion
  169. (save-match-data
  170. ;; Face only
  171. (goto-char beg)
  172. (while (re-search-forward
  173. "\\<\\([A-Z]\\)[a-zA-Z]*\\([a-z][A-Z]\\|[A-Z][a-z]\\)"
  174. end t)
  175. (glasses-make-overlay (match-beginning 1) (match-end 1)
  176. 'glasses-init))
  177. ;; Face + separator
  178. (goto-char beg)
  179. (while (re-search-forward
  180. (if glasses-separate-capital-groups
  181. "[a-z]\\([A-Z]\\)\\|[A-Z]\\([A-Z]\\)[a-z]"
  182. "[a-z]\\([A-Z]\\)")
  183. end t)
  184. (let* ((n (if (match-string 1) 1 2))
  185. (o (glasses-make-overlay (match-beginning n) (match-end n))))
  186. (goto-char (match-beginning n))
  187. (when (and glasses-uncapitalize-p
  188. (save-match-data
  189. (looking-at "[A-Z]\\($\\|[^A-Z]\\)"))
  190. (save-excursion
  191. (save-match-data
  192. (re-search-backward "\\<.")
  193. (looking-at glasses-uncapitalize-regexp))))
  194. (overlay-put o 'invisible t)
  195. (overlay-put o 'after-string (downcase (match-string n))))))
  196. ;; Separator change
  197. (when (and (not (string= glasses-original-separator glasses-separator))
  198. (not (string= glasses-original-separator "")))
  199. (goto-char beg)
  200. (let ((original-regexp (regexp-quote glasses-original-separator)))
  201. (while (re-search-forward
  202. (format "[a-zA-Z0-9]\\(\\(%s\\)+\\)[a-zA-Z0-9]"
  203. original-regexp)
  204. end t)
  205. (goto-char (match-beginning 1))
  206. (while (looking-at original-regexp)
  207. (let ((o (glasses-make-overlay (point) (1+ (point)))))
  208. ;; `concat' ensures the character properties won't merge
  209. (overlay-put o 'display (concat glasses-separator)))
  210. (goto-char (match-end 0))))))
  211. ;; Parentheses
  212. (when glasses-separate-parentheses-p
  213. (goto-char beg)
  214. (while (re-search-forward "[a-zA-Z]_*\\(\(\\)" end t)
  215. (unless (glasses-parenthesis-exception-p (point-at-bol) (match-end 1))
  216. (glasses-make-overlay (match-beginning 1) (match-end 1)
  217. 'glasses-parenthesis))))))))
  218. (defun glasses-make-unreadable (beg end)
  219. "Return identifiers in the region from BEG to END to their unreadable state."
  220. (dolist (o (overlays-in beg end))
  221. (when (glasses-overlay-p o)
  222. (delete-overlay o))))
  223. (defun glasses-convert-to-unreadable ()
  224. "Convert current buffer to unreadable identifiers and return nil.
  225. This function modifies buffer contents, it removes all the separators,
  226. recognized according to the current value of the variable `glasses-separator'."
  227. (when glasses-convert-on-write-p
  228. (let ((case-fold-search nil)
  229. (separator (regexp-quote glasses-separator)))
  230. (save-excursion
  231. (unless (string= glasses-separator "")
  232. (goto-char (point-min))
  233. (while (re-search-forward
  234. (format "[a-z]\\(%s\\)[A-Z]\\|[A-Z]\\(%s\\)[A-Z][a-z]"
  235. separator separator)
  236. nil t)
  237. (let ((n (if (match-string 1) 1 2)))
  238. (replace-match "" t nil nil n)
  239. (goto-char (match-end n))))
  240. (unless (string= glasses-separator glasses-original-separator)
  241. (goto-char (point-min))
  242. (while (re-search-forward (format "[a-zA-Z0-9]\\(%s+\\)[a-zA-Z0-9]"
  243. separator)
  244. nil t)
  245. (replace-match glasses-original-separator nil nil nil 1)
  246. (goto-char (match-beginning 1)))))
  247. (when glasses-separate-parentheses-p
  248. (goto-char (point-min))
  249. (while (re-search-forward "[a-zA-Z]_*\\( \\)\(" nil t)
  250. (unless (glasses-parenthesis-exception-p (point-at-bol) (1+ (match-end 1)))
  251. (replace-match "" t nil nil 1)))))))
  252. ;; nil must be returned to allow use in write file hooks
  253. nil)
  254. (defun glasses-change (beg end &optional _old-len)
  255. "After-change function updating glass overlays."
  256. (let ((beg-line (save-excursion (goto-char beg) (line-beginning-position)))
  257. (end-line (save-excursion (goto-char end) (line-end-position))))
  258. (glasses-make-unreadable beg-line end-line)
  259. (glasses-make-readable beg-line end-line)))
  260. ;;; Minor mode definition
  261. ;;;###autoload
  262. (define-minor-mode glasses-mode
  263. "Minor mode for making identifiers likeThis readable.
  264. With a prefix argument ARG, enable the mode if ARG is positive,
  265. and disable it otherwise. If called from Lisp, enable the mode
  266. if ARG is omitted or nil. When this mode is active, it tries to
  267. add virtual separators (like underscores) at places they belong to."
  268. :group 'glasses :lighter " o^o"
  269. (save-excursion
  270. (save-restriction
  271. (widen)
  272. ;; We erase all the overlays anyway, to avoid dual sight in some
  273. ;; circumstances
  274. (glasses-make-unreadable (point-min) (point-max))
  275. (if glasses-mode
  276. (progn
  277. (jit-lock-register 'glasses-change)
  278. (add-hook 'local-write-file-hooks
  279. 'glasses-convert-to-unreadable nil t))
  280. (jit-lock-unregister 'glasses-change)
  281. (remove-hook 'local-write-file-hooks
  282. 'glasses-convert-to-unreadable t)))))
  283. ;;; Announce
  284. (provide 'glasses)
  285. ;;; glasses.el ends here