Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 383 lines (335 sloc) 13.181 kb
43b578b @tavisrudd misc cleanup and refactoring
authored
1 (require 'dss-basic-editing)
2 (require 'k2-mode)
d6c1526 @tavisrudd lots of new helper functions
authored
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 (defun dss/display-syntax (syntax-type)
5 (eq syntax-type (syntax-ppss-context (syntax-ppss))))
6
7 (defun dss/in-syntax-p (syntax-type)
8 "This only answers if you're in a comment or string at the moment."
9 (eq syntax-type (syntax-ppss-context (syntax-ppss))))
10
11 (defun dss/in-string-p ()
12 (dss/in-syntax-p 'string))
13
14 (defun dss/in-comment-p ()
15 (dss/in-syntax-p 'comment))
16
17 (defun dss/blank-line-p ()
18 "Return non-nil iff current line is blank."
19 (save-excursion
20 (beginning-of-line)
21 (looking-at "\\s-*$")))
22
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24 (defun dss/beginning-of-string ()
25 "Go to beginning of string around point.
26 Do nothing if not in string."
27 ;; from loveshack's python-beginning-of-string
28 (interactive)
693a846 @tavisrudd some reorg + tweaks to string motion funcs
authored
29 (if (and (not (dss/in-string-p))
30 (save-excursion
31 (backward-char)
32 (dss/in-string-p)))
33 (backward-char))
d6c1526 @tavisrudd lots of new helper functions
authored
34 (let ((state (syntax-ppss)))
35 (when (eq 'string (syntax-ppss-context state))
36 (goto-char (nth 8 state)))))
37
38 (defun dss/end-of-string ()
39 (interactive)
693a846 @tavisrudd some reorg + tweaks to string motion funcs
authored
40 (if (and (not (dss/in-string-p))
41 (save-excursion
42 (forward-char)
43 (dss/in-string-p)))
44 (forward-char))
d6c1526 @tavisrudd lots of new helper functions
authored
45 (if (dss/in-string-p)
46 (progn
47 (dss/beginning-of-string)
48 (forward-sexp))))
49
50 (defun dss/mark-string ()
51 (interactive)
52 (if (dss/in-string-p)
53 (progn
54 (dss/beginning-of-string)
26b3193 @tavisrudd dss/flash-region and various tweaks
authored
55 ;; (mark-sexp) ; vs
56 (forward-char)
57 (push-mark nil nil t)
58
59
60 (dss/end-of-string)
61 (backward-char)
62 ;;
63 )))
d6c1526 @tavisrudd lots of new helper functions
authored
64
65 (defun dss/forward-string (&optional backward)
66 (interactive)
67 (if (dss/in-string-p)
68 (dss/end-of-string))
69 (while (not (dss/in-string-p))
70 (if backward
71 (backward-char)
72 (forward-char))))
73
74 (defun dss/backward-string ()
75 (interactive)
76 (if (dss/in-string-p)
77 (dss/beginning-of-string))
78 (dss/forward-string t)
79 (dss/beginning-of-string)
80 (forward-char))
81
82 ;@@TR: I should add some similar functions for working with comments etc.
83
693a846 @tavisrudd some reorg + tweaks to string motion funcs
authored
84 (defun dss/out-sexp (&optional level forward syntax)
85 "Skip out of any nested brackets.
86 Skip forward if FORWARD is non-nil, else backward.
87 If SYNTAX is non-nil it is the state returned by `syntax-ppss' at point.
88 Return non-nil if and only if skipping was done."
89 (interactive)
90 (if (dss/in-string-p)
91 (dss/beginning-of-string))
92 (progn
93 (let* ((depth (syntax-ppss-depth (or syntax (syntax-ppss))))
94 (level (or level depth))
95 (forward (if forward -1 1)))
96 (unless (zerop depth)
97 (if (> depth 0)
98 ;; Skip forward out of nested brackets.
99 (condition-case () ; beware invalid syntax
100 (progn (backward-up-list (* forward level)) t)
101 (error nil))
102 ;; Invalid syntax (too many closed brackets).
103 ;; Skip out of as many as possible.
104 (let (done)
105 (while (condition-case ()
106 (progn (backward-up-list forward)
107 (setq done t))
108 (error nil)))
109 done))))))
110
26b3193 @tavisrudd dss/flash-region and various tweaks
authored
111 ;; (bounds-of-thing-at-point 'sexp)
693a846 @tavisrudd some reorg + tweaks to string motion funcs
authored
112 (defun dss/out-one-sexp (&optional forward)
113 (interactive)
114 (dss/out-sexp 1 forward))
115
116 (defun dss/out-one-sexp-forward ()
117 (interactive)
118 (dss/out-sexp 1 1))
119
63b2f35 @tavisrudd added missing require
authored
120 (require 'hl-line)
121
26b3193 @tavisrudd dss/flash-region and various tweaks
authored
122 (defun dss/flash-region (beg end)
123 (interactive "r")
124 (let ((ovl (make-overlay beg end))
125 (was-mark-active mark-active)
126 (hl-line-mode-on hl-line-mode))
127 (setq mark-active nil)
128 (overlay-put ovl 'face 'highlight)
129 (run-with-timer 0.5 nil
130 (lambda(ovl was-mark-active)
131 (delete-overlay ovl)
132 (setq mark-active was-mark-active))
133 ovl was-mark-active)))
134
9f2f20c @tavisrudd misc reorg and tweaks
authored
135 (defun dss/indent-sexp ()
136 "http://mihai.bazon.net/projects/editing-javascript-with-emacs-js2-mode
137 can be used from any coding major mode"
138 (interactive)
139 (save-restriction
140 (save-excursion
141 (widen)
142 (let* ((inhibit-point-motion-hooks t)
143 (parse-status (syntax-ppss (point)))
144 (beg (nth 1 parse-status))
145 (end-marker (make-marker))
26b3193 @tavisrudd dss/flash-region and various tweaks
authored
146 (end (progn (goto-char beg) (forward-list) (point))))
147 (dss/flash-region beg end)
9f2f20c @tavisrudd misc reorg and tweaks
authored
148 (set-marker end-marker end)
149 (goto-char beg)
150 (while (< (point) (marker-position end-marker))
151 ;; don't reindent blank lines so we don't set the "buffer
152 ;; modified" property for nothing
153 (beginning-of-line)
154 (unless (looking-at "\\s-*$")
155 (indent-according-to-mode))
26b3193 @tavisrudd dss/flash-region and various tweaks
authored
156 (forward-line))))))
157
158
923a52e @tavisrudd miscellaneous indentation functions
authored
159 (defun dss/indent-defun ()
160 (interactive)
161 (save-excursion
162 (dss/out-sexp)
163 (forward-char)
164 (dss/indent-sexp)))
165
d0dc654 @tavisrudd some refactoring to dss/copy-defun-name
authored
166 (defun dss/defun-name ()
923a52e @tavisrudd miscellaneous indentation functions
authored
167 (interactive)
168 (save-excursion
169 (dss/out-sexp)
170 (forward-to-word 2)
d0dc654 @tavisrudd some refactoring to dss/copy-defun-name
authored
171 (mark-sexp 1)
172 (let ((defun-name (buffer-substring (region-beginning) (region-end))))
173 (if defun-name
174 (set-text-properties 0 (length defun-name) nil defun-name))
175 defun-name)))
176
177 (defun dss/copy-defun-name ()
178 (interactive)
179 (let ((defun-name (dss/defun-name)))
180 (kill-new defun-name)
181 (message defun-name)))
923a52e @tavisrudd miscellaneous indentation functions
authored
182
d657d0c @tavisrudd added dss/eval-defun
authored
183 (defun dss/eval-defun ()
184 "The built-in eval-defun doesn't choose the top level forms I would expect expect"
185 (interactive)
26b3193 @tavisrudd dss/flash-region and various tweaks
authored
186 (dss/indent-defun)
d657d0c @tavisrudd added dss/eval-defun
authored
187 (save-excursion
188 (dss/out-sexp nil t)
605352b @tavisrudd special case dss/eval-defun in clojure
authored
189 (cond ((or (equal major-mode 'clojure-mode)
190 (equal major-mode 'slime-repl-mode))
191 (slime-eval-last-expression))
f903911 @tavisrudd update smex after dss/eval-defun
authored
192 (t (progn
193 (eval-last-sexp nil)
194 (smex-update))))))
195
d657d0c @tavisrudd added dss/eval-defun
authored
196 ;; (message "%S" (preceding-sexp))
197
198
a6c9341 @tavisrudd added a few functions for jumping around parts of lisp defuns
authored
199 (defun dss/goto-defun-name ()
923a52e @tavisrudd miscellaneous indentation functions
authored
200 (interactive)
201 (dss/out-sexp)
a6c9341 @tavisrudd added a few functions for jumping around parts of lisp defuns
authored
202 (forward-to-word 2))
203
204 (defun dss/goto-defun-docstring ()
205 "Jumps to the first quote in the defun form. If there is no
206 docstring it just jumps forward to the first quote anyway. I
207 should make this smarter and have it automatically insert the
208 docstring if one does not exist."
209
210 (interactive)
211 (dss/goto-defun-name)
212 (search-forward-regexp "\""))
213
214 (defun dss/goto-defun-args ()
215 (interactive)
216 (dss/goto-defun-name)
217 (search-forward-regexp "(\\|\\["))
923a52e @tavisrudd miscellaneous indentation functions
authored
218
219 (defun dss/fix-sexp-whitespace ()
220 (interactive)
221 (save-excursion
222 (dss/out-sexp 1)
223 (forward-char)
224 (fixup-whitespace)))
9f2f20c @tavisrudd misc reorg and tweaks
authored
225
d6c1526 @tavisrudd lots of new helper functions
authored
226 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227 (defun dss/electric-pair ()
228 "If at end of line, insert character pair without surrounding spaces.
229 Otherwise, just insert the typed character."
230 (interactive)
231 ;(if (eolp) (let (parens-require-spaces) (insert-pair))
232 ; (self-insert-command 1)))
233 (if (or (dss/in-string-p)
234 (dss/in-comment-p))
235 (self-insert-command 1)
236 (let (parens-require-spaces)
237 (insert-pair))))
238
239 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
a16e888 @tavisrudd init import
authored
240 (require 'imenu)
241 (defun ido-imenu ()
242 "Update the imenu index and then use ido to select a symbol to navigate to.
243 Symbols matching the text at point are put first in the completion list.
244 Comes from http://github.com/technomancy/emacs-starter-kit/blob/master/starter-kit-defuns.el"
245 (interactive)
246 (imenu--make-index-alist)
247 (let ((name-and-pos '())
248 (symbol-names '()))
249 (flet ((addsymbols (symbol-list)
250 (when (listp symbol-list)
251 (dolist (symbol symbol-list)
252 (let ((name nil) (position nil))
253 (cond
254 ((and (listp symbol) (imenu--subalist-p symbol))
255 (addsymbols symbol))
256
257 ((listp symbol)
258 (setq name (car symbol))
259 (setq position (cdr symbol)))
260
261 ((stringp symbol)
262 (setq name symbol)
263 (setq position (get-text-property 1 'org-imenu-marker symbol))))
264
265 (unless (or (null position) (null name))
266 (add-to-list 'symbol-names name)
267 (add-to-list 'name-and-pos (cons name position))))))))
268 (addsymbols imenu--index-alist))
269 ;; If there are matching symbols at point, put them at the beginning of `symbol-names'.
270 (let ((symbol-at-point (thing-at-point 'symbol)))
271 (when symbol-at-point
272 (let* ((regexp (concat (regexp-quote symbol-at-point) "$"))
273 (matching-symbols (delq nil (mapcar (lambda (symbol)
274 (if (string-match regexp symbol) symbol))
275 symbol-names))))
276 (when matching-symbols
277 (sort matching-symbols (lambda (a b) (> (length a) (length b))))
278 (mapc (lambda (symbol) (setq symbol-names (cons symbol (delete symbol symbol-names))))
279 matching-symbols)))))
280 (let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
281 (position (cdr (assoc selected-symbol name-and-pos))))
282 (goto-char position))))
283
284 ;; ; http://nflath.com/2009/07/imenu/
285 ;; (require 'imenu)
286 ;; (setq imenu-auto-rescan t)
287 ;; (defun ido-goto-symbol ()
288 ;; "Will update the imenu index and then use ido to select a symbol to navigate to"
289 ;; (interactive)
290 ;; (imenu--make-index-alist)
291 ;; (let ((name-and-pos '())
292 ;; (symbol-names '()))
293 ;; (flet ((addsymbols (symbol-list)
294 ;; (when (listp symbol-list)
295 ;; (dolist (symbol symbol-list)
296 ;; (let ((name nil) (position nil))
297 ;; (cond
298 ;; ((and (listp symbol) (imenu--subalist-p symbol))
299 ;; (addsymbols symbol))
300 ;; ((listp symbol)
301 ;; (setq name (car symbol))
302 ;; (setq position (cdr symbol)))
303 ;; ((stringp symbol)
304 ;; (setq name symbol)
305 ;; (setq position (get-text-property 1 'org-imenu-marker symbol))))
306 ;; (unless (or (null position) (null name))
307 ;; (add-to-list 'symbol-names name)
308 ;; (add-to-list 'name-and-pos (cons name position))))))))
309 ;; (addsymbols imenu--index-alist))
310 ;; (let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
311 ;; (position (cdr (assoc selected-symbol name-and-pos))))
312 ;; (if (markerp position)
313 ;; (goto-char position) (goto-char (overlay-start position))))))
314
315de68 @tavisrudd (setq imenu-auto-rescan t)
authored
315 (setq imenu-auto-rescan t)
a16e888 @tavisrudd init import
authored
316
43b578b @tavisrudd misc cleanup and refactoring
authored
317 (require 'column-marker)
7df2889 @tavisrudd column grid
authored
318 (defvar dss/major-column-face 'dss/major-column-face
319 "major column grid marker")
320 (defface dss/major-column-face '((t (:background "#484848")))
321 "major column grid marker"
322 :group 'faces)
323
324 (defvar dss/minor-column-face 'dss/minor-column-face
325 "minor column grid marker")
326 (defface dss/minor-column-face '((t (:background "#2c2c2c")))
327 "minor column grid marker"
328 :group 'faces)
329
330 (column-marker-create dss/column-marker-1 dss/minor-column-face)
331 (column-marker-create dss/column-marker-2 dss/major-column-face)
332 (column-marker-create dss/column-marker-3 dss/minor-column-face)
333 (column-marker-create dss/column-marker-4 dss/major-column-face)
334 (column-marker-create dss/column-marker-5 dss/minor-column-face)
335 (column-marker-create dss/column-marker-6 dss/major-column-face)
336 (column-marker-create dss/column-marker-7 dss/minor-column-face)
337 (column-marker-create dss/column-marker-8 dss/major-column-face)
338
339 (defun dss/column-grid ()
340 (interactive)
341 ;; col-highlight-face
342 (hl-line-mode -1)
343 (dss/column-marker-1 10)
344 (dss/column-marker-2 20)
345 (dss/column-marker-3 30)
346 (dss/column-marker-4 40)
347 (dss/column-marker-5 50)
348 (dss/column-marker-6 60)
349 (dss/column-marker-7 70)
350 (dss/column-marker-8 80))
351
352 (defun dss/column-grid-off ()
353 (interactive)
354 (dss/column-marker-1 -1)
355 (hl-line-mode t))
356
99da27f @tavisrudd line jumping tweaks
authored
357 (defun dss/line-jump (n)
358 (interactive "nLine: ")
359 (dss/goto-line n)
360 (back-to-indentation))
361
9f2f20c @tavisrudd misc reorg and tweaks
authored
362 (defun dss/local-line-jump (n)
363 (interactive "nLine: ")
d657d0c @tavisrudd added dss/eval-defun
authored
364 (if (> n 100)
99da27f @tavisrudd line jumping tweaks
authored
365 (dss/line-jump n)
366 (dss/line-jump (+ n (* (/ (line-number-at-pos) 100) 100)))))
1bbe9e0 @tavisrudd (defun dss/local-line-jump-search (n txt)
authored
367
368 (defun dss/local-line-jump-search (n txt)
369 (interactive "nLine: \nsText:")
370 (dss/local-line-jump n)
617e470 @tavisrudd dss/local-line-jump-search now converts the search string to lowercase
authored
371 (search-forward (downcase txt) (line-end-position)))
9f2f20c @tavisrudd misc reorg and tweaks
authored
372
addf582 @tavisrudd dss/highlight-watchwords
authored
373 (defun dss/highlight-watchwords ()
374 ;; http://github.com/technomancy/emacs-starter-kit/blob/master/starter-kit-defuns.el
375 (interactive)
376 ;; (font-lock-add-keywords
377 ;; nil '(("\\<\\(FIX\\|TODO\\|FIXME\\|HACK\\|@@TR\\|REFACTOR\\)*:"
378 ;; 1 font-lock-warning-face t)))
379 (highlight-regexp "\\<\\(FIXME\\|FIX\\|TODO\\|HACK\\|TR\\|REFACTOR\\):?" 'font-lock-warning-face))
380
a16e888 @tavisrudd init import
authored
381 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
382 (provide 'dss-codenav-helpers)
Something went wrong with that request. Please try again.