-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.el
254 lines (213 loc) · 7.72 KB
/
utils.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
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
;; -*- Mode: Emacs-Lisp -*-
(defun load-private (file &optional NOMESSAGE NOSUFFIX MUST-SUFFIX)
"Just like #'load, when NOERROR=t, but warns about not loading."
(if (load file t NOMESSAGE NOSUFFIX MUST-SUFFIX)
t
(message (concat "Couldn't load private file \""
file "\"."))
nil))
;; Macros for platform independence
(defun is-platform (platform)
(or (string= system-name platform)
(eq system-type platform)))
(defmacro setq-platform (symbol &rest defs)
`(setq ,symbol
,@(or (cdr (assoc system-name defs))
(cdr (assoc system-type defs))
(error
"System name (%s) and System type (%s) not found in:\n %s."
system-name system-type defs))))
(defmacro in-platform (platform &rest body)
(if (is-platform platform)
`(progn ,@body)))
(defmacro in-platforms (plat-body &rest rest)
(let ((platform (car plat-body))
(body (cdr plat-body)))
(cond ((is-platform platform)
`(progn ,@body))
((null rest)) ;; Stop
(t `(in-platforms ,@rest)))))
;; Executes 'body from version 'version onwards
(defmacro from-version (version &rest body)
`(if (>= emacs-major-version ,version)
(progn ,@body)
(message
(concat
"The following code was not executed because "
"emacs-major-version (%d) < %d\n %S")
emacs-major-version ,version ',body)))
(defun add-to-list* (list-var elements &optional append compare-fn)
"Add several elements to list-var (using add-to-list)."
(let ((compare-fn (or compare-fn 'equal)))
(dolist (element elements)
(add-to-list list-var element append compare-fn))
(symbol-value list-var)))
(defun add-to-load-path (&rest paths)
"Adds every path (after expansion) to the variable 'load-path"
(add-to-list* 'load-path
(mapcar (lambda (path)
(expand-file-name path))
paths)))
;; (defun add-to-exec-path (&rest paths)
;; "Adds every path (after expansion) to the variable 'exec-path"
;; (add-to-list* 'exec-path
;; (mapcar (lambda (path)
;; (expand-file-name path))
;; paths)))
(defun add-to-exec-path (&rest arg)
;; From CarbonEmacs.app
"Add the each element of ARG to the PATH environment variable
and to the value of `exec-path'.
For example:
\(add-to-exec-path '(\"/usr/local/bin\" \"/usr/X11R6/bin\"))
\(add-to-exec-path \"/usr/local/bin:/usr/X11R6/bin\")
\(add-to-exec-path \"/opt/local/bin\" \"/usr/texbin\")"
(cond ((listp (car arg)) ;; First case
(setq arg (car arg)))
((null (cdr arg)) ;; Second case
(setq arg (split-string (car arg) ":"))))
;; Third case is the best one
(dolist (path (prune-directory-list arg))
(setq path (expand-file-name path))
(add-to-env "PATH" path)
(add-to-list 'exec-path path t))
(message "PATH=%s" (getenv "PATH")))
(defun add-to-env (key value)
;; From CarbonEmacs
"Document forthcoming..."
(let ((env (getenv key)))
(if (and env (not (equal env "")))
(if (not (member value (split-string env ":")))
(setenv key (concat env ":" value)))
(setenv key value))))
(defun toggle-fullscreen ()
;; Only in CarbonEmacs, Aquamacs 2.0 or patched GNU Emacs for now...
"Toggles fullscreen in emacs"
(interactive)
(set-frame-parameter nil
'fullscreen
(if (frame-parameter nil 'fullscreen)
nil
'fullboth)))
(defun toggle-transparency ()
"Toggle transparency on the active frame."
(interactive)
(set-frame-parameter nil
'alpha
(if (or (not (frame-parameter nil 'alpha))
(/= (car (frame-parameter nil 'alpha)) 100))
'(100 100)
'(85 50))))
(defun copy-line ()
;; From aadsm
"Copy line to kill ring."
(interactive)
(copy-region-as-kill (line-beginning-position)
(line-end-position)))
(defun current-itunes-song ()
"Returns the music currently playing in iTunes"
(applescript
"tell application \"iTunes\"
set currentTrack to the current track
set artist_name to the artist of currentTrack
set song_title to the name of currentTrack
return artist_name & \" - \" & song_title
end tell"))
(defun itunes-now-playing ()
"Echoes the music currently playing in iTunes"
(interactive)
(message (current-itunes-song)))
(defun itunes-next-track (p)
(interactive "p")
(setq p (if (zerop p) 1 p))
(dotimes (i p)
(applescript
"tell application \"iTunes\"
next track
end tell")))
(defvar macosx-open-program
"open"
"Default program to use in MacOS X to open files externally")
(defvar open-program
macosx-open-program
"Default program to use to open files externally")
(defun open-file-externally (file)
(interactive "fOpen file externally: ")
(process-file open-program nil t nil (expand-file-name file)))
;; (process-file macosx-open-program))
(defun quicklook-file (file)
(interactive "fQuicklook file: ")
(start-process "ql"
nil
"qlmanage"
"-p" (expand-file-name file)))
(defalias 'ql 'quicklook-file)
(defun quicklook-kill ()
(interactive)
(start-process "qlkill"
nil
"killall"
"qlmanage"))
(defalias 'qlkill 'quicklook-kill)
(defun get-region-or-ask-string (prompt)
"If the mark is active, gets the text inside the
region. Otherwise, ask for a string."
(if mark-active
(buffer-substring-no-properties (region-beginning) (region-end))
(read-string prompt)))
(defun fill-buffer (&optional p)
(interactive "p")
(save-excursion
(fill-region (point-min) (point-max) p)))
(defun hide-menus ()
(interactive)
(let ((menu-bar-old (lookup-key global-map [(menu-bar)])))
(define-key global-map [(menu-bar)] nil)
(sit-for 3) ;; That should be enough...
(define-key global-map [(menu-bar)] menu-bar-old)))
;; Create the list in the background.
;; Subsequent executions should take around 0.2 secs
;; (in-platform macosx
;; (start-process "app-switcher-list"
;; nil
;; "mdfind"
;; "kMDItemKind == Application"))
;; (defun app-list ()
;; (interactive)
;; ; (read-from-string
;; (with-output-to-string
;; (call-process "osascript"
;; nil
;; standard-output
;; nil
;; "-e"
;; "tell application \"System Events\" to \
;; set the_apps to name of application processes \
;; whose background only is false")))
(defun app-switch ()
(interactive)
(let ((app (get-region-or-ask-string
"Which application should I switch to? ")))
(applescript "tell application \""
app
"\" to activate")))
(defun applescript (&rest texts)
(let ((text (apply #'concat
texts)))
(start-process "applescript"
nil
"osascript" "-e"
text)))
;; From: http://github.com/alexott/emacs-configs/blob/401826d41e537629a1af75e92fa09d8aef5addc8/rc/emacs-rc-common-hooks.el
;; show XXX/FIXME/TODO/BUG keywords
(defun show-prog-keywords ()
;; highlight additional keywords
(font-lock-add-keywords nil
'(("\\<\\(\\|XXX\\|FIXME\\|TODO\\|BUG\\):"
1 font-lock-warning-face t)))
(font-lock-add-keywords nil '(("\\<\\(DONE\\):"
1 font-lock-doc-face t)))
;; highlight too long lines
(font-lock-add-keywords
nil '(("^[^\n]\\{120\\}\\(.*\\)$" 1 font-lock-warning-face t))))
(provide 'utils)