org-lint.el 42 KB

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