Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

313 line
12 KiB

5 年之前
  1. ;;; writegood-mode.el --- Polish up poor writing on the fly
  2. ;;
  3. ;; Author: Benjamin Beckwith
  4. ;; Created: 2010-8-12
  5. ;; Version: 2.0
  6. ;; Package-Version: 20180525.1343
  7. ;; Last-Updated: 2015-03-25
  8. ;; URL: http://github.com/bnbeckwith/writegood-mode
  9. ;; Keywords: writing weasel-words grammar
  10. ;; Compatability:
  11. ;;
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ;;
  14. ;;; Commentary:
  15. ;;
  16. ;; This minor mode tries to find and highlight problems with your
  17. ;; writing (in english).
  18. ;;
  19. ;; Behavior inspired by the weaselwords scripts to aid in good
  20. ;; writing.
  21. ;; http://matt.might.net/articles/shell-scripts-for-passive-voice-weasel-words-duplicates/
  22. ;;
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Change Log:
  26. ;;
  27. ;; 2.0.3 Add in a small decription of the Flesch-Kincaid score
  28. ;; 2.0.2 Fix Formatting in Org-mode files, make faces underline
  29. ;; 2.0.1 Make user additions to word lists dynamic
  30. ;; 2.0.0 Flesch-Kincaid scoring added to functionality
  31. ;; 1.3.0 Several pull requests added, comments checked, passive voice regexp fixed
  32. ;; 1.2.0 Fixed weasel-words regexp to have word boundaries
  33. ;; 1.1.0 Fixed regexps to be multiline.
  34. ;; 1.0.0 Initial version
  35. ;;
  36. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  37. ;;
  38. ;; This program is free software; you can redistribute it and/or
  39. ;; modify it under the terms of the GNU General Public License as
  40. ;; published by the Free Software Foundation; either version 3, or
  41. ;; (at your option) any later version.
  42. ;;
  43. ;; This program is distributed in the hope that it will be useful,
  44. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  45. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  46. ;; General Public License for more details.
  47. ;;
  48. ;; You should have received a copy of the GNU General Public License
  49. ;; along with this program; see the file COPYING. If not, write to
  50. ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  51. ;; Floor, Boston, MA 02110-1301, USA.
  52. ;;
  53. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  54. ;;
  55. ;;; Test Text:
  56. ;;
  57. ;; This mode will improve various aspects of your writing in many ways.
  58. ;; With this mode, text within comments will be searched for the
  59. ;; the duplicate problem.
  60. ;; The text is searched and aspects (even within comments) are
  61. ;; highlighted.
  62. ;; Another benefit is the the finding of duplicates.
  63. ;;
  64. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  65. ;;
  66. ;;; Code:
  67. (eval-when-compile
  68. (require 'cl))
  69. (require 'regexp-opt)
  70. (require 'faces)
  71. (defgroup writegood nil
  72. "Minor mode for highlighting bad english writing."
  73. :prefix "writegood-"
  74. :group 'help
  75. :link '(url-link "http://github.com/bnbeckwith/writegood-mode"))
  76. (defconst writegood-version "2.0"
  77. "WriteGood mode version")
  78. ;; Weaselwords
  79. (defface writegood-weasels-face
  80. '((((supports :underline (:style wave)))
  81. :underline (:style wave :color "DarkOrange"))
  82. (((class color) (background light))
  83. (:inherit font-lock-warning-face :background "moccasin"))
  84. (((class color) (background dark))
  85. (:inherit font-lock-warning-face :background "DarkOrange")))
  86. "Writegood face for weasel words"
  87. :group 'writegood)
  88. (defcustom writegood-weasel-words
  89. '("many" "various" "very" "fairly" "several" "extremely"
  90. "exceedingly" "quite" "remarkably" "few" "surprisingly"
  91. "mostly" "largely" "huge" "tiny" "are a number" "is a number"
  92. "excellent" "interestingly" "significantly" "substantially"
  93. "clearly" "vast" "relatively" "completely" "literally"
  94. "not rocket science" "outside the box")
  95. "The weasel words to use"
  96. :group 'writegood
  97. :type '(repeat string))
  98. (defun writegood-weasels-font-lock-keywords-regexp ()
  99. "Generate regex that matches weasel-words"
  100. (concat "\\b" (regexp-opt writegood-weasel-words) "\\b"))
  101. (defun writegood-weasels-font-lock-keywords ()
  102. (list (list (writegood-weasels-font-lock-keywords-regexp)
  103. 0 (quote 'writegood-weasels-face) 'prepend)))
  104. ;; Passive Voice
  105. (defface writegood-passive-voice-face
  106. '((((supports :underline (:style wave)))
  107. :underline (:style wave :color "cyan"))
  108. (((class color))
  109. (:inherit font-lock-warning-face :background "LemonChiffon")))
  110. "Writegood face for passive-voice"
  111. :group 'writegood)
  112. (defcustom writegood-passive-voice-irregulars
  113. '("awoken" "been" "born" "beat" "become" "begun" "bent" "beset"
  114. "bet" "bid" "bidden" "bound" "bitten" "bled" "blown" "broken"
  115. "bred" "brought" "broadcast" "built" "burnt" "burst" "bought"
  116. "cast" "caught" "chosen" "clung" "come" "cost" "crept" "cut"
  117. "dealt" "dug" "dived" "done" "drawn" "dreamt" "driven" "drunk"
  118. "eaten" "fallen" "fed" "felt" "fought" "found" "fit" "fled"
  119. "flung" "flown" "forbidden" "forgotten" "foregone" "forgiven"
  120. "forsaken" "frozen" "gotten" "given" "gone" "ground" "grown"
  121. "hung" "heard" "hidden" "hit" "held" "hurt" "kept" "knelt" "knit"
  122. "known" "laid" "led" "leapt" "learnt" "left" "lent" "let" "lain"
  123. "lighted" "lost" "made" "meant" "met" "misspelt" "mistaken" "mown"
  124. "overcome" "overdone" "overtaken" "overthrown" "paid" "pled" "proven"
  125. "put" "quit" "read" "rid" "ridden" "rung" "risen" "run" "sawn"
  126. "said" "seen" "sought" "sold" "sent" "set" "sewn" "shaken" "shaven"
  127. "shorn" "shed" "shone" "shod" "shot" "shown" "shrunk" "shut"
  128. "sung" "sunk" "sat" "slept" "slain" "slid" "slung" "slit"
  129. "smitten" "sown" "spoken" "sped" "spent" "spilt" "spun" "spit"
  130. "split" "spread" "sprung" "stood" "stolen" "stuck" "stung"
  131. "stunk" "stridden" "struck" "strung" "striven" "sworn" "swept"
  132. "swollen" "swum" "swung" "taken" "taught" "torn" "told" "thought"
  133. "thrived" "thrown" "thrust" "trodden" "understood" "upheld" "upset"
  134. "woken" "worn" "woven" "wed" "wept" "wound" "won" "withheld"
  135. "withstood" "wrung" "written")
  136. "List of passive voice irregular verbs"
  137. :group 'writegood
  138. :type '(repeat string))
  139. (defcustom writegood-sentence-punctuation
  140. '(?. ?? ?!)
  141. "List of punctuation denoting sentence end"
  142. :group 'writegood
  143. :type '(repeat character))
  144. (defun writegood-passive-voice-font-lock-keywords-regexp ()
  145. "Generate font-lock keywords regexp for passive-voice"
  146. (concat "\\b\\(am\\|are\\|were\\|being\\|is\\|been\\|was\\|be\\)\\b\\([[:space:]]\\|\\s<\\|\\s>\\)+\\([[:word:]]+ed\\|"
  147. (regexp-opt writegood-passive-voice-irregulars)
  148. "\\)\\b"))
  149. (defun writegood-passive-voice-font-lock-keywords ()
  150. (list (list (writegood-passive-voice-font-lock-keywords-regexp)
  151. 0 (quote 'writegood-passive-voice-face) 'prepend)))
  152. ;; Duplicates
  153. (defface writegood-duplicates-face
  154. '((((supports :underline (:style wave)))
  155. :underline (:style wave :color "DeepPink"))
  156. (((class color) (background light))
  157. (:inherit font-lock-warning-face :background "MistyRose"))
  158. (((class color) (background dark))
  159. (:inherit font-lock-warning-face :background "DeepPink")))
  160. "Writegood face for duplicate words"
  161. :group 'writegood)
  162. (defvar writegood-duplicates-font-lock-keywords-regexp
  163. "\\b\\([[:word:]]+\\)\\([[:space:]]\\|\\s<\\|\\s>\\)+\\1\\b"
  164. "Font-lock keywords for duplicates")
  165. (defun writegood-duplicates-font-lock-keywords ()
  166. (list (list writegood-duplicates-font-lock-keywords-regexp
  167. 0 (quote 'writegood-duplicates-face) 'prepend)))
  168. ;;;;;;;;;;;;;;;;;;;; Functions:
  169. (defun writegood-version ()
  170. "Tell the version you are using"
  171. (interactive)
  172. (message writegood-version))
  173. (defun writegood-weasels-turn-on ()
  174. "Turn on syntax highlighting for weasels"
  175. (font-lock-add-keywords nil (writegood-weasels-font-lock-keywords) t))
  176. (defun writegood-passive-voice-turn-on ()
  177. "Turn on warnings for passive voice"
  178. (font-lock-add-keywords nil (writegood-passive-voice-font-lock-keywords) t))
  179. (defun writegood-duplicates-turn-on ()
  180. "Turn on warnings for duplicate words"
  181. (font-lock-add-keywords nil (writegood-duplicates-font-lock-keywords) t))
  182. (defun writegood-weasels-turn-off ()
  183. "Turn on syntax highlighting for weasels"
  184. (font-lock-remove-keywords nil (writegood-weasels-font-lock-keywords)))
  185. (defun writegood-passive-voice-turn-off ()
  186. "Turn on warnings for passive voice"
  187. (font-lock-remove-keywords nil (writegood-passive-voice-font-lock-keywords)))
  188. (defun writegood-duplicates-turn-off ()
  189. "Turn on warnings for duplicate words"
  190. (font-lock-remove-keywords nil (writegood-duplicates-font-lock-keywords)))
  191. (defun writegood-turn-on ()
  192. "Turn on writegood-mode."
  193. (make-local-variable 'font-lock-keywords-case-fold-search)
  194. (setq font-lock-keywords-case-fold-search t)
  195. (writegood-weasels-turn-on)
  196. (writegood-passive-voice-turn-on)
  197. (writegood-duplicates-turn-on))
  198. (defun writegood-turn-off ()
  199. "Turn off writegood-mode."
  200. (writegood-weasels-turn-off)
  201. (writegood-passive-voice-turn-off)
  202. (writegood-duplicates-turn-off))
  203. (defun writegood-count-words (rstart rend)
  204. "Count the words specified by the region bounded by RSTART and REND."
  205. (if (boundp 'count-words)
  206. (count-words rstart rend)
  207. (how-many "[[:word:]]+" rstart rend)))
  208. (defun writegood-count-sentences (rstart rend)
  209. "Count the sentences specified by the region bounded by RSTART and REND."
  210. (how-many (regexp-opt-charset writegood-sentence-punctuation) rstart rend))
  211. (defun writegood-count-syllables (rstart rend)
  212. "Count the (approximate) number of syllables in the region bounded by RSTART and REND.
  213. Consecutive vowels count as one syllable. The endings -es -ed
  214. and -e are not counted as syllables.
  215. "
  216. (- (how-many "[aeiouy]+" rstart rend)
  217. (how-many "\\(es\\|ed\\|e\\)\\b" rstart rend)))
  218. (defun writegood-fk-parameters (&optional rstart rend)
  219. "Flesch-Kincaid reading parameters"
  220. (let* ((start (cond (rstart rstart)
  221. ((and transient-mark-mode mark-active) (region-beginning))
  222. ('t (point-min))))
  223. (end (cond (rend rend)
  224. ((and transient-mark-mode mark-active) (region-end))
  225. ('t (point-max))))
  226. (words (float (writegood-count-words start end)))
  227. (syllables (float (writegood-count-syllables start end)))
  228. (sentences (float (writegood-count-sentences start end))))
  229. (list sentences words syllables)))
  230. (defun writegood-reading-ease-score->comment (score)
  231. "Rough interpreation of the Flesch-Kincaid Reading ease SCORE.
  232. From Wikipedia URL `https://en.wikipedia.org/wiki/FleschKincaid_readability_tests'."
  233. (cond
  234. ((< score 0) "Ouch! (Proust literature)")
  235. ((and (<= 0 score) (< score 30.0)) "Very difficult (college graduate)")
  236. ((and (<= 30.0 score) (< score 50.0)) "Difficult (almost college)")
  237. ((and (<= 50.0 score) (< score 60.0)) "Fairly difficult (10-12th grade)")
  238. ((and (<= 60.0 score) (< score 70.0)) "Plain English (8-9th grade)")
  239. ((and (<= 70.0 score) (< score 80.0)) "Fairly easy (7th grade)")
  240. ((and (<= 80.0 score) (< score 90.0)) "Easy (6th grade)")
  241. ((<= 90.0 score) "Very easy (5th grade)")))
  242. ;;;###autoload
  243. (defun writegood-reading-ease (&optional start end)
  244. "Flesch-Kincaid reading ease test in the region bounded by START and END.
  245. Scores roughly between 0 and 100."
  246. (interactive)
  247. (let* ((params (writegood-fk-parameters start end))
  248. (sentences (nth 0 params))
  249. (words (nth 1 params))
  250. (syllables (nth 2 params))
  251. (score (- 206.835 (* 1.015 (/ words sentences)) (* 84.6 (/ syllables words)))))
  252. (message "Flesch-Kincaid reading ease score: %.2f %s" score
  253. (writegood-reading-ease-score->comment score))))
  254. ;;;###autoload
  255. (defun writegood-grade-level (&optional start end)
  256. "Flesch-Kincaid grade level test. Converts reading ease score to a grade level (Score ~ years of school needed to read passage)."
  257. (interactive)
  258. (let* ((params (writegood-fk-parameters start end))
  259. (sentences (nth 0 params))
  260. (words (nth 1 params))
  261. (syllables (nth 2 params))
  262. (score (+ (* 0.39 (/ words sentences)) (* 11.8 (/ syllables words)) -15.59)))
  263. (message "Flesch-Kincaid grade level score: %.2f" score)))
  264. ;;;###autoload
  265. (define-minor-mode writegood-mode
  266. "Colorize issues with the writing in the buffer."
  267. :lighter " Wg"
  268. (progn
  269. (if writegood-mode
  270. (writegood-turn-on)
  271. (writegood-turn-off))
  272. (font-lock-mode 1)))
  273. (provide 'writegood-mode)
  274. ;;; writegood-mode.el ends here