1 ;;; org-lint.el --- Linting for Org documents -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015 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 ;; - invalid Babel call blocks
72 ;; - NAME values with a colon
73 ;; - deprecated Babel header properties
74 ;; - wrong header arguments in src blocks
75 ;; - misuse of CATEGORY keyword
76 ;; - "coderef" links with unknown destination
77 ;; - "custom-id" links with unknown destination
78 ;; - "fuzzy" links with unknown destination
79 ;; - "id" links with unknown destination
80 ;; - links to non-existent local files
81 ;; - SETUPFILE keywords with non-existent file parameter
82 ;; - INCLUDE keywords with wrong link parameter
83 ;; - unknown items in OPTIONS keyword
84 ;; - spurious macro arguments or invalid macro templates
85 ;; - special properties in properties drawer
86 ;; - obsolete syntax for PROPERTIES drawers
87 ;; - missing definition for footnote references
88 ;; - missing reference for footnote definitions
89 ;; - non-footnote definitions in footnote section
90 ;; - probable invalid keywords
92 ;; - misplaced planning info line
93 ;; - incomplete drawers
94 ;; - indented diary-sexps
95 ;; - obsolete QUOTE section
101 (require 'org-element
)
109 (cl-defstruct (org-lint-checker (:copier nil
))
110 (name 'missing-checker-name
)
112 (categories '(default))
113 (trust 'high
)) ; `low' or `high'
115 (defun org-lint-missing-checker-name (_)
117 "`A checker has no `:name' property. Please verify `org-lint--checkers'"))
119 (defconst org-lint--checkers
121 (make-org-lint-checker
122 :name
'duplicate-custom-id
123 :description
"Report duplicates CUSTOM_ID properties"
125 (make-org-lint-checker
126 :name
'duplicate-name
127 :description
"Report duplicate NAME values"
128 :categories
'(babel link
))
129 (make-org-lint-checker
130 :name
'duplicate-target
131 :description
"Report duplicate targets"
133 (make-org-lint-checker
134 :name
'duplicate-footnote-definition
135 :description
"Report duplicate footnote definitions"
136 :categories
'(footnote))
137 (make-org-lint-checker
138 :name
'orphaned-affiliated-keywords
139 :description
"Report orphaned affiliated keywords"
141 (make-org-lint-checker
142 :name
'obsolete-affiliated-keywords
143 :description
"Report obsolete affiliated keywords"
144 :categories
'(obsolete))
145 (make-org-lint-checker
146 :name
'deprecated-header-syntax
147 :description
"Report deprecated Babel header syntax"
148 :categories
'(babel obsolete
)
150 (make-org-lint-checker
151 :name
'missing-language-in-src-block
152 :description
"Report missing language in src blocks"
153 :categories
'(babel))
154 (make-org-lint-checker
155 :name
'invalid-babel-call-block
156 :description
"Report invalid Babel call blocks"
157 :categories
'(babel))
158 (make-org-lint-checker
160 :description
"Report NAME values with a colon"
161 :categories
'(babel))
162 (make-org-lint-checker
163 :name
'wrong-header-argument
164 :description
"Report wrong babel headers"
165 :categories
'(babel))
166 (make-org-lint-checker
167 :name
'wrong-header-value
168 :description
"Report invalid value in babel headers"
171 (make-org-lint-checker
172 :name
'deprecated-category-setup
173 :description
"Report misuse of CATEGORY keyword"
174 :categories
'(obsolete))
175 (make-org-lint-checker
176 :name
'invalid-coderef-link
177 :description
"Report \"coderef\" links with unknown destination"
179 (make-org-lint-checker
180 :name
'invalid-custom-id-link
181 :description
"Report \"custom-id\" links with unknown destination"
183 (make-org-lint-checker
184 :name
'invalid-fuzzy-link
185 :description
"Report \"fuzzy\" links with unknown destination"
187 (make-org-lint-checker
188 :name
'invalid-id-link
189 :description
"Report \"id\" links with unknown destination"
191 (make-org-lint-checker
192 :name
'link-to-local-file
193 :description
"Report links to non-existent local files"
196 (make-org-lint-checker
197 :name
'non-existent-setupfile-parameter
198 :description
"Report SETUPFILE keywords with non-existent file parameter"
200 (make-org-lint-checker
201 :name
'wrong-include-link-parameter
202 :description
"Report INCLUDE keywords with misleading link parameter"
203 :categories
'(export)
205 (make-org-lint-checker
206 :name
'unknown-options-item
207 :description
"Report unknown items in OPTIONS keyword"
208 :categories
'(export)
210 (make-org-lint-checker
211 :name
'invalid-macro-argument-and-template
212 :description
"Report spurious macro arguments or invalid macro templates"
213 :categories
'(export)
215 (make-org-lint-checker
216 :name
'special-property-in-properties-drawer
217 :description
"Report special properties in properties drawers"
218 :categories
'(properties))
219 (make-org-lint-checker
220 :name
'obsolete-properties-drawer
221 :description
"Report obsolete syntax for properties drawers"
222 :categories
'(obsolete properties
))
223 (make-org-lint-checker
224 :name
'undefined-footnote-reference
225 :description
"Report missing definition for footnote references"
226 :categories
'(footnote))
227 (make-org-lint-checker
228 :name
'unreferenced-footnote-definition
229 :description
"Report missing reference for footnote definitions"
230 :categories
'(footnote))
231 (make-org-lint-checker
232 :name
'extraneous-element-in-footnote-section
233 :description
"Report non-footnote definitions in footnote section"
234 :categories
'(footnote))
235 (make-org-lint-checker
236 :name
'invalid-keyword-syntax
237 :description
"Report probable invalid keywords"
239 (make-org-lint-checker
241 :description
"Report invalid blocks"
243 (make-org-lint-checker
244 :name
'misplaced-planning-info
245 :description
"Report misplaced planning info line"
247 (make-org-lint-checker
248 :name
'incomplete-drawer
249 :description
"Report probable incomplete drawers"
251 (make-org-lint-checker
252 :name
'indented-diary-sexp
253 :description
"Report probable indented diary-sexps"
255 (make-org-lint-checker
257 :description
"Report obsolete QUOTE section"
258 :categories
'(obsolete)
260 "List of all available checkers.")
262 (defun org-lint--collect-duplicates
263 (ast type extract-key extract-position build-message
)
264 "Helper function to collect duplicates in parse tree AST.
266 EXTRACT-KEY is a function extracting key. It is called with
267 a single argument: the element or object. Comparison is done
270 EXTRACT-POSITION is a function returning position for the report.
271 It is called with two arguments, the object or element, and the
274 BUILD-MESSAGE is a function creating the report message. It is
275 called with one argument, the key used for comparison."
280 (lambda (position value
)
281 (push (list position
(funcall build-message value
)) reports
))))
282 (org-element-map ast type
284 (let ((key (funcall extract-key datum
)))
287 ((assoc key keys
) (cl-pushnew (assoc key keys
) originals
)
288 (funcall make-report
(funcall extract-position datum key
) key
))
289 (t (push (cons key
(funcall extract-position datum key
)) keys
))))))
290 (dolist (e originals reports
) (funcall make-report
(cdr e
) (car e
)))))
292 (defun org-lint-duplicate-custom-id (ast)
293 (org-lint--collect-duplicates
297 (and (eq (compare-strings "CUSTOM_ID" nil nil
298 (org-element-property :key property
) nil nil
301 (org-element-property :value property
)))
302 (lambda (property _
) (org-element-property :begin property
))
303 (lambda (key) (format "Duplicate CUSTOM_ID property \"%s\"" key
))))
305 (defun org-lint-duplicate-name (ast)
306 (org-lint--collect-duplicates
308 org-element-all-elements
309 (lambda (datum) (org-element-property :name datum
))
311 (goto-char (org-element-property :begin datum
))
313 (format "^[ \t]*#\\+[A-Za-z]+: +%s *$" (regexp-quote name
)))
315 (lambda (key) (format "Duplicate NAME \"%s\"" key
))))
317 (defun org-lint-duplicate-target (ast)
318 (org-lint--collect-duplicates
321 (lambda (target) (org-split-string (org-element-property :value target
)))
322 (lambda (target _
) (org-element-property :begin target
))
324 (format "Duplicate target <<%s>>" (mapconcat #'identity key
" ")))))
326 (defun org-lint-duplicate-footnote-definition (ast)
327 (org-lint--collect-duplicates
330 (lambda (definition) (org-element-property :label definition
))
331 (lambda (definition _
) (org-element-property :post-affiliated definition
))
332 (lambda (key) (format "Duplicate footnote definition \"%s\"" key
))))
334 (defun org-lint-orphaned-affiliated-keywords (ast)
335 ;; Ignore orphan RESULTS keywords, which could be generated from
336 ;; a source block returning no value.
337 (let ((keywords (cl-set-difference org-element-affiliated-keywords
338 '("RESULT" "RESULTS")
340 (org-element-map ast
'keyword
342 (let ((key (org-element-property :key k
)))
343 (and (or (let ((case-fold-search t
))
344 (org-string-match-p "\\`ATTR_[-_A-Za-z0-9]+\\'" key
))
345 (member key keywords
))
346 (list (org-element-property :post-affiliated k
)
347 (format "Orphaned affiliated keyword: \"%s\"" key
))))))))
349 (defun org-lint-obsolete-affiliated-keywords (_)
350 (let ((regexp (format "^[ \t]*#\\+%s:"
351 (regexp-opt '("DATA" "LABEL" "RESNAME" "SOURCE"
352 "SRCNAME" "TBLNAME" "RESULT" "HEADERS")
355 (while (re-search-forward regexp nil t
)
356 (let ((key (upcase (org-match-string-no-properties 1))))
358 (org-element-property :post-affiliated
(org-element-at-point)))
360 (list (line-beginning-position)
362 "Obsolete affiliated keyword: \"%s\". Use \"%s\" instead"
371 (defun org-lint-deprecated-header-syntax (ast)
372 (let* ((deprecated-babel-properties
373 (mapcar (lambda (arg) (symbol-name (car arg
)))
374 org-babel-common-header-args-w-values
))
376 (format "\\`%s[ \t]" (regexp-opt deprecated-babel-properties t
))))
377 (org-element-map ast
'(keyword node-property
)
379 (let ((key (org-element-property :key datum
)))
380 (pcase (org-element-type datum
)
382 (let ((value (org-element-property :value datum
)))
383 (and (string= key
"PROPERTY")
384 (string-match deprecated-re value
)
385 (list (org-element-property :begin datum
)
386 (format "Deprecated syntax for \"%s\". \
387 Use header-args instead"
388 (org-match-string-no-properties 1 value
))))))
390 (and (member-ignore-case key deprecated-babel-properties
)
392 (org-element-property :begin datum
)
393 (format "Deprecated syntax for \"%s\". \
394 Use :header-args: instead"
397 (defun org-lint-missing-language-in-src-block (ast)
398 (org-element-map ast
'src-block
400 (unless (org-element-property :language b
)
401 (list (org-element-property :post-affiliated b
)
402 "Missing language in source block")))))
404 (defun org-lint-invalid-babel-call-block (ast)
405 (org-element-map ast
'babel-call
408 ((not (org-element-property :call b
))
409 (list (org-element-property :post-affiliated b
)
410 "Invalid syntax in babel call block"))
411 ((let ((h (org-element-property :end-header b
)))
412 (and h
(org-string-match-p "\\`\\[.*\\]\\'" h
)))
414 (org-element-property :post-affiliated b
)
415 "Babel call's end header must not be wrapped within brackets"))))))
417 (defun org-lint-deprecated-category-setup (ast)
418 (org-element-map ast
'keyword
422 ((not (string= (org-element-property :key k
) "CATEGORY")) nil
)
424 (list (org-element-property :post-affiliated k
)
425 "Spurious CATEGORY keyword. Set :CATEGORY: property instead"))
426 (t (setf category-flag t
) nil
))))))
428 (defun org-lint-invalid-coderef-link (ast)
429 (let ((info (list :parse-tree ast
)))
430 (org-element-map ast
'link
432 (let ((ref (org-element-property :path link
)))
433 (and (equal (org-element-property :type link
) "coderef")
434 (not (ignore-errors (org-export-resolve-coderef ref info
)))
435 (list (org-element-property :begin link
)
436 (format "Unknown coderef \"%s\"" ref
))))))))
438 (defun org-lint-invalid-custom-id-link (ast)
439 (let ((info (list :parse-tree ast
)))
440 (org-element-map ast
'link
442 (and (equal (org-element-property :type link
) "custom-id")
443 (not (ignore-errors (org-export-resolve-id-link link info
)))
444 (list (org-element-property :begin link
)
445 (format "Unknown custom ID \"%s\""
446 (org-element-property :path link
))))))))
448 (defun org-lint-invalid-fuzzy-link (ast)
449 (let ((info (list :parse-tree ast
)))
450 (org-element-map ast
'link
452 (and (equal (org-element-property :type link
) "fuzzy")
453 (not (ignore-errors (org-export-resolve-fuzzy-link link info
)))
454 (list (org-element-property :begin link
)
455 (format "Unknown fuzzy location \"%s\""
456 (let ((path (org-element-property :path link
)))
457 (if (string-prefix-p "*" path
)
461 (defun org-lint-invalid-id-link (ast)
462 (org-element-map ast
'link
464 (let ((id (org-element-property :path link
)))
465 (and (equal (org-element-property :type link
) "id")
466 (not (org-id-find id
))
467 (list (org-element-property :begin link
)
468 (format "Unknown ID \"%s\"" id
)))))))
470 (defun org-lint-special-property-in-properties-drawer (ast)
471 (org-element-map ast
'node-property
473 (let ((key (org-element-property :key p
)))
474 (and (member-ignore-case key org-special-properties
)
475 (list (org-element-property :begin p
)
477 "Special property \"%s\" found in a properties drawer"
480 (defun org-lint-obsolete-properties-drawer (ast)
481 (org-element-map ast
'drawer
483 (when (equal (org-element-property :drawer-name d
) "PROPERTIES")
484 (let ((section (org-element-lineage d
'(section))))
485 (unless (org-element-map section
'property-drawer
#'identity nil t
)
486 (list (org-element-property :post-affiliated d
)
488 (goto-char (org-element-property :post-affiliated d
))
490 (or (org-at-heading-p) (org-at-planning-p)))
491 "Incorrect contents for PROPERTIES drawer"
492 "Incorrect location for PROPERTIES drawer"))))))))
494 (defun org-lint-link-to-local-file (ast)
495 (org-element-map ast
'link
497 (when (equal (org-element-property :type l
) "file")
498 (let ((file (org-link-unescape (org-element-property :path l
))))
499 (and (not (file-remote-p file
))
500 (not (file-exists-p file
))
501 (list (org-element-property :begin l
)
502 (format (if (org-element-lineage l
'(link))
503 "Link to non-existent image file \"%s\"\
505 "Link to non-existent local file \"%s\"")
508 (defun org-lint-non-existent-setupfile-parameter (ast)
509 (org-element-map ast
'keyword
511 (when (equal (org-element-property :key k
) "SETUPFILE")
512 (let ((file (org-remove-double-quotes
513 (org-element-property :value k
))))
514 (and (not (file-remote-p file
))
515 (not (file-exists-p file
))
516 (list (org-element-property :begin k
)
517 (format "Non-existent setup file \"%s\"" file
))))))))
519 (defun org-lint-wrong-include-link-parameter (ast)
520 (org-element-map ast
'keyword
522 (when (equal (org-element-property :key k
) "INCLUDE")
523 (let* ((value (org-element-property :value k
))
525 (and (string-match "^\\(\".+\"\\|\\S-+\\)[ \t]*" value
)
527 (org-remove-double-quotes (match-string 1 value
))))))
529 (list (org-element-property :post-affiliated k
)
530 "Missing location argument in INCLUDE keyword")
531 (let* ((file (org-string-nw-p
532 (if (string-match "::\\(.*\\)\\'" path
)
533 (substring path
0 (match-beginning 0))
535 (search (and (not (equal file path
))
536 (org-string-nw-p (match-string 1 path
)))))
538 (not (file-remote-p file
))
539 (not (file-exists-p file
)))
540 (list (org-element-property :post-affiliated k
)
541 "Non-existent file argument in INCLUDE keyword")
542 (let* ((visiting (if file
(find-buffer-visiting file
)
544 (buffer (or visiting
(find-file-noselect file
))))
546 (with-current-buffer buffer
550 (let ((org-link-search-inhibit-query t
))
551 (org-link-search search nil t
)))))
552 (list (org-element-property :post-affiliated k
)
554 "Invalid search part \"%s\" in INCLUDE keyword"
556 (unless visiting
(kill-buffer buffer
))))))))))))
558 (defun org-lint-unknown-options-item (ast)
559 (let ((allowed (delq nil
561 (mapcar (lambda (o) (nth 2 o
)) org-export-options-alist
)
564 (mapcar (lambda (o) (nth 2 o
))
565 (org-export-backend-options b
)))
566 org-export-registered-backends
))))
568 (org-element-map ast
'keyword
570 (when (string= (org-element-property :key k
) "OPTIONS")
571 (let ((value (org-element-property :value k
))
573 (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-*\\)[ \t]*"
576 (setf start
(match-end 0))
577 (let ((item (match-string 1 value
)))
578 (unless (member item allowed
)
579 (push (list (org-element-property :post-affiliated k
)
580 (format "Unknown OPTIONS item \"%s\"" item
))
584 (defun org-lint-invalid-macro-argument-and-template (ast)
585 (let ((extract-placeholders
589 (while (string-match "\\$\\([1-9][0-9]*\\)" template start
)
590 (setf start
(match-end 0))
591 (push (string-to-number (match-string 1 template
)) args
))
592 (sort (org-uniquify args
) #'<))))
594 ;; Check arguments for macro templates.
595 (org-element-map ast
'keyword
597 (when (string= (org-element-property :key k
) "MACRO")
598 (let* ((value (org-element-property :value k
))
599 (name (and (string-match "^\\S-+" value
)
600 (match-string 0 value
)))
602 (org-trim (substring value
(match-end 0))))))
605 (push (list (org-element-property :post-affiliated k
)
606 "Missing name in MACRO keyword")
608 ((not (org-string-nw-p template
))
609 (push (list (org-element-property :post-affiliated k
)
610 "Missing template in macro \"%s\"" name
)
613 (unless (let ((args (funcall extract-placeholders template
)))
614 (equal (number-sequence 1 (org-last args
)) args
))
615 (push (list (org-element-property :post-affiliated k
)
616 (format "Unused placeholders in macro \"%s\""
619 ;; Check arguments for macros.
620 (org-macro-initialize-templates)
621 (let ((templates (append
622 (mapcar (lambda (m) (cons m
"$1"))
623 '("author" "date" "email" "title" "results"))
624 org-macro-templates
)))
625 (org-element-map ast
'macro
627 (let* ((name (org-element-property :key macro
))
628 (template (cdr (assoc-string name templates t
))))
630 (push (list (org-element-property :begin macro
)
631 (format "Undefined macro \"%s\"" name
))
635 (funcall extract-placeholders template
))
636 (org-element-property :args macro
))))
638 (push (list (org-element-property :begin macro
)
639 (format "Unused argument%s in macro \"%s\": %s"
640 (if (> (length spurious-args
) 1) "s" "")
642 (mapconcat (lambda (a) (format "\"%s\"" a
))
648 (defun org-lint-undefined-footnote-reference (ast)
649 (let ((definitions (org-element-map ast
'footnote-definition
650 (lambda (f) (org-element-property :label f
)))))
651 (org-element-map ast
'footnote-reference
653 (let ((label (org-element-property :label f
)))
655 (not (member label definitions
))
656 (list (org-element-property :begin f
)
657 (format "Missing definition for footnote [%s]"
660 (defun org-lint-unreferenced-footnote-definition (ast)
661 (let ((references (org-element-map ast
'footnote-reference
662 (lambda (f) (org-element-property :label f
)))))
663 (org-element-map ast
'footnote-definition
665 (let ((label (org-element-property :label f
)))
667 (not (member label references
))
668 (list (org-element-property :post-affiliated f
)
669 (format "No reference for footnote definition [%s]"
672 (defun org-lint-colon-in-name (ast)
673 (org-element-map ast org-element-all-elements
675 (let ((name (org-element-property :name e
)))
677 (org-string-match-p ":" name
)
679 (goto-char (org-element-property :begin e
))
681 (format "^[ \t]*#\\+\\w+: +%s *$" (regexp-quote name
)))
684 "Name \"%s\" contains a colon; Babel cannot use it as input"
687 (defun org-lint-misplaced-planning-info (_)
688 (let ((case-fold-search t
)
690 (while (re-search-forward org-planning-line-re nil t
)
691 (unless (memq (org-element-type (org-element-at-point))
692 '(comment-block example-block export-block planning
693 src-block verse-block
))
694 (push (list (line-beginning-position) "Misplaced planning info line")
698 (defun org-lint-incomplete-drawer (_)
700 (while (re-search-forward org-drawer-regexp nil t
)
701 (let ((name (org-trim (org-match-string-no-properties 0)))
702 (element (org-element-at-point)))
703 (pcase (org-element-type element
)
704 ((or `drawer
`property-drawer
)
705 (goto-char (org-element-property :end element
))
707 ((or `comment-block
`example-block
`export-block
`src-block
711 (push (list (line-beginning-position)
712 (format "Possible incomplete drawer \"%s\"" name
))
716 (defun org-lint-indented-diary-sexp (_)
718 (while (re-search-forward "^[ \t]+%%(" nil t
)
719 (unless (memq (org-element-type (org-element-at-point))
720 '(comment-block diary-sexp example-block export-block
721 src-block verse-block
))
722 (push (list (line-beginning-position) "Possible indented diary-sexp")
726 (defun org-lint-invalid-block (_)
727 (let ((case-fold-search t
)
728 (regexp "^[ \t]*#\\+\\(BEGIN\\|END\\)\\(?::\\|_[^[:space:]]*\\)?[ \t]*")
730 (while (re-search-forward regexp nil t
)
731 (let ((name (org-trim (buffer-substring-no-properties
732 (line-beginning-position) (line-end-position)))))
734 ((and (string-prefix-p "END" (match-string 1) t
)
736 (push (list (line-beginning-position)
737 (format "Invalid block closing line \"%s\"" name
))
739 ((not (memq (org-element-type (org-element-at-point))
740 '(center-block comment-block dynamic-block example-block
741 export-block quote-block special-block
742 src-block verse-block
)))
743 (push (list (line-beginning-position)
744 (format "Possible incomplete block \"%s\""
749 (defun org-lint-invalid-keyword-syntax (_)
750 (let ((regexp "^[ \t]*#\\+\\([^[:space:]:]*\\)\\(?: \\|$\\)")
752 (format "[ \t]*#\\+%s\\(\\[.*\\]\\)?:\\(?: \\|$\\)"
753 (regexp-opt org-element-dual-keywords
)))
755 (while (re-search-forward regexp nil t
)
756 (let ((name (org-match-string-no-properties 1)))
757 (unless (or (string-prefix-p "BEGIN" name t
)
758 (string-prefix-p "END" name t
)
761 (let ((case-fold-search t
)) (looking-at exception-re
))))
762 (push (list (match-beginning 0)
763 (format "Possible missing colon in keyword \"%s\"" name
))
767 (defun org-lint-extraneous-element-in-footnote-section (ast)
768 (org-element-map ast
'headline
770 (and (org-element-property :footnote-section-p h
)
771 (org-element-map (org-element-contents h
)
774 (memq e
'(comment comment-block footnote-definition
775 property-drawer section
)))
776 org-element-all-elements
)
778 (not (and (eq (org-element-type e
) 'headline
)
779 (org-element-property :commentedp e
))))
780 nil t
'(footnote-definition property-drawer
))
781 (list (org-element-property :begin h
)
782 "Extraneous elements in footnote section")))))
784 (defun org-lint-quote-section (ast)
785 (org-element-map ast
'(headline inlinetask
)
787 (let ((title (org-element-property :raw-value h
)))
788 (and (or (string-prefix-p "QUOTE " title
)
789 (string-prefix-p (concat org-comment-string
" QUOTE ") title
))
790 (list (org-element-property :begin h
)
791 "Deprecated QUOTE section"))))))
793 (defun org-lint-wrong-header-argument (ast)
796 (lambda (datum language headers
)
798 ;; If LANGUAGE is specified, restrict allowed
799 ;; headers to both LANGUAGE-specific and default
800 ;; ones. Otherwise, accept headers from any loaded
803 org-babel-header-arg-names
806 (let ((v (intern (format "org-babel-header-args:%s" l
))))
807 (and (boundp v
) (mapcar #'car
(symbol-value v
)))))
808 (if language
(list language
)
809 (mapcar #'car org-babel-load-languages
))))))
810 (dolist (header headers
)
811 (let ((h (symbol-name (car header
)))
812 (p (or (org-element-property :post-affiliated datum
)
813 (org-element-property :begin datum
))))
815 ((not (string-prefix-p ":" h
))
818 (format "Missing colon in header argument \"%s\"" h
))
820 ((assoc-string (substring h
1) allowed
))
821 (t (push (list p
(format "Unknown header argument \"%s\"" h
))
823 (org-element-map ast
'(babel-call inline-babel-call inline-src-block keyword
824 node-property src-block
)
826 (pcase (org-element-type datum
)
827 ((or `babel-call
`inline-babel-call
)
831 (cl-mapcan #'org-babel-parse-header-arguments
833 (org-element-property :inside-header datum
)
834 (org-element-property :end-header datum
)))))
838 (org-element-property :language datum
)
839 (org-babel-parse-header-arguments
840 (org-element-property :parameters datum
))))
842 (when (string= (org-element-property :key datum
) "PROPERTY")
843 (let ((value (org-element-property :value datum
)))
844 (when (string-match "\\`header-args\\(?::\\(\\S-+\\)\\)?\\+? *"
848 (match-string 1 value
)
849 (org-babel-parse-header-arguments
850 (substring value
(match-end 0))))))))
852 (let ((key (org-element-property :key datum
)))
853 (when (let ((case-fold-search t
))
854 (string-match "\\`HEADER-ARGS\\(?::\\(\\S-+\\)\\)?\\+?"
859 (org-babel-parse-header-arguments
860 (org-element-property :value datum
))))))
864 (org-element-property :language datum
)
865 (cl-mapcan #'org-babel-parse-header-arguments
866 (cons (org-element-property :parameters datum
)
867 (org-element-property :header datum
))))))))
870 (defun org-lint-wrong-header-value (ast)
873 '(babel-call inline-babel-call inline-src-block src-block
)
875 (let* ((type (org-element-type datum
))
876 (language (org-element-property :language datum
))
877 (allowed-header-values
878 (append (and language
879 (let ((v (intern (concat "org-babel-header-args:"
881 (and (boundp v
) (symbol-value v
))))
882 org-babel-common-header-args-w-values
))
884 (org-babel-process-params
886 #'org-babel-merge-params
887 org-babel-default-header-args
889 (let ((v (intern (concat "org-babel-default-header-args:"
891 (and (boundp v
) (symbol-value v
))))
893 (list (and (memq type
'(babel-call inline-babel-call
))
894 org-babel-default-lob-header-args
))
895 (progn (goto-char (org-element-property :begin datum
))
896 (org-babel-params-from-properties language
))
898 (org-babel-parse-header-arguments
904 (cons (org-element-property :parameters datum
)
905 (org-element-property :header datum
))
908 (or (org-element-property :parameters datum
) ""))
911 (org-element-property :inside-header datum
)
913 (org-element-property :end-header datum
))))))))))))
914 (dolist (header datum-header-values
)
915 (let ((allowed-values
916 (cdr (assoc-string (substring (symbol-name (car header
)) 1)
917 allowed-header-values
))))
918 (unless (memq allowed-values
'(:any nil
))
919 (let ((values (cdr header
))
921 (dolist (v (if (stringp values
) (org-split-string values
)
923 (let ((valid-value nil
))
925 (dolist (group allowed-values
)
928 (if (stringp v
) #'assoc-string
#'assoc
)
930 (when (memq :any group
)
932 (push (cons group v
) groups-alist
)))
933 ((assq group groups-alist
)
936 (or (org-element-property :post-affiliated datum
)
937 (org-element-property :begin datum
))
939 "Forbidden combination in header \"%s\": %s, %s"
941 (cdr (assq group groups-alist
))
945 (t (push (cons group v
) groups-alist
)
946 (setf valid-value t
))))
950 (or (org-element-property :post-affiliated datum
)
951 (org-element-property :begin datum
))
952 (format "Unknown value \"%s\" for header \"%s\""
961 (defvar org-lint--report-mode-map
962 (let ((map (make-sparse-keymap)))
963 (set-keymap-parent map tabulated-list-mode-map
)
964 (define-key map
(kbd "RET") 'org-lint--jump-to-source
)
965 (define-key map
(kbd "TAB") 'org-lint--show-source
)
966 (define-key map
(kbd "C-j") 'org-lint--show-source
)
967 (define-key map
(kbd "h") 'org-lint--hide-checker
)
968 (define-key map
(kbd "i") 'org-lint--ignore-checker
)
970 "Local keymap for `org-lint--report-mode' buffers.")
972 (define-derived-mode org-lint--report-mode tabulated-list-mode
"OrgLint"
973 "Major mode used to display reports emitted during linting.
974 \\{org-lint--report-mode-map}"
975 (setf tabulated-list-format
978 (< (string-to-number (aref (cadr a
) 0))
979 (string-to-number (aref (cadr b
) 0))))
983 (tabulated-list-init-header))
985 (defun org-lint--generate-reports (buffer checkers
)
986 "Generate linting report for BUFFER.
988 CHECKERS is the list of checkers used.
990 Return an alist (ID [LINE TRUST DESCRIPTION CHECKER]), suitable
991 for `tabulated-list-printer'."
992 (with-current-buffer buffer
994 (goto-char (point-min))
995 (let ((ast (org-element-parse-buffer))
999 ;; Insert unique ID for each report. Replace buffer positions
1000 ;; with line numbers.
1008 (goto-char (car report
))
1010 (prog1 (number-to-string
1011 (incf last-line
(count-lines last-pos
(point))))
1012 (setf last-pos
(point))))
1014 ;; Insert trust level in generated reports. Also sort them
1015 ;; by buffer position in order to optimize lines computation.
1018 (let ((trust (symbol-name (org-lint-checker-trust c
))))
1021 (list (car report
) trust
(nth 1 report
) c
))
1024 (intern (format "org-lint-%s"
1025 (org-lint-checker-name c
)))
1028 #'car-less-than-car
))))))
1030 (defvar-local org-lint--source-buffer nil
1031 "Source buffer associated to current report buffer.")
1033 (defvar-local org-lint--local-checkers nil
1034 "List of checkers used to build current report.")
1036 (defun org-lint--refresh-reports ()
1037 (setq tabulated-list-entries
1038 (org-lint--generate-reports org-lint--source-buffer
1039 org-lint--local-checkers
))
1040 (tabulated-list-print))
1042 (defun org-lint--current-line ()
1043 "Return current report line, as a number."
1044 (string-to-number (aref (tabulated-list-get-entry) 0)))
1046 (defun org-lint--current-checker (&optional entry
)
1047 "Return current report checker.
1048 When optional argument ENTRY is non-nil, use this entry instead
1050 (aref (if entry
(nth 1 entry
) (tabulated-list-get-entry)) 3))
1052 (defun org-lint--display-reports (source checkers
)
1053 "Display linting reports for buffer SOURCE.
1054 CHECKERS is the list of checkers used."
1055 (let ((buffer (get-buffer-create "*Org Lint*")))
1056 (with-current-buffer buffer
1057 (org-lint--report-mode)
1058 (setf org-lint--source-buffer source
)
1059 (setf org-lint--local-checkers checkers
)
1060 (org-lint--refresh-reports)
1061 (tabulated-list-print)
1062 (add-hook 'tabulated-list-revert-hook
#'org-lint--refresh-reports nil t
))
1063 (pop-to-buffer buffer
)))
1065 (defun org-lint--jump-to-source ()
1066 "Move to source line that generated the report at point."
1068 (let ((l (org-lint--current-line)))
1069 (switch-to-buffer-other-window org-lint--source-buffer
)
1071 (org-show-set-visibility 'local
)
1074 (defun org-lint--show-source ()
1075 "Show source line that generated the report at point."
1077 (let ((buffer (current-buffer)))
1078 (org-lint--jump-to-source)
1079 (switch-to-buffer-other-window buffer
)))
1081 (defun org-lint--hide-checker ()
1082 "Hide all reports from checker that generated the report at point."
1084 (let ((c (org-lint--current-checker)))
1085 (setf tabulated-list-entries
1086 (org-remove-if (lambda (e) (equal c
(org-lint--current-checker e
)))
1087 tabulated-list-entries
))
1088 (tabulated-list-print)))
1090 (defun org-lint--ignore-checker ()
1091 "Ignore all reports from checker that generated the report at point.
1092 Checker will also be ignored in all subsequent reports."
1094 (setf org-lint--local-checkers
1095 (remove (org-lint--current-checker) org-lint--local-checkers
))
1096 (org-lint--hide-checker))
1102 (defun org-lint (&optional arg
)
1103 "Check current Org buffer for syntax mistakes.
1105 By default, run all checkers. With a single prefix ARG \
1106 \\[universal-argument],
1107 select one category of checkers only. With a double prefix
1108 \\[universal-argument] \\[universal-argument], select one precise \
1109 checker by its name.
1111 ARG can also be a list of checker names, as symbols, to run."
1113 (unless (derived-mode-p 'org-mode
) (user-error "Not in an Org buffer"))
1114 (when (org-called-interactively-p)
1115 (message "Org linting process starting..."))
1118 (`nil org-lint--checkers
)
1122 "Checker category: "
1123 (mapcar #'org-lint-checker-categories org-lint--checkers
)
1127 (assoc-string (org-lint-checker-categories c
) category
))
1128 org-lint--checkers
)))
1131 (let ((name (completing-read
1133 (mapcar #'org-lint-checker-name org-lint--checkers
)
1136 (dolist (c org-lint--checkers
)
1137 (when (string= (org-lint-checker-name c
) name
)
1138 (throw 'exit c
)))))))
1140 (org-remove-if-not (lambda (c) (memq (org-lint-checker-name c
) arg
))
1141 org-lint--checkers
))
1142 (_ (user-error "Invalid argument `%S' for `org-lint'" arg
)))))
1143 (if (not (org-called-interactively-p))
1144 (org-lint--generate-reports (current-buffer) checkers
)
1145 (org-lint--display-reports (current-buffer) checkers
)
1146 (message "Org linting process completed"))))
1150 ;;; org-lint.el ends here