-
Notifications
You must be signed in to change notification settings - Fork 2
/
init-c.el
78 lines (63 loc) · 2.6 KB
/
init-c.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
;;; init-c.el --- C, C++, Java, etc
;; Xcode
(require 'cc-mode)
(require 'xcode)
(define-key objc-mode-map [(meta r)] 'xcode-compile)
(define-key objc-mode-map [(meta K)] 'xcode-clean)
(add-hook 'c-mode-common-hook
(lambda()
(c-set-style "stroustrup")
(local-set-key (kbd "C-c <right>") 'hs-show-block)
(local-set-key (kbd "C-c <left>") 'hs-hide-block)
(local-set-key (kbd "C-c <up>") 'hs-hide-all)
(local-set-key (kbd "C-c <down>") 'hs-show-all)
(local-set-key (kbd "M-O") 'ff-find-other-file)
(hs-minor-mode t))) ; Hide and show blocks
;; Make the compilation window small
(setq compilation-window-height 12)
;; Setup the compilation window to go away on successful compiles
(setq compilation-finish-function
(lambda (buf str)
(if (string-match "exited abnormally" str)
;; Errors
(message "Compile failed; press C-x ` to visit errors")
;; Else -- make the compilation window go away
(delete-windows-on buf)
(message "Compile succeeded"))))
(setq compilation-scroll-output t)
;; Setup command to toggle between .hpp and .cpp
(defun toggle-header-buffer()
(interactive)
(let ((ext (file-name-extension buffer-file-name))
(fname (file-name-sans-extension buffer-file-name)))
(cond ((string= "h" ext)
(find-file (concat fname ".cpp")))
((string= "cpp" ext)
(find-file (concat fname ".h"))))))
;; Setup command to toggle between the file we're looking at and the
;; last one we were looking at
(defun toggle-previous-buffer()
(interactive)
(iswitch-to-buffer t))
;;; Compilation helper functions
(defun parent-dir(filename)
(expand-file-name (concat (expand-file-name filename) "/..")))
(defun search-up(filename directory)
(let ((target-file (expand-file-name filename directory)))
(cond ((file-exists-p target-file) (expand-file-name directory))
((eq directory "/") nil)
((search-up filename (parent-dir directory))))))
(defun search-and-run(filename command)
(let ((old-default-dir default-directory))
(setq default-directory (search-up filename default-directory))
(compile (concat "cd " default-directory " && " command))
(setq default-directory old-default-dir)))
(defun run-make()
(interactive)
(search-and-run "Makefile" "make"))
;; Setup shortcuts for compilation and switching between .hpp/.cpp files
(global-set-key "\M-t" 'toggle-buffer)
(global-set-key "\M-r" 'run-make)
(global-set-key "\M-n" 'next-error)
(provide 'init-c)
;;; init-c.el ends here