org-lint.el 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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* ((reports nil)
  637. (extract-placeholders
  638. (lambda (template)
  639. (let ((start 0)
  640. args)
  641. (while (string-match "\\$\\([1-9][0-9]*\\)" template start)
  642. (setf start (match-end 0))
  643. (push (string-to-number (match-string 1 template)) args))
  644. (sort (org-uniquify args) #'<))))
  645. (check-arity
  646. (lambda (arity macro)
  647. (let* ((name (org-element-property :key macro))
  648. (pos (org-element-property :begin macro))
  649. (args (org-element-property :args macro))
  650. (l (length args)))
  651. (cond
  652. ((< l (1- (car arity)))
  653. (push (list pos (format "Missing arguments in macro %S" name))
  654. reports))
  655. ((< l (car arity))
  656. (push (list pos (format "Missing argument in macro %S" name))
  657. reports))
  658. ((> l (1+ (cdr arity)))
  659. (push (let ((spurious-args (nthcdr (cdr arity) args)))
  660. (list pos
  661. (format "Spurious arguments in macro %S: %s"
  662. name
  663. (mapconcat #'org-trim spurious-args ", "))))
  664. reports))
  665. ((> l (cdr arity))
  666. (push (list pos
  667. (format "Spurious argument in macro %S: %s"
  668. name
  669. (org-last args)))
  670. reports))
  671. (t nil))))))
  672. ;; Check arguments for macro templates.
  673. (org-element-map ast 'keyword
  674. (lambda (k)
  675. (when (string= (org-element-property :key k) "MACRO")
  676. (let* ((value (org-element-property :value k))
  677. (name (and (string-match "^\\S-+" value)
  678. (match-string 0 value)))
  679. (template (and name
  680. (org-trim (substring value (match-end 0))))))
  681. (cond
  682. ((not name)
  683. (push (list (org-element-property :post-affiliated k)
  684. "Missing name in MACRO keyword")
  685. reports))
  686. ((not (org-string-nw-p template))
  687. (push (list (org-element-property :post-affiliated k)
  688. "Missing template in macro \"%s\"" name)
  689. reports))
  690. (t
  691. (unless (let ((args (funcall extract-placeholders template)))
  692. (equal (number-sequence 1 (or (org-last args) 0)) args))
  693. (push (list (org-element-property :post-affiliated k)
  694. (format "Unused placeholders in macro \"%s\""
  695. name))
  696. reports))))))))
  697. ;; Check arguments for macros.
  698. (org-macro-initialize-templates)
  699. (let ((templates (append
  700. (mapcar (lambda (m) (cons m "$1"))
  701. '("author" "date" "email" "title" "results"))
  702. org-macro-templates)))
  703. (org-element-map ast 'macro
  704. (lambda (macro)
  705. (let* ((name (org-element-property :key macro))
  706. (template (cdr (assoc-string name templates t))))
  707. (pcase template
  708. (`nil
  709. (push (list (org-element-property :begin macro)
  710. (format "Undefined macro %S" name))
  711. reports))
  712. ((guard (string= name "keyword"))
  713. (funcall check-arity '(1 . 1) macro))
  714. ((guard (string= name "modification-time"))
  715. (funcall check-arity '(1 . 2) macro))
  716. ((guard (string= name "n"))
  717. (funcall check-arity '(0 . 2) macro))
  718. ((guard (string= name "property"))
  719. (funcall check-arity '(1 . 2) macro))
  720. ((guard (string= name "time"))
  721. (funcall check-arity '(1 . 1) macro))
  722. ((pred functionp)) ;ignore (eval ...) templates
  723. (_
  724. (let* ((arg-numbers (funcall extract-placeholders template))
  725. (arity (if (null arg-numbers)
  726. '(0 . 0)
  727. (let ((m (apply #'max arg-numbers)))
  728. (cons m m)))))
  729. (funcall check-arity arity macro))))))))
  730. reports))
  731. (defun org-lint-undefined-footnote-reference (ast)
  732. (let ((definitions (org-element-map ast 'footnote-definition
  733. (lambda (f) (org-element-property :label f)))))
  734. (org-element-map ast 'footnote-reference
  735. (lambda (f)
  736. (let ((label (org-element-property :label f)))
  737. (and (eq 'standard (org-element-property :type f))
  738. (not (member label definitions))
  739. (list (org-element-property :begin f)
  740. (format "Missing definition for footnote [%s]"
  741. label))))))))
  742. (defun org-lint-unreferenced-footnote-definition (ast)
  743. (let ((references (org-element-map ast 'footnote-reference
  744. (lambda (f) (org-element-property :label f)))))
  745. (org-element-map ast 'footnote-definition
  746. (lambda (f)
  747. (let ((label (org-element-property :label f)))
  748. (and label
  749. (not (member label references))
  750. (list (org-element-property :post-affiliated f)
  751. (format "No reference for footnote definition [%s]"
  752. label))))))))
  753. (defun org-lint-colon-in-name (ast)
  754. (org-element-map ast org-element-all-elements
  755. (lambda (e)
  756. (let ((name (org-element-property :name e)))
  757. (and name
  758. (string-match-p ":" name)
  759. (list (progn
  760. (goto-char (org-element-property :begin e))
  761. (re-search-forward
  762. (format "^[ \t]*#\\+\\w+: +%s *$" (regexp-quote name)))
  763. (match-beginning 0))
  764. (format
  765. "Name \"%s\" contains a colon; Babel cannot use it as input"
  766. name)))))))
  767. (defun org-lint-misplaced-planning-info (_)
  768. (let ((case-fold-search t)
  769. reports)
  770. (while (re-search-forward org-planning-line-re nil t)
  771. (unless (memq (org-element-type (org-element-at-point))
  772. '(comment-block example-block export-block planning
  773. src-block verse-block))
  774. (push (list (line-beginning-position) "Misplaced planning info line")
  775. reports)))
  776. reports))
  777. (defun org-lint-incomplete-drawer (_)
  778. (let (reports)
  779. (while (re-search-forward org-drawer-regexp nil t)
  780. (let ((name (org-trim (match-string-no-properties 0)))
  781. (element (org-element-at-point)))
  782. (pcase (org-element-type element)
  783. (`drawer
  784. ;; Find drawer opening lines within non-empty drawers.
  785. (let ((end (org-element-property :contents-end element)))
  786. (when end
  787. (while (re-search-forward org-drawer-regexp end t)
  788. (let ((n (org-trim (match-string-no-properties 0))))
  789. (push (list (line-beginning-position)
  790. (format "Possible misleading drawer entry %S" n))
  791. reports))))
  792. (goto-char (org-element-property :end element))))
  793. (`property-drawer
  794. (goto-char (org-element-property :end element)))
  795. ((or `comment-block `example-block `export-block `src-block
  796. `verse-block)
  797. nil)
  798. (_
  799. ;; Find drawer opening lines outside of any drawer.
  800. (push (list (line-beginning-position)
  801. (format "Possible incomplete drawer %S" name))
  802. reports)))))
  803. reports))
  804. (defun org-lint-indented-diary-sexp (_)
  805. (let (reports)
  806. (while (re-search-forward "^[ \t]+%%(" nil t)
  807. (unless (memq (org-element-type (org-element-at-point))
  808. '(comment-block diary-sexp example-block export-block
  809. src-block verse-block))
  810. (push (list (line-beginning-position) "Possible indented diary-sexp")
  811. reports)))
  812. reports))
  813. (defun org-lint-invalid-block (_)
  814. (let ((case-fold-search t)
  815. (regexp "^[ \t]*#\\+\\(BEGIN\\|END\\)\\(?::\\|_[^[:space:]]*\\)?[ \t]*")
  816. reports)
  817. (while (re-search-forward regexp nil t)
  818. (let ((name (org-trim (buffer-substring-no-properties
  819. (line-beginning-position) (line-end-position)))))
  820. (cond
  821. ((and (string-prefix-p "END" (match-string 1) t)
  822. (not (eolp)))
  823. (push (list (line-beginning-position)
  824. (format "Invalid block closing line \"%s\"" name))
  825. reports))
  826. ((not (memq (org-element-type (org-element-at-point))
  827. '(center-block comment-block dynamic-block example-block
  828. export-block quote-block special-block
  829. src-block verse-block)))
  830. (push (list (line-beginning-position)
  831. (format "Possible incomplete block \"%s\""
  832. name))
  833. reports)))))
  834. reports))
  835. (defun org-lint-invalid-keyword-syntax (_)
  836. (let ((regexp "^[ \t]*#\\+\\([^[:space:]:]*\\)\\(?: \\|$\\)")
  837. (exception-re
  838. (format "[ \t]*#\\+%s\\(\\[.*\\]\\)?:\\(?: \\|$\\)"
  839. (regexp-opt org-element-dual-keywords)))
  840. reports)
  841. (while (re-search-forward regexp nil t)
  842. (let ((name (match-string-no-properties 1)))
  843. (unless (or (string-prefix-p "BEGIN" name t)
  844. (string-prefix-p "END" name t)
  845. (save-excursion
  846. (beginning-of-line)
  847. (let ((case-fold-search t)) (looking-at exception-re))))
  848. (push (list (match-beginning 0)
  849. (format "Possible missing colon in keyword \"%s\"" name))
  850. reports))))
  851. reports))
  852. (defun org-lint-extraneous-element-in-footnote-section (ast)
  853. (org-element-map ast 'headline
  854. (lambda (h)
  855. (and (org-element-property :footnote-section-p h)
  856. (org-element-map (org-element-contents h)
  857. (cl-remove-if
  858. (lambda (e)
  859. (memq e '(comment comment-block footnote-definition
  860. property-drawer section)))
  861. org-element-all-elements)
  862. (lambda (e)
  863. (not (and (eq (org-element-type e) 'headline)
  864. (org-element-property :commentedp e))))
  865. nil t '(footnote-definition property-drawer))
  866. (list (org-element-property :begin h)
  867. "Extraneous elements in footnote section are not exported")))))
  868. (defun org-lint-quote-section (ast)
  869. (org-element-map ast '(headline inlinetask)
  870. (lambda (h)
  871. (let ((title (org-element-property :raw-value h)))
  872. (and (or (string-prefix-p "QUOTE " title)
  873. (string-prefix-p (concat org-comment-string " QUOTE ") title))
  874. (list (org-element-property :begin h)
  875. "Deprecated QUOTE section"))))))
  876. (defun org-lint-file-application (ast)
  877. (org-element-map ast 'link
  878. (lambda (l)
  879. (let ((app (org-element-property :application l)))
  880. (and app
  881. (list (org-element-property :begin l)
  882. (format "Deprecated \"file+%s\" link type" app)))))))
  883. (defun org-lint-percent-encoding-link-escape (ast)
  884. (org-element-map ast 'link
  885. (lambda (l)
  886. (when (eq 'bracket (org-element-property :format l))
  887. (let* ((uri (org-element-property :path l))
  888. (start 0)
  889. (obsolete-flag
  890. (catch :obsolete
  891. (while (string-match "%\\(..\\)?" uri start)
  892. (setq start (match-end 0))
  893. (unless (member (match-string 1 uri) '("25" "5B" "5D" "20"))
  894. (throw :obsolete nil)))
  895. (string-match-p "%" uri))))
  896. (when obsolete-flag
  897. (list (org-element-property :begin l)
  898. "Link escaped with obsolete percent-encoding syntax")))))))
  899. (defun org-lint-wrong-header-argument (ast)
  900. (let* ((reports)
  901. (verify
  902. (lambda (datum language headers)
  903. (let ((allowed
  904. ;; If LANGUAGE is specified, restrict allowed
  905. ;; headers to both LANGUAGE-specific and default
  906. ;; ones. Otherwise, accept headers from any loaded
  907. ;; language.
  908. (append
  909. org-babel-header-arg-names
  910. (cl-mapcan
  911. (lambda (l)
  912. (let ((v (intern (format "org-babel-header-args:%s" l))))
  913. (and (boundp v) (mapcar #'car (symbol-value v)))))
  914. (if language (list language)
  915. (mapcar #'car org-babel-load-languages))))))
  916. (dolist (header headers)
  917. (let ((h (symbol-name (car header)))
  918. (p (or (org-element-property :post-affiliated datum)
  919. (org-element-property :begin datum))))
  920. (cond
  921. ((not (string-prefix-p ":" h))
  922. (push
  923. (list p
  924. (format "Missing colon in header argument \"%s\"" h))
  925. reports))
  926. ((assoc-string (substring h 1) allowed))
  927. (t (push (list p (format "Unknown header argument \"%s\"" h))
  928. reports)))))))))
  929. (org-element-map ast '(babel-call inline-babel-call inline-src-block keyword
  930. node-property src-block)
  931. (lambda (datum)
  932. (pcase (org-element-type datum)
  933. ((or `babel-call `inline-babel-call)
  934. (funcall verify
  935. datum
  936. nil
  937. (cl-mapcan #'org-babel-parse-header-arguments
  938. (list
  939. (org-element-property :inside-header datum)
  940. (org-element-property :end-header datum)))))
  941. (`inline-src-block
  942. (funcall verify
  943. datum
  944. (org-element-property :language datum)
  945. (org-babel-parse-header-arguments
  946. (org-element-property :parameters datum))))
  947. (`keyword
  948. (when (string= (org-element-property :key datum) "PROPERTY")
  949. (let ((value (org-element-property :value datum)))
  950. (when (string-match "\\`header-args\\(?::\\(\\S-+\\)\\)?\\+? *"
  951. value)
  952. (funcall verify
  953. datum
  954. (match-string 1 value)
  955. (org-babel-parse-header-arguments
  956. (substring value (match-end 0))))))))
  957. (`node-property
  958. (let ((key (org-element-property :key datum)))
  959. (when (let ((case-fold-search t))
  960. (string-match "\\`HEADER-ARGS\\(?::\\(\\S-+\\)\\)?\\+?"
  961. key))
  962. (funcall verify
  963. datum
  964. (match-string 1 key)
  965. (org-babel-parse-header-arguments
  966. (org-element-property :value datum))))))
  967. (`src-block
  968. (funcall verify
  969. datum
  970. (org-element-property :language datum)
  971. (cl-mapcan #'org-babel-parse-header-arguments
  972. (cons (org-element-property :parameters datum)
  973. (org-element-property :header datum))))))))
  974. reports))
  975. (defun org-lint-wrong-header-value (ast)
  976. (let (reports)
  977. (org-element-map ast
  978. '(babel-call inline-babel-call inline-src-block src-block)
  979. (lambda (datum)
  980. (let* ((type (org-element-type datum))
  981. (language (org-element-property :language datum))
  982. (allowed-header-values
  983. (append (and language
  984. (let ((v (intern (concat "org-babel-header-args:"
  985. language))))
  986. (and (boundp v) (symbol-value v))))
  987. org-babel-common-header-args-w-values))
  988. (datum-header-values
  989. (org-babel-parse-header-arguments
  990. (org-trim
  991. (pcase type
  992. (`src-block
  993. (mapconcat
  994. #'identity
  995. (cons (org-element-property :parameters datum)
  996. (org-element-property :header datum))
  997. " "))
  998. (`inline-src-block
  999. (or (org-element-property :parameters datum) ""))
  1000. (_
  1001. (concat
  1002. (org-element-property :inside-header datum)
  1003. " "
  1004. (org-element-property :end-header datum))))))))
  1005. (dolist (header datum-header-values)
  1006. (let ((allowed-values
  1007. (cdr (assoc-string (substring (symbol-name (car header)) 1)
  1008. allowed-header-values))))
  1009. (unless (memq allowed-values '(:any nil))
  1010. (let ((values (cdr header))
  1011. groups-alist)
  1012. (dolist (v (if (stringp values) (split-string values)
  1013. (list values)))
  1014. (let ((valid-value nil))
  1015. (catch 'exit
  1016. (dolist (group allowed-values)
  1017. (cond
  1018. ((not (funcall
  1019. (if (stringp v) #'assoc-string #'assoc)
  1020. v group))
  1021. (when (memq :any group)
  1022. (setf valid-value t)
  1023. (push (cons group v) groups-alist)))
  1024. ((assq group groups-alist)
  1025. (push
  1026. (list
  1027. (or (org-element-property :post-affiliated datum)
  1028. (org-element-property :begin datum))
  1029. (format
  1030. "Forbidden combination in header \"%s\": %s, %s"
  1031. (car header)
  1032. (cdr (assq group groups-alist))
  1033. v))
  1034. reports)
  1035. (throw 'exit nil))
  1036. (t (push (cons group v) groups-alist)
  1037. (setf valid-value t))))
  1038. (unless valid-value
  1039. (push
  1040. (list
  1041. (or (org-element-property :post-affiliated datum)
  1042. (org-element-property :begin datum))
  1043. (format "Unknown value \"%s\" for header \"%s\""
  1044. v
  1045. (car header)))
  1046. reports))))))))))))
  1047. reports))
  1048. (defun org-lint-spurious-colons (ast)
  1049. (org-element-map ast '(headline inlinetask)
  1050. (lambda (h)
  1051. (when (member "" (org-element-property :tags h))
  1052. (list (org-element-property :begin h)
  1053. "Tags contain a spurious colon")))))
  1054. ;;; Reports UI
  1055. (defvar org-lint--report-mode-map
  1056. (let ((map (make-sparse-keymap)))
  1057. (set-keymap-parent map tabulated-list-mode-map)
  1058. (define-key map (kbd "RET") 'org-lint--jump-to-source)
  1059. (define-key map (kbd "TAB") 'org-lint--show-source)
  1060. (define-key map (kbd "C-j") 'org-lint--show-source)
  1061. (define-key map (kbd "h") 'org-lint--hide-checker)
  1062. (define-key map (kbd "i") 'org-lint--ignore-checker)
  1063. map)
  1064. "Local keymap for `org-lint--report-mode' buffers.")
  1065. (define-derived-mode org-lint--report-mode tabulated-list-mode "OrgLint"
  1066. "Major mode used to display reports emitted during linting.
  1067. \\{org-lint--report-mode-map}"
  1068. (setf tabulated-list-format
  1069. `[("Line" 6
  1070. (lambda (a b)
  1071. (< (string-to-number (aref (cadr a) 0))
  1072. (string-to-number (aref (cadr b) 0))))
  1073. :right-align t)
  1074. ("Trust" 5 t)
  1075. ("Warning" 0 t)])
  1076. (tabulated-list-init-header))
  1077. (defun org-lint--generate-reports (buffer checkers)
  1078. "Generate linting report for BUFFER.
  1079. CHECKERS is the list of checkers used.
  1080. Return an alist (ID [LINE TRUST DESCRIPTION CHECKER]), suitable
  1081. for `tabulated-list-printer'."
  1082. (with-current-buffer buffer
  1083. (save-excursion
  1084. (goto-char (point-min))
  1085. (let ((ast (org-element-parse-buffer))
  1086. (id 0)
  1087. (last-line 1)
  1088. (last-pos 1))
  1089. ;; Insert unique ID for each report. Replace buffer positions
  1090. ;; with line numbers.
  1091. (mapcar
  1092. (lambda (report)
  1093. (list
  1094. (cl-incf id)
  1095. (apply #'vector
  1096. (cons
  1097. (progn
  1098. (goto-char (car report))
  1099. (beginning-of-line)
  1100. (prog1 (number-to-string
  1101. (cl-incf last-line
  1102. (count-lines last-pos (point))))
  1103. (setf last-pos (point))))
  1104. (cdr report)))))
  1105. ;; Insert trust level in generated reports. Also sort them
  1106. ;; by buffer position in order to optimize lines computation.
  1107. (sort (cl-mapcan
  1108. (lambda (c)
  1109. (let ((trust (symbol-name (org-lint-checker-trust c))))
  1110. (mapcar
  1111. (lambda (report)
  1112. (list (car report) trust (nth 1 report) c))
  1113. (save-excursion
  1114. (funcall
  1115. (intern (format "org-lint-%s"
  1116. (org-lint-checker-name c)))
  1117. ast)))))
  1118. checkers)
  1119. #'car-less-than-car))))))
  1120. (defvar-local org-lint--source-buffer nil
  1121. "Source buffer associated to current report buffer.")
  1122. (defvar-local org-lint--local-checkers nil
  1123. "List of checkers used to build current report.")
  1124. (defun org-lint--refresh-reports ()
  1125. (setq tabulated-list-entries
  1126. (org-lint--generate-reports org-lint--source-buffer
  1127. org-lint--local-checkers))
  1128. (tabulated-list-print))
  1129. (defun org-lint--current-line ()
  1130. "Return current report line, as a number."
  1131. (string-to-number (aref (tabulated-list-get-entry) 0)))
  1132. (defun org-lint--current-checker (&optional entry)
  1133. "Return current report checker.
  1134. When optional argument ENTRY is non-nil, use this entry instead
  1135. of current one."
  1136. (aref (if entry (nth 1 entry) (tabulated-list-get-entry)) 3))
  1137. (defun org-lint--display-reports (source checkers)
  1138. "Display linting reports for buffer SOURCE.
  1139. CHECKERS is the list of checkers used."
  1140. (let ((buffer (get-buffer-create "*Org Lint*")))
  1141. (with-current-buffer buffer
  1142. (org-lint--report-mode)
  1143. (setf org-lint--source-buffer source)
  1144. (setf org-lint--local-checkers checkers)
  1145. (org-lint--refresh-reports)
  1146. (add-hook 'tabulated-list-revert-hook #'org-lint--refresh-reports nil t))
  1147. (pop-to-buffer buffer)))
  1148. (defun org-lint--jump-to-source ()
  1149. "Move to source line that generated the report at point."
  1150. (interactive)
  1151. (let ((l (org-lint--current-line)))
  1152. (switch-to-buffer-other-window org-lint--source-buffer)
  1153. (org-goto-line l)
  1154. (org-show-set-visibility 'local)
  1155. (recenter)))
  1156. (defun org-lint--show-source ()
  1157. "Show source line that generated the report at point."
  1158. (interactive)
  1159. (let ((buffer (current-buffer)))
  1160. (org-lint--jump-to-source)
  1161. (switch-to-buffer-other-window buffer)))
  1162. (defun org-lint--hide-checker ()
  1163. "Hide all reports from checker that generated the report at point."
  1164. (interactive)
  1165. (let ((c (org-lint--current-checker)))
  1166. (setf tabulated-list-entries
  1167. (cl-remove-if (lambda (e) (equal c (org-lint--current-checker e)))
  1168. tabulated-list-entries))
  1169. (tabulated-list-print)))
  1170. (defun org-lint--ignore-checker ()
  1171. "Ignore all reports from checker that generated the report at point.
  1172. Checker will also be ignored in all subsequent reports."
  1173. (interactive)
  1174. (setf org-lint--local-checkers
  1175. (remove (org-lint--current-checker) org-lint--local-checkers))
  1176. (org-lint--hide-checker))
  1177. ;;; Public function
  1178. ;;;###autoload
  1179. (defun org-lint (&optional arg)
  1180. "Check current Org buffer for syntax mistakes.
  1181. By default, run all checkers. With a `\\[universal-argument]' prefix ARG, \
  1182. select one
  1183. category of checkers only. With a `\\[universal-argument] \
  1184. \\[universal-argument]' prefix, run one precise
  1185. checker by its name.
  1186. ARG can also be a list of checker names, as symbols, to run."
  1187. (interactive "P")
  1188. (unless (derived-mode-p 'org-mode) (user-error "Not in an Org buffer"))
  1189. (when (called-interactively-p 'any)
  1190. (message "Org linting process starting..."))
  1191. (let ((checkers
  1192. (pcase arg
  1193. (`nil org-lint--checkers)
  1194. (`(4)
  1195. (let ((category
  1196. (completing-read
  1197. "Checker category: "
  1198. (mapcar #'org-lint-checker-categories org-lint--checkers)
  1199. nil t)))
  1200. (cl-remove-if-not
  1201. (lambda (c)
  1202. (assoc-string (org-lint-checker-categories c) category))
  1203. org-lint--checkers)))
  1204. (`(16)
  1205. (list
  1206. (let ((name (completing-read
  1207. "Checker name: "
  1208. (mapcar #'org-lint-checker-name org-lint--checkers)
  1209. nil t)))
  1210. (catch 'exit
  1211. (dolist (c org-lint--checkers)
  1212. (when (string= (org-lint-checker-name c) name)
  1213. (throw 'exit c)))))))
  1214. ((pred consp)
  1215. (cl-remove-if-not (lambda (c) (memq (org-lint-checker-name c) arg))
  1216. org-lint--checkers))
  1217. (_ (user-error "Invalid argument `%S' for `org-lint'" arg)))))
  1218. (if (not (called-interactively-p 'any))
  1219. (org-lint--generate-reports (current-buffer) checkers)
  1220. (org-lint--display-reports (current-buffer) checkers)
  1221. (message "Org linting process completed"))))
  1222. (provide 'org-lint)
  1223. ;; Local variables:
  1224. ;; generated-autoload-file: "org-loaddefs.el"
  1225. ;; End:
  1226. ;;; org-lint.el ends here