org-element: Use pcase instead of case
[org-mode.git] / lisp / org-lint.el
blob5271dfba32d3dd37ac5db6578ef03ecd12e9520b
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/>.
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 ;; - 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
91 ;; - invalid blocks
92 ;; - misplaced planning info line
93 ;; - incomplete drawers
94 ;; - indented diary-sexps
95 ;; - obsolete QUOTE section
98 ;;; Code:
100 (require 'cl-lib)
101 (require 'org-element)
102 (require 'org-macro)
103 (require 'ox)
104 (require 'ob)
107 ;;; Checkers
109 (cl-defstruct (org-lint-checker (:copier nil))
110 (name 'missing-checker-name)
111 (description "")
112 (categories '(default))
113 (trust 'high)) ; `low' or `high'
115 (defun org-lint-missing-checker-name (_)
116 (error
117 "`A checker has no `:name' property. Please verify `org-lint--checkers'"))
119 (defconst org-lint--checkers
120 (list
121 (make-org-lint-checker
122 :name 'duplicate-custom-id
123 :description "Report duplicates CUSTOM_ID properties"
124 :categories '(link))
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"
132 :categories '(link))
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"
140 :trust 'low)
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)
149 :trust 'low)
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
159 :name 'colon-in-name
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"
169 :categories '(babel)
170 :trust 'low)
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"
178 :categories '(link))
179 (make-org-lint-checker
180 :name 'invalid-custom-id-link
181 :description "Report \"custom-id\" links with unknown destination"
182 :categories '(link))
183 (make-org-lint-checker
184 :name 'invalid-fuzzy-link
185 :description "Report \"fuzzy\" links with unknown destination"
186 :categories '(link))
187 (make-org-lint-checker
188 :name 'invalid-id-link
189 :description "Report \"id\" links with unknown destination"
190 :categories '(link))
191 (make-org-lint-checker
192 :name 'link-to-local-file
193 :description "Report links to non-existent local files"
194 :categories '(link)
195 :trust 'low)
196 (make-org-lint-checker
197 :name 'non-existent-setupfile-parameter
198 :description "Report SETUPFILE keywords with non-existent file parameter"
199 :trust 'low)
200 (make-org-lint-checker
201 :name 'wrong-include-link-parameter
202 :description "Report INCLUDE keywords with misleading link parameter"
203 :categories '(export)
204 :trust 'low)
205 (make-org-lint-checker
206 :name 'unknown-options-item
207 :description "Report unknown items in OPTIONS keyword"
208 :categories '(export)
209 :trust 'low)
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)
214 :trust 'low)
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"
238 :trust 'low)
239 (make-org-lint-checker
240 :name 'invalid-block
241 :description "Report invalid blocks"
242 :trust 'low)
243 (make-org-lint-checker
244 :name 'misplaced-planning-info
245 :description "Report misplaced planning info line"
246 :trust 'low)
247 (make-org-lint-checker
248 :name 'incomplete-drawer
249 :description "Report probable incomplete drawers"
250 :trust 'low)
251 (make-org-lint-checker
252 :name 'indented-diary-sexp
253 :description "Report probable indented diary-sexps"
254 :trust 'low)
255 (make-org-lint-checker
256 :name 'quote-section
257 :description "Report obsolete QUOTE section"
258 :categories '(obsolete)
259 :trust 'low))
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
268 with `equal'.
270 EXTRACT-POSITION is a function returning position for the report.
271 It is called with two arguments, the object or element, and the
272 key.
274 BUILD-MESSAGE is a function creating the report message. It is
275 called with one argument, the key used for comparison."
276 (let* (keys
277 originals
278 reports
279 (make-report
280 (lambda (position value)
281 (push (list position (funcall build-message value)) reports))))
282 (org-element-map ast type
283 (lambda (datum)
284 (let ((key (funcall extract-key datum)))
285 (cond
286 ((not key))
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
295 'node-property
296 (lambda (property)
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))
310 (lambda (datum name)
311 (goto-char (org-element-property :begin datum))
312 (re-search-forward
313 (format "^[ \t]*#\\+[A-Za-z]+: +%s *$" (regexp-quote name)))
314 (match-beginning 0))
315 (lambda (key) (format "Duplicate NAME \"%s\"" key))))
317 (defun org-lint-duplicate-target (ast)
318 (org-lint--collect-duplicates
320 'target
321 (lambda (target) (org-split-string (org-element-property :value target)))
322 (lambda (target _) (org-element-property :begin target))
323 (lambda (key)
324 (format "Duplicate target <<%s>>" (mapconcat #'identity key " ")))))
326 (defun org-lint-duplicate-footnote-definition (ast)
327 (org-lint--collect-duplicates
329 'footnote-definition
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")
339 :test #'equal)))
340 (org-element-map ast 'keyword
341 (lambda (k)
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")
353 t)))
354 reports)
355 (while (re-search-forward regexp nil t)
356 (let ((key (upcase (org-match-string-no-properties 1))))
357 (when (< (point)
358 (org-element-property :post-affiliated (org-element-at-point)))
359 (push
360 (list (line-beginning-position)
361 (format
362 "Obsolete affiliated keyword: \"%s\". Use \"%s\" instead"
364 (pcase key
365 ("HEADERS" "HEADER")
366 ("RESULT" "RESULTS")
367 (_ "NAME"))))
368 reports))))
369 reports))
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))
375 (deprecated-re
376 (format "\\`%s[ \t]" (regexp-opt deprecated-babel-properties t))))
377 (org-element-map ast '(keyword node-property)
378 (lambda (datum)
379 (let ((key (org-element-property :key datum)))
380 (pcase (org-element-type datum)
381 (`keyword
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))))))
389 (`node-property
390 (and (member-ignore-case key deprecated-babel-properties)
391 (list
392 (org-element-property :begin datum)
393 (format "Deprecated syntax for \"%s\". \
394 Use :header-args: instead"
395 key))))))))))
397 (defun org-lint-missing-language-in-src-block (ast)
398 (org-element-map ast 'src-block
399 (lambda (b)
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
406 (lambda (b)
407 (cond
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)))
413 (list
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
419 (let (category-flag)
420 (lambda (k)
421 (cond
422 ((not (string= (org-element-property :key k) "CATEGORY")) nil)
423 (category-flag
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
431 (lambda (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
441 (lambda (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
451 (lambda (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)
458 (substring path 1)
459 path)))))))))
461 (defun org-lint-invalid-id-link (ast)
462 (org-element-map ast 'link
463 (lambda (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
472 (lambda (p)
473 (let ((key (org-element-property :key p)))
474 (and (member-ignore-case key org-special-properties)
475 (list (org-element-property :begin p)
476 (format
477 "Special property \"%s\" found in a properties drawer"
478 key)))))))
480 (defun org-lint-obsolete-properties-drawer (ast)
481 (org-element-map ast 'drawer
482 (lambda (d)
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)
487 (if (save-excursion
488 (goto-char (org-element-property :post-affiliated d))
489 (forward-line -1)
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
496 (lambda (l)
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\"\
504 in link description"
505 "Link to non-existent local file \"%s\"")
506 file))))))))
508 (defun org-lint-non-existent-setupfile-parameter (ast)
509 (org-element-map ast 'keyword
510 (lambda (k)
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
521 (lambda (k)
522 (when (equal (org-element-property :key k) "INCLUDE")
523 (let* ((value (org-element-property :value k))
524 (path
525 (and (string-match "^\\(\".+\"\\|\\S-+\\)[ \t]*" value)
526 (save-match-data
527 (org-remove-double-quotes (match-string 1 value))))))
528 (if (not path)
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))
534 path)))
535 (search (and (not (equal file path))
536 (org-string-nw-p (match-string 1 path)))))
537 (if (and file
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)
543 (current-buffer)))
544 (buffer (or visiting (find-file-noselect file))))
545 (unwind-protect
546 (with-current-buffer buffer
547 (when (and search
548 (not
549 (ignore-errors
550 (let ((org-link-search-inhibit-query t))
551 (org-link-search search nil t)))))
552 (list (org-element-property :post-affiliated k)
553 (format
554 "Invalid search part \"%s\" in INCLUDE keyword"
555 search))))
556 (unless visiting (kill-buffer buffer))))))))))))
558 (defun org-lint-unknown-options-item (ast)
559 (let ((allowed (delq nil
560 (append
561 (mapcar (lambda (o) (nth 2 o)) org-export-options-alist)
562 (cl-mapcan
563 (lambda (b)
564 (mapcar (lambda (o) (nth 2 o))
565 (org-export-backend-options b)))
566 org-export-registered-backends))))
567 reports)
568 (org-element-map ast 'keyword
569 (lambda (k)
570 (when (string= (org-element-property :key k) "OPTIONS")
571 (let ((value (org-element-property :value k))
572 (start 0))
573 (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-*\\)[ \t]*"
574 value
575 start)
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))
581 reports))))))))
582 reports))
584 (defun org-lint-invalid-macro-argument-and-template (ast)
585 (let ((extract-placeholders
586 (lambda (template)
587 (let ((start 0)
588 args)
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) #'<))))
593 reports)
594 ;; Check arguments for macro templates.
595 (org-element-map ast 'keyword
596 (lambda (k)
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)))
601 (template (and name
602 (org-trim (substring value (match-end 0))))))
603 (cond
604 ((not name)
605 (push (list (org-element-property :post-affiliated k)
606 "Missing name in MACRO keyword")
607 reports))
608 ((not (org-string-nw-p template))
609 (push (list (org-element-property :post-affiliated k)
610 "Missing template in macro \"%s\"" name)
611 reports))
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\""
617 name))
618 reports))))))))
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
626 (lambda (macro)
627 (let* ((name (org-element-property :key macro))
628 (template (cdr (assoc-string name templates t))))
629 (if (not template)
630 (push (list (org-element-property :begin macro)
631 (format "Undefined macro \"%s\"" name))
632 reports)
633 (let ((spurious-args
634 (nthcdr (apply #'max
635 (funcall extract-placeholders template))
636 (org-element-property :args macro))))
637 (when spurious-args
638 (push (list (org-element-property :begin macro)
639 (format "Unused argument%s in macro \"%s\": %s"
640 (if (> (length spurious-args) 1) "s" "")
641 name
642 (mapconcat (lambda (a) (format "\"%s\"" a))
643 spurious-args
644 ", ")))
645 reports))))))))
646 reports))
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
652 (lambda (f)
653 (let ((label (org-element-property :label f)))
654 (and label
655 (not (member label definitions))
656 (list (org-element-property :begin f)
657 (format "Missing definition for footnote [%s]"
658 label))))))))
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
664 (lambda (f)
665 (let ((label (org-element-property :label f)))
666 (and label
667 (not (member label references))
668 (list (org-element-property :post-affiliated f)
669 (format "No reference for footnote definition [%s]"
670 label))))))))
672 (defun org-lint-colon-in-name (ast)
673 (org-element-map ast org-element-all-elements
674 (lambda (e)
675 (let ((name (org-element-property :name e)))
676 (and name
677 (org-string-match-p ":" name)
678 (list (progn
679 (goto-char (org-element-property :begin e))
680 (re-search-forward
681 (format "^[ \t]*#\\+\\w+: +%s *$" (regexp-quote name)))
682 (match-beginning 0))
683 (format
684 "Name \"%s\" contains a colon; Babel cannot use it as input"
685 name)))))))
687 (defun org-lint-misplaced-planning-info (_)
688 (let ((case-fold-search t)
689 reports)
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")
695 reports)))
696 reports))
698 (defun org-lint-incomplete-drawer (_)
699 (let (reports)
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))
706 nil)
707 ((or `comment-block `example-block `export-block `src-block
708 `verse-block)
709 nil)
711 (push (list (line-beginning-position)
712 (format "Possible incomplete drawer \"%s\"" name))
713 reports)))))
714 reports))
716 (defun org-lint-indented-diary-sexp (_)
717 (let (reports)
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")
723 reports)))
724 reports))
726 (defun org-lint-invalid-block (_)
727 (let ((case-fold-search t)
728 (regexp "^[ \t]*#\\+\\(BEGIN\\|END\\)\\(?::\\|_[^[:space:]]*\\)?[ \t]*")
729 reports)
730 (while (re-search-forward regexp nil t)
731 (let ((name (org-trim (buffer-substring-no-properties
732 (line-beginning-position) (line-end-position)))))
733 (cond
734 ((and (string-prefix-p "END" (match-string 1) t)
735 (not (eolp)))
736 (push (list (line-beginning-position)
737 (format "Invalid block closing line \"%s\"" name))
738 reports))
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\""
745 name))
746 reports)))))
747 reports))
749 (defun org-lint-invalid-keyword-syntax (_)
750 (let ((regexp "^[ \t]*#\\+\\([^[:space:]:]*\\)\\(?: \\|$\\)")
751 (exception-re
752 (format "[ \t]*#\\+%s\\(\\[.*\\]\\)?:\\(?: \\|$\\)"
753 (regexp-opt org-element-dual-keywords)))
754 reports)
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)
759 (save-excursion
760 (beginning-of-line)
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))
764 reports))))
765 reports))
767 (defun org-lint-extraneous-element-in-footnote-section (ast)
768 (org-element-map ast 'headline
769 (lambda (h)
770 (and (org-element-property :footnote-section-p h)
771 (org-element-map (org-element-contents h)
772 (org-remove-if
773 (lambda (e)
774 (memq e '(comment comment-block footnote-definition
775 property-drawer section)))
776 org-element-all-elements)
777 (lambda (e)
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)
786 (lambda (h)
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)
794 (let* ((reports)
795 (verify
796 (lambda (datum language headers)
797 (let ((allowed
798 ;; If LANGUAGE is specified, restrict allowed
799 ;; headers to both LANGUAGE-specific and default
800 ;; ones. Otherwise, accept headers from any loaded
801 ;; language.
802 (append
803 org-babel-header-arg-names
804 (cl-mapcan
805 (lambda (l)
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))))
814 (cond
815 ((not (string-prefix-p ":" h))
816 (push
817 (list p
818 (format "Missing colon in header argument \"%s\"" h))
819 reports))
820 ((assoc-string (substring h 1) allowed))
821 (t (push (list p (format "Unknown header argument \"%s\"" h))
822 reports)))))))))
823 (org-element-map ast '(babel-call inline-babel-call inline-src-block keyword
824 node-property src-block)
825 (lambda (datum)
826 (pcase (org-element-type datum)
827 ((or `babel-call `inline-babel-call)
828 (funcall verify
829 datum
831 (cl-mapcan #'org-babel-parse-header-arguments
832 (list
833 (org-element-property :inside-header datum)
834 (org-element-property :end-header datum)))))
835 (`inline-src-block
836 (funcall verify
837 datum
838 (org-element-property :language datum)
839 (org-babel-parse-header-arguments
840 (org-element-property :parameters datum))))
841 (`keyword
842 (when (string= (org-element-property :key datum) "PROPERTY")
843 (let ((value (org-element-property :value datum)))
844 (when (string-match "\\`header-args\\(?::\\(\\S-+\\)\\)?\\+? *"
845 value)
846 (funcall verify
847 datum
848 (match-string 1 value)
849 (org-babel-parse-header-arguments
850 (substring value (match-end 0))))))))
851 (`node-property
852 (let ((key (org-element-property :key datum)))
853 (when (let ((case-fold-search t))
854 (string-match "\\`HEADER-ARGS\\(?::\\(\\S-+\\)\\)?\\+?"
855 key))
856 (funcall verify
857 datum
858 (match-string 1 key)
859 (org-babel-parse-header-arguments
860 (org-element-property :value datum))))))
861 (`src-block
862 (funcall verify
863 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))))))))
868 reports))
870 (defun org-lint-wrong-header-value (ast)
871 (let (reports)
872 (org-element-map ast
873 '(babel-call inline-babel-call inline-src-block src-block)
874 (lambda (datum)
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:"
880 language))))
881 (and (boundp v) (symbol-value v))))
882 org-babel-common-header-args-w-values))
883 (datum-header-values
884 (org-babel-process-params
885 (apply
886 #'org-babel-merge-params
887 org-babel-default-header-args
888 (and language
889 (let ((v (intern (concat "org-babel-default-header-args:"
890 language))))
891 (and (boundp v) (symbol-value v))))
892 (append
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))
897 (list
898 (org-babel-parse-header-arguments
899 (org-trim
900 (pcase type
901 (`src-block
902 (mapconcat
903 #'identity
904 (cons (org-element-property :parameters datum)
905 (org-element-property :header datum))
906 " "))
907 (`inline-src-block
908 (or (org-element-property :parameters datum) ""))
910 (concat
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))
920 groups-alist)
921 (dolist (v (if (stringp values) (org-split-string values)
922 (list values)))
923 (let ((valid-value nil))
924 (catch 'exit
925 (dolist (group allowed-values)
926 (cond
927 ((not (funcall
928 (if (stringp v) #'assoc-string #'assoc)
929 v group))
930 (when (memq :any group)
931 (setf valid-value t)
932 (push (cons group v) groups-alist)))
933 ((assq group groups-alist)
934 (push
935 (list
936 (or (org-element-property :post-affiliated datum)
937 (org-element-property :begin datum))
938 (format
939 "Forbidden combination in header \"%s\": %s, %s"
940 (car header)
941 (cdr (assq group groups-alist))
943 reports)
944 (throw 'exit nil))
945 (t (push (cons group v) groups-alist)
946 (setf valid-value t))))
947 (unless valid-value
948 (push
949 (list
950 (or (org-element-property :post-affiliated datum)
951 (org-element-property :begin datum))
952 (format "Unknown value \"%s\" for header \"%s\""
954 (car header)))
955 reports))))))))))))
956 reports))
959 ;;; Reports UI
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)
969 map)
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
976 `[("Line" 6
977 (lambda (a b)
978 (< (string-to-number (aref (cadr a) 0))
979 (string-to-number (aref (cadr b) 0))))
980 :right-align t)
981 ("Trust" 5 t)
982 ("Warning" 0 nil)])
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
993 (save-excursion
994 (goto-char (point-min))
995 (let ((ast (org-element-parse-buffer))
996 (id 0)
997 (last-line 1)
998 (last-pos 1))
999 ;; Insert unique ID for each report. Replace buffer positions
1000 ;; with line numbers.
1001 (mapcar
1002 (lambda (report)
1003 (list
1004 (incf id)
1005 (apply #'vector
1006 (cons
1007 (progn
1008 (goto-char (car report))
1009 (beginning-of-line)
1010 (prog1 (number-to-string
1011 (incf last-line (count-lines last-pos (point))))
1012 (setf last-pos (point))))
1013 (cdr report)))))
1014 ;; Insert trust level in generated reports. Also sort them
1015 ;; by buffer position in order to optimize lines computation.
1016 (sort (cl-mapcan
1017 (lambda (c)
1018 (let ((trust (symbol-name (org-lint-checker-trust c))))
1019 (mapcar
1020 (lambda (report)
1021 (list (car report) trust (nth 1 report) c))
1022 (save-excursion
1023 (funcall
1024 (intern (format "org-lint-%s"
1025 (org-lint-checker-name c)))
1026 ast)))))
1027 checkers)
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
1049 of current one."
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."
1067 (interactive)
1068 (let ((l (org-lint--current-line)))
1069 (switch-to-buffer-other-window org-lint--source-buffer)
1070 (org-goto-line l)
1071 (org-show-set-visibility 'local)
1072 (recenter)))
1074 (defun org-lint--show-source ()
1075 "Show source line that generated the report at point."
1076 (interactive)
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."
1083 (interactive)
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."
1093 (interactive)
1094 (setf org-lint--local-checkers
1095 (remove (org-lint--current-checker) org-lint--local-checkers))
1096 (org-lint--hide-checker))
1099 ;;; Public function
1101 ;;;###autoload
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."
1112 (interactive "P")
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..."))
1116 (let ((checkers
1117 (pcase arg
1118 (`nil org-lint--checkers)
1119 (`(4)
1120 (let ((category
1121 (completing-read
1122 "Checker category: "
1123 (mapcar #'org-lint-checker-categories org-lint--checkers)
1124 nil t)))
1125 (org-remove-if-not
1126 (lambda (c)
1127 (assoc-string (org-lint-checker-categories c) category))
1128 org-lint--checkers)))
1129 (`(16)
1130 (list
1131 (let ((name (completing-read
1132 "Checker name: "
1133 (mapcar #'org-lint-checker-name org-lint--checkers)
1134 nil t)))
1135 (catch 'exit
1136 (dolist (c org-lint--checkers)
1137 (when (string= (org-lint-checker-name c) name)
1138 (throw 'exit c)))))))
1139 ((pred consp)
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"))))
1149 (provide 'org-lint)
1150 ;;; org-lint.el ends here