org-lint.el 39 KB

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