summaryrefslogtreecommitdiff
path: root/emacs-stark
blob: f473063d66e1b4bce3dad779112d92066e6e59ff (plain)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
;;; .emacs --- Config

;;; Commentary:

;;; Code:

;; support local packages
(add-to-list 'load-path "~/.emacs.d/site-lisp/")

(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/"))
(package-initialize)
(setq
 package-selected-packages '(meson-mode groovy-mode gradle-mode flycheck kotlin-mode java-imports ido-vertical-mode rg))
;; use (package-install-selected-packages) to install the ones above

;; show matching parentheses (and other characters)
(show-paren-mode t)

;; hide toolbar
(tool-bar-mode -1)
;; hide tooltips
(tooltip-mode -1)

(setq
 ;; skip startup message
 inhibit-startup-message t
 ;; show line and column number in mode-line
 line-number-mode t
 column-number-mode t
 ;; increase undo
 undo-limit 200000
 undo-strong-limit 300000
 ;; enable visible bell
 visible-bell t
 ;; enable font lockmode
 global-font-lock-mode t
 font-lock-mode t
 font-lock-maximum-decoration t)

;; Force more unique buffer names
(require 'uniquify)
(setq uniquify-buffer-name-style 'post-forward-angle-brackets)

;; Clean the buffer list at midnight
(require 'midnight)
;; Set clean-buffer-list delay to 1 day
(setq clean-buffer-list-delay-general 1)
(if (not (boundp 'clean-buffer-list-kill-regexps))
    (setq clean-buffer-list-kill-regexps '()))
;; Add preprocessed buffers to the list
(add-to-list 'clean-buffer-list-kill-regexps
             '("\\`\\*.*-preprocessed\\*\\'"))

;; load rg
(require 'rg)

(setq
 ;; create backups
 make-backup-files t
 ;; don't ask to delete backup versions
 delete-old-versions t
 ;; use version control
 version-control t)

;; put all backup files in their own directory
(let ((backup-dir "~/.emacs.d/backups/"))
  (make-directory backup-dir :parents)
  (setq backup-directory-alist
        `((".*" . ,backup-dir))))

;; show buffer in frame title
(setq frame-title-format "%b %* emacs")

;; frame parameters
;; frame size
(add-to-list 'default-frame-alist '(width . 80))
(add-to-list 'default-frame-alist '(height . 36))
;; tell WM that we really want those sizes
(add-to-list 'default-frame-alist '(user-size . t))
;; frame font and color
(add-to-list 'default-frame-alist
             '(font . "-*-proggycleantt-medium-*-*-*-*-130-*-*-*-*-iso8859-1"))
(add-to-list 'default-frame-alist '(cursor-color . "green"))
(add-to-list 'default-frame-alist '(foreground-color . "grey"))
(add-to-list 'default-frame-alist '(background-color . "black"))
(add-to-list 'default-frame-alist '(background-mode . "dark"))

;; Make man open in full frame instead of always creating half a frame
(set-variable 'Man-notify-method (quote pushy))

;; Toggle frame width
(defun toggle-frame-width (frame)
  "Toggle FRAME width between 100 and 80."
  (when window-system
    (let ((width (frame-width frame)))
      (cond ((eq width 80)
             (set-frame-width frame 100))
            (t
             (set-frame-width frame 80))))))

(defun toggle-selected-frame-width ()
  "Toggle selected frame width between 100 and 80."
  (interactive)
  (toggle-frame-width (selected-frame)))

;; default styles
(setq-default
 ;; no tabs in indentation
 indent-tabs-mode nil
 ;; tab-width 4
 tab-width 4
 ;; c-basic-offset
 c-basic-offset 4
 ;; show trailing whitespace
 show-trailing-whitespace t)

;; enable c warnings
(global-cwarn-mode t)

;; auto inserts
(eval-after-load 'autoinsert
  '(define-auto-insert
     '("\\.\\(hh\\|hxx\\|hpp\\)\\'" . "C++ header")
     '((upcase
        (mapconcat 'identity (split-string
                              (file-name-nondirectory buffer-file-name)
                              "[^a-zA-Z0-9]+") "_"))
       "#ifndef " str \n
       "#define " str \n
       \n
       _ \n
       \n
       "#endif  // " str \n)))
(eval-after-load 'autoinsert
  '(define-auto-insert
     '("\\.\\(CC?\\|cc\\|cxx\\|cpp\\|c++\\)\\'" . "C++ source")
     '(nil
       "// -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-" \n
       "//" \n
       "// Copyright (C) 2019 Opera Software AS.  All rights reserved." \n
       "//" \n
       "// This file is an original work developed by Opera Software." \n
       \n
       (let ((base (file-name-sans-extension buffer-file-name)))
         (cond
          ((file-exists-p
            (concat base ".hh"))
           (concat "#include \""
                   (file-name-nondirectory (concat base ".hh"))
                   "\"\n\n"))))
       > _ \n)))
(eval-after-load 'autoinsert
  '(define-auto-insert
     '("\\.\\([Hh]\\)\\'" . "C++ header")
     '((upcase
        (mapconcat 'identity (split-string
                              (file-name-nondirectory buffer-file-name)
                              "[^a-zA-Z0-9]+") "_"))
       "// -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-" \n
       "//" \n
       "// Copyright (C) 2019 Opera Software AS.  All rights reserved." \n
       "//" \n
       "// This file is an original work developed by Opera Software." \n
       \n
       "#ifndef " str \n
       "#define " str \n
       \n
       _ \n
       \n
       "#endif  // " str \n)))
(eval-after-load 'autoinsert
  '(define-auto-insert
     '("\\.\\([Cc]\\)\\'" . "C source")
     '(nil
       "#include \"common.h\"" \n
       \n
       (let ((base (file-name-sans-extension buffer-file-name)))
         (cond
          ((file-exists-p
            (concat base ".h"))
           (concat "#include \""
                   (file-name-nondirectory (concat base ".h"))
                   "\"\n\n"))))
       > _ \n)))
(eval-after-load 'autoinsert
  '(define-auto-insert
     '("[Mm]akefile\\'" . "Makefile")
     '(nil
       ".PHONY: all clean" \n
       \n
       "all: " _ \n
       \n
       "clean:" \n
       \n)))
(eval-after-load 'autoinsert
  '(define-auto-insert
     '("\\.\\(py\\)\\'" . "Python source")
     '(nil
       "#!/usr/bin/env python" \n
       \n
       _ \n)))
(eval-after-load 'autoinsert
  '(define-auto-insert
     '("\\.\\(java\\)\\'" . "Java source")
     '((file-name-base buffer-file-name)
       "// -*- Mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-" \n
       "//" \n
       "// Copyright (C) 2019 Opera Software AS.  All rights reserved." \n
       "//" \n
       "// This file is an original work developed by Opera Software." \n
       \n
       "package " (mapconcat 'identity (member "com" (split-string (file-name-directory buffer-file-name) "/" t)) ".") ";" \n
       \n
       "class " str " {" \n
       _ \n
       str "() {" ?\n
       "    " "}" ?\n  ;; > should work here but it doesn't
       "}" \n)))
(auto-insert-mode t)
;; don't ask before using auto-insert
(setq-default auto-insert-query nil)

;; gpg
(require 'epa-file)
(epa-file-enable)

;; keys
(global-set-key (kbd "C-c o") 'ff-find-other-file)
(global-set-key [?\C-x ?\t] 'toggle-selected-frame-width)
(define-key java-mode-map (kbd "M-I") 'java-imports-add-import-dwim)
(global-set-key (kbd "C-x f") 'find-file-in-repository)

;; java-imports
(require 'java-imports)
(setq java-imports-find-block-function 'java-imports-find-place-sorted-block)
(add-hook 'java-mode-hook 'java-imports-scan-file)

;; stop grep from asking to save buffers
(setq grep-save-buffers nil)

;; ido
(require 'ido)
(require 'ido-vertical-mode)
(ido-mode)
(ido-vertical-mode)
(setq ido-vertical-define-keys 'C-n-C-p-up-and-down)
;; stop ido from switching to another frame where the same buffer is already open
(setq ido-default-buffer-method 'selected-window)

;; find-file-in-repo
(require 'find-file-in-repository)

;; flycheck
(require 'flycheck)
(global-flycheck-mode)
(setq flycheck-check-syntax-automatically '(save new-line))

;; replace ch alias fro grep with cchh alias, ie if starting with a ".h" file
;; you might still want to search in cc files
(eval-after-load "grep"
  '(setf (cdr (assoc "ch" grep-files-aliases))
         (cdr (assoc "cchh" grep-files-aliases))))

;; make sure grep skips unimportant things
(eval-after-load "grep"
  '(progn
    (add-to-list 'grep-find-ignored-directories "out_gradle")
    (add-to-list 'grep-find-ignored-directories "bin")))

(c-add-style
 "common c"
 '("gnu"
   (c-basic-offset . 4)
   (c-offsets-alist
    .
    ((substatement-open . 0)
     (arglist-intro . ++)
     (innamespace . 0)))))

(add-hook 'c-mode-common-hook
          (lambda()
            (c-set-style "common c")))

(c-add-style
 "default c++"
 '("common c"
   (c-basic-offset . 2)))

(defun get-base-dir (path needle)
  "Get base directory for PATH using NEEDLE to cut."
  (let ((parts (split-string path "/" t)))
    (let ((index (cl-position needle parts :from-end t :test 'equal)))
      (if index
          (concat "/" (mapconcat 'identity (seq-take parts (+ index 1)) "/"))
        path))))

(make-variable-buffer-local 'flycheck-clang-definitions)
(make-variable-buffer-local 'flycheck-clang-include-path)
(make-variable-buffer-local 'flycheck-clang-language-standard)
(make-variable-buffer-local 'flycheck-clang-warnings)

(add-hook 'c++-mode-hook
          (lambda()
            (setq flycheck-clang-language-standard "c++14")
            (c-set-style "default c++")
            ;; Chromium hook
            (when (and buffer-file-name
                       (string-match "/chromium/src/" buffer-file-name))
              (let ((base (get-base-dir (file-name-directory buffer-file-name) "src")))
                (setq flycheck-clang-language-standard "c++14"
                      flycheck-clang-definitions (list "OS_ANDROID")
                      flycheck-clang-warnings (list "no-unused-parameter")
                      flycheck-clang-include-path (list base
                                                        (concat base "/out_gradle/BetaArm7Debug/gen")
                                                        (concat base "/out_gradle/BetaArm7Release/gen"))))
              (require 'google-c-style)
              (c-add-style "Google" google-c-style t))
            ;; ofa hook
            (when (and buffer-file-name
                       (string-match "/mobile/mobile/" buffer-file-name))
              (let ((base (get-base-dir (file-name-directory buffer-file-name) "mobile")))
                (setq flycheck-clang-language-standard "c++14"
                      flycheck-clang-definitions (list "OS_ANDROID")
                      flycheck-clang-warnings (list "no-unused-parameter")
                      flycheck-clang-include-path (list base (concat base "/../../chromium/src")
                                                        (concat base "/../../chromium/src/third_party/re2/src")
                                                        (concat base "/../../chromium/src/google/re2/src")
                                                        (concat base "../../chromium/src/third_party/googlemock/src/googlemock/include")
                                                        (concat base "../../chromium/src/third_party/googletest/src/googletest/include")
                                                        (concat base "/../../chromium/src/out_gradle/BetaArm7Debug/gen")
                                                        (concat base "/../../chromium/src/out_gradle/BetaArm7Release/gen"))))
              (require 'google-c-style)
              (c-add-style "Google" google-c-style t))))

(make-variable-buffer-local 'flycheck-android-java-checkstyle-jar)

;; Opera Java hook
(add-hook 'java-mode-hook
          (lambda()
            (require 'google-c-style)
            (google-set-c-style)
            (setq c-file-style "Google")
            (c-set-offset 'arglist-intro '++)
            (c-set-offset 'arglist-cont nil)
            (c-set-offset 'arglist-cont-nonempty '++)
            (setq indent-tabs-mode nil)
            (setq c-basic-offset 4)
            (setq flycheck-android-java-checkstyle-path
                  "tools/checkstyle"
                  flycheck-android-java-checkstyle-config
                  "checkstyle-config-android.xml"
                  flycheck-android-java-checkstyle-properties
                  "checkstyle.properties")

            ;; ofa hook
            (when (and buffer-file-name
                       (string-match "/mobile/mobile/" buffer-file-name))
              (require 'flycheck-android-experimental)
              (setq flycheck-android-java-checkstyle-jar
                    "tools/checkstyle/custom/custom-checkstyle-checks.jar:../../chromium/src/third_party/checkstyle/checkstyle-8.5-all.jar"
                    flycheck-android-java-variant
                    "betaArm7Debug"
                    flycheck-android-java-skip-gen t))
            ;; mini hook
            (when (and buffer-file-name
                       (string-match "/mobile/android/" buffer-file-name))
              (require 'flycheck-android-experimental)
              (setq flycheck-android-java-checkstyle-jar
                    "../../chromium/src/third_party/checkstyle/checkstyle-6.5-all.jar"))))

;;;