Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 314 lines (280 sloc) 11.414 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)
55 (mark-sexp))))
56
57 (defun dss/forward-string (&optional backward)
58 (interactive)
59 (if (dss/in-string-p)
60 (dss/end-of-string))
61 (while (not (dss/in-string-p))
62 (if backward
63 (backward-char)
64 (forward-char))))
65
66 (defun dss/backward-string ()
67 (interactive)
68 (if (dss/in-string-p)
69 (dss/beginning-of-string))
70 (dss/forward-string t)
71 (dss/beginning-of-string)
72 (forward-char))
73
74 ;@@TR: I should add some similar functions for working with comments etc.
75
693a846 @tavisrudd some reorg + tweaks to string motion funcs
authored
76 (defun dss/out-sexp (&optional level forward syntax)
77 "Skip out of any nested brackets.
78 Skip forward if FORWARD is non-nil, else backward.
79 If SYNTAX is non-nil it is the state returned by `syntax-ppss' at point.
80 Return non-nil if and only if skipping was done."
81 (interactive)
82 (if (dss/in-string-p)
83 (dss/beginning-of-string))
84 (progn
85 (let* ((depth (syntax-ppss-depth (or syntax (syntax-ppss))))
86 (level (or level depth))
87 (forward (if forward -1 1)))
88 (unless (zerop depth)
89 (if (> depth 0)
90 ;; Skip forward out of nested brackets.
91 (condition-case () ; beware invalid syntax
92 (progn (backward-up-list (* forward level)) t)
93 (error nil))
94 ;; Invalid syntax (too many closed brackets).
95 ;; Skip out of as many as possible.
96 (let (done)
97 (while (condition-case ()
98 (progn (backward-up-list forward)
99 (setq done t))
100 (error nil)))
101 done))))))
102
103 (defun dss/out-one-sexp (&optional forward)
104 (interactive)
105 (dss/out-sexp 1 forward))
106
107 (defun dss/out-one-sexp-forward ()
108 (interactive)
109 (dss/out-sexp 1 1))
110
9f2f20c @tavisrudd misc reorg and tweaks
authored
111 (defun dss/indent-sexp ()
112 "http://mihai.bazon.net/projects/editing-javascript-with-emacs-js2-mode
113 can be used from any coding major mode"
114 (interactive)
115 (save-restriction
116 (save-excursion
117 (widen)
118 (let* ((inhibit-point-motion-hooks t)
119 (parse-status (syntax-ppss (point)))
120 (beg (nth 1 parse-status))
121 (end-marker (make-marker))
122 (end (progn (goto-char beg) (forward-list) (point)))
123 (ovl (make-overlay beg end)))
124 (set-marker end-marker end)
125 (overlay-put ovl 'face 'highlight)
126 (goto-char beg)
127 (while (< (point) (marker-position end-marker))
128 ;; don't reindent blank lines so we don't set the "buffer
129 ;; modified" property for nothing
130 (beginning-of-line)
131 (unless (looking-at "\\s-*$")
132 (indent-according-to-mode))
133 (forward-line))
134 (run-with-timer 0.5 nil (lambda(ovl)
135 (delete-overlay ovl)) ovl)))))
923a52e @tavisrudd miscellaneous indentation functions
authored
136 (defun dss/indent-defun ()
137 (interactive)
138 (save-excursion
139 (dss/out-sexp)
140 (forward-char)
141 (dss/indent-sexp)))
142
143 (defun dss/copy-defun-name ()
144 (interactive)
145 (save-excursion
146 (dss/out-sexp)
147 (forward-to-word 2)
148 (k2-copy-whole-sexp)))
149
150 (defun dss/goto-defun-args ()
151 (interactive)
152 (dss/out-sexp)
153 (forward-to-word 2)
154 (search-forward "("))
155
156 (defun dss/fix-sexp-whitespace ()
157 (interactive)
158 (save-excursion
159 (dss/out-sexp 1)
160 (forward-char)
161 (fixup-whitespace)))
9f2f20c @tavisrudd misc reorg and tweaks
authored
162
d6c1526 @tavisrudd lots of new helper functions
authored
163 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164 (defun dss/electric-pair ()
165 "If at end of line, insert character pair without surrounding spaces.
166 Otherwise, just insert the typed character."
167 (interactive)
168 ;(if (eolp) (let (parens-require-spaces) (insert-pair))
169 ; (self-insert-command 1)))
170 (if (or (dss/in-string-p)
171 (dss/in-comment-p))
172 (self-insert-command 1)
173 (let (parens-require-spaces)
174 (insert-pair))))
175
176 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
a16e888 @tavisrudd init import
authored
177 (require 'imenu)
178 (defun ido-imenu ()
179 "Update the imenu index and then use ido to select a symbol to navigate to.
180 Symbols matching the text at point are put first in the completion list.
181 Comes from http://github.com/technomancy/emacs-starter-kit/blob/master/starter-kit-defuns.el"
182 (interactive)
183 (imenu--make-index-alist)
184 (let ((name-and-pos '())
185 (symbol-names '()))
186 (flet ((addsymbols (symbol-list)
187 (when (listp symbol-list)
188 (dolist (symbol symbol-list)
189 (let ((name nil) (position nil))
190 (cond
191 ((and (listp symbol) (imenu--subalist-p symbol))
192 (addsymbols symbol))
193
194 ((listp symbol)
195 (setq name (car symbol))
196 (setq position (cdr symbol)))
197
198 ((stringp symbol)
199 (setq name symbol)
200 (setq position (get-text-property 1 'org-imenu-marker symbol))))
201
202 (unless (or (null position) (null name))
203 (add-to-list 'symbol-names name)
204 (add-to-list 'name-and-pos (cons name position))))))))
205 (addsymbols imenu--index-alist))
206 ;; If there are matching symbols at point, put them at the beginning of `symbol-names'.
207 (let ((symbol-at-point (thing-at-point 'symbol)))
208 (when symbol-at-point
209 (let* ((regexp (concat (regexp-quote symbol-at-point) "$"))
210 (matching-symbols (delq nil (mapcar (lambda (symbol)
211 (if (string-match regexp symbol) symbol))
212 symbol-names))))
213 (when matching-symbols
214 (sort matching-symbols (lambda (a b) (> (length a) (length b))))
215 (mapc (lambda (symbol) (setq symbol-names (cons symbol (delete symbol symbol-names))))
216 matching-symbols)))))
217 (let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
218 (position (cdr (assoc selected-symbol name-and-pos))))
219 (goto-char position))))
220
221 ;; ; http://nflath.com/2009/07/imenu/
222 ;; (require 'imenu)
223 ;; (setq imenu-auto-rescan t)
224 ;; (defun ido-goto-symbol ()
225 ;; "Will update the imenu index and then use ido to select a symbol to navigate to"
226 ;; (interactive)
227 ;; (imenu--make-index-alist)
228 ;; (let ((name-and-pos '())
229 ;; (symbol-names '()))
230 ;; (flet ((addsymbols (symbol-list)
231 ;; (when (listp symbol-list)
232 ;; (dolist (symbol symbol-list)
233 ;; (let ((name nil) (position nil))
234 ;; (cond
235 ;; ((and (listp symbol) (imenu--subalist-p symbol))
236 ;; (addsymbols symbol))
237 ;; ((listp symbol)
238 ;; (setq name (car symbol))
239 ;; (setq position (cdr symbol)))
240 ;; ((stringp symbol)
241 ;; (setq name symbol)
242 ;; (setq position (get-text-property 1 'org-imenu-marker symbol))))
243 ;; (unless (or (null position) (null name))
244 ;; (add-to-list 'symbol-names name)
245 ;; (add-to-list 'name-and-pos (cons name position))))))))
246 ;; (addsymbols imenu--index-alist))
247 ;; (let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
248 ;; (position (cdr (assoc selected-symbol name-and-pos))))
249 ;; (if (markerp position)
250 ;; (goto-char position) (goto-char (overlay-start position))))))
251
252
43b578b @tavisrudd misc cleanup and refactoring
authored
253 (require 'column-marker)
7df2889 @tavisrudd column grid
authored
254 (defvar dss/major-column-face 'dss/major-column-face
255 "major column grid marker")
256 (defface dss/major-column-face '((t (:background "#484848")))
257 "major column grid marker"
258 :group 'faces)
259
260 (defvar dss/minor-column-face 'dss/minor-column-face
261 "minor column grid marker")
262 (defface dss/minor-column-face '((t (:background "#2c2c2c")))
263 "minor column grid marker"
264 :group 'faces)
265
266 (column-marker-create dss/column-marker-1 dss/minor-column-face)
267 (column-marker-create dss/column-marker-2 dss/major-column-face)
268 (column-marker-create dss/column-marker-3 dss/minor-column-face)
269 (column-marker-create dss/column-marker-4 dss/major-column-face)
270 (column-marker-create dss/column-marker-5 dss/minor-column-face)
271 (column-marker-create dss/column-marker-6 dss/major-column-face)
272 (column-marker-create dss/column-marker-7 dss/minor-column-face)
273 (column-marker-create dss/column-marker-8 dss/major-column-face)
274
275 (defun dss/column-grid ()
276 (interactive)
277 ;; col-highlight-face
278 (hl-line-mode -1)
279 (dss/column-marker-1 10)
280 (dss/column-marker-2 20)
281 (dss/column-marker-3 30)
282 (dss/column-marker-4 40)
283 (dss/column-marker-5 50)
284 (dss/column-marker-6 60)
285 (dss/column-marker-7 70)
286 (dss/column-marker-8 80))
287
288 (defun dss/column-grid-off ()
289 (interactive)
290 (dss/column-marker-1 -1)
291 (hl-line-mode t))
292
9f2f20c @tavisrudd misc reorg and tweaks
authored
293 (defun dss/local-line-jump (n)
294 (interactive "nLine: ")
295 ;(message (format "%d" (+ n (* (/ (line-number-at-pos) 100) 100))))
1bbe9e0 @tavisrudd (defun dss/local-line-jump-search (n txt)
authored
296 (dss/goto-line (+ n (* (/ (line-number-at-pos) 100) 100)))
297 (back-to-indentation))
298
299 (defun dss/local-line-jump-search (n txt)
300 (interactive "nLine: \nsText:")
301 (dss/local-line-jump n)
302 (search-forward txt (line-end-position)))
9f2f20c @tavisrudd misc reorg and tweaks
authored
303
addf582 @tavisrudd dss/highlight-watchwords
authored
304 (defun dss/highlight-watchwords ()
305 ;; http://github.com/technomancy/emacs-starter-kit/blob/master/starter-kit-defuns.el
306 (interactive)
307 ;; (font-lock-add-keywords
308 ;; nil '(("\\<\\(FIX\\|TODO\\|FIXME\\|HACK\\|@@TR\\|REFACTOR\\)*:"
309 ;; 1 font-lock-warning-face t)))
310 (highlight-regexp "\\<\\(FIXME\\|FIX\\|TODO\\|HACK\\|TR\\|REFACTOR\\):?" 'font-lock-warning-face))
311
a16e888 @tavisrudd init import
authored
312 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
313 (provide 'dss-codenav-helpers)
Something went wrong with that request. Please try again.