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