org-lint.el 42 KB

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