forked from Sarcasm/irony-mode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
irony-cdb.el
142 lines (110 loc) · 4.63 KB
/
irony-cdb.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
;;; irony-cdb.el --- compilation databases support for irony
;; Copyright (C) 2012-2014 Guillaume Papin
;; Author: Guillaume Papin <[email protected]>
;; Keywords: c, convenience, tools
;; 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 defines the compilation database interface of irony-mode.
;;
;; Note:For compilation database that looks for a specific file, such as
;; .clang_complete or compile_commands.json, favor `locate-dominating-file' to a
;; handwritten logic if possible as it may be configured by the user to do "the
;; Right Thing (TM)". See `locate-dominating-stop-dir-regexp'.
;;
;;; Code:
(require 'irony)
(require 'cl-lib)
(autoload 'irony-cdb-clang-complete "irony-cdb-clang-complete")
(autoload 'irony-cdb-json "irony-cdb-json")
(autoload 'irony-cdb-libclang "irony-cdb-libclang")
;;
;; Customizable variables
;;
(defgroup irony-cdb nil
"Irony's compilation database interface."
:group 'irony)
(defcustom irony-cdb-compilation-databases '(irony-cdb-clang-complete
irony-cdb-libclang
irony-cdb-json)
"List of active compilation databases.
The compilation database should respond for the following commands:
`get-compile-options': Takes no argument. This function finds the
compile options used for the current buffer. It must return a
list of cons where the first element is a set of compile options
and the second element the working directory expected for these
commands. The compilation database should return an empty list
for files that it cannot handle."
:type '(repeat function)
:group 'irony-cdb)
;;
;; Internal variables
;;
(defvar-local irony-cdb--compilation-database nil)
;;
;; Irony Compilation Database Interface
;;
;;;###autoload
(defun irony-cdb-autosetup-compile-options ()
(interactive)
(irony--awhen (irony-cdb--autodetect-compile-options)
(setq irony-cdb--compilation-database (nth 0 it))
(irony-cdb--update-compile-options (nth 1 it) (nth 2 it))))
;;;###autoload
(defun irony-cdb-menu ()
(interactive)
(let ((compilation-database irony-cdb--compilation-database)
(working-directory irony--working-directory)
(compile-options irony--compile-options))
(save-excursion
(save-window-excursion
(delete-other-windows)
(let ((buffer (get-buffer-create "*Irony/Compilation DB Menu*")))
(with-current-buffer buffer
(erase-buffer)
(if (null compilation-database)
(insert "No compilation database in use.\n")
(insert (format "Compilation Database: %s\n\n"
(symbol-name compilation-database)))
(insert (format " Working Directory: %s\n" working-directory))
(insert (format " Compile Options: %s\n"
(mapconcat 'identity compile-options " "))))
(insert "\n[q] to quit"))
(let ((pop-up-windows t))
(display-buffer buffer t))
(fit-window-to-buffer (get-buffer-window buffer))
(irony--read-char-choice "Irony CDB Buffer" (list ?q)))))
;; clear `read-char-choice' prompt
(message "")))
;;
;; Functions
;;
(defun irony-cdb--update-compile-options (compile-options
&optional working-directory)
(setq irony--compile-options compile-options
irony--working-directory working-directory))
(defun irony-cdb--autodetect-compile-options ()
(catch 'found
(dolist (compilation-database irony-cdb-compilation-databases)
(irony--awhen (funcall compilation-database 'get-compile-options)
(throw 'found (list compilation-database
(caar it)
(cdar it)))))))
(defun irony-cdb--remove-compiler-from-flags (flags)
"Removes the compiler from flags read from a compilation database.
In the future this might get more involved."
(cdr flags))
(provide 'irony-cdb)
;; Local Variables:
;; byte-compile-warnings: (not cl-functions)
;; End:
;;; irony-cdb.el ends here