📄 vip.el
字号:
(defun vip-Append (arg) "Append at end of line." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (setq vip-d-com (list 'vip-Append val ?r)) (end-of-line) (if (equal com ?r) (vip-loop val (yank)) (vip-change-mode-to-insert))))(defun vip-Insert (arg) "Insert before first non-white." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (setq vip-d-com (list 'vip-Insert val ?r)) (back-to-indentation) (if (equal com ?r) (vip-loop val (yank)) (vip-change-mode-to-insert))))(defun vip-open-line (arg) "Open line below." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (setq vip-d-com (list 'vip-open-line val ?r)) (let ((col (current-indentation))) (if (equal com ?r) (vip-loop val (progn (end-of-line) (newline 1) (if vip-open-with-indent (indent-to col)) (yank))) (end-of-line) (newline 1) (if vip-open-with-indent (indent-to col)) (vip-change-mode-to-insert)))))(defun vip-Open-line (arg) "Open line above." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (setq vip-d-com (list 'vip-Open-line val ?r)) (let ((col (current-indentation))) (if (equal com ?r) (vip-loop val (progn (beginning-of-line) (open-line 1) (if vip-open-with-indent (indent-to col)) (yank))) (beginning-of-line) (open-line 1) (if vip-open-with-indent (indent-to col)) (vip-change-mode-to-insert)))))(defun vip-open-line-at-point (arg) "Open line at point." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (setq vip-d-com (list 'vip-open-line-at-point val ?r)) (if (equal com ?r) (vip-loop val (progn (open-line 1) (yank))) (open-line 1) (vip-change-mode-to-insert))))(defun vip-substitute (arg) "Substitute characters." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (save-excursion (set-mark (point)) (forward-char val) (if (equal com ?r) (vip-change-subr (mark) (point)) (vip-change (mark) (point)))) (setq vip-d-com (list 'vip-substitute val ?r)))) (defun vip-substitute-line (arg) "Substitute lines." (interactive "p") (vip-line (cons arg ?C)));; line command(defun vip-line (arg) (let ((val (car arg)) (com (cdr arg))) (move-marker vip-com-point (point)) (next-line (1- val)) (vip-execute-com 'vip-line val com)))(defun vip-yank-line (arg) "Yank ARG lines (in vi's sense)" (interactive "P") (let ((val (vip-p-val arg))) (vip-line (cons val ?Y)))) ;; region command(defun vip-region (arg) (interactive "P") (let ((val (vip-P-val arg)) (com (vip-getcom arg))) (move-marker vip-com-point (point)) (exchange-point-and-mark) (vip-execute-com 'vip-region val com)))(defun vip-Region (arg) (interactive "P") (let ((val (vip-P-val arg)) (com (vip-getCom arg))) (move-marker vip-com-point (point)) (exchange-point-and-mark) (vip-execute-com 'vip-Region val com))) (defun vip-replace-char (arg) "Replace the following ARG chars by the character read." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (setq vip-d-com (list 'vip-replace-char val ?r)) (vip-replace-char-subr (if (equal com ?r) vip-d-char (read-char)) val)))(defun vip-replace-char-subr (char arg) (delete-char arg t) (setq vip-d-char char) (vip-loop (if (> arg 0) arg (- arg)) (insert char)) (backward-char arg))(defun vip-replace-string () "Replace string. If you supply null string as the string to be replaced,the query replace mode will toggle between string replace and regexp replace." (interactive) (let (str) (setq str (vip-read-string (if vip-re-replace "Replace regexp: " "Replace string: "))) (if (string= str "") (progn (setq vip-re-replace (not vip-re-replace)) (message (format "Replace mode changed to %s." (if vip-re-replace "regexp replace" "string replace")))) (if vip-re-replace (replace-regexp str (vip-read-string (format "Replace regexp \"%s\" with: " str))) (replace-string str (vip-read-string (format "Replace \"%s\" with: " str)))))));; basic cursor movement. j, k, l, m commands.(defun vip-forward-char (arg) "Move point right ARG characters (left if ARG negative).On reaching endof buffer, stop and signal error." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (if com (move-marker vip-com-point (point))) (forward-char val) (if com (vip-execute-com 'vip-forward-char val com))))(defun vip-backward-char (arg) "Move point left ARG characters (right if ARG negative). On reachingbeginning of buffer, stop and signal error." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (if com (move-marker vip-com-point (point))) (backward-char val) (if com (vip-execute-com 'vip-backward-char val com))));; word command(defun vip-forward-word (arg) "Forward word." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (if com (move-marker vip-com-point (point))) (forward-word val) (skip-chars-forward " \t\n") (if com (progn (if (or (= com ?c) (= com (- ?c))) (progn (backward-word 1) (forward-word 1))) (if (or (= com ?d) (= com ?y)) (progn (backward-word 1) (forward-word 1) (skip-chars-forward " \t"))) (vip-execute-com 'vip-forward-word val com)))))(defun vip-end-of-word (arg) "Move point to end of current word." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (if com (move-marker vip-com-point (point))) (forward-char) (forward-word val) (backward-char) (if com (progn (forward-char) (vip-execute-com 'vip-end-of-word val com))))) (defun vip-backward-word (arg) "Backward word." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (if com (move-marker vip-com-point (point))) (backward-word val) (if com (vip-execute-com 'vip-backward-word val com))))(defun vip-forward-Word (arg) "Forward word delimited by white character." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (if com (move-marker vip-com-point (point))) (re-search-forward "[^ \t\n]*[ \t\n]+" nil t val) (if com (progn (if (or (= com ?c) (= com (- ?c))) (progn (backward-word 1) (forward-word 1))) (if (or (= com ?d) (= com ?y)) (progn (backward-word 1) (forward-word 1) (skip-chars-forward " \t"))) (vip-execute-com 'vip-forward-Word val com)))))(defun vip-end-of-Word (arg) "Move forward to end of word delimited by white character." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (if com (move-marker vip-com-point (point))) (forward-char) (if (re-search-forward "[^ \t\n]+" nil t val) (backward-char)) (if com (progn (forward-char) (vip-execute-com 'vip-end-of-Word val com)))))(defun vip-backward-Word (arg) "Backward word delimited by white character." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (if com (move-marker vip-com-point (point))) (if (re-search-backward "[ \t\n]+[^ \t\n]+" nil t val) (forward-char) (goto-char (point-min))) (if com (vip-execute-com 'vip-backward-Word val com))))(defun vip-beginning-of-line (arg) "Go to beginning of line." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (if com (move-marker vip-com-point (point))) (beginning-of-line val) (if com (vip-execute-com 'vip-beginning-of-line val com))))(defun vip-bol-and-skip-white (arg) "Beginning of line at first non-white character." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (if com (move-marker vip-com-point (point))) (back-to-indentation) (if com (vip-execute-com 'vip-bol-and-skip-white val com))))(defun vip-goto-eol (arg) "Go to end of line." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (if com (move-marker vip-com-point (point))) (end-of-line val) (if com (vip-execute-com 'vip-goto-eol val com))))(defun vip-next-line (arg) "Go to next line." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getCom arg))) (if com (move-marker vip-com-point (point))) (next-line-internal val) (setq this-command 'next-line) (if com (vip-execute-com 'vip-next-line val com))))(defun vip-next-line-at-bol (arg) "Next line at beginning of line." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getCom arg))) (if com (move-marker vip-com-point (point))) (next-line val) (back-to-indentation) (if com (vip-execute-com 'vip-next-line-at-bol val com))))(defun vip-previous-line (arg) "Go to previous line." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getCom arg))) (if com (move-marker vip-com-point (point))) (next-line (- val)) (setq this-command 'previous-line) (if com (vip-execute-com 'vip-previous-line val com))))(defun vip-previous-line-at-bol (arg) "Previous line at beginning of line." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getCom arg))) (if com (move-marker vip-com-point (point))) (next-line (- val)) (back-to-indentation) (if com (vip-execute-com 'vip-previous-line val com))))(defun vip-change-to-eol (arg) "Change to end of line." (interactive "P") (vip-goto-eol (cons arg ?c)))(defun vip-kill-line (arg) "Delete line." (interactive "P") (vip-goto-eol (cons arg ?d)));; moving around(defun vip-goto-line (arg) "Go to ARG's line. Without ARG go to end of buffer." (interactive "P") (let ((val (vip-P-val arg)) (com (vip-getCom arg))) (move-marker vip-com-point (point)) (set-mark (point)) (if (null val) (goto-char (point-max)) (goto-char (point-min)) (forward-line (1- val))) (back-to-indentation) (if com (vip-execute-com 'vip-goto-line val com))))(defun vip-find-char (arg char forward offset) "Find ARG's occurence of CHAR on the current line. If FORWARD thensearch is forward, otherwise backward. OFFSET is used to adjust pointafter search." (let ((arg (if forward arg (- arg))) point) (save-excursion (save-restriction (if (> arg 0) (narrow-to-region ;; forward search begins here (if (eolp) (error "") (point)) ;; forward search ends here (progn (next-line 1) (beginning-of-line) (point))) (narrow-to-region ;; backward search begins from here (if (bolp) (error "") (point)) ;; backward search ends here (progn (beginning-of-line) (point)))) ;; if arg > 0, point is forwarded before search. (if (> arg 0) (goto-char (1+ (point-min))) (goto-char (point-max))) (let ((case-fold-search nil)) (search-forward (char-to-string char) nil 0 arg)) (setq point (point)) (if (or (and (> arg 0) (= point (point-max))) (and (< arg 0) (= point (point-min)))) (error "")))) (goto-char (+ point (if (> arg 0) (if offset -2 -1) (if offset 1 0))))))(defun vip-find-char-forward (arg) "Find char on the line. If called interactively read the char to findfrom the terminal, and if called from vip-repeat, the char last used isused. This behaviour is controlled by the sign of prefix numeric value." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (if (> val 0) ;; this means that the function was called interactively (setq vip-f-char (read-char) vip-f-forward t vip-f-offset nil) (setq val (- val))) (if com (move-marker vip-com-point (point))) (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t nil) (setq val (- val)) (if com (progn (setq vip-F-char vip-f-char);; set new vip-F-char (forward-char) (vip-execute-com 'vip-find-char-forward val com)))))(defun vip-goto-char-forward (arg) "Go up to char ARG forward on line." (interactive "P") (let ((val (vip-p-val arg)) (com (vip-getcom arg))) (if (> val 0) ;; this means that the function was called interactively (setq vip-f-char (read-char) vip-f-forward t vip-f-offset t) (setq val (- val))) (if com (move-marker vip-com-point (point))) (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t t) (setq val (- val)) (if com (progn
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -