org-lint.el 42 KB

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