org-lint.el 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. ;;; org-lint.el --- Linting for Org documents -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015-2022 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 (string-equal-ignore-case
  308. "CUSTOM_ID" (org-element-property :key property))
  309. (org-element-property :value property)))
  310. (lambda (property _) (org-element-property :begin property))
  311. (lambda (key) (format "Duplicate CUSTOM_ID property \"%s\"" key))))
  312. (defun org-lint-duplicate-name (ast)
  313. (org-lint--collect-duplicates
  314. ast
  315. org-element-all-elements
  316. (lambda (datum) (org-element-property :name datum))
  317. (lambda (datum name)
  318. (goto-char (org-element-property :begin datum))
  319. (re-search-forward
  320. (format "^[ \t]*#\\+[A-Za-z]+:[ \t]*%s[ \t]*$" (regexp-quote name)))
  321. (match-beginning 0))
  322. (lambda (key) (format "Duplicate NAME \"%s\"" key))))
  323. (defun org-lint-duplicate-target (ast)
  324. (org-lint--collect-duplicates
  325. ast
  326. 'target
  327. (lambda (target) (split-string (org-element-property :value target)))
  328. (lambda (target _) (org-element-property :begin target))
  329. (lambda (key)
  330. (format "Duplicate target <<%s>>" (mapconcat #'identity key " ")))))
  331. (defun org-lint-duplicate-footnote-definition (ast)
  332. (org-lint--collect-duplicates
  333. ast
  334. 'footnote-definition
  335. (lambda (definition) (org-element-property :label definition))
  336. (lambda (definition _) (org-element-property :post-affiliated definition))
  337. (lambda (key) (format "Duplicate footnote definition \"%s\"" key))))
  338. (defun org-lint-orphaned-affiliated-keywords (ast)
  339. ;; Ignore orphan RESULTS keywords, which could be generated from
  340. ;; a source block returning no value.
  341. (let ((keywords (cl-set-difference org-element-affiliated-keywords
  342. '("RESULT" "RESULTS")
  343. :test #'equal)))
  344. (org-element-map ast 'keyword
  345. (lambda (k)
  346. (let ((key (org-element-property :key k)))
  347. (and (or (let ((case-fold-search t))
  348. (string-match-p "\\`ATTR_[-_A-Za-z0-9]+\\'" key))
  349. (member key keywords))
  350. (list (org-element-property :post-affiliated k)
  351. (format "Orphaned affiliated keyword: \"%s\"" key))))))))
  352. (defun org-lint-obsolete-affiliated-keywords (_)
  353. (let ((regexp (format "^[ \t]*#\\+%s:"
  354. (regexp-opt '("DATA" "LABEL" "RESNAME" "SOURCE"
  355. "SRCNAME" "TBLNAME" "RESULT" "HEADERS")
  356. t)))
  357. reports)
  358. (while (re-search-forward regexp nil t)
  359. (let ((key (upcase (match-string-no-properties 1))))
  360. (when (< (point)
  361. (org-element-property :post-affiliated (org-element-at-point)))
  362. (push
  363. (list (line-beginning-position)
  364. (format
  365. "Obsolete affiliated keyword: \"%s\". Use \"%s\" instead"
  366. key
  367. (pcase key
  368. ("HEADERS" "HEADER")
  369. ("RESULT" "RESULTS")
  370. (_ "NAME"))))
  371. reports))))
  372. reports))
  373. (defun org-lint-deprecated-export-blocks (ast)
  374. (let ((deprecated '("ASCII" "BEAMER" "HTML" "LATEX" "MAN" "MARKDOWN" "MD"
  375. "ODT" "ORG" "TEXINFO")))
  376. (org-element-map ast 'special-block
  377. (lambda (b)
  378. (let ((type (org-element-property :type b)))
  379. (when (member-ignore-case type deprecated)
  380. (list
  381. (org-element-property :post-affiliated b)
  382. (format
  383. "Deprecated syntax for export block. Use \"BEGIN_EXPORT %s\" \
  384. instead"
  385. type))))))))
  386. (defun org-lint-deprecated-header-syntax (ast)
  387. (let* ((deprecated-babel-properties
  388. ;; DIR is also used for attachments.
  389. (delete "dir"
  390. (mapcar (lambda (arg) (downcase (symbol-name (car arg))))
  391. org-babel-common-header-args-w-values)))
  392. (deprecated-re
  393. (format "\\`%s[ \t]" (regexp-opt deprecated-babel-properties t))))
  394. (org-element-map ast '(keyword node-property)
  395. (lambda (datum)
  396. (let ((key (org-element-property :key datum)))
  397. (pcase (org-element-type datum)
  398. (`keyword
  399. (let ((value (org-element-property :value datum)))
  400. (and (string= key "PROPERTY")
  401. (string-match deprecated-re value)
  402. (list (org-element-property :begin datum)
  403. (format "Deprecated syntax for \"%s\". \
  404. Use header-args instead"
  405. (match-string-no-properties 1 value))))))
  406. (`node-property
  407. (and (member-ignore-case key deprecated-babel-properties)
  408. (list
  409. (org-element-property :begin datum)
  410. (format "Deprecated syntax for \"%s\". \
  411. Use :header-args: instead"
  412. key))))))))))
  413. (defun org-lint-missing-language-in-src-block (ast)
  414. (org-element-map ast 'src-block
  415. (lambda (b)
  416. (unless (org-element-property :language b)
  417. (list (org-element-property :post-affiliated b)
  418. "Missing language in source block")))))
  419. (defun org-lint-missing-backend-in-export-block (ast)
  420. (org-element-map ast 'export-block
  421. (lambda (b)
  422. (unless (org-element-property :type b)
  423. (list (org-element-property :post-affiliated b)
  424. "Missing back-end in export block")))))
  425. (defun org-lint-invalid-babel-call-block (ast)
  426. (org-element-map ast 'babel-call
  427. (lambda (b)
  428. (cond
  429. ((not (org-element-property :call b))
  430. (list (org-element-property :post-affiliated b)
  431. "Invalid syntax in babel call block"))
  432. ((let ((h (org-element-property :end-header b)))
  433. (and h (string-match-p "\\`\\[.*\\]\\'" h)))
  434. (list
  435. (org-element-property :post-affiliated b)
  436. "Babel call's end header must not be wrapped within brackets"))))))
  437. (defun org-lint-deprecated-category-setup (ast)
  438. (org-element-map ast 'keyword
  439. (let (category-flag)
  440. (lambda (k)
  441. (cond
  442. ((not (string= (org-element-property :key k) "CATEGORY")) nil)
  443. (category-flag
  444. (list (org-element-property :post-affiliated k)
  445. "Spurious CATEGORY keyword. Set :CATEGORY: property instead"))
  446. (t (setf category-flag t) nil))))))
  447. (defun org-lint-invalid-coderef-link (ast)
  448. (let ((info (list :parse-tree ast)))
  449. (org-element-map ast 'link
  450. (lambda (link)
  451. (let ((ref (org-element-property :path link)))
  452. (and (equal (org-element-property :type link) "coderef")
  453. (not (ignore-errors (org-export-resolve-coderef ref info)))
  454. (list (org-element-property :begin link)
  455. (format "Unknown coderef \"%s\"" ref))))))))
  456. (defun org-lint-invalid-custom-id-link (ast)
  457. (let ((info (list :parse-tree ast)))
  458. (org-element-map ast 'link
  459. (lambda (link)
  460. (and (equal (org-element-property :type link) "custom-id")
  461. (not (ignore-errors (org-export-resolve-id-link link info)))
  462. (list (org-element-property :begin link)
  463. (format "Unknown custom ID \"%s\""
  464. (org-element-property :path link))))))))
  465. (defun org-lint-invalid-fuzzy-link (ast)
  466. (let ((info (list :parse-tree ast)))
  467. (org-element-map ast 'link
  468. (lambda (link)
  469. (and (equal (org-element-property :type link) "fuzzy")
  470. (not (ignore-errors (org-export-resolve-fuzzy-link link info)))
  471. (list (org-element-property :begin link)
  472. (format "Unknown fuzzy location \"%s\""
  473. (let ((path (org-element-property :path link)))
  474. (if (string-prefix-p "*" path)
  475. (substring path 1)
  476. path)))))))))
  477. (defun org-lint-invalid-id-link (ast)
  478. (org-element-map ast 'link
  479. (lambda (link)
  480. (let ((id (org-element-property :path link)))
  481. (and (equal (org-element-property :type link) "id")
  482. (not (org-id-find id))
  483. (list (org-element-property :begin link)
  484. (format "Unknown ID \"%s\"" id)))))))
  485. (defun org-lint-special-property-in-properties-drawer (ast)
  486. (org-element-map ast 'node-property
  487. (lambda (p)
  488. (let ((key (org-element-property :key p)))
  489. (and (member-ignore-case key org-special-properties)
  490. (list (org-element-property :begin p)
  491. (format
  492. "Special property \"%s\" found in a properties drawer"
  493. key)))))))
  494. (defun org-lint-obsolete-properties-drawer (ast)
  495. (org-element-map ast 'drawer
  496. (lambda (d)
  497. (when (equal (org-element-property :drawer-name d) "PROPERTIES")
  498. (let ((headline? (org-element-lineage d '(headline)))
  499. (before
  500. (mapcar #'org-element-type
  501. (assq d (reverse (org-element-contents
  502. (org-element-property :parent d)))))))
  503. (list (org-element-property :post-affiliated d)
  504. (if (or (and headline? (member before '(nil (planning))))
  505. (and (null headline?) (member before '(nil (comment)))))
  506. "Incorrect contents for PROPERTIES drawer"
  507. "Incorrect location for PROPERTIES drawer")))))))
  508. (defun org-lint-invalid-effort-property (ast)
  509. (org-element-map ast 'node-property
  510. (lambda (p)
  511. (when (equal "EFFORT" (org-element-property :key p))
  512. (let ((value (org-element-property :value p)))
  513. (and (org-string-nw-p value)
  514. (not (org-duration-p value))
  515. (list (org-element-property :begin p)
  516. (format "Invalid effort duration format: %S" value))))))))
  517. (defun org-lint-link-to-local-file (ast)
  518. (org-element-map ast 'link
  519. (lambda (l)
  520. (let ((type (org-element-property :type l)))
  521. (pcase type
  522. ((or "attachment" "file")
  523. (let* ((path (org-element-property :path l))
  524. (file (if (string= type "file")
  525. path
  526. (org-with-point-at (org-element-property :begin l)
  527. (org-attach-expand path)))))
  528. (and (not (file-remote-p file))
  529. (not (file-exists-p file))
  530. (list (org-element-property :begin l)
  531. (format (if (org-element-lineage l '(link))
  532. "Link to non-existent image file %S \
  533. in description"
  534. "Link to non-existent local file %S")
  535. file)))))
  536. (_ nil))))))
  537. (defun org-lint-non-existent-setupfile-parameter (ast)
  538. (org-element-map ast 'keyword
  539. (lambda (k)
  540. (when (equal (org-element-property :key k) "SETUPFILE")
  541. (let ((file (org-unbracket-string
  542. "\"" "\""
  543. (org-element-property :value k))))
  544. (and (not (org-url-p file))
  545. (not (file-remote-p file))
  546. (not (file-exists-p file))
  547. (list (org-element-property :begin k)
  548. (format "Non-existent setup file %S" file))))))))
  549. (defun org-lint-wrong-include-link-parameter (ast)
  550. (org-element-map ast 'keyword
  551. (lambda (k)
  552. (when (equal (org-element-property :key k) "INCLUDE")
  553. (let* ((value (org-element-property :value k))
  554. (path
  555. (and (string-match "^\\(\".+\"\\|\\S-+\\)[ \t]*" value)
  556. (save-match-data
  557. (org-strip-quotes (match-string 1 value))))))
  558. (if (not path)
  559. (list (org-element-property :post-affiliated k)
  560. "Missing location argument in INCLUDE keyword")
  561. (let* ((file (org-string-nw-p
  562. (if (string-match "::\\(.*\\)\\'" path)
  563. (substring path 0 (match-beginning 0))
  564. path)))
  565. (search (and (not (equal file path))
  566. (org-string-nw-p (match-string 1 path)))))
  567. (if (and file
  568. (not (file-remote-p file))
  569. (not (file-exists-p file)))
  570. (list (org-element-property :post-affiliated k)
  571. "Non-existent file argument in INCLUDE keyword")
  572. (let* ((visiting (if file (find-buffer-visiting file)
  573. (current-buffer)))
  574. (buffer (or visiting (find-file-noselect file)))
  575. (org-link-search-must-match-exact-headline t))
  576. (unwind-protect
  577. (with-current-buffer buffer
  578. (when (and search
  579. (not (ignore-errors
  580. (org-link-search search nil t))))
  581. (list (org-element-property :post-affiliated k)
  582. (format
  583. "Invalid search part \"%s\" in INCLUDE keyword"
  584. search))))
  585. (unless visiting (kill-buffer buffer))))))))))))
  586. (defun org-lint-obsolete-include-markup (ast)
  587. (let ((regexp (format "\\`\\(?:\".+\"\\|\\S-+\\)[ \t]+%s"
  588. (regexp-opt
  589. '("ASCII" "BEAMER" "HTML" "LATEX" "MAN" "MARKDOWN" "MD"
  590. "ODT" "ORG" "TEXINFO")
  591. t))))
  592. (org-element-map ast 'keyword
  593. (lambda (k)
  594. (when (equal (org-element-property :key k) "INCLUDE")
  595. (let ((case-fold-search t)
  596. (value (org-element-property :value k)))
  597. (when (string-match regexp value)
  598. (let ((markup (match-string-no-properties 1 value)))
  599. (list (org-element-property :post-affiliated k)
  600. (format "Obsolete markup \"%s\" in INCLUDE keyword. \
  601. Use \"export %s\" instead"
  602. markup
  603. markup))))))))))
  604. (defun org-lint-unknown-options-item (ast)
  605. (let ((allowed (delq nil
  606. (append
  607. (mapcar (lambda (o) (nth 2 o)) org-export-options-alist)
  608. (cl-mapcan
  609. (lambda (b)
  610. (mapcar (lambda (o) (nth 2 o))
  611. (org-export-backend-options b)))
  612. org-export-registered-backends))))
  613. reports)
  614. (org-element-map ast 'keyword
  615. (lambda (k)
  616. (when (string= (org-element-property :key k) "OPTIONS")
  617. (let ((value (org-element-property :value k))
  618. (start 0))
  619. (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-+\\)?[ \t]*"
  620. value
  621. start)
  622. (setf start (match-end 0))
  623. (let ((item (match-string 1 value)))
  624. (unless (member item allowed)
  625. (push (list (org-element-property :post-affiliated k)
  626. (format "Unknown OPTIONS item \"%s\"" item))
  627. reports))
  628. (unless (match-string 2 value)
  629. (push (list (org-element-property :post-affiliated k)
  630. (format "Missing value for option item %S" item))
  631. reports))))))))
  632. reports))
  633. (defun org-lint-invalid-macro-argument-and-template (ast)
  634. (let* ((reports nil)
  635. (extract-placeholders
  636. (lambda (template)
  637. (let ((start 0)
  638. args)
  639. (while (string-match "\\$\\([1-9][0-9]*\\)" template start)
  640. (setf start (match-end 0))
  641. (push (string-to-number (match-string 1 template)) args))
  642. (sort (org-uniquify args) #'<))))
  643. (check-arity
  644. (lambda (arity macro)
  645. (let* ((name (org-element-property :key macro))
  646. (pos (org-element-property :begin macro))
  647. (args (org-element-property :args macro))
  648. (l (length args)))
  649. (cond
  650. ((< l (1- (car arity)))
  651. (push (list pos (format "Missing arguments in macro %S" name))
  652. reports))
  653. ((< l (car arity))
  654. (push (list pos (format "Missing argument in macro %S" name))
  655. reports))
  656. ((> l (1+ (cdr arity)))
  657. (push (let ((spurious-args (nthcdr (cdr arity) args)))
  658. (list pos
  659. (format "Spurious arguments in macro %S: %s"
  660. name
  661. (mapconcat #'org-trim spurious-args ", "))))
  662. reports))
  663. ((> l (cdr arity))
  664. (push (list pos
  665. (format "Spurious argument in macro %S: %s"
  666. name
  667. (org-last args)))
  668. reports))
  669. (t nil))))))
  670. ;; Check arguments for macro templates.
  671. (org-element-map ast 'keyword
  672. (lambda (k)
  673. (when (string= (org-element-property :key k) "MACRO")
  674. (let* ((value (org-element-property :value k))
  675. (name (and (string-match "^\\S-+" value)
  676. (match-string 0 value)))
  677. (template (and name
  678. (org-trim (substring value (match-end 0))))))
  679. (cond
  680. ((not name)
  681. (push (list (org-element-property :post-affiliated k)
  682. "Missing name in MACRO keyword")
  683. reports))
  684. ((not (org-string-nw-p template))
  685. (push (list (org-element-property :post-affiliated k)
  686. "Missing template in macro \"%s\"" name)
  687. reports))
  688. (t
  689. (unless (let ((args (funcall extract-placeholders template)))
  690. (equal (number-sequence 1 (or (org-last args) 0)) args))
  691. (push (list (org-element-property :post-affiliated k)
  692. (format "Unused placeholders in macro \"%s\""
  693. name))
  694. reports))))))))
  695. ;; Check arguments for macros.
  696. (org-macro-initialize-templates)
  697. (let ((templates (append
  698. (mapcar (lambda (m) (cons m "$1"))
  699. '("author" "date" "email" "title" "results"))
  700. org-macro-templates)))
  701. (org-element-map ast 'macro
  702. (lambda (macro)
  703. (let* ((name (org-element-property :key macro))
  704. (template (cdr (assoc-string name templates t))))
  705. (pcase template
  706. (`nil
  707. (push (list (org-element-property :begin macro)
  708. (format "Undefined macro %S" name))
  709. reports))
  710. ((guard (string= name "keyword"))
  711. (funcall check-arity '(1 . 1) macro))
  712. ((guard (string= name "modification-time"))
  713. (funcall check-arity '(1 . 2) macro))
  714. ((guard (string= name "n"))
  715. (funcall check-arity '(0 . 2) macro))
  716. ((guard (string= name "property"))
  717. (funcall check-arity '(1 . 2) macro))
  718. ((guard (string= name "time"))
  719. (funcall check-arity '(1 . 1) macro))
  720. ((pred functionp)) ;ignore (eval ...) templates
  721. (_
  722. (let* ((arg-numbers (funcall extract-placeholders template))
  723. (arity (if (null arg-numbers)
  724. '(0 . 0)
  725. (let ((m (apply #'max arg-numbers)))
  726. (cons m m)))))
  727. (funcall check-arity arity macro))))))))
  728. reports))
  729. (defun org-lint-undefined-footnote-reference (ast)
  730. (let ((definitions
  731. (org-element-map ast '(footnote-definition footnote-reference)
  732. (lambda (f)
  733. (and (or (eq 'footnote-definition (org-element-type f))
  734. (eq 'inline (org-element-property :type f)))
  735. (org-element-property :label f))))))
  736. (org-element-map ast 'footnote-reference
  737. (lambda (f)
  738. (let ((label (org-element-property :label f)))
  739. (and (eq 'standard (org-element-property :type f))
  740. (not (member label definitions))
  741. (list (org-element-property :begin f)
  742. (format "Missing definition for footnote [%s]"
  743. label))))))))
  744. (defun org-lint-unreferenced-footnote-definition (ast)
  745. (let ((references (org-element-map ast 'footnote-reference
  746. (lambda (f) (org-element-property :label f)))))
  747. (org-element-map ast 'footnote-definition
  748. (lambda (f)
  749. (let ((label (org-element-property :label f)))
  750. (and label
  751. (not (member label references))
  752. (list (org-element-property :post-affiliated f)
  753. (format "No reference for footnote definition [%s]"
  754. label))))))))
  755. (defun org-lint-colon-in-name (ast)
  756. (org-element-map ast org-element-all-elements
  757. (lambda (e)
  758. (let ((name (org-element-property :name e)))
  759. (and name
  760. (string-match-p ":" name)
  761. (list (progn
  762. (goto-char (org-element-property :begin e))
  763. (re-search-forward
  764. (format "^[ \t]*#\\+\\w+: +%s *$" (regexp-quote name)))
  765. (match-beginning 0))
  766. (format
  767. "Name \"%s\" contains a colon; Babel cannot use it as input"
  768. name)))))))
  769. (defun org-lint-misplaced-planning-info (_)
  770. (let ((case-fold-search t)
  771. reports)
  772. (while (re-search-forward org-planning-line-re nil t)
  773. (unless (memq (org-element-type (org-element-at-point))
  774. '(comment-block example-block export-block planning
  775. src-block verse-block))
  776. (push (list (line-beginning-position) "Misplaced planning info line")
  777. reports)))
  778. reports))
  779. (defun org-lint-incomplete-drawer (_)
  780. (let (reports)
  781. (while (re-search-forward org-drawer-regexp nil t)
  782. (let ((name (org-trim (match-string-no-properties 0)))
  783. (element (org-element-at-point)))
  784. (pcase (org-element-type element)
  785. (`drawer
  786. ;; Find drawer opening lines within non-empty drawers.
  787. (let ((end (org-element-property :contents-end element)))
  788. (when end
  789. (while (re-search-forward org-drawer-regexp end t)
  790. (let ((n (org-trim (match-string-no-properties 0))))
  791. (push (list (line-beginning-position)
  792. (format "Possible misleading drawer entry %S" n))
  793. reports))))
  794. (goto-char (org-element-property :end element))))
  795. (`property-drawer
  796. (goto-char (org-element-property :end element)))
  797. ((or `comment-block `example-block `export-block `src-block
  798. `verse-block)
  799. nil)
  800. (_
  801. ;; Find drawer opening lines outside of any drawer.
  802. (push (list (line-beginning-position)
  803. (format "Possible incomplete drawer %S" name))
  804. reports)))))
  805. reports))
  806. (defun org-lint-indented-diary-sexp (_)
  807. (let (reports)
  808. (while (re-search-forward "^[ \t]+%%(" nil t)
  809. (unless (memq (org-element-type (org-element-at-point))
  810. '(comment-block diary-sexp example-block export-block
  811. src-block verse-block))
  812. (push (list (line-beginning-position) "Possible indented diary-sexp")
  813. reports)))
  814. reports))
  815. (defun org-lint-invalid-block (_)
  816. (let ((case-fold-search t)
  817. (regexp "^[ \t]*#\\+\\(BEGIN\\|END\\)\\(?::\\|_[^[:space:]]*\\)?[ \t]*")
  818. reports)
  819. (while (re-search-forward regexp nil t)
  820. (let ((name (org-trim (buffer-substring-no-properties
  821. (line-beginning-position) (line-end-position)))))
  822. (cond
  823. ((and (string-prefix-p "END" (match-string 1) t)
  824. (not (eolp)))
  825. (push (list (line-beginning-position)
  826. (format "Invalid block closing line \"%s\"" name))
  827. reports))
  828. ((not (memq (org-element-type (org-element-at-point))
  829. '(center-block comment-block dynamic-block example-block
  830. export-block quote-block special-block
  831. src-block verse-block)))
  832. (push (list (line-beginning-position)
  833. (format "Possible incomplete block \"%s\""
  834. name))
  835. reports)))))
  836. reports))
  837. (defun org-lint-invalid-keyword-syntax (_)
  838. (let ((regexp "^[ \t]*#\\+\\([^[:space:]:]*\\)\\(?: \\|$\\)")
  839. (exception-re
  840. (format "[ \t]*#\\+%s\\(\\[.*\\]\\)?:\\(?: \\|$\\)"
  841. (regexp-opt org-element-dual-keywords)))
  842. reports)
  843. (while (re-search-forward regexp nil t)
  844. (let ((name (match-string-no-properties 1)))
  845. (unless (or (string-prefix-p "BEGIN" name t)
  846. (string-prefix-p "END" name t)
  847. (save-excursion
  848. (beginning-of-line)
  849. (let ((case-fold-search t)) (looking-at exception-re))))
  850. (push (list (match-beginning 0)
  851. (format "Possible missing colon in keyword \"%s\"" name))
  852. reports))))
  853. reports))
  854. (defun org-lint-extraneous-element-in-footnote-section (ast)
  855. (org-element-map ast 'headline
  856. (lambda (h)
  857. (and (org-element-property :footnote-section-p h)
  858. (org-element-map (org-element-contents h)
  859. (cl-remove-if
  860. (lambda (e)
  861. (memq e '(comment comment-block footnote-definition
  862. property-drawer section)))
  863. org-element-all-elements)
  864. (lambda (e)
  865. (not (and (eq (org-element-type e) 'headline)
  866. (org-element-property :commentedp e))))
  867. nil t '(footnote-definition property-drawer))
  868. (list (org-element-property :begin h)
  869. "Extraneous elements in footnote section are not exported")))))
  870. (defun org-lint-quote-section (ast)
  871. (org-element-map ast '(headline inlinetask)
  872. (lambda (h)
  873. (let ((title (org-element-property :raw-value h)))
  874. (and (or (string-prefix-p "QUOTE " title)
  875. (string-prefix-p (concat org-comment-string " QUOTE ") title))
  876. (list (org-element-property :begin h)
  877. "Deprecated QUOTE section"))))))
  878. (defun org-lint-file-application (ast)
  879. (org-element-map ast 'link
  880. (lambda (l)
  881. (let ((app (org-element-property :application l)))
  882. (and app
  883. (list (org-element-property :begin l)
  884. (format "Deprecated \"file+%s\" link type" app)))))))
  885. (defun org-lint-percent-encoding-link-escape (ast)
  886. (org-element-map ast 'link
  887. (lambda (l)
  888. (when (eq 'bracket (org-element-property :format l))
  889. (let* ((uri (org-element-property :path l))
  890. (start 0)
  891. (obsolete-flag
  892. (catch :obsolete
  893. (while (string-match "%\\(..\\)?" uri start)
  894. (setq start (match-end 0))
  895. (unless (member (match-string 1 uri) '("25" "5B" "5D" "20"))
  896. (throw :obsolete nil)))
  897. (string-match-p "%" uri))))
  898. (when obsolete-flag
  899. (list (org-element-property :begin l)
  900. "Link escaped with obsolete percent-encoding syntax")))))))
  901. (defun org-lint-wrong-header-argument (ast)
  902. (let* ((reports)
  903. (verify
  904. (lambda (datum language headers)
  905. (let ((allowed
  906. ;; If LANGUAGE is specified, restrict allowed
  907. ;; headers to both LANGUAGE-specific and default
  908. ;; ones. Otherwise, accept headers from any loaded
  909. ;; language.
  910. (append
  911. org-babel-header-arg-names
  912. (cl-mapcan
  913. (lambda (l)
  914. (let ((v (intern (format "org-babel-header-args:%s" l))))
  915. (and (boundp v) (mapcar #'car (symbol-value v)))))
  916. (if language (list language)
  917. (mapcar #'car org-babel-load-languages))))))
  918. (dolist (header headers)
  919. (let ((h (symbol-name (car header)))
  920. (p (or (org-element-property :post-affiliated datum)
  921. (org-element-property :begin datum))))
  922. (cond
  923. ((not (string-prefix-p ":" h))
  924. (push
  925. (list p
  926. (format "Missing colon in header argument \"%s\"" h))
  927. reports))
  928. ((assoc-string (substring h 1) allowed))
  929. (t (push (list p (format "Unknown header argument \"%s\"" h))
  930. reports)))))))))
  931. (org-element-map ast '(babel-call inline-babel-call inline-src-block keyword
  932. node-property src-block)
  933. (lambda (datum)
  934. (pcase (org-element-type datum)
  935. ((or `babel-call `inline-babel-call)
  936. (funcall verify
  937. datum
  938. nil
  939. (cl-mapcan #'org-babel-parse-header-arguments
  940. (list
  941. (org-element-property :inside-header datum)
  942. (org-element-property :end-header datum)))))
  943. (`inline-src-block
  944. (funcall verify
  945. datum
  946. (org-element-property :language datum)
  947. (org-babel-parse-header-arguments
  948. (org-element-property :parameters datum))))
  949. (`keyword
  950. (when (string= (org-element-property :key datum) "PROPERTY")
  951. (let ((value (org-element-property :value datum)))
  952. (when (string-match "\\`header-args\\(?::\\(\\S-+\\)\\)?\\+? *"
  953. value)
  954. (funcall verify
  955. datum
  956. (match-string 1 value)
  957. (org-babel-parse-header-arguments
  958. (substring value (match-end 0))))))))
  959. (`node-property
  960. (let ((key (org-element-property :key datum)))
  961. (when (let ((case-fold-search t))
  962. (string-match "\\`HEADER-ARGS\\(?::\\(\\S-+\\)\\)?\\+?"
  963. key))
  964. (funcall verify
  965. datum
  966. (match-string 1 key)
  967. (org-babel-parse-header-arguments
  968. (org-element-property :value datum))))))
  969. (`src-block
  970. (funcall verify
  971. datum
  972. (org-element-property :language datum)
  973. (cl-mapcan #'org-babel-parse-header-arguments
  974. (cons (org-element-property :parameters datum)
  975. (org-element-property :header datum))))))))
  976. reports))
  977. (defun org-lint-wrong-header-value (ast)
  978. (let (reports)
  979. (org-element-map ast
  980. '(babel-call inline-babel-call inline-src-block src-block)
  981. (lambda (datum)
  982. (let* ((type (org-element-type datum))
  983. (language (org-element-property :language datum))
  984. (allowed-header-values
  985. (append (and language
  986. (let ((v (intern (concat "org-babel-header-args:"
  987. language))))
  988. (and (boundp v) (symbol-value v))))
  989. org-babel-common-header-args-w-values))
  990. (datum-header-values
  991. (org-babel-parse-header-arguments
  992. (org-trim
  993. (pcase type
  994. (`src-block
  995. (mapconcat
  996. #'identity
  997. (cons (org-element-property :parameters datum)
  998. (org-element-property :header datum))
  999. " "))
  1000. (`inline-src-block
  1001. (or (org-element-property :parameters datum) ""))
  1002. (_
  1003. (concat
  1004. (org-element-property :inside-header datum)
  1005. " "
  1006. (org-element-property :end-header datum))))))))
  1007. (dolist (header datum-header-values)
  1008. (let ((allowed-values
  1009. (cdr (assoc-string (substring (symbol-name (car header)) 1)
  1010. allowed-header-values))))
  1011. (unless (memq allowed-values '(:any nil))
  1012. (let ((values (cdr header))
  1013. groups-alist)
  1014. (dolist (v (if (stringp values) (split-string values)
  1015. (list values)))
  1016. (let ((valid-value nil))
  1017. (catch 'exit
  1018. (dolist (group allowed-values)
  1019. (cond
  1020. ((not (funcall
  1021. (if (stringp v) #'assoc-string #'assoc)
  1022. v group))
  1023. (when (memq :any group)
  1024. (setf valid-value t)
  1025. (push (cons group v) groups-alist)))
  1026. ((assq group groups-alist)
  1027. (push
  1028. (list
  1029. (or (org-element-property :post-affiliated datum)
  1030. (org-element-property :begin datum))
  1031. (format
  1032. "Forbidden combination in header \"%s\": %s, %s"
  1033. (car header)
  1034. (cdr (assq group groups-alist))
  1035. v))
  1036. reports)
  1037. (throw 'exit nil))
  1038. (t (push (cons group v) groups-alist)
  1039. (setf valid-value t))))
  1040. (unless valid-value
  1041. (push
  1042. (list
  1043. (or (org-element-property :post-affiliated datum)
  1044. (org-element-property :begin datum))
  1045. (format "Unknown value \"%s\" for header \"%s\""
  1046. v
  1047. (car header)))
  1048. reports))))))))))))
  1049. reports))
  1050. (defun org-lint-spurious-colons (ast)
  1051. (org-element-map ast '(headline inlinetask)
  1052. (lambda (h)
  1053. (when (member "" (org-element-property :tags h))
  1054. (list (org-element-property :begin h)
  1055. "Tags contain a spurious colon")))))
  1056. ;;; Reports UI
  1057. (defvar org-lint--report-mode-map
  1058. (let ((map (make-sparse-keymap)))
  1059. (set-keymap-parent map tabulated-list-mode-map)
  1060. (define-key map (kbd "RET") 'org-lint--jump-to-source)
  1061. (define-key map (kbd "TAB") 'org-lint--show-source)
  1062. (define-key map (kbd "C-j") 'org-lint--show-source)
  1063. (define-key map (kbd "h") 'org-lint--hide-checker)
  1064. (define-key map (kbd "i") 'org-lint--ignore-checker)
  1065. map)
  1066. "Local keymap for `org-lint--report-mode' buffers.")
  1067. (define-derived-mode org-lint--report-mode tabulated-list-mode "OrgLint"
  1068. "Major mode used to display reports emitted during linting.
  1069. \\{org-lint--report-mode-map}"
  1070. (setf tabulated-list-format
  1071. `[("Line" 6
  1072. (lambda (a b)
  1073. (< (string-to-number (aref (cadr a) 0))
  1074. (string-to-number (aref (cadr b) 0))))
  1075. :right-align t)
  1076. ("Trust" 5 t)
  1077. ("Warning" 0 t)])
  1078. (tabulated-list-init-header))
  1079. (defun org-lint--generate-reports (buffer checkers)
  1080. "Generate linting report for BUFFER.
  1081. CHECKERS is the list of checkers used.
  1082. Return an alist (ID [LINE TRUST DESCRIPTION CHECKER]), suitable
  1083. for `tabulated-list-printer'."
  1084. (with-current-buffer buffer
  1085. (save-excursion
  1086. (goto-char (point-min))
  1087. (let ((ast (org-element-parse-buffer))
  1088. (id 0)
  1089. (last-line 1)
  1090. (last-pos 1))
  1091. ;; Insert unique ID for each report. Replace buffer positions
  1092. ;; with line numbers.
  1093. (mapcar
  1094. (lambda (report)
  1095. (list
  1096. (cl-incf id)
  1097. (apply #'vector
  1098. (cons
  1099. (progn
  1100. (goto-char (car report))
  1101. (beginning-of-line)
  1102. (prog1 (number-to-string
  1103. (cl-incf last-line
  1104. (count-lines last-pos (point))))
  1105. (setf last-pos (point))))
  1106. (cdr report)))))
  1107. ;; Insert trust level in generated reports. Also sort them
  1108. ;; by buffer position in order to optimize lines computation.
  1109. (sort (cl-mapcan
  1110. (lambda (c)
  1111. (let ((trust (symbol-name (org-lint-checker-trust c))))
  1112. (mapcar
  1113. (lambda (report)
  1114. (list (car report) trust (nth 1 report) c))
  1115. (save-excursion
  1116. (funcall
  1117. (intern (format "org-lint-%s"
  1118. (org-lint-checker-name c)))
  1119. ast)))))
  1120. checkers)
  1121. #'car-less-than-car))))))
  1122. (defvar-local org-lint--source-buffer nil
  1123. "Source buffer associated to current report buffer.")
  1124. (defvar-local org-lint--local-checkers nil
  1125. "List of checkers used to build current report.")
  1126. (defun org-lint--refresh-reports ()
  1127. (setq tabulated-list-entries
  1128. (org-lint--generate-reports org-lint--source-buffer
  1129. org-lint--local-checkers))
  1130. (tabulated-list-print))
  1131. (defun org-lint--current-line ()
  1132. "Return current report line, as a number."
  1133. (string-to-number (aref (tabulated-list-get-entry) 0)))
  1134. (defun org-lint--current-checker (&optional entry)
  1135. "Return current report checker.
  1136. When optional argument ENTRY is non-nil, use this entry instead
  1137. of current one."
  1138. (aref (if entry (nth 1 entry) (tabulated-list-get-entry)) 3))
  1139. (defun org-lint--display-reports (source checkers)
  1140. "Display linting reports for buffer SOURCE.
  1141. CHECKERS is the list of checkers used."
  1142. (let ((buffer (get-buffer-create "*Org Lint*")))
  1143. (with-current-buffer buffer
  1144. (org-lint--report-mode)
  1145. (setf org-lint--source-buffer source)
  1146. (setf org-lint--local-checkers checkers)
  1147. (org-lint--refresh-reports)
  1148. (add-hook 'tabulated-list-revert-hook #'org-lint--refresh-reports nil t))
  1149. (pop-to-buffer buffer)))
  1150. (defun org-lint--jump-to-source ()
  1151. "Move to source line that generated the report at point."
  1152. (interactive)
  1153. (let ((l (org-lint--current-line)))
  1154. (switch-to-buffer-other-window org-lint--source-buffer)
  1155. (org-goto-line l)
  1156. (org-show-set-visibility 'local)
  1157. (recenter)))
  1158. (defun org-lint--show-source ()
  1159. "Show source line that generated the report at point."
  1160. (interactive)
  1161. (let ((buffer (current-buffer)))
  1162. (org-lint--jump-to-source)
  1163. (switch-to-buffer-other-window buffer)))
  1164. (defun org-lint--hide-checker ()
  1165. "Hide all reports from checker that generated the report at point."
  1166. (interactive)
  1167. (let ((c (org-lint--current-checker)))
  1168. (setf tabulated-list-entries
  1169. (cl-remove-if (lambda (e) (equal c (org-lint--current-checker e)))
  1170. tabulated-list-entries))
  1171. (tabulated-list-print)))
  1172. (defun org-lint--ignore-checker ()
  1173. "Ignore all reports from checker that generated the report at point.
  1174. Checker will also be ignored in all subsequent reports."
  1175. (interactive)
  1176. (setf org-lint--local-checkers
  1177. (remove (org-lint--current-checker) org-lint--local-checkers))
  1178. (org-lint--hide-checker))
  1179. ;;; Public function
  1180. ;;;###autoload
  1181. (defun org-lint (&optional arg)
  1182. "Check current Org buffer for syntax mistakes.
  1183. By default, run all checkers. With a `\\[universal-argument]' prefix ARG, \
  1184. select one
  1185. category of checkers only. With a `\\[universal-argument] \
  1186. \\[universal-argument]' prefix, run one precise
  1187. checker by its name.
  1188. ARG can also be a list of checker names, as symbols, to run."
  1189. (interactive "P")
  1190. (unless (derived-mode-p 'org-mode) (user-error "Not in an Org buffer"))
  1191. (when (called-interactively-p 'any)
  1192. (message "Org linting process starting..."))
  1193. (let ((checkers
  1194. (pcase arg
  1195. (`nil org-lint--checkers)
  1196. (`(4)
  1197. (let ((category
  1198. (completing-read
  1199. "Checker category: "
  1200. (mapcar #'org-lint-checker-categories org-lint--checkers)
  1201. nil t)))
  1202. (cl-remove-if-not
  1203. (lambda (c)
  1204. (assoc-string (org-lint-checker-categories c) category))
  1205. org-lint--checkers)))
  1206. (`(16)
  1207. (list
  1208. (let ((name (completing-read
  1209. "Checker name: "
  1210. (mapcar #'org-lint-checker-name org-lint--checkers)
  1211. nil t)))
  1212. (catch 'exit
  1213. (dolist (c org-lint--checkers)
  1214. (when (string= (org-lint-checker-name c) name)
  1215. (throw 'exit c)))))))
  1216. ((pred consp)
  1217. (cl-remove-if-not (lambda (c) (memq (org-lint-checker-name c) arg))
  1218. org-lint--checkers))
  1219. (_ (user-error "Invalid argument `%S' for `org-lint'" arg)))))
  1220. (if (not (called-interactively-p 'any))
  1221. (org-lint--generate-reports (current-buffer) checkers)
  1222. (org-lint--display-reports (current-buffer) checkers)
  1223. (message "Org linting process completed"))))
  1224. (provide 'org-lint)
  1225. ;; Local variables:
  1226. ;; generated-autoload-file: "org-loaddefs.el"
  1227. ;; End:
  1228. ;;; org-lint.el ends here