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
104 (require 'org-element
)
112 (cl-defstruct (org-lint-checker (:copier nil
))
113 (name 'missing-checker-name
)
115 (categories '(default))
116 (trust 'high
)) ; `low' or `high'
118 (defun org-lint-missing-checker-name (_)
120 "`A checker has no `:name' property. Please verify `org-lint--checkers'"))
122 (defconst org-lint--checkers
124 (make-org-lint-checker
125 :name
'duplicate-custom-id
126 :description
"Report duplicates CUSTOM_ID properties"
128 (make-org-lint-checker
129 :name
'duplicate-name
130 :description
"Report duplicate NAME values"
131 :categories
'(babel link
))
132 (make-org-lint-checker
133 :name
'duplicate-target
134 :description
"Report duplicate targets"
136 (make-org-lint-checker
137 :name
'duplicate-footnote-definition
138 :description
"Report duplicate footnote definitions"
139 :categories
'(footnote))
140 (make-org-lint-checker
141 :name
'orphaned-affiliated-keywords
142 :description
"Report orphaned affiliated keywords"
144 (make-org-lint-checker
145 :name
'obsolete-affiliated-keywords
146 :description
"Report obsolete affiliated keywords"
147 :categories
'(obsolete))
148 (make-org-lint-checker
149 :name
'deprecated-export-blocks
150 :description
"Report deprecated export block syntax"
151 :categories
'(obsolete export
)
153 (make-org-lint-checker
154 :name
'deprecated-header-syntax
155 :description
"Report deprecated Babel header syntax"
156 :categories
'(obsolete babel
)
158 (make-org-lint-checker
159 :name
'missing-language-in-src-block
160 :description
"Report missing language in src blocks"
161 :categories
'(babel))
162 (make-org-lint-checker
163 :name
'missing-backend-in-export-block
164 :description
"Report missing back-end in export blocks"
165 :categories
'(export))
166 (make-org-lint-checker
167 :name
'invalid-babel-call-block
168 :description
"Report invalid Babel call blocks"
169 :categories
'(babel))
170 (make-org-lint-checker
172 :description
"Report NAME values with a colon"
173 :categories
'(babel))
174 (make-org-lint-checker
175 :name
'wrong-header-argument
176 :description
"Report wrong babel headers"
177 :categories
'(babel))
178 (make-org-lint-checker
179 :name
'wrong-header-value
180 :description
"Report invalid value in babel headers"
183 (make-org-lint-checker
184 :name
'deprecated-category-setup
185 :description
"Report misuse of CATEGORY keyword"
186 :categories
'(obsolete))
187 (make-org-lint-checker
188 :name
'invalid-coderef-link
189 :description
"Report \"coderef\" links with unknown destination"
191 (make-org-lint-checker
192 :name
'invalid-custom-id-link
193 :description
"Report \"custom-id\" links with unknown destination"
195 (make-org-lint-checker
196 :name
'invalid-fuzzy-link
197 :description
"Report \"fuzzy\" links with unknown destination"
199 (make-org-lint-checker
200 :name
'invalid-id-link
201 :description
"Report \"id\" links with unknown destination"
203 (make-org-lint-checker
204 :name
'link-to-local-file
205 :description
"Report links to non-existent local files"
208 (make-org-lint-checker
209 :name
'non-existent-setupfile-parameter
210 :description
"Report SETUPFILE keywords with non-existent file parameter"
212 (make-org-lint-checker
213 :name
'wrong-include-link-parameter
214 :description
"Report INCLUDE keywords with misleading link parameter"
215 :categories
'(export)
217 (make-org-lint-checker
218 :name
'obsolete-include-markup
219 :description
"Report obsolete markup in INCLUDE keyword"
220 :categories
'(obsolete export
)
222 (make-org-lint-checker
223 :name
'unknown-options-item
224 :description
"Report unknown items in OPTIONS keyword"
225 :categories
'(export)
227 (make-org-lint-checker
228 :name
'invalid-macro-argument-and-template
229 :description
"Report spurious macro arguments or invalid macro templates"
230 :categories
'(export)
232 (make-org-lint-checker
233 :name
'special-property-in-properties-drawer
234 :description
"Report special properties in properties drawers"
235 :categories
'(properties))
236 (make-org-lint-checker
237 :name
'obsolete-properties-drawer
238 :description
"Report obsolete syntax for properties drawers"
239 :categories
'(obsolete properties
))
240 (make-org-lint-checker
241 :name
'undefined-footnote-reference
242 :description
"Report missing definition for footnote references"
243 :categories
'(footnote))
244 (make-org-lint-checker
245 :name
'unreferenced-footnote-definition
246 :description
"Report missing reference for footnote definitions"
247 :categories
'(footnote))
248 (make-org-lint-checker
249 :name
'extraneous-element-in-footnote-section
250 :description
"Report non-footnote definitions in footnote section"
251 :categories
'(footnote))
252 (make-org-lint-checker
253 :name
'invalid-keyword-syntax
254 :description
"Report probable invalid keywords"
256 (make-org-lint-checker
258 :description
"Report invalid blocks"
260 (make-org-lint-checker
261 :name
'misplaced-planning-info
262 :description
"Report misplaced planning info line"
264 (make-org-lint-checker
265 :name
'incomplete-drawer
266 :description
"Report probable incomplete drawers"
268 (make-org-lint-checker
269 :name
'indented-diary-sexp
270 :description
"Report probable indented diary-sexps"
272 (make-org-lint-checker
274 :description
"Report obsolete QUOTE section"
275 :categories
'(obsolete)
277 "List of all available checkers.")
279 (defun org-lint--collect-duplicates
280 (ast type extract-key extract-position build-message
)
281 "Helper function to collect duplicates in parse tree AST.
283 EXTRACT-KEY is a function extracting key. It is called with
284 a single argument: the element or object. Comparison is done
287 EXTRACT-POSITION is a function returning position for the report.
288 It is called with two arguments, the object or element, and the
291 BUILD-MESSAGE is a function creating the report message. It is
292 called with one argument, the key used for comparison."
297 (lambda (position value
)
298 (push (list position
(funcall build-message value
)) reports
))))
299 (org-element-map ast type
301 (let ((key (funcall extract-key datum
)))
304 ((assoc key keys
) (cl-pushnew (assoc key keys
) originals
)
305 (funcall make-report
(funcall extract-position datum key
) key
))
306 (t (push (cons key
(funcall extract-position datum key
)) keys
))))))
307 (dolist (e originals reports
) (funcall make-report
(cdr e
) (car e
)))))
309 (defun org-lint-duplicate-custom-id (ast)
310 (org-lint--collect-duplicates
314 (and (eq (compare-strings "CUSTOM_ID" nil nil
315 (org-element-property :key property
) nil nil
318 (org-element-property :value property
)))
319 (lambda (property _
) (org-element-property :begin property
))
320 (lambda (key) (format "Duplicate CUSTOM_ID property \"%s\"" key
))))
322 (defun org-lint-duplicate-name (ast)
323 (org-lint--collect-duplicates
325 org-element-all-elements
326 (lambda (datum) (org-element-property :name datum
))
328 (goto-char (org-element-property :begin datum
))
330 (format "^[ \t]*#\\+[A-Za-z]+: +%s *$" (regexp-quote name
)))
332 (lambda (key) (format "Duplicate NAME \"%s\"" key
))))
334 (defun org-lint-duplicate-target (ast)
335 (org-lint--collect-duplicates
338 (lambda (target) (org-split-string (org-element-property :value target
)))
339 (lambda (target _
) (org-element-property :begin target
))
341 (format "Duplicate target <<%s>>" (mapconcat #'identity key
" ")))))
343 (defun org-lint-duplicate-footnote-definition (ast)
344 (org-lint--collect-duplicates
347 (lambda (definition) (org-element-property :label definition
))
348 (lambda (definition _
) (org-element-property :post-affiliated definition
))
349 (lambda (key) (format "Duplicate footnote definition \"%s\"" key
))))
351 (defun org-lint-orphaned-affiliated-keywords (ast)
352 ;; Ignore orphan RESULTS keywords, which could be generated from
353 ;; a source block returning no value.
354 (let ((keywords (cl-set-difference org-element-affiliated-keywords
355 '("RESULT" "RESULTS")
357 (org-element-map ast
'keyword
359 (let ((key (org-element-property :key k
)))
360 (and (or (let ((case-fold-search t
))
361 (org-string-match-p "\\`ATTR_[-_A-Za-z0-9]+\\'" key
))
362 (member key keywords
))
363 (list (org-element-property :post-affiliated k
)
364 (format "Orphaned affiliated keyword: \"%s\"" key
))))))))
366 (defun org-lint-obsolete-affiliated-keywords (_)
367 (let ((regexp (format "^[ \t]*#\\+%s:"
368 (regexp-opt '("DATA" "LABEL" "RESNAME" "SOURCE"
369 "SRCNAME" "TBLNAME" "RESULT" "HEADERS")
372 (while (re-search-forward regexp nil t
)
373 (let ((key (upcase (match-string-no-properties 1))))
375 (org-element-property :post-affiliated
(org-element-at-point)))
377 (list (line-beginning-position)
379 "Obsolete affiliated keyword: \"%s\". Use \"%s\" instead"
388 (defun org-lint-deprecated-export-blocks (ast)
389 (let ((deprecated '("ASCII" "BEAMER" "HTML" "LATEX" "MAN" "MARKDOWN" "MD"
390 "ODT" "ORG" "TEXINFO")))
391 (org-element-map ast
'special-block
393 (let ((type (org-element-property :type b
)))
394 (when (member-ignore-case type deprecated
)
396 (org-element-property :post-affiliated b
)
398 "Deprecated syntax for export block. Use \"BEGIN_EXPORT %s\" \
402 (defun org-lint-deprecated-header-syntax (ast)
403 (let* ((deprecated-babel-properties
404 (mapcar (lambda (arg) (symbol-name (car arg
)))
405 org-babel-common-header-args-w-values
))
407 (format "\\`%s[ \t]" (regexp-opt deprecated-babel-properties t
))))
408 (org-element-map ast
'(keyword node-property
)
410 (let ((key (org-element-property :key datum
)))
411 (pcase (org-element-type datum
)
413 (let ((value (org-element-property :value datum
)))
414 (and (string= key
"PROPERTY")
415 (string-match deprecated-re value
)
416 (list (org-element-property :begin datum
)
417 (format "Deprecated syntax for \"%s\". \
418 Use header-args instead"
419 (match-string-no-properties 1 value
))))))
421 (and (member-ignore-case key deprecated-babel-properties
)
423 (org-element-property :begin datum
)
424 (format "Deprecated syntax for \"%s\". \
425 Use :header-args: instead"
428 (defun org-lint-missing-language-in-src-block (ast)
429 (org-element-map ast
'src-block
431 (unless (org-element-property :language b
)
432 (list (org-element-property :post-affiliated b
)
433 "Missing language in source block")))))
435 (defun org-lint-missing-backend-in-export-block (ast)
436 (org-element-map ast
'export-block
438 (unless (org-element-property :type b
)
439 (list (org-element-property :post-affiliated b
)
440 "Missing back-end in export block")))))
442 (defun org-lint-invalid-babel-call-block (ast)
443 (org-element-map ast
'babel-call
446 ((not (org-element-property :call b
))
447 (list (org-element-property :post-affiliated b
)
448 "Invalid syntax in babel call block"))
449 ((let ((h (org-element-property :end-header b
)))
450 (and h
(org-string-match-p "\\`\\[.*\\]\\'" h
)))
452 (org-element-property :post-affiliated b
)
453 "Babel call's end header must not be wrapped within brackets"))))))
455 (defun org-lint-deprecated-category-setup (ast)
456 (org-element-map ast
'keyword
460 ((not (string= (org-element-property :key k
) "CATEGORY")) nil
)
462 (list (org-element-property :post-affiliated k
)
463 "Spurious CATEGORY keyword. Set :CATEGORY: property instead"))
464 (t (setf category-flag t
) nil
))))))
466 (defun org-lint-invalid-coderef-link (ast)
467 (let ((info (list :parse-tree ast
)))
468 (org-element-map ast
'link
470 (let ((ref (org-element-property :path link
)))
471 (and (equal (org-element-property :type link
) "coderef")
472 (not (ignore-errors (org-export-resolve-coderef ref info
)))
473 (list (org-element-property :begin link
)
474 (format "Unknown coderef \"%s\"" ref
))))))))
476 (defun org-lint-invalid-custom-id-link (ast)
477 (let ((info (list :parse-tree ast
)))
478 (org-element-map ast
'link
480 (and (equal (org-element-property :type link
) "custom-id")
481 (not (ignore-errors (org-export-resolve-id-link link info
)))
482 (list (org-element-property :begin link
)
483 (format "Unknown custom ID \"%s\""
484 (org-element-property :path link
))))))))
486 (defun org-lint-invalid-fuzzy-link (ast)
487 (let ((info (list :parse-tree ast
)))
488 (org-element-map ast
'link
490 (and (equal (org-element-property :type link
) "fuzzy")
491 (not (ignore-errors (org-export-resolve-fuzzy-link link info
)))
492 (list (org-element-property :begin link
)
493 (format "Unknown fuzzy location \"%s\""
494 (let ((path (org-element-property :path link
)))
495 (if (string-prefix-p "*" path
)
499 (defun org-lint-invalid-id-link (ast)
500 (org-element-map ast
'link
502 (let ((id (org-element-property :path link
)))
503 (and (equal (org-element-property :type link
) "id")
504 (not (org-id-find id
))
505 (list (org-element-property :begin link
)
506 (format "Unknown ID \"%s\"" id
)))))))
508 (defun org-lint-special-property-in-properties-drawer (ast)
509 (org-element-map ast
'node-property
511 (let ((key (org-element-property :key p
)))
512 (and (member-ignore-case key org-special-properties
)
513 (list (org-element-property :begin p
)
515 "Special property \"%s\" found in a properties drawer"
518 (defun org-lint-obsolete-properties-drawer (ast)
519 (org-element-map ast
'drawer
521 (when (equal (org-element-property :drawer-name d
) "PROPERTIES")
522 (let ((section (org-element-lineage d
'(section))))
523 (unless (org-element-map section
'property-drawer
#'identity nil t
)
524 (list (org-element-property :post-affiliated d
)
526 (goto-char (org-element-property :post-affiliated d
))
528 (or (org-at-heading-p) (org-at-planning-p)))
529 "Incorrect contents for PROPERTIES drawer"
530 "Incorrect location for PROPERTIES drawer"))))))))
532 (defun org-lint-link-to-local-file (ast)
533 (org-element-map ast
'link
535 (when (equal (org-element-property :type l
) "file")
536 (let ((file (org-link-unescape (org-element-property :path l
))))
537 (and (not (file-remote-p file
))
538 (not (file-exists-p file
))
539 (list (org-element-property :begin l
)
540 (format (if (org-element-lineage l
'(link))
541 "Link to non-existent image file \"%s\"\
543 "Link to non-existent local file \"%s\"")
546 (defun org-lint-non-existent-setupfile-parameter (ast)
547 (org-element-map ast
'keyword
549 (when (equal (org-element-property :key k
) "SETUPFILE")
550 (let ((file (org-remove-double-quotes
551 (org-element-property :value k
))))
552 (and (not (file-remote-p file
))
553 (not (file-exists-p file
))
554 (list (org-element-property :begin k
)
555 (format "Non-existent setup file \"%s\"" file
))))))))
557 (defun org-lint-wrong-include-link-parameter (ast)
558 (org-element-map ast
'keyword
560 (when (equal (org-element-property :key k
) "INCLUDE")
561 (let* ((value (org-element-property :value k
))
563 (and (string-match "^\\(\".+\"\\|\\S-+\\)[ \t]*" value
)
565 (org-remove-double-quotes (match-string 1 value
))))))
567 (list (org-element-property :post-affiliated k
)
568 "Missing location argument in INCLUDE keyword")
569 (let* ((file (org-string-nw-p
570 (if (string-match "::\\(.*\\)\\'" path
)
571 (substring path
0 (match-beginning 0))
573 (search (and (not (equal file path
))
574 (org-string-nw-p (match-string 1 path
)))))
576 (not (file-remote-p file
))
577 (not (file-exists-p file
)))
578 (list (org-element-property :post-affiliated k
)
579 "Non-existent file argument in INCLUDE keyword")
580 (let* ((visiting (if file
(find-buffer-visiting file
)
582 (buffer (or visiting
(find-file-noselect file
))))
584 (with-current-buffer buffer
588 (let ((org-link-search-inhibit-query t
))
589 (org-link-search search nil t
)))))
590 (list (org-element-property :post-affiliated k
)
592 "Invalid search part \"%s\" in INCLUDE keyword"
594 (unless visiting
(kill-buffer buffer
))))))))))))
596 (defun org-lint-obsolete-include-markup (ast)
597 (let ((regexp (format "\\`\\(?:\".+\"\\|\\S-+\\)[ \t]+%s"
599 '("ASCII" "BEAMER" "HTML" "LATEX" "MAN" "MARKDOWN" "MD"
600 "ODT" "ORG" "TEXINFO")
602 (org-element-map ast
'keyword
604 (when (equal (org-element-property :key k
) "INCLUDE")
605 (let ((case-fold-search t
)
606 (value (org-element-property :value k
)))
607 (when (string-match regexp value
)
608 (let ((markup (match-string-no-properties 1 value
)))
609 (list (org-element-property :post-affiliated k
)
610 (format "Obsolete markup \"%s\" in INCLUDE keyword. \
611 Use \"export %s\" instead"
615 (defun org-lint-unknown-options-item (ast)
616 (let ((allowed (delq nil
618 (mapcar (lambda (o) (nth 2 o
)) org-export-options-alist
)
621 (mapcar (lambda (o) (nth 2 o
))
622 (org-export-backend-options b
)))
623 org-export-registered-backends
))))
625 (org-element-map ast
'keyword
627 (when (string= (org-element-property :key k
) "OPTIONS")
628 (let ((value (org-element-property :value k
))
630 (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-*\\)[ \t]*"
633 (setf start
(match-end 0))
634 (let ((item (match-string 1 value
)))
635 (unless (member item allowed
)
636 (push (list (org-element-property :post-affiliated k
)
637 (format "Unknown OPTIONS item \"%s\"" item
))
641 (defun org-lint-invalid-macro-argument-and-template (ast)
642 (let ((extract-placeholders
646 (while (string-match "\\$\\([1-9][0-9]*\\)" template start
)
647 (setf start
(match-end 0))
648 (push (string-to-number (match-string 1 template
)) args
))
649 (sort (org-uniquify args
) #'<))))
651 ;; Check arguments for macro templates.
652 (org-element-map ast
'keyword
654 (when (string= (org-element-property :key k
) "MACRO")
655 (let* ((value (org-element-property :value k
))
656 (name (and (string-match "^\\S-+" value
)
657 (match-string 0 value
)))
659 (org-trim (substring value
(match-end 0))))))
662 (push (list (org-element-property :post-affiliated k
)
663 "Missing name in MACRO keyword")
665 ((not (org-string-nw-p template
))
666 (push (list (org-element-property :post-affiliated k
)
667 "Missing template in macro \"%s\"" name
)
670 (unless (let ((args (funcall extract-placeholders template
)))
671 (equal (number-sequence 1 (or (org-last args
) 0)) args
))
672 (push (list (org-element-property :post-affiliated k
)
673 (format "Unused placeholders in macro \"%s\""
676 ;; Check arguments for macros.
677 (org-macro-initialize-templates)
678 (let ((templates (append
679 (mapcar (lambda (m) (cons m
"$1"))
680 '("author" "date" "email" "title" "results"))
681 org-macro-templates
)))
682 (org-element-map ast
'macro
684 (let* ((name (org-element-property :key macro
))
685 (template (cdr (assoc-string name templates t
))))
687 (push (list (org-element-property :begin macro
)
688 (format "Undefined macro \"%s\"" name
))
690 (let ((arg-numbers (funcall extract-placeholders template
)))
693 (nthcdr (apply #'max arg-numbers
)
694 (org-element-property :args macro
))))
697 (list (org-element-property :begin macro
)
698 (format "Unused argument%s in macro \"%s\": %s"
699 (if (> (length spurious-args
) 1) "s" "")
701 (mapconcat (lambda (a) (format "\"%s\"" a
))
707 (defun org-lint-undefined-footnote-reference (ast)
708 (let ((definitions (org-element-map ast
'footnote-definition
709 (lambda (f) (org-element-property :label f
)))))
710 (org-element-map ast
'footnote-reference
712 (let ((label (org-element-property :label f
)))
714 (not (member label definitions
))
715 (list (org-element-property :begin f
)
716 (format "Missing definition for footnote [%s]"
719 (defun org-lint-unreferenced-footnote-definition (ast)
720 (let ((references (org-element-map ast
'footnote-reference
721 (lambda (f) (org-element-property :label f
)))))
722 (org-element-map ast
'footnote-definition
724 (let ((label (org-element-property :label f
)))
726 (not (member label references
))
727 (list (org-element-property :post-affiliated f
)
728 (format "No reference for footnote definition [%s]"
731 (defun org-lint-colon-in-name (ast)
732 (org-element-map ast org-element-all-elements
734 (let ((name (org-element-property :name e
)))
736 (org-string-match-p ":" name
)
738 (goto-char (org-element-property :begin e
))
740 (format "^[ \t]*#\\+\\w+: +%s *$" (regexp-quote name
)))
743 "Name \"%s\" contains a colon; Babel cannot use it as input"
746 (defun org-lint-misplaced-planning-info (_)
747 (let ((case-fold-search t
)
749 (while (re-search-forward org-planning-line-re nil t
)
750 (unless (memq (org-element-type (org-element-at-point))
751 '(comment-block example-block export-block planning
752 src-block verse-block
))
753 (push (list (line-beginning-position) "Misplaced planning info line")
757 (defun org-lint-incomplete-drawer (_)
759 (while (re-search-forward org-drawer-regexp nil t
)
760 (let ((name (org-trim (match-string-no-properties 0)))
761 (element (org-element-at-point)))
762 (pcase (org-element-type element
)
763 ((or `drawer
`property-drawer
)
764 (goto-char (org-element-property :end element
))
766 ((or `comment-block
`example-block
`export-block
`src-block
770 (push (list (line-beginning-position)
771 (format "Possible incomplete drawer \"%s\"" name
))
775 (defun org-lint-indented-diary-sexp (_)
777 (while (re-search-forward "^[ \t]+%%(" nil t
)
778 (unless (memq (org-element-type (org-element-at-point))
779 '(comment-block diary-sexp example-block export-block
780 src-block verse-block
))
781 (push (list (line-beginning-position) "Possible indented diary-sexp")
785 (defun org-lint-invalid-block (_)
786 (let ((case-fold-search t
)
787 (regexp "^[ \t]*#\\+\\(BEGIN\\|END\\)\\(?::\\|_[^[:space:]]*\\)?[ \t]*")
789 (while (re-search-forward regexp nil t
)
790 (let ((name (org-trim (buffer-substring-no-properties
791 (line-beginning-position) (line-end-position)))))
793 ((and (string-prefix-p "END" (match-string 1) t
)
795 (push (list (line-beginning-position)
796 (format "Invalid block closing line \"%s\"" name
))
798 ((not (memq (org-element-type (org-element-at-point))
799 '(center-block comment-block dynamic-block example-block
800 export-block quote-block special-block
801 src-block verse-block
)))
802 (push (list (line-beginning-position)
803 (format "Possible incomplete block \"%s\""
808 (defun org-lint-invalid-keyword-syntax (_)
809 (let ((regexp "^[ \t]*#\\+\\([^[:space:]:]*\\)\\(?: \\|$\\)")
811 (format "[ \t]*#\\+%s\\(\\[.*\\]\\)?:\\(?: \\|$\\)"
812 (regexp-opt org-element-dual-keywords
)))
814 (while (re-search-forward regexp nil t
)
815 (let ((name (match-string-no-properties 1)))
816 (unless (or (string-prefix-p "BEGIN" name t
)
817 (string-prefix-p "END" name t
)
820 (let ((case-fold-search t
)) (looking-at exception-re
))))
821 (push (list (match-beginning 0)
822 (format "Possible missing colon in keyword \"%s\"" name
))
826 (defun org-lint-extraneous-element-in-footnote-section (ast)
827 (org-element-map ast
'headline
829 (and (org-element-property :footnote-section-p h
)
830 (org-element-map (org-element-contents h
)
833 (memq e
'(comment comment-block footnote-definition
834 property-drawer section
)))
835 org-element-all-elements
)
837 (not (and (eq (org-element-type e
) 'headline
)
838 (org-element-property :commentedp e
))))
839 nil t
'(footnote-definition property-drawer
))
840 (list (org-element-property :begin h
)
841 "Extraneous elements in footnote section")))))
843 (defun org-lint-quote-section (ast)
844 (org-element-map ast
'(headline inlinetask
)
846 (let ((title (org-element-property :raw-value h
)))
847 (and (or (string-prefix-p "QUOTE " title
)
848 (string-prefix-p (concat org-comment-string
" QUOTE ") title
))
849 (list (org-element-property :begin h
)
850 "Deprecated QUOTE section"))))))
852 (defun org-lint-wrong-header-argument (ast)
855 (lambda (datum language headers
)
857 ;; If LANGUAGE is specified, restrict allowed
858 ;; headers to both LANGUAGE-specific and default
859 ;; ones. Otherwise, accept headers from any loaded
862 org-babel-header-arg-names
865 (let ((v (intern (format "org-babel-header-args:%s" l
))))
866 (and (boundp v
) (mapcar #'car
(symbol-value v
)))))
867 (if language
(list language
)
868 (mapcar #'car org-babel-load-languages
))))))
869 (dolist (header headers
)
870 (let ((h (symbol-name (car header
)))
871 (p (or (org-element-property :post-affiliated datum
)
872 (org-element-property :begin datum
))))
874 ((not (string-prefix-p ":" h
))
877 (format "Missing colon in header argument \"%s\"" h
))
879 ((assoc-string (substring h
1) allowed
))
880 (t (push (list p
(format "Unknown header argument \"%s\"" h
))
882 (org-element-map ast
'(babel-call inline-babel-call inline-src-block keyword
883 node-property src-block
)
885 (pcase (org-element-type datum
)
886 ((or `babel-call
`inline-babel-call
)
890 (cl-mapcan #'org-babel-parse-header-arguments
892 (org-element-property :inside-header datum
)
893 (org-element-property :end-header datum
)))))
897 (org-element-property :language datum
)
898 (org-babel-parse-header-arguments
899 (org-element-property :parameters datum
))))
901 (when (string= (org-element-property :key datum
) "PROPERTY")
902 (let ((value (org-element-property :value datum
)))
903 (when (string-match "\\`header-args\\(?::\\(\\S-+\\)\\)?\\+? *"
907 (match-string 1 value
)
908 (org-babel-parse-header-arguments
909 (substring value
(match-end 0))))))))
911 (let ((key (org-element-property :key datum
)))
912 (when (let ((case-fold-search t
))
913 (string-match "\\`HEADER-ARGS\\(?::\\(\\S-+\\)\\)?\\+?"
918 (org-babel-parse-header-arguments
919 (org-element-property :value datum
))))))
923 (org-element-property :language datum
)
924 (cl-mapcan #'org-babel-parse-header-arguments
925 (cons (org-element-property :parameters datum
)
926 (org-element-property :header datum
))))))))
929 (defun org-lint-wrong-header-value (ast)
932 '(babel-call inline-babel-call inline-src-block src-block
)
934 (let* ((type (org-element-type datum
))
935 (language (org-element-property :language datum
))
936 (allowed-header-values
937 (append (and language
938 (let ((v (intern (concat "org-babel-header-args:"
940 (and (boundp v
) (symbol-value v
))))
941 org-babel-common-header-args-w-values
))
944 #'org-babel-merge-params
945 org-babel-default-header-args
947 (let ((v (intern (concat "org-babel-default-header-args:"
949 (and (boundp v
) (symbol-value v
))))
951 (list (and (memq type
'(babel-call inline-babel-call
))
952 org-babel-default-lob-header-args
))
953 (progn (goto-char (org-element-property :begin datum
))
954 (org-babel-params-from-properties language
))
956 (org-babel-parse-header-arguments
962 (cons (org-element-property :parameters datum
)
963 (org-element-property :header datum
))
966 (or (org-element-property :parameters datum
) ""))
969 (org-element-property :inside-header datum
)
971 (org-element-property :end-header datum
)))))))))))
972 (dolist (header datum-header-values
)
973 (let ((allowed-values
974 (cdr (assoc-string (substring (symbol-name (car header
)) 1)
975 allowed-header-values
))))
976 (unless (memq allowed-values
'(:any nil
))
977 (let ((values (cdr header
))
979 (dolist (v (if (stringp values
) (org-split-string values
)
981 (let ((valid-value nil
))
983 (dolist (group allowed-values
)
986 (if (stringp v
) #'assoc-string
#'assoc
)
988 (when (memq :any group
)
990 (push (cons group v
) groups-alist
)))
991 ((assq group groups-alist
)
994 (or (org-element-property :post-affiliated datum
)
995 (org-element-property :begin datum
))
997 "Forbidden combination in header \"%s\": %s, %s"
999 (cdr (assq group groups-alist
))
1003 (t (push (cons group v
) groups-alist
)
1004 (setf valid-value t
))))
1008 (or (org-element-property :post-affiliated datum
)
1009 (org-element-property :begin datum
))
1010 (format "Unknown value \"%s\" for header \"%s\""
1019 (defvar org-lint--report-mode-map
1020 (let ((map (make-sparse-keymap)))
1021 (set-keymap-parent map tabulated-list-mode-map
)
1022 (define-key map
(kbd "RET") 'org-lint--jump-to-source
)
1023 (define-key map
(kbd "TAB") 'org-lint--show-source
)
1024 (define-key map
(kbd "C-j") 'org-lint--show-source
)
1025 (define-key map
(kbd "h") 'org-lint--hide-checker
)
1026 (define-key map
(kbd "i") 'org-lint--ignore-checker
)
1028 "Local keymap for `org-lint--report-mode' buffers.")
1030 (define-derived-mode org-lint--report-mode tabulated-list-mode
"OrgLint"
1031 "Major mode used to display reports emitted during linting.
1032 \\{org-lint--report-mode-map}"
1033 (setf tabulated-list-format
1036 (< (string-to-number (aref (cadr a
) 0))
1037 (string-to-number (aref (cadr b
) 0))))
1041 (tabulated-list-init-header))
1043 (defun org-lint--generate-reports (buffer checkers
)
1044 "Generate linting report for BUFFER.
1046 CHECKERS is the list of checkers used.
1048 Return an alist (ID [LINE TRUST DESCRIPTION CHECKER]), suitable
1049 for `tabulated-list-printer'."
1050 (with-current-buffer buffer
1052 (goto-char (point-min))
1053 (let ((ast (org-element-parse-buffer))
1057 ;; Insert unique ID for each report. Replace buffer positions
1058 ;; with line numbers.
1066 (goto-char (car report
))
1068 (prog1 (number-to-string
1069 (incf last-line
(count-lines last-pos
(point))))
1070 (setf last-pos
(point))))
1072 ;; Insert trust level in generated reports. Also sort them
1073 ;; by buffer position in order to optimize lines computation.
1076 (let ((trust (symbol-name (org-lint-checker-trust c
))))
1079 (list (car report
) trust
(nth 1 report
) c
))
1082 (intern (format "org-lint-%s"
1083 (org-lint-checker-name c
)))
1086 #'car-less-than-car
))))))
1088 (defvar-local org-lint--source-buffer nil
1089 "Source buffer associated to current report buffer.")
1091 (defvar-local org-lint--local-checkers nil
1092 "List of checkers used to build current report.")
1094 (defun org-lint--refresh-reports ()
1095 (setq tabulated-list-entries
1096 (org-lint--generate-reports org-lint--source-buffer
1097 org-lint--local-checkers
))
1098 (tabulated-list-print))
1100 (defun org-lint--current-line ()
1101 "Return current report line, as a number."
1102 (string-to-number (aref (tabulated-list-get-entry) 0)))
1104 (defun org-lint--current-checker (&optional entry
)
1105 "Return current report checker.
1106 When optional argument ENTRY is non-nil, use this entry instead
1108 (aref (if entry
(nth 1 entry
) (tabulated-list-get-entry)) 3))
1110 (defun org-lint--display-reports (source checkers
)
1111 "Display linting reports for buffer SOURCE.
1112 CHECKERS is the list of checkers used."
1113 (let ((buffer (get-buffer-create "*Org Lint*")))
1114 (with-current-buffer buffer
1115 (org-lint--report-mode)
1116 (setf org-lint--source-buffer source
)
1117 (setf org-lint--local-checkers checkers
)
1118 (org-lint--refresh-reports)
1119 (tabulated-list-print)
1120 (add-hook 'tabulated-list-revert-hook
#'org-lint--refresh-reports nil t
))
1121 (pop-to-buffer buffer
)))
1123 (defun org-lint--jump-to-source ()
1124 "Move to source line that generated the report at point."
1126 (let ((l (org-lint--current-line)))
1127 (switch-to-buffer-other-window org-lint--source-buffer
)
1129 (org-show-set-visibility 'local
)
1132 (defun org-lint--show-source ()
1133 "Show source line that generated the report at point."
1135 (let ((buffer (current-buffer)))
1136 (org-lint--jump-to-source)
1137 (switch-to-buffer-other-window buffer
)))
1139 (defun org-lint--hide-checker ()
1140 "Hide all reports from checker that generated the report at point."
1142 (let ((c (org-lint--current-checker)))
1143 (setf tabulated-list-entries
1144 (cl-remove-if (lambda (e) (equal c
(org-lint--current-checker e
)))
1145 tabulated-list-entries
))
1146 (tabulated-list-print)))
1148 (defun org-lint--ignore-checker ()
1149 "Ignore all reports from checker that generated the report at point.
1150 Checker will also be ignored in all subsequent reports."
1152 (setf org-lint--local-checkers
1153 (remove (org-lint--current-checker) org-lint--local-checkers
))
1154 (org-lint--hide-checker))
1160 (defun org-lint (&optional arg
)
1161 "Check current Org buffer for syntax mistakes.
1163 By default, run all checkers. With a single prefix ARG \
1164 \\[universal-argument],
1165 select one category of checkers only. With a double prefix
1166 \\[universal-argument] \\[universal-argument], select one precise \
1167 checker by its name.
1169 ARG can also be a list of checker names, as symbols, to run."
1171 (unless (derived-mode-p 'org-mode
) (user-error "Not in an Org buffer"))
1172 (when (org-called-interactively-p)
1173 (message "Org linting process starting..."))
1176 (`nil org-lint--checkers
)
1180 "Checker category: "
1181 (mapcar #'org-lint-checker-categories org-lint--checkers
)
1185 (assoc-string (org-lint-checker-categories c
) category
))
1186 org-lint--checkers
)))
1189 (let ((name (completing-read
1191 (mapcar #'org-lint-checker-name org-lint--checkers
)
1194 (dolist (c org-lint--checkers
)
1195 (when (string= (org-lint-checker-name c
) name
)
1196 (throw 'exit c
)))))))
1198 (cl-remove-if-not (lambda (c) (memq (org-lint-checker-name c
) arg
))
1199 org-lint--checkers
))
1200 (_ (user-error "Invalid argument `%S' for `org-lint'" arg
)))))
1201 (if (not (org-called-interactively-p))
1202 (org-lint--generate-reports (current-buffer) checkers
)
1203 (org-lint--display-reports (current-buffer) checkers
)
1204 (message "Org linting process completed"))))
1208 ;;; org-lint.el ends here