org-lint.el 42 KB

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