My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
;-*- coding: utf-8 -*-

(require 'redo "redo.elc" t) ; for redo shortcut

(delete-selection-mode 1) ; turn on text selection highlighting and make typing override selected text (Note: when delete-selection-mode is on, then transient-mark-mode is automatically on too.)

(defun print-buffer-confirm ()
"Print current buffer, but ask for confirmation first."
(interactive)
(when
(y-or-n-p "Print current buffer?")
(print-buffer)
)
)

(defun call-keyword-completion ()
"Call the command that has keyboard shortcut M-TAB."
(interactive)
(call-interactively (key-binding (kbd "M-TAB")))
)

(defun describe-major-mode ()
"Show inline doc for current major-mode."
;; code by Kevin Rodgers. 2009-02-25
(interactive)
(describe-function major-mode))

(defun copy-all ()
"Put the whole buffer content into the kill-ring.
If narrow-to-region is in effect, then copy that region only."
(interactive)
(kill-new (buffer-string))
(message "Buffer content copied copy-region-as-kill")
)

(defun cut-all ()
"Cut the whole buffer content into the kill-ring.
If narrow-to-region is in effect, then cut that region only."
(interactive)
(kill-region (point-min) (point-max))
(message "Buffer content cut")
)

(defadvice kill-ring-save (before slick-copy activate compile)
"When called interactively with no active region, copy the current line."
(interactive
(if mark-active
(list (region-beginning) (region-end))
(progn
(message "Current line is copied.")
(list (line-beginning-position) (line-beginning-position 2)) ) ) ))

(defadvice kill-region (before slick-copy activate compile)
"When called interactively with no active region, cut the current line."
(interactive
(if mark-active
(list (region-beginning) (region-end))
(progn
(list (line-beginning-position) (line-beginning-position 2)) ) ) ))

;;; CURSOR MOVEMENT

(defun forward-open-bracket ()
"Move cursor to the next occurrence of left bracket or quotation mark."
(interactive)
(forward-char 1)
(search-forward-regexp (regexp-opt '( "(" "{" "[" "<" "〔" "【" "〖" "〈" "「" "『" "“" "‘" "‹" "«")))
;; (search-forward-regexp "\\s(\\|\\s\"\\|<\\|“\\|‘\\|‹") ; using syntax table
(backward-char 1)
)

(defun backward-open-bracket ()
"Move cursor to the previous occurrence of left bracket or quotation mark.."
(interactive)
(search-backward-regexp (regexp-opt '( "(" "{" "[" "<" "〔" "【" "〖" "〈" "「" "『" "“" "‘" "‹" "«")))
)

(defun forward-close-bracket ()
"Move cursor to the next occurrence of right bracket or quotation mark."
(interactive)
(search-forward-regexp (regexp-opt '( ")" "\\]" "}" ">" "〕" "】" "〗" "〉" "」" "』" "”" "’" "›" "»")))
;; (search-forward-regexp "\\s)\\|\\s\"\\|>\\|”\\|’\\|›") ;using syntax table
)

(defun backward-close-bracket ()
"Move cursor to the previous occurrence of right bracket or quotation mark."
(interactive)
(backward-char 1)
(search-backward-regexp (regexp-opt '( ")" "\\]" "}" ">" "〕" "】" "〗" "〉" "」" "』" "”" "’" "›" "»")))
(forward-char 1)
)

(defun forward-block ()
"Move cursor forward to next occurrence of double newline character.
In most major modes, this is the same as `forward-paragraph', however,
this function behaves the same in any mode.
“forward-paragraph” is mode dependent, because it depends on
syntax table that has different meaning for “paragraph”."
(interactive)
(skip-chars-forward "\n")
(when (not (search-forward-regexp "\n[[:blank:]]*\n" nil t))
(goto-char (point-max)) ) )

(defun backward-block ()
"Move cursor backward to previous occurrence of double newline char.
See: `forward-block'"
(interactive)
(skip-chars-backward "\n")
(when (not (search-backward-regexp "\n[[:blank:]]*\n" nil t))
(goto-char (point-min))
)
)

;;; TEXT SELECTION RELATED

(defun select-current-line ()
"Select the current line"
(interactive)
(end-of-line) ; move to end of line
(set-mark (line-beginning-position)))

(defun select-current-block ()
"Select the current block of next between empty lines."
(interactive)
(let (p1 p2)
(progn
(if (re-search-backward "\n[ \t]*\n" nil "move")
(progn (re-search-forward "\n[ \t]*\n")
(setq p1 (point) ) )
(setq p1 (point) )
)
(if (re-search-forward "\n[ \t]*\n" nil "move")
(progn (re-search-backward "\n[ \t]*\n")
(setq p2 (point) ))
(setq p2 (point) ) ) )
(set-mark p1) ) )

(defun select-text-in-quote ()
"Select text between the nearest left and right delimiters.
Delimiters are paired characters:
() [] {} «» ‹› “” 〖〗 【】 「」 『』 () 〈〉 《》 〔〕 ⦗⦘ 〘〙 ⦅⦆ 〚〛 ⦃⦄
For practical purposes, also: \" \" ' '"
(interactive)
(let (p1)
(skip-chars-backward "^<>([{“「『‹«(〈《〔【〖⦗〘⦅〚⦃\"'")
(setq p1 (point))
(skip-chars-forward "^<>)]}”」』›»)〉》〕】〗⦘〙⦆〛⦄\"'")
(set-mark p1)
)
)

;; by Nikolaj Schumacher, 2008-10-20. Released under GPL.
(defun semnav-up (arg)
(interactive "p")
(when (nth 3 (syntax-ppss))
(if (> arg 0)
(progn
(skip-syntax-forward "^\"")
(goto-char (1+ (point)))
(decf arg))
(skip-syntax-backward "^\"")
(goto-char (1- (point)))
(incf arg)))
(up-list arg))

;; by Nikolaj Schumacher, 2008-10-20. Released under GPL.
(defun extend-selection (arg &optional incremental)
"Select the current word.
Subsequent calls expands the selection to larger semantic unit."
(interactive (list (prefix-numeric-value current-prefix-arg)
(or (and transient-mark-mode mark-active)
(eq last-command this-command))))
(if incremental
(progn
(semnav-up (- arg))
(forward-sexp)
(mark-sexp -1))
(if (> arg 1)
(extend-selection (1- arg) t)
(if (looking-at "\\=\\(\\s_\\|\\sw\\)*\\_>")
(goto-char (match-end 0))
(unless (memq (char-before) '(?\) ?\"))
(forward-sexp)))
(mark-sexp -1))))

;;; TEXT TRANSFORMATION RELATED

(defun kill-line-backward ()
"Kill text between the beginning of the line to the cursor position.
If there's no text, delete the previous line ending."
(interactive)
(if (looking-back "\n")
(delete-char -1)
(kill-line 0)
)
)

(defun move-cursor-next-pane ()
"Move cursor to the next pane."
(interactive)
(other-window 1)
)

(defun move-cursor-previous-pane ()
"Move cursor to the previous pane."
(interactive)
(other-window -1)
)

(defun compact-uncompact-block ()
"Remove or add line ending chars on current paragraph.
This command is similar to a toggle of `fill-paragraph'.
When there is a text selection, act on the region."
(interactive)

;; This command symbol has a property “'stateIsCompact-p”.
(let (currentStateIsCompact (bigFillColumnVal 4333999) (deactivate-mark nil))

(save-excursion
;; Determine whether the text is currently compact.
(setq currentStateIsCompact
(if (eq last-command this-command)
(get this-command 'stateIsCompact-p)
(if (> (- (line-end-position) (line-beginning-position)) fill-column) t nil) ) )

(if (region-active-p)
(if currentStateIsCompact
(fill-region (region-beginning) (region-end))
(let ((fill-column bigFillColumnVal))
(fill-region (region-beginning) (region-end))) )
(if currentStateIsCompact
(fill-paragraph nil)
(let ((fill-column bigFillColumnVal))
(fill-paragraph nil)) ) )

(put this-command 'stateIsCompact-p (if currentStateIsCompact nil t)) ) ) )

(defun shrink-whitespaces ()
"Remove white spaces around cursor to just one or none.
If current line does not contain non-white space chars, then remove blank lines to just one.
If current line contains non-white space chars, then shrink any whitespace char surrounding cursor to just one space.
If current line is a single space, remove that space.

Calling this command 3 times will always result in no whitespaces around cursor."
(interactive)
(let (
cursor-point
line-has-meat-p ; current line contains non-white space chars
spaceTabNeighbor-p
whitespace-begin whitespace-end
space-or-tab-begin space-or-tab-end
line-begin-pos line-end-pos
)
(save-excursion
;; todo: might consider whitespace as defined by syntax table, and also consider whitespace chars in unicode if syntax table doesn't already considered it.
(setq cursor-point (point))

(setq spaceTabNeighbor-p (if (or (looking-at " \\|\t") (looking-back " \\|\t")) t nil) )
(move-beginning-of-line 1) (setq line-begin-pos (point) )
(move-end-of-line 1) (setq line-end-pos (point) )
;; (re-search-backward "\n$") (setq line-begin-pos (point) )
;; (re-search-forward "\n$") (setq line-end-pos (point) )
(setq line-has-meat-p (if (< 0 (count-matches "[[:graph:]]" line-begin-pos line-end-pos)) t nil) )
(goto-char cursor-point)

(skip-chars-backward "\t ")
(setq space-or-tab-begin (point))

(skip-chars-backward "\t \n")
(setq whitespace-begin (point))

(goto-char cursor-point) (skip-chars-forward "\t ")
(setq space-or-tab-end (point))
(skip-chars-forward "\t \n")
(setq whitespace-end (point))
)

(if line-has-meat-p
(let (deleted-text)
(when spaceTabNeighbor-p
;; remove all whitespaces in the range
(setq deleted-text (delete-and-extract-region space-or-tab-begin space-or-tab-end))
;; insert a whitespace only if we have removed something
;; different that a simple whitespace
(if (not (string= deleted-text " "))
(insert " ") ) ) )

(progn
;; (delete-region whitespace-begin whitespace-end)
;; (insert "\n")
(delete-blank-lines)
)
;; todo: possibly code my own delete-blank-lines here for better efficiency, because delete-blank-lines seems complex.
)
)
)

(defun toggle-letter-case ()
"Toggle the letter case of current word or text selection.
Toggles between: “all lower”, “Init Caps”, “ALL CAPS”."
(interactive)
(let (p1 p2 (deactivate-mark nil) (case-fold-search nil))
(if (region-active-p)
(setq p1 (region-beginning) p2 (region-end))
(let ((bds (bounds-of-thing-at-point 'word) ) )
(setq p1 (car bds) p2 (cdr bds)) ) )

(when (not (eq last-command this-command))
(save-excursion
(goto-char p1)
(cond
((looking-at "[[:lower:]][[:lower:]]") (put this-command 'state "all lower"))
((looking-at "[[:upper:]][[:upper:]]") (put this-command 'state "all caps") )
((looking-at "[[:upper:]][[:lower:]]") (put this-command 'state "init caps") )
((looking-at "[[:lower:]]") (put this-command 'state "all lower"))
((looking-at "[[:upper:]]") (put this-command 'state "all caps") )
(t (put this-command 'state "all lower") ) ) ) )

(cond
((string= "all lower" (get this-command 'state))
(upcase-initials-region p1 p2) (put this-command 'state "init caps"))
((string= "init caps" (get this-command 'state))
(upcase-region p1 p2) (put this-command 'state "all caps"))
((string= "all caps" (get this-command 'state))
(downcase-region p1 p2) (put this-command 'state "all lower")) )
) )

;;; FRAME

(defun switch-to-next-frame ()
"Select the next frame on current display, and raise it."
(interactive)
(other-frame 1)
)

(defun switch-to-previous-frame ()
"Select the previous frame on current display, and raise it."
(interactive)
(other-frame -1)
)

;;; BUFFER RELATED

(defun next-user-buffer ()
"Switch to the next user buffer.
User buffers are those whose name does not start with *."
(interactive)
(next-buffer)
(let ((i 0))
(while (and (string-equal "*" (substring (buffer-name) 0 1)) (< i 20))
(setq i (1+ i)) (next-buffer))))

(defun previous-user-buffer ()
"Switch to the previous user buffer.
User buffers are those whose name does not start with *."
(interactive)
(previous-buffer)
(let ((i 0))
(while (and (string-equal "*" (substring (buffer-name) 0 1)) (< i 20))
(setq i (1+ i)) (previous-buffer) )))

(defun next-emacs-buffer ()
"Switch to the next emacs buffer.
Emacs buffers are those whose name starts with *."
(interactive)
(next-buffer)
(let ((i 0))
(while (and (not (string-equal "*" (substring (buffer-name) 0 1))) (< i 20))
(setq i (1+ i)) (next-buffer) )))

(defun previous-emacs-buffer ()
"Switch to the previous emacs buffer.
Emacs buffers are those whose name starts with *."
(interactive)
(previous-buffer)
(let ((i 0))
(while (and (not (string-equal "*" (substring (buffer-name) 0 1))) (< i 20))
(setq i (1+ i)) (previous-buffer) )))

(defun new-empty-buffer ()
"Opens a new empty buffer."
(interactive)
(let ((buf (generate-new-buffer "untitled")))
(switch-to-buffer buf)
(funcall (and initial-major-mode))
(setq buffer-offer-save t)))
;; note: emacs won't offer to save a buffer that's
;; not associated with a file,
;; even if buffer-modified-p is true.
;; One work around is to define your own my-kill-buffer function
;; that wraps around kill-buffer, and check on the buffer modification
;; status to offer save
;; This custome kill buffer is close-current-buffer.

(defun open-in-external-app ()
"Open the current file or dired marked files in external app.
Works in Microsoft Windows, Mac OS X, Linux."
(interactive)
(let ( doIt
(myFileList
(cond
((string-equal major-mode "dired-mode") (dired-get-marked-files))
(t (list (buffer-file-name))) ) ) )

(setq doIt (if (<= (length myFileList) 5)
t
(y-or-n-p "Open more than 5 files?") ) )

(when doIt
(cond
((string-equal system-type "windows-nt")
(mapc (lambda (fPath) (w32-shell-execute "open" (replace-regexp-in-string "/" "\\" fPath t t)) ) myFileList)
)
((string-equal system-type "darwin")
(mapc (lambda (fPath) (shell-command (format "open \"%s\"" fPath)) ) myFileList) )
((string-equal system-type "gnu/linux")
(mapc (lambda (fPath) (let ((process-connection-type nil)) (start-process "" nil "xdg-open" fPath)) ) myFileList) ) ) ) ) )

(defun open-in-desktop ()
"Open the current file in desktop.
Works in Microsoft Windows, Mac OS X, Linux."
(interactive)
(cond
((string-equal system-type "windows-nt")
(w32-shell-execute "explore" (replace-regexp-in-string "/" "\\" default-directory t t)))
((string-equal system-type "darwin") (shell-command "open ."))
((string-equal system-type "gnu/linux") (shell-command "xdg-open ."))
) )

(defvar recently-closed-buffers (cons nil nil) "A list of recently closed buffers. The max number to track is controlled by the variable recently-closed-buffers-max.")
(defvar recently-closed-buffers-max 30 "The maximum length for recently-closed-buffers.")

(defun close-current-buffer ()
"Close the current buffer.

Similar to (kill-buffer (current-buffer)) with the following addition:

• prompt user to save if the buffer has been modified even if the buffer is not associated with a file.
• make sure the buffer shown after closing is a user buffer.
• if the buffer is a file, add the path to the list `recently-closed-buffers'.

A emacs buffer is one who's name starts with *.
Else it is a user buffer."
(interactive)
(let (emacsBuff-p isEmacsBufferAfter)

(setq emacsBuff-p (if (string-match "^*" (buffer-name)) t nil) )

(if (string= major-mode "minibuffer-inactive-mode")
nil ; if minibuffer, do nothing
(progn
;; offer to save buffers that are non-empty and modified, even for non-file visiting buffer. (because kill-buffer does not offer to save buffers that are not associated with files)
(when (and (buffer-modified-p)
(not emacsBuff-p)
(not (string-equal major-mode "dired-mode"))
(if (equal (buffer-file-name) nil)
(if (string-equal "" (save-restriction (widen) (buffer-string))) nil t)
t
)
)
(if (y-or-n-p (format "Buffer %s modified; Do you want to save?" (buffer-name)))
(save-buffer)
(set-buffer-modified-p nil)))

;; save to a list of closed buffer
(when (not (equal buffer-file-name nil))
(setq recently-closed-buffers
(cons (cons (buffer-name) (buffer-file-name)) recently-closed-buffers))
(when (> (length recently-closed-buffers) recently-closed-buffers-max)
(setq recently-closed-buffers (butlast recently-closed-buffers 1))
)
)

;; close
(kill-buffer (current-buffer))

;; if emacs buffer, switch to a user buffer
(if (string-match "^*" (buffer-name))
(setq isEmacsBufferAfter t)
(setq isEmacsBufferAfter nil))
(when isEmacsBufferAfter
(next-user-buffer) ) )
)
) )

(defun open-last-closed ()
"Open the last closed file."
(interactive)
(find-file (cdr (pop recently-closed-buffers)) ) )

Change log

r863 by xahlee on May 24 (46 hours ago)   Diff
ErgoEmacs keybinding version 5.5.0. Major
changes. See _HISTORY.txt file.
Go to: 
Project members, sign in to write a code review

Older revisions

r861 by xahlee on May 22 (3 days ago)   Diff
ErgoEmacs keybinding. fixed a error on
forward-close-bracket
r860 by xahlee on May 22 (3 days ago)   Diff
ErgoEmacs keybinding. improved
implementation of forward-open-
bracket, backward-open-bracket,
forward-close-bracket, backward-close-
bracket
r840 by xahlee on May 15, 2012   Diff
ErgoEmacs keybinding, version 5.4.6.
Major change. See history file.
All revisions of this file

File info

Size: 17657 bytes, 489 lines

File properties

svn:executable
*
Powered by Google Project Hosting