PageRenderTime 24ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lisp/progmodes/glasses.el

https://bitbucket.org/zielmicha/emacs
Emacs Lisp | 341 lines | 224 code | 58 blank | 59 comment | 9 complexity | 987fabe1ceffa01f2e588d17d99a8267 MD5 | raw file
  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. ;;; User variables
  44. (defgroup glasses nil
  45. "Make unreadable code likeThis(one) readable."
  46. :version "21.1"
  47. :group 'tools)
  48. (defcustom glasses-separator "_"
  49. "String to be displayed as a visual separator in identifiers.
  50. It is used both for adding missing separators and for replacing separators
  51. defined by `glasses-original-separator'. If you don't want to add missing
  52. separators, set `glasses-separator' to an empty string. If you don't want to
  53. replace existent separators, set `glasses-original-separator' to an empty
  54. string."
  55. :group 'glasses
  56. :type 'string
  57. :set 'glasses-custom-set
  58. :initialize 'custom-initialize-default)
  59. (defcustom glasses-original-separator "_"
  60. "String to be displayed as `glasses-separator' in separator positions.
  61. For instance, if you set it to \"_\" and set `glasses-separator' to \"-\",
  62. underscore separators are displayed as hyphens.
  63. If `glasses-original-separator' is an empty string, no such display change is
  64. performed."
  65. :group 'glasses
  66. :type 'string
  67. :set 'glasses-custom-set
  68. :initialize 'custom-initialize-default
  69. :version "22.1")
  70. (defcustom glasses-face nil
  71. "Face to be put on capitals of an identifier looked through glasses.
  72. If it is nil, no face is placed at the capitalized letter.
  73. For example, you can set `glasses-separator' to an empty string and
  74. `glasses-face' to `bold'. Then unreadable identifiers will have no separators,
  75. but will have their capitals in bold."
  76. :group 'glasses
  77. :type '(choice (const :tag "None" nil) face)
  78. :set 'glasses-custom-set
  79. :initialize 'custom-initialize-default)
  80. (defcustom glasses-separate-parentheses-p t
  81. "If non-nil, ensure space between an identifier and an opening parenthesis."
  82. :group 'glasses
  83. :type 'boolean)
  84. (defcustom glasses-separate-parentheses-exceptions
  85. '("^#[\t ]*define[\t ]*[A-Za-z0-9_-]* ?($")
  86. "List of regexp that are exceptions for `glasses-separate-parentheses-p'.
  87. They are matched to the current line truncated to the point where the
  88. parenthesis expression starts."
  89. :group 'glasses
  90. :type '(repeat regexp))
  91. (defcustom glasses-separate-capital-groups t
  92. "If non-nil, try to separate groups of capital letters.
  93. When the value is non-nil, HTMLSomething and IPv6 are displayed
  94. as HTML_Something and I_Pv6 respectively. Set the value to nil
  95. if you prefer to display them unchanged."
  96. :group 'glasses
  97. :type 'boolean
  98. :version "24.1")
  99. (defcustom glasses-uncapitalize-p nil
  100. "If non-nil, downcase embedded capital letters in identifiers.
  101. Only identifiers starting with lower case letters are affected, letters inside
  102. other identifiers are unchanged."
  103. :group 'glasses
  104. :type 'boolean
  105. :set 'glasses-custom-set
  106. :initialize 'custom-initialize-default)
  107. (defcustom glasses-uncapitalize-regexp "[a-z]"
  108. "Regexp matching beginnings of words to be uncapitalized.
  109. Only words starting with this regexp are uncapitalized.
  110. The regexp is case sensitive.
  111. It has any effect only when `glasses-uncapitalize-p' is non-nil."
  112. :group 'glasses
  113. :type 'regexp
  114. :set 'glasses-custom-set
  115. :initialize 'custom-initialize-default)
  116. (defcustom glasses-convert-on-write-p nil
  117. "If non-nil, remove separators when writing glasses buffer to a file.
  118. If you are confused by glasses so much, that you write the separators into code
  119. during coding, set this variable to t. The separators will be removed on each
  120. file write then.
  121. Note the removal action does not try to be much clever, so it can remove real
  122. separators too."
  123. :group 'glasses
  124. :type 'boolean)
  125. (defun glasses-custom-set (symbol value)
  126. "Set value of the variable SYMBOL to VALUE and update overlay categories.
  127. Used in :set parameter of some customized glasses variables."
  128. (set-default symbol value)
  129. (glasses-set-overlay-properties))
  130. ;;; Utility functions
  131. (defun glasses-parenthesis-exception-p (beg end)
  132. "Tell if (BEG, END) is an exception to `glasses-separate-parentheses-p'.
  133. See `glasses-separate-parentheses-exceptions'."
  134. (save-match-data
  135. (let ((str (buffer-substring beg end)))
  136. (catch 'match
  137. (dolist (re glasses-separate-parentheses-exceptions)
  138. (and (string-match re str) (throw 'match t)))))))
  139. (defun glasses-set-overlay-properties ()
  140. "Set properties of glasses overlays.
  141. Consider current setting of user variables."
  142. ;; In-identifier overlay
  143. (put 'glasses 'evaporate t)
  144. (put 'glasses 'before-string glasses-separator)
  145. (put 'glasses 'face glasses-face)
  146. ;; Beg-identifier overlay
  147. (put 'glasses-init 'evaporate t)
  148. (put 'glasses-init 'face glasses-face)
  149. ;; Parenthesis overlay
  150. (put 'glasses-parenthesis 'evaporate t)
  151. (put 'glasses-parenthesis 'before-string " "))
  152. (glasses-set-overlay-properties)
  153. (defun glasses-overlay-p (overlay)
  154. "Return whether OVERLAY is an overlay of glasses mode."
  155. (memq (overlay-get overlay 'category)
  156. '(glasses glasses-init glasses-parenthesis)))
  157. (defun glasses-make-overlay (beg end &optional category)
  158. "Create and return readability overlay over the region from BEG to END.
  159. CATEGORY is the overlay category. If it is nil, use the `glasses' category."
  160. (let ((overlay (make-overlay beg end)))
  161. (overlay-put overlay 'category (or category 'glasses))
  162. overlay))
  163. (defun glasses-make-readable (beg end)
  164. "Make identifiers in the region from BEG to END readable."
  165. (let ((case-fold-search nil))
  166. (save-excursion
  167. (save-match-data
  168. ;; Face only
  169. (goto-char beg)
  170. (while (re-search-forward
  171. "\\<\\([A-Z]\\)[a-zA-Z]*\\([a-z][A-Z]\\|[A-Z][a-z]\\)"
  172. end t)
  173. (glasses-make-overlay (match-beginning 1) (match-end 1)
  174. 'glasses-init))
  175. ;; Face + separator
  176. (goto-char beg)
  177. (while (re-search-forward
  178. (if glasses-separate-capital-groups
  179. "[a-z]\\([A-Z]\\)\\|[A-Z]\\([A-Z]\\)[a-z]"
  180. "[a-z]\\([A-Z]\\)")
  181. end t)
  182. (let* ((n (if (match-string 1) 1 2))
  183. (o (glasses-make-overlay (match-beginning n) (match-end n))))
  184. (goto-char (match-beginning n))
  185. (when (and glasses-uncapitalize-p
  186. (save-match-data
  187. (looking-at "[A-Z]\\($\\|[^A-Z]\\)"))
  188. (save-excursion
  189. (save-match-data
  190. (re-search-backward "\\<.")
  191. (looking-at glasses-uncapitalize-regexp))))
  192. (overlay-put o 'invisible t)
  193. (overlay-put o 'after-string (downcase (match-string n))))))
  194. ;; Separator change
  195. (when (and (not (string= glasses-original-separator glasses-separator))
  196. (not (string= glasses-original-separator "")))
  197. (goto-char beg)
  198. (let ((original-regexp (regexp-quote glasses-original-separator)))
  199. (while (re-search-forward
  200. (format "[a-zA-Z0-9]\\(\\(%s\\)+\\)[a-zA-Z0-9]"
  201. original-regexp)
  202. end t)
  203. (goto-char (match-beginning 1))
  204. (while (looking-at original-regexp)
  205. (let ((o (glasses-make-overlay (point) (1+ (point)))))
  206. ;; `concat' ensures the character properties won't merge
  207. (overlay-put o 'display (concat glasses-separator)))
  208. (goto-char (match-end 0))))))
  209. ;; Parentheses
  210. (when glasses-separate-parentheses-p
  211. (goto-char beg)
  212. (while (re-search-forward "[a-zA-Z]_*\\(\(\\)" end t)
  213. (unless (glasses-parenthesis-exception-p (point-at-bol) (match-end 1))
  214. (glasses-make-overlay (match-beginning 1) (match-end 1)
  215. 'glasses-parenthesis))))))))
  216. (defun glasses-make-unreadable (beg end)
  217. "Return identifiers in the region from BEG to END to their unreadable state."
  218. (dolist (o (overlays-in beg end))
  219. (when (glasses-overlay-p o)
  220. (delete-overlay o))))
  221. (defun glasses-convert-to-unreadable ()
  222. "Convert current buffer to unreadable identifiers and return nil.
  223. This function modifies buffer contents, it removes all the separators,
  224. recognized according to the current value of the variable `glasses-separator'."
  225. (when glasses-convert-on-write-p
  226. (let ((case-fold-search nil)
  227. (separator (regexp-quote glasses-separator)))
  228. (save-excursion
  229. (unless (string= glasses-separator "")
  230. (goto-char (point-min))
  231. (while (re-search-forward
  232. (format "[a-z]\\(%s\\)[A-Z]\\|[A-Z]\\(%s\\)[A-Z][a-z]"
  233. separator separator)
  234. nil t)
  235. (let ((n (if (match-string 1) 1 2)))
  236. (replace-match "" t nil nil n)
  237. (goto-char (match-end n))))
  238. (unless (string= glasses-separator glasses-original-separator)
  239. (goto-char (point-min))
  240. (while (re-search-forward (format "[a-zA-Z0-9]\\(%s+\\)[a-zA-Z0-9]"
  241. separator)
  242. nil t)
  243. (replace-match glasses-original-separator nil nil nil 1)
  244. (goto-char (match-beginning 1)))))
  245. (when glasses-separate-parentheses-p
  246. (goto-char (point-min))
  247. (while (re-search-forward "[a-zA-Z]_*\\( \\)\(" nil t)
  248. (unless (glasses-parenthesis-exception-p (point-at-bol) (1+ (match-end 1)))
  249. (replace-match "" t nil nil 1)))))))
  250. ;; nil must be returned to allow use in write file hooks
  251. nil)
  252. (defun glasses-change (beg end &optional _old-len)
  253. "After-change function updating glass overlays."
  254. (let ((beg-line (save-excursion (goto-char beg) (line-beginning-position)))
  255. (end-line (save-excursion (goto-char end) (line-end-position))))
  256. (glasses-make-unreadable beg-line end-line)
  257. (glasses-make-readable beg-line end-line)))
  258. ;;; Minor mode definition
  259. ;;;###autoload
  260. (define-minor-mode glasses-mode
  261. "Minor mode for making identifiers likeThis readable.
  262. With a prefix argument ARG, enable the mode if ARG is positive,
  263. and disable it otherwise. If called from Lisp, enable the mode
  264. if ARG is omitted or nil. When this mode is active, it tries to
  265. add virtual separators (like underscores) at places they belong to."
  266. :group 'glasses :lighter " o^o"
  267. (save-excursion
  268. (save-restriction
  269. (widen)
  270. ;; We erase all the overlays anyway, to avoid dual sight in some
  271. ;; circumstances
  272. (glasses-make-unreadable (point-min) (point-max))
  273. (if glasses-mode
  274. (progn
  275. (jit-lock-register 'glasses-change)
  276. (add-hook 'local-write-file-hooks
  277. 'glasses-convert-to-unreadable nil t))
  278. (jit-lock-unregister 'glasses-change)
  279. (remove-hook 'local-write-file-hooks
  280. 'glasses-convert-to-unreadable t)))))
  281. ;;; Announce
  282. (provide 'glasses)
  283. ;;; glasses.el ends here