org-lint.el 41 KB

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