cpp.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. ;;; cpp.el --- highlight or hide text according to cpp conditionals
  2. ;; Copyright (C) 1994-1995, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
  4. ;; Keywords: c, faces, tools
  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. ;; Parse a text for C preprocessor conditionals, and highlight or hide
  18. ;; the text inside the conditionals as you wish.
  19. ;; This package is inspired by Jim Coplien's delta editor for SCCS.
  20. ;;; Todo:
  21. ;; Should parse "#if" and "#elif" expressions and merge the faces
  22. ;; somehow.
  23. ;; Somehow it is sometimes possible to make changes near a read only
  24. ;; area which you can't undo. Their are other strange effects in that
  25. ;; area.
  26. ;; The Edit buffer should -- optionally -- appear in its own frame.
  27. ;; Conditionals seem to be rear-sticky. They shouldn't be.
  28. ;; Restore window configurations when exiting CPP Edit buffer.
  29. ;;; Code:
  30. ;;; Customization:
  31. (defgroup cpp nil
  32. "Highlight or hide text according to cpp conditionals."
  33. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  34. :group 'c
  35. :prefix "cpp-")
  36. (defcustom cpp-config-file (convert-standard-filename ".cpp.el")
  37. "*File name to save cpp configuration."
  38. :type 'file
  39. :group 'cpp)
  40. (define-widget 'cpp-face 'lazy
  41. "Either a face or the special symbol 'invisible'."
  42. :type '(choice (const invisible) (face)))
  43. (defcustom cpp-known-face 'invisible
  44. "*Face used for known cpp symbols."
  45. :type 'cpp-face
  46. :group 'cpp)
  47. (defcustom cpp-unknown-face 'highlight
  48. "*Face used for unknown cpp symbols."
  49. :type 'cpp-face
  50. :group 'cpp)
  51. (defcustom cpp-face-type 'light
  52. "*Indicate what background face type you prefer.
  53. Can be either light or dark for color screens, mono for monochrome
  54. screens, and none if you don't use a window system and don't have
  55. a color-capable display."
  56. :options '(light dark mono nil)
  57. :type 'symbol
  58. :group 'cpp)
  59. (defcustom cpp-known-writable t
  60. "*Non-nil means you are allowed to modify the known conditionals."
  61. :type 'boolean
  62. :group 'cpp)
  63. (defcustom cpp-unknown-writable t
  64. "*Non-nil means you are allowed to modify the unknown conditionals."
  65. :type 'boolean
  66. :group 'cpp)
  67. (defcustom cpp-edit-list nil
  68. "Alist of cpp macros and information about how they should be displayed.
  69. Each entry is a list with the following elements:
  70. 0. The name of the macro (a string).
  71. 1. Face used for text that is `ifdef' the macro.
  72. 2. Face used for text that is `ifndef' the macro.
  73. 3. t, nil, or `both' depending on what text may be edited."
  74. :type '(repeat (list (string :tag "Macro")
  75. (cpp-face :tag "True")
  76. (cpp-face :tag "False")
  77. (choice (const :tag "True branch writable" t)
  78. (const :tag "False branch writable" nil)
  79. (const :tag "Both branches writable" both))))
  80. :group 'cpp)
  81. (defvar cpp-overlay-list nil)
  82. ;; List of cpp overlays active in the current buffer.
  83. (make-variable-buffer-local 'cpp-overlay-list)
  84. (defvar cpp-callback-data)
  85. (defvar cpp-state-stack)
  86. (defconst cpp-face-type-list
  87. '(("light color background" . light)
  88. ("dark color background" . dark)
  89. ("monochrome" . mono)
  90. ("tty" . none))
  91. "Alist of strings and names of the defined face collections.")
  92. (defconst cpp-writable-list
  93. ;; Names used for the writable property.
  94. '(("writable" . t)
  95. ("read-only" . nil)))
  96. (defvar cpp-button-event nil)
  97. ;; This will be t in the callback for `cpp-make-button'.
  98. (defvar cpp-edit-buffer nil)
  99. ;; Real buffer whose cpp display information we are editing.
  100. (make-variable-buffer-local 'cpp-edit-buffer)
  101. (defconst cpp-branch-list
  102. ;; Alist of branches.
  103. '(("false" . nil)
  104. ("true" . t)
  105. ("both" . both)))
  106. (defcustom cpp-face-default-list nil
  107. "Alist of faces you can choose from for cpp conditionals.
  108. Each element has the form (STRING . FACE), where STRING
  109. serves as a name (for `cpp-highlight-buffer' only)
  110. and FACE is either a face (a symbol)
  111. or a cons cell (background-color . COLOR)."
  112. :type '(repeat (cons string (choice face (cons (const background-color) string))))
  113. :group 'cpp)
  114. (defcustom cpp-face-light-name-list
  115. '("light gray" "light blue" "light cyan" "light yellow" "light pink"
  116. "pale green" "beige" "orange" "magenta" "violet" "medium purple"
  117. "turquoise")
  118. "Background colors useful with dark foreground colors."
  119. :type '(repeat string)
  120. :group 'cpp)
  121. (defcustom cpp-face-dark-name-list
  122. '("dim gray" "blue" "cyan" "yellow" "red"
  123. "dark green" "brown" "dark orange" "dark khaki" "dark violet" "purple"
  124. "dark turquoise")
  125. "Background colors useful with light foreground colors."
  126. :type '(repeat string)
  127. :group 'cpp)
  128. (defcustom cpp-face-light-list nil
  129. "Alist of names and faces to be used for light backgrounds."
  130. :type '(repeat (cons string (choice face
  131. (cons (const background-color) string))))
  132. :group 'cpp)
  133. (defcustom cpp-face-dark-list nil
  134. "Alist of names and faces to be used for dark backgrounds."
  135. :type '(repeat (cons string (choice face
  136. (cons (const background-color) string))))
  137. :group 'cpp)
  138. (defcustom cpp-face-mono-list
  139. '(("bold" . bold)
  140. ("bold-italic" . bold-italic)
  141. ("italic" . italic)
  142. ("underline" . underline))
  143. "Alist of names and faces to be used for monochrome screens."
  144. :type '(repeat (cons string face))
  145. :group 'cpp)
  146. (defcustom cpp-face-none-list
  147. '(("default" . default)
  148. ("invisible" . invisible))
  149. "Alist of names and faces available even if you don't use a window system."
  150. :type '(repeat (cons string cpp-face))
  151. :group 'cpp)
  152. (defvar cpp-face-all-list
  153. (append cpp-face-light-list
  154. cpp-face-dark-list
  155. cpp-face-mono-list
  156. cpp-face-none-list)
  157. "All faces used for highlighting text inside cpp conditionals.")
  158. ;;; Parse Buffer:
  159. (defvar cpp-parse-symbols nil
  160. "List of cpp macros used in the local buffer.")
  161. (make-variable-buffer-local 'cpp-parse-symbols)
  162. (defconst cpp-parse-regexp
  163. ;; Regexp matching all tokens needed to find conditionals.
  164. (concat
  165. "'\\|\"\\|/\\*\\|//\\|"
  166. "\\(^[ \t]*#[ \t]*\\(ifdef\\|ifndef\\|if\\|"
  167. "elif\\|else\\|endif\\)\\b\\)"))
  168. ;;;###autoload
  169. (defun cpp-highlight-buffer (arg)
  170. "Highlight C code according to preprocessor conditionals.
  171. This command pops up a buffer which you should edit to specify
  172. what kind of highlighting to use, and the criteria for highlighting.
  173. A prefix arg suppresses display of that buffer."
  174. (interactive "P")
  175. (unless (or (eq t buffer-invisibility-spec)
  176. (memq 'cpp buffer-invisibility-spec))
  177. (add-to-invisibility-spec 'cpp))
  178. (setq cpp-parse-symbols nil)
  179. (cpp-parse-reset)
  180. (if (null cpp-edit-list)
  181. (cpp-edit-load))
  182. (let (cpp-state-stack)
  183. (save-excursion
  184. (goto-char (point-min))
  185. (cpp-progress-message "Parsing...")
  186. (while (re-search-forward cpp-parse-regexp nil t)
  187. (cpp-progress-message "Parsing...%d%%"
  188. (/ (* 100 (- (point) (point-min))) (buffer-size)))
  189. (let ((match (buffer-substring (match-beginning 0) (match-end 0))))
  190. (cond ((or (string-equal match "'")
  191. (string-equal match "\""))
  192. (goto-char (match-beginning 0))
  193. (condition-case nil
  194. (forward-sexp)
  195. (error (cpp-parse-error
  196. "Unterminated string or character"))))
  197. ((string-equal match "/*")
  198. (or (search-forward "*/" nil t)
  199. (error "Unterminated comment")))
  200. ((string-equal match "//")
  201. (skip-chars-forward "^\n\r"))
  202. (t
  203. (end-of-line 1)
  204. (let ((from (match-beginning 1))
  205. (to (1+ (point)))
  206. (type (buffer-substring (match-beginning 2)
  207. (match-end 2)))
  208. (expr (buffer-substring (match-end 1) (point))))
  209. (cond ((string-equal type "ifdef")
  210. (cpp-parse-open t expr from to))
  211. ((string-equal type "ifndef")
  212. (cpp-parse-open nil expr from to))
  213. ((string-equal type "if")
  214. (cpp-parse-open t expr from to))
  215. ((string-equal type "elif")
  216. (let (cpp-known-face cpp-unknown-face)
  217. (cpp-parse-close from to))
  218. (cpp-parse-open t expr from to))
  219. ((string-equal type "else")
  220. (or cpp-state-stack
  221. (cpp-parse-error "Top level #else"))
  222. (let ((entry (list (not (nth 0 (car cpp-state-stack)))
  223. (nth 1 (car cpp-state-stack))
  224. from to)))
  225. (cpp-parse-close from to)
  226. (setq cpp-state-stack (cons entry cpp-state-stack))))
  227. ((string-equal type "endif")
  228. (cpp-parse-close from to))
  229. (t
  230. (cpp-parse-error "Parser error"))))))))
  231. (message "Parsing...done"))
  232. (if cpp-state-stack
  233. (save-excursion
  234. (goto-char (nth 3 (car cpp-state-stack)))
  235. (cpp-parse-error "Unclosed conditional"))))
  236. (or arg
  237. (null cpp-parse-symbols)
  238. (cpp-parse-edit)))
  239. (defun cpp-parse-open (branch expr begin end)
  240. "Push information about conditional-beginning onto `cpp-state-stack'."
  241. ;; Discard comments within this line.
  242. (while (string-match "\\b[ \t]*/\\*.*\\*/[ \t]*\\b" expr)
  243. (setq expr (concat (substring expr 0 (match-beginning 0))
  244. (substring expr (match-end 0)))))
  245. ;; If a comment starts on this line and continues past, discard it.
  246. (if (string-match "\\b[ \t]*/\\*" expr)
  247. (setq expr (substring expr 0 (match-beginning 0))))
  248. ;; Delete any C++ comment from the line.
  249. (if (string-match "\\b[ \t]*\\(//.*\\)?$" expr)
  250. (setq expr (substring expr 0 (match-beginning 0))))
  251. (while (string-match "[ \t]+" expr)
  252. (setq expr (concat (substring expr 0 (match-beginning 0))
  253. (substring expr (match-end 0)))))
  254. (setq cpp-state-stack (cons (list branch expr begin end) cpp-state-stack))
  255. (or (member expr cpp-parse-symbols)
  256. (setq cpp-parse-symbols
  257. (cons expr cpp-parse-symbols)))
  258. (if (assoc expr cpp-edit-list)
  259. (cpp-make-known-overlay begin end)
  260. (cpp-make-unknown-overlay begin end)))
  261. (defun cpp-parse-close (from to)
  262. ;; Pop top of cpp-state-stack and create overlay.
  263. (let ((entry (assoc (nth 1 (car cpp-state-stack)) cpp-edit-list))
  264. (branch (nth 0 (car cpp-state-stack)))
  265. (end (nth 3 (car cpp-state-stack))))
  266. (setq cpp-state-stack (cdr cpp-state-stack))
  267. (if entry
  268. (let ((face (nth (if branch 1 2) entry))
  269. (read-only (eq (not branch) (nth 3 entry)))
  270. (priority (length cpp-state-stack))
  271. (overlay (make-overlay end from)))
  272. (cpp-make-known-overlay from to)
  273. (setq cpp-overlay-list (cons overlay cpp-overlay-list))
  274. (if priority (overlay-put overlay 'priority priority))
  275. (cond ((eq face 'invisible)
  276. (cpp-make-overlay-hidden overlay))
  277. ((eq face 'default))
  278. (t
  279. (overlay-put overlay 'face face)))
  280. (if read-only
  281. (cpp-make-overlay-read-only overlay)
  282. (cpp-make-overlay-sticky overlay)))
  283. (cpp-make-unknown-overlay from to))))
  284. (defun cpp-parse-error (error)
  285. ;; Error message issued by the cpp parser.
  286. (error "%s at line %d" error (count-lines (point-min) (point))))
  287. (defun cpp-parse-reset ()
  288. "Reset display of cpp conditionals to normal."
  289. (interactive)
  290. (while cpp-overlay-list
  291. (delete-overlay (car cpp-overlay-list))
  292. (setq cpp-overlay-list (cdr cpp-overlay-list))))
  293. ;;;###autoload
  294. (defun cpp-parse-edit ()
  295. "Edit display information for cpp conditionals."
  296. (interactive)
  297. (or cpp-parse-symbols
  298. (cpp-highlight-buffer t))
  299. (let ((buffer (current-buffer)))
  300. (pop-to-buffer "*CPP Edit*")
  301. (cpp-edit-mode)
  302. (setq cpp-edit-buffer buffer)
  303. (cpp-edit-reset)))
  304. ;;; Overlays:
  305. (defun cpp-make-known-overlay (start end)
  306. ;; Create an overlay for a known cpp command from START to END.
  307. (let ((overlay (make-overlay start end)))
  308. (if (eq cpp-known-face 'invisible)
  309. (cpp-make-overlay-hidden overlay)
  310. (or (eq cpp-known-face 'default)
  311. (overlay-put overlay 'face cpp-known-face))
  312. (if cpp-known-writable
  313. ()
  314. (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
  315. (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only))))
  316. (setq cpp-overlay-list (cons overlay cpp-overlay-list))))
  317. (defun cpp-make-unknown-overlay (start end)
  318. ;; Create an overlay for an unknown cpp command from START to END.
  319. (let ((overlay (make-overlay start end)))
  320. (cond ((eq cpp-unknown-face 'invisible)
  321. (cpp-make-overlay-hidden overlay))
  322. ((eq cpp-unknown-face 'default))
  323. (t
  324. (overlay-put overlay 'face cpp-unknown-face)))
  325. (if cpp-unknown-writable
  326. ()
  327. (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
  328. (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)))
  329. (setq cpp-overlay-list (cons overlay cpp-overlay-list))))
  330. (defun cpp-make-overlay-hidden (overlay)
  331. ;; Make overlay hidden and intangible.
  332. (overlay-put overlay 'invisible 'cpp)
  333. (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
  334. (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)))
  335. (defun cpp-make-overlay-read-only (overlay)
  336. ;; Make overlay read only.
  337. (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
  338. (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only))
  339. (overlay-put overlay 'insert-behind-hooks '(cpp-signal-read-only)))
  340. (defun cpp-make-overlay-sticky (overlay)
  341. ;; Make OVERLAY grow when you insert text at either end.
  342. (overlay-put overlay 'insert-in-front-hooks '(cpp-grow-overlay))
  343. (overlay-put overlay 'insert-behind-hooks '(cpp-grow-overlay)))
  344. (defun cpp-signal-read-only (overlay after start end &optional _len)
  345. ;; Only allow deleting the whole overlay.
  346. ;; Trying to change a read-only overlay.
  347. (if (and (not after)
  348. (or (< (overlay-start overlay) start)
  349. (> (overlay-end overlay) end)))
  350. (error "This text is read only")))
  351. (defun cpp-grow-overlay (overlay after start end &optional _len)
  352. ;; Make OVERLAY grow to contain range START to END.
  353. (if after
  354. (move-overlay overlay
  355. (min start (overlay-start overlay))
  356. (max end (overlay-end overlay)))))
  357. ;;; Edit Buffer:
  358. (defvar cpp-edit-mode-map
  359. (let ((map (make-keymap)))
  360. (suppress-keymap map)
  361. (define-key map [ down-mouse-2 ] 'cpp-push-button)
  362. (define-key map [ mouse-2 ] 'ignore)
  363. (define-key map " " 'scroll-up-command)
  364. (define-key map "\C-?" 'scroll-down-command)
  365. (define-key map [ delete ] 'scroll-down)
  366. (define-key map "\C-c\C-c" 'cpp-edit-apply)
  367. (define-key map "a" 'cpp-edit-apply)
  368. (define-key map "A" 'cpp-edit-apply)
  369. (define-key map "r" 'cpp-edit-reset)
  370. (define-key map "R" 'cpp-edit-reset)
  371. (define-key map "s" 'cpp-edit-save)
  372. (define-key map "S" 'cpp-edit-save)
  373. (define-key map "l" 'cpp-edit-load)
  374. (define-key map "L" 'cpp-edit-load)
  375. (define-key map "h" 'cpp-edit-home)
  376. (define-key map "H" 'cpp-edit-home)
  377. (define-key map "b" 'cpp-edit-background)
  378. (define-key map "B" 'cpp-edit-background)
  379. (define-key map "k" 'cpp-edit-known)
  380. (define-key map "K" 'cpp-edit-known)
  381. (define-key map "u" 'cpp-edit-unknown)
  382. (define-key map "u" 'cpp-edit-unknown)
  383. (define-key map "t" 'cpp-edit-true)
  384. (define-key map "T" 'cpp-edit-true)
  385. (define-key map "f" 'cpp-edit-false)
  386. (define-key map "F" 'cpp-edit-false)
  387. (define-key map "w" 'cpp-edit-write)
  388. (define-key map "W" 'cpp-edit-write)
  389. (define-key map "X" 'cpp-edit-toggle-known)
  390. (define-key map "x" 'cpp-edit-toggle-known)
  391. (define-key map "Y" 'cpp-edit-toggle-unknown)
  392. (define-key map "y" 'cpp-edit-toggle-unknown)
  393. (define-key map "q" 'bury-buffer)
  394. (define-key map "Q" 'bury-buffer)
  395. map)
  396. "Keymap for `cpp-edit-mode'.")
  397. (defvar cpp-edit-symbols nil)
  398. ;; Symbols defined in the edit buffer.
  399. (make-variable-buffer-local 'cpp-edit-symbols)
  400. (define-derived-mode cpp-edit-mode fundamental-mode "CPP Edit"
  401. "Major mode for editing the criteria for highlighting cpp conditionals.
  402. Click on objects to change them.
  403. You can also use the keyboard accelerators indicated like this: [K]ey."
  404. (buffer-disable-undo)
  405. (auto-save-mode -1)
  406. (setq buffer-read-only t))
  407. (defun cpp-edit-apply ()
  408. "Apply edited display information to original buffer."
  409. (interactive)
  410. (cpp-edit-home)
  411. (cpp-highlight-buffer t))
  412. (defun cpp-edit-reset ()
  413. "Reset display information from original buffer."
  414. (interactive)
  415. (let ((buffer (current-buffer))
  416. (buffer-read-only nil)
  417. (start (window-start))
  418. (pos (point))
  419. symbols)
  420. (set-buffer cpp-edit-buffer)
  421. (setq symbols cpp-parse-symbols)
  422. (set-buffer buffer)
  423. (setq cpp-edit-symbols symbols)
  424. (erase-buffer)
  425. (insert "CPP Display Information for `")
  426. (cpp-make-button (buffer-name cpp-edit-buffer) 'cpp-edit-home)
  427. (insert "'\n\nClick mouse-2 on item you want to change or use\n"
  428. "or switch to this buffer and type the keyboard equivalents.\n"
  429. "Keyboard equivalents are indicated with brackets like [T]his.\n\n")
  430. (cpp-make-button "[H]ome (display the C file)" 'cpp-edit-home)
  431. (insert " ")
  432. (cpp-make-button "[A]pply new settings" 'cpp-edit-apply)
  433. (insert "\n")
  434. (cpp-make-button "[S]ave settings" 'cpp-edit-save)
  435. (insert " ")
  436. (cpp-make-button "[L]oad settings" 'cpp-edit-load)
  437. (insert "\n\n")
  438. (insert "[B]ackground: ")
  439. (cpp-make-button (car (rassq cpp-face-type cpp-face-type-list))
  440. 'cpp-edit-background)
  441. (insert "\n[K]nown conditionals: ")
  442. (cpp-make-button (cpp-face-name cpp-known-face)
  443. 'cpp-edit-known nil t)
  444. (insert " [X] ")
  445. (cpp-make-button (car (rassq cpp-known-writable cpp-writable-list))
  446. 'cpp-edit-toggle-known)
  447. (insert "\n[U]nknown conditionals: ")
  448. (cpp-make-button (cpp-face-name cpp-unknown-face)
  449. 'cpp-edit-unknown nil t)
  450. (insert " [Y] ")
  451. (cpp-make-button (car (rassq cpp-unknown-writable cpp-writable-list))
  452. 'cpp-edit-toggle-unknown)
  453. (insert (format "\n\n\n%39s: %14s %14s %7s\n\n" "Expression"
  454. "[T]rue Face" "[F]alse Face" "[W]rite"))
  455. (setq symbols (reverse symbols))
  456. (while symbols
  457. (let* ((symbol (car symbols))
  458. (entry (assoc symbol cpp-edit-list))
  459. (true (nth 1 entry))
  460. (false (nth 2 entry))
  461. (write (if entry (nth 3 entry) 'both)))
  462. (setq symbols (cdr symbols))
  463. (if (and entry ; Make default entries unknown.
  464. (or (null true) (eq true 'default))
  465. (or (null false) (eq false 'default))
  466. (eq write 'both))
  467. (setq cpp-edit-list (delq entry cpp-edit-list)
  468. entry nil))
  469. (if (> (length symbol) 39)
  470. (insert (substring symbol 0 39) ": ")
  471. (insert (format "%39s: " symbol)))
  472. (cpp-make-button (cpp-face-name true)
  473. 'cpp-edit-true symbol t 14)
  474. (insert " ")
  475. (cpp-make-button (cpp-face-name false)
  476. 'cpp-edit-false symbol t 14)
  477. (insert " ")
  478. (cpp-make-button (car (rassq write cpp-branch-list))
  479. 'cpp-edit-write symbol nil 6)
  480. (insert "\n")))
  481. (insert "\n\n")
  482. (set-window-start nil start)
  483. (goto-char pos)))
  484. (defun cpp-edit-load ()
  485. "Load cpp configuration."
  486. (interactive)
  487. (cond ((null init-file-user)
  488. ;; If -q was specified, don't load any init files.
  489. nil)
  490. ((file-readable-p cpp-config-file)
  491. (load-file cpp-config-file))
  492. ((file-readable-p (concat "~/" cpp-config-file))
  493. (load-file cpp-config-file)))
  494. (if (derived-mode-p 'cpp-edit-mode)
  495. (cpp-edit-reset)))
  496. (defun cpp-edit-save ()
  497. "Save the current cpp configuration in a file."
  498. (interactive)
  499. (require 'pp)
  500. (with-current-buffer cpp-edit-buffer
  501. (let ((buffer (find-file-noselect cpp-config-file)))
  502. (set-buffer buffer)
  503. (erase-buffer)
  504. (pp (list 'setq 'cpp-known-face
  505. (list 'quote cpp-known-face)) buffer)
  506. (pp (list 'setq 'cpp-unknown-face
  507. (list 'quote cpp-unknown-face)) buffer)
  508. (pp (list 'setq 'cpp-face-type
  509. (list 'quote cpp-face-type)) buffer)
  510. (pp (list 'setq 'cpp-known-writable
  511. (list 'quote cpp-known-writable)) buffer)
  512. (pp (list 'setq 'cpp-unknown-writable
  513. (list 'quote cpp-unknown-writable)) buffer)
  514. (pp (list 'setq 'cpp-edit-list
  515. (list 'quote cpp-edit-list)) buffer)
  516. (write-file cpp-config-file))))
  517. (defun cpp-edit-home ()
  518. "Switch back to original buffer."
  519. (interactive)
  520. (if cpp-button-event
  521. (read-event))
  522. (pop-to-buffer cpp-edit-buffer))
  523. (defun cpp-edit-background ()
  524. "Change default face collection."
  525. (interactive)
  526. (call-interactively 'cpp-choose-default-face)
  527. (cpp-edit-reset))
  528. (defun cpp-edit-known ()
  529. "Select default for known conditionals."
  530. (interactive)
  531. (setq cpp-known-face (cpp-choose-face "Known face" cpp-known-face))
  532. (cpp-edit-reset))
  533. (defun cpp-edit-unknown ()
  534. "Select default for unknown conditionals."
  535. (interactive)
  536. (setq cpp-unknown-face (cpp-choose-face "Unknown face" cpp-unknown-face))
  537. (cpp-edit-reset))
  538. (defun cpp-edit-toggle-known (arg)
  539. "Toggle writable status for known conditionals.
  540. With optional argument ARG, make them writable if ARG is positive,
  541. otherwise make them unwritable."
  542. (interactive "@P")
  543. (if (or (and (null arg) cpp-known-writable)
  544. (<= (prefix-numeric-value arg) 0))
  545. (setq cpp-known-writable nil)
  546. (setq cpp-known-writable t))
  547. (cpp-edit-reset))
  548. (defun cpp-edit-toggle-unknown (arg)
  549. "Toggle writable status for unknown conditionals.
  550. With optional argument ARG, make them writable if ARG is positive,
  551. otherwise make them unwritable."
  552. (interactive "@P")
  553. (if (or (and (null arg) cpp-unknown-writable)
  554. (<= (prefix-numeric-value arg) 0))
  555. (setq cpp-unknown-writable nil)
  556. (setq cpp-unknown-writable t))
  557. (cpp-edit-reset))
  558. (defun cpp-edit-true (symbol face)
  559. "Select SYMBOL's true FACE used for highlighting taken conditionals."
  560. (interactive
  561. (let ((symbol (cpp-choose-symbol)))
  562. (list symbol
  563. (cpp-choose-face "True face"
  564. (nth 1 (assoc symbol cpp-edit-list))))))
  565. (setcar (nthcdr 1 (cpp-edit-list-entry-get-or-create symbol)) face)
  566. (cpp-edit-reset))
  567. (defun cpp-edit-false (symbol face)
  568. "Select SYMBOL's false FACE used for highlighting untaken conditionals."
  569. (interactive
  570. (let ((symbol (cpp-choose-symbol)))
  571. (list symbol
  572. (cpp-choose-face "False face"
  573. (nth 2 (assoc symbol cpp-edit-list))))))
  574. (setcar (nthcdr 2 (cpp-edit-list-entry-get-or-create symbol)) face)
  575. (cpp-edit-reset))
  576. (defun cpp-edit-write (symbol branch)
  577. "Set which branches of SYMBOL should be writable to BRANCH.
  578. BRANCH should be either nil (false branch), t (true branch) or 'both."
  579. (interactive (list (cpp-choose-symbol) (cpp-choose-branch)))
  580. (setcar (nthcdr 3 (cpp-edit-list-entry-get-or-create symbol)) branch)
  581. (cpp-edit-reset))
  582. (defun cpp-edit-list-entry-get-or-create (symbol)
  583. ;; Return the entry for SYMBOL in `cpp-edit-list'.
  584. ;; If it does not exist, create it.
  585. (let ((entry (assoc symbol cpp-edit-list)))
  586. (or entry
  587. (setq entry (list symbol nil nil 'both nil)
  588. cpp-edit-list (cons entry cpp-edit-list)))
  589. entry))
  590. ;;; Prompts:
  591. (defun cpp-choose-symbol ()
  592. ;; Choose a symbol if called from keyboard, otherwise use the one clicked on.
  593. (if cpp-button-event
  594. cpp-callback-data
  595. (completing-read "Symbol: " cpp-edit-symbols nil t)))
  596. (defun cpp-choose-branch ()
  597. ;; Choose a branch, either nil, t, or both.
  598. (if cpp-button-event
  599. (x-popup-menu cpp-button-event
  600. (list "Branch" (cons "Branch" cpp-branch-list)))
  601. (cdr (assoc (completing-read "Branch: " cpp-branch-list nil t)
  602. cpp-branch-list))))
  603. (defun cpp-choose-face (prompt default)
  604. ;; Choose a face from cpp-face-default-list.
  605. ;; PROMPT is what to say to the user.
  606. ;; DEFAULT is the default face.
  607. (or (if cpp-button-event
  608. (x-popup-menu cpp-button-event
  609. (list prompt (cons prompt cpp-face-default-list)))
  610. (let ((name (car (rassq default cpp-face-default-list))))
  611. (cdr (assoc (completing-read (if name
  612. (concat prompt
  613. " (default " name "): ")
  614. (concat prompt ": "))
  615. cpp-face-default-list nil t)
  616. cpp-face-all-list))))
  617. default))
  618. (defun cpp-choose-default-face (type)
  619. ;; Choose default face list for screen of TYPE.
  620. ;; Type must be one of the types defined in `cpp-face-type-list'.
  621. (interactive (list (if cpp-button-event
  622. (x-popup-menu cpp-button-event
  623. (list "Screen type"
  624. (cons "Screen type"
  625. cpp-face-type-list)))
  626. (cdr (assoc (completing-read "Screen type: "
  627. cpp-face-type-list
  628. nil t)
  629. cpp-face-type-list)))))
  630. (cond ((null type))
  631. ((eq type 'light)
  632. (if cpp-face-light-list
  633. ()
  634. (setq cpp-face-light-list
  635. (mapcar 'cpp-create-bg-face cpp-face-light-name-list))
  636. (setq cpp-face-all-list
  637. (append cpp-face-all-list cpp-face-light-list)))
  638. (setq cpp-face-type 'light)
  639. (setq cpp-face-default-list
  640. (append cpp-face-light-list cpp-face-none-list)))
  641. ((eq type 'dark)
  642. (if cpp-face-dark-list
  643. ()
  644. (setq cpp-face-dark-list
  645. (mapcar 'cpp-create-bg-face cpp-face-dark-name-list))
  646. (setq cpp-face-all-list
  647. (append cpp-face-all-list cpp-face-dark-list)))
  648. (setq cpp-face-type 'dark)
  649. (setq cpp-face-default-list
  650. (append cpp-face-dark-list cpp-face-none-list)))
  651. ((eq type 'mono)
  652. (setq cpp-face-type 'mono)
  653. (setq cpp-face-default-list
  654. (append cpp-face-mono-list cpp-face-none-list)))
  655. (t
  656. (setq cpp-face-type 'none)
  657. (setq cpp-face-default-list cpp-face-none-list))))
  658. ;;; Buttons:
  659. (defun cpp-make-button (name callback &optional data face padding)
  660. ;; Create a button at point.
  661. ;; NAME is the name of the button.
  662. ;; CALLBACK is the function to call when the button is pushed.
  663. ;; DATA will be made available to CALLBACK
  664. ;;in the free variable cpp-callback-data.
  665. ;; FACE means that NAME is the name of a face in `cpp-face-all-list'.
  666. ;; PADDING means NAME will be right justified at that length.
  667. (let ((name (format "%s" name))
  668. from to)
  669. (cond ((null padding)
  670. (setq from (point))
  671. (insert name))
  672. ((> (length name) padding)
  673. (setq from (point))
  674. (insert (substring name 0 padding)))
  675. (t
  676. (insert (make-string (- padding (length name)) ? ))
  677. (setq from (point))
  678. (insert name)))
  679. (setq to (point))
  680. (setq face
  681. (if face
  682. (let ((check (cdr (assoc name cpp-face-all-list))))
  683. (if (memq check '(default invisible))
  684. 'bold
  685. check))
  686. 'bold))
  687. (add-text-properties from to
  688. (append (list 'face face)
  689. '(mouse-face highlight)
  690. '(help-echo "mouse-2: change/use this item")
  691. (list 'cpp-callback callback)
  692. (if data (list 'cpp-data data))))))
  693. (defun cpp-push-button (event)
  694. ;; Pushed a CPP button.
  695. (interactive "@e")
  696. (set-buffer (window-buffer (posn-window (event-start event))))
  697. (let ((pos (posn-point (event-start event))))
  698. (let ((cpp-callback-data (get-text-property pos 'cpp-data))
  699. (fun (get-text-property pos 'cpp-callback))
  700. (cpp-button-event event))
  701. (cond (fun
  702. (call-interactively (get-text-property pos 'cpp-callback)))
  703. ((lookup-key global-map [ down-mouse-2])
  704. (call-interactively (lookup-key global-map [ down-mouse-2])))))))
  705. ;;; Faces:
  706. (defun cpp-create-bg-face (color)
  707. ;; Create entry for face with background COLOR.
  708. (cons color (cons 'background-color color)))
  709. (cpp-choose-default-face
  710. (if (or window-system (display-color-p)) cpp-face-type 'none))
  711. (defun cpp-face-name (face)
  712. ;; Return the name of FACE from `cpp-face-all-list'.
  713. (let ((entry (rassq (if face face 'default) cpp-face-all-list)))
  714. (if entry
  715. (car entry)
  716. (format "<%s>" face))))
  717. ;;; Utilities:
  718. (defvar cpp-progress-time 0)
  719. ;; Last time we issued a progress message.
  720. (defun cpp-progress-message (&rest args)
  721. ;; Report progress at most once a second. Take same ARGS as `message'.
  722. (let ((time (nth 1 (current-time))))
  723. (if (= time cpp-progress-time)
  724. ()
  725. (setq cpp-progress-time time)
  726. (apply 'message args))))
  727. (provide 'cpp)
  728. ;;; cpp.el ends here