-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.el
213 lines (187 loc) · 7.8 KB
/
util.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
;; -*- Emacs-Lisp -*-
;; Time-stamp: <2010-09-26 01:01:33 Sunday by taoshanwen>
(require 'eval-after-load)
(defvar mswin (equal window-system 'w32) "Non-nil means windows system.")
(defvar cygwin (equal system-type 'cygwin) "Non-nil means cygwin system.")
(defvar use-cua nil "Use CUA mode or not.")
(defvar last-region-beg nil "Beginning of last region.")
(defvar last-region-end nil "End of last region.")
(defvar last-region-is-rect nil "Last region is rectangle or not.")
(defvar last-region-use-cua nil "Last region use CUA mode or not.")
(defconst system-head-file-dir (list "/usr/include" "/usr/local/include" "/usr/include/sys") "系统头文件目录")
(defconst user-head-file-dir (list "." "../" ) "用户头文件目录")
(defconst is-before-emacs-21 (>= 21 emacs-major-version) "是否是emacs 21或以前的版本")
(defconst is-after-emacs-23 (<= 23 emacs-major-version) "是否是emacs 23或以后的版本")
(defvar c-modes '(c-mode c++-mode awk-mode java-mode) "*C modes.")
(defvar dev-modes (append c-modes '(python-mode perl-mode makefile-gmake-mode)) "*Modes for develop.")
;;;###autoload
(defun execute-command-on-file (file command)
"对FILE执行命令COMMAND"
(interactive
(list (read-file-name "File execute command on: ")
(let* ((input ""))
(while (string= input "")
(setq input (read-string "命令: ")))
input)))
(if file
(when (yes-or-no-p (concat command " file `" file "'?"))
(shell-command (concat command " \"" file "\"")))
(message "Executing command `%s'..." command)
(shell-command command)))
;;;###autoload
(defun execute-command-on-current-file (command)
"对当前buffer执行命令COMMAND, 如果该buffer对应文件的话, 再执行`revert-buffer-no-confirm'"
(interactive
(list (let* ((input ""))
(while (string= input "")
(setq input (read-string "命令: ")))
input)))
(let* ((file (buffer-file-name)))
(execute-command-on-file file command)
(if file
(revert-buffer-no-confirm))))
;;;###autoload
(defun execute-command-on-current-dir (command)
"对当前目录执行命令COMMAND."
(interactive
(list (let* ((input ""))
(while (string= input "")
(setq input (read-string "命令: ")))
input)))
(let* ((file (buffer-file-name)))
(execute-command-on-file default-directory command)
(if file
(revert-buffer-no-confirm))))
;;;###autoload
(defmacro def-execute-command-on-file-command (command)
"Make definition of command which execute command on file."
`(defun ,(intern (subst-char-in-string ?\ ?- command)) (file)
,(concat "Run command `" command "' on file FILE.")
(interactive (list (read-file-name (concat "File to " ,command ": "))))
(execute-command-on-file file ,command)))
;;;###autoload
(defmacro def-execute-command-on-current-file-command (command)
"Make definition of command which execute command on current file."
`(defun ,(am-intern (subst-char-in-string ?\ ?- command) "-current-file") ()
,(concat "Execute command `" command "' on current file.")
(interactive)
(execute-command-on-current-file ,command)))
;;;###autoload
(defmacro def-execute-command-on-current-dir-command (command)
"Make definition of command which execute command on current directory."
`(defun ,(am-intern (subst-char-in-string ?\ ?- command) "-current-dir") ()
,(concat "Execute command `" command "' on current directory.")
(interactive)
(execute-command-on-current-dir ,command)))
;;;###autoload
(defmacro define-kbd (keymap key def) `(define-key ,keymap (kbd ,key) ,def))
;;;###autoload
(defmacro local-set-kbd (key command) `(local-set-key (kbd ,key) ,command))
;;;###autoload
(defmacro global-set-kbd (key command) `(global-set-key (kbd ,key) ,command))
;;;###autoload
(defalias 'apply-define-key 'eal-define-keys-commonly)
;;;###autoload
(defalias 'define-key-list 'eal-define-keys-commonly)
;;;###autoload
(defun apply-args-list-to-fun (fun-list args-list)
"Apply args list to function FUN-LIST.
FUN-LIST can be a symbol, also can be a list whose element is a symbol."
(let ((is-list (and (listp fun-list) (not (functionp fun-list)))))
(dolist (args args-list)
(if is-list
(dolist (fun fun-list)
(apply-args-to-fun fun args))
(apply-args-to-fun fun-list args)))))
;;;###autoload
(defun apply-args-to-fun (fun args)
"Apply args to function FUN."
(if (listp args)
(eval `(,fun ,@args))
(eval `(,fun ,args))))
;;;###autoload
(defun kill-buffer-when-shell-command-exit ()
"Close current buffer when `shell-command' exit."
(let ((process (ignore-errors (get-buffer-process (current-buffer)))))
(when process
(set-process-sentinel process
(lambda (proc change)
(when (string-match "\\(finished\\|exited\\)" change)
(kill-buffer (process-buffer proc))))))))
;;;###autoload
(defun list-colors-display-htm (&optional list)
"Create HTML page which lists all the defined colors."
(interactive)
(if (and (null list) window-system)
(progn
(setq list (x-defined-colors))
;; Delete duplicate colors.
(let ((l list))
(while (cdr l)
(if (facemenu-color-equal (car l) (car (cdr l)))
(setcdr l (cdr (cdr l)))
(setq l (cdr l)))))))
(with-output-to-temp-buffer "*Colors*"
(save-excursion
(set-buffer standard-output)
(insert "<html>\n"
"<head>\n"
"<meta http-equiv=\"Content-Style-Type\" content=\"text/css\">\n"
"<title>Colors</title>\n"
"</head>\n"
"<body>\n"
"<h1>Colors</h1>\n"
"<p>\n"
"<pre>\n")
(let (s)
(while list
(insert (format (concat "<span style=\"background-color:%s\">%-20s</span>"
" "
"<span style=\"color:%s\">%s</span>"
"\n")
(html-color (car list)) (car list)
(html-color (car list)) (car list)))
(setq list (cdr list))))
(insert "</pre>"
"</body>"
"</html>"))))
;;;###autoload
(defun html-color (string)
"Convert colors names to rgb(n1,n2,n3) strings."
(format "rgb(%d,%d,%d)"
(/ (nth 0 (x-color-values string)) 256)
(/ (nth 1 (x-color-values string)) 256)
(/ (nth 2 (x-color-values string)) 256)))
;;;###autoload
(defmacro def-command-max-window (command)
"Make definition of command which after execute command COMMAND execute `delete-other-windows'."
`(defun ,(am-intern command "-max-window") ()
,(concat "After run command `" command "' execute command `delete-other-windows'.")
(interactive)
(call-interactively ',(intern command))
(delete-other-windows)))
;;;###autoload
(defun delete-current-window (&optional frame)
"Delete window which showing current buffer."
(interactive
(list (and current-prefix-arg
(or (natnump (prefix-numeric-value current-prefix-arg))
'visible))))
(if (one-window-p)
(bury-buffer)
(delete-windows-on (current-buffer) frame)))
;;;###autoload
(defmacro def-turn-on (command &optional is-on)
"Make definition of command whose name is COMMAND-on when IS-ON is t
and COMMAND-off when IS-ON is nil."
(let ((on (if is-on "on" "off")))
`(defun ,(am-intern command "-" on) ()
,(concat "Turn " on " `" command "'.")
(interactive)
(funcall ',(intern command) ,(if is-on 1 -1)))))
;;;###autoload
(defun unset-key (keymap key)
"Remove binding of KEY in map KEYMAP.
KEY is a string or vector representing a sequence of keystrokes."
(define-key keymap key nil))
(provide 'util)