mwe-color-box.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. ;;; mwe-color-box.el --- display color boxes for each nesting level
  2. ;; Copyright (C) 2004, 2007 Free Software Foundation, Inc.
  3. ;; Author: Michael Weber <michaelw@foldr.org>
  4. ;; Keywords: faces, games
  5. ;; Initial-Version: <2004-11-07 22:05:07 michaelw>
  6. ;; Time-stamp: <2007-03-17 11:48:06 michaelw>
  7. ;; This file 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 2, or (at your option)
  10. ;; any later version.
  11. ;; This file 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; see the file COPYING. If not, write to
  17. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. ;; Boston, MA 02111-1307, USA.
  19. ;;; Commentary:
  20. ;; Color-boxify current buffer with M-x mwe:color-box-buffer
  21. ;; Sit back. Enjoy. :)
  22. ;; Notes:
  23. ;; * Buffer is made read-only, so that editing is not possible
  24. ;;
  25. ;; Inspired by http://www.32768.com/bill/weblog/000660.shtml#000660
  26. ;;
  27. ;; As an example, uncomment FACTORIAL, mark it, and
  28. ;; use M-x `mwe:color-box-region/miscbill'.
  29. ;;
  30. ;; To get the same colors as on the webpage, eval:
  31. ;; (custom-set-faces
  32. ;; '(mwe:nesting-face-0 ((((class color)) (:background "#90b0f0"))))
  33. ;; '(mwe:nesting-face-1 ((((class color)) (:background "#b090f0"))))
  34. ;; '(mwe:nesting-face-2 ((((class color)) (:background "#f0b090"))))
  35. ;; '(mwe:nesting-face-3 ((((class color)) (:background "#90b0f0"))))
  36. ;; '(mwe:nesting-face-4 ((((class color)) (:background "#90f0b0"))))
  37. ;; '(mwe:nesting-face-5 ((((class color)) (:background "#b0f090"))))
  38. ;; '(mwe:nesting-face-6 ((((class color)) (:background "#b090f0"))))
  39. ;; '(mwe:nesting-face-7 ((((class color)) (:background "#90b0f0"))))
  40. ;; '(mwe:nesting-face-8 ((((class color)) (:background "#b0f090")))))
  41. ;; (DEFUN FACTORIAL
  42. ;; (X)
  43. ;; (COND
  44. ;; (
  45. ;; (EQ X 1)
  46. ;; 1)
  47. ;; (T
  48. ;; (* X
  49. ;; (FACTORIAL
  50. ;; (- X 1)
  51. ;; )
  52. ;; )
  53. ;; )
  54. ;; )
  55. ;; )
  56. ;;; Code:
  57. (eval-and-compile
  58. (require 'cl)
  59. (require 'rect)
  60. (require 'thingatpt))
  61. (defvar *mwe:color-box-face-list*
  62. '(mwe:nesting-face-0 mwe:nesting-face-1
  63. mwe:nesting-face-2 mwe:nesting-face-3
  64. mwe:nesting-face-4 mwe:nesting-face-5
  65. mwe:nesting-face-6 mwe:nesting-face-7
  66. mwe:nesting-face-8 mwe:nesting-face-9)
  67. "Faces used for color boxes.
  68. See `mwe:color-box-region'.")
  69. ;;; color boxes
  70. (defun mwe:color-box-color (depth)
  71. "Determines color of color box at nesting depth DEPTH."
  72. (nth (mod depth (length *mwe:color-box-face-list*))
  73. *mwe:color-box-face-list*))
  74. ;;;###autoload
  75. (defun mwe:color-box-region (beg end &optional rmargin reg-tok-fn)
  76. "Create nested color boxes for region BEG to END.
  77. If positive number, RMARGIN sets right margin of color boxes to column RMARGIN.
  78. If Non-nil, REG-TOK-FN sets the tokenizer. If nil, uses `mwe:tokenize-region'.
  79. Calls `mwe:color-box-color' with argument DEPTH to pick color."
  80. (interactive "*r")
  81. (let ((buf (current-buffer)))
  82. (with-output-to-temp-buffer (buffer-name
  83. (get-buffer-create "*Color Boxified*"))
  84. (with-current-buffer standard-output
  85. (lisp-mode)
  86. (insert-buffer-substring buf beg end)
  87. (setq reg-tok-fn (or reg-tok-fn #'mwe:tokenize-region))
  88. (let ((indent-tabs-mode nil))
  89. (save-match-data
  90. (loop
  91. for (type depth ov . ignore) in (funcall reg-tok-fn beg end)
  92. for beg = (overlay-start ov)
  93. for end = (overlay-end ov)
  94. for maxcol = (if (natnump rmargin) rmargin
  95. (mwe:region-max-column beg end))
  96. do (case type
  97. ((sexp)
  98. (mwe:rectangle-put-properties (overlay-start ov)
  99. (overlay-end ov)
  100. (if rmargin (- maxcol depth) maxcol)
  101. 'face (mwe:color-box-color depth))))
  102. do (delete-overlay ov))
  103. (toggle-read-only 1)))))))
  104. ;;;###autoload
  105. (defun mwe:color-box-buffer (&optional buf)
  106. "Create nested color boxes for buffer.
  107. See also `mwe:color-box-region'."
  108. (interactive "*")
  109. (with-current-buffer (or buf (current-buffer))
  110. (let ((*mwe:region-tokenizer* #'mwe:slist-tokenizer))
  111. (mwe:color-box-region (point-min) (point-max)))))
  112. ;;; property helpers
  113. (defvar *mwe:color-box-overlays* ()
  114. "List of active color box overlays.
  115. See `mwe:color-box-region'.")
  116. (make-variable-buffer-local '*mwe:color-box-overlays*)
  117. (defun mwe:line-put-properties (startcol endcol &rest props)
  118. "Sets properties PROPS for current line.
  119. Start and end columns are given by STARTCOL and ENDCOL.
  120. If ENDCOL exceeds current line length, whitespace is added up to ENDCOL."
  121. (move-to-column startcol t)
  122. (let ((ov (make-overlay (point)
  123. (progn (move-to-column endcol t) (point))
  124. (current-buffer)
  125. t nil)))
  126. (push ov *mwe:color-box-overlays*)
  127. (apply 'overlay-put ov props)))
  128. (defun mwe:rectangle-put-properties (beg end maxcol &rest props)
  129. "Sets properties PROPS for rectangle.
  130. Rectangle is given by points BEG and END. Right margin is at column MAXCOL."
  131. (save-excursion
  132. (let* ((end (if (> maxcol 0)
  133. (progn (goto-char end)
  134. (move-to-column maxcol t)
  135. (point))
  136. end)))
  137. (apply 'apply-on-rectangle 'mwe:line-put-properties beg end props))))
  138. (defun mwe:region-max-column (beg end &optional trailing-space-p)
  139. "Returns maximum line width of region BEG to END.
  140. If TRAILING-SPACE-P is nil (default), ignore whitespace at end of lines."
  141. (interactive "r")
  142. (save-excursion
  143. (save-restriction
  144. (goto-char beg)
  145. (narrow-to-region (line-beginning-position) end)
  146. (let ((maxcol 0))
  147. (while (not (eobp))
  148. (end-of-line)
  149. (unless trailing-space-p
  150. (skip-chars-backward "\t "))
  151. (when (< maxcol (current-column))
  152. (setq maxcol (current-column)))
  153. (forward-line))
  154. (when (interactive-p)
  155. (message "maximum column in region: %d" maxcol))
  156. maxcol))))
  157. ;;; tokenizing
  158. (defvar *mwe:region-tokenizer* #'mwe:slist-tokenizer
  159. "Refers to function used for tokenizing.
  160. Should be bound locally before using function `mwe:tokenize-region'.")
  161. (defun mwe:tokenize-region (beg end)
  162. "Tokenize region BEG to END.
  163. First prepares region, then calls function referred to in
  164. variable `*mwe:region-tokenizer*'."
  165. (save-restriction
  166. (goto-char beg)
  167. (narrow-to-region (line-beginning-position) end)
  168. (let ((maxcol (mwe:region-max-column beg end)))
  169. (apply-on-rectangle (lambda (scol ecol maxcol)
  170. (move-to-column maxcol t))
  171. (point-min)
  172. (point-max)
  173. maxcol))
  174. (untabify (point-min) (point-max))
  175. (funcall *mwe:region-tokenizer*)))
  176. ;;; s-expression tokenizer
  177. (defun mwe:skip-whitespace ()
  178. "Skip over whitespace and comments."
  179. (interactive)
  180. (while (forward-comment +1)))
  181. (defun mwe:make-sexp-token (beg end type depth &optional slist)
  182. "Make sexp tokens.
  183. Arguments are:
  184. BEG point where token start
  185. END point where token ends
  186. TYPE SEXP or ATOM
  187. DEPTH nesting depth
  188. SLIST list of sub-token where current sexp is built from
  189. (optional, depending on token type)"
  190. (flet ((mwe:make-overlay (beg end)
  191. (make-overlay beg end (current-buffer) nil t)))
  192. (cons (list type depth (mwe:make-overlay beg end)) slist)))
  193. (defadvice mwe:make-sexp-token (before mwe:make-sexp-token-hide-parens
  194. (beg end type depth &optional slist)
  195. activate compile)
  196. (when (and slist mwe:color-box-hide-parens)
  197. (when (eq ?\( (char-after beg))
  198. (put-text-property beg (1+ beg) 'invisible 'color-box-mode))
  199. (when (eq ?\) (char-before end))
  200. (put-text-property (1- end) end 'invisible 'color-box-mode))))
  201. (defun mwe:sexp-tokenizer (&optional depth)
  202. "S-expression tokenizer.
  203. DEPTH is current nesting level.
  204. Returns list of tokens.
  205. Tokens are built via calls to `mwe:make-sexp-token'. Arguments are:
  206. 1. BEG point where token start
  207. 2. END point where token ends
  208. 3. TYPE SEXP or ATOM
  209. 4. DEPTH nesting depth
  210. 5. SLIST list of sub-token where current sexp is built from
  211. (may be nil)
  212. "
  213. (setq depth (or depth 0))
  214. (mwe:skip-whitespace)
  215. (cond ((looking-at "(")
  216. (let* ((point (prog1 (point) (forward-char)))
  217. (toks (mwe:slist-tokenizer (1+ depth)))
  218. (epoint (progn (end-of-sexp) (point))))
  219. (mwe:make-sexp-token point epoint 'sexp depth toks)))
  220. ((looking-at "['`]")
  221. (let* ((point (prog1 (point) (forward-char)))
  222. (toks (mwe:sexp-tokenizer (1+ depth)))
  223. (epoint (point)))
  224. (mwe:make-sexp-token point epoint 'sexp depth toks)))
  225. (t (let* ((point (point))
  226. (epoint (progn (end-of-sexp) (point))))
  227. (when (< point epoint)
  228. (mwe:make-sexp-token point epoint 'atom depth))))))
  229. (defun mwe:slist-tokenizer (&optional depth)
  230. "S-list tokenizer.
  231. DEPTH is current nesting level.
  232. See also `mwe:sexp-tokenizer'."
  233. (setq depth (or depth 0))
  234. (mwe:skip-whitespace)
  235. (loop until (or (looking-at ")") (eobp))
  236. nconc (prog1 (mwe:sexp-tokenizer depth)
  237. (mwe:skip-whitespace))))
  238. ;;; Miscellaneous Bill's rendering theme
  239. ;;;###autoload
  240. (defun mwe:color-box-region/miscbill (beg end &optional rmargin)
  241. (interactive "*r")
  242. (let ((*mwe:color-box-colors/miscbill* (copy-list *mwe:color-box-face-list*)))
  243. (nconc *mwe:color-box-colors/miscbill* *mwe:color-box-colors/miscbill*)
  244. (flet ((mwe:color-box-color (depth)
  245. (pop *mwe:color-box-colors/miscbill*)))
  246. (mwe:color-box-region beg end (case rmargin
  247. ((0) nil)
  248. ((nil) 30)
  249. (t rmargin))))))
  250. ;;; faces
  251. (defgroup mwe:color-box '((mwe:nesting-faces custom-group))
  252. "Color boxes."
  253. :group 'editing)
  254. (defcustom mwe:color-box-hide-parens t
  255. "Hide parentheses when activating color boxes."
  256. :type '(choice (const :tag "Yes" t)
  257. (const :tag "No" nil))
  258. :group 'mwe:color-box)
  259. (defgroup mwe:nesting-faces ()
  260. "Nesting level faces."
  261. :group 'faces)
  262. (defface mwe:nesting-face-0
  263. '((((class color))
  264. (:background "gray2")))
  265. "Face for displaying nesting-level 0."
  266. :group 'mwe:nesting-faces)
  267. (defface mwe:nesting-face-1
  268. '((((class color))
  269. (:background "gray10")))
  270. "Face for displaying nesting-level 1."
  271. :group 'mwe:nesting-faces)
  272. (defface mwe:nesting-face-2
  273. '((((class color))
  274. (:background "gray17")))
  275. "Face for displaying nesting-level 2."
  276. :group 'mwe:nesting-faces)
  277. (defface mwe:nesting-face-3
  278. '((((class color))
  279. (:background "gray25")))
  280. "Face for displaying nesting-level 3."
  281. :group 'mwe:nesting-faces)
  282. (defface mwe:nesting-face-4
  283. '((((class color))
  284. (:background "gray32")))
  285. "Face for displaying nesting-level 4."
  286. :group 'mwe:nesting-faces)
  287. (defface mwe:nesting-face-5
  288. '((((class color))
  289. (:background "gray40")))
  290. "Face for displaying nesting-level 5."
  291. :group 'mwe:nesting-faces)
  292. (defface mwe:nesting-face-6
  293. '((((class color))
  294. (:background "gray47")))
  295. "Face for displaying nesting-level 6."
  296. :group 'mwe:nesting-faces)
  297. (defface mwe:nesting-face-7
  298. '((((class color))
  299. (:background "gray52")))
  300. "Face for displaying nesting-level 7."
  301. :group 'mwe:nesting-faces)
  302. (defface mwe:nesting-face-8
  303. '((((class color))
  304. (:background "gray60")))
  305. "Face for displaying nesting-level 8."
  306. :group 'mwe:nesting-faces)
  307. (defface mwe:nesting-face-9
  308. '((((class color))
  309. (:background "gray67")))
  310. "Face for displaying nesting-level 9."
  311. :group 'mwe:nesting-faces)
  312. (provide 'mwe-color-box)
  313. ;;; mwe-color-box.el ends here