-
Notifications
You must be signed in to change notification settings - Fork 0
/
pel-make.el
348 lines (299 loc) · 12.8 KB
/
pel-make.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
;;; pel-make.el --- Makefile editing support. -*- lexical-binding: t; -*-
;; Created : Friday, January 15 2021.
;; Author : Pierre Rouleau <[email protected]>
;; Time-stamp: <2023-01-31 10:10:16 EST, updated by Pierre Rouleau>
;; This file is part of the PEL package.
;; This file is not part of GNU Emacs.
;; Copyright (C) 2021, 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 commands to navigate inside make files. These
;; complements what the standard Emacs make-mode.el provides. The provided
;; commands are:
;;
;; * `pel-make-next-macro'
;; * `pel-make-previous-macro'
;;
;; These 2 commands navigate across the macro definition statements. They
;; support shift marking and can be used to count the number of make macro
;; statements.
;;; --------------------------------------------------------------------------
;;; Dependencies:
;;
;;
(require 'pel--base) ; use: `pel-count-string'
(require 'pel-syntax) ; use: `pel-syntax-conditional-forward'
;; ; `pel-syntax-conditional-backward'
(require 'make-mode) ; for nmake support @ end of file
;;; --------------------------------------------------------------------------
;;; Code:
;;
(defconst pel-make-macro-regxp "^[ \t]*\\(\\_<[_[:alpha:]][_[:alnum:]]+\\_>\\) *[?:]*="
"Regexp used to search make file macro definitions.")
;; --
;;-pel-autoload
(defun pel-make-next-macro (&optional n silent dont-push-mark)
"Move to the beginning of next N make file macro definition statement.
The function skips over comments.
If no valid form is found, don't move point, issue an error
describing the failure unless SILENT is non-nil, in which case
the function returns nil on error and non-nil on success.
The error message states the number of instanced searched, the
regexp used and the number of instances found.
On success, the function push original position on the mark ring
unless DONT-PUSH-MARK is non-nil.
The command support shift-marking."
(interactive "^p")
(if (< n 0)
(pel-make-previous-macro (abs n))
(let ((start-pos (point))
(count 0))
(condition-case err
(progn
(dotimes (_ n)
(end-of-line) ; don't stay on current definition line if cursor is at bol
(re-search-forward pel-make-macro-regxp)
(setq count (1+ count)))
(back-to-indentation)
(unless dont-push-mark
(push-mark start-pos))
t)
(search-failed
;; restore original position when search failed
(goto-char start-pos)
(unless silent
(user-error "Found only %d of requested %s:\n%s"
count
(pel-count-string n "macro definition statement")
err)))))))
;;-pel-autoload
(defun pel-make-previous-macro (&optional n silent dont-push-mark)
"Move to the beginning of previous N make file macro definition statement.
The function skips over comments.
If no valid form is found, don't move point, issue an error
describing the failure unless SILENT is non-nil, in which case
the function returns nil on error and non-nil on success.
The error message states the number of instanced searched, the
regexp used and the number of instances found.
On success, the function push original position on the mark ring
unless DONT-PUSH-MARK is non-nil.
The command support shift-marking."
(interactive "^p")
(if (< n 0)
(pel-make-previous-macro (abs n))
(let ((start-pos (point))
(count 0))
(condition-case err
(progn
(dotimes (_ n)
(re-search-backward pel-make-macro-regxp)
(setq count (1+ count)))
(back-to-indentation)
(unless dont-push-mark
(push-mark start-pos))
t)
(search-failed
;; restore original position when search failed
(goto-char start-pos)
(unless silent
(user-error "Found only %d of requested %s:\n%s"
count
(pel-count-string n "macro definition statement")
err)))))))
;; ---------------------------------------------------------------------------
;; Navigate across if/else/endif
;; -----------------------------
;; G1-------------------------------------------------------------------------------------------------------------------|
;; G2---------------------------------------------------------------------------------------------------------------|
;; end---> g3------------------------|
;; c4--------------|
;; else--> g5------------------------|
;; c6---------------|
;; G7------------------------------------------------|
;; if*---> G8-------------------------------|
;; g9-----| g10----| g11-----|
;; "^[[:blank:]]*\(\(\(endif\([#[:blank:]].*\)?\)\|\(else\([#[:blank:]].*\)?\)\|\(\(\(ifdef\)\|\(ifeq\)\|\(ifneq\)\)[[:blank:]].*\)\)\)$"
(defconst pel--make-conditional-regexp
"^[[:blank:]]*\\(\\(\\(endif\\([#[:blank:]].*\\)?\\)\\|\\(else\\([#[:blank:]].*\\)?\\)\\|\\(\\(\\(ifn*def\\)\\|\\(ifeq\\)\\|\\(ifneq\\)\\)[[:blank:]].*\\)\\)\\)$"
"Regexp to find make conditionals.")
(defconst pel--make-conditional-group-if 8
"Significant matching group when searching beginning of make conditional.")
(defconst pel--make-conditional-else 5
"Significant matching group when searching else of make conditional.")
(defconst pel--make-conditional-end 3
"Significant matching group when searching end of make conditional.")
(defun pel--make-token-from-match (match-result)
"Return a if, else, end token from search MATCH-RRESULT."
(cond
((nth 15 match-result) 'if)
((nth 11 match-result) 'else)
((nth 7 match-result) 'end)
(t (error "Invalid search result"))))
(defun pel--make-token-pos-from-match (match-result)
"Return token position from search MATCH-RESULT."
(cond
((nth 15 match-result) (or
(nth 16 match-result) ; ifdef
(nth 18 match-result) ; ifeq
(nth 20 match-result))) ; ifneq
((nth 11 match-result) (nth 10 match-result)) ; else
((nth 7 match-result) (nth 4 match-result)) ; endif
(t (error "Invalid search result"))))
;; (defun roup ()
;; ""
;; (interactive)
;; (let ((msg-data nil))
;; (re-search-forward pel--make-conditional-regexp nil :noerror)
;; (setq msg-data (match-data))
;; (message "%s at %S"
;; (pel--make-token-from-match msg-data)
;; (pel--make-token-pos-from-match msg-data))))
(defun pel--after-else-p ()
"Return t if point is on the same line and after a `else'."
(let ((original-pos (point))
(else-pos nil))
(save-excursion
(let ((eol-pos (progn
(end-of-line nil)
(point))))
(beginning-of-line nil)
(when (re-search-forward
"[[:blank:]]?\\(else\\)\\([#[:blank:]].*\\)?$"
eol-pos
:no-error)
;; store position of the end of `else' in else-pos
(setq else-pos (nth 3 (match-data))))))
(and else-pos
(>= original-pos else-pos))))
;;-pel-autoload
(defun pel-make-forward-conditional (&optional to-else)
"Move point forward to matching end of make conditional.
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--make-conditional-regexp
(function pel--make-token-from-match)
(function pel--make-token-pos-from-match)
0
to-else
(if to-else
"else"
"endif statement"))
(forward-word 1))
(defun pel-make-backward-conditional (&optional to-else)
"Move point backward to matching beginning of make conditional.
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")
(pel-syntax-conditional-backward
pel--make-conditional-regexp
(function pel--make-token-from-match)
(function pel--make-token-pos-from-match)
0
to-else
(if to-else
"else statement"
"beginning of if statement")))
;;-pel-autoload
(defun pel-make-outward-forward-conditional (&optional nest-count)
"Move point forward, outward to end of current if statement.
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--make-conditional-regexp
(function pel--make-token-from-match)
(function pel--make-token-pos-from-match)
(abs nest-count)
nil
"endif statement")
(forward-word 1))
(defun pel-make-outward-backward-conditional (&optional nest-count)
"Move point backward, outward to beginning of current if statement.
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--make-conditional-regexp
(function pel--make-token-from-match)
(function pel--make-token-pos-from-match)
(abs nest-count)
nil
"beginning of if statement"))
;; ---------------------------------------------------------------------------
;;-pel-autoload
(defun pel-make-conditionals-occur (&optional nlines)
"Show make 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."
(interactive "^P")
(occur pel--make-conditional-regexp
nlines
(when (use-region-p)
(region-bounds))))
;; ---------------------------------------------------------------------------
;; NMake format support
;; --------------------
;; Credit:
;; Original code taken from: https://www.emacswiki.org/emacs/MakefileMode
;; Author: https://www.emacswiki.org/emacs/RolfUnger
;; Updates as per Visual Studio 2019 specs for Nmake
;; TODO: the nmake specs state that the ! must be at the beginning of the line
;; but it can be followed by spaces and tab characters. Unfortunately the
;; makefile-mode code does not support regexp.
;; Change the code to accept regexp and support the full syntax.
(defconst makefile-nmake-statements
`("!cmdswitches" "!CMDSWITCHES"
"!else" "!ELSE"
"!elseif" "!ELSEIF"
"!elseifdef" "!ELSEIFDEF"
"!endif" "!ENDIF"
"!error" "!ERROR"
"!if" "!IF"
"!ifdef" "!IFDEF"
"!ifndef" "!IFNDEF"
"!include" "!INCLUDE"
"!message" "!MESSAGE"
"!undef" "!UNDEF"
,@makefile-statements)
"List of keywords understood by nmake.")
(defconst makefile-nmake-font-lock-keywords
(makefile-make-font-lock-keywords
makefile-var-use-regex
makefile-nmake-statements
t))
;;-pel-autoload
(define-derived-mode makefile-nmake-mode makefile-mode "Nmake"
"An adapted `makefile-mode' that knows about nmake."
(setq font-lock-defaults
`(makefile-nmake-font-lock-keywords ,@(cdr font-lock-defaults))))
(define-key makefile-mode-map "\C-c\C-m\C-n" 'makefile-nmake-mode)
;;; --------------------------------------------------------------------------
(provide 'pel-make)
;;; pel-make.el ends here