1 ;;; org-lint.el --- Linting for Org documents -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2018 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;; This library implements linting for Org syntax. The sole public
26 ;; function is `org-lint', which see.
28 ;; Internally, the library defines a new structure:
29 ;; `org-lint-checker', with the following slots:
31 ;; - NAME: Unique check identifier, as a non-nil symbol that doesn't
32 ;; start with an hyphen.
34 ;; The check is done calling the function `org-lint-NAME' with one
35 ;; mandatory argument, the parse tree describing the current Org
36 ;; buffer. Such function calls are wrapped within
37 ;; a `save-excursion' and point is always at `point-min'. Its
38 ;; return value has to be an alist (POSITION MESSAGE) when
39 ;; POSITION refer to the buffer position of the error, as an
40 ;; integer, and MESSAGE is a string describing the error.
42 ;; - DESCRIPTION: Summary about the check, as a string.
44 ;; - CATEGORIES: Categories relative to the check, as a list of
45 ;; symbol. They are used for filtering when calling `org-lint'.
46 ;; Checkers not explicitly associated to a category are collected
47 ;; in the `default' one.
49 ;; - TRUST: The trust level one can have in the check. It is either
50 ;; `low' or `high', depending on the heuristics implemented and
51 ;; the nature of the check. This has an indicative value only and
52 ;; is displayed along reports.
54 ;; All checks have to be listed in `org-lint--checkers'.
56 ;; Results are displayed in a special "*Org Lint*" buffer with
57 ;; a dedicated major mode, derived from `tabulated-list-mode'.
59 ;; In addition to the usual key-bindings inherited from it, "C-j" and
60 ;; "TAB" display problematic line reported under point whereas "RET"
61 ;; jumps to it. Also, "h" hides all reports similar to the current
62 ;; one. Additionally, "i" removes them from subsequent reports.
64 ;; Checks currently implemented are:
66 ;; - duplicate CUSTOM_ID properties
67 ;; - duplicate NAME values
68 ;; - duplicate targets
69 ;; - duplicate footnote definitions
70 ;; - orphaned affiliated keywords
71 ;; - obsolete affiliated keywords
72 ;; - missing language in src blocks
73 ;; - missing back-end in export blocks
74 ;; - invalid Babel call blocks
75 ;; - NAME values with a colon
76 ;; - deprecated export block syntax
77 ;; - deprecated Babel header properties
78 ;; - wrong header arguments in src blocks
79 ;; - misuse of CATEGORY keyword
80 ;; - "coderef" links with unknown destination
81 ;; - "custom-id" links with unknown destination
82 ;; - "fuzzy" links with unknown destination
83 ;; - "id" links with unknown destination
84 ;; - links to non-existent local files
85 ;; - SETUPFILE keywords with non-existent file parameter
86 ;; - INCLUDE keywords with wrong link parameter
87 ;; - obsolete markup in INCLUDE keyword
88 ;; - unknown items in OPTIONS keyword
89 ;; - spurious macro arguments or invalid macro templates
90 ;; - special properties in properties drawer
91 ;; - obsolete syntax for PROPERTIES drawers
92 ;; - Invalid EFFORT property value
93 ;; - missing definition for footnote references
94 ;; - missing reference for footnote definitions
95 ;; - non-footnote definitions in footnote section
96 ;; - probable invalid keywords
98 ;; - misplaced planning info line
99 ;; - incomplete drawers
100 ;; - indented diary-sexps
101 ;; - obsolete QUOTE section
102 ;; - obsolete "file+application" link
108 (require 'org-element
)
116 (cl-defstruct (org-lint-checker (:copier nil
))
117 (name 'missing-checker-name
)
119 (categories '(default))
120 (trust 'high
)) ; `low' or `high'
122 (defun org-lint-missing-checker-name (_)
124 "`A checker has no `:name' property. Please verify `org-lint--checkers'"))
126 (defconst org-lint--checkers
128 (make-org-lint-checker
129 :name
'duplicate-custom-id
130 :description
"Report duplicates CUSTOM_ID properties"
132 (make-org-lint-checker
133 :name
'duplicate-name
134 :description
"Report duplicate NAME values"
135 :categories
'(babel link
))
136 (make-org-lint-checker
137 :name
'duplicate-target
138 :description
"Report duplicate targets"
140 (make-org-lint-checker
141 :name
'duplicate-footnote-definition
142 :description
"Report duplicate footnote definitions"
143 :categories
'(footnote))
144 (make-org-lint-checker
145 :name
'orphaned-affiliated-keywords
146 :description
"Report orphaned affiliated keywords"
148 (make-org-lint-checker
149 :name
'obsolete-affiliated-keywords
150 :description
"Report obsolete affiliated keywords"
151 :categories
'(obsolete))
152 (make-org-lint-checker
153 :name
'deprecated-export-blocks
154 :description
"Report deprecated export block syntax"
155 :categories
'(obsolete export
)
157 (make-org-lint-checker
158 :name
'deprecated-header-syntax
159 :description
"Report deprecated Babel header syntax"
160 :categories
'(obsolete babel
)
162 (make-org-lint-checker
163 :name
'missing-language-in-src-block
164 :description
"Report missing language in src blocks"
165 :categories
'(babel))
166 (make-org-lint-checker
167 :name
'missing-backend-in-export-block
168 :description
"Report missing back-end in export blocks"
169 :categories
'(export))
170 (make-org-lint-checker
171 :name
'invalid-babel-call-block
172 :description
"Report invalid Babel call blocks"
173 :categories
'(babel))
174 (make-org-lint-checker
176 :description
"Report NAME values with a colon"
177 :categories
'(babel))
178 (make-org-lint-checker
179 :name
'wrong-header-argument
180 :description
"Report wrong babel headers"
181 :categories
'(babel))
182 (make-org-lint-checker
183 :name
'wrong-header-value
184 :description
"Report invalid value in babel headers"
187 (make-org-lint-checker
188 :name
'deprecated-category-setup
189 :description
"Report misuse of CATEGORY keyword"
190 :categories
'(obsolete))
191 (make-org-lint-checker
192 :name
'invalid-coderef-link
193 :description
"Report \"coderef\" links with unknown destination"
195 (make-org-lint-checker
196 :name
'invalid-custom-id-link
197 :description
"Report \"custom-id\" links with unknown destination"
199 (make-org-lint-checker
200 :name
'invalid-fuzzy-link
201 :description
"Report \"fuzzy\" links with unknown destination"
203 (make-org-lint-checker
204 :name
'invalid-id-link
205 :description
"Report \"id\" links with unknown destination"
207 (make-org-lint-checker
208 :name
'link-to-local-file
209 :description
"Report links to non-existent local files"
212 (make-org-lint-checker
213 :name
'non-existent-setupfile-parameter
214 :description
"Report SETUPFILE keywords with non-existent file parameter"
216 (make-org-lint-checker
217 :name
'wrong-include-link-parameter
218 :description
"Report INCLUDE keywords with misleading link parameter"
219 :categories
'(export)
221 (make-org-lint-checker
222 :name
'obsolete-include-markup
223 :description
"Report obsolete markup in INCLUDE keyword"
224 :categories
'(obsolete export
)
226 (make-org-lint-checker
227 :name
'unknown-options-item
228 :description
"Report unknown items in OPTIONS keyword"
229 :categories
'(export)
231 (make-org-lint-checker
232 :name
'invalid-macro-argument-and-template
233 :description
"Report spurious macro arguments or invalid macro templates"
234 :categories
'(export)
236 (make-org-lint-checker
237 :name
'special-property-in-properties-drawer
238 :description
"Report special properties in properties drawers"
239 :categories
'(properties))
240 (make-org-lint-checker
241 :name
'obsolete-properties-drawer
242 :description
"Report obsolete syntax for properties drawers"
243 :categories
'(obsolete properties
))
244 (make-org-lint-checker
245 :name
'invalid-effort-property
246 :description
"Report invalid duration in EFFORT property"
247 :categories
'(properties))
248 (make-org-lint-checker
249 :name
'undefined-footnote-reference
250 :description
"Report missing definition for footnote references"
251 :categories
'(footnote))
252 (make-org-lint-checker
253 :name
'unreferenced-footnote-definition
254 :description
"Report missing reference for footnote definitions"
255 :categories
'(footnote))
256 (make-org-lint-checker
257 :name
'extraneous-element-in-footnote-section
258 :description
"Report non-footnote definitions in footnote section"
259 :categories
'(footnote))
260 (make-org-lint-checker
261 :name
'invalid-keyword-syntax
262 :description
"Report probable invalid keywords"
264 (make-org-lint-checker
266 :description
"Report invalid blocks"
268 (make-org-lint-checker
269 :name
'misplaced-planning-info
270 :description
"Report misplaced planning info line"
272 (make-org-lint-checker
273 :name
'incomplete-drawer
274 :description
"Report probable incomplete drawers"
276 (make-org-lint-checker
277 :name
'indented-diary-sexp
278 :description
"Report probable indented diary-sexps"
280 (make-org-lint-checker
282 :description
"Report obsolete QUOTE section"
283 :categories
'(obsolete)
285 (make-org-lint-checker
286 :name
'file-application
287 :description
"Report obsolete \"file+application\" link"
288 :categories
'(link obsolete
)))
289 "List of all available checkers.")
291 (defun org-lint--collect-duplicates
292 (ast type extract-key extract-position build-message
)
293 "Helper function to collect duplicates in parse tree AST.
295 EXTRACT-KEY is a function extracting key. It is called with
296 a single argument: the element or object. Comparison is done
299 EXTRACT-POSITION is a function returning position for the report.
300 It is called with two arguments, the object or element, and the
303 BUILD-MESSAGE is a function creating the report message. It is
304 called with one argument, the key used for comparison."
309 (lambda (position value
)
310 (push (list position
(funcall build-message value
)) reports
))))
311 (org-element-map ast type
313 (let ((key (funcall extract-key datum
)))
316 ((assoc key keys
) (cl-pushnew (assoc key keys
) originals
)
317 (funcall make-report
(funcall extract-position datum key
) key
))
318 (t (push (cons key
(funcall extract-position datum key
)) keys
))))))
319 (dolist (e originals reports
) (funcall make-report
(cdr e
) (car e
)))))
321 (defun org-lint-duplicate-custom-id (ast)
322 (org-lint--collect-duplicates
326 (and (eq (compare-strings "CUSTOM_ID" nil nil
327 (org-element-property :key property
) nil nil
330 (org-element-property :value property
)))
331 (lambda (property _
) (org-element-property :begin property
))
332 (lambda (key) (format "Duplicate CUSTOM_ID property \"%s\"" key
))))
334 (defun org-lint-duplicate-name (ast)
335 (org-lint--collect-duplicates
337 org-element-all-elements
338 (lambda (datum) (org-element-property :name datum
))
340 (goto-char (org-element-property :begin datum
))
342 (format "^[ \t]*#\\+[A-Za-z]+: +%s *$" (regexp-quote name
)))
344 (lambda (key) (format "Duplicate NAME \"%s\"" key
))))
346 (defun org-lint-duplicate-target (ast)
347 (org-lint--collect-duplicates
350 (lambda (target) (split-string (org-element-property :value target
)))
351 (lambda (target _
) (org-element-property :begin target
))
353 (format "Duplicate target <<%s>>" (mapconcat #'identity key
" ")))))
355 (defun org-lint-duplicate-footnote-definition (ast)
356 (org-lint--collect-duplicates
359 (lambda (definition) (org-element-property :label definition
))
360 (lambda (definition _
) (org-element-property :post-affiliated definition
))
361 (lambda (key) (format "Duplicate footnote definition \"%s\"" key
))))
363 (defun org-lint-orphaned-affiliated-keywords (ast)
364 ;; Ignore orphan RESULTS keywords, which could be generated from
365 ;; a source block returning no value.
366 (let ((keywords (cl-set-difference org-element-affiliated-keywords
367 '("RESULT" "RESULTS")
369 (org-element-map ast
'keyword
371 (let ((key (org-element-property :key k
)))
372 (and (or (let ((case-fold-search t
))
373 (string-match-p "\\`ATTR_[-_A-Za-z0-9]+\\'" key
))
374 (member key keywords
))
375 (list (org-element-property :post-affiliated k
)
376 (format "Orphaned affiliated keyword: \"%s\"" key
))))))))
378 (defun org-lint-obsolete-affiliated-keywords (_)
379 (let ((regexp (format "^[ \t]*#\\+%s:"
380 (regexp-opt '("DATA" "LABEL" "RESNAME" "SOURCE"
381 "SRCNAME" "TBLNAME" "RESULT" "HEADERS")
384 (while (re-search-forward regexp nil t
)
385 (let ((key (upcase (match-string-no-properties 1))))
387 (org-element-property :post-affiliated
(org-element-at-point)))
389 (list (line-beginning-position)
391 "Obsolete affiliated keyword: \"%s\". Use \"%s\" instead"
400 (defun org-lint-deprecated-export-blocks (ast)
401 (let ((deprecated '("ASCII" "BEAMER" "HTML" "LATEX" "MAN" "MARKDOWN" "MD"
402 "ODT" "ORG" "TEXINFO")))
403 (org-element-map ast
'special-block
405 (let ((type (org-element-property :type b
)))
406 (when (member-ignore-case type deprecated
)
408 (org-element-property :post-affiliated b
)
410 "Deprecated syntax for export block. Use \"BEGIN_EXPORT %s\" \
414 (defun org-lint-deprecated-header-syntax (ast)
415 (let* ((deprecated-babel-properties
416 (mapcar (lambda (arg) (symbol-name (car arg
)))
417 org-babel-common-header-args-w-values
))
419 (format "\\`%s[ \t]" (regexp-opt deprecated-babel-properties t
))))
420 (org-element-map ast
'(keyword node-property
)
422 (let ((key (org-element-property :key datum
)))
423 (pcase (org-element-type datum
)
425 (let ((value (org-element-property :value datum
)))
426 (and (string= key
"PROPERTY")
427 (string-match deprecated-re value
)
428 (list (org-element-property :begin datum
)
429 (format "Deprecated syntax for \"%s\". \
430 Use header-args instead"
431 (match-string-no-properties 1 value
))))))
433 (and (member-ignore-case key deprecated-babel-properties
)
435 (org-element-property :begin datum
)
436 (format "Deprecated syntax for \"%s\". \
437 Use :header-args: instead"
440 (defun org-lint-missing-language-in-src-block (ast)
441 (org-element-map ast
'src-block
443 (unless (org-element-property :language b
)
444 (list (org-element-property :post-affiliated b
)
445 "Missing language in source block")))))
447 (defun org-lint-missing-backend-in-export-block (ast)
448 (org-element-map ast
'export-block
450 (unless (org-element-property :type b
)
451 (list (org-element-property :post-affiliated b
)
452 "Missing back-end in export block")))))
454 (defun org-lint-invalid-babel-call-block (ast)
455 (org-element-map ast
'babel-call
458 ((not (org-element-property :call b
))
459 (list (org-element-property :post-affiliated b
)
460 "Invalid syntax in babel call block"))
461 ((let ((h (org-element-property :end-header b
)))
462 (and h
(string-match-p "\\`\\[.*\\]\\'" h
)))
464 (org-element-property :post-affiliated b
)
465 "Babel call's end header must not be wrapped within brackets"))))))
467 (defun org-lint-deprecated-category-setup (ast)
468 (org-element-map ast
'keyword
472 ((not (string= (org-element-property :key k
) "CATEGORY")) nil
)
474 (list (org-element-property :post-affiliated k
)
475 "Spurious CATEGORY keyword. Set :CATEGORY: property instead"))
476 (t (setf category-flag t
) nil
))))))
478 (defun org-lint-invalid-coderef-link (ast)
479 (let ((info (list :parse-tree ast
)))
480 (org-element-map ast
'link
482 (let ((ref (org-element-property :path link
)))
483 (and (equal (org-element-property :type link
) "coderef")
484 (not (ignore-errors (org-export-resolve-coderef ref info
)))
485 (list (org-element-property :begin link
)
486 (format "Unknown coderef \"%s\"" ref
))))))))
488 (defun org-lint-invalid-custom-id-link (ast)
489 (let ((info (list :parse-tree ast
)))
490 (org-element-map ast
'link
492 (and (equal (org-element-property :type link
) "custom-id")
493 (not (ignore-errors (org-export-resolve-id-link link info
)))
494 (list (org-element-property :begin link
)
495 (format "Unknown custom ID \"%s\""
496 (org-element-property :path link
))))))))
498 (defun org-lint-invalid-fuzzy-link (ast)
499 (let ((info (list :parse-tree ast
)))
500 (org-element-map ast
'link
502 (and (equal (org-element-property :type link
) "fuzzy")
503 (not (ignore-errors (org-export-resolve-fuzzy-link link info
)))
504 (list (org-element-property :begin link
)
505 (format "Unknown fuzzy location \"%s\""
506 (let ((path (org-element-property :path link
)))
507 (if (string-prefix-p "*" path
)
511 (defun org-lint-invalid-id-link (ast)
512 (org-element-map ast
'link
514 (let ((id (org-element-property :path link
)))
515 (and (equal (org-element-property :type link
) "id")
516 (not (org-id-find id
))
517 (list (org-element-property :begin link
)
518 (format "Unknown ID \"%s\"" id
)))))))
520 (defun org-lint-special-property-in-properties-drawer (ast)
521 (org-element-map ast
'node-property
523 (let ((key (org-element-property :key p
)))
524 (and (member-ignore-case key org-special-properties
)
525 (list (org-element-property :begin p
)
527 "Special property \"%s\" found in a properties drawer"
530 (defun org-lint-obsolete-properties-drawer (ast)
531 (org-element-map ast
'drawer
533 (when (equal (org-element-property :drawer-name d
) "PROPERTIES")
534 (let ((section (org-element-lineage d
'(section))))
535 (unless (org-element-map section
'property-drawer
#'identity nil t
)
536 (list (org-element-property :post-affiliated d
)
538 (goto-char (org-element-property :post-affiliated d
))
540 (or (org-at-heading-p) (org-at-planning-p)))
541 "Incorrect contents for PROPERTIES drawer"
542 "Incorrect location for PROPERTIES drawer"))))))))
544 (defun org-lint-invalid-effort-property (ast)
545 (org-element-map ast
'node-property
547 (when (equal "EFFORT" (org-element-property :key p
))
548 (let ((value (org-element-property :value p
)))
549 (and (org-string-nw-p value
)
550 (not (org-duration-p value
))
551 (list (org-element-property :begin p
)
552 (format "Invalid effort duration format: %S" value
))))))))
554 (defun org-lint-link-to-local-file (ast)
555 (org-element-map ast
'link
557 (when (equal (org-element-property :type l
) "file")
558 (let ((file (org-link-unescape (org-element-property :path l
))))
559 (and (not (file-remote-p file
))
560 (not (file-exists-p file
))
561 (list (org-element-property :begin l
)
562 (format (if (org-element-lineage l
'(link))
563 "Link to non-existent image file \"%s\"\
565 "Link to non-existent local file \"%s\"")
568 (defun org-lint-non-existent-setupfile-parameter (ast)
569 (org-element-map ast
'keyword
571 (when (equal (org-element-property :key k
) "SETUPFILE")
572 (let ((file (org-unbracket-string
574 (org-element-property :value k
))))
575 (and (not (file-remote-p file
))
576 (not (file-exists-p file
))
577 (list (org-element-property :begin k
)
578 (format "Non-existent setup file \"%s\"" file
))))))))
580 (defun org-lint-wrong-include-link-parameter (ast)
581 (org-element-map ast
'keyword
583 (when (equal (org-element-property :key k
) "INCLUDE")
584 (let* ((value (org-element-property :value k
))
586 (and (string-match "^\\(\".+\"\\|\\S-+\\)[ \t]*" value
)
588 (org-unbracket-string "\"" "\"" (match-string 1 value
))))))
590 (list (org-element-property :post-affiliated k
)
591 "Missing location argument in INCLUDE keyword")
592 (let* ((file (org-string-nw-p
593 (if (string-match "::\\(.*\\)\\'" path
)
594 (substring path
0 (match-beginning 0))
596 (search (and (not (equal file path
))
597 (org-string-nw-p (match-string 1 path
)))))
599 (not (file-remote-p file
))
600 (not (file-exists-p file
)))
601 (list (org-element-property :post-affiliated k
)
602 "Non-existent file argument in INCLUDE keyword")
603 (let* ((visiting (if file
(find-buffer-visiting file
)
605 (buffer (or visiting
(find-file-noselect file
))))
607 (with-current-buffer buffer
611 (let ((org-link-search-inhibit-query t
))
612 (org-link-search search nil t
)))))
613 (list (org-element-property :post-affiliated k
)
615 "Invalid search part \"%s\" in INCLUDE keyword"
617 (unless visiting
(kill-buffer buffer
))))))))))))
619 (defun org-lint-obsolete-include-markup (ast)
620 (let ((regexp (format "\\`\\(?:\".+\"\\|\\S-+\\)[ \t]+%s"
622 '("ASCII" "BEAMER" "HTML" "LATEX" "MAN" "MARKDOWN" "MD"
623 "ODT" "ORG" "TEXINFO")
625 (org-element-map ast
'keyword
627 (when (equal (org-element-property :key k
) "INCLUDE")
628 (let ((case-fold-search t
)
629 (value (org-element-property :value k
)))
630 (when (string-match regexp value
)
631 (let ((markup (match-string-no-properties 1 value
)))
632 (list (org-element-property :post-affiliated k
)
633 (format "Obsolete markup \"%s\" in INCLUDE keyword. \
634 Use \"export %s\" instead"
638 (defun org-lint-unknown-options-item (ast)
639 (let ((allowed (delq nil
641 (mapcar (lambda (o) (nth 2 o
)) org-export-options-alist
)
644 (mapcar (lambda (o) (nth 2 o
))
645 (org-export-backend-options b
)))
646 org-export-registered-backends
))))
648 (org-element-map ast
'keyword
650 (when (string= (org-element-property :key k
) "OPTIONS")
651 (let ((value (org-element-property :value k
))
653 (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-*\\)[ \t]*"
656 (setf start
(match-end 0))
657 (let ((item (match-string 1 value
)))
658 (unless (member item allowed
)
659 (push (list (org-element-property :post-affiliated k
)
660 (format "Unknown OPTIONS item \"%s\"" item
))
664 (defun org-lint-invalid-macro-argument-and-template (ast)
665 (let ((extract-placeholders
669 (while (string-match "\\$\\([1-9][0-9]*\\)" template start
)
670 (setf start
(match-end 0))
671 (push (string-to-number (match-string 1 template
)) args
))
672 (sort (org-uniquify args
) #'<))))
674 ;; Check arguments for macro templates.
675 (org-element-map ast
'keyword
677 (when (string= (org-element-property :key k
) "MACRO")
678 (let* ((value (org-element-property :value k
))
679 (name (and (string-match "^\\S-+" value
)
680 (match-string 0 value
)))
682 (org-trim (substring value
(match-end 0))))))
685 (push (list (org-element-property :post-affiliated k
)
686 "Missing name in MACRO keyword")
688 ((not (org-string-nw-p template
))
689 (push (list (org-element-property :post-affiliated k
)
690 "Missing template in macro \"%s\"" name
)
693 (unless (let ((args (funcall extract-placeholders template
)))
694 (equal (number-sequence 1 (or (org-last args
) 0)) args
))
695 (push (list (org-element-property :post-affiliated k
)
696 (format "Unused placeholders in macro \"%s\""
699 ;; Check arguments for macros.
700 (org-macro-initialize-templates)
701 (let ((templates (append
702 (mapcar (lambda (m) (cons m
"$1"))
703 '("author" "date" "email" "title" "results"))
704 org-macro-templates
)))
705 (org-element-map ast
'macro
707 (let* ((name (org-element-property :key macro
))
708 (template (cdr (assoc-string name templates t
))))
710 (push (list (org-element-property :begin macro
)
711 (format "Undefined macro \"%s\"" name
))
713 (let ((arg-numbers (funcall extract-placeholders template
)))
716 (nthcdr (apply #'max arg-numbers
)
717 (org-element-property :args macro
))))
720 (list (org-element-property :begin macro
)
721 (format "Unused argument%s in macro \"%s\": %s"
722 (if (> (length spurious-args
) 1) "s" "")
724 (mapconcat (lambda (a) (format "\"%s\"" a
))
730 (defun org-lint-undefined-footnote-reference (ast)
731 (let ((definitions (org-element-map ast
'footnote-definition
732 (lambda (f) (org-element-property :label f
)))))
733 (org-element-map ast
'footnote-reference
735 (let ((label (org-element-property :label f
)))
736 (and (eq 'standard
(org-element-property :type f
))
737 (not (member label definitions
))
738 (list (org-element-property :begin f
)
739 (format "Missing definition for footnote [%s]"
742 (defun org-lint-unreferenced-footnote-definition (ast)
743 (let ((references (org-element-map ast
'footnote-reference
744 (lambda (f) (org-element-property :label f
)))))
745 (org-element-map ast
'footnote-definition
747 (let ((label (org-element-property :label f
)))
749 (not (member label references
))
750 (list (org-element-property :post-affiliated f
)
751 (format "No reference for footnote definition [%s]"
754 (defun org-lint-colon-in-name (ast)
755 (org-element-map ast org-element-all-elements
757 (let ((name (org-element-property :name e
)))
759 (string-match-p ":" name
)
761 (goto-char (org-element-property :begin e
))
763 (format "^[ \t]*#\\+\\w+: +%s *$" (regexp-quote name
)))
766 "Name \"%s\" contains a colon; Babel cannot use it as input"
769 (defun org-lint-misplaced-planning-info (_)
770 (let ((case-fold-search t
)
772 (while (re-search-forward org-planning-line-re nil t
)
773 (unless (memq (org-element-type (org-element-at-point))
774 '(comment-block example-block export-block planning
775 src-block verse-block
))
776 (push (list (line-beginning-position) "Misplaced planning info line")
780 (defun org-lint-incomplete-drawer (_)
782 (while (re-search-forward org-drawer-regexp nil t
)
783 (let ((name (org-trim (match-string-no-properties 0)))
784 (element (org-element-at-point)))
785 (pcase (org-element-type element
)
786 ((or `drawer
`property-drawer
)
787 (goto-char (org-element-property :end element
))
789 ((or `comment-block
`example-block
`export-block
`src-block
793 (push (list (line-beginning-position)
794 (format "Possible incomplete drawer \"%s\"" name
))
798 (defun org-lint-indented-diary-sexp (_)
800 (while (re-search-forward "^[ \t]+%%(" nil t
)
801 (unless (memq (org-element-type (org-element-at-point))
802 '(comment-block diary-sexp example-block export-block
803 src-block verse-block
))
804 (push (list (line-beginning-position) "Possible indented diary-sexp")
808 (defun org-lint-invalid-block (_)
809 (let ((case-fold-search t
)
810 (regexp "^[ \t]*#\\+\\(BEGIN\\|END\\)\\(?::\\|_[^[:space:]]*\\)?[ \t]*")
812 (while (re-search-forward regexp nil t
)
813 (let ((name (org-trim (buffer-substring-no-properties
814 (line-beginning-position) (line-end-position)))))
816 ((and (string-prefix-p "END" (match-string 1) t
)
818 (push (list (line-beginning-position)
819 (format "Invalid block closing line \"%s\"" name
))
821 ((not (memq (org-element-type (org-element-at-point))
822 '(center-block comment-block dynamic-block example-block
823 export-block quote-block special-block
824 src-block verse-block
)))
825 (push (list (line-beginning-position)
826 (format "Possible incomplete block \"%s\""
831 (defun org-lint-invalid-keyword-syntax (_)
832 (let ((regexp "^[ \t]*#\\+\\([^[:space:]:]*\\)\\(?: \\|$\\)")
834 (format "[ \t]*#\\+%s\\(\\[.*\\]\\)?:\\(?: \\|$\\)"
835 (regexp-opt org-element-dual-keywords
)))
837 (while (re-search-forward regexp nil t
)
838 (let ((name (match-string-no-properties 1)))
839 (unless (or (string-prefix-p "BEGIN" name t
)
840 (string-prefix-p "END" name t
)
843 (let ((case-fold-search t
)) (looking-at exception-re
))))
844 (push (list (match-beginning 0)
845 (format "Possible missing colon in keyword \"%s\"" name
))
849 (defun org-lint-extraneous-element-in-footnote-section (ast)
850 (org-element-map ast
'headline
852 (and (org-element-property :footnote-section-p h
)
853 (org-element-map (org-element-contents h
)
856 (memq e
'(comment comment-block footnote-definition
857 property-drawer section
)))
858 org-element-all-elements
)
860 (not (and (eq (org-element-type e
) 'headline
)
861 (org-element-property :commentedp e
))))
862 nil t
'(footnote-definition property-drawer
))
863 (list (org-element-property :begin h
)
864 "Extraneous elements in footnote section are not exported")))))
866 (defun org-lint-quote-section (ast)
867 (org-element-map ast
'(headline inlinetask
)
869 (let ((title (org-element-property :raw-value h
)))
870 (and (or (string-prefix-p "QUOTE " title
)
871 (string-prefix-p (concat org-comment-string
" QUOTE ") title
))
872 (list (org-element-property :begin h
)
873 "Deprecated QUOTE section"))))))
875 (defun org-lint-file-application (ast)
876 (org-element-map ast
'link
878 (let ((app (org-element-property :application l
)))
880 (list (org-element-property :begin l
)
881 (format "Deprecated \"file+%s\" link type" app
)))))))
883 (defun org-lint-wrong-header-argument (ast)
886 (lambda (datum language headers
)
888 ;; If LANGUAGE is specified, restrict allowed
889 ;; headers to both LANGUAGE-specific and default
890 ;; ones. Otherwise, accept headers from any loaded
893 org-babel-header-arg-names
896 (let ((v (intern (format "org-babel-header-args:%s" l
))))
897 (and (boundp v
) (mapcar #'car
(symbol-value v
)))))
898 (if language
(list language
)
899 (mapcar #'car org-babel-load-languages
))))))
900 (dolist (header headers
)
901 (let ((h (symbol-name (car header
)))
902 (p (or (org-element-property :post-affiliated datum
)
903 (org-element-property :begin datum
))))
905 ((not (string-prefix-p ":" h
))
908 (format "Missing colon in header argument \"%s\"" h
))
910 ((assoc-string (substring h
1) allowed
))
911 (t (push (list p
(format "Unknown header argument \"%s\"" h
))
913 (org-element-map ast
'(babel-call inline-babel-call inline-src-block keyword
914 node-property src-block
)
916 (pcase (org-element-type datum
)
917 ((or `babel-call
`inline-babel-call
)
921 (cl-mapcan #'org-babel-parse-header-arguments
923 (org-element-property :inside-header datum
)
924 (org-element-property :end-header datum
)))))
928 (org-element-property :language datum
)
929 (org-babel-parse-header-arguments
930 (org-element-property :parameters datum
))))
932 (when (string= (org-element-property :key datum
) "PROPERTY")
933 (let ((value (org-element-property :value datum
)))
934 (when (string-match "\\`header-args\\(?::\\(\\S-+\\)\\)?\\+? *"
938 (match-string 1 value
)
939 (org-babel-parse-header-arguments
940 (substring value
(match-end 0))))))))
942 (let ((key (org-element-property :key datum
)))
943 (when (let ((case-fold-search t
))
944 (string-match "\\`HEADER-ARGS\\(?::\\(\\S-+\\)\\)?\\+?"
949 (org-babel-parse-header-arguments
950 (org-element-property :value datum
))))))
954 (org-element-property :language datum
)
955 (cl-mapcan #'org-babel-parse-header-arguments
956 (cons (org-element-property :parameters datum
)
957 (org-element-property :header datum
))))))))
960 (defun org-lint-wrong-header-value (ast)
963 '(babel-call inline-babel-call inline-src-block src-block
)
965 (let* ((type (org-element-type datum
))
966 (language (org-element-property :language datum
))
967 (allowed-header-values
968 (append (and language
969 (let ((v (intern (concat "org-babel-header-args:"
971 (and (boundp v
) (symbol-value v
))))
972 org-babel-common-header-args-w-values
))
974 (org-babel-parse-header-arguments
980 (cons (org-element-property :parameters datum
)
981 (org-element-property :header datum
))
984 (or (org-element-property :parameters datum
) ""))
987 (org-element-property :inside-header datum
)
989 (org-element-property :end-header datum
))))))))
990 (dolist (header datum-header-values
)
991 (let ((allowed-values
992 (cdr (assoc-string (substring (symbol-name (car header
)) 1)
993 allowed-header-values
))))
994 (unless (memq allowed-values
'(:any nil
))
995 (let ((values (cdr header
))
997 (dolist (v (if (stringp values
) (split-string values
)
999 (let ((valid-value nil
))
1001 (dolist (group allowed-values
)
1004 (if (stringp v
) #'assoc-string
#'assoc
)
1006 (when (memq :any group
)
1007 (setf valid-value t
)
1008 (push (cons group v
) groups-alist
)))
1009 ((assq group groups-alist
)
1012 (or (org-element-property :post-affiliated datum
)
1013 (org-element-property :begin datum
))
1015 "Forbidden combination in header \"%s\": %s, %s"
1017 (cdr (assq group groups-alist
))
1021 (t (push (cons group v
) groups-alist
)
1022 (setf valid-value t
))))
1026 (or (org-element-property :post-affiliated datum
)
1027 (org-element-property :begin datum
))
1028 (format "Unknown value \"%s\" for header \"%s\""
1038 (defvar org-lint--report-mode-map
1039 (let ((map (make-sparse-keymap)))
1040 (set-keymap-parent map tabulated-list-mode-map
)
1041 (define-key map
(kbd "RET") 'org-lint--jump-to-source
)
1042 (define-key map
(kbd "TAB") 'org-lint--show-source
)
1043 (define-key map
(kbd "C-j") 'org-lint--show-source
)
1044 (define-key map
(kbd "h") 'org-lint--hide-checker
)
1045 (define-key map
(kbd "i") 'org-lint--ignore-checker
)
1047 "Local keymap for `org-lint--report-mode' buffers.")
1049 (define-derived-mode org-lint--report-mode tabulated-list-mode
"OrgLint"
1050 "Major mode used to display reports emitted during linting.
1051 \\{org-lint--report-mode-map}"
1052 (setf tabulated-list-format
1055 (< (string-to-number (aref (cadr a
) 0))
1056 (string-to-number (aref (cadr b
) 0))))
1060 (tabulated-list-init-header))
1062 (defun org-lint--generate-reports (buffer checkers
)
1063 "Generate linting report for BUFFER.
1065 CHECKERS is the list of checkers used.
1067 Return an alist (ID [LINE TRUST DESCRIPTION CHECKER]), suitable
1068 for `tabulated-list-printer'."
1069 (with-current-buffer buffer
1071 (goto-char (point-min))
1072 (let ((ast (org-element-parse-buffer))
1076 ;; Insert unique ID for each report. Replace buffer positions
1077 ;; with line numbers.
1085 (goto-char (car report
))
1087 (prog1 (number-to-string
1089 (count-lines last-pos
(point))))
1090 (setf last-pos
(point))))
1092 ;; Insert trust level in generated reports. Also sort them
1093 ;; by buffer position in order to optimize lines computation.
1096 (let ((trust (symbol-name (org-lint-checker-trust c
))))
1099 (list (car report
) trust
(nth 1 report
) c
))
1102 (intern (format "org-lint-%s"
1103 (org-lint-checker-name c
)))
1106 #'car-less-than-car
))))))
1108 (defvar-local org-lint--source-buffer nil
1109 "Source buffer associated to current report buffer.")
1111 (defvar-local org-lint--local-checkers nil
1112 "List of checkers used to build current report.")
1114 (defun org-lint--refresh-reports ()
1115 (setq tabulated-list-entries
1116 (org-lint--generate-reports org-lint--source-buffer
1117 org-lint--local-checkers
))
1118 (tabulated-list-print))
1120 (defun org-lint--current-line ()
1121 "Return current report line, as a number."
1122 (string-to-number (aref (tabulated-list-get-entry) 0)))
1124 (defun org-lint--current-checker (&optional entry
)
1125 "Return current report checker.
1126 When optional argument ENTRY is non-nil, use this entry instead
1128 (aref (if entry
(nth 1 entry
) (tabulated-list-get-entry)) 3))
1130 (defun org-lint--display-reports (source checkers
)
1131 "Display linting reports for buffer SOURCE.
1132 CHECKERS is the list of checkers used."
1133 (let ((buffer (get-buffer-create "*Org Lint*")))
1134 (with-current-buffer buffer
1135 (org-lint--report-mode)
1136 (setf org-lint--source-buffer source
)
1137 (setf org-lint--local-checkers checkers
)
1138 (org-lint--refresh-reports)
1139 (tabulated-list-print)
1140 (add-hook 'tabulated-list-revert-hook
#'org-lint--refresh-reports nil t
))
1141 (pop-to-buffer buffer
)))
1143 (defun org-lint--jump-to-source ()
1144 "Move to source line that generated the report at point."
1146 (let ((l (org-lint--current-line)))
1147 (switch-to-buffer-other-window org-lint--source-buffer
)
1149 (org-show-set-visibility 'local
)
1152 (defun org-lint--show-source ()
1153 "Show source line that generated the report at point."
1155 (let ((buffer (current-buffer)))
1156 (org-lint--jump-to-source)
1157 (switch-to-buffer-other-window buffer
)))
1159 (defun org-lint--hide-checker ()
1160 "Hide all reports from checker that generated the report at point."
1162 (let ((c (org-lint--current-checker)))
1163 (setf tabulated-list-entries
1164 (cl-remove-if (lambda (e) (equal c
(org-lint--current-checker e
)))
1165 tabulated-list-entries
))
1166 (tabulated-list-print)))
1168 (defun org-lint--ignore-checker ()
1169 "Ignore all reports from checker that generated the report at point.
1170 Checker will also be ignored in all subsequent reports."
1172 (setf org-lint--local-checkers
1173 (remove (org-lint--current-checker) org-lint--local-checkers
))
1174 (org-lint--hide-checker))
1180 (defun org-lint (&optional arg
)
1181 "Check current Org buffer for syntax mistakes.
1183 By default, run all checkers. With a `\\[universal-argument]' prefix ARG, \
1185 category of checkers only. With a `\\[universal-argument] \
1186 \\[universal-argument]' prefix, run one precise
1187 checker by its name.
1189 ARG can also be a list of checker names, as symbols, to run."
1191 (unless (derived-mode-p 'org-mode
) (user-error "Not in an Org buffer"))
1192 (when (called-interactively-p 'any
)
1193 (message "Org linting process starting..."))
1196 (`nil org-lint--checkers
)
1200 "Checker category: "
1201 (mapcar #'org-lint-checker-categories org-lint--checkers
)
1205 (assoc-string (org-lint-checker-categories c
) category
))
1206 org-lint--checkers
)))
1209 (let ((name (completing-read
1211 (mapcar #'org-lint-checker-name org-lint--checkers
)
1214 (dolist (c org-lint--checkers
)
1215 (when (string= (org-lint-checker-name c
) name
)
1216 (throw 'exit c
)))))))
1218 (cl-remove-if-not (lambda (c) (memq (org-lint-checker-name c
) arg
))
1219 org-lint--checkers
))
1220 (_ (user-error "Invalid argument `%S' for `org-lint'" arg
)))))
1221 (if (not (called-interactively-p 'any
))
1222 (org-lint--generate-reports (current-buffer) checkers
)
1223 (org-lint--display-reports (current-buffer) checkers
)
1224 (message "Org linting process completed"))))
1228 ;;; org-lint.el ends here