org-lint.el 44 KB

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