org-lint.el 44 KB

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