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/>.
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
95 ;; - misplaced planning info line
96 ;; - incomplete drawers
97 ;; - indented diary-sexps
98 ;; - obsolete QUOTE section
99 ;; - obsolete "file+application" link
105 (require 'org-element
)
113 (cl-defstruct (org-lint-checker (:copier nil
))
114 (name 'missing-checker-name
)
116 (categories '(default))
117 (trust 'high
)) ; `low' or `high'
119 (defun org-lint-missing-checker-name (_)
121 "`A checker has no `:name' property. Please verify `org-lint--checkers'"))
123 (defconst org-lint--checkers
125 (make-org-lint-checker
126 :name
'duplicate-custom-id
127 :description
"Report duplicates CUSTOM_ID properties"
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"
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"
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
)
154 (make-org-lint-checker
155 :name
'deprecated-header-syntax
156 :description
"Report deprecated Babel header syntax"
157 :categories
'(obsolete babel
)
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
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"
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"
192 (make-org-lint-checker
193 :name
'invalid-custom-id-link
194 :description
"Report \"custom-id\" links with unknown destination"
196 (make-org-lint-checker
197 :name
'invalid-fuzzy-link
198 :description
"Report \"fuzzy\" links with unknown destination"
200 (make-org-lint-checker
201 :name
'invalid-id-link
202 :description
"Report \"id\" links with unknown destination"
204 (make-org-lint-checker
205 :name
'link-to-local-file
206 :description
"Report links to non-existent local files"
209 (make-org-lint-checker
210 :name
'non-existent-setupfile-parameter
211 :description
"Report SETUPFILE keywords with non-existent file parameter"
213 (make-org-lint-checker
214 :name
'wrong-include-link-parameter
215 :description
"Report INCLUDE keywords with misleading link parameter"
216 :categories
'(export)
218 (make-org-lint-checker
219 :name
'obsolete-include-markup
220 :description
"Report obsolete markup in INCLUDE keyword"
221 :categories
'(obsolete export
)
223 (make-org-lint-checker
224 :name
'unknown-options-item
225 :description
"Report unknown items in OPTIONS keyword"
226 :categories
'(export)
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)
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"
257 (make-org-lint-checker
259 :description
"Report invalid blocks"
261 (make-org-lint-checker
262 :name
'misplaced-planning-info
263 :description
"Report misplaced planning info line"
265 (make-org-lint-checker
266 :name
'incomplete-drawer
267 :description
"Report probable incomplete drawers"
269 (make-org-lint-checker
270 :name
'indented-diary-sexp
271 :description
"Report probable indented diary-sexps"
273 (make-org-lint-checker
275 :description
"Report obsolete QUOTE section"
276 :categories
'(obsolete)
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
292 EXTRACT-POSITION is a function returning position for the report.
293 It is called with two arguments, the object or element, and the
296 BUILD-MESSAGE is a function creating the report message. It is
297 called with one argument, the key used for comparison."
302 (lambda (position value
)
303 (push (list position
(funcall build-message value
)) reports
))))
304 (org-element-map ast type
306 (let ((key (funcall extract-key datum
)))
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
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
))
333 (goto-char (org-element-property :begin datum
))
335 (format "^[ \t]*#\\+[A-Za-z]+: +%s *$" (regexp-quote name
)))
337 (lambda (key) (format "Duplicate NAME \"%s\"" key
))))
339 (defun org-lint-duplicate-target (ast)
340 (org-lint--collect-duplicates
343 (lambda (target) (org-split-string (org-element-property :value target
)))
344 (lambda (target _
) (org-element-property :begin target
))
346 (format "Duplicate target <<%s>>" (mapconcat #'identity key
" ")))))
348 (defun org-lint-duplicate-footnote-definition (ast)
349 (org-lint--collect-duplicates
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")
362 (org-element-map ast
'keyword
364 (let ((key (org-element-property :key k
)))
365 (and (or (let ((case-fold-search t
))
366 (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")
377 (while (re-search-forward regexp nil t
)
378 (let ((key (upcase (match-string-no-properties 1))))
380 (org-element-property :post-affiliated
(org-element-at-point)))
382 (list (line-beginning-position)
384 "Obsolete affiliated keyword: \"%s\". Use \"%s\" instead"
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
398 (let ((type (org-element-property :type b
)))
399 (when (member-ignore-case type deprecated
)
401 (org-element-property :post-affiliated b
)
403 "Deprecated syntax for export block. Use \"BEGIN_EXPORT %s\" \
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
))
412 (format "\\`%s[ \t]" (regexp-opt deprecated-babel-properties t
))))
413 (org-element-map ast
'(keyword node-property
)
415 (let ((key (org-element-property :key datum
)))
416 (pcase (org-element-type datum
)
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
))))))
426 (and (member-ignore-case key deprecated-babel-properties
)
428 (org-element-property :begin datum
)
429 (format "Deprecated syntax for \"%s\". \
430 Use :header-args: instead"
433 (defun org-lint-missing-language-in-src-block (ast)
434 (org-element-map ast
'src-block
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
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
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
(string-match-p "\\`\\[.*\\]\\'" h
)))
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
465 ((not (string= (org-element-property :key k
) "CATEGORY")) nil
)
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
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
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
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
)
504 (defun org-lint-invalid-id-link (ast)
505 (org-element-map ast
'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
516 (let ((key (org-element-property :key p
)))
517 (and (member-ignore-case key org-special-properties
)
518 (list (org-element-property :begin p
)
520 "Special property \"%s\" found in a properties drawer"
523 (defun org-lint-obsolete-properties-drawer (ast)
524 (org-element-map ast
'drawer
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
)
531 (goto-char (org-element-property :post-affiliated d
))
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
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\"\
548 "Link to non-existent local file \"%s\"")
551 (defun org-lint-non-existent-setupfile-parameter (ast)
552 (org-element-map ast
'keyword
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
565 (when (equal (org-element-property :key k
) "INCLUDE")
566 (let* ((value (org-element-property :value k
))
568 (and (string-match "^\\(\".+\"\\|\\S-+\\)[ \t]*" value
)
570 (org-remove-double-quotes (match-string 1 value
))))))
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))
578 (search (and (not (equal file path
))
579 (org-string-nw-p (match-string 1 path
)))))
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
)
587 (buffer (or visiting
(find-file-noselect file
))))
589 (with-current-buffer buffer
593 (let ((org-link-search-inhibit-query t
))
594 (org-link-search search nil t
)))))
595 (list (org-element-property :post-affiliated k
)
597 "Invalid search part \"%s\" in INCLUDE keyword"
599 (unless visiting
(kill-buffer buffer
))))))))))))
601 (defun org-lint-obsolete-include-markup (ast)
602 (let ((regexp (format "\\`\\(?:\".+\"\\|\\S-+\\)[ \t]+%s"
604 '("ASCII" "BEAMER" "HTML" "LATEX" "MAN" "MARKDOWN" "MD"
605 "ODT" "ORG" "TEXINFO")
607 (org-element-map ast
'keyword
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"
620 (defun org-lint-unknown-options-item (ast)
621 (let ((allowed (delq nil
623 (mapcar (lambda (o) (nth 2 o
)) org-export-options-alist
)
626 (mapcar (lambda (o) (nth 2 o
))
627 (org-export-backend-options b
)))
628 org-export-registered-backends
))))
630 (org-element-map ast
'keyword
632 (when (string= (org-element-property :key k
) "OPTIONS")
633 (let ((value (org-element-property :value k
))
635 (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-*\\)[ \t]*"
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
))
646 (defun org-lint-invalid-macro-argument-and-template (ast)
647 (let ((extract-placeholders
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
) #'<))))
656 ;; Check arguments for macro templates.
657 (org-element-map ast
'keyword
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
)))
664 (org-trim (substring value
(match-end 0))))))
667 (push (list (org-element-property :post-affiliated k
)
668 "Missing name in MACRO keyword")
670 ((not (org-string-nw-p template
))
671 (push (list (org-element-property :post-affiliated k
)
672 "Missing template in macro \"%s\"" name
)
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\""
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
689 (let* ((name (org-element-property :key macro
))
690 (template (cdr (assoc-string name templates t
))))
692 (push (list (org-element-property :begin macro
)
693 (format "Undefined macro \"%s\"" name
))
695 (let ((arg-numbers (funcall extract-placeholders template
)))
698 (nthcdr (apply #'max arg-numbers
)
699 (org-element-property :args macro
))))
702 (list (org-element-property :begin macro
)
703 (format "Unused argument%s in macro \"%s\": %s"
704 (if (> (length spurious-args
) 1) "s" "")
706 (mapconcat (lambda (a) (format "\"%s\"" a
))
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
717 (let ((label (org-element-property :label f
)))
719 (not (member label definitions
))
720 (list (org-element-property :begin f
)
721 (format "Missing definition for footnote [%s]"
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
729 (let ((label (org-element-property :label f
)))
731 (not (member label references
))
732 (list (org-element-property :post-affiliated f
)
733 (format "No reference for footnote definition [%s]"
736 (defun org-lint-colon-in-name (ast)
737 (org-element-map ast org-element-all-elements
739 (let ((name (org-element-property :name e
)))
741 (string-match-p ":" name
)
743 (goto-char (org-element-property :begin e
))
745 (format "^[ \t]*#\\+\\w+: +%s *$" (regexp-quote name
)))
748 "Name \"%s\" contains a colon; Babel cannot use it as input"
751 (defun org-lint-misplaced-planning-info (_)
752 (let ((case-fold-search t
)
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")
762 (defun org-lint-incomplete-drawer (_)
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
))
771 ((or `comment-block
`example-block
`export-block
`src-block
775 (push (list (line-beginning-position)
776 (format "Possible incomplete drawer \"%s\"" name
))
780 (defun org-lint-indented-diary-sexp (_)
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")
790 (defun org-lint-invalid-block (_)
791 (let ((case-fold-search t
)
792 (regexp "^[ \t]*#\\+\\(BEGIN\\|END\\)\\(?::\\|_[^[:space:]]*\\)?[ \t]*")
794 (while (re-search-forward regexp nil t
)
795 (let ((name (org-trim (buffer-substring-no-properties
796 (line-beginning-position) (line-end-position)))))
798 ((and (string-prefix-p "END" (match-string 1) t
)
800 (push (list (line-beginning-position)
801 (format "Invalid block closing line \"%s\"" name
))
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\""
813 (defun org-lint-invalid-keyword-syntax (_)
814 (let ((regexp "^[ \t]*#\\+\\([^[:space:]:]*\\)\\(?: \\|$\\)")
816 (format "[ \t]*#\\+%s\\(\\[.*\\]\\)?:\\(?: \\|$\\)"
817 (regexp-opt org-element-dual-keywords
)))
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
)
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
))
831 (defun org-lint-extraneous-element-in-footnote-section (ast)
832 (org-element-map ast
'headline
834 (and (org-element-property :footnote-section-p h
)
835 (org-element-map (org-element-contents h
)
838 (memq e
'(comment comment-block footnote-definition
839 property-drawer section
)))
840 org-element-all-elements
)
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
)
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
860 (let ((app (org-element-property :application l
)))
862 (list (org-element-property :begin l
)
863 (format "Deprecated \"file+%s\" link type" app
)))))))
865 (defun org-lint-wrong-header-argument (ast)
868 (lambda (datum language headers
)
870 ;; If LANGUAGE is specified, restrict allowed
871 ;; headers to both LANGUAGE-specific and default
872 ;; ones. Otherwise, accept headers from any loaded
875 org-babel-header-arg-names
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
))))
887 ((not (string-prefix-p ":" h
))
890 (format "Missing colon in header argument \"%s\"" h
))
892 ((assoc-string (substring h
1) allowed
))
893 (t (push (list p
(format "Unknown header argument \"%s\"" h
))
895 (org-element-map ast
'(babel-call inline-babel-call inline-src-block keyword
896 node-property src-block
)
898 (pcase (org-element-type datum
)
899 ((or `babel-call
`inline-babel-call
)
903 (cl-mapcan #'org-babel-parse-header-arguments
905 (org-element-property :inside-header datum
)
906 (org-element-property :end-header datum
)))))
910 (org-element-property :language datum
)
911 (org-babel-parse-header-arguments
912 (org-element-property :parameters datum
))))
914 (when (string= (org-element-property :key datum
) "PROPERTY")
915 (let ((value (org-element-property :value datum
)))
916 (when (string-match "\\`header-args\\(?::\\(\\S-+\\)\\)?\\+? *"
920 (match-string 1 value
)
921 (org-babel-parse-header-arguments
922 (substring value
(match-end 0))))))))
924 (let ((key (org-element-property :key datum
)))
925 (when (let ((case-fold-search t
))
926 (string-match "\\`HEADER-ARGS\\(?::\\(\\S-+\\)\\)?\\+?"
931 (org-babel-parse-header-arguments
932 (org-element-property :value 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
))))))))
942 (defun org-lint-wrong-header-value (ast)
945 '(babel-call inline-babel-call inline-src-block src-block
)
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:"
953 (and (boundp v
) (symbol-value v
))))
954 org-babel-common-header-args-w-values
))
957 #'org-babel-merge-params
958 org-babel-default-header-args
960 (let ((v (intern (concat "org-babel-default-header-args:"
962 (and (boundp v
) (symbol-value v
))))
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
))
969 (org-babel-parse-header-arguments
975 (cons (org-element-property :parameters datum
)
976 (org-element-property :header datum
))
979 (or (org-element-property :parameters datum
) ""))
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
))
992 (dolist (v (if (stringp values
) (org-split-string values
)
994 (let ((valid-value nil
))
996 (dolist (group allowed-values
)
999 (if (stringp v
) #'assoc-string
#'assoc
)
1001 (when (memq :any group
)
1002 (setf valid-value t
)
1003 (push (cons group v
) groups-alist
)))
1004 ((assq group groups-alist
)
1007 (or (org-element-property :post-affiliated datum
)
1008 (org-element-property :begin datum
))
1010 "Forbidden combination in header \"%s\": %s, %s"
1012 (cdr (assq group groups-alist
))
1016 (t (push (cons group v
) groups-alist
)
1017 (setf valid-value t
))))
1021 (or (org-element-property :post-affiliated datum
)
1022 (org-element-property :begin datum
))
1023 (format "Unknown value \"%s\" for header \"%s\""
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
)
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
1049 (< (string-to-number (aref (cadr a
) 0))
1050 (string-to-number (aref (cadr b
) 0))))
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
1065 (goto-char (point-min))
1066 (let ((ast (org-element-parse-buffer))
1070 ;; Insert unique ID for each report. Replace buffer positions
1071 ;; with line numbers.
1079 (goto-char (car report
))
1081 (prog1 (number-to-string
1083 (count-lines last-pos
(point))))
1084 (setf last-pos
(point))))
1086 ;; Insert trust level in generated reports. Also sort them
1087 ;; by buffer position in order to optimize lines computation.
1090 (let ((trust (symbol-name (org-lint-checker-trust c
))))
1093 (list (car report
) trust
(nth 1 report
) c
))
1096 (intern (format "org-lint-%s"
1097 (org-lint-checker-name c
)))
1100 #'car-less-than-car
))))))
1102 (defvar-local org-lint--source-buffer nil
1103 "Source buffer associated to current report buffer.")
1105 (defvar-local org-lint--local-checkers nil
1106 "List of checkers used to build current report.")
1108 (defun org-lint--refresh-reports ()
1109 (setq tabulated-list-entries
1110 (org-lint--generate-reports org-lint--source-buffer
1111 org-lint--local-checkers
))
1112 (tabulated-list-print))
1114 (defun org-lint--current-line ()
1115 "Return current report line, as a number."
1116 (string-to-number (aref (tabulated-list-get-entry) 0)))
1118 (defun org-lint--current-checker (&optional entry
)
1119 "Return current report checker.
1120 When optional argument ENTRY is non-nil, use this entry instead
1122 (aref (if entry
(nth 1 entry
) (tabulated-list-get-entry)) 3))
1124 (defun org-lint--display-reports (source checkers
)
1125 "Display linting reports for buffer SOURCE.
1126 CHECKERS is the list of checkers used."
1127 (let ((buffer (get-buffer-create "*Org Lint*")))
1128 (with-current-buffer buffer
1129 (org-lint--report-mode)
1130 (setf org-lint--source-buffer source
)
1131 (setf org-lint--local-checkers checkers
)
1132 (org-lint--refresh-reports)
1133 (tabulated-list-print)
1134 (add-hook 'tabulated-list-revert-hook
#'org-lint--refresh-reports nil t
))
1135 (pop-to-buffer buffer
)))
1137 (defun org-lint--jump-to-source ()
1138 "Move to source line that generated the report at point."
1140 (let ((l (org-lint--current-line)))
1141 (switch-to-buffer-other-window org-lint--source-buffer
)
1143 (org-show-set-visibility 'local
)
1146 (defun org-lint--show-source ()
1147 "Show source line that generated the report at point."
1149 (let ((buffer (current-buffer)))
1150 (org-lint--jump-to-source)
1151 (switch-to-buffer-other-window buffer
)))
1153 (defun org-lint--hide-checker ()
1154 "Hide all reports from checker that generated the report at point."
1156 (let ((c (org-lint--current-checker)))
1157 (setf tabulated-list-entries
1158 (cl-remove-if (lambda (e) (equal c
(org-lint--current-checker e
)))
1159 tabulated-list-entries
))
1160 (tabulated-list-print)))
1162 (defun org-lint--ignore-checker ()
1163 "Ignore all reports from checker that generated the report at point.
1164 Checker will also be ignored in all subsequent reports."
1166 (setf org-lint--local-checkers
1167 (remove (org-lint--current-checker) org-lint--local-checkers
))
1168 (org-lint--hide-checker))
1174 (defun org-lint (&optional arg
)
1175 "Check current Org buffer for syntax mistakes.
1177 By default, run all checkers. With a single prefix ARG \
1178 \\[universal-argument],
1179 select one category of checkers only. With a double prefix
1180 \\[universal-argument] \\[universal-argument], select one precise \
1181 checker by its name.
1183 ARG can also be a list of checker names, as symbols, to run."
1185 (unless (derived-mode-p 'org-mode
) (user-error "Not in an Org buffer"))
1186 (when (called-interactively-p 'any
)
1187 (message "Org linting process starting..."))
1190 (`nil org-lint--checkers
)
1194 "Checker category: "
1195 (mapcar #'org-lint-checker-categories org-lint--checkers
)
1199 (assoc-string (org-lint-checker-categories c
) category
))
1200 org-lint--checkers
)))
1203 (let ((name (completing-read
1205 (mapcar #'org-lint-checker-name org-lint--checkers
)
1208 (dolist (c org-lint--checkers
)
1209 (when (string= (org-lint-checker-name c
) name
)
1210 (throw 'exit c
)))))))
1212 (cl-remove-if-not (lambda (c) (memq (org-lint-checker-name c
) arg
))
1213 org-lint--checkers
))
1214 (_ (user-error "Invalid argument `%S' for `org-lint'" arg
)))))
1215 (if (not (called-interactively-p 'any
))
1216 (org-lint--generate-reports (current-buffer) checkers
)
1217 (org-lint--display-reports (current-buffer) checkers
)
1218 (message "Org linting process completed"))))
1222 ;;; org-lint.el ends here