-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpel--syntax-macros.el
107 lines (86 loc) · 3.7 KB
/
pel--syntax-macros.el
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
;;; pel--syntax-macros.el --- Syntax helper macros. -*- lexical-binding: t; -*-
;; Created : Saturday, October 15 2022.
;; Author : Pierre Rouleau <[email protected]>
;; Time-stamp: <2023-11-28 14:56:09 EST, updated by Pierre Rouleau>
;; This file is part of the PEL package.
;; This file is not part of GNU Emacs.
;; Copyright (C) 2022, 2023 Pierre Rouleau
;;
;; 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 <http://www.gnu.org/licenses/>.
;;; --------------------------------------------------------------------------
;;; Commentary:
;;
;; This file provides a set of macros used to document the use of the list
;; returned by the `syntax-ppss' function.
;;
;; Internal Macros for self documenting code:
;; - `pel--inside-string-p'
;; - `pel--inside-block-p'
;; - `pel--inside-comment-p'
;; - `pel--open-parens-pos'
;; Predicate utilities:
;; - `pel-inside-block-p'
;; - `pel-inside-comment-p'
;; - `pel-inside-string-p'
;; - `pel-inside-code-p'
;;; --------------------------------------------------------------------------
;;; Dependencies:
;;
;;
;;; --------------------------------------------------------------------------
;;; Code:
;;
;; Macros for self documenting code
;; --------------------------------
;;
;; These macros are useful to reduce the need to call functions that create a
;; let-bound variable while providing meaningful names.
(defmacro pel--inside-string-p (syntax)
"Return non-nil if point is inside string according to SYNTAX list."
`(nth 3 ,syntax))
(defmacro pel--inside-block-p (syntax)
"Return non-nil if point is inside matching pair block according to SYNTAX."
`(> (nth 0 ,syntax) 0))
(defmacro pel--inside-comment-p (syntax)
"Return non-nil if point is inside comment according to SYNTAX."
`(nth 4 ,syntax))
(defmacro pel--open-parens-pos (syntax)
"Return list of position of open parens according to SYNTAX.
Each integer in the list is the position of the open parens,
starting with the outermost one. Return nil if not outside parens."
`(nth 9 ,syntax))
;; Predicate utilities
;; -------------------
(defun pel-inside-block-p (&optional pos)
"Return non-nil if POS, or point, is between a code matched-pair block.
Return nil otherwise. Return nil when point is inside string or comment."
(let ((syntax (syntax-ppss pos)))
(unless (pel--inside-string-p syntax)
(pel--inside-block-p syntax))))
(defun pel-inside-comment-p (&optional pos)
"Return non-nil if POS, or point, is inside a comment, nil otherwise.
When inside comment, return t if inside non-nestable comment,
otherwise return an integer indicating the current comment nesting."
(pel--inside-comment-p (syntax-ppss pos)))
(defun pel-inside-string-p (&optional pos)
"Return non-nil if POS, or point, is inside a string, nil otherwise."
(pel--inside-string-p (syntax-ppss pos)))
(defun pel-inside-code-p (&optional pos)
"Return t if POS, or point, is inside code; not in string nor comment."
(let ((syntax-sexp (syntax-ppss pos)))
(and (not (nth 3 syntax-sexp))
(not (nth 4 syntax-sexp)))))
;;; --------------------------------------------------------------------------
(provide 'pel--syntax-macros)
;;; pel--syntax-macros.el ends here