forked from redguardtoo/emacs.d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-utils.el
257 lines (227 loc) · 8.76 KB
/
init-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
254
255
256
257
;; elisp version of try...catch...finally
(defmacro safe-wrap (fn &rest clean-up)
`(unwind-protect
(let (retval)
(condition-case ex
(setq retval (progn ,fn))
('error
(message (format "Caught exception: [%s]" ex))
(setq retval (cons 'exception (list ex)))))
retval)
,@clean-up))
;; Handier way to add modes to auto-mode-alist
(defun add-auto-mode (mode &rest patterns)
"Add entries to `auto-mode-alist' to use `MODE' for all given file `PATTERNS'."
(dolist (pattern patterns)
(add-to-list 'auto-mode-alist (cons pattern mode))))
;;----------------------------------------------------------------------------
;; String utilities missing from core emacs
;;----------------------------------------------------------------------------
(defun string-all-matches (regex str &optional group)
"Find all matches for `REGEX' within `STR', returning the full match string or group `GROUP'."
(let ((result nil)
(pos 0)
(group (or group 0)))
(while (string-match regex str pos)
(push (match-string group str) result)
(setq pos (match-end group)))
result))
(defun string-rtrim (str)
"Remove trailing whitespace from `STR'."
(replace-regexp-in-string "[ \t\n]*$" "" str))
;; Find the directory containing a given library
(defun directory-of-library (library-name)
"Return the directory in which the `LIBRARY-NAME' load file is found."
(file-name-as-directory (file-name-directory (find-library-name library-name))))
(defun my-insert-str (str)
;; ivy8 or ivy9
(if (consp str) (setq str (cdr str)))
;; evil-mode?
(if (and (functionp 'evil-normal-state-p)
(boundp 'evil-move-cursor-back)
(evil-normal-state-p)
(not (eolp))
(not (eobp)))
(forward-char))
;; insert now
(insert str))
(defun my-line-str (&optional line-end)
(buffer-substring-no-properties (line-beginning-position)
(if line-end line-end (line-end-position))))
(defun my-buffer-str ()
(buffer-substring-no-properties (point-min) (point-max)))
(defun my-selected-str ()
(buffer-substring-no-properties (region-beginning) (region-end)))
(defun my-use-selected-string-or-ask (hint)
"Use selected region or ask user input for string."
(if (region-active-p) (my-selected-str)
(if (string= "" hint) (thing-at-point 'symbol)
(read-string hint))))
;; Delete the current file
(defun delete-this-file ()
"Delete the current file, and kill the buffer."
(interactive)
(or (buffer-file-name) (error "No file is currently being edited"))
(when (yes-or-no-p (format "Really delete '%s'?"
(file-name-nondirectory buffer-file-name)))
(delete-file (buffer-file-name))
(kill-this-buffer)))
;;----------------------------------------------------------------------------
;; Rename the current file
;;----------------------------------------------------------------------------
(defun rename-this-file-and-buffer (new-name)
"Renames both current buffer and file it's visiting to NEW-NAME."
(interactive "sNew name: ")
(let ((name (buffer-name))
(filename (buffer-file-name)))
(unless filename
(error "Buffer '%s' is not visiting a file!" name))
(if (get-buffer new-name)
(message "A buffer named '%s' already exists!" new-name)
(progn
(rename-file filename new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil)))))
;;----------------------------------------------------------------------------
;; Browse current HTML file
;;----------------------------------------------------------------------------
(defun browse-current-file ()
"Open the current file as a URL using `browse-url'."
(interactive)
(browse-url-generic (concat "file://" (buffer-file-name))))
(require 'cl)
(defmacro with-selected-frame (frame &rest forms)
(let ((prev-frame (gensym))
(new-frame (gensym)))
`(progn
(let* ((,new-frame (or ,frame (selected-frame)))
(,prev-frame (selected-frame)))
(select-frame ,new-frame)
(unwind-protect
(progn ,@forms)
(select-frame ,prev-frame))))))
(defvar load-user-customized-major-mode-hook t)
(defvar cached-normal-file-full-path nil)
(defun buffer-too-big-p ()
(or (> (buffer-size) (* 5000 64))
(> (line-number-at-pos (point-max)) 5000)))
(defun is-buffer-file-temp ()
(interactive)
"If (buffer-file-name) is nil or a temp file or HTML file converted from org file"
(let ((f (buffer-file-name))
org
(rlt t))
(cond
((not load-user-customized-major-mode-hook) t)
((not f)
;; file does not exist at all
(setq rlt t))
((string= f cached-normal-file-full-path)
(setq rlt nil))
((string-match (concat "^" temporary-file-directory) f)
;; file is create from temp directory
(setq rlt t))
((and (string-match "\.html$" f)
(file-exists-p (setq org (replace-regexp-in-string "\.html$" ".org" f))))
;; file is a html file exported from org-mode
(setq rlt t))
(t
(setq cached-normal-file-full-path f)
(setq rlt nil)))
rlt))
(defun my-guess-mplayer-path ()
(let ((rlt "mplayer"))
(cond
(*is-a-mac* (setq rlt "mplayer -quiet"))
(*linux* (setq rlt "mplayer -quiet -stop-xscreensaver"))
(*cygwin*
(if (file-executable-p "/cygdrive/c/mplayer/mplayer.exe")
(setq rlt "/cygdrive/c/mplayer/mplayer.exe -quiet")
(setq rlt "/cygdrive/d/mplayer/mplayer.exe -quiet")))
(t ; windows
(if (file-executable-p "c:\\\\mplayer\\\\mplayer.exe")
(setq rlt "c:\\\\mplayer\\\\mplayer.exe -quiet")
(setq rlt "d:\\\\mplayer\\\\mplayer.exe -quiet"))))
rlt))
(defun my-guess-image-viewer-path (file &optional is-stream)
(let ((rlt "mplayer"))
(cond
(*is-a-mac*
(setq rlt
(format "open %s &" file)))
(*linux*
(setq rlt
(if is-stream (format "curl -L %s | feh -F - &" file) (format "feh -F %s &" file))))
(*cygwin* (setq rlt "feh -F"))
(t ; windows
(setq rlt
(format "rundll32.exe %SystemRoot%\\\\System32\\\\\shimgvw.dll, ImageView_Fullscreen %s &" file))))
rlt))
;; {{ simpleclip has problem on Emacs 25.1
(defun test-simpleclip ()
(simpleclip-set-contents "testsimpleclip!")
(string= "testsimpleclip!" (simpleclip-get-contents)))
(setq simpleclip-works (test-simpleclip))
(defun my-gclip ()
(if simpleclip-works (simpleclip-get-contents)
(cond
((eq system-type 'darwin)
(with-output-to-string
(with-current-buffer standard-output
(call-process "/usr/bin/pbpaste" nil t nil "-Prefer" "txt"))))
((eq system-type 'cygwin)
(with-output-to-string
(with-current-buffer standard-output
(call-process "getclip" nil t nil))))
((memq system-type '(gnu gnu/linux gnu/kfreebsd))
(with-output-to-string
(with-current-buffer standard-output
(call-process "xsel" nil t nil "--clipboard" "--output")))))))
(defun my-pclip (str-val)
(if simpleclip-works (simpleclip-set-contents str-val)
(cond
((eq system-type 'darwin)
(with-temp-buffer
(insert str-val)
(call-process-region (point-min) (point-max) "/usr/bin/pbcopy")))
((eq system-type 'cygwin)
(with-temp-buffer
(insert str-val)
(call-process-region (point-min) (point-max) "putclip")))
((memq system-type '(gnu gnu/linux gnu/kfreebsd))
(with-temp-buffer
(insert str-val)
(call-process-region (point-min) (point-max) "xsel" nil nil nil "--clipboard" "--input"))))))
;; }}
(defun make-concated-string-from-clipboard (concat-char)
(let* ((str (replace-regexp-in-string "'" "" (upcase (my-gclip))))
(rlt (replace-regexp-in-string "[ ,-:]+" concat-char str)))
rlt))
;; {{ diff region SDK
(defun diff-region-exit-from-certain-buffer (buffer-name)
(bury-buffer buffer-name)
(winner-undo))
(defmacro diff-region-open-diff-output (content buffer-name)
`(let ((rlt-buf (get-buffer-create ,buffer-name)))
(save-current-buffer
(switch-to-buffer-other-window rlt-buf)
(set-buffer rlt-buf)
(erase-buffer)
(insert ,content)
(diff-mode)
(goto-char (point-min))
;; evil keybinding
(if (fboundp 'evil-local-set-key)
(evil-local-set-key 'normal "q"
(lambda ()
(interactive)
(diff-region-exit-from-certain-buffer ,buffer-name))))
;; Emacs key binding
(local-set-key (kbd "C-c C-c")
(lambda ()
(interactive)
(diff-region-exit-from-certain-buffer ,buffer-name)))
)))
;; }}
(provide 'init-utils)