fixme-mode.el 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ;;; fixme-mode.el --- Makes FIXME, TODO, etc. appear in big, angry letters
  2. ;; Filename: fixme-mode.el
  3. ;; Description: Makes source code warnings (FIXME, TODO, etc.) stand out
  4. ;; in a big way.
  5. ;; Author: Bryan Waite, based on some code found at
  6. ;; http://c2.com/cgi/wiki?FixmeComment
  7. ;; Copyright (C) 2009-2010, Bryan Waite
  8. ;; License: MIT License (not reproduced for space reasons)
  9. ;; Compatibility: Only tested under Emacs 23.1 on Ubuntu Linux.
  10. ;; Usage: Just add (require 'fixme-mode) to your .emacs file
  11. ;; and then type M-x fixme-mode to try it out.
  12. (require 'cl)
  13. (defconst fixme-mode-version 0.2)
  14. (defgroup fixme-mode nil
  15. "Highlights FIXME, TODO, and other warnings in source code"
  16. :prefix "fixme-"
  17. :link '(url-link "http://www.thesiteiwillonedayhave.com"))
  18. (defcustom fixme-modes '(erlang-mode java-mode c-mode emacs-lisp-mode jde-mode
  19. scheme-mode python-mode ruby-mode cperl-mode
  20. slime-mode common-lisp-mode c++-mode d-mode
  21. js2-mode haskell-mode tuareg-mode lua-mode
  22. pascal-mode fortran-mode prolog-mode asm-mode
  23. csharp-mode sml-mode)
  24. "The modes which fixme should apply to"
  25. :group 'fixme-mode)
  26. (defcustom fixme-highlighted-words '("FIXME" "TODO" "BUG" "KLUDGE")
  27. "Words to highlight"
  28. :group 'fixme-mode)
  29. (defcustom fixme-foreground-color "Red"
  30. "Font foreground colour"
  31. :group 'fixme-mode)
  32. (defcustom fixme-background-color "Yellow"
  33. "Font background color"
  34. :group 'fixme-mode)
  35. (defvar fixme-keyword-re-string ""
  36. "The regular expression to use for searching for fixme words. Generated with fixme-register-keyword-re")
  37. (defvar fixme-keyword-font-lock '()
  38. "Font lock keywords. Generated from fixme-register-font-lock-keywords")
  39. (make-face 'font-lock-fixme-face)
  40. (defun fixme-next ()
  41. "Goto the next fixme highlighted word"
  42. (interactive)
  43. (lexical-let ((old-case case-fold-search))
  44. (setq case-fold-search nil)
  45. (search-forward-regexp fixme-keyword-re-string)
  46. (setq case-fold-search old-case)))
  47. (defun fixme-prev ()
  48. "Goto the previous fixme highlighted word"
  49. (interactive)
  50. (lexical-let ((old-case case-fold-search))
  51. (setq case-fold-search nil)
  52. (search-backward-regexp fixme-keyword-re-string)
  53. (setq case-fold-search old-case)))
  54. (defun fixme-show-all-fixmes ()
  55. "Show all fixme strings in the current file"
  56. (interactive)
  57. (let ((buf (buffer-file-name)))
  58. (when buf
  59. ;;(grep (concat "grep -nH -e " (concat "\"" fixme-keyword-re-string "\" " buf)))
  60. (occur fixme-keyword-re-string)
  61. )))
  62. (defun fixme-register-keyword-re ()
  63. "Generate the regular expression string from fixme-highlighted-words
  64. and store the result in fixme-keyword-re-string"
  65. (lexical-let ((num-words (length fixme-highlighted-words))
  66. (word-count 0))
  67. (setq fixme-keyword-re-string "")
  68. (dolist (word fixme-highlighted-words)
  69. (incf word-count)
  70. (setq fixme-keyword-re-string (concat fixme-keyword-re-string word))
  71. (when (< word-count num-words) ;;only add the OR in if we're not at the end
  72. (setq fixme-keyword-re-string (concat fixme-keyword-re-string "\\|"))))))
  73. (defun fixme-register-font-lock-keywords ()
  74. "Generate the font-lock keywords from fixme-highlighted-words
  75. and store the result in fixme-keyword-font-lock"
  76. (lexical-let ((stuff '()))
  77. (dolist (word fixme-highlighted-words)
  78. (setq stuff (append stuff `((,(concat "\\<\\(" word "\\)") 1 'font-lock-fixme-face t)))))
  79. (setq fixme-keyword-font-lock stuff)))
  80. (defun fixme-register-keywords ()
  81. "Register the font-lock keywords from fixme-keyword-font-lock with the modes
  82. listed in fixme-modes"
  83. (mapc (lambda (mode)
  84. (font-lock-add-keywords
  85. mode
  86. fixme-keyword-font-lock))
  87. fixme-modes)
  88. (make-face 'font-lock-fixme-face)
  89. (modify-face 'font-lock-fixme-face fixme-foreground-color
  90. fixme-background-color nil t nil t nil nil))
  91. (defun fixme-remove-keywords ()
  92. "Remove the font-lock keywords from fixme-keyword-font-lock with the modes
  93. listed in fixme-modes"
  94. (mapc (lambda (mode)
  95. (font-lock-remove-keywords
  96. mode
  97. fixme-keyword-font-lock))
  98. fixme-modes))
  99. (defun fixme-reload-keywords ()
  100. "Run this if you change the fixme-modes or fixme-highlighted-words variables
  101. to update the font-lock and searching variables"
  102. (interactive)
  103. (fixme-register-keyword-re)
  104. (fixme-register-font-lock-keywords)
  105. (fixme-register-keywords)
  106. )
  107. ;;;###autoload(defvar fixme-mode nil)
  108. (define-minor-mode fixme-mode
  109. "A minor mode for making FIXME and other warnings stand out"
  110. nil
  111. " Fixme"
  112. nil
  113. :global
  114. :group fixme-mode
  115. :version fixme-mode-version
  116. (if fixme-mode
  117. (fixme-reload-keywords)
  118. (fixme-remove-keywords))
  119. )
  120. (provide 'fixme-mode)