Skip to content
Newer
Older
100644 354 lines (312 sloc) 12.1 KB
43b578b @tavisrudd misc cleanup and refactoring
authored Aug 19, 2010
1 (require 'dss-basic-editing)
2 (require 'k2-mode)
d6c1526 @tavisrudd lots of new helper functions
authored Jun 9, 2010
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 Jun 25, 2010
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 Jun 10, 2010
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 Jun 25, 2010
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 Jun 10, 2010
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 Nov 12, 2010
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 Jun 10, 2010
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 Jun 25, 2010
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 Nov 13, 2010
111 ;; (bounds-of-thing-at-point 'sexp)
693a846 @tavisrudd some reorg + tweaks to string motion funcs
authored Jun 25, 2010
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
26b3193 @tavisrudd dss/flash-region and various tweaks
authored Nov 13, 2010
120 (defun dss/flash-region (beg end)
121 (interactive "r")
122 (let ((ovl (make-overlay beg end))
123 (was-mark-active mark-active)
124 (hl-line-mode-on hl-line-mode))
125 (setq mark-active nil)
126 (overlay-put ovl 'face 'highlight)
127 (run-with-timer 0.5 nil
128 (lambda(ovl was-mark-active)
129 (delete-overlay ovl)
130 (setq mark-active was-mark-active))
131 ovl was-mark-active)))
132
9f2f20c @tavisrudd misc reorg and tweaks
authored Jun 30, 2010
133 (defun dss/indent-sexp ()
134 "http://mihai.bazon.net/projects/editing-javascript-with-emacs-js2-mode
135 can be used from any coding major mode"
136 (interactive)
137 (save-restriction
138 (save-excursion
139 (widen)
140 (let* ((inhibit-point-motion-hooks t)
141 (parse-status (syntax-ppss (point)))
142 (beg (nth 1 parse-status))
143 (end-marker (make-marker))
26b3193 @tavisrudd dss/flash-region and various tweaks
authored Nov 13, 2010
144 (end (progn (goto-char beg) (forward-list) (point))))
145 (dss/flash-region beg end)
9f2f20c @tavisrudd misc reorg and tweaks
authored Jul 1, 2010
146 (set-marker end-marker end)
147 (goto-char beg)
148 (while (< (point) (marker-position end-marker))
149 ;; don't reindent blank lines so we don't set the "buffer
150 ;; modified" property for nothing
151 (beginning-of-line)
152 (unless (looking-at "\\s-*$")
153 (indent-according-to-mode))
26b3193 @tavisrudd dss/flash-region and various tweaks
authored Nov 13, 2010
154 (forward-line))))))
155
156
923a52e @tavisrudd miscellaneous indentation functions
authored Aug 12, 2010
157 (defun dss/indent-defun ()
158 (interactive)
159 (save-excursion
160 (dss/out-sexp)
161 (forward-char)
162 (dss/indent-sexp)))
163
164 (defun dss/copy-defun-name ()
165 (interactive)
166 (save-excursion
167 (dss/out-sexp)
168 (forward-to-word 2)
169 (k2-copy-whole-sexp)))
170
d657d0c @tavisrudd added dss/eval-defun
authored Oct 27, 2010
171 (defun dss/eval-defun ()
172 "The built-in eval-defun doesn't choose the top level forms I would expect expect"
173 (interactive)
26b3193 @tavisrudd dss/flash-region and various tweaks
authored Nov 13, 2010
174 (dss/indent-defun)
d657d0c @tavisrudd added dss/eval-defun
authored Oct 27, 2010
175 (save-excursion
176 (dss/out-sexp nil t)
605352b @tavisrudd special case dss/eval-defun in clojure
authored Feb 24, 2011
177 (cond ((or (equal major-mode 'clojure-mode)
178 (equal major-mode 'slime-repl-mode))
179 (slime-eval-last-expression))
180 (t (eval-last-sexp nil)))
181 ))
d657d0c @tavisrudd added dss/eval-defun
authored Oct 27, 2010
182 ;; (message "%S" (preceding-sexp))
183
184
923a52e @tavisrudd miscellaneous indentation functions
authored Aug 13, 2010
185 (defun dss/goto-defun-args ()
186 (interactive)
187 (dss/out-sexp)
188 (forward-to-word 2)
189 (search-forward "("))
190
191 (defun dss/fix-sexp-whitespace ()
192 (interactive)
193 (save-excursion
194 (dss/out-sexp 1)
195 (forward-char)
196 (fixup-whitespace)))
9f2f20c @tavisrudd misc reorg and tweaks
authored Jul 1, 2010
197
d6c1526 @tavisrudd lots of new helper functions
authored Jun 10, 2010
198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
199 (defun dss/electric-pair ()
200 "If at end of line, insert character pair without surrounding spaces.
201 Otherwise, just insert the typed character."
202 (interactive)
203 ;(if (eolp) (let (parens-require-spaces) (insert-pair))
204 ; (self-insert-command 1)))
205 (if (or (dss/in-string-p)
206 (dss/in-comment-p))
207 (self-insert-command 1)
208 (let (parens-require-spaces)
209 (insert-pair))))
210
211 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
a16e888 @tavisrudd init import
authored Apr 23, 2010
212 (require 'imenu)
213 (defun ido-imenu ()
214 "Update the imenu index and then use ido to select a symbol to navigate to.
215 Symbols matching the text at point are put first in the completion list.
216 Comes from http://github.com/technomancy/emacs-starter-kit/blob/master/starter-kit-defuns.el"
217 (interactive)
218 (imenu--make-index-alist)
219 (let ((name-and-pos '())
220 (symbol-names '()))
221 (flet ((addsymbols (symbol-list)
222 (when (listp symbol-list)
223 (dolist (symbol symbol-list)
224 (let ((name nil) (position nil))
225 (cond
226 ((and (listp symbol) (imenu--subalist-p symbol))
227 (addsymbols symbol))
228
229 ((listp symbol)
230 (setq name (car symbol))
231 (setq position (cdr symbol)))
232
233 ((stringp symbol)
234 (setq name symbol)
235 (setq position (get-text-property 1 'org-imenu-marker symbol))))
236
237 (unless (or (null position) (null name))
238 (add-to-list 'symbol-names name)
239 (add-to-list 'name-and-pos (cons name position))))))))
240 (addsymbols imenu--index-alist))
241 ;; If there are matching symbols at point, put them at the beginning of `symbol-names'.
242 (let ((symbol-at-point (thing-at-point 'symbol)))
243 (when symbol-at-point
244 (let* ((regexp (concat (regexp-quote symbol-at-point) "$"))
245 (matching-symbols (delq nil (mapcar (lambda (symbol)
246 (if (string-match regexp symbol) symbol))
247 symbol-names))))
248 (when matching-symbols
249 (sort matching-symbols (lambda (a b) (> (length a) (length b))))
250 (mapc (lambda (symbol) (setq symbol-names (cons symbol (delete symbol symbol-names))))
251 matching-symbols)))))
252 (let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
253 (position (cdr (assoc selected-symbol name-and-pos))))
254 (goto-char position))))
255
256 ;; ; http://nflath.com/2009/07/imenu/
257 ;; (require 'imenu)
258 ;; (setq imenu-auto-rescan t)
259 ;; (defun ido-goto-symbol ()
260 ;; "Will update the imenu index and then use ido to select a symbol to navigate to"
261 ;; (interactive)
262 ;; (imenu--make-index-alist)
263 ;; (let ((name-and-pos '())
264 ;; (symbol-names '()))
265 ;; (flet ((addsymbols (symbol-list)
266 ;; (when (listp symbol-list)
267 ;; (dolist (symbol symbol-list)
268 ;; (let ((name nil) (position nil))
269 ;; (cond
270 ;; ((and (listp symbol) (imenu--subalist-p symbol))
271 ;; (addsymbols symbol))
272 ;; ((listp symbol)
273 ;; (setq name (car symbol))
274 ;; (setq position (cdr symbol)))
275 ;; ((stringp symbol)
276 ;; (setq name symbol)
277 ;; (setq position (get-text-property 1 'org-imenu-marker symbol))))
278 ;; (unless (or (null position) (null name))
279 ;; (add-to-list 'symbol-names name)
280 ;; (add-to-list 'name-and-pos (cons name position))))))))
281 ;; (addsymbols imenu--index-alist))
282 ;; (let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
283 ;; (position (cdr (assoc selected-symbol name-and-pos))))
284 ;; (if (markerp position)
285 ;; (goto-char position) (goto-char (overlay-start position))))))
286
287
43b578b @tavisrudd misc cleanup and refactoring
authored Aug 19, 2010
288 (require 'column-marker)
7df2889 @tavisrudd column grid
authored May 31, 2010
289 (defvar dss/major-column-face 'dss/major-column-face
290 "major column grid marker")
291 (defface dss/major-column-face '((t (:background "#484848")))
292 "major column grid marker"
293 :group 'faces)
294
295 (defvar dss/minor-column-face 'dss/minor-column-face
296 "minor column grid marker")
297 (defface dss/minor-column-face '((t (:background "#2c2c2c")))
298 "minor column grid marker"
299 :group 'faces)
300
301 (column-marker-create dss/column-marker-1 dss/minor-column-face)
302 (column-marker-create dss/column-marker-2 dss/major-column-face)
303 (column-marker-create dss/column-marker-3 dss/minor-column-face)
304 (column-marker-create dss/column-marker-4 dss/major-column-face)
305 (column-marker-create dss/column-marker-5 dss/minor-column-face)
306 (column-marker-create dss/column-marker-6 dss/major-column-face)
307 (column-marker-create dss/column-marker-7 dss/minor-column-face)
308 (column-marker-create dss/column-marker-8 dss/major-column-face)
309
310 (defun dss/column-grid ()
311 (interactive)
312 ;; col-highlight-face
313 (hl-line-mode -1)
314 (dss/column-marker-1 10)
315 (dss/column-marker-2 20)
316 (dss/column-marker-3 30)
317 (dss/column-marker-4 40)
318 (dss/column-marker-5 50)
319 (dss/column-marker-6 60)
320 (dss/column-marker-7 70)
321 (dss/column-marker-8 80))
322
323 (defun dss/column-grid-off ()
324 (interactive)
325 (dss/column-marker-1 -1)
326 (hl-line-mode t))
327
99da27f @tavisrudd line jumping tweaks
authored Nov 17, 2010
328 (defun dss/line-jump (n)
329 (interactive "nLine: ")
330 (dss/goto-line n)
331 (back-to-indentation))
332
9f2f20c @tavisrudd misc reorg and tweaks
authored Jul 1, 2010
333 (defun dss/local-line-jump (n)
334 (interactive "nLine: ")
d657d0c @tavisrudd added dss/eval-defun
authored Oct 27, 2010
335 (if (> n 100)
99da27f @tavisrudd line jumping tweaks
authored Nov 17, 2010
336 (dss/line-jump n)
337 (dss/line-jump (+ n (* (/ (line-number-at-pos) 100) 100)))))
1bbe9e0 @tavisrudd (defun dss/local-line-jump-search (n txt)
authored Sep 27, 2010
338
339 (defun dss/local-line-jump-search (n txt)
340 (interactive "nLine: \nsText:")
341 (dss/local-line-jump n)
342 (search-forward txt (line-end-position)))
9f2f20c @tavisrudd misc reorg and tweaks
authored Jul 1, 2010
343
addf582 @tavisrudd dss/highlight-watchwords
authored Sep 19, 2010
344 (defun dss/highlight-watchwords ()
345 ;; http://github.com/technomancy/emacs-starter-kit/blob/master/starter-kit-defuns.el
346 (interactive)
347 ;; (font-lock-add-keywords
348 ;; nil '(("\\<\\(FIX\\|TODO\\|FIXME\\|HACK\\|@@TR\\|REFACTOR\\)*:"
349 ;; 1 font-lock-warning-face t)))
350 (highlight-regexp "\\<\\(FIXME\\|FIX\\|TODO\\|HACK\\|TR\\|REFACTOR\\):?" 'font-lock-warning-face))
351
a16e888 @tavisrudd init import
authored Apr 23, 2010
352 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
353 (provide 'dss-codenav-helpers)
Something went wrong with that request. Please try again.