org-lint.el 44 KB

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