aboutsummaryrefslogtreecommitdiff
path: root/emacs/consfigurator.el.in
blob: d86690d39bed03f55e13272ccd979edbf86829c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
;;; consfigurator.el --- Utilities for working with Consfigurator consfigs

;; Author: Sean Whitton <spwhitton@spwhitton.name>
;; Version: 1.2.3

;; Copyright (C) 2021, 2023  Sean Whitton
;; Copyright (C) 2002-2023  Free Software Foundation, Inc.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'lisp-mode)
(require 'perl-mode)

;;;###autoload
(defun activate-consfigurator-indentation-hints ()
  "Activate indentation hints for Consfigurator properties.

Note that these hints assume you're using the recommended consfig
defpackage, where the :consfigurator package is used unqualified,
and packages providing properties have local nicknames
corresponding to the final dot-delimited component of their names."
  (interactive)
  ;; Properties
  @putforms@

  ;; Other operators
  (put 'with-unapply 'common-lisp-indent-function '(&body))
  (put 'os:typecase 'common-lisp-indent-function '(&rest (&whole 2 &rest 1)))
  (put 'os:etypecase 'common-lisp-indent-function '(&rest (&whole 2 &rest 1))))

;; Based on `perl-syntax-propertize-special-constructs', which see.
(defun consfigurator--finish-propertize-qq (limit)
  (let ((state (syntax-ppss)))
    ;; Check we're within our special quotation.
    (unless (or (not (nth 3 state))
		(and (characterp (nth 3 state))
		     (null (get-text-property (nth 8 state) 'syntax-table))))
      (let* ((startpos (point))
	     (twoargs
	      (save-excursion
		(let (parse-sexp-lookup-properties)
		  (goto-char (nth 8 state))
		  (skip-syntax-backward "w ")
		  (member
		   (buffer-substring (point) (progn (forward-word-strictly 1)
						    (point)))
		   '("tr" "s" "y")))))
	     (char (char-after (nth 8 state)))
	     middle
	     (close (cdr (assq char perl-quote-like-pairs)))
	     (st (perl-quote-syntax-table char)))
	(when (with-syntax-table st
		(if close
		    (condition-case nil
			(progn (goto-char (1+ (nth 8 state)))
			       (up-list 1)
			       t)
		      (scan-error (goto-char startpos) nil))
		  (not (or (nth 8 (parse-partial-sexp
				   (if twoargs (1+ (nth 8 state)) (point))
				   limit nil nil state 'syntax-table))
			   (and twoargs (not close)
				(setq middle (point))
				(nth 8 (parse-partial-sexp
					(point) limit
					nil nil state 'syntax-table)))))))
	  (if (and middle (eq char ?\"))
	      (put-text-property (1- middle) middle 'syntax-table
				 (string-to-syntax "."))
	    (put-text-property (1- (point)) (point) 'syntax-table
			       (if close (string-to-syntax "|")
				 (string-to-syntax "\""))))
	  (put-text-property (nth 8 state) (point) 'syntax-multiline t)
	  (when (and twoargs close (< (point) limit))
	    (put-text-property (point) (1+ (point)) 'syntax-table
			       (if (assq (char-after) perl-quote-like-pairs)
				   (string-to-syntax "|")
				 (string-to-syntax "\"")))
	    (forward-char 1)
	    (consfigurator--finish-propertize-qq limit)))))))

(defun consfigurator-syntax-propertize-function (start end)
  "`syntax-propertize-function' for (some of) Consfigurator's readtable.

Modes that use this should add `syntax-propertize-multiline' to
`syntax-propertize-extend-region-functions'."
  (goto-char start)
  (consfigurator--finish-propertize-qq end)
  (cl-flet ((in-string-or-comment-p ()
	      (nth 8 (save-excursion (syntax-ppss (match-beginning 1))))))
    (let (case-fold-search)
      (funcall
       (syntax-propertize-rules
	;; Also recognise Let Over Lambda's #~ and a #!~ negated version.
	;; We might want to add these to Consfigurator's readtable.
	((rx (group-n 1
	       (: ?# (| (: ?? (group-n 2 (opt (| "rx" "r" "x"))))
			(: (opt ?!) ?~ (group-n 2 (opt (| "tr" ?m ?s ?y)))))))
	     (group-n 3 nonl))
	 (1 (and (not (in-string-or-comment-p)) (string-to-syntax "'")))
	 ;; Based on code in one branch of `perl-syntax-propertize-function'.
	 (3 (ignore
	     (or (in-string-or-comment-p)
		 (let* ((beg (match-beginning 3))
			(char (char-after beg))
			(prop
			 (cond ((assq char perl-quote-like-pairs)
				(string-to-syntax "|"))
			       ((and (eq char ?\")
				     (member (match-string 2) '("" "m")))
				nil)
			       (t
				(string-to-syntax "\"")))))
		   (put-text-property beg (1+ beg) 'syntax-table prop)
		   (consfigurator--finish-propertize-qq-heredoc end)))))))
       (point) end))))

;;;###autoload
(define-derived-mode consfigurator-lisp-mode lisp-mode "Lisp"
  "Derivative of `lisp-mode' for files using Consfigurator's readtable."
  (add-hook 'syntax-propertize-extend-region-functions
            #'syntax-propertize-multiline t t)
  (setq-local syntax-propertize-function
	      #'consfigurator-syntax-propertize-function))

(provide 'consfigurator)

;;; consfigurator.el ends here