-
Notifications
You must be signed in to change notification settings - Fork 0
/
pel-c-preproc.el
246 lines (207 loc) · 8.8 KB
/
pel-c-preproc.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
;;; pel-c-preproc.el --- C Pre-processor specific support. -*- lexical-binding: t; -*-
;; Created : Monday, October 10 2022.
;; Author : Pierre Rouleau <[email protected]>
;; Time-stamp: <2023-01-31 12:23:36 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 implements navigation commands that move across C preprocessor
;; conditional inclusion statements found in C and C++ source code files. It
;; supports nested statements and provides 4 commands.
;;
;; The current implementation does not yet support C++ #elifdef and #elifndef
;; statements, but it supports the C preprocessor statements that were
;; available before.
;;; --------------------------------------------------------------------------
;;; Dependencies:
;;
;;
(require 'pel-syntax) ; use: `pel-syntax-conditional-forward'
;; ; `pel-syntax-conditional-backward'
;;; --------------------------------------------------------------------------
;;; Code:
;;
;; Navigate across #if|#ifdef|#ifndef / #elif| #else / #endif
;; ------------------------------------------------------------
(defconst pel--c-preproc-conditional-regexp
"^[[:blank:]]*#[[:blank:]]*\\(\\(if\\(\\(def\\)\\|\\(ndef\\)\\)*\\)\\|\\(el\\(se\\|if\\)\\)\\|\\(endif\\)\\)"
"Regexp to find C preprocessor conditionals.")
(defconst pel--c-preproc-conditional-group-if 2
"Significant matching group when searching beginning of #if|#ifdef|#ifndef.")
(defconst pel--c-preproc-conditional-else 6
"Significant matching group when searching #else|#elif conditional.")
(defconst pel--c-preproc-conditional-end 8
"Significant matching group when searching #endif conditional.")
;; -----
(defun pel--c-preproc-token-from-match (match-result)
"Return a if, else, end token from search MATCH-RESULT."
;; Result seen by using roup test function below.
;; #if: [0-5] all are set: use 5.
;;
;; #ifdef: [0-9] all are set: use 5 to identify an if beginning,
;; 8 to identify #ifdef
;;
;; #ifndef: [0-11] all except 8 and 9 are set.
;; Use 5 to identify an if beginning.
;; Use 11 to identify #ifndef
;;
;; #else: [0-15] set:0,1,2,3,12,13,14,15
;; Use 15 to identify else
;; #elif: same as #else
;;
;; #endif: [0-17] set:0,1,2,3,16,17: Use 17 to identify end
(cond
((nth 5 match-result) 'if)
((nth 15 match-result) 'else)
((nth 17 match-result) 'end)
(t (error "Invalid search result"))))
(defun pel--c-preproc-token-pos-from-match (match-result)
"Return token position from search MATCH-RESULT."
(let ((pos
(cond
((nth 5 match-result) (nth 0 match-result)) ; #if | #ifdef | #ifndef
((nth 15 match-result) (nth 15 match-result)) ; #else | #elif
((nth 17 match-result) (nth 17 match-result)) ; #endif
(t (error "Invalid search result")))))
(message "==> %S from %S" pos match-result)
pos))
;; (defun roup ()
;; "Test for development"
;; (interactive)
;; (let ((msg-data nil))
;; (re-search-forward pel--c-preproc-conditional-regexp nil :noerror)
;; (setq msg-data (match-data))
;; (message "%s at %S"
;; (pel--c-preproc-token-from-match msg-data)
;; msg-data)))
;; -------
;;-pel-autoload
(defun pel-c-preproc-forward-conditional (&optional to-else)
"Move point forward to matching #endif.
If a command prefix TO-ELSE is specified, move point forward
after the matching else statement instead.
On success, push the original position on the mark ring and
return the new position. On error, issue user error on mismatch."
(interactive "^P")
(beginning-of-line nil)
(pel-syntax-conditional-forward
pel--c-preproc-conditional-regexp
(function pel--c-preproc-token-from-match)
(function pel--c-preproc-token-pos-from-match)
0
to-else
(if to-else
"#else|#elif"
"#endif")))
;;-pel-autoload
(defun pel-c-preproc-backward-conditional (&optional to-else)
"Move point backward to matching #if, #ifdef or #ifndef.
If a command prefix TO-ELSE is specified, move point backward
after the matching else statement instead.
On success, push the original position on the mark ring and
return the new position. On error, issue user error on mismatch."
(interactive "^P")
(end-of-line nil)
(pel-syntax-conditional-backward
pel--c-preproc-conditional-regexp
(function pel--c-preproc-token-from-match)
(function pel--c-preproc-token-pos-from-match)
0
to-else
(if to-else
"#else|#elif"
"#if|#ifdef|#ifndef")))
;;-pel-autoload
(defun pel-c-preproc-outward-forward-conditional (&optional nest-count)
"Move point forward, outward to the end of current #if|ifdef|ifndef block.
By default move 1 nest level outward. A larger count can be
specified with optional NEST-COUNT numeric argument.
On success, push the original position on the mark ring and
return the new position. On error, issue user error on mismatch."
(interactive "^p")
(pel-syntax-conditional-forward
pel--c-preproc-conditional-regexp
(function pel--c-preproc-token-from-match)
(function pel--c-preproc-token-pos-from-match)
(abs nest-count)
nil
"#endif"))
;;-pel-autoload
(defun pel-c-preproc-outward-backward-conditional (&optional nest-count)
"Move point outward to the beginning of current #if|ifdef|ifndef block.
By default move 1 nest level outward. A larger count can be
specified with optional NEST-COUNT numeric argument.
On success, push the original position on the mark ring and
return the new position. On error, issue user error on mismatch."
(interactive "^p")
(pel-syntax-conditional-backward
pel--c-preproc-conditional-regexp
(function pel--c-preproc-token-from-match)
(function pel--c-preproc-token-pos-from-match)
(abs nest-count)
nil
"#if|#ifdef|#ifndef"))
;; ---------------------------------------------------------------------------
(defun pel--projectile-multi-occur (regexp &optional nlines)
"Do a `multi-occur' in the project's buffers for specified REGEXP.
Show NLINES of context if specified, otherwise default to
`list-matching-lines-default-context-lines' user-option."
(if (and pel-use-projectile
(fboundp 'projectile-acquire-root)
(fboundp 'projectile-project-buffers))
(let ((project (projectile-acquire-root)))
(multi-occur (projectile-project-buffers project)
regexp
nlines))
(user-error "Projectile is not available.
Activate projectile first with F11 F8 F8")))
;;-pel-autoload
(defun pel-c-preproc-conditionals-occur (&optional nlines use-projectile)
"Show C preprocessor conditional statements inside an occur buffer.
Each line is displayed with NLINES before and after, or -NLINES
before if NLINES is negative.
NLINES defaults to `list-matching-lines-default-context-lines'.
If a region is defined the search is restricted to the region.
If USE-PROJECTILE is not nil and projectile is available (via
`pel-use-projectile'), then perform the search using the
`projectile-multi-occur' function. "
(interactive "^P")
(if (and use-projectile
pel-use-projectile
(fboundp 'projectile-multi-occur))
(pel--projectile-multi-occur
pel--c-preproc-conditional-regexp
nlines)
(occur pel--c-preproc-conditional-regexp
nlines
(when (use-region-p)
(region-bounds)))))
;;-pel-autoload
(defun pel-c-preproc-conditionals-multi-occur (&optional nlines)
"Show C preprocessor conditional statements of project files in occur buffer.
Each line is displayed with NLINES before and after, or -NLINES
before if NLINES is negative.
NLINES defaults to `list-matching-lines-default-context-lines'.
If a region is defined the search is restricted to the region.
If projectile is not available, just search in current buffer."
(interactive "P")
(pel-c-preproc-conditionals-occur nlines :use-projectile))
;;; --------------------------------------------------------------------------
(provide 'pel-c-preproc)
;;; pel-c-preproc.el ends here