org-lint.el 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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-remove-double-quotes
  508. (org-element-property :value k))))
  509. (and (not (file-remote-p file))
  510. (not (file-exists-p file))
  511. (list (org-element-property :begin k)
  512. (format "Non-existent setup file \"%s\"" file))))))))
  513. (defun org-lint-wrong-include-link-parameter (ast)
  514. (org-element-map ast 'keyword
  515. (lambda (k)
  516. (when (equal (org-element-property :key k) "INCLUDE")
  517. (let* ((value (org-element-property :value k))
  518. (path
  519. (and (string-match "^\\(\".+\"\\|\\S-+\\)[ \t]*" value)
  520. (save-match-data
  521. (org-remove-double-quotes (match-string 1 value))))))
  522. (if (not path)
  523. (list (org-element-property :post-affiliated k)
  524. "Missing location argument in INCLUDE keyword")
  525. (let* ((file (org-string-nw-p
  526. (if (string-match "::\\(.*\\)\\'" path)
  527. (substring path 0 (match-beginning 0))
  528. path)))
  529. (search (and (not (equal file path))
  530. (org-string-nw-p (match-string 1 path)))))
  531. (if (and file
  532. (not (file-remote-p file))
  533. (not (file-exists-p file)))
  534. (list (org-element-property :post-affiliated k)
  535. "Non-existent file argument in INCLUDE keyword")
  536. (let* ((visiting (if file (find-buffer-visiting file)
  537. (current-buffer)))
  538. (buffer (or visiting (find-file-noselect file))))
  539. (unwind-protect
  540. (with-current-buffer buffer
  541. (when (and search
  542. (not
  543. (ignore-errors
  544. (let ((org-link-search-inhibit-query t))
  545. (org-link-search search nil t)))))
  546. (list (org-element-property :post-affiliated k)
  547. (format
  548. "Invalid search part \"%s\" in INCLUDE keyword"
  549. search))))
  550. (unless visiting (kill-buffer buffer))))))))))))
  551. (defun org-lint-obsolete-include-markup (ast)
  552. (let ((regexp (format "\\`\\(?:\".+\"\\|\\S-+\\)[ \t]+%s"
  553. (regexp-opt
  554. '("ASCII" "BEAMER" "HTML" "LATEX" "MAN" "MARKDOWN" "MD"
  555. "ODT" "ORG" "TEXINFO")
  556. t))))
  557. (org-element-map ast 'keyword
  558. (lambda (k)
  559. (when (equal (org-element-property :key k) "INCLUDE")
  560. (let ((case-fold-search t)
  561. (value (org-element-property :value k)))
  562. (when (string-match regexp value)
  563. (let ((markup (match-string-no-properties 1 value)))
  564. (list (org-element-property :post-affiliated k)
  565. (format "Obsolete markup \"%s\" in INCLUDE keyword. \
  566. Use \"export %s\" instead"
  567. markup
  568. markup))))))))))
  569. (defun org-lint-unknown-options-item (ast)
  570. (let ((allowed (delq nil
  571. (append
  572. (mapcar (lambda (o) (nth 2 o)) org-export-options-alist)
  573. (cl-mapcan
  574. (lambda (b)
  575. (mapcar (lambda (o) (nth 2 o))
  576. (org-export-backend-options b)))
  577. org-export-registered-backends))))
  578. reports)
  579. (org-element-map ast 'keyword
  580. (lambda (k)
  581. (when (string= (org-element-property :key k) "OPTIONS")
  582. (let ((value (org-element-property :value k))
  583. (start 0))
  584. (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-*\\)[ \t]*"
  585. value
  586. start)
  587. (setf start (match-end 0))
  588. (let ((item (match-string 1 value)))
  589. (unless (member item allowed)
  590. (push (list (org-element-property :post-affiliated k)
  591. (format "Unknown OPTIONS item \"%s\"" item))
  592. reports))))))))
  593. reports))
  594. (defun org-lint-invalid-macro-argument-and-template (ast)
  595. (let ((extract-placeholders
  596. (lambda (template)
  597. (let ((start 0)
  598. args)
  599. (while (string-match "\\$\\([1-9][0-9]*\\)" template start)
  600. (setf start (match-end 0))
  601. (push (string-to-number (match-string 1 template)) args))
  602. (sort (org-uniquify args) #'<))))
  603. reports)
  604. ;; Check arguments for macro templates.
  605. (org-element-map ast 'keyword
  606. (lambda (k)
  607. (when (string= (org-element-property :key k) "MACRO")
  608. (let* ((value (org-element-property :value k))
  609. (name (and (string-match "^\\S-+" value)
  610. (match-string 0 value)))
  611. (template (and name
  612. (org-trim (substring value (match-end 0))))))
  613. (cond
  614. ((not name)
  615. (push (list (org-element-property :post-affiliated k)
  616. "Missing name in MACRO keyword")
  617. reports))
  618. ((not (org-string-nw-p template))
  619. (push (list (org-element-property :post-affiliated k)
  620. "Missing template in macro \"%s\"" name)
  621. reports))
  622. (t
  623. (unless (let ((args (funcall extract-placeholders template)))
  624. (equal (number-sequence 1 (or (org-last args) 0)) args))
  625. (push (list (org-element-property :post-affiliated k)
  626. (format "Unused placeholders in macro \"%s\""
  627. name))
  628. reports))))))))
  629. ;; Check arguments for macros.
  630. (org-macro-initialize-templates)
  631. (let ((templates (append
  632. (mapcar (lambda (m) (cons m "$1"))
  633. '("author" "date" "email" "title" "results"))
  634. org-macro-templates)))
  635. (org-element-map ast 'macro
  636. (lambda (macro)
  637. (let* ((name (org-element-property :key macro))
  638. (template (cdr (assoc-string name templates t))))
  639. (if (not template)
  640. (push (list (org-element-property :begin macro)
  641. (format "Undefined macro \"%s\"" name))
  642. reports)
  643. (let ((arg-numbers (funcall extract-placeholders template)))
  644. (when arg-numbers
  645. (let ((spurious-args
  646. (nthcdr (apply #'max arg-numbers)
  647. (org-element-property :args macro))))
  648. (when spurious-args
  649. (push
  650. (list (org-element-property :begin macro)
  651. (format "Unused argument%s in macro \"%s\": %s"
  652. (if (> (length spurious-args) 1) "s" "")
  653. name
  654. (mapconcat (lambda (a) (format "\"%s\"" a))
  655. spurious-args
  656. ", ")))
  657. reports))))))))))
  658. reports))
  659. (defun org-lint-undefined-footnote-reference (ast)
  660. (let ((definitions (org-element-map ast 'footnote-definition
  661. (lambda (f) (org-element-property :label f)))))
  662. (org-element-map ast 'footnote-reference
  663. (lambda (f)
  664. (let ((label (org-element-property :label f)))
  665. (and label
  666. (not (member label definitions))
  667. (list (org-element-property :begin f)
  668. (format "Missing definition for footnote [%s]"
  669. label))))))))
  670. (defun org-lint-unreferenced-footnote-definition (ast)
  671. (let ((references (org-element-map ast 'footnote-reference
  672. (lambda (f) (org-element-property :label f)))))
  673. (org-element-map ast 'footnote-definition
  674. (lambda (f)
  675. (let ((label (org-element-property :label f)))
  676. (and label
  677. (not (member label references))
  678. (list (org-element-property :post-affiliated f)
  679. (format "No reference for footnote definition [%s]"
  680. label))))))))
  681. (defun org-lint-colon-in-name (ast)
  682. (org-element-map ast org-element-all-elements
  683. (lambda (e)
  684. (let ((name (org-element-property :name e)))
  685. (and name
  686. (string-match-p ":" name)
  687. (list (progn
  688. (goto-char (org-element-property :begin e))
  689. (re-search-forward
  690. (format "^[ \t]*#\\+\\w+: +%s *$" (regexp-quote name)))
  691. (match-beginning 0))
  692. (format
  693. "Name \"%s\" contains a colon; Babel cannot use it as input"
  694. name)))))))
  695. (defun org-lint-misplaced-planning-info (_)
  696. (let ((case-fold-search t)
  697. reports)
  698. (while (re-search-forward org-planning-line-re nil t)
  699. (unless (memq (org-element-type (org-element-at-point))
  700. '(comment-block example-block export-block planning
  701. src-block verse-block))
  702. (push (list (line-beginning-position) "Misplaced planning info line")
  703. reports)))
  704. reports))
  705. (defun org-lint-incomplete-drawer (_)
  706. (let (reports)
  707. (while (re-search-forward org-drawer-regexp nil t)
  708. (let ((name (org-trim (match-string-no-properties 0)))
  709. (element (org-element-at-point)))
  710. (pcase (org-element-type element)
  711. ((or `drawer `property-drawer)
  712. (goto-char (org-element-property :end element))
  713. nil)
  714. ((or `comment-block `example-block `export-block `src-block
  715. `verse-block)
  716. nil)
  717. (_
  718. (push (list (line-beginning-position)
  719. (format "Possible incomplete drawer \"%s\"" name))
  720. reports)))))
  721. reports))
  722. (defun org-lint-indented-diary-sexp (_)
  723. (let (reports)
  724. (while (re-search-forward "^[ \t]+%%(" nil t)
  725. (unless (memq (org-element-type (org-element-at-point))
  726. '(comment-block diary-sexp example-block export-block
  727. src-block verse-block))
  728. (push (list (line-beginning-position) "Possible indented diary-sexp")
  729. reports)))
  730. reports))
  731. (defun org-lint-invalid-block (_)
  732. (let ((case-fold-search t)
  733. (regexp "^[ \t]*#\\+\\(BEGIN\\|END\\)\\(?::\\|_[^[:space:]]*\\)?[ \t]*")
  734. reports)
  735. (while (re-search-forward regexp nil t)
  736. (let ((name (org-trim (buffer-substring-no-properties
  737. (line-beginning-position) (line-end-position)))))
  738. (cond
  739. ((and (string-prefix-p "END" (match-string 1) t)
  740. (not (eolp)))
  741. (push (list (line-beginning-position)
  742. (format "Invalid block closing line \"%s\"" name))
  743. reports))
  744. ((not (memq (org-element-type (org-element-at-point))
  745. '(center-block comment-block dynamic-block example-block
  746. export-block quote-block special-block
  747. src-block verse-block)))
  748. (push (list (line-beginning-position)
  749. (format "Possible incomplete block \"%s\""
  750. name))
  751. reports)))))
  752. reports))
  753. (defun org-lint-invalid-keyword-syntax (_)
  754. (let ((regexp "^[ \t]*#\\+\\([^[:space:]:]*\\)\\(?: \\|$\\)")
  755. (exception-re
  756. (format "[ \t]*#\\+%s\\(\\[.*\\]\\)?:\\(?: \\|$\\)"
  757. (regexp-opt org-element-dual-keywords)))
  758. reports)
  759. (while (re-search-forward regexp nil t)
  760. (let ((name (match-string-no-properties 1)))
  761. (unless (or (string-prefix-p "BEGIN" name t)
  762. (string-prefix-p "END" name t)
  763. (save-excursion
  764. (beginning-of-line)
  765. (let ((case-fold-search t)) (looking-at exception-re))))
  766. (push (list (match-beginning 0)
  767. (format "Possible missing colon in keyword \"%s\"" name))
  768. reports))))
  769. reports))
  770. (defun org-lint-extraneous-element-in-footnote-section (ast)
  771. (org-element-map ast 'headline
  772. (lambda (h)
  773. (and (org-element-property :footnote-section-p h)
  774. (org-element-map (org-element-contents h)
  775. (cl-remove-if
  776. (lambda (e)
  777. (memq e '(comment comment-block footnote-definition
  778. property-drawer section)))
  779. org-element-all-elements)
  780. (lambda (e)
  781. (not (and (eq (org-element-type e) 'headline)
  782. (org-element-property :commentedp e))))
  783. nil t '(footnote-definition property-drawer))
  784. (list (org-element-property :begin h)
  785. "Extraneous elements in footnote section")))))
  786. (defun org-lint-quote-section (ast)
  787. (org-element-map ast '(headline inlinetask)
  788. (lambda (h)
  789. (let ((title (org-element-property :raw-value h)))
  790. (and (or (string-prefix-p "QUOTE " title)
  791. (string-prefix-p (concat org-comment-string " QUOTE ") title))
  792. (list (org-element-property :begin h)
  793. "Deprecated QUOTE section"))))))
  794. (defun org-lint-file-application (ast)
  795. (org-element-map ast 'link
  796. (lambda (l)
  797. (let ((app (org-element-property :application l)))
  798. (and app
  799. (list (org-element-property :begin l)
  800. (format "Deprecated \"file+%s\" link type" app)))))))
  801. (defun org-lint-wrong-header-argument (ast)
  802. (let* ((reports)
  803. (verify
  804. (lambda (datum language headers)
  805. (let ((allowed
  806. ;; If LANGUAGE is specified, restrict allowed
  807. ;; headers to both LANGUAGE-specific and default
  808. ;; ones. Otherwise, accept headers from any loaded
  809. ;; language.
  810. (append
  811. org-babel-header-arg-names
  812. (cl-mapcan
  813. (lambda (l)
  814. (let ((v (intern (format "org-babel-header-args:%s" l))))
  815. (and (boundp v) (mapcar #'car (symbol-value v)))))
  816. (if language (list language)
  817. (mapcar #'car org-babel-load-languages))))))
  818. (dolist (header headers)
  819. (let ((h (symbol-name (car header)))
  820. (p (or (org-element-property :post-affiliated datum)
  821. (org-element-property :begin datum))))
  822. (cond
  823. ((not (string-prefix-p ":" h))
  824. (push
  825. (list p
  826. (format "Missing colon in header argument \"%s\"" h))
  827. reports))
  828. ((assoc-string (substring h 1) allowed))
  829. (t (push (list p (format "Unknown header argument \"%s\"" h))
  830. reports)))))))))
  831. (org-element-map ast '(babel-call inline-babel-call inline-src-block keyword
  832. node-property src-block)
  833. (lambda (datum)
  834. (pcase (org-element-type datum)
  835. ((or `babel-call `inline-babel-call)
  836. (funcall verify
  837. datum
  838. nil
  839. (cl-mapcan #'org-babel-parse-header-arguments
  840. (list
  841. (org-element-property :inside-header datum)
  842. (org-element-property :end-header datum)))))
  843. (`inline-src-block
  844. (funcall verify
  845. datum
  846. (org-element-property :language datum)
  847. (org-babel-parse-header-arguments
  848. (org-element-property :parameters datum))))
  849. (`keyword
  850. (when (string= (org-element-property :key datum) "PROPERTY")
  851. (let ((value (org-element-property :value datum)))
  852. (when (string-match "\\`header-args\\(?::\\(\\S-+\\)\\)?\\+? *"
  853. value)
  854. (funcall verify
  855. datum
  856. (match-string 1 value)
  857. (org-babel-parse-header-arguments
  858. (substring value (match-end 0))))))))
  859. (`node-property
  860. (let ((key (org-element-property :key datum)))
  861. (when (let ((case-fold-search t))
  862. (string-match "\\`HEADER-ARGS\\(?::\\(\\S-+\\)\\)?\\+?"
  863. key))
  864. (funcall verify
  865. datum
  866. (match-string 1 key)
  867. (org-babel-parse-header-arguments
  868. (org-element-property :value datum))))))
  869. (`src-block
  870. (funcall verify
  871. datum
  872. (org-element-property :language datum)
  873. (cl-mapcan #'org-babel-parse-header-arguments
  874. (cons (org-element-property :parameters datum)
  875. (org-element-property :header datum))))))))
  876. reports))
  877. (defun org-lint-wrong-header-value (ast)
  878. (let (reports)
  879. (org-element-map ast
  880. '(babel-call inline-babel-call inline-src-block src-block)
  881. (lambda (datum)
  882. (let* ((type (org-element-type datum))
  883. (language (org-element-property :language datum))
  884. (allowed-header-values
  885. (append (and language
  886. (let ((v (intern (concat "org-babel-header-args:"
  887. language))))
  888. (and (boundp v) (symbol-value v))))
  889. org-babel-common-header-args-w-values))
  890. (datum-header-values
  891. (apply
  892. #'org-babel-merge-params
  893. org-babel-default-header-args
  894. (and language
  895. (let ((v (intern (concat "org-babel-default-header-args:"
  896. language))))
  897. (and (boundp v) (symbol-value v))))
  898. (append
  899. (list (and (memq type '(babel-call inline-babel-call))
  900. org-babel-default-lob-header-args))
  901. (progn (goto-char (org-element-property :begin datum))
  902. (org-babel-params-from-properties language))
  903. (list
  904. (org-babel-parse-header-arguments
  905. (org-trim
  906. (pcase type
  907. (`src-block
  908. (mapconcat
  909. #'identity
  910. (cons (org-element-property :parameters datum)
  911. (org-element-property :header datum))
  912. " "))
  913. (`inline-src-block
  914. (or (org-element-property :parameters datum) ""))
  915. (_
  916. (concat
  917. (org-element-property :inside-header datum)
  918. " "
  919. (org-element-property :end-header datum)))))))))))
  920. (dolist (header datum-header-values)
  921. (let ((allowed-values
  922. (cdr (assoc-string (substring (symbol-name (car header)) 1)
  923. allowed-header-values))))
  924. (unless (memq allowed-values '(:any nil))
  925. (let ((values (cdr header))
  926. groups-alist)
  927. (dolist (v (if (stringp values) (org-split-string values)
  928. (list values)))
  929. (let ((valid-value nil))
  930. (catch 'exit
  931. (dolist (group allowed-values)
  932. (cond
  933. ((not (funcall
  934. (if (stringp v) #'assoc-string #'assoc)
  935. v group))
  936. (when (memq :any group)
  937. (setf valid-value t)
  938. (push (cons group v) groups-alist)))
  939. ((assq group groups-alist)
  940. (push
  941. (list
  942. (or (org-element-property :post-affiliated datum)
  943. (org-element-property :begin datum))
  944. (format
  945. "Forbidden combination in header \"%s\": %s, %s"
  946. (car header)
  947. (cdr (assq group groups-alist))
  948. v))
  949. reports)
  950. (throw 'exit nil))
  951. (t (push (cons group v) groups-alist)
  952. (setf valid-value t))))
  953. (unless valid-value
  954. (push
  955. (list
  956. (or (org-element-property :post-affiliated datum)
  957. (org-element-property :begin datum))
  958. (format "Unknown value \"%s\" for header \"%s\""
  959. v
  960. (car header)))
  961. reports))))))))))))
  962. reports))
  963. ;;; Reports UI
  964. (defvar org-lint--report-mode-map
  965. (let ((map (make-sparse-keymap)))
  966. (set-keymap-parent map tabulated-list-mode-map)
  967. (define-key map (kbd "RET") 'org-lint--jump-to-source)
  968. (define-key map (kbd "TAB") 'org-lint--show-source)
  969. (define-key map (kbd "C-j") 'org-lint--show-source)
  970. (define-key map (kbd "h") 'org-lint--hide-checker)
  971. (define-key map (kbd "i") 'org-lint--ignore-checker)
  972. map)
  973. "Local keymap for `org-lint--report-mode' buffers.")
  974. (define-derived-mode org-lint--report-mode tabulated-list-mode "OrgLint"
  975. "Major mode used to display reports emitted during linting.
  976. \\{org-lint--report-mode-map}"
  977. (setf tabulated-list-format
  978. `[("Line" 6
  979. (lambda (a b)
  980. (< (string-to-number (aref (cadr a) 0))
  981. (string-to-number (aref (cadr b) 0))))
  982. :right-align t)
  983. ("Trust" 5 t)
  984. ("Warning" 0 t)])
  985. (tabulated-list-init-header))
  986. (defun org-lint--generate-reports (buffer checkers)
  987. "Generate linting report for BUFFER.
  988. CHECKERS is the list of checkers used.
  989. Return an alist (ID [LINE TRUST DESCRIPTION CHECKER]), suitable
  990. for `tabulated-list-printer'."
  991. (with-current-buffer buffer
  992. (save-excursion
  993. (goto-char (point-min))
  994. (let ((ast (org-element-parse-buffer))
  995. (id 0)
  996. (last-line 1)
  997. (last-pos 1))
  998. ;; Insert unique ID for each report. Replace buffer positions
  999. ;; with line numbers.
  1000. (mapcar
  1001. (lambda (report)
  1002. (list
  1003. (cl-incf id)
  1004. (apply #'vector
  1005. (cons
  1006. (progn
  1007. (goto-char (car report))
  1008. (beginning-of-line)
  1009. (prog1 (number-to-string
  1010. (cl-incf last-line
  1011. (count-lines last-pos (point))))
  1012. (setf last-pos (point))))
  1013. (cdr report)))))
  1014. ;; Insert trust level in generated reports. Also sort them
  1015. ;; by buffer position in order to optimize lines computation.
  1016. (sort (cl-mapcan
  1017. (lambda (c)
  1018. (let ((trust (symbol-name (org-lint-checker-trust c))))
  1019. (mapcar
  1020. (lambda (report)
  1021. (list (car report) trust (nth 1 report) c))
  1022. (save-excursion
  1023. (funcall
  1024. (intern (format "org-lint-%s"
  1025. (org-lint-checker-name c)))
  1026. ast)))))
  1027. checkers)
  1028. #'car-less-than-car))))))
  1029. (defvar-local org-lint--source-buffer nil
  1030. "Source buffer associated to current report buffer.")
  1031. (defvar-local org-lint--local-checkers nil
  1032. "List of checkers used to build current report.")
  1033. (defun org-lint--refresh-reports ()
  1034. (setq tabulated-list-entries
  1035. (org-lint--generate-reports org-lint--source-buffer
  1036. org-lint--local-checkers))
  1037. (tabulated-list-print))
  1038. (defun org-lint--current-line ()
  1039. "Return current report line, as a number."
  1040. (string-to-number (aref (tabulated-list-get-entry) 0)))
  1041. (defun org-lint--current-checker (&optional entry)
  1042. "Return current report checker.
  1043. When optional argument ENTRY is non-nil, use this entry instead
  1044. of current one."
  1045. (aref (if entry (nth 1 entry) (tabulated-list-get-entry)) 3))
  1046. (defun org-lint--display-reports (source checkers)
  1047. "Display linting reports for buffer SOURCE.
  1048. CHECKERS is the list of checkers used."
  1049. (let ((buffer (get-buffer-create "*Org Lint*")))
  1050. (with-current-buffer buffer
  1051. (org-lint--report-mode)
  1052. (setf org-lint--source-buffer source)
  1053. (setf org-lint--local-checkers checkers)
  1054. (org-lint--refresh-reports)
  1055. (tabulated-list-print)
  1056. (add-hook 'tabulated-list-revert-hook #'org-lint--refresh-reports nil t))
  1057. (pop-to-buffer buffer)))
  1058. (defun org-lint--jump-to-source ()
  1059. "Move to source line that generated the report at point."
  1060. (interactive)
  1061. (let ((l (org-lint--current-line)))
  1062. (switch-to-buffer-other-window org-lint--source-buffer)
  1063. (org-goto-line l)
  1064. (org-show-set-visibility 'local)
  1065. (recenter)))
  1066. (defun org-lint--show-source ()
  1067. "Show source line that generated the report at point."
  1068. (interactive)
  1069. (let ((buffer (current-buffer)))
  1070. (org-lint--jump-to-source)
  1071. (switch-to-buffer-other-window buffer)))
  1072. (defun org-lint--hide-checker ()
  1073. "Hide all reports from checker that generated the report at point."
  1074. (interactive)
  1075. (let ((c (org-lint--current-checker)))
  1076. (setf tabulated-list-entries
  1077. (cl-remove-if (lambda (e) (equal c (org-lint--current-checker e)))
  1078. tabulated-list-entries))
  1079. (tabulated-list-print)))
  1080. (defun org-lint--ignore-checker ()
  1081. "Ignore all reports from checker that generated the report at point.
  1082. Checker will also be ignored in all subsequent reports."
  1083. (interactive)
  1084. (setf org-lint--local-checkers
  1085. (remove (org-lint--current-checker) org-lint--local-checkers))
  1086. (org-lint--hide-checker))
  1087. ;;; Public function
  1088. ;;;###autoload
  1089. (defun org-lint (&optional arg)
  1090. "Check current Org buffer for syntax mistakes.
  1091. By default, run all checkers. With a single prefix ARG \
  1092. \\[universal-argument],
  1093. select one category of checkers only. With a double prefix
  1094. \\[universal-argument] \\[universal-argument], select one precise \
  1095. checker by its name.
  1096. ARG can also be a list of checker names, as symbols, to run."
  1097. (interactive "P")
  1098. (unless (derived-mode-p 'org-mode) (user-error "Not in an Org buffer"))
  1099. (when (called-interactively-p 'any)
  1100. (message "Org linting process starting..."))
  1101. (let ((checkers
  1102. (pcase arg
  1103. (`nil org-lint--checkers)
  1104. (`(4)
  1105. (let ((category
  1106. (completing-read
  1107. "Checker category: "
  1108. (mapcar #'org-lint-checker-categories org-lint--checkers)
  1109. nil t)))
  1110. (cl-remove-if-not
  1111. (lambda (c)
  1112. (assoc-string (org-lint-checker-categories c) category))
  1113. org-lint--checkers)))
  1114. (`(16)
  1115. (list
  1116. (let ((name (completing-read
  1117. "Checker name: "
  1118. (mapcar #'org-lint-checker-name org-lint--checkers)
  1119. nil t)))
  1120. (catch 'exit
  1121. (dolist (c org-lint--checkers)
  1122. (when (string= (org-lint-checker-name c) name)
  1123. (throw 'exit c)))))))
  1124. ((pred consp)
  1125. (cl-remove-if-not (lambda (c) (memq (org-lint-checker-name c) arg))
  1126. org-lint--checkers))
  1127. (_ (user-error "Invalid argument `%S' for `org-lint'" arg)))))
  1128. (if (not (called-interactively-p 'any))
  1129. (org-lint--generate-reports (current-buffer) checkers)
  1130. (org-lint--display-reports (current-buffer) checkers)
  1131. (message "Org linting process completed"))))
  1132. (provide 'org-lint)
  1133. ;;; org-lint.el ends here