-
Notifications
You must be signed in to change notification settings - Fork 2
/
run-command-recipes-cpp.el
160 lines (134 loc) · 5.88 KB
/
run-command-recipes-cpp.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
;;; run-command-recipes-cpp.el --- Recipe of `run-command' for C++ -*- lexical-binding: t; -*-
;; Author: semenInRussia <[email protected]>
;; Version: 0.1.0
;; Keywords: extensions run-command
;; Homepage: https://github.com/semenInRussia/emacs-run-command-recipes
;; URL: https://github.com/semenInRussia/emacs-run-command-recipes/blob/main/docs/cpp.md
;; 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/>.
;;; Commentary:
;; For use this code put the following to your Emacs configuration:
;;
;; (run-command-recipes-use 'cpp)
;;
;;; Code:
(require 'run-command-recipes-project)
(defcustom run-command-recipes-cpp-major-modes
'(c++-mode)
"List of major modes in which the recipe for C++ should work."
:type '(repeat symbol)
:group 'run-command-recipes)
(defcustom run-command-recipes-cpp-clang "clang++"
"Command line to execute/compile C++ code using Clang compiler."
:type 'string
:group 'run-command-recipes)
(defcustom run-command-recipes-cpp-gcc "g++"
"Command line to execute/compile C++ code using GCC compiler."
:type 'string
:group 'run-command-recipes)
(defcustom run-command-recipes-cpp-flags "-Wall -Werror"
"String which specify flags which are passed to a C++ compiler."
:type 'string
:group 'run-command-recipes)
(defun run-command-recipes-cpp-p ()
"Return t, when recipe of `run-command' for C++ should work."
(and (buffer-file-name)
(memq major-mode run-command-recipes-cpp-major-modes)))
(defun run-command-recipes-cpp--exe-name (filename &optional dir)
"Return the filename for executable to produce when compile FILENAME C++ file.
Defaults to just chop extension, like main.c => main
Consider that the command will be ran inside DIR"
(concat (or dir "")
(file-name-base filename)))
;; NOTE: that I provide two different recipes functions (for clang and
;; GCC) which are almost the same, but both of them are useful for C.
;;
;; The more naive way is to write one function for clang and GCC, but
;; in this case user can't delete avoid usage one of them. Now you
;; can do anything like:
;;
;; \\(advice-add 'run-command-recipes-cpp-gcc :around #'ignore)
;;
;; and disable GCC support
(defun run-command-recipes-cpp ()
"Support of C to execute the current file using `run-command'.
See either `run-command-recipes-cpp-gcc' or
`run-command-recipes-cpp-clang' for details"
(append (run-command-recipes-cpp-gcc)
(run-command-recipes-cpp-clang)))
(defun run-command-recipes-cpp-gcc ()
"Support of GCC to execute the current C++ file using `run-command'.
Recipe of `run-command' for support of GCC compiler of C++ to execute
the current file with it. See `run-command-recipes' (variable) if
don't know what recipe means.
NOTE that if you prefer the Clang compiler and don't need to see when
`run-command' inside C buffer, then you should use `fset' or
`advice-add' with `ignore' over `run-command-recipes-cpp-gcc'.
\\(advice-add \\='run-command-recipes-cpp-gcc :around #\\='ignore)"
(when (and (executable-find run-command-recipes-cpp-gcc)
(buffer-file-name)
(run-command-recipes-cpp-p))
(let* ((cmd run-command-recipes-cpp-gcc)
(dir (run-command-recipes-project-root))
(exe (run-command-recipes-cpp--exe-name (buffer-file-name) dir)))
(list
(list
:command-name "gpp-compile-and-exec"
:display "G++: compile, execute file"
:working-directory dir
:command-line
(concat cmd " " (buffer-file-name)
" " run-command-recipes-cpp-flags
" -o " exe
" && " exe))
(list
:command-name "gpp-only-compile"
:display "G++: compile file"
:working-directory dir
:command-line
(concat cmd " " (buffer-file-name)
" " run-command-recipes-cpp-flags
" -o " exe))))))
(defun run-command-recipes-cpp-clang ()
"Support of Clang compiler of C++ to run the current file using `run-command'.
Recipe of `run-command' for support of Clang compiler of C++ to
execute the current file with it. See `run-command-recipes'
\\(variable) if don't know what recipe means.
NOTE that if you prefer the GCC compiler and don't need to see when
`run-command' inside C buffer, then you should use `fset' or
`advice-add' with `ignore' over `run-command-recipes-cpp-clang'.
\\(advice-add \\='run-command-recipes-cpp-clang :around #\\='ignore)"
(when (and (executable-find run-command-recipes-cpp-clang)
(buffer-file-name)
(run-command-recipes-cpp-p))
(let* ((cmd run-command-recipes-cpp-clang)
(dir (run-command-recipes-project-root))
(exe (run-command-recipes-cpp--exe-name (buffer-file-name) dir)))
(list
(list
:command-name "clangpp-compile-and-exec"
:display "Clang++: compile, execute file"
:working-directory dir
:command-line
(concat cmd " " (buffer-file-name)
" " run-command-recipes-cpp-flags
" -o " exe
" && " exe))
(list
:command-name "clangpp-only-compile"
:display "Clang++: compile file"
:working-directory dir
:command-line
(concat cmd " " (buffer-file-name)
" " run-command-recipes-cpp-flags
" -o " exe))))))
(provide 'run-command-recipes-cpp)
;;; run-command-recipes-cpp.el ends here