diff options
Diffstat (limited to 'emacs/.emacs.d/lisp/my/my-editing.el')
-rw-r--r-- | emacs/.emacs.d/lisp/my/my-editing.el | 79 |
1 files changed, 72 insertions, 7 deletions
diff --git a/emacs/.emacs.d/lisp/my/my-editing.el b/emacs/.emacs.d/lisp/my/my-editing.el index aa65ba1..e6499ff 100644 --- a/emacs/.emacs.d/lisp/my/my-editing.el +++ b/emacs/.emacs.d/lisp/my/my-editing.el @@ -90,7 +90,7 @@ (interactive) (zap-up-to-char -1 ?/)) -(defun my-toggle-forward-word-viper-symbol () +(defun my-toggle-forward-word-symbol () (interactive) (require 'viper) (cond ((eq (lookup-key (current-global-map) "\M-f") 'forward-word) @@ -102,14 +102,47 @@ (progn (define-key global-map "\M-f" 'forward-symbol) (define-key global-map "\M-b" - (lambda () (interactive) - (forward-symbol -1))) + (lambda () (interactive) + (forward-symbol -1))) (message "M-f is forward-symbol"))) (t (progn (define-key global-map "\M-f" 'forward-word) (define-key global-map "\M-b" 'backward-word) (message "M-f is forward-word"))))) +;;; todo: move to my-viper +;;; do not skip underscore +(defun viper-forward-word-kernel (val) + (while (> val 0) + (cond ((viper-looking-at-alpha) + (viper-skip-alpha-forward "") + (viper-skip-separators t)) + ((viper-looking-at-separator) + (viper-skip-separators t)) + ((not (viper-looking-at-alphasep)) + (viper-skip-nonalphasep-forward) + (viper-skip-separators t))) + (setq val (1- val)))) + +(defun viper-backward-word-kernel (val) + (while (> val 0) + (viper-backward-char-carefully) + (cond ((viper-looking-at-alpha) + (viper-skip-alpha-backward "")) + ((viper-looking-at-separator) + (forward-char) + (viper-skip-separators nil) + (viper-backward-char-carefully) + (cond ((viper-looking-at-alpha) + (viper-skip-alpha-backward "_")) + ((not (viper-looking-at-alphasep)) + (viper-skip-nonalphasep-backward)) + ((bobp)) ; could still be at separator, but at beg of buffer + (t (forward-char)))) + ((not (viper-looking-at-alphasep)) + (viper-skip-nonalphasep-backward))) + (setq val (1- val)))) + (defun my--duplicate-buffer-substring (beg end &optional indent) "Duplicate buffer substring between BEG and END positions. With optional INDENT, run `indent-for-tab-command' after @@ -495,7 +528,7 @@ With an prefix-arg, copy the file name relative to project root." (interactive) (let ((old-max (point-max)) (old-point (point))) - (comment-kill (or n 1)) + (when comment-start (comment-kill (or n 1))) (when (= old-max (point-max)) (goto-char old-point) (kill-sexp n)))) @@ -513,11 +546,32 @@ With an prefix-arg, copy the file name relative to project root." (defun my-elide-region (b e) (interactive "r") - (let ((message-elide-ellipsis (concat comment-start - " [... %l lines elided] -"))) + (let ((message-elide-ellipsis + (if (> 1 (count-lines b (min (1+ e) (point-max)))) + (concat comment-start + " [... %l lines elided] +") + (format " [... %d words elided]" (count-words b e))))) (message-elide-region b e))) +(defun my-elide-text (text limit) + "Elide TEXT to about LIMIT characters." + (let ((keep (- limit 25))) + (when (< keep 0) + (error "Too few characters to limit to. Should be at least 25.")) + (with-temp-buffer + (insert text) + (goto-char (point-min)) + (while (and (<= (point) keep) (< (point) (point-max))) + (forward-word)) + (cond ((> (point) keep) + (backward-word) + (my-elide-region (point) (point-max)) + (buffer-string)) + (t text)) + )) + ) + (defun my-replace-no-filter (old-fun &rest r) (let ((search-invisible t)) (apply old-fun r))) @@ -525,5 +579,16 @@ With an prefix-arg, copy the file name relative to project root." (defun my-turn-off-truncate-lines () (setq truncate-lines nil)) +(defun my-write-file () + "Same as `write-file', but keep the old buffer and remain there. + +In other words, create a new buffer with the same content and +execute `write-file', then switch back to the current buffer." + (interactive) + (let ((old-buffer (current-buffer))) + (with-temp-buffer + (insert-buffer-substring old-buffer) + (call-interactively 'write-file)))) + (provide 'my-editing) ;;; my-editing.el ends here |