org-lint: Remove useless checker
[org-mode/org-tableheadings.git] / lisp / org-lint.el
blobf7a45a834fa2f7c4c36cf4349a7a88af6524f510
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/>.
23 ;;; Commentary:
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
97 ;; - invalid blocks
98 ;; - misplaced planning info line
99 ;; - incomplete drawers
100 ;; - indented diary-sexps
101 ;; - obsolete QUOTE section
102 ;; - obsolete "file+application" link
105 ;;; Code:
107 (require 'cl-lib)
108 (require 'org-element)
109 (require 'org-macro)
110 (require 'ox)
111 (require 'ob)
114 ;;; Checkers
116 (cl-defstruct (org-lint-checker (:copier nil))
117 (name 'missing-checker-name)
118 (description "")
119 (categories '(default))
120 (trust 'high)) ; `low' or `high'
122 (defun org-lint-missing-checker-name (_)
123 (error
124 "`A checker has no `:name' property. Please verify `org-lint--checkers'"))
126 (defconst org-lint--checkers
127 (list
128 (make-org-lint-checker
129 :name 'duplicate-custom-id
130 :description "Report duplicates CUSTOM_ID properties"
131 :categories '(link))
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"
139 :categories '(link))
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"
147 :trust 'low)
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)
156 :trust 'low)
157 (make-org-lint-checker
158 :name 'deprecated-header-syntax
159 :description "Report deprecated Babel header syntax"
160 :categories '(obsolete babel)
161 :trust 'low)
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
175 :name 'colon-in-name
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"
185 :categories '(babel)
186 :trust 'low)
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"
194 :categories '(link))
195 (make-org-lint-checker
196 :name 'invalid-custom-id-link
197 :description "Report \"custom-id\" links with unknown destination"
198 :categories '(link))
199 (make-org-lint-checker
200 :name 'invalid-fuzzy-link
201 :description "Report \"fuzzy\" links with unknown destination"
202 :categories '(link))
203 (make-org-lint-checker
204 :name 'invalid-id-link
205 :description "Report \"id\" links with unknown destination"
206 :categories '(link))
207 (make-org-lint-checker
208 :name 'link-to-local-file
209 :description "Report links to non-existent local files"
210 :categories '(link)
211 :trust 'low)
212 (make-org-lint-checker
213 :name 'non-existent-setupfile-parameter
214 :description "Report SETUPFILE keywords with non-existent file parameter"
215 :trust 'low)
216 (make-org-lint-checker
217 :name 'wrong-include-link-parameter
218 :description "Report INCLUDE keywords with misleading link parameter"
219 :categories '(export)
220 :trust 'low)
221 (make-org-lint-checker
222 :name 'obsolete-include-markup
223 :description "Report obsolete markup in INCLUDE keyword"
224 :categories '(obsolete export)
225 :trust 'low)
226 (make-org-lint-checker
227 :name 'unknown-options-item
228 :description "Report unknown items in OPTIONS keyword"
229 :categories '(export)
230 :trust 'low)
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)
235 :trust 'low)
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"
263 :trust 'low)
264 (make-org-lint-checker
265 :name 'invalid-block
266 :description "Report invalid blocks"
267 :trust 'low)
268 (make-org-lint-checker
269 :name 'misplaced-planning-info
270 :description "Report misplaced planning info line"
271 :trust 'low)
272 (make-org-lint-checker
273 :name 'incomplete-drawer
274 :description "Report probable incomplete drawers"
275 :trust 'low)
276 (make-org-lint-checker
277 :name 'indented-diary-sexp
278 :description "Report probable indented diary-sexps"
279 :trust 'low)
280 (make-org-lint-checker
281 :name 'quote-section
282 :description "Report obsolete QUOTE section"
283 :categories '(obsolete)
284 :trust 'low)
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
297 with `equal'.
299 EXTRACT-POSITION is a function returning position for the report.
300 It is called with two arguments, the object or element, and the
301 key.
303 BUILD-MESSAGE is a function creating the report message. It is
304 called with one argument, the key used for comparison."
305 (let* (keys
306 originals
307 reports
308 (make-report
309 (lambda (position value)
310 (push (list position (funcall build-message value)) reports))))
311 (org-element-map ast type
312 (lambda (datum)
313 (let ((key (funcall extract-key datum)))
314 (cond
315 ((not key))
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
324 'node-property
325 (lambda (property)
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))
339 (lambda (datum name)
340 (goto-char (org-element-property :begin datum))
341 (re-search-forward
342 (format "^[ \t]*#\\+[A-Za-z]+: +%s *$" (regexp-quote name)))
343 (match-beginning 0))
344 (lambda (key) (format "Duplicate NAME \"%s\"" key))))
346 (defun org-lint-duplicate-target (ast)
347 (org-lint--collect-duplicates
349 'target
350 (lambda (target) (split-string (org-element-property :value target)))
351 (lambda (target _) (org-element-property :begin target))
352 (lambda (key)
353 (format "Duplicate target <<%s>>" (mapconcat #'identity key " ")))))
355 (defun org-lint-duplicate-footnote-definition (ast)
356 (org-lint--collect-duplicates
358 'footnote-definition
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")
368 :test #'equal)))
369 (org-element-map ast 'keyword
370 (lambda (k)
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")
382 t)))
383 reports)
384 (while (re-search-forward regexp nil t)
385 (let ((key (upcase (match-string-no-properties 1))))
386 (when (< (point)
387 (org-element-property :post-affiliated (org-element-at-point)))
388 (push
389 (list (line-beginning-position)
390 (format
391 "Obsolete affiliated keyword: \"%s\". Use \"%s\" instead"
393 (pcase key
394 ("HEADERS" "HEADER")
395 ("RESULT" "RESULTS")
396 (_ "NAME"))))
397 reports))))
398 reports))
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
404 (lambda (b)
405 (let ((type (org-element-property :type b)))
406 (when (member-ignore-case type deprecated)
407 (list
408 (org-element-property :post-affiliated b)
409 (format
410 "Deprecated syntax for export block. Use \"BEGIN_EXPORT %s\" \
411 instead"
412 type))))))))
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))
418 (deprecated-re
419 (format "\\`%s[ \t]" (regexp-opt deprecated-babel-properties t))))
420 (org-element-map ast '(keyword node-property)
421 (lambda (datum)
422 (let ((key (org-element-property :key datum)))
423 (pcase (org-element-type datum)
424 (`keyword
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))))))
432 (`node-property
433 (and (member-ignore-case key deprecated-babel-properties)
434 (list
435 (org-element-property :begin datum)
436 (format "Deprecated syntax for \"%s\". \
437 Use :header-args: instead"
438 key))))))))))
440 (defun org-lint-missing-language-in-src-block (ast)
441 (org-element-map ast 'src-block
442 (lambda (b)
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
449 (lambda (b)
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
456 (lambda (b)
457 (cond
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)))
463 (list
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
469 (let (category-flag)
470 (lambda (k)
471 (cond
472 ((not (string= (org-element-property :key k) "CATEGORY")) nil)
473 (category-flag
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
481 (lambda (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
491 (lambda (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
501 (lambda (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)
508 (substring path 1)
509 path)))))))))
511 (defun org-lint-invalid-id-link (ast)
512 (org-element-map ast 'link
513 (lambda (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
522 (lambda (p)
523 (let ((key (org-element-property :key p)))
524 (and (member-ignore-case key org-special-properties)
525 (list (org-element-property :begin p)
526 (format
527 "Special property \"%s\" found in a properties drawer"
528 key)))))))
530 (defun org-lint-obsolete-properties-drawer (ast)
531 (org-element-map ast 'drawer
532 (lambda (d)
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)
537 (if (save-excursion
538 (goto-char (org-element-property :post-affiliated d))
539 (forward-line -1)
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
546 (lambda (p)
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
556 (lambda (l)
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\"\
564 in link description"
565 "Link to non-existent local file \"%s\"")
566 file))))))))
568 (defun org-lint-non-existent-setupfile-parameter (ast)
569 (org-element-map ast 'keyword
570 (lambda (k)
571 (when (equal (org-element-property :key k) "SETUPFILE")
572 (let ((file (org-unbracket-string
573 "\"" "\""
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
582 (lambda (k)
583 (when (equal (org-element-property :key k) "INCLUDE")
584 (let* ((value (org-element-property :value k))
585 (path
586 (and (string-match "^\\(\".+\"\\|\\S-+\\)[ \t]*" value)
587 (save-match-data
588 (org-unbracket-string "\"" "\"" (match-string 1 value))))))
589 (if (not path)
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))
595 path)))
596 (search (and (not (equal file path))
597 (org-string-nw-p (match-string 1 path)))))
598 (if (and file
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)
604 (current-buffer)))
605 (buffer (or visiting (find-file-noselect file))))
606 (unwind-protect
607 (with-current-buffer buffer
608 (when (and search
609 (not
610 (ignore-errors
611 (let ((org-link-search-inhibit-query t))
612 (org-link-search search nil t)))))
613 (list (org-element-property :post-affiliated k)
614 (format
615 "Invalid search part \"%s\" in INCLUDE keyword"
616 search))))
617 (unless visiting (kill-buffer buffer))))))))))))
619 (defun org-lint-obsolete-include-markup (ast)
620 (let ((regexp (format "\\`\\(?:\".+\"\\|\\S-+\\)[ \t]+%s"
621 (regexp-opt
622 '("ASCII" "BEAMER" "HTML" "LATEX" "MAN" "MARKDOWN" "MD"
623 "ODT" "ORG" "TEXINFO")
624 t))))
625 (org-element-map ast 'keyword
626 (lambda (k)
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"
635 markup
636 markup))))))))))
638 (defun org-lint-unknown-options-item (ast)
639 (let ((allowed (delq nil
640 (append
641 (mapcar (lambda (o) (nth 2 o)) org-export-options-alist)
642 (cl-mapcan
643 (lambda (b)
644 (mapcar (lambda (o) (nth 2 o))
645 (org-export-backend-options b)))
646 org-export-registered-backends))))
647 reports)
648 (org-element-map ast 'keyword
649 (lambda (k)
650 (when (string= (org-element-property :key k) "OPTIONS")
651 (let ((value (org-element-property :value k))
652 (start 0))
653 (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-*\\)[ \t]*"
654 value
655 start)
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))
661 reports))))))))
662 reports))
664 (defun org-lint-invalid-macro-argument-and-template (ast)
665 (let ((extract-placeholders
666 (lambda (template)
667 (let ((start 0)
668 args)
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) #'<))))
673 reports)
674 ;; Check arguments for macro templates.
675 (org-element-map ast 'keyword
676 (lambda (k)
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)))
681 (template (and name
682 (org-trim (substring value (match-end 0))))))
683 (cond
684 ((not name)
685 (push (list (org-element-property :post-affiliated k)
686 "Missing name in MACRO keyword")
687 reports))
688 ((not (org-string-nw-p template))
689 (push (list (org-element-property :post-affiliated k)
690 "Missing template in macro \"%s\"" name)
691 reports))
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\""
697 name))
698 reports))))))))
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
706 (lambda (macro)
707 (let* ((name (org-element-property :key macro))
708 (template (cdr (assoc-string name templates t))))
709 (if (not template)
710 (push (list (org-element-property :begin macro)
711 (format "Undefined macro \"%s\"" name))
712 reports)
713 (let ((arg-numbers (funcall extract-placeholders template)))
714 (when arg-numbers
715 (let ((spurious-args
716 (nthcdr (apply #'max arg-numbers)
717 (org-element-property :args macro))))
718 (when spurious-args
719 (push
720 (list (org-element-property :begin macro)
721 (format "Unused argument%s in macro \"%s\": %s"
722 (if (> (length spurious-args) 1) "s" "")
723 name
724 (mapconcat (lambda (a) (format "\"%s\"" a))
725 spurious-args
726 ", ")))
727 reports))))))))))
728 reports))
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
734 (lambda (f)
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]"
740 label))))))))
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
746 (lambda (f)
747 (let ((label (org-element-property :label f)))
748 (and label
749 (not (member label references))
750 (list (org-element-property :post-affiliated f)
751 (format "No reference for footnote definition [%s]"
752 label))))))))
754 (defun org-lint-colon-in-name (ast)
755 (org-element-map ast org-element-all-elements
756 (lambda (e)
757 (let ((name (org-element-property :name e)))
758 (and name
759 (string-match-p ":" name)
760 (list (progn
761 (goto-char (org-element-property :begin e))
762 (re-search-forward
763 (format "^[ \t]*#\\+\\w+: +%s *$" (regexp-quote name)))
764 (match-beginning 0))
765 (format
766 "Name \"%s\" contains a colon; Babel cannot use it as input"
767 name)))))))
769 (defun org-lint-misplaced-planning-info (_)
770 (let ((case-fold-search t)
771 reports)
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")
777 reports)))
778 reports))
780 (defun org-lint-incomplete-drawer (_)
781 (let (reports)
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))
788 nil)
789 ((or `comment-block `example-block `export-block `src-block
790 `verse-block)
791 nil)
793 (push (list (line-beginning-position)
794 (format "Possible incomplete drawer \"%s\"" name))
795 reports)))))
796 reports))
798 (defun org-lint-indented-diary-sexp (_)
799 (let (reports)
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")
805 reports)))
806 reports))
808 (defun org-lint-invalid-block (_)
809 (let ((case-fold-search t)
810 (regexp "^[ \t]*#\\+\\(BEGIN\\|END\\)\\(?::\\|_[^[:space:]]*\\)?[ \t]*")
811 reports)
812 (while (re-search-forward regexp nil t)
813 (let ((name (org-trim (buffer-substring-no-properties
814 (line-beginning-position) (line-end-position)))))
815 (cond
816 ((and (string-prefix-p "END" (match-string 1) t)
817 (not (eolp)))
818 (push (list (line-beginning-position)
819 (format "Invalid block closing line \"%s\"" name))
820 reports))
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\""
827 name))
828 reports)))))
829 reports))
831 (defun org-lint-invalid-keyword-syntax (_)
832 (let ((regexp "^[ \t]*#\\+\\([^[:space:]:]*\\)\\(?: \\|$\\)")
833 (exception-re
834 (format "[ \t]*#\\+%s\\(\\[.*\\]\\)?:\\(?: \\|$\\)"
835 (regexp-opt org-element-dual-keywords)))
836 reports)
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)
841 (save-excursion
842 (beginning-of-line)
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))
846 reports))))
847 reports))
849 (defun org-lint-extraneous-element-in-footnote-section (ast)
850 (org-element-map ast 'headline
851 (lambda (h)
852 (and (org-element-property :footnote-section-p h)
853 (org-element-map (org-element-contents h)
854 (cl-remove-if
855 (lambda (e)
856 (memq e '(comment comment-block footnote-definition
857 property-drawer section)))
858 org-element-all-elements)
859 (lambda (e)
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)
868 (lambda (h)
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
877 (lambda (l)
878 (let ((app (org-element-property :application l)))
879 (and app
880 (list (org-element-property :begin l)
881 (format "Deprecated \"file+%s\" link type" app)))))))
883 (defun org-lint-wrong-header-argument (ast)
884 (let* ((reports)
885 (verify
886 (lambda (datum language headers)
887 (let ((allowed
888 ;; If LANGUAGE is specified, restrict allowed
889 ;; headers to both LANGUAGE-specific and default
890 ;; ones. Otherwise, accept headers from any loaded
891 ;; language.
892 (append
893 org-babel-header-arg-names
894 (cl-mapcan
895 (lambda (l)
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))))
904 (cond
905 ((not (string-prefix-p ":" h))
906 (push
907 (list p
908 (format "Missing colon in header argument \"%s\"" h))
909 reports))
910 ((assoc-string (substring h 1) allowed))
911 (t (push (list p (format "Unknown header argument \"%s\"" h))
912 reports)))))))))
913 (org-element-map ast '(babel-call inline-babel-call inline-src-block keyword
914 node-property src-block)
915 (lambda (datum)
916 (pcase (org-element-type datum)
917 ((or `babel-call `inline-babel-call)
918 (funcall verify
919 datum
921 (cl-mapcan #'org-babel-parse-header-arguments
922 (list
923 (org-element-property :inside-header datum)
924 (org-element-property :end-header datum)))))
925 (`inline-src-block
926 (funcall verify
927 datum
928 (org-element-property :language datum)
929 (org-babel-parse-header-arguments
930 (org-element-property :parameters datum))))
931 (`keyword
932 (when (string= (org-element-property :key datum) "PROPERTY")
933 (let ((value (org-element-property :value datum)))
934 (when (string-match "\\`header-args\\(?::\\(\\S-+\\)\\)?\\+? *"
935 value)
936 (funcall verify
937 datum
938 (match-string 1 value)
939 (org-babel-parse-header-arguments
940 (substring value (match-end 0))))))))
941 (`node-property
942 (let ((key (org-element-property :key datum)))
943 (when (let ((case-fold-search t))
944 (string-match "\\`HEADER-ARGS\\(?::\\(\\S-+\\)\\)?\\+?"
945 key))
946 (funcall verify
947 datum
948 (match-string 1 key)
949 (org-babel-parse-header-arguments
950 (org-element-property :value datum))))))
951 (`src-block
952 (funcall verify
953 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))))))))
958 reports))
960 (defun org-lint-wrong-header-value (ast)
961 (let (reports)
962 (org-element-map ast
963 '(babel-call inline-babel-call inline-src-block src-block)
964 (lambda (datum)
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:"
970 language))))
971 (and (boundp v) (symbol-value v))))
972 org-babel-common-header-args-w-values))
973 (datum-header-values
974 (org-babel-parse-header-arguments
975 (org-trim
976 (pcase type
977 (`src-block
978 (mapconcat
979 #'identity
980 (cons (org-element-property :parameters datum)
981 (org-element-property :header datum))
982 " "))
983 (`inline-src-block
984 (or (org-element-property :parameters datum) ""))
986 (concat
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))
996 groups-alist)
997 (dolist (v (if (stringp values) (split-string values)
998 (list values)))
999 (let ((valid-value nil))
1000 (catch 'exit
1001 (dolist (group allowed-values)
1002 (cond
1003 ((not (funcall
1004 (if (stringp v) #'assoc-string #'assoc)
1005 v group))
1006 (when (memq :any group)
1007 (setf valid-value t)
1008 (push (cons group v) groups-alist)))
1009 ((assq group groups-alist)
1010 (push
1011 (list
1012 (or (org-element-property :post-affiliated datum)
1013 (org-element-property :begin datum))
1014 (format
1015 "Forbidden combination in header \"%s\": %s, %s"
1016 (car header)
1017 (cdr (assq group groups-alist))
1019 reports)
1020 (throw 'exit nil))
1021 (t (push (cons group v) groups-alist)
1022 (setf valid-value t))))
1023 (unless valid-value
1024 (push
1025 (list
1026 (or (org-element-property :post-affiliated datum)
1027 (org-element-property :begin datum))
1028 (format "Unknown value \"%s\" for header \"%s\""
1030 (car header)))
1031 reports))))))))))))
1032 reports))
1036 ;;; Reports UI
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)
1046 map)
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
1053 `[("Line" 6
1054 (lambda (a b)
1055 (< (string-to-number (aref (cadr a) 0))
1056 (string-to-number (aref (cadr b) 0))))
1057 :right-align t)
1058 ("Trust" 5 t)
1059 ("Warning" 0 t)])
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
1070 (save-excursion
1071 (goto-char (point-min))
1072 (let ((ast (org-element-parse-buffer))
1073 (id 0)
1074 (last-line 1)
1075 (last-pos 1))
1076 ;; Insert unique ID for each report. Replace buffer positions
1077 ;; with line numbers.
1078 (mapcar
1079 (lambda (report)
1080 (list
1081 (cl-incf id)
1082 (apply #'vector
1083 (cons
1084 (progn
1085 (goto-char (car report))
1086 (beginning-of-line)
1087 (prog1 (number-to-string
1088 (cl-incf last-line
1089 (count-lines last-pos (point))))
1090 (setf last-pos (point))))
1091 (cdr report)))))
1092 ;; Insert trust level in generated reports. Also sort them
1093 ;; by buffer position in order to optimize lines computation.
1094 (sort (cl-mapcan
1095 (lambda (c)
1096 (let ((trust (symbol-name (org-lint-checker-trust c))))
1097 (mapcar
1098 (lambda (report)
1099 (list (car report) trust (nth 1 report) c))
1100 (save-excursion
1101 (funcall
1102 (intern (format "org-lint-%s"
1103 (org-lint-checker-name c)))
1104 ast)))))
1105 checkers)
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
1127 of current one."
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."
1145 (interactive)
1146 (let ((l (org-lint--current-line)))
1147 (switch-to-buffer-other-window org-lint--source-buffer)
1148 (org-goto-line l)
1149 (org-show-set-visibility 'local)
1150 (recenter)))
1152 (defun org-lint--show-source ()
1153 "Show source line that generated the report at point."
1154 (interactive)
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."
1161 (interactive)
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."
1171 (interactive)
1172 (setf org-lint--local-checkers
1173 (remove (org-lint--current-checker) org-lint--local-checkers))
1174 (org-lint--hide-checker))
1177 ;;; Public function
1179 ;;;###autoload
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, \
1184 select one
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."
1190 (interactive "P")
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..."))
1194 (let ((checkers
1195 (pcase arg
1196 (`nil org-lint--checkers)
1197 (`(4)
1198 (let ((category
1199 (completing-read
1200 "Checker category: "
1201 (mapcar #'org-lint-checker-categories org-lint--checkers)
1202 nil t)))
1203 (cl-remove-if-not
1204 (lambda (c)
1205 (assoc-string (org-lint-checker-categories c) category))
1206 org-lint--checkers)))
1207 (`(16)
1208 (list
1209 (let ((name (completing-read
1210 "Checker name: "
1211 (mapcar #'org-lint-checker-name org-lint--checkers)
1212 nil t)))
1213 (catch 'exit
1214 (dolist (c org-lint--checkers)
1215 (when (string= (org-lint-checker-name c) name)
1216 (throw 'exit c)))))))
1217 ((pred consp)
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"))))
1227 (provide 'org-lint)
1228 ;;; org-lint.el ends here