forked from cjohansen/.emacs.d
-
Notifications
You must be signed in to change notification settings - Fork 208
/
misc-defuns.el
209 lines (180 loc) · 7.12 KB
/
misc-defuns.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
;; Misc defuns go here
;; It wouldn't hurt to look for patterns and extract once in a while
(defmacro create-simple-keybinding-command (name key)
`(defmacro ,name (&rest fns)
(list 'global-set-key (kbd ,key) `(lambda ()
(interactive)
,@fns))))
(create-simple-keybinding-command f2 "<f2>")
(create-simple-keybinding-command f5 "<f5>")
(create-simple-keybinding-command f6 "<f6>")
(create-simple-keybinding-command f7 "<f7>")
(create-simple-keybinding-command f8 "<f8>")
(create-simple-keybinding-command f9 "<f9>")
(create-simple-keybinding-command f10 "<f10>")
(create-simple-keybinding-command f11 "<f11>")
(create-simple-keybinding-command f12 "<f12>")
(defun goto-line-with-feedback ()
"Show line numbers temporarily, while prompting for the line number input"
(interactive)
(unwind-protect
(progn
(display-line-numbers-mode 1)
(call-interactively 'goto-line))
(display-line-numbers-mode -1)))
(defun goto-last-modification ()
(interactive)
(undo-tree-undo)
(undo-tree-redo))
(defun open-line-and-indent ()
(interactive)
(newline-and-indent)
(end-of-line 0)
(indent-for-tab-command))
;; start a httpd-server in current directory
(defun httpd-start-here (directory port)
(interactive (list (read-directory-name "Root directory: " default-directory nil t)
(read-number "Port: " 8017)))
(setq httpd-root directory)
(setq httpd-port port)
(httpd-start)
(browse-url (concat "http://localhost:" (number-to-string port) "/")))
;; shorthand for interactive lambdas
(defmacro λ (&rest body)
`(lambda ()
(interactive)
,@body))
(global-set-key (kbd "s-l") (λ (insert "\u03bb")))
;; command to help set up magit-gh-pulls
(defun magit-gh-pulls-setup (repoid)
(interactive "suser/repo: ")
(shell-command "git config --add magit.extension gh-pulls")
(shell-command (concat "git config magit.gh-pulls-repo " repoid)))
;; Increase/decrease selective display
(defun inc-selective-display (arg)
(interactive "P")
(if (numberp arg)
(set-selective-display arg)
(if (numberp selective-display)
(set-selective-display (+ 2 selective-display))
(set-selective-display 2)))
(create-temp-selective-display-keymap))
(defun dec-selective-display ()
(interactive)
(when (and (numberp selective-display)
(> selective-display 2))
(set-selective-display (- selective-display 2)))
(create-temp-selective-display-keymap))
(defun clear-selective-display ()
(interactive)
(when (numberp selective-display)
(set-selective-display nil)))
(defun create-temp-selective-display-keymap ()
(set-temporary-overlay-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "+") 'inc-selective-display)
(define-key map (kbd "-") 'dec-selective-display)
(define-key map (kbd "0") 'clear-selective-display)
map))
(message "Type + to reveal more, - for less, 0 to reset."))
;; Add spaces and proper formatting to display-line-numbers-mode. It uses more room than
;; necessary, but that's not a problem since it's only in use when going to
;; lines.
(setq display-line-numbers-mode (lambda (line)
(propertize
(format (concat " %"
(number-to-string
(length (number-to-string
(line-number-at-pos (point-max)))))
"d ")
line)
'face 'linum)))
(defun isearch-yank-selection ()
"Put selection from buffer into search string."
(interactive)
(when (region-active-p)
(deactivate-mark))
(isearch-yank-internal (lambda () (mark))))
(defun region-as-string ()
(buffer-substring (region-beginning)
(region-end)))
(defun isearch-forward-use-region ()
(interactive)
(when (region-active-p)
(add-to-history 'search-ring (region-as-string))
(deactivate-mark))
(call-interactively 'isearch-forward))
(defun isearch-backward-use-region ()
(interactive)
(when (region-active-p)
(add-to-history 'search-ring (region-as-string))
(deactivate-mark))
(call-interactively 'isearch-backward))
(eval-after-load "multiple-cursors"
'(progn
(unsupported-cmd isearch-forward-use-region ".")
(unsupported-cmd isearch-backward-use-region ".")))
(defun view-url ()
"Open a new buffer containing the contents of URL."
(interactive)
(let* ((default (thing-at-point-url-at-point))
(url (read-from-minibuffer "URL: " default)))
(switch-to-buffer (url-retrieve-synchronously url))
(rename-buffer url t)
;; TODO: switch to nxml/nxhtml mode
(cond ((search-forward "<?xml" nil t) (xml-mode))
((search-forward "<html" nil t) (html-mode)))))
(defun linkify-region-from-kill-ring (start end)
(interactive "r")
(let ((text (buffer-substring start end)))
(delete-region start end)
(insert "<a href=\"")
(yank)
(insert (concat "\">" text "</a>"))))
(defun buffer-to-html (buffer)
(with-current-buffer (htmlize-buffer buffer)
(buffer-string)))
(defun sudo-edit (&optional arg)
(interactive "p")
(if (or arg (not buffer-file-name))
(find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))
(find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
(defun add-file-find-hook-with-pattern (pattern fn &optional contents)
"Add a find-file-hook that calls FN for files where PATTERN
matches the file name, and optionally, where CONTENT matches file contents.
Both PATTERN and CONTENTS are matched as regular expressions."
(lexical-let ((re-pattern pattern)
(fun fn)
(re-content contents))
(add-hook 'find-file-hook
(lambda ()
(if (and
(string-match re-pattern (buffer-file-name))
(or (null re-content)
(string-match re-content
(buffer-substring (point-min) (point-max)))))
(apply fun ()))))))
;; Fix kmacro-edit-lossage, it's normal implementation
;; is bound tightly to C-h
(defun kmacro-edit-lossage ()
"Edit most recent 300 keystrokes as a keyboard macro."
(interactive)
(kmacro-push-ring)
(edit-kbd-macro 'view-lossage))
(defmacro comment (&rest ignore))
(defun what-face (pos)
(interactive "d")
(let ((face (or (get-char-property pos 'read-face-name)
(get-char-property pos 'face))))
(if face (message "Face: %s" face) (message "No face at %d" pos))))
(defun goto-next-line-with-same-indentation ()
(interactive)
(back-to-indentation)
(re-search-forward (s-concat "^" (s-repeat (current-column) " ") "[^ \t\r\n\v\f]")
nil nil (if (= 0 (current-column)) 2 1))
(back-to-indentation))
(defun goto-prev-line-with-same-indentation ()
(interactive)
(back-to-indentation)
(re-search-backward (s-concat "^" (s-repeat (current-column) " ") "[^ \t\r\n\v\f]"))
(back-to-indentation))