forked from tromey/gdb-refactoring-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cxx-rewrite.el
77 lines (67 loc) · 2.06 KB
/
cxx-rewrite.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
;;; This rewrites C++ keywords in the gdb source.
(defconst dir-to-scan (pop argv))
(unless (file-directory-p dir-to-scan)
(error "Usage: emacs --script cxx-rewrite.el DIR"))
(defconst cxxrewrite-dirs '("." "cli" "mi" "tui" "python" "guile" "nat" "common" "gdbserver" "compile"))
(defun cxxrewrite-files ()
(apply #'nconc
(mapcar (lambda (dir)
(directory-files (expand-file-name dir dir-to-scan)
t "\\.[chy]$"))
cxxrewrite-dirs)))
(defconst cxxrewrite-keywords
'(("bool" . "boolean")
("catch" . "catcher")
("class" . "theclass")
("const_cast" . nil)
("delete" . "to_delete")
("dynamic_cast" . nil)
("explicit" . "is_explicit")
("export" . "to_export")
("false" . nil)
("friend" . nil)
("mutable" . "mutable_obj")
("namespace" . "the_namespace")
("new" . "newobj")
("noexcept" . nil)
("nullptr" . nil)
("operator" . "oper")
("private" . "priv")
("protected" . nil)
("public" . "is_public")
("reinterpret_cast" . nil)
("static_cast" . nil)
("template" . "templ")
("this" . "self")
("throw" . nil)
("true" . nil)
("try" . "attempt")
("typename" . "type_name")
("typeid" . "type_id")
("using" . "using_decl")
("virtual" . nil)))
(defconst cxxrewrite-keywords-rx
(regexp-opt (mapcar #'car cxxrewrite-keywords) 'symbols))
(defun cxxrewrite-symbols ()
(goto-char (point-min))
(let ((case-fold-search nil))
(while (re-search-forward cxxrewrite-keywords-rx nil t)
(unless (nth 8 (syntax-ppss))
(let ((new-val (cdr (assoc (match-string 0) cxxrewrite-keywords))))
(if new-val
(replace-match new-val t t)
(message "%s:%d: could not replace %s"
(buffer-file-name)
(line-number-at-pos)
(match-string 0))))))))
(defun cxxrewrite-rewrite-one (file)
(find-file file)
(unless buffer-read-only
(cxxrewrite-symbols)
(if (buffer-modified-p)
(save-buffer))))
(defun cxxrewrite-rewrite ()
(dolist (file (cxxrewrite-files))
(message "Processing %s" file)
(cxxrewrite-rewrite-one file)))
(cxxrewrite-rewrite)