📄 vi.el
字号:
(if (vi-string-end-with-nl-p put-text) ; put back text as lines (if after-p (progn (next-line 1) (beginning-of-line)) (beginning-of-line)) (if after-p (forward-char 1))) (push-mark (point)) (insert put-text) (exchange-point-and-mark);; (back-to-indentation) ; this is not allowed if we allow yank-pop (vi-set-last-change-command 'vi-put-before arg after-p))))(defun vi-put-after (arg) "Put yanked (in vi sense) text back after/below cursor." (interactive "P") (vi-put-before arg t))(defun vi-shell-op (motion-command arg &optional shell-command) "Perform shell command (as filter) on range specified by MOTION-COMMANDwith ARG. If SHELL-COMMAND is not given, ask for one from minibuffer.If char argument is given, it directs the output to a *temp* buffer." (let* ((range (vi-effective-range motion-command arg)) (begin (car range)) (end (cdr range))) (if (= begin end) nil ; point not moved, abort op (cond ((null shell-command) (setq shell-command (read-string "!" nil)) (setq vi-last-shell-command shell-command))) (shell-command-on-region begin end shell-command (not (vi-prefix-char-value arg))) t)))(defun vi-shift-op (motion-command arg amount) "Perform shift command on range specified by MOTION-COMMAND with ARG forAMOUNT on each line. Negative amount means shift left.SPECIAL FEATURE: char argument can be used to specify shift amount(1-9)." (let* ((range (vi-effective-range motion-command arg)) (begin (car range)) (end (cdr range))) (if (= begin end) nil ; point not moved, abort op (if (vi-prefix-char-value arg) (setq amount (if (> amount 0) (- (vi-prefix-char-value arg) ?0) (- ?0 (vi-prefix-char-value arg))))) (indent-rigidly begin end amount) t)))(defun vi-indent-op (motion-command arg) "Perform indent command on range specified by MOTION-COMMAND with ARG." (let* ((range (vi-effective-range motion-command arg)) (begin (car range)) (end (cdr range))) (if (= begin end) nil ; point not moved, abort op (indent-region begin end nil) ; insert TAB as indent command t)))(defun vi-narrow-op (motion-command arg) "Narrow to region specified by MOTION-COMMAND with ARG." (let* ((range (vi-effective-range motion-command arg)) (begin (car range)) (end (cdr range)) reg) (if (= begin end) nil ; point not moved, abort op (narrow-to-region begin end))))(defun vi-get-mark (char) "Return contents of vi mark register named CHAR, or nil if undefined." (cdr (assq char vi-mark-alist)))(defun vi-set-mark (char) "Set contents of vi mark register named CHAR to current point. '@' is thespecial anonymous mark register." (interactive "c") (if (char-equal char ?@) (set-mark-command nil) (let ((aelt (assq char vi-mark-alist))) (if aelt (move-marker (cdr aelt) (point)) ; fixed 6/12/86 (setq aelt (cons char (copy-marker (point)))) (setq vi-mark-alist (cons aelt vi-mark-alist))))))(defun vi-find-matching-paren () "Locate the matching paren. It's a hack right now." (interactive) (cond ((looking-at "[[({]") (forward-sexp 1) (backward-char 1)) ((looking-at "[])}]") (forward-char 1) (backward-sexp 1)) (t (ding))))(defun vi-backward-blank-delimited-word (count) "Backward COUNT blank-delimited words." (interactive "p") (if (re-search-backward "[ \t\n\`][^ \t\n\`]+" nil t count) (if (not (bobp)) (forward-char 1))))(defun vi-forward-blank-delimited-word (count) "Forward COUNT blank-delimited words." (interactive "p") (if (re-search-forward "[^ \t\n]*[ \t\n]+[^ \t\n]" nil t count) (if (not (eobp)) (backward-char 1))))(defun vi-end-of-blank-delimited-word (count) "Forward to the end of the COUNT'th blank-delimited word." (interactive "p") (if (re-search-forward "[^ \t\n\']+[ \t\n\']" nil t count) (if (not (eobp)) (backward-char 2))))(defun vi-home-window-line (arg) "To window home or arg'th line from the top of the window." (interactive "p") (move-to-window-line (1- arg)) (back-to-indentation))(defun vi-last-window-line (arg) "To window last line or arg'th line from the bottom of the window." (interactive "p") (move-to-window-line (- arg)) (back-to-indentation))(defun vi-middle-window-line () "To the middle line of the window." (interactive) (move-to-window-line nil) (back-to-indentation))(defun vi-forward-word (count) "Stop at the beginning of the COUNT'th words from point." (interactive "p") (if (re-search-forward "\\w*\\W+\\<" nil t count) t (vi-ding)))(defun vi-set-last-change-command (fun &rest args) "Set (FUN . ARGS) as the last-change-command." (setq vi-last-change-command (cons fun args)))(defun vi-redo-last-change-command (count &optional command) "Redo last change command COUNT times. If the optional COMMAND is given,it is used instead of the current last-change-command." (interactive "p") (if (null command) (setq command vi-last-change-command)) (if (null command) (message "No last change command available.") (while (> count 0) (apply (car command) (cdr command)) (setq count (1- count)))))(defun vi-kill-char (count) "Kill COUNT chars from current point." (interactive "*p") (delete-char count t) ; save in kill ring (vi-set-last-change-command 'delete-char count t))(defun vi-transpose-objects (arg unit) "Transpose objects, the following char specifies unit of objects to betransposed -- \"c\" for chars, \"l\" for lines, \"w\" for words, \"s\" for sexp, \"p\" for paragraph.For the use of the prefix-arg, refer to individual functions called." (interactive "*P\nc") (if (char-equal unit ??) (progn (message "Transpose: c(har), l(ine), p(aragraph), s(-exp), w(ord),") (setq unit (read-char)))) (vi-set-last-change-command 'vi-transpose-objects arg unit) (cond ((char-equal unit ?c) (transpose-chars arg)) ((char-equal unit ?l) (transpose-lines (vi-prefix-numeric-value arg))) ((char-equal unit ?p) (transpose-paragraphs (vi-prefix-numeric-value arg))) ((char-equal unit ?s) (transpose-sexps (vi-prefix-numeric-value arg))) ((char-equal unit ?w) (transpose-words (vi-prefix-numeric-value arg))) (t (vi-transpose-objects arg ??))))(defun vi-query-replace (arg) "Query replace, use regexp version if ARG is non-nil." (interactive "*P") (let ((rcmd (if arg 'query-replace-regexp 'query-replace))) (call-interactively rcmd nil)))(defun vi-replace (arg) "Replace strings, use regexp version if ARG is non-nil." (interactive "*P") (let ((rcmd (if arg 'replace-regexp 'replace-string))) (call-interactively rcmd nil))) (defun vi-adjust-window (arg position) "Move current line to the top/center/bottom of the window." (interactive "p\nc") (cond ((char-equal position ?\r) (recenter 0)) ((char-equal position ?-) (recenter -1)) ((char-equal position ?.) (recenter (/ (window-height) 2))) (t (message "Move current line to: \\r(top) -(bottom) .(middle)") (setq position (read-char)) (vi-adjust-window arg position))))(defun vi-goto-column (col) "Go to given column of the current line." (interactive "p") (let ((opoint (point))) (beginning-of-line) (while (> col 1) (if (eolp) (setq col 0) (forward-char 1) (setq col (1- col)))) (if (= col 1) t (goto-char opoint) (ding))))(defun vi-name-last-change-or-macro (arg char) "Give name to the last change command or just defined kbd macro. If prefixARG is given, name last macro, otherwise name last change command. Thefollowing CHAR will be the name for the command or macro." (interactive "P\nc") (if arg (name-last-kbd-macro (intern (char-to-string char))) (if (eq (car vi-last-change-command) 'vi-first-redo-insertion) (let* ((args (cdr vi-last-change-command)) ; save the insertion text (str (buffer-substring (nth 0 args) (nth 1 args))) (overwrite-p (nth 2 args)) (prefix-code (nth 3 args))) (vi-set-last-change-command 'vi-more-redo-insertion str overwrite-p prefix-code))) (fset (intern (char-to-string char)) vi-last-change-command))) (defun vi-call-named-change-or-macro (count char) "Execute COUNT times the keyboard macro definition named by the following CHAR." (interactive "p\nc") (if (stringp (symbol-function (intern (char-to-string char)))) (execute-kbd-macro (intern (char-to-string char)) count) (vi-redo-last-change-command count (symbol-function (intern (char-to-string char))))))(defun vi-change-case (arg) ; could be made as an operator ? "Change the case of the char after point." (interactive "*p") (catch 'exit (if (looking-at "[a-z]") (upcase-region (point) (+ (point) arg)) (if (looking-at "[A-Z]") (downcase-region (point) (+ (point) arg)) (ding) (throw 'exit nil))) (vi-set-last-change-command 'vi-change-case arg) ;should avoid redundant save (forward-char arg)))(defun vi-ask-for-info (char) "Inquire status info. The next CHAR will specify the particular info requested." (interactive "c") (cond ((char-equal char ?l) (what-line)) ((char-equal char ?c) (what-cursor-position)) ((char-equal char ?p) (what-page)) (t (message "Ask for: l(ine number), c(ursor position), p(age number)") (setq char (read-char)) (vi-ask-for-info char))))(defun vi-mark-region (arg region) "Mark region approriately. The next char REGION is d(efun),s(-exp),b(uffer),p(aragraph), P(age), f(unction in C/Pascal etc.), w(ord), e(nd of sentence),l(ines)." (interactive "p\nc") (cond ((char-equal region ?d) (mark-defun arg)) ((char-equal region ?s) (mark-sexp arg)) ((char-equal region ?b) (mark-whole-buffer)) ((char-equal region ?p) (mark-paragraph arg)) ((char-equal region ?P) (mark-page arg)) ((char-equal region ?f) (mark-c-function arg)) ((char-equal region ?w) (mark-word arg)) ((char-equal region ?e) (mark-end-of-sentence arg)) ((char-equal region ?l) (vi-mark-lines arg)) (t (message "Mark: d(efun),s(-exp),b(uf),p(arag),P(age),f(unct),w(ord),e(os),l(ines)") (setq region (read-char)) (vi-mark-region arg region))))(defun vi-mark-lines (num) "Mark NUM of lines from current line as current region." (beginning-of-line 1) (push-mark) (end-of-line num))(defun vi-verify-spelling (arg unit) "Verify spelling for the objects specified by char UNIT : [b(uffer),r(egion), s(tring), w(ord) ]." (interactive "P\nc") (setq prefix-arg arg) ; seems not needed (cond ((char-equal unit ?b) (call-interactively 'spell-buffer)) ((char-equal unit ?r) (call-interactively 'spell-region)) ((char-equal unit ?s) (call-interactively 'spell-string)) ((char-equal unit ?w) (call-interactively 'spell-word)) (t (message "Spell check: b(uffer), r(egion), s(tring), w(ord)") (setq unit (read-char)) (vi-verify-spelling arg unit))))(defun vi-do-old-mode-C-c-command (arg) "This is a hack for accessing mode specific C-c commands in vi-mode." (interactive "P") (let ((cmd (lookup-key vi-mode-old-local-map (concat "\C-c" (char-to-string (read-char)))))) (if (catch 'exit-vi-mode ; kludge hack due to dynamic binding ; of case-fold-search (if (null cmd) (progn (ding) nil) (let ((case-fold-search vi-mode-old-case-fold)) ; a hack (setq prefix-arg arg) (command-execute cmd nil) nil))) (progn (vi-back-to-old-mode) (setq prefix-arg arg) (command-execute cmd nil)))))(defun vi-quote-words (arg char) "Quote ARG words from the word point is on with the pattern specified by theCHAR. Currently, CHAR could be [,{,(,\",',`,<,*, etc." (interactive "*p\nc") (while (not (string-match "[[({<\"'`*]" (char-to-string char))) (message "Enter any of [,{,(,<,\",',`,* as quoting character.") (setq char (read-char))) (vi-set-last-change-command 'vi-quote-words arg char) (if (not (looking-at "\\<")) (forward-word -1)) (insert char) (cond ((char-equal char ?[) (setq char ?])) ((char-equal char ?{) (setq char ?})) ((char-equal char ?<) (setq char ?>)) ((char-equal char ?() (setq char ?))) ((char-equal char ?`) (setq char ?'))) (vi-end-of-word arg) (forward-char 1) (insert char))(defun vi-locate-def () "Locate definition in current file for the name before the point. It assumesa `(def..' always starts at the beginning of a line." (interactive) (let (name) (save-excursion (setq name (buffer-substring (progn (vi-backward-blank-delimited-word 1) (skip-chars-forward "^a-zA-Z") (point)) (progn (vi-end-of-blank-delimited-word 1) (forward-char) (skip-chars-backward "^a-zA-Z") (point))))) (set-mark-command nil) (goto-char (point-min)) (if (re-search-forward (concat "^(def[unvarconst ]*" name) nil t) nil (message "No definition for \"%s\" in current file." name (ding)) (set-mark-command t))))(defun vi-split-open-line (arg) "Insert a newline and leave point before it.With arg, inserts that many newlines." (interactive "*p") (vi-goto-insert-state 1 (list (function (lambda (arg) (let ((flag (and (bolp) (not (bobp))))) (if flag (forward-char -1)) (while (> arg 0) (save-excursion (insert ?\n) (if fill-prefix (insert fill-prefix))) (setq arg (1- arg))) (if flag (forward-char 1))))) arg) t))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -