summaryrefslogtreecommitdiffstats
path: root/.emacs.d/functions.el
blob: 95fd4554a44d3fddf906da297d3de0bbb958a94b (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
(defun quote-region ()
  (interactive)
  (let ((beginning (region-beginning))
        (end (region-end)))
    (save-excursion
      (goto-char end)
      (insert ?')
      (goto-char beginning)
      (insert ?'))))

(defun what-face (pos)
  "Find out which face the current position uses"
  (interactive "d")
  (let ((face (or (get-char-property (point) 'read-face-name)
                  (get-char-property (point) 'face))))
    (if face
        (message "Face: %s" face)
      (message "No face at %d" pos))))

(defun my-comp-finish-function (buf str)
  "Don't show compilation window if everything went ok"
  (if (string-match "exited abnormally" str)
      ;; there were errors
      (message "compilation errors, press C-x ` to visit")
    ;; no errors, make the compilation window go away in 0.5 seconds
    (run-at-time 0.5 nil 'delete-windows-on buf)
    (message "NO COMPILATION ERRORS!")))

(defun fullscreen ()
  "Fill the entire screen with emacs"
  (interactive)
  (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
                         '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
  (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
                         '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0)))

(defun c-toggle-header-source ()
  "Toggle between a C source and header file"
  (interactive)
  (let ((ext (file-name-extension (buffer-file-name)))
        (noext (file-name-sans-extension (buffer-file-name))))
    (if (string= (substring ext 0 1) "c")
        (find-file (concat noext ".h"))
      (find-file (concat noext ".c")))))

(defun browse-to-current-file ()
  "Show current file in browser"
  (interactive)
  (browse-url buffer-file-name))

(defun comment-line ()
  "Toggle comment on a line"
  (interactive)
  (save-excursion
    (beginning-of-line)
    (insert "//")))

(defun add-php-keywords ()
  "Designed for c and c-style languages

Currently adds | & ! . + = - / % * , < > ? : ->"
  ;; Add ! at the beginning of font lock
  (font-lock-add-keywords
   'php-mode
   '(("\\([!]\\|\\=>\\)" 1 font-lock-operator-face)))
  ;; Add the rest at the end of font lock
  (font-lock-add-keywords
   'php-mode
   '(("\\(->\\|[|.+=&/%*,:?<>-]\\)" 1 font-lock-operator-face)
     ("\\(;\\)" 1 font-lock-end-statement)) 1))

(defun add-html-keywords ()
  "Designed for html, show some smarty tags"
  (font-lock-add-keywords
   'html-mode
   '(("{\\(\\*.*\\*\\)}" 1 font-comment-face)
     ("{\\/?\\(extends\\|block\\|foreach\\(else\\)?\\|if\\)"
      1 font-lock-builtin-face)
     ("\\$\\(\\(?:\\sw\\|\\s_\\)+\\)\\(?:|\\(\\(?:\\sw\\|\\s_\\)+\\):\\)"
      (1 font-lock-variable-name-face)
      (2 font-lock-function-name-face))
     ("\\$\\(\\(?:\\sw\\|\\s_\\)+\\)"
      1 font-lock-variable-name-face)
     ("{\\(\\(?:\\sw\\|\\s_\\)+\\).*}"
      1 font-lock-function-name-face))))

(defun buffer-major-mode (buffer-or-string)
  "Find out which major-mode is currently used"
  (with-current-buffer buffer-or-string major-mode))

(defun setup-system-frame-colours (&rest frame)
  (let ((f (if (car frame)
               (car frame)
             (selected-frame))))
    (progn
      (set-frame-font "-*-tamsyn-medium-*-*-*-15-*-*-*-*-80-*-*"))))

(defun show-init-sections ()
  (interactive)
  (occur ";;\s +.*\s +;;")
  (other-window 1))

(defun list-functions ()
  (interactive)
  (occur
   "\\(?:\\(?:private\\|protected\\|public\\) \\)?function \\(?:\\sw\\)+(\\sw*)"))

(defun insert-init-title (title width)
  (interactive "stitle: \nnwidth: ")
  (insert-char ?\; width)
  (insert "\n;;")
  (insert-char ?\s (floor (/ (- (- width 4.0) (length title)) 2)))
  (insert title)
  (insert-char ?\s (ceiling (/ (- (- width 4.0) (length title)) 2)))
  (insert ";;\n")
  (insert-char ?\; width))

(defun x-init ()
  "Initialization only for X"
  (require 'lcars-theme)
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;                       THEME SETTINGS                       ;;
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  (lcars-theme-set-faces
   'lcars

   ;; markdown-mode
   '(markdown-link-face (:inherit 'link))
   '(markdown-header-face-1 (:inherit 'org-level-1))
   '(markdown-header-face-2 (:inherit 'org-level-2))
   '(markdown-header-face-3 (:inherit 'org-level-3))
   '(markdown-header-face-4 (:inherit 'org-level-4))
   '(markdown-header-face-5 (:inherit 'org-level-5))
   '(markdown-header-face-6 (:inherit 'org-level-6)))

  (add-hook 'emacs-startup-hook 'fullscreen))

(defun cli-init ()
  "Add a space to the linum column"
  (setq linum-format "%d "))

(defun replace-occurrences (from to)
  (save-excursion
    (goto-char (point-min))
    (while (search-forward from nil t)
      (replace-match to))))

(defun replace-html-special-chars ()
  (replace-occurrences "é" "&eacute;"))

(defun on-before-save ()
  (if (eq (buffer-major-mode (current-buffer)) 'html-mode)
      (replace-html-special-chars))
  (if (not (eq (buffer-major-mode (current-buffer)) 'markdown-mode))
      (delete-trailing-whitespace)))

(defun on-after-save ()
  (let ((fname (buffer-file-name)))
    (let ((suffix (file-name-extension fname)))
      (if (string-equal suffix "el")
          (byte-compile-file fname)))))

(defun on-prog-mode ()
  (font-lock-add-keywords
   nil
   '(("\\b\\(0[xX][0-9a-fA-F]+[lL]?\\|[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\([lL]\\|[fF]\\|[dD]\\)?\\)\\b"
      0 font-lock-constant-face)
     ("\\b\\(\\.[0-9]+\\([eE][-+]?[0-9]+\\)?\\([lL]\\|[fF]\\|[dD]\\)?\\)\\b"
      0 font-lock-constant-face)))
  (rainbow-delimiters-mode)
  (pretty-lambdas))

(defun on-c-mode ()
  (local-set-key [f8]       'c-toggle-header-source)
  (local-set-key [f9]       'compile)
  (local-set-key [C-m]      'newline-and-indent)
  (local-set-key [C-return] 'newline))

(defun on-html-mode ()
  (local-set-key [f9] 'browse-to-current-file)
  (setq fill-column 73)
  (auto-fill-mode))

(defun on-php-mode ()
  (local-set-key [f6] 'comment-line))

(defun on-mail-mode ()
  (turn-on-auto-fill)
  (search-forward "\n\n"))

(defun pretty-lambdas ()
  (font-lock-add-keywords
   nil `(("(\\(lambda\\>\\)"
          (0 (progn
               (compose-region (match-beginning 1)
                               (match-end 1)
                               )))))))

;; http://emacs-fu.blogspot.com/2009/01/navigating-through-source-code-using.html
(defun djcb-gtags-create-or-update ()
  "create or update the gnu global tag file"
  (interactive)
  (if (not (= 0 (call-process "global" nil nil nil " -p"))) ; tagfile
      (let ((olddir default-directory)  ; doesn't exist?
            (topdir (read-directory-name
                     "gtags: top of source tree:" default-directory)))
        (cd topdir)
        (shell-command "gtags && echo 'created tagfile'")
        (cd olddir))                    ; restore
    ;; tagfile already exists; update it
    (shell-command "global -u && echo 'updated tagfile'")))