org-lint.el 43 KB

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