org-lint.el 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. ;;; org-lint.el --- Linting for Org documents -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015-2016 Free Software Foundation
  3. ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This library implements linting for Org syntax. The sole public
  17. ;; function is `org-lint', which see.
  18. ;; Internally, the library defines a new structure:
  19. ;; `org-lint-checker', with the following slots:
  20. ;; - NAME: Unique check identifier, as a non-nil symbol that doesn't
  21. ;; start with an hyphen.
  22. ;;
  23. ;; The check is done calling the function `org-lint-NAME' with one
  24. ;; mandatory argument, the parse tree describing the current Org
  25. ;; buffer. Such function calls are wrapped within
  26. ;; a `save-excursion' and point is always at `point-min'. Its
  27. ;; return value has to be an alist (POSITION MESSAGE) when
  28. ;; POSITION refer to the buffer position of the error, as an
  29. ;; integer, and MESSAGE is a string describing the error.
  30. ;; - DESCRIPTION: Summary about the check, as a string.
  31. ;; - CATEGORIES: Categories relative to the check, as a list of
  32. ;; symbol. They are used for filtering when calling `org-lint'.
  33. ;; Checkers not explicitly associated to a category are collected
  34. ;; in the `default' one.
  35. ;; - TRUST: The trust level one can have in the check. It is either
  36. ;; `low' or `high', depending on the heuristics implemented and
  37. ;; the nature of the check. This has an indicative value only and
  38. ;; is displayed along reports.
  39. ;; All checks have to be listed in `org-lint--checkers'.
  40. ;; Results are displayed in a special "*Org Lint*" buffer with
  41. ;; a dedicated major mode, derived from `tabulated-list-mode'.
  42. ;;
  43. ;; In addition to the usual key-bindings inherited from it, "C-j" and
  44. ;; "TAB" display problematic line reported under point whereas "RET"
  45. ;; jumps to it. Also, "h" hides all reports similar to the current
  46. ;; one. Additionally, "i" removes them from subsequent reports.
  47. ;; Checks currently implemented are:
  48. ;; - duplicate CUSTOM_ID properties
  49. ;; - duplicate NAME values
  50. ;; - duplicate targets
  51. ;; - duplicate footnote definitions
  52. ;; - orphaned affiliated keywords
  53. ;; - obsolete affiliated keywords
  54. ;; - missing language in src blocks
  55. ;; - missing back-end in export blocks
  56. ;; - invalid Babel call blocks
  57. ;; - NAME values with a colon
  58. ;; - deprecated export block syntax
  59. ;; - deprecated Babel header properties
  60. ;; - wrong header arguments in src blocks
  61. ;; - misuse of CATEGORY keyword
  62. ;; - "coderef" links with unknown destination
  63. ;; - "custom-id" links with unknown destination
  64. ;; - "fuzzy" links with unknown destination
  65. ;; - "id" links with unknown destination
  66. ;; - links to non-existent local files
  67. ;; - SETUPFILE keywords with non-existent file parameter
  68. ;; - INCLUDE keywords with wrong link parameter
  69. ;; - obsolete markup in INCLUDE keyword
  70. ;; - unknown items in OPTIONS keyword
  71. ;; - spurious macro arguments or invalid macro templates
  72. ;; - special properties in properties drawer
  73. ;; - obsolete syntax for PROPERTIES drawers
  74. ;; - missing definition for footnote references
  75. ;; - missing reference for footnote definitions
  76. ;; - non-footnote definitions in footnote section
  77. ;; - probable invalid keywords
  78. ;; - invalid blocks
  79. ;; - misplaced planning info line
  80. ;; - incomplete drawers
  81. ;; - indented diary-sexps
  82. ;; - obsolete QUOTE section
  83. ;; - obsolete "file+application" link
  84. ;;; Code:
  85. (require 'cl-lib)
  86. (require 'org-element)
  87. (require 'org-macro)
  88. (require 'ox)
  89. (require 'ob)
  90. ;;; Checkers
  91. (cl-defstruct (org-lint-checker (:copier nil))
  92. (name 'missing-checker-name)
  93. (description "")
  94. (categories '(default))
  95. (trust 'high)) ; `low' or `high'
  96. (defun org-lint-missing-checker-name (_)
  97. (error
  98. "`A checker has no `:name' property. Please verify `org-lint--checkers'"))
  99. (defconst org-lint--checkers
  100. (list
  101. (make-org-lint-checker
  102. :name 'duplicate-custom-id
  103. :description "Report duplicates CUSTOM_ID properties"
  104. :categories '(link))
  105. (make-org-lint-checker
  106. :name 'duplicate-name
  107. :description "Report duplicate NAME values"
  108. :categories '(babel link))
  109. (make-org-lint-checker
  110. :name 'duplicate-target
  111. :description "Report duplicate targets"
  112. :categories '(link))
  113. (make-org-lint-checker
  114. :name 'duplicate-footnote-definition
  115. :description "Report duplicate footnote definitions"
  116. :categories '(footnote))
  117. (make-org-lint-checker
  118. :name 'orphaned-affiliated-keywords
  119. :description "Report orphaned affiliated keywords"
  120. :trust 'low)
  121. (make-org-lint-checker
  122. :name 'obsolete-affiliated-keywords
  123. :description "Report obsolete affiliated keywords"
  124. :categories '(obsolete))
  125. (make-org-lint-checker
  126. :name 'deprecated-export-blocks
  127. :description "Report deprecated export block syntax"
  128. :categories '(obsolete export)
  129. :trust 'low)
  130. (make-org-lint-checker
  131. :name 'deprecated-header-syntax
  132. :description "Report deprecated Babel header syntax"
  133. :categories '(obsolete babel)
  134. :trust 'low)
  135. (make-org-lint-checker
  136. :name 'missing-language-in-src-block
  137. :description "Report missing language in src blocks"
  138. :categories '(babel))
  139. (make-org-lint-checker
  140. :name 'missing-backend-in-export-block
  141. :description "Report missing back-end in export blocks"
  142. :categories '(export))
  143. (make-org-lint-checker
  144. :name 'invalid-babel-call-block
  145. :description "Report invalid Babel call blocks"
  146. :categories '(babel))
  147. (make-org-lint-checker
  148. :name 'colon-in-name
  149. :description "Report NAME values with a colon"
  150. :categories '(babel))
  151. (make-org-lint-checker
  152. :name 'wrong-header-argument
  153. :description "Report wrong babel headers"
  154. :categories '(babel))
  155. (make-org-lint-checker
  156. :name 'wrong-header-value
  157. :description "Report invalid value in babel headers"
  158. :categories '(babel)
  159. :trust 'low)
  160. (make-org-lint-checker
  161. :name 'deprecated-category-setup
  162. :description "Report misuse of CATEGORY keyword"
  163. :categories '(obsolete))
  164. (make-org-lint-checker
  165. :name 'invalid-coderef-link
  166. :description "Report \"coderef\" links with unknown destination"
  167. :categories '(link))
  168. (make-org-lint-checker
  169. :name 'invalid-custom-id-link
  170. :description "Report \"custom-id\" links with unknown destination"
  171. :categories '(link))
  172. (make-org-lint-checker
  173. :name 'invalid-fuzzy-link
  174. :description "Report \"fuzzy\" links with unknown destination"
  175. :categories '(link))
  176. (make-org-lint-checker
  177. :name 'invalid-id-link
  178. :description "Report \"id\" links with unknown destination"
  179. :categories '(link))
  180. (make-org-lint-checker
  181. :name 'link-to-local-file
  182. :description "Report links to non-existent local files"
  183. :categories '(link)
  184. :trust 'low)
  185. (make-org-lint-checker
  186. :name 'non-existent-setupfile-parameter
  187. :description "Report SETUPFILE keywords with non-existent file parameter"
  188. :trust 'low)
  189. (make-org-lint-checker
  190. :name 'wrong-include-link-parameter
  191. :description "Report INCLUDE keywords with misleading link parameter"
  192. :categories '(export)
  193. :trust 'low)
  194. (make-org-lint-checker
  195. :name 'obsolete-include-markup
  196. :description "Report obsolete markup in INCLUDE keyword"
  197. :categories '(obsolete export)
  198. :trust 'low)
  199. (make-org-lint-checker
  200. :name 'unknown-options-item
  201. :description "Report unknown items in OPTIONS keyword"
  202. :categories '(export)
  203. :trust 'low)
  204. (make-org-lint-checker
  205. :name 'invalid-macro-argument-and-template
  206. :description "Report spurious macro arguments or invalid macro templates"
  207. :categories '(export)
  208. :trust 'low)
  209. (make-org-lint-checker
  210. :name 'special-property-in-properties-drawer
  211. :description "Report special properties in properties drawers"
  212. :categories '(properties))
  213. (make-org-lint-checker
  214. :name 'obsolete-properties-drawer
  215. :description "Report obsolete syntax for properties drawers"
  216. :categories '(obsolete properties))
  217. (make-org-lint-checker
  218. :name 'undefined-footnote-reference
  219. :description "Report missing definition for footnote references"
  220. :categories '(footnote))
  221. (make-org-lint-checker
  222. :name 'unreferenced-footnote-definition
  223. :description "Report missing reference for footnote definitions"
  224. :categories '(footnote))
  225. (make-org-lint-checker
  226. :name 'extraneous-element-in-footnote-section
  227. :description "Report non-footnote definitions in footnote section"
  228. :categories '(footnote))
  229. (make-org-lint-checker
  230. :name 'invalid-keyword-syntax
  231. :description "Report probable invalid keywords"
  232. :trust 'low)
  233. (make-org-lint-checker
  234. :name 'invalid-block
  235. :description "Report invalid blocks"
  236. :trust 'low)
  237. (make-org-lint-checker
  238. :name 'misplaced-planning-info
  239. :description "Report misplaced planning info line"
  240. :trust 'low)
  241. (make-org-lint-checker
  242. :name 'incomplete-drawer
  243. :description "Report probable incomplete drawers"
  244. :trust 'low)
  245. (make-org-lint-checker
  246. :name 'indented-diary-sexp
  247. :description "Report probable indented diary-sexps"
  248. :trust 'low)
  249. (make-org-lint-checker
  250. :name 'quote-section
  251. :description "Report obsolete QUOTE section"
  252. :categories '(obsolete)
  253. :trust 'low)
  254. (make-org-lint-checker
  255. :name 'file-application
  256. :description "Report obsolete \"file+application\" link"
  257. :categories '(link obsolete)))
  258. "List of all available checkers.")
  259. (defun org-lint--collect-duplicates
  260. (ast type extract-key extract-position build-message)
  261. "Helper function to collect duplicates in parse tree AST.
  262. EXTRACT-KEY is a function extracting key. It is called with
  263. a single argument: the element or object. Comparison is done
  264. with `equal'.
  265. EXTRACT-POSITION is a function returning position for the report.
  266. It is called with two arguments, the object or element, and the
  267. key.
  268. BUILD-MESSAGE is a function creating the report message. It is
  269. called with one argument, the key used for comparison."
  270. (let* (keys
  271. originals
  272. reports
  273. (make-report
  274. (lambda (position value)
  275. (push (list position (funcall build-message value)) reports))))
  276. (org-element-map ast type
  277. (lambda (datum)
  278. (let ((key (funcall extract-key datum)))
  279. (cond
  280. ((not key))
  281. ((assoc key keys) (cl-pushnew (assoc key keys) originals)
  282. (funcall make-report (funcall extract-position datum key) key))
  283. (t (push (cons key (funcall extract-position datum key)) keys))))))
  284. (dolist (e originals reports) (funcall make-report (cdr e) (car e)))))
  285. (defun org-lint-duplicate-custom-id (ast)
  286. (org-lint--collect-duplicates
  287. ast
  288. 'node-property
  289. (lambda (property)
  290. (and (eq (compare-strings "CUSTOM_ID" nil nil
  291. (org-element-property :key property) nil nil
  292. t)
  293. t)
  294. (org-element-property :value property)))
  295. (lambda (property _) (org-element-property :begin property))
  296. (lambda (key) (format "Duplicate CUSTOM_ID property \"%s\"" key))))
  297. (defun org-lint-duplicate-name (ast)
  298. (org-lint--collect-duplicates
  299. ast
  300. org-element-all-elements
  301. (lambda (datum) (org-element-property :name datum))
  302. (lambda (datum name)
  303. (goto-char (org-element-property :begin datum))
  304. (re-search-forward
  305. (format "^[ \t]*#\\+[A-Za-z]+: +%s *$" (regexp-quote name)))
  306. (match-beginning 0))
  307. (lambda (key) (format "Duplicate NAME \"%s\"" key))))
  308. (defun org-lint-duplicate-target (ast)
  309. (org-lint--collect-duplicates
  310. ast
  311. 'target
  312. (lambda (target) (org-split-string (org-element-property :value target)))
  313. (lambda (target _) (org-element-property :begin target))
  314. (lambda (key)
  315. (format "Duplicate target <<%s>>" (mapconcat #'identity key " ")))))
  316. (defun org-lint-duplicate-footnote-definition (ast)
  317. (org-lint--collect-duplicates
  318. ast
  319. 'footnote-definition
  320. (lambda (definition) (org-element-property :label definition))
  321. (lambda (definition _) (org-element-property :post-affiliated definition))
  322. (lambda (key) (format "Duplicate footnote definition \"%s\"" key))))
  323. (defun org-lint-orphaned-affiliated-keywords (ast)
  324. ;; Ignore orphan RESULTS keywords, which could be generated from
  325. ;; a source block returning no value.
  326. (let ((keywords (cl-set-difference org-element-affiliated-keywords
  327. '("RESULT" "RESULTS")
  328. :test #'equal)))
  329. (org-element-map ast 'keyword
  330. (lambda (k)
  331. (let ((key (org-element-property :key k)))
  332. (and (or (let ((case-fold-search t))
  333. (string-match-p "\\`ATTR_[-_A-Za-z0-9]+\\'" key))
  334. (member key keywords))
  335. (list (org-element-property :post-affiliated k)
  336. (format "Orphaned affiliated keyword: \"%s\"" key))))))))
  337. (defun org-lint-obsolete-affiliated-keywords (_)
  338. (let ((regexp (format "^[ \t]*#\\+%s:"
  339. (regexp-opt '("DATA" "LABEL" "RESNAME" "SOURCE"
  340. "SRCNAME" "TBLNAME" "RESULT" "HEADERS")
  341. t)))
  342. reports)
  343. (while (re-search-forward regexp nil t)
  344. (let ((key (upcase (match-string-no-properties 1))))
  345. (when (< (point)
  346. (org-element-property :post-affiliated (org-element-at-point)))
  347. (push
  348. (list (line-beginning-position)
  349. (format
  350. "Obsolete affiliated keyword: \"%s\". Use \"%s\" instead"
  351. key
  352. (pcase key
  353. ("HEADERS" "HEADER")
  354. ("RESULT" "RESULTS")
  355. (_ "NAME"))))
  356. reports))))
  357. reports))
  358. (defun org-lint-deprecated-export-blocks (ast)
  359. (let ((deprecated '("ASCII" "BEAMER" "HTML" "LATEX" "MAN" "MARKDOWN" "MD"
  360. "ODT" "ORG" "TEXINFO")))
  361. (org-element-map ast 'special-block
  362. (lambda (b)
  363. (let ((type (org-element-property :type b)))
  364. (when (member-ignore-case type deprecated)
  365. (list
  366. (org-element-property :post-affiliated b)
  367. (format
  368. "Deprecated syntax for export block. Use \"BEGIN_EXPORT %s\" \
  369. instead"
  370. type))))))))
  371. (defun org-lint-deprecated-header-syntax (ast)
  372. (let* ((deprecated-babel-properties
  373. (mapcar (lambda (arg) (symbol-name (car arg)))
  374. org-babel-common-header-args-w-values))
  375. (deprecated-re
  376. (format "\\`%s[ \t]" (regexp-opt deprecated-babel-properties t))))
  377. (org-element-map ast '(keyword node-property)
  378. (lambda (datum)
  379. (let ((key (org-element-property :key datum)))
  380. (pcase (org-element-type datum)
  381. (`keyword
  382. (let ((value (org-element-property :value datum)))
  383. (and (string= key "PROPERTY")
  384. (string-match deprecated-re value)
  385. (list (org-element-property :begin datum)
  386. (format "Deprecated syntax for \"%s\". \
  387. Use header-args instead"
  388. (match-string-no-properties 1 value))))))
  389. (`node-property
  390. (and (member-ignore-case key deprecated-babel-properties)
  391. (list
  392. (org-element-property :begin datum)
  393. (format "Deprecated syntax for \"%s\". \
  394. Use :header-args: instead"
  395. key))))))))))
  396. (defun org-lint-missing-language-in-src-block (ast)
  397. (org-element-map ast 'src-block
  398. (lambda (b)
  399. (unless (org-element-property :language b)
  400. (list (org-element-property :post-affiliated b)
  401. "Missing language in source block")))))
  402. (defun org-lint-missing-backend-in-export-block (ast)
  403. (org-element-map ast 'export-block
  404. (lambda (b)
  405. (unless (org-element-property :type b)
  406. (list (org-element-property :post-affiliated b)
  407. "Missing back-end in export block")))))
  408. (defun org-lint-invalid-babel-call-block (ast)
  409. (org-element-map ast 'babel-call
  410. (lambda (b)
  411. (cond
  412. ((not (org-element-property :call b))
  413. (list (org-element-property :post-affiliated b)
  414. "Invalid syntax in babel call block"))
  415. ((let ((h (org-element-property :end-header b)))
  416. (and h (string-match-p "\\`\\[.*\\]\\'" h)))
  417. (list
  418. (org-element-property :post-affiliated b)
  419. "Babel call's end header must not be wrapped within brackets"))))))
  420. (defun org-lint-deprecated-category-setup (ast)
  421. (org-element-map ast 'keyword
  422. (let (category-flag)
  423. (lambda (k)
  424. (cond
  425. ((not (string= (org-element-property :key k) "CATEGORY")) nil)
  426. (category-flag
  427. (list (org-element-property :post-affiliated k)
  428. "Spurious CATEGORY keyword. Set :CATEGORY: property instead"))
  429. (t (setf category-flag t) nil))))))
  430. (defun org-lint-invalid-coderef-link (ast)
  431. (let ((info (list :parse-tree ast)))
  432. (org-element-map ast 'link
  433. (lambda (link)
  434. (let ((ref (org-element-property :path link)))
  435. (and (equal (org-element-property :type link) "coderef")
  436. (not (ignore-errors (org-export-resolve-coderef ref info)))
  437. (list (org-element-property :begin link)
  438. (format "Unknown coderef \"%s\"" ref))))))))
  439. (defun org-lint-invalid-custom-id-link (ast)
  440. (let ((info (list :parse-tree ast)))
  441. (org-element-map ast 'link
  442. (lambda (link)
  443. (and (equal (org-element-property :type link) "custom-id")
  444. (not (ignore-errors (org-export-resolve-id-link link info)))
  445. (list (org-element-property :begin link)
  446. (format "Unknown custom ID \"%s\""
  447. (org-element-property :path link))))))))
  448. (defun org-lint-invalid-fuzzy-link (ast)
  449. (let ((info (list :parse-tree ast)))
  450. (org-element-map ast 'link
  451. (lambda (link)
  452. (and (equal (org-element-property :type link) "fuzzy")
  453. (not (ignore-errors (org-export-resolve-fuzzy-link link info)))
  454. (list (org-element-property :begin link)
  455. (format "Unknown fuzzy location \"%s\""
  456. (let ((path (org-element-property :path link)))
  457. (if (string-prefix-p "*" path)
  458. (substring path 1)
  459. path)))))))))
  460. (defun org-lint-invalid-id-link (ast)
  461. (org-element-map ast 'link
  462. (lambda (link)
  463. (let ((id (org-element-property :path link)))
  464. (and (equal (org-element-property :type link) "id")
  465. (not (org-id-find id))
  466. (list (org-element-property :begin link)
  467. (format "Unknown ID \"%s\"" id)))))))
  468. (defun org-lint-special-property-in-properties-drawer (ast)
  469. (org-element-map ast 'node-property
  470. (lambda (p)
  471. (let ((key (org-element-property :key p)))
  472. (and (member-ignore-case key org-special-properties)
  473. (list (org-element-property :begin p)
  474. (format
  475. "Special property \"%s\" found in a properties drawer"
  476. key)))))))
  477. (defun org-lint-obsolete-properties-drawer (ast)
  478. (org-element-map ast 'drawer
  479. (lambda (d)
  480. (when (equal (org-element-property :drawer-name d) "PROPERTIES")
  481. (let ((section (org-element-lineage d '(section))))
  482. (unless (org-element-map section 'property-drawer #'identity nil t)
  483. (list (org-element-property :post-affiliated d)
  484. (if (save-excursion
  485. (goto-char (org-element-property :post-affiliated d))
  486. (forward-line -1)
  487. (or (org-at-heading-p) (org-at-planning-p)))
  488. "Incorrect contents for PROPERTIES drawer"
  489. "Incorrect location for PROPERTIES drawer"))))))))
  490. (defun org-lint-link-to-local-file (ast)
  491. (org-element-map ast 'link
  492. (lambda (l)
  493. (when (equal (org-element-property :type l) "file")
  494. (let ((file (org-link-unescape (org-element-property :path l))))
  495. (and (not (file-remote-p file))
  496. (not (file-exists-p file))
  497. (list (org-element-property :begin l)
  498. (format (if (org-element-lineage l '(link))
  499. "Link to non-existent image file \"%s\"\
  500. in link description"
  501. "Link to non-existent local file \"%s\"")
  502. file))))))))
  503. (defun org-lint-non-existent-setupfile-parameter (ast)
  504. (org-element-map ast 'keyword
  505. (lambda (k)
  506. (when (equal (org-element-property :key k) "SETUPFILE")
  507. (let ((file (org-unbracket-string
  508. "\"" "\""
  509. (org-element-property :value k))))
  510. (and (not (file-remote-p file))
  511. (not (file-exists-p file))
  512. (list (org-element-property :begin k)
  513. (format "Non-existent setup file \"%s\"" file))))))))
  514. (defun org-lint-wrong-include-link-parameter (ast)
  515. (org-element-map ast 'keyword
  516. (lambda (k)
  517. (when (equal (org-element-property :key k) "INCLUDE")
  518. (let* ((value (org-element-property :value k))
  519. (path
  520. (and (string-match "^\\(\".+\"\\|\\S-+\\)[ \t]*" value)
  521. (save-match-data
  522. (org-unbracket-string "\"" "\"" (match-string 1 value))))))
  523. (if (not path)
  524. (list (org-element-property :post-affiliated k)
  525. "Missing location argument in INCLUDE keyword")
  526. (let* ((file (org-string-nw-p
  527. (if (string-match "::\\(.*\\)\\'" path)
  528. (substring path 0 (match-beginning 0))
  529. path)))
  530. (search (and (not (equal file path))
  531. (org-string-nw-p (match-string 1 path)))))
  532. (if (and file
  533. (not (file-remote-p file))
  534. (not (file-exists-p file)))
  535. (list (org-element-property :post-affiliated k)
  536. "Non-existent file argument in INCLUDE keyword")
  537. (let* ((visiting (if file (find-buffer-visiting file)
  538. (current-buffer)))
  539. (buffer (or visiting (find-file-noselect file))))
  540. (unwind-protect
  541. (with-current-buffer buffer
  542. (when (and search
  543. (not
  544. (ignore-errors
  545. (let ((org-link-search-inhibit-query t))
  546. (org-link-search search nil t)))))
  547. (list (org-element-property :post-affiliated k)
  548. (format
  549. "Invalid search part \"%s\" in INCLUDE keyword"
  550. search))))
  551. (unless visiting (kill-buffer buffer))))))))))))
  552. (defun org-lint-obsolete-include-markup (ast)
  553. (let ((regexp (format "\\`\\(?:\".+\"\\|\\S-+\\)[ \t]+%s"
  554. (regexp-opt
  555. '("ASCII" "BEAMER" "HTML" "LATEX" "MAN" "MARKDOWN" "MD"
  556. "ODT" "ORG" "TEXINFO")
  557. t))))
  558. (org-element-map ast 'keyword
  559. (lambda (k)
  560. (when (equal (org-element-property :key k) "INCLUDE")
  561. (let ((case-fold-search t)
  562. (value (org-element-property :value k)))
  563. (when (string-match regexp value)
  564. (let ((markup (match-string-no-properties 1 value)))
  565. (list (org-element-property :post-affiliated k)
  566. (format "Obsolete markup \"%s\" in INCLUDE keyword. \
  567. Use \"export %s\" instead"
  568. markup
  569. markup))))))))))
  570. (defun org-lint-unknown-options-item (ast)
  571. (let ((allowed (delq nil
  572. (append
  573. (mapcar (lambda (o) (nth 2 o)) org-export-options-alist)
  574. (cl-mapcan
  575. (lambda (b)
  576. (mapcar (lambda (o) (nth 2 o))
  577. (org-export-backend-options b)))
  578. org-export-registered-backends))))
  579. reports)
  580. (org-element-map ast 'keyword
  581. (lambda (k)
  582. (when (string= (org-element-property :key k) "OPTIONS")
  583. (let ((value (org-element-property :value k))
  584. (start 0))
  585. (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-*\\)[ \t]*"
  586. value
  587. start)
  588. (setf start (match-end 0))
  589. (let ((item (match-string 1 value)))
  590. (unless (member item allowed)
  591. (push (list (org-element-property :post-affiliated k)
  592. (format "Unknown OPTIONS item \"%s\"" item))
  593. reports))))))))
  594. reports))
  595. (defun org-lint-invalid-macro-argument-and-template (ast)
  596. (let ((extract-placeholders
  597. (lambda (template)
  598. (let ((start 0)
  599. args)
  600. (while (string-match "\\$\\([1-9][0-9]*\\)" template start)
  601. (setf start (match-end 0))
  602. (push (string-to-number (match-string 1 template)) args))
  603. (sort (org-uniquify args) #'<))))
  604. reports)
  605. ;; Check arguments for macro templates.
  606. (org-element-map ast 'keyword
  607. (lambda (k)
  608. (when (string= (org-element-property :key k) "MACRO")
  609. (let* ((value (org-element-property :value k))
  610. (name (and (string-match "^\\S-+" value)
  611. (match-string 0 value)))
  612. (template (and name
  613. (org-trim (substring value (match-end 0))))))
  614. (cond
  615. ((not name)
  616. (push (list (org-element-property :post-affiliated k)
  617. "Missing name in MACRO keyword")
  618. reports))
  619. ((not (org-string-nw-p template))
  620. (push (list (org-element-property :post-affiliated k)
  621. "Missing template in macro \"%s\"" name)
  622. reports))
  623. (t
  624. (unless (let ((args (funcall extract-placeholders template)))
  625. (equal (number-sequence 1 (or (org-last args) 0)) args))
  626. (push (list (org-element-property :post-affiliated k)
  627. (format "Unused placeholders in macro \"%s\""
  628. name))
  629. reports))))))))
  630. ;; Check arguments for macros.
  631. (org-macro-initialize-templates)
  632. (let ((templates (append
  633. (mapcar (lambda (m) (cons m "$1"))
  634. '("author" "date" "email" "title" "results"))
  635. org-macro-templates)))
  636. (org-element-map ast 'macro
  637. (lambda (macro)
  638. (let* ((name (org-element-property :key macro))
  639. (template (cdr (assoc-string name templates t))))
  640. (if (not template)
  641. (push (list (org-element-property :begin macro)
  642. (format "Undefined macro \"%s\"" name))
  643. reports)
  644. (let ((arg-numbers (funcall extract-placeholders template)))
  645. (when arg-numbers
  646. (let ((spurious-args
  647. (nthcdr (apply #'max arg-numbers)
  648. (org-element-property :args macro))))
  649. (when spurious-args
  650. (push
  651. (list (org-element-property :begin macro)
  652. (format "Unused argument%s in macro \"%s\": %s"
  653. (if (> (length spurious-args) 1) "s" "")
  654. name
  655. (mapconcat (lambda (a) (format "\"%s\"" a))
  656. spurious-args
  657. ", ")))
  658. reports))))))))))
  659. reports))
  660. (defun org-lint-undefined-footnote-reference (ast)
  661. (let ((definitions (org-element-map ast 'footnote-definition
  662. (lambda (f) (org-element-property :label f)))))
  663. (org-element-map ast 'footnote-reference
  664. (lambda (f)
  665. (let ((label (org-element-property :label f)))
  666. (and label
  667. (not (member label definitions))
  668. (list (org-element-property :begin f)
  669. (format "Missing definition for footnote [%s]"
  670. label))))))))
  671. (defun org-lint-unreferenced-footnote-definition (ast)
  672. (let ((references (org-element-map ast 'footnote-reference
  673. (lambda (f) (org-element-property :label f)))))
  674. (org-element-map ast 'footnote-definition
  675. (lambda (f)
  676. (let ((label (org-element-property :label f)))
  677. (and label
  678. (not (member label references))
  679. (list (org-element-property :post-affiliated f)
  680. (format "No reference for footnote definition [%s]"
  681. label))))))))
  682. (defun org-lint-colon-in-name (ast)
  683. (org-element-map ast org-element-all-elements
  684. (lambda (e)
  685. (let ((name (org-element-property :name e)))
  686. (and name
  687. (string-match-p ":" name)
  688. (list (progn
  689. (goto-char (org-element-property :begin e))
  690. (re-search-forward
  691. (format "^[ \t]*#\\+\\w+: +%s *$" (regexp-quote name)))
  692. (match-beginning 0))
  693. (format
  694. "Name \"%s\" contains a colon; Babel cannot use it as input"
  695. name)))))))
  696. (defun org-lint-misplaced-planning-info (_)
  697. (let ((case-fold-search t)
  698. reports)
  699. (while (re-search-forward org-planning-line-re nil t)
  700. (unless (memq (org-element-type (org-element-at-point))
  701. '(comment-block example-block export-block planning
  702. src-block verse-block))
  703. (push (list (line-beginning-position) "Misplaced planning info line")
  704. reports)))
  705. reports))
  706. (defun org-lint-incomplete-drawer (_)
  707. (let (reports)
  708. (while (re-search-forward org-drawer-regexp nil t)
  709. (let ((name (org-trim (match-string-no-properties 0)))
  710. (element (org-element-at-point)))
  711. (pcase (org-element-type element)
  712. ((or `drawer `property-drawer)
  713. (goto-char (org-element-property :end element))
  714. nil)
  715. ((or `comment-block `example-block `export-block `src-block
  716. `verse-block)
  717. nil)
  718. (_
  719. (push (list (line-beginning-position)
  720. (format "Possible incomplete drawer \"%s\"" name))
  721. reports)))))
  722. reports))
  723. (defun org-lint-indented-diary-sexp (_)
  724. (let (reports)
  725. (while (re-search-forward "^[ \t]+%%(" nil t)
  726. (unless (memq (org-element-type (org-element-at-point))
  727. '(comment-block diary-sexp example-block export-block
  728. src-block verse-block))
  729. (push (list (line-beginning-position) "Possible indented diary-sexp")
  730. reports)))
  731. reports))
  732. (defun org-lint-invalid-block (_)
  733. (let ((case-fold-search t)
  734. (regexp "^[ \t]*#\\+\\(BEGIN\\|END\\)\\(?::\\|_[^[:space:]]*\\)?[ \t]*")
  735. reports)
  736. (while (re-search-forward regexp nil t)
  737. (let ((name (org-trim (buffer-substring-no-properties
  738. (line-beginning-position) (line-end-position)))))
  739. (cond
  740. ((and (string-prefix-p "END" (match-string 1) t)
  741. (not (eolp)))
  742. (push (list (line-beginning-position)
  743. (format "Invalid block closing line \"%s\"" name))
  744. reports))
  745. ((not (memq (org-element-type (org-element-at-point))
  746. '(center-block comment-block dynamic-block example-block
  747. export-block quote-block special-block
  748. src-block verse-block)))
  749. (push (list (line-beginning-position)
  750. (format "Possible incomplete block \"%s\""
  751. name))
  752. reports)))))
  753. reports))
  754. (defun org-lint-invalid-keyword-syntax (_)
  755. (let ((regexp "^[ \t]*#\\+\\([^[:space:]:]*\\)\\(?: \\|$\\)")
  756. (exception-re
  757. (format "[ \t]*#\\+%s\\(\\[.*\\]\\)?:\\(?: \\|$\\)"
  758. (regexp-opt org-element-dual-keywords)))
  759. reports)
  760. (while (re-search-forward regexp nil t)
  761. (let ((name (match-string-no-properties 1)))
  762. (unless (or (string-prefix-p "BEGIN" name t)
  763. (string-prefix-p "END" name t)
  764. (save-excursion
  765. (beginning-of-line)
  766. (let ((case-fold-search t)) (looking-at exception-re))))
  767. (push (list (match-beginning 0)
  768. (format "Possible missing colon in keyword \"%s\"" name))
  769. reports))))
  770. reports))
  771. (defun org-lint-extraneous-element-in-footnote-section (ast)
  772. (org-element-map ast 'headline
  773. (lambda (h)
  774. (and (org-element-property :footnote-section-p h)
  775. (org-element-map (org-element-contents h)
  776. (cl-remove-if
  777. (lambda (e)
  778. (memq e '(comment comment-block footnote-definition
  779. property-drawer section)))
  780. org-element-all-elements)
  781. (lambda (e)
  782. (not (and (eq (org-element-type e) 'headline)
  783. (org-element-property :commentedp e))))
  784. nil t '(footnote-definition property-drawer))
  785. (list (org-element-property :begin h)
  786. "Extraneous elements in footnote section are not exported")))))
  787. (defun org-lint-quote-section (ast)
  788. (org-element-map ast '(headline inlinetask)
  789. (lambda (h)
  790. (let ((title (org-element-property :raw-value h)))
  791. (and (or (string-prefix-p "QUOTE " title)
  792. (string-prefix-p (concat org-comment-string " QUOTE ") title))
  793. (list (org-element-property :begin h)
  794. "Deprecated QUOTE section"))))))
  795. (defun org-lint-file-application (ast)
  796. (org-element-map ast 'link
  797. (lambda (l)
  798. (let ((app (org-element-property :application l)))
  799. (and app
  800. (list (org-element-property :begin l)
  801. (format "Deprecated \"file+%s\" link type" app)))))))
  802. (defun org-lint-wrong-header-argument (ast)
  803. (let* ((reports)
  804. (verify
  805. (lambda (datum language headers)
  806. (let ((allowed
  807. ;; If LANGUAGE is specified, restrict allowed
  808. ;; headers to both LANGUAGE-specific and default
  809. ;; ones. Otherwise, accept headers from any loaded
  810. ;; language.
  811. (append
  812. org-babel-header-arg-names
  813. (cl-mapcan
  814. (lambda (l)
  815. (let ((v (intern (format "org-babel-header-args:%s" l))))
  816. (and (boundp v) (mapcar #'car (symbol-value v)))))
  817. (if language (list language)
  818. (mapcar #'car org-babel-load-languages))))))
  819. (dolist (header headers)
  820. (let ((h (symbol-name (car header)))
  821. (p (or (org-element-property :post-affiliated datum)
  822. (org-element-property :begin datum))))
  823. (cond
  824. ((not (string-prefix-p ":" h))
  825. (push
  826. (list p
  827. (format "Missing colon in header argument \"%s\"" h))
  828. reports))
  829. ((assoc-string (substring h 1) allowed))
  830. (t (push (list p (format "Unknown header argument \"%s\"" h))
  831. reports)))))))))
  832. (org-element-map ast '(babel-call inline-babel-call inline-src-block keyword
  833. node-property src-block)
  834. (lambda (datum)
  835. (pcase (org-element-type datum)
  836. ((or `babel-call `inline-babel-call)
  837. (funcall verify
  838. datum
  839. nil
  840. (cl-mapcan #'org-babel-parse-header-arguments
  841. (list
  842. (org-element-property :inside-header datum)
  843. (org-element-property :end-header datum)))))
  844. (`inline-src-block
  845. (funcall verify
  846. datum
  847. (org-element-property :language datum)
  848. (org-babel-parse-header-arguments
  849. (org-element-property :parameters datum))))
  850. (`keyword
  851. (when (string= (org-element-property :key datum) "PROPERTY")
  852. (let ((value (org-element-property :value datum)))
  853. (when (string-match "\\`header-args\\(?::\\(\\S-+\\)\\)?\\+? *"
  854. value)
  855. (funcall verify
  856. datum
  857. (match-string 1 value)
  858. (org-babel-parse-header-arguments
  859. (substring value (match-end 0))))))))
  860. (`node-property
  861. (let ((key (org-element-property :key datum)))
  862. (when (let ((case-fold-search t))
  863. (string-match "\\`HEADER-ARGS\\(?::\\(\\S-+\\)\\)?\\+?"
  864. key))
  865. (funcall verify
  866. datum
  867. (match-string 1 key)
  868. (org-babel-parse-header-arguments
  869. (org-element-property :value datum))))))
  870. (`src-block
  871. (funcall verify
  872. datum
  873. (org-element-property :language datum)
  874. (cl-mapcan #'org-babel-parse-header-arguments
  875. (cons (org-element-property :parameters datum)
  876. (org-element-property :header datum))))))))
  877. reports))
  878. (defun org-lint-wrong-header-value (ast)
  879. (let (reports)
  880. (org-element-map ast
  881. '(babel-call inline-babel-call inline-src-block src-block)
  882. (lambda (datum)
  883. (let* ((type (org-element-type datum))
  884. (language (org-element-property :language datum))
  885. (allowed-header-values
  886. (append (and language
  887. (let ((v (intern (concat "org-babel-header-args:"
  888. language))))
  889. (and (boundp v) (symbol-value v))))
  890. org-babel-common-header-args-w-values))
  891. (datum-header-values
  892. (org-babel-parse-header-arguments
  893. (org-trim
  894. (pcase type
  895. (`src-block
  896. (mapconcat
  897. #'identity
  898. (cons (org-element-property :parameters datum)
  899. (org-element-property :header datum))
  900. " "))
  901. (`inline-src-block
  902. (or (org-element-property :parameters datum) ""))
  903. (_
  904. (concat
  905. (org-element-property :inside-header datum)
  906. " "
  907. (org-element-property :end-header datum))))))))
  908. (dolist (header datum-header-values)
  909. (let ((allowed-values
  910. (cdr (assoc-string (substring (symbol-name (car header)) 1)
  911. allowed-header-values))))
  912. (unless (memq allowed-values '(:any nil))
  913. (let ((values (cdr header))
  914. groups-alist)
  915. (dolist (v (if (stringp values) (org-split-string values)
  916. (list values)))
  917. (let ((valid-value nil))
  918. (catch 'exit
  919. (dolist (group allowed-values)
  920. (cond
  921. ((not (funcall
  922. (if (stringp v) #'assoc-string #'assoc)
  923. v group))
  924. (when (memq :any group)
  925. (setf valid-value t)
  926. (push (cons group v) groups-alist)))
  927. ((assq group groups-alist)
  928. (push
  929. (list
  930. (or (org-element-property :post-affiliated datum)
  931. (org-element-property :begin datum))
  932. (format
  933. "Forbidden combination in header \"%s\": %s, %s"
  934. (car header)
  935. (cdr (assq group groups-alist))
  936. v))
  937. reports)
  938. (throw 'exit nil))
  939. (t (push (cons group v) groups-alist)
  940. (setf valid-value t))))
  941. (unless valid-value
  942. (push
  943. (list
  944. (or (org-element-property :post-affiliated datum)
  945. (org-element-property :begin datum))
  946. (format "Unknown value \"%s\" for header \"%s\""
  947. v
  948. (car header)))
  949. reports))))))))))))
  950. reports))
  951. ;;; Reports UI
  952. (defvar org-lint--report-mode-map
  953. (let ((map (make-sparse-keymap)))
  954. (set-keymap-parent map tabulated-list-mode-map)
  955. (define-key map (kbd "RET") 'org-lint--jump-to-source)
  956. (define-key map (kbd "TAB") 'org-lint--show-source)
  957. (define-key map (kbd "C-j") 'org-lint--show-source)
  958. (define-key map (kbd "h") 'org-lint--hide-checker)
  959. (define-key map (kbd "i") 'org-lint--ignore-checker)
  960. map)
  961. "Local keymap for `org-lint--report-mode' buffers.")
  962. (define-derived-mode org-lint--report-mode tabulated-list-mode "OrgLint"
  963. "Major mode used to display reports emitted during linting.
  964. \\{org-lint--report-mode-map}"
  965. (setf tabulated-list-format
  966. `[("Line" 6
  967. (lambda (a b)
  968. (< (string-to-number (aref (cadr a) 0))
  969. (string-to-number (aref (cadr b) 0))))
  970. :right-align t)
  971. ("Trust" 5 t)
  972. ("Warning" 0 t)])
  973. (tabulated-list-init-header))
  974. (defun org-lint--generate-reports (buffer checkers)
  975. "Generate linting report for BUFFER.
  976. CHECKERS is the list of checkers used.
  977. Return an alist (ID [LINE TRUST DESCRIPTION CHECKER]), suitable
  978. for `tabulated-list-printer'."
  979. (with-current-buffer buffer
  980. (save-excursion
  981. (goto-char (point-min))
  982. (let ((ast (org-element-parse-buffer))
  983. (id 0)
  984. (last-line 1)
  985. (last-pos 1))
  986. ;; Insert unique ID for each report. Replace buffer positions
  987. ;; with line numbers.
  988. (mapcar
  989. (lambda (report)
  990. (list
  991. (cl-incf id)
  992. (apply #'vector
  993. (cons
  994. (progn
  995. (goto-char (car report))
  996. (beginning-of-line)
  997. (prog1 (number-to-string
  998. (cl-incf last-line
  999. (count-lines last-pos (point))))
  1000. (setf last-pos (point))))
  1001. (cdr report)))))
  1002. ;; Insert trust level in generated reports. Also sort them
  1003. ;; by buffer position in order to optimize lines computation.
  1004. (sort (cl-mapcan
  1005. (lambda (c)
  1006. (let ((trust (symbol-name (org-lint-checker-trust c))))
  1007. (mapcar
  1008. (lambda (report)
  1009. (list (car report) trust (nth 1 report) c))
  1010. (save-excursion
  1011. (funcall
  1012. (intern (format "org-lint-%s"
  1013. (org-lint-checker-name c)))
  1014. ast)))))
  1015. checkers)
  1016. #'car-less-than-car))))))
  1017. (defvar-local org-lint--source-buffer nil
  1018. "Source buffer associated to current report buffer.")
  1019. (defvar-local org-lint--local-checkers nil
  1020. "List of checkers used to build current report.")
  1021. (defun org-lint--refresh-reports ()
  1022. (setq tabulated-list-entries
  1023. (org-lint--generate-reports org-lint--source-buffer
  1024. org-lint--local-checkers))
  1025. (tabulated-list-print))
  1026. (defun org-lint--current-line ()
  1027. "Return current report line, as a number."
  1028. (string-to-number (aref (tabulated-list-get-entry) 0)))
  1029. (defun org-lint--current-checker (&optional entry)
  1030. "Return current report checker.
  1031. When optional argument ENTRY is non-nil, use this entry instead
  1032. of current one."
  1033. (aref (if entry (nth 1 entry) (tabulated-list-get-entry)) 3))
  1034. (defun org-lint--display-reports (source checkers)
  1035. "Display linting reports for buffer SOURCE.
  1036. CHECKERS is the list of checkers used."
  1037. (let ((buffer (get-buffer-create "*Org Lint*")))
  1038. (with-current-buffer buffer
  1039. (org-lint--report-mode)
  1040. (setf org-lint--source-buffer source)
  1041. (setf org-lint--local-checkers checkers)
  1042. (org-lint--refresh-reports)
  1043. (tabulated-list-print)
  1044. (add-hook 'tabulated-list-revert-hook #'org-lint--refresh-reports nil t))
  1045. (pop-to-buffer buffer)))
  1046. (defun org-lint--jump-to-source ()
  1047. "Move to source line that generated the report at point."
  1048. (interactive)
  1049. (let ((l (org-lint--current-line)))
  1050. (switch-to-buffer-other-window org-lint--source-buffer)
  1051. (org-goto-line l)
  1052. (org-show-set-visibility 'local)
  1053. (recenter)))
  1054. (defun org-lint--show-source ()
  1055. "Show source line that generated the report at point."
  1056. (interactive)
  1057. (let ((buffer (current-buffer)))
  1058. (org-lint--jump-to-source)
  1059. (switch-to-buffer-other-window buffer)))
  1060. (defun org-lint--hide-checker ()
  1061. "Hide all reports from checker that generated the report at point."
  1062. (interactive)
  1063. (let ((c (org-lint--current-checker)))
  1064. (setf tabulated-list-entries
  1065. (cl-remove-if (lambda (e) (equal c (org-lint--current-checker e)))
  1066. tabulated-list-entries))
  1067. (tabulated-list-print)))
  1068. (defun org-lint--ignore-checker ()
  1069. "Ignore all reports from checker that generated the report at point.
  1070. Checker will also be ignored in all subsequent reports."
  1071. (interactive)
  1072. (setf org-lint--local-checkers
  1073. (remove (org-lint--current-checker) org-lint--local-checkers))
  1074. (org-lint--hide-checker))
  1075. ;;; Public function
  1076. ;;;###autoload
  1077. (defun org-lint (&optional arg)
  1078. "Check current Org buffer for syntax mistakes.
  1079. By default, run all checkers. With a `\\[universal-argument]' prefix ARG, \
  1080. select one
  1081. category of checkers only. With a `\\[universal-argument] \
  1082. \\[universal-argument]' prefix, run one precise
  1083. checker by its name.
  1084. ARG can also be a list of checker names, as symbols, to run."
  1085. (interactive "P")
  1086. (unless (derived-mode-p 'org-mode) (user-error "Not in an Org buffer"))
  1087. (when (called-interactively-p 'any)
  1088. (message "Org linting process starting..."))
  1089. (let ((checkers
  1090. (pcase arg
  1091. (`nil org-lint--checkers)
  1092. (`(4)
  1093. (let ((category
  1094. (completing-read
  1095. "Checker category: "
  1096. (mapcar #'org-lint-checker-categories org-lint--checkers)
  1097. nil t)))
  1098. (cl-remove-if-not
  1099. (lambda (c)
  1100. (assoc-string (org-lint-checker-categories c) category))
  1101. org-lint--checkers)))
  1102. (`(16)
  1103. (list
  1104. (let ((name (completing-read
  1105. "Checker name: "
  1106. (mapcar #'org-lint-checker-name org-lint--checkers)
  1107. nil t)))
  1108. (catch 'exit
  1109. (dolist (c org-lint--checkers)
  1110. (when (string= (org-lint-checker-name c) name)
  1111. (throw 'exit c)))))))
  1112. ((pred consp)
  1113. (cl-remove-if-not (lambda (c) (memq (org-lint-checker-name c) arg))
  1114. org-lint--checkers))
  1115. (_ (user-error "Invalid argument `%S' for `org-lint'" arg)))))
  1116. (if (not (called-interactively-p 'any))
  1117. (org-lint--generate-reports (current-buffer) checkers)
  1118. (org-lint--display-reports (current-buffer) checkers)
  1119. (message "Org linting process completed"))))
  1120. (provide 'org-lint)
  1121. ;;; org-lint.el ends here