Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-lint.el
blob744be3beccb2678b03e13e7f52555eb52f058c2c
1 ;;; org-lint.el --- Linting for Org documents -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2016 Free Software Foundation
5 ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements linting for Org syntax. The sole public
24 ;; function is `org-lint', which see.
26 ;; Internally, the library defines a new structure:
27 ;; `org-lint-checker', with the following slots:
29 ;; - NAME: Unique check identifier, as a non-nil symbol that doesn't
30 ;; start with an hyphen.
32 ;; The check is done calling the function `org-lint-NAME' with one
33 ;; mandatory argument, the parse tree describing the current Org
34 ;; buffer. Such function calls are wrapped within
35 ;; a `save-excursion' and point is always at `point-min'. Its
36 ;; return value has to be an alist (POSITION MESSAGE) when
37 ;; POSITION refer to the buffer position of the error, as an
38 ;; integer, and MESSAGE is a string describing the error.
40 ;; - DESCRIPTION: Summary about the check, as a string.
42 ;; - CATEGORIES: Categories relative to the check, as a list of
43 ;; symbol. They are used for filtering when calling `org-lint'.
44 ;; Checkers not explicitly associated to a category are collected
45 ;; in the `default' one.
47 ;; - TRUST: The trust level one can have in the check. It is either
48 ;; `low' or `high', depending on the heuristics implemented and
49 ;; the nature of the check. This has an indicative value only and
50 ;; is displayed along reports.
52 ;; All checks have to be listed in `org-lint--checkers'.
54 ;; Results are displayed in a special "*Org Lint*" buffer with
55 ;; a dedicated major mode, derived from `tabulated-list-mode'.
57 ;; In addition to the usual key-bindings inherited from it, "C-j" and
58 ;; "TAB" display problematic line reported under point whereas "RET"
59 ;; jumps to it. Also, "h" hides all reports similar to the current
60 ;; one. Additionally, "i" removes them from subsequent reports.
62 ;; Checks currently implemented are:
64 ;; - duplicate CUSTOM_ID properties
65 ;; - duplicate NAME values
66 ;; - duplicate targets
67 ;; - duplicate footnote definitions
68 ;; - orphaned affiliated keywords
69 ;; - obsolete affiliated keywords
70 ;; - missing language in src blocks
71 ;; - missing back-end in export blocks
72 ;; - invalid Babel call blocks
73 ;; - NAME values with a colon
74 ;; - deprecated export block syntax
75 ;; - deprecated Babel header properties
76 ;; - wrong header arguments in src blocks
77 ;; - misuse of CATEGORY keyword
78 ;; - "coderef" links with unknown destination
79 ;; - "custom-id" links with unknown destination
80 ;; - "fuzzy" links with unknown destination
81 ;; - "id" links with unknown destination
82 ;; - links to non-existent local files
83 ;; - SETUPFILE keywords with non-existent file parameter
84 ;; - INCLUDE keywords with wrong link parameter
85 ;; - obsolete markup in INCLUDE keyword
86 ;; - unknown items in OPTIONS keyword
87 ;; - spurious macro arguments or invalid macro templates
88 ;; - special properties in properties drawer
89 ;; - obsolete syntax for PROPERTIES drawers
90 ;; - missing definition for footnote references
91 ;; - missing reference for footnote definitions
92 ;; - non-footnote definitions in footnote section
93 ;; - probable invalid keywords
94 ;; - invalid blocks
95 ;; - misplaced planning info line
96 ;; - incomplete drawers
97 ;; - indented diary-sexps
98 ;; - obsolete QUOTE section
101 ;;; Code:
103 (require 'cl-lib)
104 (require 'org-element)
105 (require 'org-macro)
106 (require 'ox)
107 (require 'ob)
110 ;;; Checkers
112 (cl-defstruct (org-lint-checker (:copier nil))
113 (name 'missing-checker-name)
114 (description "")
115 (categories '(default))
116 (trust 'high)) ; `low' or `high'
118 (defun org-lint-missing-checker-name (_)
119 (error
120 "`A checker has no `:name' property. Please verify `org-lint--checkers'"))
122 (defconst org-lint--checkers
123 (list
124 (make-org-lint-checker
125 :name 'duplicate-custom-id
126 :description "Report duplicates CUSTOM_ID properties"
127 :categories '(link))
128 (make-org-lint-checker
129 :name 'duplicate-name
130 :description "Report duplicate NAME values"
131 :categories '(babel link))
132 (make-org-lint-checker
133 :name 'duplicate-target
134 :description "Report duplicate targets"
135 :categories '(link))
136 (make-org-lint-checker
137 :name 'duplicate-footnote-definition
138 :description "Report duplicate footnote definitions"
139 :categories '(footnote))
140 (make-org-lint-checker
141 :name 'orphaned-affiliated-keywords
142 :description "Report orphaned affiliated keywords"
143 :trust 'low)
144 (make-org-lint-checker
145 :name 'obsolete-affiliated-keywords
146 :description "Report obsolete affiliated keywords"
147 :categories '(obsolete))
148 (make-org-lint-checker
149 :name 'deprecated-export-blocks
150 :description "Report deprecated export block syntax"
151 :categories '(obsolete export)
152 :trust 'low)
153 (make-org-lint-checker
154 :name 'deprecated-header-syntax
155 :description "Report deprecated Babel header syntax"
156 :categories '(obsolete babel)
157 :trust 'low)
158 (make-org-lint-checker
159 :name 'missing-language-in-src-block
160 :description "Report missing language in src blocks"
161 :categories '(babel))
162 (make-org-lint-checker
163 :name 'missing-backend-in-export-block
164 :description "Report missing back-end in export blocks"
165 :categories '(export))
166 (make-org-lint-checker
167 :name 'invalid-babel-call-block
168 :description "Report invalid Babel call blocks"
169 :categories '(babel))
170 (make-org-lint-checker
171 :name 'colon-in-name
172 :description "Report NAME values with a colon"
173 :categories '(babel))
174 (make-org-lint-checker
175 :name 'wrong-header-argument
176 :description "Report wrong babel headers"
177 :categories '(babel))
178 (make-org-lint-checker
179 :name 'wrong-header-value
180 :description "Report invalid value in babel headers"
181 :categories '(babel)
182 :trust 'low)
183 (make-org-lint-checker
184 :name 'deprecated-category-setup
185 :description "Report misuse of CATEGORY keyword"
186 :categories '(obsolete))
187 (make-org-lint-checker
188 :name 'invalid-coderef-link
189 :description "Report \"coderef\" links with unknown destination"
190 :categories '(link))
191 (make-org-lint-checker
192 :name 'invalid-custom-id-link
193 :description "Report \"custom-id\" links with unknown destination"
194 :categories '(link))
195 (make-org-lint-checker
196 :name 'invalid-fuzzy-link
197 :description "Report \"fuzzy\" links with unknown destination"
198 :categories '(link))
199 (make-org-lint-checker
200 :name 'invalid-id-link
201 :description "Report \"id\" links with unknown destination"
202 :categories '(link))
203 (make-org-lint-checker
204 :name 'link-to-local-file
205 :description "Report links to non-existent local files"
206 :categories '(link)
207 :trust 'low)
208 (make-org-lint-checker
209 :name 'non-existent-setupfile-parameter
210 :description "Report SETUPFILE keywords with non-existent file parameter"
211 :trust 'low)
212 (make-org-lint-checker
213 :name 'wrong-include-link-parameter
214 :description "Report INCLUDE keywords with misleading link parameter"
215 :categories '(export)
216 :trust 'low)
217 (make-org-lint-checker
218 :name 'obsolete-include-markup
219 :description "Report obsolete markup in INCLUDE keyword"
220 :categories '(obsolete export)
221 :trust 'low)
222 (make-org-lint-checker
223 :name 'unknown-options-item
224 :description "Report unknown items in OPTIONS keyword"
225 :categories '(export)
226 :trust 'low)
227 (make-org-lint-checker
228 :name 'invalid-macro-argument-and-template
229 :description "Report spurious macro arguments or invalid macro templates"
230 :categories '(export)
231 :trust 'low)
232 (make-org-lint-checker
233 :name 'special-property-in-properties-drawer
234 :description "Report special properties in properties drawers"
235 :categories '(properties))
236 (make-org-lint-checker
237 :name 'obsolete-properties-drawer
238 :description "Report obsolete syntax for properties drawers"
239 :categories '(obsolete properties))
240 (make-org-lint-checker
241 :name 'undefined-footnote-reference
242 :description "Report missing definition for footnote references"
243 :categories '(footnote))
244 (make-org-lint-checker
245 :name 'unreferenced-footnote-definition
246 :description "Report missing reference for footnote definitions"
247 :categories '(footnote))
248 (make-org-lint-checker
249 :name 'extraneous-element-in-footnote-section
250 :description "Report non-footnote definitions in footnote section"
251 :categories '(footnote))
252 (make-org-lint-checker
253 :name 'invalid-keyword-syntax
254 :description "Report probable invalid keywords"
255 :trust 'low)
256 (make-org-lint-checker
257 :name 'invalid-block
258 :description "Report invalid blocks"
259 :trust 'low)
260 (make-org-lint-checker
261 :name 'misplaced-planning-info
262 :description "Report misplaced planning info line"
263 :trust 'low)
264 (make-org-lint-checker
265 :name 'incomplete-drawer
266 :description "Report probable incomplete drawers"
267 :trust 'low)
268 (make-org-lint-checker
269 :name 'indented-diary-sexp
270 :description "Report probable indented diary-sexps"
271 :trust 'low)
272 (make-org-lint-checker
273 :name 'quote-section
274 :description "Report obsolete QUOTE section"
275 :categories '(obsolete)
276 :trust 'low))
277 "List of all available checkers.")
279 (defun org-lint--collect-duplicates
280 (ast type extract-key extract-position build-message)
281 "Helper function to collect duplicates in parse tree AST.
283 EXTRACT-KEY is a function extracting key. It is called with
284 a single argument: the element or object. Comparison is done
285 with `equal'.
287 EXTRACT-POSITION is a function returning position for the report.
288 It is called with two arguments, the object or element, and the
289 key.
291 BUILD-MESSAGE is a function creating the report message. It is
292 called with one argument, the key used for comparison."
293 (let* (keys
294 originals
295 reports
296 (make-report
297 (lambda (position value)
298 (push (list position (funcall build-message value)) reports))))
299 (org-element-map ast type
300 (lambda (datum)
301 (let ((key (funcall extract-key datum)))
302 (cond
303 ((not key))
304 ((assoc key keys) (cl-pushnew (assoc key keys) originals)
305 (funcall make-report (funcall extract-position datum key) key))
306 (t (push (cons key (funcall extract-position datum key)) keys))))))
307 (dolist (e originals reports) (funcall make-report (cdr e) (car e)))))
309 (defun org-lint-duplicate-custom-id (ast)
310 (org-lint--collect-duplicates
312 'node-property
313 (lambda (property)
314 (and (eq (compare-strings "CUSTOM_ID" nil nil
315 (org-element-property :key property) nil nil
318 (org-element-property :value property)))
319 (lambda (property _) (org-element-property :begin property))
320 (lambda (key) (format "Duplicate CUSTOM_ID property \"%s\"" key))))
322 (defun org-lint-duplicate-name (ast)
323 (org-lint--collect-duplicates
325 org-element-all-elements
326 (lambda (datum) (org-element-property :name datum))
327 (lambda (datum name)
328 (goto-char (org-element-property :begin datum))
329 (re-search-forward
330 (format "^[ \t]*#\\+[A-Za-z]+: +%s *$" (regexp-quote name)))
331 (match-beginning 0))
332 (lambda (key) (format "Duplicate NAME \"%s\"" key))))
334 (defun org-lint-duplicate-target (ast)
335 (org-lint--collect-duplicates
337 'target
338 (lambda (target) (org-split-string (org-element-property :value target)))
339 (lambda (target _) (org-element-property :begin target))
340 (lambda (key)
341 (format "Duplicate target <<%s>>" (mapconcat #'identity key " ")))))
343 (defun org-lint-duplicate-footnote-definition (ast)
344 (org-lint--collect-duplicates
346 'footnote-definition
347 (lambda (definition) (org-element-property :label definition))
348 (lambda (definition _) (org-element-property :post-affiliated definition))
349 (lambda (key) (format "Duplicate footnote definition \"%s\"" key))))
351 (defun org-lint-orphaned-affiliated-keywords (ast)
352 ;; Ignore orphan RESULTS keywords, which could be generated from
353 ;; a source block returning no value.
354 (let ((keywords (cl-set-difference org-element-affiliated-keywords
355 '("RESULT" "RESULTS")
356 :test #'equal)))
357 (org-element-map ast 'keyword
358 (lambda (k)
359 (let ((key (org-element-property :key k)))
360 (and (or (let ((case-fold-search t))
361 (org-string-match-p "\\`ATTR_[-_A-Za-z0-9]+\\'" key))
362 (member key keywords))
363 (list (org-element-property :post-affiliated k)
364 (format "Orphaned affiliated keyword: \"%s\"" key))))))))
366 (defun org-lint-obsolete-affiliated-keywords (_)
367 (let ((regexp (format "^[ \t]*#\\+%s:"
368 (regexp-opt '("DATA" "LABEL" "RESNAME" "SOURCE"
369 "SRCNAME" "TBLNAME" "RESULT" "HEADERS")
370 t)))
371 reports)
372 (while (re-search-forward regexp nil t)
373 (let ((key (upcase (match-string-no-properties 1))))
374 (when (< (point)
375 (org-element-property :post-affiliated (org-element-at-point)))
376 (push
377 (list (line-beginning-position)
378 (format
379 "Obsolete affiliated keyword: \"%s\". Use \"%s\" instead"
381 (pcase key
382 ("HEADERS" "HEADER")
383 ("RESULT" "RESULTS")
384 (_ "NAME"))))
385 reports))))
386 reports))
388 (defun org-lint-deprecated-export-blocks (ast)
389 (let ((deprecated '("ASCII" "BEAMER" "HTML" "LATEX" "MAN" "MARKDOWN" "MD"
390 "ODT" "ORG" "TEXINFO")))
391 (org-element-map ast 'special-block
392 (lambda (b)
393 (let ((type (org-element-property :type b)))
394 (when (member-ignore-case type deprecated)
395 (list
396 (org-element-property :post-affiliated b)
397 (format
398 "Deprecated syntax for export block. Use \"BEGIN_EXPORT %s\" \
399 instead"
400 type))))))))
402 (defun org-lint-deprecated-header-syntax (ast)
403 (let* ((deprecated-babel-properties
404 (mapcar (lambda (arg) (symbol-name (car arg)))
405 org-babel-common-header-args-w-values))
406 (deprecated-re
407 (format "\\`%s[ \t]" (regexp-opt deprecated-babel-properties t))))
408 (org-element-map ast '(keyword node-property)
409 (lambda (datum)
410 (let ((key (org-element-property :key datum)))
411 (pcase (org-element-type datum)
412 (`keyword
413 (let ((value (org-element-property :value datum)))
414 (and (string= key "PROPERTY")
415 (string-match deprecated-re value)
416 (list (org-element-property :begin datum)
417 (format "Deprecated syntax for \"%s\". \
418 Use header-args instead"
419 (match-string-no-properties 1 value))))))
420 (`node-property
421 (and (member-ignore-case key deprecated-babel-properties)
422 (list
423 (org-element-property :begin datum)
424 (format "Deprecated syntax for \"%s\". \
425 Use :header-args: instead"
426 key))))))))))
428 (defun org-lint-missing-language-in-src-block (ast)
429 (org-element-map ast 'src-block
430 (lambda (b)
431 (unless (org-element-property :language b)
432 (list (org-element-property :post-affiliated b)
433 "Missing language in source block")))))
435 (defun org-lint-missing-backend-in-export-block (ast)
436 (org-element-map ast 'export-block
437 (lambda (b)
438 (unless (org-element-property :type b)
439 (list (org-element-property :post-affiliated b)
440 "Missing back-end in export block")))))
442 (defun org-lint-invalid-babel-call-block (ast)
443 (org-element-map ast 'babel-call
444 (lambda (b)
445 (cond
446 ((not (org-element-property :call b))
447 (list (org-element-property :post-affiliated b)
448 "Invalid syntax in babel call block"))
449 ((let ((h (org-element-property :end-header b)))
450 (and h (org-string-match-p "\\`\\[.*\\]\\'" h)))
451 (list
452 (org-element-property :post-affiliated b)
453 "Babel call's end header must not be wrapped within brackets"))))))
455 (defun org-lint-deprecated-category-setup (ast)
456 (org-element-map ast 'keyword
457 (let (category-flag)
458 (lambda (k)
459 (cond
460 ((not (string= (org-element-property :key k) "CATEGORY")) nil)
461 (category-flag
462 (list (org-element-property :post-affiliated k)
463 "Spurious CATEGORY keyword. Set :CATEGORY: property instead"))
464 (t (setf category-flag t) nil))))))
466 (defun org-lint-invalid-coderef-link (ast)
467 (let ((info (list :parse-tree ast)))
468 (org-element-map ast 'link
469 (lambda (link)
470 (let ((ref (org-element-property :path link)))
471 (and (equal (org-element-property :type link) "coderef")
472 (not (ignore-errors (org-export-resolve-coderef ref info)))
473 (list (org-element-property :begin link)
474 (format "Unknown coderef \"%s\"" ref))))))))
476 (defun org-lint-invalid-custom-id-link (ast)
477 (let ((info (list :parse-tree ast)))
478 (org-element-map ast 'link
479 (lambda (link)
480 (and (equal (org-element-property :type link) "custom-id")
481 (not (ignore-errors (org-export-resolve-id-link link info)))
482 (list (org-element-property :begin link)
483 (format "Unknown custom ID \"%s\""
484 (org-element-property :path link))))))))
486 (defun org-lint-invalid-fuzzy-link (ast)
487 (let ((info (list :parse-tree ast)))
488 (org-element-map ast 'link
489 (lambda (link)
490 (and (equal (org-element-property :type link) "fuzzy")
491 (not (ignore-errors (org-export-resolve-fuzzy-link link info)))
492 (list (org-element-property :begin link)
493 (format "Unknown fuzzy location \"%s\""
494 (let ((path (org-element-property :path link)))
495 (if (string-prefix-p "*" path)
496 (substring path 1)
497 path)))))))))
499 (defun org-lint-invalid-id-link (ast)
500 (org-element-map ast 'link
501 (lambda (link)
502 (let ((id (org-element-property :path link)))
503 (and (equal (org-element-property :type link) "id")
504 (not (org-id-find id))
505 (list (org-element-property :begin link)
506 (format "Unknown ID \"%s\"" id)))))))
508 (defun org-lint-special-property-in-properties-drawer (ast)
509 (org-element-map ast 'node-property
510 (lambda (p)
511 (let ((key (org-element-property :key p)))
512 (and (member-ignore-case key org-special-properties)
513 (list (org-element-property :begin p)
514 (format
515 "Special property \"%s\" found in a properties drawer"
516 key)))))))
518 (defun org-lint-obsolete-properties-drawer (ast)
519 (org-element-map ast 'drawer
520 (lambda (d)
521 (when (equal (org-element-property :drawer-name d) "PROPERTIES")
522 (let ((section (org-element-lineage d '(section))))
523 (unless (org-element-map section 'property-drawer #'identity nil t)
524 (list (org-element-property :post-affiliated d)
525 (if (save-excursion
526 (goto-char (org-element-property :post-affiliated d))
527 (forward-line -1)
528 (or (org-at-heading-p) (org-at-planning-p)))
529 "Incorrect contents for PROPERTIES drawer"
530 "Incorrect location for PROPERTIES drawer"))))))))
532 (defun org-lint-link-to-local-file (ast)
533 (org-element-map ast 'link
534 (lambda (l)
535 (when (equal (org-element-property :type l) "file")
536 (let ((file (org-link-unescape (org-element-property :path l))))
537 (and (not (file-remote-p file))
538 (not (file-exists-p file))
539 (list (org-element-property :begin l)
540 (format (if (org-element-lineage l '(link))
541 "Link to non-existent image file \"%s\"\
542 in link description"
543 "Link to non-existent local file \"%s\"")
544 file))))))))
546 (defun org-lint-non-existent-setupfile-parameter (ast)
547 (org-element-map ast 'keyword
548 (lambda (k)
549 (when (equal (org-element-property :key k) "SETUPFILE")
550 (let ((file (org-remove-double-quotes
551 (org-element-property :value k))))
552 (and (not (file-remote-p file))
553 (not (file-exists-p file))
554 (list (org-element-property :begin k)
555 (format "Non-existent setup file \"%s\"" file))))))))
557 (defun org-lint-wrong-include-link-parameter (ast)
558 (org-element-map ast 'keyword
559 (lambda (k)
560 (when (equal (org-element-property :key k) "INCLUDE")
561 (let* ((value (org-element-property :value k))
562 (path
563 (and (string-match "^\\(\".+\"\\|\\S-+\\)[ \t]*" value)
564 (save-match-data
565 (org-remove-double-quotes (match-string 1 value))))))
566 (if (not path)
567 (list (org-element-property :post-affiliated k)
568 "Missing location argument in INCLUDE keyword")
569 (let* ((file (org-string-nw-p
570 (if (string-match "::\\(.*\\)\\'" path)
571 (substring path 0 (match-beginning 0))
572 path)))
573 (search (and (not (equal file path))
574 (org-string-nw-p (match-string 1 path)))))
575 (if (and file
576 (not (file-remote-p file))
577 (not (file-exists-p file)))
578 (list (org-element-property :post-affiliated k)
579 "Non-existent file argument in INCLUDE keyword")
580 (let* ((visiting (if file (find-buffer-visiting file)
581 (current-buffer)))
582 (buffer (or visiting (find-file-noselect file))))
583 (unwind-protect
584 (with-current-buffer buffer
585 (when (and search
586 (not
587 (ignore-errors
588 (let ((org-link-search-inhibit-query t))
589 (org-link-search search nil t)))))
590 (list (org-element-property :post-affiliated k)
591 (format
592 "Invalid search part \"%s\" in INCLUDE keyword"
593 search))))
594 (unless visiting (kill-buffer buffer))))))))))))
596 (defun org-lint-obsolete-include-markup (ast)
597 (let ((regexp (format "\\`\\(?:\".+\"\\|\\S-+\\)[ \t]+%s"
598 (regexp-opt
599 '("ASCII" "BEAMER" "HTML" "LATEX" "MAN" "MARKDOWN" "MD"
600 "ODT" "ORG" "TEXINFO")
601 t))))
602 (org-element-map ast 'keyword
603 (lambda (k)
604 (when (equal (org-element-property :key k) "INCLUDE")
605 (let ((case-fold-search t)
606 (value (org-element-property :value k)))
607 (when (string-match regexp value)
608 (let ((markup (match-string-no-properties 1 value)))
609 (list (org-element-property :post-affiliated k)
610 (format "Obsolete markup \"%s\" in INCLUDE keyword. \
611 Use \"export %s\" instead"
612 markup
613 markup))))))))))
615 (defun org-lint-unknown-options-item (ast)
616 (let ((allowed (delq nil
617 (append
618 (mapcar (lambda (o) (nth 2 o)) org-export-options-alist)
619 (cl-mapcan
620 (lambda (b)
621 (mapcar (lambda (o) (nth 2 o))
622 (org-export-backend-options b)))
623 org-export-registered-backends))))
624 reports)
625 (org-element-map ast 'keyword
626 (lambda (k)
627 (when (string= (org-element-property :key k) "OPTIONS")
628 (let ((value (org-element-property :value k))
629 (start 0))
630 (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-*\\)[ \t]*"
631 value
632 start)
633 (setf start (match-end 0))
634 (let ((item (match-string 1 value)))
635 (unless (member item allowed)
636 (push (list (org-element-property :post-affiliated k)
637 (format "Unknown OPTIONS item \"%s\"" item))
638 reports))))))))
639 reports))
641 (defun org-lint-invalid-macro-argument-and-template (ast)
642 (let ((extract-placeholders
643 (lambda (template)
644 (let ((start 0)
645 args)
646 (while (string-match "\\$\\([1-9][0-9]*\\)" template start)
647 (setf start (match-end 0))
648 (push (string-to-number (match-string 1 template)) args))
649 (sort (org-uniquify args) #'<))))
650 reports)
651 ;; Check arguments for macro templates.
652 (org-element-map ast 'keyword
653 (lambda (k)
654 (when (string= (org-element-property :key k) "MACRO")
655 (let* ((value (org-element-property :value k))
656 (name (and (string-match "^\\S-+" value)
657 (match-string 0 value)))
658 (template (and name
659 (org-trim (substring value (match-end 0))))))
660 (cond
661 ((not name)
662 (push (list (org-element-property :post-affiliated k)
663 "Missing name in MACRO keyword")
664 reports))
665 ((not (org-string-nw-p template))
666 (push (list (org-element-property :post-affiliated k)
667 "Missing template in macro \"%s\"" name)
668 reports))
670 (unless (let ((args (funcall extract-placeholders template)))
671 (equal (number-sequence 1 (or (org-last args) 0)) args))
672 (push (list (org-element-property :post-affiliated k)
673 (format "Unused placeholders in macro \"%s\""
674 name))
675 reports))))))))
676 ;; Check arguments for macros.
677 (org-macro-initialize-templates)
678 (let ((templates (append
679 (mapcar (lambda (m) (cons m "$1"))
680 '("author" "date" "email" "title" "results"))
681 org-macro-templates)))
682 (org-element-map ast 'macro
683 (lambda (macro)
684 (let* ((name (org-element-property :key macro))
685 (template (cdr (assoc-string name templates t))))
686 (if (not template)
687 (push (list (org-element-property :begin macro)
688 (format "Undefined macro \"%s\"" name))
689 reports)
690 (let ((arg-numbers (funcall extract-placeholders template)))
691 (when arg-numbers
692 (let ((spurious-args
693 (nthcdr (apply #'max arg-numbers)
694 (org-element-property :args macro))))
695 (when spurious-args
696 (push
697 (list (org-element-property :begin macro)
698 (format "Unused argument%s in macro \"%s\": %s"
699 (if (> (length spurious-args) 1) "s" "")
700 name
701 (mapconcat (lambda (a) (format "\"%s\"" a))
702 spurious-args
703 ", ")))
704 reports))))))))))
705 reports))
707 (defun org-lint-undefined-footnote-reference (ast)
708 (let ((definitions (org-element-map ast 'footnote-definition
709 (lambda (f) (org-element-property :label f)))))
710 (org-element-map ast 'footnote-reference
711 (lambda (f)
712 (let ((label (org-element-property :label f)))
713 (and label
714 (not (member label definitions))
715 (list (org-element-property :begin f)
716 (format "Missing definition for footnote [%s]"
717 label))))))))
719 (defun org-lint-unreferenced-footnote-definition (ast)
720 (let ((references (org-element-map ast 'footnote-reference
721 (lambda (f) (org-element-property :label f)))))
722 (org-element-map ast 'footnote-definition
723 (lambda (f)
724 (let ((label (org-element-property :label f)))
725 (and label
726 (not (member label references))
727 (list (org-element-property :post-affiliated f)
728 (format "No reference for footnote definition [%s]"
729 label))))))))
731 (defun org-lint-colon-in-name (ast)
732 (org-element-map ast org-element-all-elements
733 (lambda (e)
734 (let ((name (org-element-property :name e)))
735 (and name
736 (org-string-match-p ":" name)
737 (list (progn
738 (goto-char (org-element-property :begin e))
739 (re-search-forward
740 (format "^[ \t]*#\\+\\w+: +%s *$" (regexp-quote name)))
741 (match-beginning 0))
742 (format
743 "Name \"%s\" contains a colon; Babel cannot use it as input"
744 name)))))))
746 (defun org-lint-misplaced-planning-info (_)
747 (let ((case-fold-search t)
748 reports)
749 (while (re-search-forward org-planning-line-re nil t)
750 (unless (memq (org-element-type (org-element-at-point))
751 '(comment-block example-block export-block planning
752 src-block verse-block))
753 (push (list (line-beginning-position) "Misplaced planning info line")
754 reports)))
755 reports))
757 (defun org-lint-incomplete-drawer (_)
758 (let (reports)
759 (while (re-search-forward org-drawer-regexp nil t)
760 (let ((name (org-trim (match-string-no-properties 0)))
761 (element (org-element-at-point)))
762 (pcase (org-element-type element)
763 ((or `drawer `property-drawer)
764 (goto-char (org-element-property :end element))
765 nil)
766 ((or `comment-block `example-block `export-block `src-block
767 `verse-block)
768 nil)
770 (push (list (line-beginning-position)
771 (format "Possible incomplete drawer \"%s\"" name))
772 reports)))))
773 reports))
775 (defun org-lint-indented-diary-sexp (_)
776 (let (reports)
777 (while (re-search-forward "^[ \t]+%%(" nil t)
778 (unless (memq (org-element-type (org-element-at-point))
779 '(comment-block diary-sexp example-block export-block
780 src-block verse-block))
781 (push (list (line-beginning-position) "Possible indented diary-sexp")
782 reports)))
783 reports))
785 (defun org-lint-invalid-block (_)
786 (let ((case-fold-search t)
787 (regexp "^[ \t]*#\\+\\(BEGIN\\|END\\)\\(?::\\|_[^[:space:]]*\\)?[ \t]*")
788 reports)
789 (while (re-search-forward regexp nil t)
790 (let ((name (org-trim (buffer-substring-no-properties
791 (line-beginning-position) (line-end-position)))))
792 (cond
793 ((and (string-prefix-p "END" (match-string 1) t)
794 (not (eolp)))
795 (push (list (line-beginning-position)
796 (format "Invalid block closing line \"%s\"" name))
797 reports))
798 ((not (memq (org-element-type (org-element-at-point))
799 '(center-block comment-block dynamic-block example-block
800 export-block quote-block special-block
801 src-block verse-block)))
802 (push (list (line-beginning-position)
803 (format "Possible incomplete block \"%s\""
804 name))
805 reports)))))
806 reports))
808 (defun org-lint-invalid-keyword-syntax (_)
809 (let ((regexp "^[ \t]*#\\+\\([^[:space:]:]*\\)\\(?: \\|$\\)")
810 (exception-re
811 (format "[ \t]*#\\+%s\\(\\[.*\\]\\)?:\\(?: \\|$\\)"
812 (regexp-opt org-element-dual-keywords)))
813 reports)
814 (while (re-search-forward regexp nil t)
815 (let ((name (match-string-no-properties 1)))
816 (unless (or (string-prefix-p "BEGIN" name t)
817 (string-prefix-p "END" name t)
818 (save-excursion
819 (beginning-of-line)
820 (let ((case-fold-search t)) (looking-at exception-re))))
821 (push (list (match-beginning 0)
822 (format "Possible missing colon in keyword \"%s\"" name))
823 reports))))
824 reports))
826 (defun org-lint-extraneous-element-in-footnote-section (ast)
827 (org-element-map ast 'headline
828 (lambda (h)
829 (and (org-element-property :footnote-section-p h)
830 (org-element-map (org-element-contents h)
831 (cl-remove-if
832 (lambda (e)
833 (memq e '(comment comment-block footnote-definition
834 property-drawer section)))
835 org-element-all-elements)
836 (lambda (e)
837 (not (and (eq (org-element-type e) 'headline)
838 (org-element-property :commentedp e))))
839 nil t '(footnote-definition property-drawer))
840 (list (org-element-property :begin h)
841 "Extraneous elements in footnote section")))))
843 (defun org-lint-quote-section (ast)
844 (org-element-map ast '(headline inlinetask)
845 (lambda (h)
846 (let ((title (org-element-property :raw-value h)))
847 (and (or (string-prefix-p "QUOTE " title)
848 (string-prefix-p (concat org-comment-string " QUOTE ") title))
849 (list (org-element-property :begin h)
850 "Deprecated QUOTE section"))))))
852 (defun org-lint-wrong-header-argument (ast)
853 (let* ((reports)
854 (verify
855 (lambda (datum language headers)
856 (let ((allowed
857 ;; If LANGUAGE is specified, restrict allowed
858 ;; headers to both LANGUAGE-specific and default
859 ;; ones. Otherwise, accept headers from any loaded
860 ;; language.
861 (append
862 org-babel-header-arg-names
863 (cl-mapcan
864 (lambda (l)
865 (let ((v (intern (format "org-babel-header-args:%s" l))))
866 (and (boundp v) (mapcar #'car (symbol-value v)))))
867 (if language (list language)
868 (mapcar #'car org-babel-load-languages))))))
869 (dolist (header headers)
870 (let ((h (symbol-name (car header)))
871 (p (or (org-element-property :post-affiliated datum)
872 (org-element-property :begin datum))))
873 (cond
874 ((not (string-prefix-p ":" h))
875 (push
876 (list p
877 (format "Missing colon in header argument \"%s\"" h))
878 reports))
879 ((assoc-string (substring h 1) allowed))
880 (t (push (list p (format "Unknown header argument \"%s\"" h))
881 reports)))))))))
882 (org-element-map ast '(babel-call inline-babel-call inline-src-block keyword
883 node-property src-block)
884 (lambda (datum)
885 (pcase (org-element-type datum)
886 ((or `babel-call `inline-babel-call)
887 (funcall verify
888 datum
890 (cl-mapcan #'org-babel-parse-header-arguments
891 (list
892 (org-element-property :inside-header datum)
893 (org-element-property :end-header datum)))))
894 (`inline-src-block
895 (funcall verify
896 datum
897 (org-element-property :language datum)
898 (org-babel-parse-header-arguments
899 (org-element-property :parameters datum))))
900 (`keyword
901 (when (string= (org-element-property :key datum) "PROPERTY")
902 (let ((value (org-element-property :value datum)))
903 (when (string-match "\\`header-args\\(?::\\(\\S-+\\)\\)?\\+? *"
904 value)
905 (funcall verify
906 datum
907 (match-string 1 value)
908 (org-babel-parse-header-arguments
909 (substring value (match-end 0))))))))
910 (`node-property
911 (let ((key (org-element-property :key datum)))
912 (when (let ((case-fold-search t))
913 (string-match "\\`HEADER-ARGS\\(?::\\(\\S-+\\)\\)?\\+?"
914 key))
915 (funcall verify
916 datum
917 (match-string 1 key)
918 (org-babel-parse-header-arguments
919 (org-element-property :value datum))))))
920 (`src-block
921 (funcall verify
922 datum
923 (org-element-property :language datum)
924 (cl-mapcan #'org-babel-parse-header-arguments
925 (cons (org-element-property :parameters datum)
926 (org-element-property :header datum))))))))
927 reports))
929 (defun org-lint-wrong-header-value (ast)
930 (let (reports)
931 (org-element-map ast
932 '(babel-call inline-babel-call inline-src-block src-block)
933 (lambda (datum)
934 (let* ((type (org-element-type datum))
935 (language (org-element-property :language datum))
936 (allowed-header-values
937 (append (and language
938 (let ((v (intern (concat "org-babel-header-args:"
939 language))))
940 (and (boundp v) (symbol-value v))))
941 org-babel-common-header-args-w-values))
942 (datum-header-values
943 (apply
944 #'org-babel-merge-params
945 org-babel-default-header-args
946 (and language
947 (let ((v (intern (concat "org-babel-default-header-args:"
948 language))))
949 (and (boundp v) (symbol-value v))))
950 (append
951 (list (and (memq type '(babel-call inline-babel-call))
952 org-babel-default-lob-header-args))
953 (progn (goto-char (org-element-property :begin datum))
954 (org-babel-params-from-properties language))
955 (list
956 (org-babel-parse-header-arguments
957 (org-trim
958 (pcase type
959 (`src-block
960 (mapconcat
961 #'identity
962 (cons (org-element-property :parameters datum)
963 (org-element-property :header datum))
964 " "))
965 (`inline-src-block
966 (or (org-element-property :parameters datum) ""))
968 (concat
969 (org-element-property :inside-header datum)
971 (org-element-property :end-header datum)))))))))))
972 (dolist (header datum-header-values)
973 (let ((allowed-values
974 (cdr (assoc-string (substring (symbol-name (car header)) 1)
975 allowed-header-values))))
976 (unless (memq allowed-values '(:any nil))
977 (let ((values (cdr header))
978 groups-alist)
979 (dolist (v (if (stringp values) (org-split-string values)
980 (list values)))
981 (let ((valid-value nil))
982 (catch 'exit
983 (dolist (group allowed-values)
984 (cond
985 ((not (funcall
986 (if (stringp v) #'assoc-string #'assoc)
987 v group))
988 (when (memq :any group)
989 (setf valid-value t)
990 (push (cons group v) groups-alist)))
991 ((assq group groups-alist)
992 (push
993 (list
994 (or (org-element-property :post-affiliated datum)
995 (org-element-property :begin datum))
996 (format
997 "Forbidden combination in header \"%s\": %s, %s"
998 (car header)
999 (cdr (assq group groups-alist))
1001 reports)
1002 (throw 'exit nil))
1003 (t (push (cons group v) groups-alist)
1004 (setf valid-value t))))
1005 (unless valid-value
1006 (push
1007 (list
1008 (or (org-element-property :post-affiliated datum)
1009 (org-element-property :begin datum))
1010 (format "Unknown value \"%s\" for header \"%s\""
1012 (car header)))
1013 reports))))))))))))
1014 reports))
1017 ;;; Reports UI
1019 (defvar org-lint--report-mode-map
1020 (let ((map (make-sparse-keymap)))
1021 (set-keymap-parent map tabulated-list-mode-map)
1022 (define-key map (kbd "RET") 'org-lint--jump-to-source)
1023 (define-key map (kbd "TAB") 'org-lint--show-source)
1024 (define-key map (kbd "C-j") 'org-lint--show-source)
1025 (define-key map (kbd "h") 'org-lint--hide-checker)
1026 (define-key map (kbd "i") 'org-lint--ignore-checker)
1027 map)
1028 "Local keymap for `org-lint--report-mode' buffers.")
1030 (define-derived-mode org-lint--report-mode tabulated-list-mode "OrgLint"
1031 "Major mode used to display reports emitted during linting.
1032 \\{org-lint--report-mode-map}"
1033 (setf tabulated-list-format
1034 `[("Line" 6
1035 (lambda (a b)
1036 (< (string-to-number (aref (cadr a) 0))
1037 (string-to-number (aref (cadr b) 0))))
1038 :right-align t)
1039 ("Trust" 5 t)
1040 ("Warning" 0 t)])
1041 (tabulated-list-init-header))
1043 (defun org-lint--generate-reports (buffer checkers)
1044 "Generate linting report for BUFFER.
1046 CHECKERS is the list of checkers used.
1048 Return an alist (ID [LINE TRUST DESCRIPTION CHECKER]), suitable
1049 for `tabulated-list-printer'."
1050 (with-current-buffer buffer
1051 (save-excursion
1052 (goto-char (point-min))
1053 (let ((ast (org-element-parse-buffer))
1054 (id 0)
1055 (last-line 1)
1056 (last-pos 1))
1057 ;; Insert unique ID for each report. Replace buffer positions
1058 ;; with line numbers.
1059 (mapcar
1060 (lambda (report)
1061 (list
1062 (incf id)
1063 (apply #'vector
1064 (cons
1065 (progn
1066 (goto-char (car report))
1067 (beginning-of-line)
1068 (prog1 (number-to-string
1069 (incf last-line (count-lines last-pos (point))))
1070 (setf last-pos (point))))
1071 (cdr report)))))
1072 ;; Insert trust level in generated reports. Also sort them
1073 ;; by buffer position in order to optimize lines computation.
1074 (sort (cl-mapcan
1075 (lambda (c)
1076 (let ((trust (symbol-name (org-lint-checker-trust c))))
1077 (mapcar
1078 (lambda (report)
1079 (list (car report) trust (nth 1 report) c))
1080 (save-excursion
1081 (funcall
1082 (intern (format "org-lint-%s"
1083 (org-lint-checker-name c)))
1084 ast)))))
1085 checkers)
1086 #'car-less-than-car))))))
1088 (defvar-local org-lint--source-buffer nil
1089 "Source buffer associated to current report buffer.")
1091 (defvar-local org-lint--local-checkers nil
1092 "List of checkers used to build current report.")
1094 (defun org-lint--refresh-reports ()
1095 (setq tabulated-list-entries
1096 (org-lint--generate-reports org-lint--source-buffer
1097 org-lint--local-checkers))
1098 (tabulated-list-print))
1100 (defun org-lint--current-line ()
1101 "Return current report line, as a number."
1102 (string-to-number (aref (tabulated-list-get-entry) 0)))
1104 (defun org-lint--current-checker (&optional entry)
1105 "Return current report checker.
1106 When optional argument ENTRY is non-nil, use this entry instead
1107 of current one."
1108 (aref (if entry (nth 1 entry) (tabulated-list-get-entry)) 3))
1110 (defun org-lint--display-reports (source checkers)
1111 "Display linting reports for buffer SOURCE.
1112 CHECKERS is the list of checkers used."
1113 (let ((buffer (get-buffer-create "*Org Lint*")))
1114 (with-current-buffer buffer
1115 (org-lint--report-mode)
1116 (setf org-lint--source-buffer source)
1117 (setf org-lint--local-checkers checkers)
1118 (org-lint--refresh-reports)
1119 (tabulated-list-print)
1120 (add-hook 'tabulated-list-revert-hook #'org-lint--refresh-reports nil t))
1121 (pop-to-buffer buffer)))
1123 (defun org-lint--jump-to-source ()
1124 "Move to source line that generated the report at point."
1125 (interactive)
1126 (let ((l (org-lint--current-line)))
1127 (switch-to-buffer-other-window org-lint--source-buffer)
1128 (org-goto-line l)
1129 (org-show-set-visibility 'local)
1130 (recenter)))
1132 (defun org-lint--show-source ()
1133 "Show source line that generated the report at point."
1134 (interactive)
1135 (let ((buffer (current-buffer)))
1136 (org-lint--jump-to-source)
1137 (switch-to-buffer-other-window buffer)))
1139 (defun org-lint--hide-checker ()
1140 "Hide all reports from checker that generated the report at point."
1141 (interactive)
1142 (let ((c (org-lint--current-checker)))
1143 (setf tabulated-list-entries
1144 (cl-remove-if (lambda (e) (equal c (org-lint--current-checker e)))
1145 tabulated-list-entries))
1146 (tabulated-list-print)))
1148 (defun org-lint--ignore-checker ()
1149 "Ignore all reports from checker that generated the report at point.
1150 Checker will also be ignored in all subsequent reports."
1151 (interactive)
1152 (setf org-lint--local-checkers
1153 (remove (org-lint--current-checker) org-lint--local-checkers))
1154 (org-lint--hide-checker))
1157 ;;; Public function
1159 ;;;###autoload
1160 (defun org-lint (&optional arg)
1161 "Check current Org buffer for syntax mistakes.
1163 By default, run all checkers. With a single prefix ARG \
1164 \\[universal-argument],
1165 select one category of checkers only. With a double prefix
1166 \\[universal-argument] \\[universal-argument], select one precise \
1167 checker by its name.
1169 ARG can also be a list of checker names, as symbols, to run."
1170 (interactive "P")
1171 (unless (derived-mode-p 'org-mode) (user-error "Not in an Org buffer"))
1172 (when (org-called-interactively-p)
1173 (message "Org linting process starting..."))
1174 (let ((checkers
1175 (pcase arg
1176 (`nil org-lint--checkers)
1177 (`(4)
1178 (let ((category
1179 (completing-read
1180 "Checker category: "
1181 (mapcar #'org-lint-checker-categories org-lint--checkers)
1182 nil t)))
1183 (cl-remove-if-not
1184 (lambda (c)
1185 (assoc-string (org-lint-checker-categories c) category))
1186 org-lint--checkers)))
1187 (`(16)
1188 (list
1189 (let ((name (completing-read
1190 "Checker name: "
1191 (mapcar #'org-lint-checker-name org-lint--checkers)
1192 nil t)))
1193 (catch 'exit
1194 (dolist (c org-lint--checkers)
1195 (when (string= (org-lint-checker-name c) name)
1196 (throw 'exit c)))))))
1197 ((pred consp)
1198 (cl-remove-if-not (lambda (c) (memq (org-lint-checker-name c) arg))
1199 org-lint--checkers))
1200 (_ (user-error "Invalid argument `%S' for `org-lint'" arg)))))
1201 (if (not (org-called-interactively-p))
1202 (org-lint--generate-reports (current-buffer) checkers)
1203 (org-lint--display-reports (current-buffer) checkers)
1204 (message "Org linting process completed"))))
1207 (provide 'org-lint)
1208 ;;; org-lint.el ends here