Mercurial > emacs
changeset 10789:af7c0bb1059f
Initial revision
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Mon, 20 Feb 1995 23:15:10 +0000 |
parents | 61c7b37349d2 |
children | 7d1a9faaf158 |
files | lisp/emulation/viper-ex.el lisp/emulation/viper-keym.el lisp/emulation/viper-macs.el lisp/emulation/viper-mous.el lisp/emulation/viper-util.el lisp/emulation/viper.el |
diffstat | 6 files changed, 10056 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/emulation/viper-ex.el Mon Feb 20 23:15:10 1995 +0000 @@ -0,0 +1,1888 @@ +;;; viper-ex.el -- functions implementing the Ex commands for Viper + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 2, or (at your option) +;; any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + + +(require 'viper-util) + +;;; Variables + +(defconst vip-ex-work-buf-name " *ex-working-space*") +(defconst vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name)) + + +;;; Completion in :set command + +;; The list of Ex commands. Used for completing command names. +(defconst ex-token-alist + '(("!") ("=") (">") ("&") ("~") + ("yank") ("xit") ("WWrite") ("Write") ("write") ("wq") ("visual") + ("version") ("vglobal") ("unmap") ("undo") ("tag") ("transfer") ("suspend") + ("substitute") ("submitReport") ("stop") ("sr") ("source") ("shell") + ("set") ("rewind") ("recover") ("read") ("quit") ("pwd") + ("put") ("preserve") ("PreviousRelatedFile") ("RelatedFile") + ("next") ("Next") ("move") ("mark") ("map") ("kmark") ("join") + ("help") ("goto") ("global") ("file") ("edit") ("delete") ("copy") + ("chdir") ("cd") ("Buffer") ("buffer") ("args")) ) + +;; A-list of Ex variables that can be set using the :set command. +(defconst ex-variable-alist + '(("wrapscan") ("ws") ("wrapmargin") ("wm") + ("tab-stop-local") ("tsl") ("tabstop") ("ts") + ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh") + ("readonly") ("ro") + ("nowrapscan") ("nows") ("noshowmatch") ("nosm") + ("noreadonly") ("noro") ("nomagic") ("noma") + ("noignorecase") ("noic") ("noautoindent") ("noai") + ("magic") ("ma") ("ignorecase") ("ic") ("autoindent") ("ai") + )) + + + +;; Token recognized during parsing of Ex commands (e.g., "read", "comma") +(defvar ex-token nil) + +;; Type of token. +;; If non-nil, gives type of address; if nil, it is a command. +(defvar ex-token-type nil) + +;; List of addresses passed to Ex command +(defvar ex-addresses nil) + +;; It seems that this flag is used only for `#', `print', and `list', which +;; aren't implemented. Check later. +(defvar ex-flag nil) + +;; "buffer" where Ex commands keep deleted data. +;; In Emacs terms, this is a register. +(defvar ex-buffer nil) + +;; Value of ex count. +(defvar ex-count nil) + +;; Flag for global command. +(defvar ex-g-flag nil) + +;; If t, global command is executed on lines not matching ex-g-pat. +(defvar ex-g-variant nil) + +;; Save reg-exp used in substitute. +(defvar ex-reg-exp nil) + + +;; Replace pattern for substitute. +(defvar ex-repl nil) + +;; Pattern for global command. +(defvar ex-g-pat nil) + +;; `sh' doesn't seem to expand wildcards, like `*' +(defconst ex-find-file-shell "csh" + "Shell in which to interpret wildcards.") +(defvar ex-find-file-shell-options "-f" + "*Options to pass to `ex-find-file-shell'.") + +;; Remembers the previous Ex tag. +(defvar ex-tag nil) + +;; file used by Ex commands like :r, :w, :n +(defvar ex-file nil) + +;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc. +(defvar ex-variant nil) + +;; Specified the offset of an Ex command, such as :read. +(defvar ex-offset nil) + +;; Tells Ex that this is a w>> command. +(defvar ex-append nil) + +;; File containing the shell command to be executed at Ex prompt, +;; e.g., :r !date +(defvar ex-cmdfile nil) + +;; flag used in vip-ex-read-file-name to indicate that we may be reading +;; multiple file names. Used for :edit and :next +(defvar vip-keep-reading-filename nil) + +(defconst ex-cycle-other-window t + "*If t, :n and :b cycles through files and buffers in other window. +Then :N and :B cycles in the current window. If nil, this behavior is +reversed.") + +(defconst ex-cycle-through-non-files nil + "*Cycle through *scratch* and other buffers that don't visit any file.") + +;; Last shell command executed with :! command. +(defvar vip-ex-last-shell-com nil) + +;; Indicates if Minibuffer was exited temporarily in Ex-command. +(defvar vip-incomplete-ex-cmd nil) + +;; Remembers the last ex-command prompt. +(defvar vip-last-ex-prompt "") + + +;;; Code + +(defun vip-check-sub (str) + "Check if ex-token is an initial segment of STR." + (let ((length (length ex-token))) + (if (and (<= length (length str)) + (string= ex-token (substring str 0 length))) + (setq ex-token str) + (setq ex-token-type 'non-command)))) + +(defun vip-get-ex-com-subr () + "Get a complete ex command." + (let (case-fold-search) + (set-mark (point)) + (re-search-forward "[a-zA-Z][a-zA-Z]*") + (setq ex-token-type 'command) + (setq ex-token (buffer-substring (point) (mark t))) + (exchange-point-and-mark) + (cond ((looking-at "a") + (cond ((looking-at "ab") (vip-check-sub "abbreviate")) + ((looking-at "ar") (vip-check-sub "args")) + (t (vip-check-sub "append")))) + ((looking-at "h") (vip-check-sub "help")) + ((looking-at "c") + (cond ((looking-at "cd") (vip-check-sub "cd")) + ((looking-at "ch") (vip-check-sub "chdir")) + ((looking-at "co") (vip-check-sub "copy")) + (t (vip-check-sub "change")))) + ((looking-at "d") (vip-check-sub "delete")) + ((looking-at "b") (vip-check-sub "buffer")) + ((looking-at "B") (vip-check-sub "Buffer")) + ((looking-at "e") + (if (looking-at "ex") (vip-check-sub "ex") + (vip-check-sub "edit"))) + ((looking-at "f") (vip-check-sub "file")) + ((looking-at "g") (vip-check-sub "global")) + ((looking-at "i") (vip-check-sub "insert")) + ((looking-at "j") (vip-check-sub "join")) + ((looking-at "l") (vip-check-sub "list")) + ((looking-at "m") + (cond ((looking-at "map") (vip-check-sub "map")) + ((looking-at "mar") (vip-check-sub "mark")) + (t (vip-check-sub "move")))) + ((looking-at "k[a-z][^a-z]") + (setq ex-token "kmark") + (forward-char 1) + (exchange-point-and-mark)) ;; this is canceled out by another + ;; exchange-point-and-mark at the end + ((looking-at "k") (vip-check-sub "kmark")) + ((looking-at "n") (if (looking-at "nu") + (vip-check-sub "number") + (vip-check-sub "next"))) + ((looking-at "N") (vip-check-sub "Next")) + ((looking-at "o") (vip-check-sub "open")) + ((looking-at "p") + (cond ((looking-at "pre") (vip-check-sub "preserve")) + ((looking-at "pu") (vip-check-sub "put")) + ((looking-at "pw") (vip-check-sub "pwd")) + (t (vip-check-sub "print")))) + ((looking-at "P") (vip-check-sub "PreviousRelatedFile")) + ((looking-at "R") (vip-check-sub "RelatedFile")) + ((looking-at "q") (vip-check-sub "quit")) + ((looking-at "r") + (cond ((looking-at "rec") (vip-check-sub "recover")) + ((looking-at "rew") (vip-check-sub "rewind")) + (t (vip-check-sub "read")))) + ((looking-at "s") + (cond ((looking-at "se") (vip-check-sub "set")) + ((looking-at "sh") (vip-check-sub "shell")) + ((looking-at "so") (vip-check-sub "source")) + ((looking-at "sr") (vip-check-sub "sr")) + ((looking-at "st") (vip-check-sub "stop")) + ((looking-at "sus") (vip-check-sub "suspend")) + ((looking-at "subm") (vip-check-sub "submitReport")) + (t (vip-check-sub "substitute")))) + ((looking-at "t") + (if (looking-at "ta") (vip-check-sub "tag") + (vip-check-sub "transfer"))) + ((looking-at "u") + (cond ((looking-at "una") (vip-check-sub "unabbreviate")) + ((looking-at "unm") (vip-check-sub "unmap")) + (t (vip-check-sub "undo")))) + ((looking-at "v") + (cond ((looking-at "ve") (vip-check-sub "version")) + ((looking-at "vi") (vip-check-sub "visual")) + (t (vip-check-sub "vglobal")))) + ((looking-at "w") + (if (looking-at "wq") (vip-check-sub "wq") + (vip-check-sub "write"))) + ((looking-at "W") + (if (looking-at "WW") + (vip-check-sub "WWrite") + (vip-check-sub "Write"))) + ((looking-at "x") (vip-check-sub "xit")) + ((looking-at "y") (vip-check-sub "yank")) + ((looking-at "z") (vip-check-sub "z"))) + (exchange-point-and-mark) + )) + +(defun vip-get-ex-token () + "Get an ex-token which is either an address or a command. +A token has a type, \(command, address, end-mark\), and a value." + (save-window-excursion + (set-buffer vip-ex-work-buf) + (skip-chars-forward " \t|") + (cond ((looking-at "#") + (setq ex-token-type 'command) + (setq ex-token (char-to-string (following-char))) + (forward-char 1)) + ((looking-at "[a-z]") (vip-get-ex-com-subr)) + ((looking-at "\\.") + (forward-char 1) + (setq ex-token-type 'dot)) + ((looking-at "[0-9]") + (set-mark (point)) + (re-search-forward "[0-9]*") + (setq ex-token-type + (cond ((eq ex-token-type 'plus) 'add-number) + ((eq ex-token-type 'minus) 'sub-number) + (t 'abs-number))) + (setq ex-token (string-to-int (buffer-substring (point) (mark t))))) + ((looking-at "\\$") + (forward-char 1) + (setq ex-token-type 'end)) + ((looking-at "%") + (forward-char 1) + (setq ex-token-type 'whole)) + ((looking-at "+") + (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]")) + (forward-char 1) + (insert "1") + (backward-char 1) + (setq ex-token-type 'plus)) + ((looking-at "+[0-9]") + (forward-char 1) + (setq ex-token-type 'plus)) + (t + (error vip-BadAddress)))) + ((looking-at "-") + (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]")) + (forward-char 1) + (insert "1") + (backward-char 1) + (setq ex-token-type 'minus)) + ((looking-at "-[0-9]") + (forward-char 1) + (setq ex-token-type 'minus)) + (t + (error vip-BadAddress)))) + ((looking-at "/") + (forward-char 1) + (set-mark (point)) + (let ((cont t)) + (while (and (not (eolp)) cont) + ;;(re-search-forward "[^/]*/") + (re-search-forward "[^/]*\\(/\\|\n\\)") + (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/")) + (setq cont nil)))) + (backward-char 1) + (setq ex-token (buffer-substring (point) (mark t))) + (if (looking-at "/") (forward-char 1)) + (setq ex-token-type 'search-forward)) + ((looking-at "\\?") + (forward-char 1) + (set-mark (point)) + (let ((cont t)) + (while (and (not (eolp)) cont) + ;;(re-search-forward "[^\\?]*\\?") + (re-search-forward "[^\\?]*\\(\\?\\|\n\\)") + (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?")) + (setq cont nil)) + (backward-char 1) + (if (not (looking-at "\n")) (forward-char 1)))) + (setq ex-token-type 'search-backward) + (setq ex-token (buffer-substring (1- (point)) (mark t)))) + ((looking-at ",") + (forward-char 1) + (setq ex-token-type 'comma)) + ((looking-at ";") + (forward-char 1) + (setq ex-token-type 'semi-colon)) + ((looking-at "[!=><&~]") + (setq ex-token-type 'command) + (setq ex-token (char-to-string (following-char))) + (forward-char 1)) + ((looking-at "'") + (setq ex-token-type 'goto-mark) + (forward-char 1) + (cond ((looking-at "'") (setq ex-token nil)) + ((looking-at "[a-z]") (setq ex-token (following-char))) + (t (error "Marks are ' and a-z"))) + (forward-char 1)) + ((looking-at "\n") + (setq ex-token-type 'end-mark) + (setq ex-token "goto")) + (t + (error vip-BadExCommand))))) + +;; Reads Ex command. Tries to determine if it has to exit because command +;; is complete or invalid. If not, keeps reading command. +(defun ex-cmd-read-exit () + (interactive) + (setq vip-incomplete-ex-cmd t) + (let ((quit-regex1 (concat + "\\(" + "set[ \t]*" "\\|" "edit[ \t]*" "\\|" "[nN]ext[ \t]*" + "\\|" "unm[ \t]*" "\\|" "^[ \t]*rep" + "\\)")) + (quit-regex2 (concat + "[a-zA-Z][ \t]*" + "\\(" "!" "\\|" ">>" + "\\|" "\\+[0-9]+" + "\\)" + "*[ \t]*$")) + (stay-regex (concat + "\\(" + "^[ \t]*$" "\\|" "[ktgjmsz][ \t]*$" "\\|" "^[ \t]*ab.*" + "\\|" "tr[ansfer \t]*" "\\|" "sr[ \t]*" + "\\|" "mo.*" "\\|" "^[ \t]*k?ma[^p]*" + "\\|" "^[ \t]*fi.*" "\\|" "v?gl.*" "\\|" "[vg][ \t]*$" + "\\|" "jo.*" "\\|" "^[ \t]*ta.*" "\\|" "^[ \t]*una.*" + "\\|" "^[ \t]*su.*" "\\|['`][a-z][ \t]*" + "\\|" "![ \t]*[a-zA-Z].*" + "\\)" + "!*"))) + + (save-window-excursion ;; put cursor at the end of the Ex working buffer + (set-buffer vip-ex-work-buf) + (goto-char (point-max))) + (cond ((vip-looking-back quit-regex1) (exit-minibuffer)) + ((vip-looking-back stay-regex) (insert " ")) + ((vip-looking-back quit-regex2) (exit-minibuffer)) + (t (insert " "))))) + +;; complete Ex command +(defun ex-cmd-complete () + (interactive) + (let (save-pos dist compl-list string-to-complete completion-result) + + (save-excursion + (setq dist (skip-chars-backward "[a-zA-Z!=>&~]") + save-pos (point))) + + (if (or (= dist 0) + (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)") + (vip-looking-back + "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*+[ \t]+[a-zA-Z!=>&~]+")) + ;; Preceding characters are not the ones allowed in an Ex command + ;; or we have typed past command name. + ;; Note: we didn't do parsing, so there may be surprises. + (if (or (vip-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*") + (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)") + (looking-at "[^ \t\n\C-m]")) + nil + (with-output-to-temp-buffer "*Completions*" + (display-completion-list + (vip-alist-to-list ex-token-alist)))) + ;; Preceding chars may be part of a command name + (setq string-to-complete (buffer-substring save-pos (point))) + (setq completion-result + (try-completion string-to-complete ex-token-alist)) + + (cond ((eq completion-result t) ;; exact match--do nothing + (vip-tmp-insert-at-eob " (Sole completion)")) + ((eq completion-result nil) + (vip-tmp-insert-at-eob " (No match)")) + (t ;; partial completion + (goto-char save-pos) + (delete-region (point) (point-max)) + (insert completion-result) + (let (case-fold-search) + (setq compl-list + (vip-filter-alist (concat "^" completion-result) + ex-token-alist))) + (if (> (length compl-list) 1) + (with-output-to-temp-buffer "*Completions*" + (display-completion-list + (vip-alist-to-list (reverse compl-list))))))) + ))) + +(defun vip-ex (&optional string) + "Ex commands within Viper." + (interactive) + (or string + (setq ex-g-flag nil + ex-g-variant nil)) + (let* ((map (copy-keymap minibuffer-local-map)) + (address nil) + (cont t) + (dot (point)) + com-str) + + (vip-add-keymap vip-ex-cmd-map map) + + (setq com-str (or string (vip-read-string-with-history + ":" + nil + 'vip-ex-history + (car vip-ex-history) + map))) + (save-window-excursion + ;; just a precaution + (or (vip-buffer-live-p vip-ex-work-buf) + (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))) + (set-buffer vip-ex-work-buf) + (delete-region (point-min) (point-max)) + (insert com-str "\n") + (goto-char (point-min))) + (setq ex-token-type nil + ex-addresses nil) + (while cont + (vip-get-ex-token) + (cond ((memq ex-token-type '(command end-mark)) + (if address (setq ex-addresses (cons address ex-addresses))) + (cond ((string= ex-token "global") + (ex-global nil) + (setq cont nil)) + ((string= ex-token "vglobal") + (ex-global t) + (setq cont nil)) + (t + (vip-execute-ex-command) + (save-window-excursion + (set-buffer vip-ex-work-buf) + (skip-chars-forward " \t") + (cond ((looking-at "|") + (forward-char 1)) + ((looking-at "\n") + (setq cont nil)) + (t (error "`%s': %s" ex-token vip-SpuriousText))) + )) + )) + ((eq ex-token-type 'non-command) + (error (format "`%s': %s" ex-token vip-BadExCommand))) + ((eq ex-token-type 'whole) + (setq ex-addresses + (cons (point-max) (cons (point-min) ex-addresses)))) + ((eq ex-token-type 'comma) + (setq ex-addresses + (cons (if (null address) (point) address) ex-addresses))) + ((eq ex-token-type 'semi-colon) + (if address (setq dot address)) + (setq ex-addresses + (cons (if (null address) (point) address) ex-addresses))) + (t (let ((ans (vip-get-ex-address-subr address dot))) + (if ans (setq address ans)))))))) + +(defun vip-get-ex-pat () + "Get a regular expression and set `ex-variant', if found." + (save-window-excursion + (set-buffer vip-ex-work-buf) + (skip-chars-forward " \t") + (if (looking-at "!") + (progn + (setq ex-g-variant (not ex-g-variant) + ex-g-flag (not ex-g-flag)) + (forward-char 1) + (skip-chars-forward " \t"))) + (let ((c (following-char))) + (if (string-match "[0-9A-Za-z]" (format "%c" c)) + (error + "Global regexp must be inside matching non-alphanumeric chars")) + (if (looking-at "[^\\\\\n]") + (progn + (forward-char 1) + (set-mark (point)) + (let ((cont t)) + (while (and (not (eolp)) cont) + (if (not (re-search-forward (format "[^%c]*%c" c c) nil t)) + (if (member ex-token '("global" "vglobal")) + (error + "Missing closing delimiter for global regexp") + (goto-char (point-max)))) + (if (not (vip-looking-back + (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c))) + (setq cont nil)))) + (setq ex-token + (if (= (mark t) (point)) "" + (buffer-substring (1- (point)) (mark t)))) + (backward-char 1)) + (setq ex-token nil)) + c))) + +(defun vip-get-ex-command () + "get an ex command" + (save-window-excursion + (set-buffer vip-ex-work-buf) + (if (looking-at "/") (forward-char 1)) + (skip-chars-forward " \t") + (cond ((looking-at "[a-z]") + (vip-get-ex-com-subr) + (if (eq ex-token-type 'non-command) + (error "`%s': %s" ex-token vip-BadExCommand))) + ((looking-at "[!=><&~]") + (setq ex-token (char-to-string (following-char))) + (forward-char 1)) + (t (error vip-BadExCommand))))) + +(defun vip-get-ex-opt-gc (c) + "Get an Ex option g or c." + (save-window-excursion + (set-buffer vip-ex-work-buf) + (if (looking-at (format "%c" c)) (forward-char 1)) + (skip-chars-forward " \t") + (cond ((looking-at "g") + (setq ex-token "g") + (forward-char 1) + t) + ((looking-at "c") + (setq ex-token "c") + (forward-char 1) + t) + (t nil)))) + +(defun vip-default-ex-addresses (&optional whole-flag) + "Compute default addresses. WHOLE-FLAG means use the whole buffer." + (cond ((null ex-addresses) + (setq ex-addresses + (if whole-flag + (cons (point-max) (cons (point-min) nil)) + (cons (point) (cons (point) nil))))) + ((null (cdr ex-addresses)) + (setq ex-addresses + (cons (car ex-addresses) ex-addresses))))) + +(defun vip-get-ex-address () + "Get an ex-address as a marker and set ex-flag if a flag is found." + (let ((address (point-marker)) (cont t)) + (setq ex-token "") + (setq ex-flag nil) + (while cont + (vip-get-ex-token) + (cond ((eq ex-token-type 'command) + (if (member ex-token '("print" "list" "#")) + (progn + (setq ex-flag t + cont nil)) + (error "Address expected in this Ex command"))) + ((eq ex-token-type 'end-mark) + (setq cont nil)) + ((eq ex-token-type 'whole) + (error "Trailing address expected")) + ((eq ex-token-type 'comma) + (error "`%s': %s" ex-token vip-SpuriousText)) + (t (let ((ans (vip-get-ex-address-subr address (point-marker)))) + (if ans (setq address ans)))))) + address)) + +(defun vip-get-ex-address-subr (old-address dot) + "Returns an address as a point." + (let ((address nil)) + (if (null old-address) (setq old-address dot)) + (cond ((eq ex-token-type 'dot) + (setq address dot)) + ((eq ex-token-type 'add-number) + (save-excursion + (goto-char old-address) + (forward-line (if (= old-address 0) (1- ex-token) ex-token)) + (setq address (point-marker)))) + ((eq ex-token-type 'sub-number) + (save-excursion + (goto-char old-address) + (forward-line (- ex-token)) + (setq address (point-marker)))) + ((eq ex-token-type 'abs-number) + (save-excursion + (goto-char (point-min)) + (if (= ex-token 0) (setq address 0) + (forward-line (1- ex-token)) + (setq address (point-marker))))) + ((eq ex-token-type 'end) + (setq address (point-max-marker))) + ((eq ex-token-type 'plus) t);; do nothing + ((eq ex-token-type 'minus) t);; do nothing + ((eq ex-token-type 'search-forward) + (save-excursion + (ex-search-address t) + (setq address (point-marker)))) + ((eq ex-token-type 'search-backward) + (save-excursion + (ex-search-address nil) + (setq address (point-marker)))) + ((eq ex-token-type 'goto-mark) + (save-excursion + (if (null ex-token) + (exchange-point-and-mark) + (goto-char (vip-register-to-point + (1+ (- ex-token ?a)) 'enforce-buffer))) + (setq address (point-marker))))) + address)) + + +(defun ex-search-address (forward) + "Search pattern and set address." + (if (string= ex-token "") + (if (null vip-s-string) + (error vip-NoPrevSearch) + (setq ex-token vip-s-string)) + (setq vip-s-string ex-token)) + (if forward + (progn + (forward-line 1) + (re-search-forward ex-token)) + (forward-line -1) + (re-search-backward ex-token))) + +(defun vip-get-ex-buffer () + "Get a buffer name and set `ex-count' and `ex-flag' if found." + (setq ex-buffer nil) + (setq ex-count nil) + (setq ex-flag nil) + (save-window-excursion + (set-buffer vip-ex-work-buf) + (skip-chars-forward " \t") + (if (looking-at "[a-zA-Z]") + (progn + (setq ex-buffer (following-char)) + (forward-char 1) + (skip-chars-forward " \t"))) + (if (looking-at "[0-9]") + (progn + (set-mark (point)) + (re-search-forward "[0-9][0-9]*") + (setq ex-count (string-to-int (buffer-substring (point) (mark t)))) + (skip-chars-forward " \t"))) + (if (looking-at "[pl#]") + (progn + (setq ex-flag t) + (forward-char 1))) + (if (not (looking-at "[\n|]")) + (error "`%s': %s" ex-token vip-SpuriousText)))) + +(defun vip-get-ex-count () + (setq ex-variant nil + ex-count nil + ex-flag nil) + (save-window-excursion + (set-buffer vip-ex-work-buf) + (skip-chars-forward " \t") + (if (looking-at "!") + (progn + (setq ex-variant t) + (forward-char 1))) + (skip-chars-forward " \t") + (if (looking-at "[0-9]") + (progn + (set-mark (point)) + (re-search-forward "[0-9][0-9]*") + (setq ex-count (string-to-int (buffer-substring (point) (mark t)))) + (skip-chars-forward " \t"))) + (if (looking-at "[pl#]") + (progn + (setq ex-flag t) + (forward-char 1))) + (if (not (looking-at "[\n|]")) + (error "`%s': %s" + (buffer-substring (point-min) (1- (point-max))) vip-BadExCommand)))) + +(defun ex-expand-filsyms (cmd buf) + "Expand \% and \# in ex command." + (let (cf pf ret) + (save-excursion + (set-buffer buf) + (setq cf buffer-file-name) + (setq pf (ex-next nil t))) ;; this finds alternative file name + (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd)) + (error "No current file to substitute for `\%'")) + (if (and (null pf) (string-match "[^\\]#\\|\\`#" cmd)) + (error "No alternate file to substitute for `#'")) + (save-excursion + (set-buffer (get-buffer-create " ex-tmp")) + (insert cmd) + (goto-char (point-min)) + (while (re-search-forward "%\\|#" nil t) + (let ((data (match-data)) + (char (buffer-substring (match-beginning 0) (match-end 0)))) + (if (vip-looking-back (concat "\\\\" char)) + (replace-match char) + (store-match-data data) + (if (string= char "%") + (replace-match cf) + (replace-match pf))))) + (end-of-line) + (setq ret (buffer-substring (point-min) (point))) + (kill-buffer (current-buffer)) + (message "%s" ret)) + ret)) + +(defun vip-get-ex-file () + "Get a file name and set ex-variant, `ex-append' and `ex-offset' if found." + (let (prompt) + (setq ex-file nil + ex-variant nil + ex-append nil + ex-offset nil + ex-cmdfile nil) + (save-excursion + (save-window-excursion + (set-buffer vip-ex-work-buf) + (skip-chars-forward " \t") + (if (looking-at "!") + (if (not (vip-looking-back "[ \t]")) + (progn + (setq ex-variant t) + (forward-char 1) + (skip-chars-forward " \t")) + (setq ex-cmdfile t) + (forward-char 1) + (skip-chars-forward " \t"))) + (if (looking-at ">>") + (progn + (setq ex-append t + ex-variant t) + (forward-char 2) + (skip-chars-forward " \t"))) + (if (looking-at "+") + (progn + (forward-char 1) + (set-mark (point)) + (re-search-forward "[ \t\n]") + (backward-char 1) + (setq ex-offset (buffer-substring (point) (mark t))) + (forward-char 1) + (skip-chars-forward " \t"))) + ;; this takes care of :r, :w, etc., when they get file names + ;; from the history list + (if (member ex-token '("read" "write" "edit" "visual")) + (progn + (setq ex-file (buffer-substring (point) (1- (point-max)))) + (setq ex-file + ;; For :e, match multiple non-white strings separated + ;; by white. For others, find the first non-white string + (if (string-match + (if (string= ex-token "edit") + "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*" + "[^ \t\n]+") + ex-file) + (progn + ;; if file name comes from history, don't leave + ;; minibuffer when the user types space + (setq vip-incomplete-ex-cmd nil) + ;; this must be the last clause in this progn + (substring ex-file (match-beginning 0) (match-end 0)) + ) + "")) + ;; this leaves only the command name in the work area + ;; file names are gone + (delete-region (point) (1- (point-max))) + )) + (goto-char (point-max)) + (skip-chars-backward " \t\n") + (setq prompt (buffer-substring (point-min) (point))) + )) + + (setq vip-last-ex-prompt prompt) + + ;; If we just finished reading command, redisplay prompt + (if vip-incomplete-ex-cmd + (setq ex-file (vip-ex-read-file-name (format ":%s " prompt))) + ;; file was typed in-line + (setq ex-file (or ex-file ""))) + )) + + +;; Completes file name or exits minibuffer. If Ex command accepts multiple +;; file names, arranges to re-enter the minibuffer. +(defun vip-complete-filename-or-exit () + (interactive) + (setq vip-keep-reading-filename t) + ;; don't exit if directory---ex-commands don't + (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer)) + (t (minibuffer-complete-word)))) + + +(defun ex-cmd-accepts-multiple-files-p (token) + (member token '("edit" "next" "Next"))) + +;; If user doesn't enter anything, then "" is returned, i.e., the +;; prompt-directory is not returned. +(defun vip-ex-read-file-name (prompt) + (let* ((str "") + (minibuffer-local-completion-map + (copy-keymap minibuffer-local-completion-map)) + beg end cont val) + + (vip-add-keymap ex-read-filename-map minibuffer-local-completion-map) + + (setq cont (setq vip-keep-reading-filename t)) + (while cont + (setq vip-keep-reading-filename nil + val (read-file-name (concat prompt str) nil default-directory) + str (concat str (if (equal val "") "" " ") + val (if (equal val "") "" " "))) + + ;; Only edit, next, and Next commands accept multiple files. + ;; vip-keep-reading-filename is set in the anonymous function that is + ;; bound to " " in ex-read-filename-map. + (setq cont (and vip-keep-reading-filename + (ex-cmd-accepts-multiple-files-p ex-token))) + ) + + (setq beg (string-match "[^ \t]" str) ;; delete leading blanks + end (string-match "[ \t]*$" str)) ;; delete trailing blanks + (if (member ex-token '("read" "write")) + (if (string-match "[\t ]*!" str) + ;; this is actually a shell command + (progn + (setq ex-cmdfile t) + (setq beg (1+ beg)) + (setq vip-last-ex-prompt (concat vip-last-ex-prompt " !"))))) + (substring str (or beg 0) end))) + +(defun vip-execute-ex-command () + "Execute ex command using the value of addresses." + (vip-deactivate-mark) + (cond ((string= ex-token "args") (ex-args)) + ((string= ex-token "copy") (ex-copy nil)) + ((string= ex-token "cd") (ex-cd)) + ((string= ex-token "chdir") (ex-cd)) + ((string= ex-token "delete") (ex-delete)) + ((string= ex-token "edit") (ex-edit)) + ((string= ex-token "file") (vip-info-on-file)) + ((string= ex-token "goto") (ex-goto)) + ((string= ex-token "help") (ex-help)) + ((string= ex-token "join") (ex-line "join")) + ((string= ex-token "kmark") (ex-mark)) + ((string= ex-token "mark") (ex-mark)) + ((string= ex-token "map") (ex-map)) + ((string= ex-token "move") (ex-copy t)) + ((string= ex-token "next") (ex-next ex-cycle-other-window)) + ((string= ex-token "Next") (ex-next (not ex-cycle-other-window))) + ((string= ex-token "RelatedFile") (ex-next-related-buffer 1)) + ((string= ex-token "put") (ex-put)) + ((string= ex-token "pwd") (ex-pwd)) + ((string= ex-token "preserve") (ex-preserve)) + ((string= ex-token "PreviousRelatedFile") (ex-next-related-buffer -1)) + ((string= ex-token "quit") (ex-quit)) + ((string= ex-token "read") (ex-read)) + ((string= ex-token "recover") (ex-recover)) + ((string= ex-token "rewind") (ex-rewind)) + ((string= ex-token "submitReport") (vip-submit-report)) + ((string= ex-token "set") (ex-set)) + ((string= ex-token "shell") (ex-shell)) + ((string= ex-token "source") (ex-source)) + ((string= ex-token "sr") (ex-substitute t t)) + ((string= ex-token "substitute") (ex-substitute)) + ((string= ex-token "suspend") (suspend-emacs)) + ((string= ex-token "stop") (suspend-emacs)) + ((string= ex-token "transfer") (ex-copy nil)) + ((string= ex-token "buffer") (if ex-cycle-other-window + (vip-switch-to-buffer-other-window) + (vip-switch-to-buffer))) + ((string= ex-token "Buffer") (if ex-cycle-other-window + (vip-switch-to-buffer) + (vip-switch-to-buffer-other-window))) + ((string= ex-token "tag") (ex-tag)) + ((string= ex-token "undo") (vip-undo)) + ((string= ex-token "unmap") (ex-unmap)) + ((string= ex-token "version") (vip-version)) + ((string= ex-token "visual") (ex-edit)) + ((string= ex-token "write") (ex-write nil)) + ((string= ex-token "Write") (save-some-buffers)) + ((string= ex-token "wq") (ex-write t)) + ((string= ex-token "WWrite") (save-some-buffers t)) ; don't ask + ((string= ex-token "xit") (ex-write t)) + ((string= ex-token "yank") (ex-yank)) + ((string= ex-token "!") (ex-command)) + ((string= ex-token "=") (ex-line-no)) + ((string= ex-token ">") (ex-line "right")) + ((string= ex-token "<") (ex-line "left")) + ((string= ex-token "&") (ex-substitute t)) + ((string= ex-token "~") (ex-substitute t t)) + ((or (string= ex-token "append") + (string= ex-token "change") + (string= ex-token "insert") + (string= ex-token "open")) + (error + (format "`%s': Obsolete command, not supported by Viper" + ex-token))) + ((or (string= ex-token "abbreviate") + (string= ex-token "unabbreviate")) + (error + (format + "`%s': Vi's abbrevs are obsolete. Use more powerful Emacs' abbrevs" + ex-token))) + ((or (string= ex-token "list") + (string= ex-token "print") + (string= ex-token "z") + (string= ex-token "#")) + (error + (format "`%s': Command not implemented in Viper" ex-token))) + (t (error (format "`%s': %s" ex-token vip-BadExCommand))))) + +(defun vip-undisplayed-files () + (mapcar + (function + (lambda (b) + (if (null (get-buffer-window b)) + (let ((f (buffer-file-name b))) + (if f f + (if ex-cycle-through-non-files + (let ((s (buffer-name b))) + (if (string= " " (substring s 0 1)) + nil + s)) + nil))) + nil))) + (buffer-list))) + + +(defun ex-args () + (let ((l (vip-undisplayed-files)) + (args "") + (file-count 1)) + (while (not (null l)) + (if (car l) + (setq args (format "%s %d) %s\n" args file-count (car l)) + file-count (1+ file-count))) + (setq l (cdr l))) + (if (string= args "") + (message "All files are already displayed") + (save-excursion + (save-window-excursion + (with-output-to-temp-buffer " *vip-info*" + (princ "\n\nThese files are not displayed in any window.\n") + (princ "\n=============\n") + (princ args) + (princ "\n=============\n") + (princ "\nThe numbers can be given as counts to :next. ") + (princ "\n\nPress any key to continue...\n\n")) + (vip-read-char-exclusive)))))) + +(defun ex-cd () + "Ex cd command. Default directory of this buffer changes." + (vip-get-ex-file) + (if (string= ex-file "") + (setq ex-file "~")) + (setq default-directory (file-name-as-directory (expand-file-name ex-file)))) + +(defun ex-copy (del-flag) + "Ex copy and move command. DEL-FLAG means delete." + (vip-default-ex-addresses) + (let ((address (vip-get-ex-address)) + (end (car ex-addresses)) (beg (car (cdr ex-addresses)))) + (goto-char end) + (save-excursion + (push-mark beg t) + (vip-enlarge-region (mark t) (point)) + (if del-flag + (kill-region (point) (mark t)) + (copy-region-as-kill (point) (mark t))) + (if ex-flag + (progn + (with-output-to-temp-buffer "*copy text*" + (princ + (if (or del-flag ex-g-flag ex-g-variant) + (current-kill 0) + (buffer-substring (point) (mark t))))) + (condition-case nil + (progn + (read-string "[Hit return to continue] ") + (save-excursion (kill-buffer "*copy text*"))) + (quit (save-excursion (kill-buffer "*copy text*")) + (signal 'quit nil)))))) + (if (= address 0) + (goto-char (point-min)) + (goto-char address) + (forward-line 1)) + (insert (current-kill 0)))) + +(defun ex-delete () + "Ex delete command." + (vip-default-ex-addresses) + (vip-get-ex-buffer) + (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))) + (if (> beg end) (error vip-FirstAddrExceedsSecond)) + (save-excursion + (vip-enlarge-region beg end) + (exchange-point-and-mark) + (if ex-count + (progn + (set-mark (point)) + (forward-line (1- ex-count))) + (set-mark end)) + (vip-enlarge-region (point) (mark t)) + (if ex-flag + ;; show text to be deleted and ask for confirmation + (progn + (with-output-to-temp-buffer " *delete text*" + (princ (buffer-substring (point) (mark t)))) + (condition-case nil + (read-string "[Hit return to continue] ") + (quit + (save-excursion (kill-buffer " *delete text*")) + (error ""))) + (save-excursion (kill-buffer " *delete text*"))) + (if ex-buffer + (cond ((vip-valid-register ex-buffer '(Letter)) + (vip-append-to-register + (downcase ex-buffer) (point) (mark t))) + ((vip-valid-register ex-buffer) + (copy-to-register ex-buffer (point) (mark t) nil)) + (t (error vip-InvalidRegister ex-buffer)))) + (kill-region (point) (mark t)))))) + + + +(defun ex-edit (&optional file) + "Ex edit command. +In Viper, `e' and `e!' behave identically. In both cases, the user is +asked if current buffer should really be discarded. +This command can take multiple file names. It replaces the current buffer +with the first file in its argument list." + (if (not file) + (vip-get-ex-file)) + (cond ((and (string= ex-file "") buffer-file-name) + (setq ex-file (abbreviate-file-name (buffer-file-name)))) + ((string= ex-file "") + (error vip-NoFileSpecified))) + + (let (msg do-edit) + (if buffer-file-name + (cond ((buffer-modified-p) + (setq msg + (format "Buffer %s is modified. Edit buffer? " + (buffer-name)) + do-edit t)) + ((not (verify-visited-file-modtime (current-buffer))) + (setq msg + (format "File %s changed on disk. Reread from disk? " + buffer-file-name) + do-edit t)) + (t (setq do-edit nil)))) + + (if do-edit + (if (yes-or-no-p msg) + (progn + (set-buffer-modified-p nil) + (kill-buffer (current-buffer))) + (message "Buffer %s was left intact" (buffer-name)))) + ) ; let + + (if (null (setq file (get-file-buffer ex-file))) + (progn + (ex-find-file ex-file) + (vip-change-state-to-vi) + (goto-char (point-min))) + (switch-to-buffer file)) + (if ex-offset + (progn + (save-window-excursion + (set-buffer vip-ex-work-buf) + (delete-region (point-min) (point-max)) + (insert ex-offset "\n") + (goto-char (point-min))) + (goto-char (vip-get-ex-address)) + (beginning-of-line))) + (ex-fixup-history vip-last-ex-prompt ex-file)) + +;; splits the string FILESPEC into substrings separated by newlines `\012' +;; each line assumed to be a file name. find-file's each file thus obtained. +(defun ex-find-file (filespec) + (let (s f filebuf) + (if (string-match "[^a-zA-Z0-9_.-/]" filespec) + (progn + (save-excursion + (set-buffer (get-buffer-create " ex-tmp")) + (call-process ex-find-file-shell nil t nil + ex-find-file-shell-options + "-c" + (format "echo %s | tr ' ' '\\012'" filespec)) + (goto-char (point-min)) + (while (not (eobp)) + (setq s (point)) + (end-of-line) + (setq f (buffer-substring s (point))) + (setq filebuf (find-file-noselect f)) + (forward-to-indentation 1)) + (kill-buffer (current-buffer)))) + (setq filebuf (find-file-noselect (setq f filespec)))) + (switch-to-buffer filebuf) + )) + +(defun ex-global (variant) + "Ex global command." + (let ((gcommand ex-token)) + (if (or ex-g-flag ex-g-variant) + (error "`%s' within `global' is not allowed" gcommand) + (if variant + (setq ex-g-flag nil + ex-g-variant t) + (setq ex-g-flag t + ex-g-variant nil))) + (vip-get-ex-pat) + (if (null ex-token) + (error "`%s': Missing regular expression" gcommand))) + + (if (string= ex-token "") + (if (null vip-s-string) + (error vip-NoPrevSearch) + (setq ex-g-pat vip-s-string)) + (setq ex-g-pat ex-token + vip-s-string ex-token)) + (if (null ex-addresses) + (setq ex-addresses (list (point-max) (point-min))) + (vip-default-ex-addresses)) + (let ((marks nil) (mark-count 0) + com-str (end (car ex-addresses)) (beg (car (cdr ex-addresses)))) + (if (> beg end) (error vip-FirstAddrExceedsSecond)) + (save-excursion + (vip-enlarge-region beg end) + (exchange-point-and-mark) + (let ((cont t) (limit (point-marker))) + (exchange-point-and-mark) + ;; skip the last line if empty + (beginning-of-line) + (if (eobp) (vip-backward-char-carefully)) + (while (and cont (not (bobp)) (>= (point) limit)) + (beginning-of-line) + (set-mark (point)) + (end-of-line) + (let ((found (re-search-backward ex-g-pat (mark t) t))) + (if (or (and ex-g-flag found) + (and ex-g-variant (not found))) + (progn + (end-of-line) + (setq mark-count (1+ mark-count)) + (setq marks (cons (point-marker) marks))))) + (beginning-of-line) + (if (bobp) (setq cont nil) + (forward-line -1) + (end-of-line))))) + (save-window-excursion + (set-buffer vip-ex-work-buf) + (setq com-str (buffer-substring (1+ (point)) (1- (point-max))))) + (while marks + (goto-char (car marks)) + (vip-ex com-str) + (setq mark-count (1- mark-count)) + (setq marks (cdr marks))))) + +(defun ex-goto () + "Ex goto command." + (if (null ex-addresses) + (setq ex-addresses (cons (point) nil))) + (push-mark (point) t) + (goto-char (car ex-addresses)) + (beginning-of-line)) + +(defun ex-line (com) + "Ex line commands. COM is join, shift-right or shift-left." + (vip-default-ex-addresses) + (vip-get-ex-count) + (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point) + (if (> beg end) (error vip-FirstAddrExceedsSecond)) + (save-excursion + (vip-enlarge-region beg end) + (exchange-point-and-mark) + (if ex-count + (progn + (set-mark (point)) + (forward-line ex-count))) + (if ex-flag + ;; show text to be joined and ask for confirmation + (progn + (with-output-to-temp-buffer " *text*" + (princ (buffer-substring (point) (mark t)))) + (condition-case nil + (progn + (read-string "[Hit return to continue] ") + (ex-line-subr com (point) (mark t))) + (quit (ding))) + (save-excursion (kill-buffer " *text*"))) + (ex-line-subr com (point) (mark t))) + (setq point (point))) + (goto-char (1- point)) + (beginning-of-line))) + +(defun ex-line-subr (com beg end) + (cond ((string= com "join") + (goto-char (min beg end)) + (while (and (not (eobp)) (< (point) (max beg end))) + (end-of-line) + (if (and (<= (point) (max beg end)) (not (eobp))) + (progn + (forward-line 1) + (delete-region (point) (1- (point))) + (if (not ex-variant) (fixup-whitespace)))))) + ((or (string= com "right") (string= com "left")) + (indent-rigidly + (min beg end) (max beg end) + (if (string= com "right") vip-shift-width (- vip-shift-width))) + (goto-char (max beg end)) + (end-of-line) + (vip-forward-char-carefully)))) + + +(defun ex-mark () + "Ex mark command." + (let (char) + (if (null ex-addresses) + (setq ex-addresses + (cons (point) nil))) + (save-window-excursion + (set-buffer vip-ex-work-buf) + (skip-chars-forward " \t") + (if (looking-at "[a-z]") + (progn + (setq char (following-char)) + (forward-char 1) + (skip-chars-forward " \t") + (if (not (looking-at "[\n|]")) + (error "`%s': %s" ex-token vip-SpuriousText))) + (error "`%s' requires a following letter" ex-token))) + (save-excursion + (goto-char (car ex-addresses)) + (point-to-register (1+ (- char ?a)))))) + + + +;; Alternate file is the file next to the first one in the buffer ring +(defun ex-next (cycle-other-window &optional find-alt-file) + (catch 'ex-edit + (let (count l) + (if (not find-alt-file) + (progn + (vip-get-ex-file) + (if (or (char-or-string-p ex-offset) + (not (string= "" ex-file))) + ;(and (not (string= "" ex-file)) + ; (not (string-match "[0-9]+" ex-file)))) + (progn + (ex-edit t) + (throw 'ex-edit nil)) + (setq count (string-to-int ex-file)) + (if (= count 0) (setq count 1)) + (if (< count 0) (error "Usage: `next <count>' (count >= 0)")))) + (setq count 1)) + (setq l (vip-undisplayed-files)) + (while (> count 0) + (while (and (not (null l)) (null (car l))) + (setq l (cdr l))) + (setq count (1- count)) + (if (> count 0) + (setq l (cdr l)))) + (if find-alt-file (car l) + (progn + (if (car l) + (let* ((w (if cycle-other-window + (get-lru-window) (selected-window))) + (b (window-buffer w))) + (set-window-buffer w (get-file-buffer (car l))) + (bury-buffer b)) + (error "Not that many undisplayed files"))))))) + + +(defun ex-next-related-buffer (direction &optional no-recursion) + + (vip-ring-rotate1 vip-related-files-and-buffers-ring direction) + + (let ((file-or-buffer-name + (vip-current-ring-item vip-related-files-and-buffers-ring)) + (old-ring vip-related-files-and-buffers-ring) + (old-win (selected-window)) + skip-rest buf wind) + + (or (and (ring-p vip-related-files-and-buffers-ring) + (> (ring-length vip-related-files-and-buffers-ring) 0)) + (error "This buffer has no related files or buffers")) + + (or (stringp file-or-buffer-name) + (error + "File and buffer names must be strings, %S" file-or-buffer-name)) + + (setq buf (cond ((get-buffer file-or-buffer-name)) + ((file-exists-p file-or-buffer-name) + (find-file-noselect file-or-buffer-name)) + )) + + (if (not (vip-buffer-live-p buf)) + (error "Didn't find buffer %S or file %S" + file-or-buffer-name + (abbreviate-file-name (expand-file-name file-or-buffer-name)))) + + (if (equal buf (current-buffer)) + (or no-recursion + ;; try again + (setq skip-rest t) + (ex-next-related-buffer direction 'norecursion))) + + (if skip-rest + () + ;; setup buffer + (if (setq wind (vip-get-visible-buffer-window buf)) + () + (setq wind (get-lru-window (if vip-xemacs-p nil 'visible))) + (set-window-buffer wind buf)) + + (if window-system + (progn + (vip-raise-frame (vip-window-frame wind)) + (if (equal (vip-window-frame wind) (vip-window-frame old-win)) + (save-window-excursion (select-window wind) (sit-for 1)) + (select-window wind))) + (save-window-excursion (select-window wind) (sit-for 1))) + + (save-excursion + (set-buffer buf) + (setq vip-related-files-and-buffers-ring old-ring)) + + (setq vip-local-search-start-marker (point-marker)) + ))) + + +(defun ex-preserve () + "Force auto save." + (message "Autosaving all buffers that need to be saved...") + (do-auto-save t)) + +(defun ex-put () + "Ex put." + (let ((point (if (null ex-addresses) (point) (car ex-addresses)))) + (vip-get-ex-buffer) + (setq vip-use-register ex-buffer) + (goto-char point) + (if (bobp) (vip-Put-back 1) (vip-put-back 1)))) + +(defun ex-pwd () + "Ex print working directory." + (message default-directory)) + +(defun ex-quit () + "Ex quit command." + (if (< vip-expert-level 3) + (save-buffers-kill-emacs) + (kill-buffer (current-buffer)))) + + +(defun ex-read () + "Ex read command." + (vip-get-ex-file) + (let ((point (if (null ex-addresses) (point) (car ex-addresses)))) + (goto-char point) + (vip-add-newline-at-eob-if-necessary) + (if (not (or (bobp) (eobp))) (forward-line 1)) + (if (and (not ex-variant) (string= ex-file "")) + (progn + (if (null buffer-file-name) + (error vip-NoFileSpecified)) + (setq ex-file buffer-file-name))) + (if ex-cmdfile + (shell-command ex-file t) + (insert-file-contents ex-file))) + (ex-fixup-history vip-last-ex-prompt ex-file)) + +;; this function fixes ex-history for some commands like ex-read, ex-edit +(defun ex-fixup-history (&rest args) + (setq vip-ex-history + (cons (mapconcat 'identity args " ") (cdr vip-ex-history)))) + + +(defun ex-recover () + "Ex recover from emacs \#file\#." + (vip-get-ex-file) + (if (or ex-append ex-offset) + (error "`recover': %s" vip-SpuriousText)) + (if (string= ex-file "") + (progn + (if (null buffer-file-name) + (error "This buffer isn't visiting any file")) + (setq ex-file buffer-file-name)) + (setq ex-file (expand-file-name ex-file))) + (if (and (not (string= ex-file (buffer-file-name))) + (buffer-modified-p) + (not ex-variant)) + (error "No write since last change \(:rec! overrides\)")) + (recover-file ex-file)) + +(defun ex-rewind () + "Tell that `rewind' is obsolete and that one should use `:next count'" + (message + "Use `:n <count>' instead. Counts are obtained from the `:args' command")) + + +;; read variable name for ex-set +(defun ex-set-read-variable () + (let ((minibuffer-local-completion-map + (copy-keymap minibuffer-local-completion-map)) + (cursor-in-echo-area t) + str batch) + (define-key + minibuffer-local-completion-map " " 'minibuffer-complete-and-exit) + (define-key minibuffer-local-completion-map "=" 'exit-minibuffer) + (if (vip-set-unread-command-events + (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil "\C-m")) + (progn + (setq batch t) + (vip-set-unread-command-events ?\C-m))) + (message ":set <Variable> [= <Value>]") + (or batch (sit-for 2)) + + (while (string-match "^[ \\t\\n]*$" + (setq str + (completing-read ":set " ex-variable-alist))) + (message ":set <Variable> ") + ;; if there are unread events, don't wait + (or (vip-set-unread-command-events "") (sit-for 2)) + ) ; while + str)) + + +(defun ex-set () + (let ((var (ex-set-read-variable)) + (val 0) + (set-cmd "setq") + (ask-if-save t) + (auto-cmd-label "; don't touch or else...") + (delete-turn-on-auto-fill-pattern + "([ \t]*add-hook[ \t]+'vip-insert-state-hooks[ \t]+'turn-on-auto-fill.*)") + actual-lisp-cmd lisp-cmd-del-pattern + val2 orig-var) + (setq orig-var var) + (cond ((member var '("ai" "autoindent")) + (setq var "vip-auto-indent" + val "t")) + ((member var '("noai" "noautoindent")) + (setq var "vip-auto-indent" + val "nil")) + ((member var '("ic" "ignorecase")) + (setq var "vip-case-fold-search" + val "t")) + ((member var '("noic" "noignorecase")) + (setq var "vip-case-fold-search" + val "nil")) + ((member var '("ma" "magic")) + (setq var "vip-re-search" + val "t")) + ((member var '("noma" "nomagic")) + (setq var "vip-re-search" + val "nil")) + ((member var '("ro" "readonly")) + (setq var "buffer-read-only" + val "t")) + ((member var '("noro" "noreadonly")) + (setq var "buffer-read-only" + val "nil")) + ((member var '("sm" "showmatch")) + (setq var "blink-matching-paren" + val "t")) + ((member var '("nosm" "noshowmatch")) + (setq var "blink-matching-paren" + val "nil")) + ((member var '("ws" "wrapscan")) + (setq var "vip-search-wrap-around-t" + val "t")) + ((member var '("nows" "nowrapscan")) + (setq var "vip-search-wrap-around-t" + val "nil"))) + (if (eq val 0) ;; value must be set by the user + (let ((cursor-in-echo-area t)) + (message (format ":set %s = <Value>" var)) + ;; if there are unread events, don't wait + (or (vip-set-unread-command-events "") (sit-for 2)) + (setq val (read-string (format ":set %s = " var))) + (ex-fixup-history "set" orig-var val) + + ;; check numerical values + (if (member var + '("sw" "shiftwidth" "ts" "tabstop" "wm" "wrapmargin")) + (condition-case nil + (or (numberp (setq val2 (car (read-from-string val)))) + (error "%s: Invalid value, numberp, %S" var val)) + (error + (error "%s: Invalid value, numberp, %S" var val)))) + + (cond + ((member var '("sw" "shiftwidth")) + (setq var "vip-shift-width")) + ((member var '("ts" "tabstop")) + ;; make it take effect in curr buff and new bufs + (kill-local-variable 'tab-width) + (setq var "tab-width" + set-cmd "setq-default")) + ((member var '("tsl" "tab-stop-local")) + (setq var "tab-width" + set-cmd "setq" + ask-if-save nil)) + ((member var '("wm" "wrapmargin")) + ;; make it take effect in curr buff and new bufs + (kill-local-variable 'fill-column) + (setq var "fill-column" + val (format "(- (window-width) %s)" val) + set-cmd "setq-default")) + ((member var '("sh" "shell")) + (setq var "explicit-shell-file-name" + val (format "\"%s\"" val))))) + (ex-fixup-history "set" orig-var)) + + (setq actual-lisp-cmd (format "\n(%s %s %s) %s" + set-cmd var val auto-cmd-label)) + (setq lisp-cmd-del-pattern + (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s" + set-cmd var auto-cmd-label)) + + (if (and ask-if-save + (y-or-n-p (format "Do you want to save this setting in %s " + vip-custom-file-name))) + (progn + (vip-save-string-in-file + actual-lisp-cmd vip-custom-file-name + ;; del pattern + lisp-cmd-del-pattern) + (if (string= var "fill-column") + (if (> val2 0) + (vip-save-string-in-file + (concat + "(add-hook 'vip-insert-state-hooks 'turn-on-auto-fill) " + auto-cmd-label) + vip-custom-file-name + delete-turn-on-auto-fill-pattern) + (vip-save-string-in-file + nil vip-custom-file-name delete-turn-on-auto-fill-pattern) + (vip-save-string-in-file + nil vip-custom-file-name + ;; del pattern + lisp-cmd-del-pattern) + )) + )) + + (message (format "%s %s %s" set-cmd var (if (string-match "^[ \t]*$" val) + (format "%S" val) + val))) + (eval (car (read-from-string actual-lisp-cmd))) + (if (string= var "fill-column") + (if (> val2 0) + (auto-fill-mode 1) + (auto-fill-mode -1))) + + )) + +;; In inline args, skip regex-forw and (optionally) chars-back. +;; Optional 3d arg is a string that should replace ' ' to prevent its +;; special meaning +(defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str) + (save-excursion + (set-buffer vip-ex-work-buf) + (goto-char (point-min)) + (re-search-forward regex-forw nil t) + (let ((beg (point)) + end) + (goto-char (point-max)) + (if chars-back + (skip-chars-backward chars-back) + (skip-chars-backward " \t\n\C-m")) + (setq end (point)) + ;; replace SPC with `=' to suppress the special meaning SPC has + ;; in Ex commands + (goto-char beg) + (if replace-str + (while (re-search-forward " +" nil t) + (replace-match replace-str nil t) + (vip-forward-char-carefully))) + (goto-char end) + (buffer-substring beg end)))) + + +(defun ex-shell () + "Ex shell command." + (shell)) + +(defun ex-help () + "Viper help. Invokes Info." + (condition-case nil + (progn + (pop-to-buffer (get-buffer-create "*info*")) + (info vip-info-file-name) + (message "Type `i' to search for a specific topic")) + (error (beep 1) + (with-output-to-temp-buffer " *vip-info*" + (princ "The Info file for Viper does not seem to be installed. + +This file is part of the distribution of Viper. If you do not +have the full distribution, please obtain it from the `anonymous' +FTP account at `archive.cis.ohio-state.edu': + + /pub/gnu/emacs/elisp-archive/modes/viper.shar + +The Info files for Viper should be installed as <name>, <name>-1, etc., +where <name> is the value of `vip-info-file-name'."))))) + +(defun ex-source () + "Ex source command. Loads the file specified as argument or `~/.vip'." + (vip-get-ex-file) + (if (string= ex-file "") + (load vip-custom-file-name) + (load ex-file))) + +(defun ex-substitute (&optional repeat r-flag) + "Ex substitute command. +If REPEAT use previous regexp which is ex-reg-exp or vip-s-string" + (let ((opt-g nil) + (opt-c nil) + (matched-pos nil) + (case-fold-search vip-case-fold-search) + delim pat repl) + (if repeat (setq ex-token nil) (setq delim (vip-get-ex-pat))) + (if (null ex-token) + (setq pat (if r-flag vip-s-string ex-reg-exp) + repl ex-repl + delim (string-to-char pat)) + (setq pat (if (string= ex-token "") vip-s-string ex-token)) + (setq vip-s-string pat + ex-reg-exp pat) + (setq delim (vip-get-ex-pat)) + (if (null ex-token) + (setq ex-token "" + ex-repl "") + (setq repl ex-token + ex-repl ex-token))) + (while (vip-get-ex-opt-gc delim) + (if (string= ex-token "g") (setq opt-g t) (setq opt-c t))) + (vip-get-ex-count) + (if ex-count + (save-excursion + (if ex-addresses (goto-char (car ex-addresses))) + (set-mark (point)) + (forward-line (1- ex-count)) + (setq ex-addresses (cons (point) (cons (mark t) nil)))) + (if (null ex-addresses) + (setq ex-addresses (cons (point) (cons (point) nil))) + (if (null (cdr ex-addresses)) + (setq ex-addresses (cons (car ex-addresses) ex-addresses))))) + ;(setq G opt-g) + (let ((beg (car ex-addresses)) + (end (car (cdr ex-addresses))) + eol-mark) + (save-excursion + (vip-enlarge-region beg end) + (let ((limit (save-excursion + (goto-char (max (point) (mark t))) + (point-marker)))) + (goto-char (min (point) (mark t))) + (while (< (point) limit) + (end-of-line) + (setq eol-mark (point-marker)) + (beginning-of-line) + (if opt-g + (progn + (while (and (not (eolp)) + (re-search-forward pat eol-mark t)) + (if (or (not opt-c) (y-or-n-p "Replace? ")) + (progn + (setq matched-pos (point)) + (if (not (stringp repl)) + (error "Can't perform Ex substitution: No previous replacement pattern")) + (replace-match repl t t)))) + (end-of-line) + (vip-forward-char-carefully)) + (if (null pat) + (error + "Can't repeat Ex substitution: No previous regular expression")) + (if (and (re-search-forward pat eol-mark t) + (or (not opt-c) (y-or-n-p "Replace? "))) + (progn + (setq matched-pos (point)) + (if (not (stringp repl)) + (error "Can't perform Ex substitution: No previous replacement pattern")) + (replace-match repl t t))) + (end-of-line) + (vip-forward-char-carefully)))))) + (if matched-pos (goto-char matched-pos)) + (beginning-of-line) + (if opt-c (message "done")))) + +(defun ex-tag () + "Ex tag command." + (let (tag) + (save-window-excursion + (set-buffer vip-ex-work-buf) + (skip-chars-forward " \t") + (set-mark (point)) + (skip-chars-forward "^ |\t\n") + (setq tag (buffer-substring (mark t) (point)))) + (if (not (string= tag "")) (setq ex-tag tag)) + (vip-change-state-to-emacs) + (condition-case conds + (progn + (if (string= tag "") + (find-tag ex-tag t) + (find-tag-other-window ex-tag)) + (vip-change-state-to-vi)) + (error + (vip-change-state-to-vi) + (vip-message-conditions conds))))) + +(defun ex-write (q-flag) + "Ex write command." + (vip-default-ex-addresses t) + (vip-get-ex-file) + (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) + temp-buf writing-same-file region + file-exists writing-whole-file) + (if (> beg end) (error vip-FirstAddrExceedsSecond)) + (if ex-cmdfile + (progn + (vip-enlarge-region beg end) + (shell-command-on-region (point) (mark t) ex-file)) + (if (and (string= ex-file "") (not (buffer-file-name))) + (setq ex-file + (read-file-name + (format "Buffer %s isn't visiting any file. File to save in: " + (buffer-name))))) + + (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end)) + ex-file (if (string= ex-file "") + (buffer-file-name) + (expand-file-name ex-file)) + file-exists (file-exists-p ex-file) + writing-same-file (string= ex-file (buffer-file-name))) + (if (and writing-whole-file writing-same-file) + (if (not (buffer-modified-p)) + (message "(No changes need to be saved)") + (save-buffer) + (ex-write-info file-exists ex-file beg end)) + ;; writing some other file or portion of the currents + ;; file---create temp buffer for it + ;; disable undo in that buffer, for efficiency + (buffer-disable-undo (setq temp-buf (create-file-buffer ex-file))) + (unwind-protect + (save-excursion + (if (and file-exists + (not writing-same-file) + (not (yes-or-no-p + (format "File %s exists. Overwrite? " ex-file)))) + (error "Quit") + (vip-enlarge-region beg end) + (setq region (buffer-substring (point) (mark t))) + (set-buffer temp-buf) + (set-visited-file-name ex-file) + (erase-buffer) + (if (and file-exists ex-append) + (insert-file-contents ex-file)) + (goto-char (point-max)) + (insert region) + (save-buffer) + (ex-write-info file-exists ex-file (point-min) (point-max)) + ) + (set-buffer temp-buf) + (set-buffer-modified-p nil) + (kill-buffer temp-buf) + ) + )) + ;; this prevents the loss of data if writing part of the buffer + (if (and (buffer-file-name) writing-same-file) + (set-visited-file-modtime)) + (or writing-whole-file + (not writing-same-file) + (set-buffer-modified-p t)) + (if q-flag + (if (< vip-expert-level 2) + (save-buffers-kill-emacs) + (kill-buffer (current-buffer)))) + ))) + + +(defun ex-write-info (exists file-name beg end) + (message "`%s'%s %d lines, %d characters" + (abbreviate-file-name file-name) + (if exists "" " [New file]") + (count-lines beg (min (1+ end) (point-max))) + (- end beg))) + +(defun ex-yank () + "Ex yank command." + (vip-default-ex-addresses) + (vip-get-ex-buffer) + (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))) + (if (> beg end) (error vip-FirstAddrExceedsSecond)) + (save-excursion + (vip-enlarge-region beg end) + (exchange-point-and-mark) + (if (or ex-g-flag ex-g-variant) + (error "Can't execute `yank' within `global'")) + (if ex-count + (progn + (set-mark (point)) + (forward-line (1- ex-count))) + (set-mark end)) + (vip-enlarge-region (point) (mark t)) + (if ex-flag (error "`yank': %s" vip-SpuriousText)) + (if ex-buffer + (cond ((vip-valid-register ex-buffer '(Letter)) + (vip-append-to-register + (downcase ex-buffer) (point) (mark t))) + ((vip-valid-register ex-buffer) + (copy-to-register ex-buffer (point) (mark t) nil)) + (t (error vip-InvalidRegister ex-buffer)))) + (copy-region-as-kill (point) (mark t))))) + +(defun ex-command () + "Execute shell command." + (let (command) + (save-window-excursion + (set-buffer vip-ex-work-buf) + (skip-chars-forward " \t") + (setq command (buffer-substring (point) (point-max))) + (end-of-line)) + (setq command (ex-expand-filsyms command (current-buffer))) + (if (and (> (length command) 0) (string= "!" (substring command 0 1))) + (if vip-ex-last-shell-com + (setq command (concat vip-ex-last-shell-com (substring command 1))) + (error "No previous shell command"))) + (setq vip-ex-last-shell-com command) + (if (null ex-addresses) + (shell-command command) + (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))) + (if (null beg) (setq beg end)) + (save-excursion + (goto-char beg) + (set-mark end) + (vip-enlarge-region (point) (mark t)) + (shell-command-on-region (point) (mark t) command t)) + (goto-char beg))))) + +(defun ex-line-no () + "Print line number." + (message "%d" + (1+ (count-lines + (point-min) + (if (null ex-addresses) (point-max) (car ex-addresses)))))) + +(defun vip-info-on-file () + "Give information on the file visited by the current buffer." + (interactive) + (message "%s: pos=%d(%d) line=%d(%d) col=%d %s" + (if (buffer-file-name) + (abbreviate-file-name (buffer-file-name)) + "[No visited file]") + (point) (1- (point-max)) + (count-lines (point-min) (vip-line-pos 'end)) + (count-lines (point-min) (point-max)) + (1+ (current-column)) + (if (buffer-modified-p) "[Modified]" "[Unchanged]") + )) + + +(provide 'viper-ex) + +;;; viper-ex.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/emulation/viper-keym.el Mon Feb 20 23:15:10 1995 +0000 @@ -0,0 +1,525 @@ +;;; viper-keym.el -- Main Viper keymaps + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 2, or (at your option) +;; any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + + +(require 'viper-util) + +;;; Variables + +;;; Keymaps + +;; Keymaps for vital things like \e and C-z. +;; Not for users +(defvar vip-vi-intercept-map (make-sparse-keymap)) +(defvar vip-insert-intercept-map (make-sparse-keymap)) +(defvar vip-emacs-intercept-map (make-sparse-keymap)) + +(vip-deflocalvar vip-vi-local-user-map (make-sparse-keymap) + "Keymap for user-defined local bindings. +Useful for changing bindings such as ZZ in certain major modes. +For instance, in letter-mode, one may want to bind ZZ to +mh-send-letter. In a newsreader such as gnus, tin, or rn, ZZ could be bound +to save-buffers-kill-emacs then post article, etc.") +(put 'vip-vi-local-user-map 'permanent-local t) + +(defvar vip-vi-global-user-map (make-sparse-keymap) + "Keymap for user-defined global bindings. +These bindings are seen in all Viper buffers.") + +(defvar vip-vi-basic-map (make-keymap) + "This is the main keymap in effect in Viper's Vi state. +This map is global, shared by all buffers.") + +(defvar vip-vi-kbd-map (make-sparse-keymap) + "This keymap keeps keyboard macros defined via the :map command.") + +(defvar vip-vi-diehard-map (make-sparse-keymap) + "This keymap is in use when the user asks Viper to simulate Vi very closely. +This happens when vip-expert-level is 1 or 2. See vip-set-expert-level.") + + +(vip-deflocalvar vip-insert-local-user-map (make-sparse-keymap) + "Auxiliary map for per-buffer user-defined keybindings in Insert state.") +(put 'vip-insert-local-user-map 'permanent-local t) + +(defvar vip-insert-global-user-map (make-sparse-keymap) + "Auxiliary map for global user-defined bindings in Insert state.") + +(defvar vip-insert-basic-map (make-sparse-keymap) + "The basic insert-mode keymap.") + +(defvar vip-insert-diehard-map (make-keymap) + "Map used when user wants vi-style keys in insert mode. +Most of the Emacs keys are suppressed. This map overshadows +vip-insert-basic-map. Not recommended, except for novice users.") + +(defvar vip-insert-kbd-map (make-sparse-keymap) + "This keymap keeps VI-style kbd macros for insert mode.") + +(defvar vip-replace-map (make-sparse-keymap) + "Map used in Viper's replace state.") + +(defvar vip-emacs-global-user-map (make-sparse-keymap) + "Auxiliary map for global user-defined bindings in Emacs state.") + +(defvar vip-emacs-kbd-map (make-sparse-keymap) + "This keymap keeps Vi-style kbd macros for emacs mode.") + +(vip-deflocalvar vip-emacs-local-user-map (make-sparse-keymap) + "Auxiliary map for local user-defined bindings in Emacs state.") +(put 'vip-emacs-local-user-map 'permanent-local t) + +;; This keymap should stay empty +(defvar vip-empty-keymap (make-sparse-keymap)) + + +;;; Variables used by minor modes + +;; Association list of the form +;; ((major-mode . keymap) (major-mode . keymap) ...) +;; Viper uses these keymaps to make user-requested adjustments +;; to its Vi state in various major modes.") +(defvar vip-vi-state-modifier-alist nil) + +;; Association list of the form +;; ((major-mode . keymap) (major-mode . keymap) ...) +;; Viper uses these keymaps to make user-requested adjustments +;; to its Insert state in various major modes.") +(defvar vip-insert-state-modifier-alist nil) + +;; Association list of the form +;; ((major-mode . keymap) (major-mode . keymap) ...) +;; Viper uses these keymaps to make user-requested adjustments +;; to its Emacs state in various major modes. +(defvar vip-emacs-state-modifier-alist nil) + +;; Tells vip-add-local-keys to create a new vip-vi-local-user-map for new +;; buffers. Not a user option. +(vip-deflocalvar vip-need-new-vi-local-map t "") +(put 'vip-need-new-vi-local-map 'permanent-local t) + +;; Tells vip-add-local-keys to create a new vip-insert-local-user-map for new +;; buffers. Not a user option. +(vip-deflocalvar vip-need-new-insert-local-map t "") +(put 'vip-need-new-insert-local-map 'permanent-local t) + +;; Tells vip-add-local-keys to create a new vip-emacs-local-user-map for new +;; buffers. Not a user option. +(vip-deflocalvar vip-need-new-emacs-local-map t "") +(put 'vip-need-new-emacs-local-map 'permanent-local t) + + + +;; Insert mode keymap + +;; for novice users, pretend you are the real vi. +(define-key vip-insert-diehard-map "\t" 'vip-insert-tab) +(define-key vip-insert-diehard-map "\C-a" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-b" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-c" 'vip-change-state-to-vi) +(define-key vip-insert-diehard-map "\C-e" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-f" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-g" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-i" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-k" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-l" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-n" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-o" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-p" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-q" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-r" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-s" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-u" 'vip-erase-line) +(define-key vip-insert-diehard-map "\C-x" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-y" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-z" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-]" 'self-insert-command) +(define-key vip-insert-diehard-map "\C-_" 'self-insert-command) + +(let ((i ?\ )) + (while (<= i ?~) + (define-key vip-insert-diehard-map (make-string 1 i) 'self-insert-command) + (setq i (1+ i)))) + +;; Insert mode map when user wants emacs style +(define-key vip-insert-basic-map "\C-d" 'vip-backward-indent) +(define-key vip-insert-basic-map "\C-w" 'vip-delete-backward-word) +(define-key vip-insert-basic-map "\C-t" 'vip-forward-indent) +(define-key vip-insert-basic-map + (if vip-xemacs-p [(shift tab)] [S-tab]) 'vip-insert-tab) +(define-key vip-insert-basic-map "\C-v" 'quoted-insert) +(define-key vip-insert-basic-map "\C-?" 'vip-del-backward-char-in-insert) +(define-key vip-insert-basic-map "\C-c\M-p" + 'vip-insert-prev-from-insertion-ring) +(define-key vip-insert-basic-map "\C-c\M-n" + 'vip-insert-next-from-insertion-ring) + + +;; Replace keymap +(define-key vip-replace-map "\C-t" 'vip-forward-indent) +(define-key vip-replace-map "\C-j" 'vip-replace-state-exit-cmd) +(define-key vip-replace-map "\C-m" 'vip-replace-state-exit-cmd) +(define-key vip-replace-map "\C-?" 'vip-del-backward-char-in-replace) + + + +;; Vi keymaps + +(define-key vip-vi-basic-map "\C-^" + (function (lambda () (interactive) (vip-ex "e#")))) +(define-key vip-vi-basic-map "\C-b" 'vip-scroll-back) +(define-key vip-vi-basic-map "\C-d" 'vip-scroll-up) +(define-key vip-vi-basic-map "\C-e" 'vip-scroll-up-one) +(define-key vip-vi-basic-map "\C-f" 'vip-scroll) +(define-key vip-vi-basic-map "\C-m" 'vip-next-line-at-bol) +(define-key vip-vi-basic-map "\C-u" 'vip-scroll-down) +(define-key vip-vi-basic-map "\C-y" 'vip-scroll-down-one) +(define-key vip-vi-basic-map "\C-s" 'vip-isearch-forward) +(define-key vip-vi-basic-map "\C-r" 'vip-isearch-backward) +;(define-key vip-vi-basic-map "\C-\\" 'universal-argument) +(define-key vip-vi-basic-map "\C-c/" 'vip-toggle-search-style) +(define-key vip-vi-basic-map "\C-cg" 'vip-info-on-file) + +(define-key vip-vi-basic-map "\C-c\M-p" 'vip-prev-destructive-command) +(define-key vip-vi-basic-map "\C-c\M-n" 'vip-next-destructive-command) + + +(define-key vip-vi-basic-map " " 'vip-forward-char) +(define-key vip-vi-basic-map "!" 'vip-command-argument) +(define-key vip-vi-basic-map "\"" 'vip-command-argument) +(define-key vip-vi-basic-map "#" 'vip-command-argument) +(define-key vip-vi-basic-map "$" 'vip-goto-eol) +(define-key vip-vi-basic-map "%" 'vip-paren-match) +(define-key vip-vi-basic-map "&" + (function (lambda () (interactive) (vip-ex "&")))) +(define-key vip-vi-basic-map "'" 'vip-goto-mark-and-skip-white) +(define-key vip-vi-basic-map "(" 'vip-backward-sentence) +(define-key vip-vi-basic-map ")" 'vip-forward-sentence) +(define-key vip-vi-basic-map "*" 'call-last-kbd-macro) +(define-key vip-vi-basic-map "+" 'vip-next-line-at-bol) +(define-key vip-vi-basic-map "," 'vip-repeat-find-opposite) +(define-key vip-vi-basic-map "-" 'vip-previous-line-at-bol) +(define-key vip-vi-basic-map "." 'vip-repeat) +(define-key vip-vi-basic-map "/" 'vip-search-forward) + +(define-key vip-vi-basic-map "0" 'vip-beginning-of-line) +(define-key vip-vi-basic-map "1" 'vip-digit-argument) +(define-key vip-vi-basic-map "2" 'vip-digit-argument) +(define-key vip-vi-basic-map "3" 'vip-digit-argument) +(define-key vip-vi-basic-map "4" 'vip-digit-argument) +(define-key vip-vi-basic-map "5" 'vip-digit-argument) +(define-key vip-vi-basic-map "6" 'vip-digit-argument) +(define-key vip-vi-basic-map "7" 'vip-digit-argument) +(define-key vip-vi-basic-map "8" 'vip-digit-argument) +(define-key vip-vi-basic-map "9" 'vip-digit-argument) + +(define-key vip-vi-basic-map ":" 'vip-ex) +(define-key vip-vi-basic-map ";" 'vip-repeat-find) +(define-key vip-vi-basic-map "<" 'vip-command-argument) +(define-key vip-vi-basic-map "=" 'vip-command-argument) +(define-key vip-vi-basic-map ">" 'vip-command-argument) +(define-key vip-vi-basic-map "?" 'vip-search-backward) +(define-key vip-vi-basic-map "@" 'vip-register-macro) + +(define-key vip-vi-basic-map "A" 'vip-Append) +(define-key vip-vi-basic-map "B" 'vip-backward-Word) +(define-key vip-vi-basic-map "C" 'vip-change-to-eol) +(define-key vip-vi-basic-map "D" 'vip-kill-line) +(define-key vip-vi-basic-map "E" 'vip-end-of-Word) +(define-key vip-vi-basic-map "F" 'vip-find-char-backward) +(define-key vip-vi-basic-map "G" 'vip-goto-line) +(define-key vip-vi-basic-map "H" 'vip-window-top) +(define-key vip-vi-basic-map "I" 'vip-Insert) +(define-key vip-vi-basic-map "J" 'vip-join-lines) +(define-key vip-vi-basic-map "K" 'vip-nil) +(define-key vip-vi-basic-map "L" 'vip-window-bottom) +(define-key vip-vi-basic-map "M" 'vip-window-middle) +(define-key vip-vi-basic-map "N" 'vip-search-Next) +(define-key vip-vi-basic-map "O" 'vip-Open-line) +(define-key vip-vi-basic-map "P" 'vip-Put-back) +(define-key vip-vi-basic-map "Q" 'vip-query-replace) +(define-key vip-vi-basic-map "R" 'vip-overwrite) +(define-key vip-vi-basic-map "S" 'vip-substitute-line) +(define-key vip-vi-basic-map "T" 'vip-goto-char-backward) +(define-key vip-vi-basic-map "U" 'vip-undo) +(define-key vip-vi-basic-map "V" 'find-file-other-window) +(define-key vip-vi-basic-map "W" 'vip-forward-Word) +(define-key vip-vi-basic-map "X" 'vip-delete-backward-char) +(define-key vip-vi-basic-map "Y" 'vip-yank-line) +(define-key vip-vi-basic-map "ZZ" 'vip-save-kill-buffer) + +(define-key vip-vi-basic-map "\\" 'vip-escape-to-emacs) +(define-key vip-vi-basic-map "[" 'vip-brac-function) +(define-key vip-vi-basic-map "]" 'vip-ket-function) +(define-key vip-vi-basic-map "_" 'vip-alternate-ESC) +(define-key vip-vi-basic-map "^" 'vip-bol-and-skip-white) +(define-key vip-vi-basic-map "`" 'vip-goto-mark) + +(define-key vip-vi-basic-map "a" 'vip-append) +(define-key vip-vi-basic-map "b" 'vip-backward-word) +(define-key vip-vi-basic-map "c" 'vip-command-argument) +(define-key vip-vi-basic-map "d" 'vip-command-argument) +(define-key vip-vi-basic-map "e" 'vip-end-of-word) +(define-key vip-vi-basic-map "f" 'vip-find-char-forward) +(define-key vip-vi-basic-map "g" 'vip-nil) +(define-key vip-vi-basic-map "h" 'vip-backward-char) +(define-key vip-vi-basic-map "i" 'vip-insert) +(define-key vip-vi-basic-map "j" 'vip-next-line) +(define-key vip-vi-basic-map "k" 'vip-previous-line) +(define-key vip-vi-basic-map "l" 'vip-forward-char) +(define-key vip-vi-basic-map "m" 'vip-mark-point) +(define-key vip-vi-basic-map "n" 'vip-search-next) +(define-key vip-vi-basic-map "o" 'vip-open-line) +(define-key vip-vi-basic-map "p" 'vip-put-back) +(define-key vip-vi-basic-map "q" 'vip-nil) +(define-key vip-vi-basic-map "r" 'vip-replace-char) +(define-key vip-vi-basic-map "s" 'vip-substitute) +(define-key vip-vi-basic-map "t" 'vip-goto-char-forward) +(define-key vip-vi-basic-map "u" 'vip-undo) +(define-key vip-vi-basic-map "v" 'find-file) +(define-key vip-vi-basic-map "\C-v" 'vip-find-file-other-frame) +(define-key vip-vi-basic-map "w" 'vip-forward-word) +(define-key vip-vi-basic-map "x" 'vip-delete-char) +(define-key vip-vi-basic-map "y" 'vip-command-argument) +(define-key vip-vi-basic-map "zH" 'vip-line-to-top) +(define-key vip-vi-basic-map "zM" 'vip-line-to-middle) +(define-key vip-vi-basic-map "zL" 'vip-line-to-bottom) +(define-key vip-vi-basic-map "z\C-m" 'vip-line-to-top) +(define-key vip-vi-basic-map "z." 'vip-line-to-middle) +(define-key vip-vi-basic-map "z-" 'vip-line-to-bottom) + +(define-key vip-vi-basic-map "{" 'vip-backward-paragraph) +(define-key vip-vi-basic-map "|" 'vip-goto-col) +(define-key vip-vi-basic-map "}" 'vip-forward-paragraph) +(define-key vip-vi-basic-map "~" 'vip-toggle-case) +(define-key vip-vi-basic-map "\C-?" 'vip-backward-char) + +;;; Escape from Emacs to Vi for one command +(global-set-key "\M-\C-z" 'vip-escape-to-vi) ;; in emacs-state + +;;; This is vip-vi-diehard-map. Used when vip-vi-diehard-minor-mode is on. + +(define-key vip-vi-diehard-map "\C-a" 'vip-nil) +(define-key vip-vi-diehard-map "\C-c" 'vip-nil) +(define-key vip-vi-diehard-map "\C-g" 'vip-info-on-file) +(define-key vip-vi-diehard-map "\C-i" 'vip-nil) +(define-key vip-vi-diehard-map "\C-k" 'vip-nil) +(define-key vip-vi-diehard-map "\C-l" 'redraw-display) +(define-key vip-vi-diehard-map "\C-n" 'vip-next-line) +(define-key vip-vi-diehard-map "\C-o" 'vip-nil) +(define-key vip-vi-diehard-map "\C-p" 'vip-previous-line) +(define-key vip-vi-diehard-map "\C-q" 'vip-nil) +(define-key vip-vi-diehard-map "\C-r" 'redraw-display) +(define-key vip-vi-diehard-map "\C-s" 'vip-nil) +(define-key vip-vi-diehard-map "\C-t" 'vip-nil) +(define-key vip-vi-diehard-map "\C-v" 'vip-nil) +(define-key vip-vi-diehard-map "\C-w" 'vip-nil) +(define-key vip-vi-diehard-map "@" 'vip-nil) +(define-key vip-vi-diehard-map "*" 'vip-nil) +(define-key vip-vi-diehard-map "#" 'vip-nil) +(define-key vip-vi-diehard-map "\C-_" 'vip-nil) +(define-key vip-vi-diehard-map "\C-]" 'vip-nil);; This is actually tags. + + +;;; Minibuffer keymap + + +(defvar vip-minibuffer-map (make-sparse-keymap) + "Keymap used to modify keys when Minibuffer is in Insert state.") + +(define-key vip-minibuffer-map "\C-m" 'vip-exit-minibuffer) +(define-key vip-minibuffer-map "\C-j" 'vip-exit-minibuffer) + +;; Map used to read Ex-style commands. +(defvar vip-ex-cmd-map (make-sparse-keymap)) +(define-key vip-ex-cmd-map " " 'ex-cmd-read-exit) +(define-key vip-ex-cmd-map "\t" 'ex-cmd-complete) + +;; Keymap for reading file names in Ex-style commands. +(defvar ex-read-filename-map (make-sparse-keymap)) +(define-key ex-read-filename-map " " 'vip-complete-filename-or-exit) + + + + +;;; Code + +(defun vip-add-local-keys (state alist) + "Override some vi-state or insert-state bindings in the current buffer. +The effect is seen in the current buffer only. +Useful for customizing mailer buffers, gnus, etc. +STATE is 'vi-state, 'insert-state, or 'emacs-state +ALIST is of the form ((key . func) (key . func) ...) +Normally, this would be called from a hook to a major mode or +on a per buffer basis. +Usage: + (vip-add-local-keys state '((key-str . func) (key-str . func)...)) " + + (let (map) + (cond ((eq state 'vi-state) + (if vip-need-new-vi-local-map + (setq vip-vi-local-user-map (make-sparse-keymap))) + (setq vip-need-new-vi-local-map nil + map vip-vi-local-user-map)) + ((eq state 'insert-state) + (if vip-need-new-insert-local-map + (setq vip-insert-local-user-map (make-sparse-keymap))) + (setq vip-need-new-insert-local-map nil + map vip-insert-local-user-map)) + ((eq state 'emacs-state) + (if vip-need-new-emacs-local-map + (setq vip-emacs-local-user-map (make-sparse-keymap))) + (setq vip-need-new-emacs-local-map nil + map vip-emacs-local-user-map)) + (t + (error + "Invalid state in vip-add-local-keys: %S. Valid states: vi-state, insert-state or emacs-state" state))) + + (vip-modify-keymap map alist) + (vip-normalize-minor-mode-map-alist) + (vip-set-mode-vars-for vip-current-state))) + + +(defun vip-modify-major-mode (mode state keymap) + "Modify key bindings in a major-mode in a Viper state using a keymap. + +If the default for a major mode is emacs-state, then modifications to this +major mode may not take effect until the buffer switches state to Vi, +Insert or Emacs. If this happens, add vip-change-state-to-emacs to this +major mode's hook. If no such hook exists, you may have to put an advice on +the function that invokes the major mode. See vip-set-hooks for hints. + +The above needs not to be done for major modes that come up in Vi or Insert +state by default. + +Arguments: (major-mode vip-state keymap)" + (let ((alist + (cond ((eq state 'vi-state) 'vip-vi-state-modifier-alist) + ((eq state 'insert-state) 'vip-insert-state-modifier-alist) + ((eq state 'emacs-state) 'vip-emacs-state-modifier-alist))) + elt) + (if (setq elt (assoc mode (eval alist))) + (set alist (delq elt (eval alist)))) + (set alist (cons (cons mode keymap) (eval alist))) + + ;; Normalization usually doesn't help here, since one needs to + ;; normalize in the actual buffer where changes to the keymap are + ;; to take place. However, it doesn't hurt, and it helps whenever this + ;; function is actually called from within the right buffer. + (vip-normalize-minor-mode-map-alist) + + (vip-set-mode-vars-for vip-current-state))) + + +(defun vip-debug-keymaps () + "Displays variables that control Viper's keymaps." + (interactive) + (with-output-to-temp-buffer " *vip-debug*" + (princ (format "Buffer name: %s\n\n" (buffer-name))) + (princ "Variables: \n") + (princ (format "major-mode: %S\n" major-mode)) + (princ (format "vip-current-state: %S\n" vip-current-state)) + (princ (format "vip-mode-string: %S\n\n" vip-mode-string)) + (princ (format "vip-vi-intercept-minor-mode: %S\n" + vip-vi-intercept-minor-mode)) + (princ (format "vip-insert-intercept-minor-mode: %S\n" + vip-insert-intercept-minor-mode)) + (princ (format "vip-emacs-intercept-minor-mode: %S\n" + vip-emacs-intercept-minor-mode)) + (princ (format "vip-vi-minibuffer-minor-mode: %S\n" + vip-vi-minibuffer-minor-mode)) + (princ (format "vip-insert-minibuffer-minor-mode: %S\n\n" + vip-insert-minibuffer-minor-mode)) + (princ (format "vip-vi-local-user-minor-mode: %S\n" + vip-vi-local-user-minor-mode)) + (princ (format "vip-vi-global-user-minor-mode: %S\n" + vip-vi-global-user-minor-mode)) + (princ (format "vip-vi-kbd-minor-mode: %S\n" vip-vi-kbd-minor-mode)) + (princ (format "vip-vi-state-modifier-minor-mode: %S\n" + vip-vi-state-modifier-minor-mode)) + (princ (format "vip-vi-diehard-minor-mode: %S\n" + vip-vi-diehard-minor-mode)) + (princ (format "vip-vi-basic-minor-mode: %S\n" vip-vi-basic-minor-mode)) + (princ (format "vip-replace-minor-mode: %S\n" vip-replace-minor-mode)) + (princ (format "vip-insert-local-user-minor-mode: %S\n" + vip-insert-local-user-minor-mode)) + (princ (format "vip-insert-global-user-minor-mode: %S\n" + vip-insert-global-user-minor-mode)) + (princ (format "vip-insert-kbd-minor-mode: %S\n" + vip-insert-kbd-minor-mode)) + (princ (format "vip-insert-state-modifier-minor-mode: %S\n" + vip-insert-state-modifier-minor-mode)) + (princ (format "vip-insert-diehard-minor-mode: %S\n" + vip-insert-diehard-minor-mode)) + (princ (format "vip-insert-basic-minor-mode: %S\n" + vip-insert-basic-minor-mode)) + (princ (format "vip-emacs-local-user-minor-mode: %S\n" + vip-emacs-local-user-minor-mode)) + (princ (format "vip-emacs-kbd-minor-mode: %S\n" + vip-emacs-kbd-minor-mode)) + (princ (format "vip-emacs-global-user-minor-mode: %S\n" + vip-emacs-global-user-minor-mode)) + (princ (format "vip-emacs-state-modifier-minor-mode: %S\n" + vip-emacs-state-modifier-minor-mode)) + + (princ (format "\nvip-expert-level %S\n" vip-expert-level)) + (princ (format "vip-no-multiple-ESC %S\n" vip-no-multiple-ESC)) + (princ (format "vip-always %S\n" vip-always)) + (princ (format "vip-ex-style-motion %S\n" + vip-ex-style-motion)) + (princ (format "vip-ex-style-editing-in-insert %S\n" + vip-ex-style-editing-in-insert)) + (princ (format "vip-want-emacs-keys-in-vi %S\n" + vip-want-emacs-keys-in-vi)) + (princ (format "vip-want-emacs-keys-in-insert %S\n" + vip-want-emacs-keys-in-insert)) + (princ (format "vip-want-ctl-h-help %S\n" vip-want-ctl-h-help)) + + (princ "\n\n\n") + (princ (format "Default value for minor-mode-map-alist: \n%S\n\n" + (default-value 'minor-mode-map-alist))) + (princ (format "Actual value for minor-mode-map-alist: \n%S\n" + minor-mode-map-alist)) + )) + + +;;; Keymap utils + +(defun vip-add-keymap (mapsrc mapdst) + "Add contents of mapsrc to mapdst. It is assumed that mapsrc is sparse." + (if vip-xemacs-p + (map-keymap (function (lambda (key binding) + (define-key mapdst key binding))) + mapsrc) + (mapcar + (function (lambda (p) + (define-key mapdst (vector (car p)) (cdr p)) + )) + (cdr mapsrc)))) + +(defun vip-modify-keymap (map alist) + "Modifies MAP with bindings specified in the ALIST. The alist has the +form ((key . function) (key . function) ... )." + (mapcar (function (lambda (p) + (define-key map (eval (car p)) (cdr p)))) + alist)) + + +(provide 'viper-keym) + +;;; viper-keym.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/emulation/viper-macs.el Mon Feb 20 23:15:10 1995 +0000 @@ -0,0 +1,902 @@ +;;; viper-macs.el -- functions implementing keyboard macros for Viper + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 2, or (at your option) +;; any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + + +(require 'viper-util) + +;;; Variables + +;; Register holding last macro. +(defvar vip-last-macro-reg nil) + +;; format of the elements of kbd alists: +;; (name ((buf . macr)...(buf . macr)) ((maj-mode . macr)...) (t . macr)) +;; kbd macro alist for Vi state +(defvar vip-vi-kbd-macro-alist nil) +;; same for insert/replace state +(defvar vip-insert-kbd-macro-alist nil) +;; same for emacs state +(defvar vip-emacs-kbd-macro-alist nil) + +;; Internal var that passes info between start-kbd-macro and end-kbd-macro +;; in :map and :map! +(defvar vip-kbd-macro-parameters nil) + +(defvar vip-this-kbd-macro nil + "Vector of keys representing the name of currently running Viper kbd macro.") +(defvar vip-last-kbd-macro nil + "Vector of keys representing the name of last Viper keyboard macro.") + +(defconst vip-fast-keyseq-timeout 200 + "*Key sequences separated by this many miliseconds are interpreted as a macro, if such a macro is defined. +This also controls ESC-keysequences generated by keyboard function keys.") + + +(defvar vip-repeat-from-history-key 'f1 + "Prefix key for invocation of vip-repeat-from-history function, +which repeats previous destructive commands from the history of such +commands. +This function can then be invoked as <this-key> 1 or <this-key> 2. +The notation for these keys is borrowed from XEmacs. Basically, +a key is a symbol, e.g., `a', `\\1', `f2', etc., or a list, e.g., +`(meta control f1)'.") + + + +;;; Code + +(defun ex-map () + "Ex map command." + (let ((mod-char "") + macro-name macro-body map-args ins) + (save-window-excursion + (set-buffer vip-ex-work-buf) + (if (looking-at "!") + (progn + (setq ins t + mod-char "!") + (forward-char 1)))) + (setq map-args (ex-map-read-args mod-char) + macro-name (car map-args) + macro-body (cdr map-args)) + (setq vip-kbd-macro-parameters (list ins mod-char macro-name macro-body)) + (if macro-body + (vip-end-mapping-kbd-macro 'ignore) + (ex-fixup-history (format "map%s %S" mod-char + (vip-display-macro macro-name))) + ;; if defining macro for insert, switch there for authentic WYSIWYG + (if ins (vip-change-state-to-insert)) + (start-kbd-macro nil) + (define-key vip-vi-intercept-map "\C-x)" 'vip-end-mapping-kbd-macro) + (define-key vip-insert-intercept-map "\C-x)" 'vip-end-mapping-kbd-macro) + (define-key vip-emacs-intercept-map "\C-x)" 'vip-end-mapping-kbd-macro) + (message "Mapping %S in %s state. Hit `C-x )' to complete the mapping" + (vip-display-macro macro-name) + (if ins "Insert" "Vi"))) + )) + + +(defun ex-unmap () + "Ex unmap." + (let ((mod-char "") + temp macro-name ins) + (save-window-excursion + (set-buffer vip-ex-work-buf) + (if (looking-at "!") + (progn + (setq ins t + mod-char "!") + (forward-char 1)))) + + (setq macro-name (ex-unmap-read-args mod-char)) + (setq temp (vip-fixup-macro (vconcat macro-name))) ;; copy and fixup + (ex-fixup-history (format "unmap%s %S" mod-char + (vip-display-macro temp))) + (vip-unrecord-kbd-macro macro-name (if ins 'insert-state 'vi-state)) + )) + + +;; read arguments for ex-map +(defun ex-map-read-args (variant) + (let ((cursor-in-echo-area t) + (key-seq []) + temp key event message + macro-name macro-body args) + + (condition-case nil + (setq args (concat (ex-get-inline-cmd-args ".*map[!]*[ \t]?" "\n\C-m") + " nil nil ") + temp (read-from-string args) + macro-name (car temp) + macro-body (car (read-from-string args (cdr temp)))) + (error + (signal + 'error + '("map: Macro name and body must be a quoted string or a vector")))) + + ;; We expect macro-name to be a vector, a string, or a quoted string. + ;; In the second case, it will emerge as a symbol when read from + ;; the above read-from-string. So we need to convert it into a string + (if macro-name + (cond ((vectorp macro-name) nil) + ((stringp macro-name) + (setq macro-name (vconcat macro-name))) + (t (setq macro-name (vconcat (prin1-to-string macro-name))))) + (message ":map%s <Name>" variant)(sit-for 2) + (while + (not (member key + '(?\C-m ?\n (control m) (control j) return linefeed))) + (setq key-seq (vconcat key-seq (if key (vector key) []))) + ;; the only keys available for editing are these-- no help while there + (if (member + key + '(?\b ?\d '^? '^H (control h) (control \?) backspace delete)) + (setq key-seq (subseq key-seq 0 (- (length key-seq) 2)))) + (setq message + (format ":map%s %s" + variant (if (> (length key-seq) 0) + (prin1-to-string (vip-display-macro key-seq)) + ""))) + (message message) + (setq event (vip-read-event)) + (setq key + (if (vip-mouse-event-p event) + (progn + (message "%s (No mouse---only keyboard keys, please)" + message) + (sit-for 2) + nil) + (vip-event-key event))) + ) + (setq macro-name key-seq)) + + (if (= (length macro-name) 0) + (error "Can't map an empty macro name")) + (setq macro-name (vip-fixup-macro macro-name)) + (if (vip-char-array-p macro-name) + (setq macro-name (vip-char-array-to-macro macro-name))) + + (if macro-body + (cond ((vip-char-array-p macro-body) + (setq macro-body (vip-char-array-to-macro macro-body))) + ((vectorp macro-body) nil) + (t (error "map: Invalid syntax in macro definition")))) + (cons macro-name macro-body) + )) + + + +;; read arguments for ex-unmap +(defun ex-unmap-read-args (variant) + (let ((cursor-in-echo-area t) + (macro-alist (if (string= variant "!") + vip-insert-kbd-macro-alist + vip-vi-kbd-macro-alist)) + ;; these are disabled just in case, to avoid surprises when doing + ;; completing-read + vip-vi-kbd-minor-mode vip-insert-kbd-minor-mode + vip-emacs-kbd-minor-mode + vip-vi-intercept-minor-mode vip-insert-intercept-minor-mode + vip-emacs-intercept-minor-mode + event message + key key-seq macro-name) + (setq macro-name (ex-get-inline-cmd-args ".*unma?p?[!]*[ \t]*")) + + (if (> (length macro-name) 0) + () + (message ":unmap%s <Name>" variant) (sit-for 2) + (while + (not + (member key '(?\C-m ?\n (control m) (control j) return linefeed))) + (setq key-seq (vconcat key-seq (if key (vector key) []))) + ;; the only keys available for editing are these-- no help while there + (cond ((member + key + '(?\b ?\d '^? '^H (control h) (control \?) backspace delete)) + (setq key-seq (subseq key-seq 0 (- (length key-seq) 2)))) + ((member key '(tab (control i) ?\t)) + (setq key-seq (subseq key-seq 0 (1- (length key-seq)))) + (setq message + (format ":unmap%s %s" + variant (if (> (length key-seq) 0) + (prin1-to-string + (vip-display-macro key-seq)) + ""))) + (setq key-seq + (vip-do-sequence-completion key-seq macro-alist message)) + )) + (setq message + (format ":unmap%s %s" + variant (if (> (length key-seq) 0) + (prin1-to-string + (vip-display-macro key-seq)) + ""))) + (message message) + (setq event (vip-read-event)) + (setq key + (if (vip-mouse-event-p event) + (progn + (message "%s (No mouse---only keyboard keys, please)" + message) + (sit-for 2) + nil) + (vip-event-key event))) + ) + (setq macro-name key-seq)) + + (if (= (length macro-name) 0) + (error "Can't unmap an empty macro name")) + + ;; convert macro names into vector, if starts with a `[' + (if (memq (elt macro-name 0) '(?\[ ?\")) + (car (read-from-string macro-name)) + (vconcat macro-name)) + )) + + + +(defun vip-end-mapping-kbd-macro (&optional ignore) + "Terminate kbd macro." + (interactive) + (define-key vip-vi-intercept-map "\C-x)" nil) + (define-key vip-insert-intercept-map "\C-x)" nil) + (define-key vip-emacs-intercept-map "\C-x)" nil) + (if (and (not ignore) + (or (not vip-kbd-macro-parameters) + (not defining-kbd-macro))) + (error "Not mapping a kbd-macro")) + (let ((mod-char (nth 1 vip-kbd-macro-parameters)) + (ins (nth 0 vip-kbd-macro-parameters)) + (macro-name (nth 2 vip-kbd-macro-parameters)) + (macro-body (nth 3 vip-kbd-macro-parameters))) + (setq vip-kbd-macro-parameters nil) + (or ignore + (progn + (end-kbd-macro nil) + (setq macro-body (vip-events-to-macro last-kbd-macro)) + ;; always go back to Vi, since this is where we started + ;; defining macro + (vip-change-state-to-vi))) + + (vip-record-kbd-macro macro-name + (if ins 'insert-state 'vi-state) + (vip-display-macro macro-body)) + + (ex-fixup-history (format "map%s %S %S" mod-char + (vip-display-macro macro-name) + (vip-display-macro macro-body))) + )) + + + +(defadvice start-kbd-macro (after vip-kbd-advice activate) + "Remove Viper's intercepting bindings for C-x ). +This may be needed if the previous `:map' command terminated abnormally." + (define-key vip-vi-intercept-map "\C-x)" nil) + (define-key vip-insert-intercept-map "\C-x)" nil) + (define-key vip-emacs-intercept-map "\C-x)" nil)) + + + +;;; Recording, unrecording, executing + +;; accepts as macro names: strings and vectors. +;; strings must be strings of characters; vectors must be vectors of keys +;; in canonic form. the canonic form is essentially the form used in XEmacs +(defun vip-record-kbd-macro (macro-name state macro-body &optional scope) + "Record a Vi macro. Can be used in `.vip' file to define permanent macros. +MACRO-NAME is a string of characters or a vector of keys. STATE is +either `vi-state' or `insert-state'. It specifies the Viper state in which to +define the macro. MACRO-BODY is a string that represents the keyboard macro. +Optional SCOPE says whether the macro should be global \(t\), mode-specific +\(a major-mode symbol\), or buffer-specific \(buffer name, a string\). +If SCOPE is nil, the user is asked to specify the scope." + (let* (state-name keymap + (macro-alist-var + (cond ((eq state 'vi-state) + (setq state-name "Vi state" + keymap vip-vi-kbd-map) + 'vip-vi-kbd-macro-alist) + ((memq state '(insert-state replace-state)) + (setq state-name "Insert state" + keymap vip-insert-kbd-map) + 'vip-insert-kbd-macro-alist) + (t + (setq state-name "Emacs state" + keymap vip-emacs-kbd-map) + 'vip-emacs-kbd-macro-alist) + )) + new-elt old-elt old-sub-elt msg + temp lis lis2) + + (if (= (length macro-name) 0) + (error "Can't map an empty macro name")) + + ;; Macro-name is usually a vector. However, command history or macros + ;; recorded in ~/.vip may be recorded as strings. So, convert to vectors. + (setq macro-name (vip-fixup-macro macro-name)) + (if (vip-char-array-p macro-name) + (setq macro-name (vip-char-array-to-macro macro-name))) + (setq macro-body (vip-fixup-macro macro-body)) + (if (vip-char-array-p macro-body) + (setq macro-body (vip-char-array-to-macro macro-body))) + + ;; don't ask if scope is given and is of the right type + (or (eq scope t) + (stringp scope) + (and scope (symbolp scope)) + (progn + (setq scope + (cond + ((y-or-n-p + (format + "Map this macro for buffer `%s' only? " + (buffer-name))) + (setq msg + (format + "%S is mapped to %s for %s in `%s'" + (vip-display-macro macro-name) + (vip-abbreviate-string + (format + "%S" + (setq temp (vip-display-macro macro-body))) + 14 "" "" + (if (stringp temp) " ....\"" " ....]")) + state-name (buffer-name))) + (buffer-name)) + ((y-or-n-p + (format + "Map this macro for the major mode `%S' only? " + major-mode)) + (setq msg + (format + "%S is mapped to %s for %s in `%S'" + (vip-display-macro macro-name) + (vip-abbreviate-string + (format + "%S" + (setq temp (vip-display-macro macro-body))) + 14 "" "" + (if (stringp macro-body) " ....\"" " ....]")) + state-name major-mode)) + major-mode) + (t + (setq msg + (format + "%S is globally mapped to %s in %s" + (vip-display-macro macro-name) + (vip-abbreviate-string + (format + "%S" + (setq temp (vip-display-macro macro-body))) + 14 "" "" + (if (stringp macro-body) " ....\"" " ....]")) + state-name)) + t))) + (if (y-or-n-p (format "Save this macro in %s? " + (abbreviate-file-name vip-custom-file-name))) + (vip-save-string-in-file + (format "\n(vip-record-kbd-macro %S '%S %s '%S)" + (vip-display-macro macro-name) + state macro-body scope) + vip-custom-file-name)) + + (message msg) + )) + + (setq new-elt + (cons macro-name + (cond ((eq scope t) (list nil nil (cons t nil))) + ((symbolp scope) + (list nil (list (cons scope nil)) (cons t nil))) + ((stringp scope) + (list (list (cons scope nil)) nil (cons t nil)))))) + (setq old-elt (assoc macro-name (eval macro-alist-var))) + + (if (null old-elt) + (progn + ;; insert new-elt in macro-alist-var and keep the list sorted + (define-key + keymap + (vector (vip-key-to-emacs-key (aref macro-name 0))) + 'vip-exec-mapped-kbd-macro) + (setq lis (eval macro-alist-var)) + (while (and lis (string< (vip-array-to-string (car (car lis))) + (vip-array-to-string macro-name))) + (setq lis2 (cons (car lis) lis2)) + (setq lis (cdr lis))) + + (setq lis2 (reverse lis2)) + (set macro-alist-var (append lis2 (cons new-elt lis))) + (setq old-elt new-elt))) + (setq old-sub-elt + (cond ((eq scope t) (vip-kbd-global-pair old-elt)) + ((symbolp scope) (assoc scope (vip-kbd-mode-alist old-elt))) + ((stringp scope) (assoc scope (vip-kbd-buf-alist old-elt))))) + (if old-sub-elt + (setcdr old-sub-elt macro-body) + (cond ((symbolp scope) (setcar (cdr (cdr old-elt)) + (cons (cons scope macro-body) + (vip-kbd-mode-alist old-elt)))) + ((stringp scope) (setcar (cdr old-elt) + (cons (cons scope macro-body) + (vip-kbd-buf-alist old-elt)))))) + )) + + + +;; macro name must be a vector of vip-style keys +(defun vip-unrecord-kbd-macro (macro-name state) + (let* (state-name keymap + (macro-alist-var + (cond ((eq state 'vi-state) + (setq state-name "Vi state" + keymap vip-vi-kbd-map) + 'vip-vi-kbd-macro-alist) + ((memq state '(insert-state replace-state)) + (setq state-name "Insert state" + keymap vip-insert-kbd-map) + 'vip-insert-kbd-macro-alist) + (t + (setq state-name "Emacs state" + keymap vip-emacs-kbd-map) + 'vip-emacs-kbd-macro-alist) + )) + buf-mapping mode-mapping global-mapping + macro-pair macro-entry) + + ;; Macro-name is usually a vector. However, command history or macros + ;; recorded in ~/.vip may appear as strings. So, convert to vectors. + (setq macro-name (vip-fixup-macro macro-name)) + (if (vip-char-array-p macro-name) + (setq macro-name (vip-char-array-to-macro macro-name))) + + (setq macro-entry (assoc macro-name (eval macro-alist-var))) + (if (= (length macro-name) 0) + (error "Can't unmap an empty macro name")) + (if (null macro-entry) + (error "%S is not mapped to a macro for %s in `%s'" + (vip-display-macro macro-name) + state-name (buffer-name))) + + (setq buf-mapping (vip-kbd-buf-pair macro-entry) + mode-mapping (vip-kbd-mode-pair macro-entry) + global-mapping (vip-kbd-global-pair macro-entry)) + + (cond ((and (cdr buf-mapping) + (or (and (not (cdr mode-mapping)) (not (cdr global-mapping))) + (y-or-n-p + (format "Unmap %S for `%s' only? " + (vip-display-macro macro-name) + (buffer-name))))) + (setq macro-pair buf-mapping) + (message "%S is unmapped for %s in `%s'" + (vip-display-macro macro-name) + state-name (buffer-name))) + ((and (cdr mode-mapping) + (or (not (cdr global-mapping)) + (y-or-n-p + (format "Unmap %S for the major mode `%S' only? " + (vip-display-macro macro-name) + major-mode)))) + (setq macro-pair mode-mapping) + (message "%S is unmapped for %s in %S" + (vip-display-macro macro-name) state-name major-mode)) + ((cdr (setq macro-pair (vip-kbd-global-pair macro-entry))) + (message + "Global mapping of %S for %s is removed" + (vip-display-macro macro-name) state-name)) + (t (error "%S is not mapped to a macro for %s in `%s'" + (vip-display-macro macro-name) + state-name (buffer-name)))) + (setcdr macro-pair nil) + (or (cdr buf-mapping) + (cdr mode-mapping) + (cdr global-mapping) + (progn + (set macro-alist-var (delq macro-entry (eval macro-alist-var))) + (if (vip-can-release-key (aref macro-name 0) + (eval macro-alist-var)) + (define-key + keymap + (vector (vip-key-to-emacs-key (aref macro-name 0))) + nil)) + )) + )) + +;; Checks if MACRO-ALIST has an entry for a macro name starting with +;; CHAR. If not, this indicates that the binding for this char +;; in vip-vi/insert-kbd-map can be released. +(defun vip-can-release-key (char macro-alist) + (let ((lis macro-alist) + (can-release t) + macro-name) + + (while (and lis can-release) + (setq macro-name (car (car lis))) + (if (eq char (aref macro-name 0)) + (setq can-release nil)) + (setq lis (cdr lis))) + can-release)) + + +(defun vip-exec-mapped-kbd-macro (count) + "Dispatch kbd macro." + (interactive "P") + (let* ((macro-alist (cond ((eq vip-current-state 'vi-state) + vip-vi-kbd-macro-alist) + ((memq vip-current-state + '(insert-state replace-state)) + vip-insert-kbd-macro-alist) + (t + vip-emacs-kbd-macro-alist))) + (unmatched-suffix "") + ;; Macros and keys are executed with other macros turned off + ;; For macros, this is done to avoid macro recursion + vip-vi-kbd-minor-mode vip-insert-kbd-minor-mode + vip-emacs-kbd-minor-mode + next-best-match keyseq event-seq + macro-first-char macro-alist-elt macro-body + command) + + (setq macro-first-char last-command-event + event-seq (vip-read-fast-keysequence macro-first-char macro-alist) + keyseq (vip-events-to-macro event-seq) + macro-alist-elt (assoc keyseq macro-alist) + next-best-match (vip-find-best-matching-macro macro-alist keyseq)) + + (if (null macro-alist-elt) + (setq macro-alist-elt (car next-best-match) + unmatched-suffix (subseq event-seq (cdr next-best-match)))) + + (cond ((null macro-alist-elt)) + ((setq macro-body (vip-kbd-buf-definition macro-alist-elt))) + ((setq macro-body (vip-kbd-mode-definition macro-alist-elt))) + ((setq macro-body (vip-kbd-global-definition macro-alist-elt)))) + + ;; when defining keyboard macro, don't use the macro mappings + (if (and macro-body (not defining-kbd-macro)) + ;; block cmd executed as part of a macro from entering command history + (let ((command-history command-history)) + (setq vip-this-kbd-macro (car macro-alist-elt)) + (execute-kbd-macro (vip-macro-to-events macro-body) count) + (setq vip-this-kbd-macro nil + vip-last-kbd-macro (car macro-alist-elt)) + (vip-set-unread-command-events unmatched-suffix)) + ;; If not a macro, or the macro is suppressed while defining another + ;; macro, put keyseq back on the event queue + (vip-set-unread-command-events event-seq) + ;; if the user typed arg, then use it if prefix arg is not set by + ;; some other command (setting prefix arg can happen if we do, say, + ;; 2dw and there is a macro starting with 2. Then control will go to + ;; this routine + (or prefix-arg (setq prefix-arg count)) + (setq command (key-binding (read-key-sequence nil))) + (if (commandp command) + (command-execute command) + (beep 1))) + )) + + + +;;; Displaying and completing macros + +(defun vip-describe-kbd-macros () + "Show currently defined keyboard macros." + (interactive) + (with-output-to-temp-buffer " *vip-info*" + (princ "Macros in Vi state:\n===================\n") + (mapcar 'vip-describe-one-macro vip-vi-kbd-macro-alist) + (princ "\n\nMacros in Insert and Replace states:\n====================================\n") + (mapcar 'vip-describe-one-macro vip-insert-kbd-macro-alist) + (princ "\n\nMacros in Emacs state:\n======================\n") + (mapcar 'vip-describe-one-macro vip-emacs-kbd-macro-alist) + )) + +(defun vip-describe-one-macro (macro) + (princ (format "\n *** Mappings for %S:\n ------------\n" + (vip-display-macro (car macro)))) + (princ " ** Buffer-specific:") + (if (vip-kbd-buf-alist macro) + (mapcar 'vip-describe-one-macro-elt (vip-kbd-buf-alist macro)) + (princ " none\n")) + (princ "\n ** Mode-specific:") + (if (vip-kbd-mode-alist macro) + (mapcar 'vip-describe-one-macro-elt (vip-kbd-mode-alist macro)) + (princ " none\n")) + (princ "\n ** Global:") + (if (vip-kbd-global-definition macro) + (princ + (format "\n %S" + (cdr (vip-kbd-global-pair macro)))) + (princ " none")) + (princ "\n")) + +(defun vip-describe-one-macro-elt (elt) + (let ((name (car elt)) + (defn (cdr elt))) + (princ (format "\n * %S:\n %S\n" name defn)))) + + + +;; check if SEQ is a prefix of some car of an element in ALIST +(defun vip-keyseq-is-a-possible-macro (seq alist) + (let ((converted-seq (vip-events-to-macro seq))) + (eval (cons 'or + (mapcar + (function (lambda (elt) + (vip-prefix-subseq-p converted-seq elt))) + (vip-this-buffer-macros alist)))))) + +;; whether SEQ1 is a prefix of SEQ2 +(defun vip-prefix-subseq-p (seq1 seq2) + (let ((len1 (length seq1)) + (len2 (length seq2))) + (if (<= len1 len2) + (equal seq1 (subseq seq2 0 len1))))) + +;; find the longest common prefix +(defun vip-common-seq-prefix (&rest seqs) + (let* ((first (car seqs)) + (rest (cdr seqs)) + (pref []) + (idx 0) + len) + (if (= (length seqs) 0) + (setq len 0) + (setq len (apply 'min (mapcar 'length seqs)))) + (while (< idx len) + (if (eval (cons 'and + (mapcar (function (lambda (s) + (equal (elt first idx) + (elt s idx)))) + rest))) + (setq pref (vconcat pref (vector (elt first idx))))) + (setq idx (1+ idx))) + pref)) + +;; get all sequences that match PREFIX from a given A-LIST +(defun vip-extract-matching-alist-members (pref alist) + (delq nil (mapcar (function (lambda (elt) + (if (vip-prefix-subseq-p pref elt) + elt))) + (vip-this-buffer-macros alist)))) + +(defun vip-do-sequence-completion (seq alist compl-message) + (let* ((matches (vip-extract-matching-alist-members seq alist)) + (new-seq (apply 'vip-common-seq-prefix matches)) + ) + (cond ((and (equal seq new-seq) (= (length matches) 1)) + (message "%s (Sole completion)" compl-message) + (sit-for 2)) + ((null matches) + (message "%s (No match)" compl-message) + (sit-for 2) + (setq new-seq seq)) + ((member seq matches) + (message "%s (Complete, but not unique)" compl-message) + (sit-for 2) + (vip-display-vector-completions matches)) + ((equal seq new-seq) + (vip-display-vector-completions matches))) + new-seq)) + + +(defun vip-display-vector-completions (list) + (with-output-to-temp-buffer "*Completions*" + (display-completion-list + (mapcar 'prin1-to-string + (mapcar 'vip-display-macro list))))) + + + +;; alist is the alist of macros +;; str is the fast key sequence entered +;; returns: (matching-macro-def . unmatched-suffix-start-index) +(defun vip-find-best-matching-macro (alist str) + (let ((lis alist) + (def-len 0) + (str-len (length str)) + match unmatched-start-idx found macro-def) + (while (and (not found) lis) + (setq macro-def (car lis) + def-len (length (car macro-def))) + (if (and (>= str-len def-len) + (equal (car macro-def) (subseq str 0 def-len))) + (if (or (vip-kbd-buf-definition macro-def) + (vip-kbd-mode-definition macro-def) + (vip-kbd-global-definition macro-def)) + (setq found t)) + ) + (setq lis (cdr lis))) + + (if found + (setq match macro-def + unmatched-start-idx def-len) + (setq match nil + unmatched-start-idx 0)) + + (cons match unmatched-start-idx))) + + + +;; returns a list of names of macros defined for the current buffer +(defun vip-this-buffer-macros (macro-alist) + (let (candidates) + (setq candidates + (mapcar (function + (lambda (elt) + (if (or (vip-kbd-buf-definition elt) + (vip-kbd-mode-definition elt) + (vip-kbd-global-definition elt)) + (car elt)))) + macro-alist)) + (setq candidates (delq nil candidates)))) + + +;; if seq of key symbols can be converted to a string--do so. Otherwise, do +;; nothing. +(defun vip-display-macro (macro-name) + (cond ((vip-char-symbol-sequence-p macro-name) + (mapconcat 'symbol-name macro-name "")) + ((vip-char-array-p macro-name) + (mapconcat 'char-to-string macro-name "")) + (t macro-name))) + +(defun vip-events-to-macro (event-seq) + (vconcat (mapcar 'vip-event-key event-seq))) + +;; convert strings of characters or arrays of characters to Viper macro form +(defun vip-char-array-to-macro (array) + (let ((vec (vconcat array)) + macro) + (if vip-xemacs-p + (setq macro (mapcar 'character-to-event vec)) + (setq macro vec)) + (vconcat (mapcar 'vip-event-key macro)))) + +;; For macros bodies and names, goes over and checks if all members are +;; names of keys (actually, it only checks if they are symbols or lists +;; if a digit is found, it is converted into a symbol (0 -> \0, etc). +;; If not list or vector, doesn't change its argument +(defun vip-fixup-macro (macro) + (let ((len (length macro)) + (idx 0) + elt break) + (if (or (vectorp macro) (listp macro)) + (while (and (< idx len) (not break)) + (setq elt (elt macro idx)) + (cond ((numberp elt) + ;; fix number + (if (and (<= 0 elt) (<= elt 9)) + (cond ((arrayp macro) + (aset macro + idx + (intern (char-to-string (+ ?0 elt))))) + ((listp macro) + (setcar (nthcdr idx macro) + (intern (char-to-string (+ ?0 elt))))) + ))) + ;;(setq break t))) + ((listp elt) + (vip-fixup-macro elt)) + ((symbolp elt) nil) + (t (setq break t))) + (setq idx (1+ idx)))) + + (if break + (error "Wrong type macro component, symbol-or-listp, %S" elt) + macro))) + +(defun vip-char-array-p (array) + (eval (cons 'and (mapcar 'numberp array)))) + +(defun vip-macro-to-events (macro-body) + (vconcat (mapcar 'vip-key-to-emacs-key macro-body))) + + +;; check if vec is a vector of character symbols +(defun vip-char-symbol-sequence-p (vec) + (and + (sequencep vec) + (eval + (cons 'and + (mapcar + (function (lambda (elt) + (and (symbolp elt) (= (length (symbol-name elt)) 1)))) + vec))))) + + +;; Check if vec is a vector of key-press events representing characters +;; XEmacs only +(defun vip-event-vector-p (vec) + (and (vectorp vec) + (eval (cons 'and (mapcar '(lambda (elt) (if (eventp elt) t)) vec))))) + + +;;; Reading fast key sequences + +;; Assuming that CHAR was the first character in a fast succession of key +;; strokes, read the rest. Return the vector of keys that was entered in +;; this fast succession of key strokes. +;; A fast keysequence is one that is terminated by a pause longer than +;; vip-fast-keyseq-timeout. +(defun vip-read-fast-keysequence (event macro-alist) + (let ((lis (vector event)) + next-event) + (while (and (vip-fast-keysequence-p) + (vip-keyseq-is-a-possible-macro lis macro-alist)) + (setq next-event (vip-read-event)) + (or (vip-mouse-event-p next-event) + (setq lis (vconcat lis (vector next-event))))) + lis)) + + +;;; Keyboard macros in registers + +;; sets register to last-kbd-macro carefully. +(defun vip-set-register-macro (reg) + (if (get-register reg) + (if (y-or-n-p "Register contains data. Overwrite? ") + () + (error + "Macro not saved in register. Can still be invoked via `C-x e'"))) + (set-register reg last-kbd-macro)) + +(defun vip-register-macro (count) + "Keyboard macros in registers - a modified \@ command." + (interactive "P") + (let ((reg (downcase (read-char)))) + (cond ((or (and (<= ?a reg) (<= reg ?z))) + (setq vip-last-macro-reg reg) + (if defining-kbd-macro + (progn + (end-kbd-macro) + (vip-set-register-macro reg)) + (execute-kbd-macro (get-register reg) count))) + ((or (= ?@ reg) (= ?\^j reg) (= ?\^m reg)) + (if vip-last-macro-reg + nil + (error "No previous kbd macro")) + (execute-kbd-macro (get-register vip-last-macro-reg) count)) + ((= ?\# reg) + (start-kbd-macro count)) + ((= ?! reg) + (setq reg (downcase (read-char))) + (if (or (and (<= ?a reg) (<= reg ?z))) + (progn + (setq vip-last-macro-reg reg) + (vip-set-register-macro reg)))) + (t + (error (format "`%c': Unknown register" reg)))))) + + +(defun vip-global-execute () + "Call last keyboad macro for each line in the region." + (if (> (point) (mark t)) (exchange-point-and-mark)) + (beginning-of-line) + (call-last-kbd-macro) + (while (< (point) (mark t)) + (forward-line 1) + (beginning-of-line) + (call-last-kbd-macro))) + + +(provide 'viper-macs) + +;;; viper-macs.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/emulation/viper-mous.el Mon Feb 20 23:15:10 1995 +0000 @@ -0,0 +1,457 @@ +;;; viper-mous.el -- Mouse support for Viper + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 2, or (at your option) +;; any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + + +(require 'viper-util) + + +;;; Variables + +;; Variable used for catching the switch-frame event. +;; If non-nil, indicates that previous-frame should be the selected +;; one. Used by vip-mouse-click-get-word. Not a user option. +(defvar vip-frame-of-focus nil) + +;; Frame that was selected before the switch-frame event. +(defconst vip-pre-click-frame (vip-selected-frame)) + +(defvar vip-surrounding-word-function 'vip-surrounding-word + "*Function that determines what constitutes a word for clicking events. +Takes two parameters: a COUNT, indicating how many words to return, +and CLICK-COUNT, telling whether this is the first click, a double-click, +or a tripple-click.") + +;; time interval in millisecond within which successive clicks are +;; considered related +(defconst vip-multiclick-timeout (if vip-xemacs-p + 500 + double-click-time) + "*Time interval in millisecond within which successive clicks are +considered related.") + +;; current event click count; XEmacs only +(defvar vip-current-click-count 0) +;; time stamp of the last click event; XEmacs only +(defvar vip-last-click-event-timestamp 0) + +;; Local variable used to toggle wraparound search on click. +(vip-deflocalvar vip-mouse-click-search-noerror t) + +;; Local variable used to delimit search after wraparound. +(vip-deflocalvar vip-mouse-click-search-limit nil) + +;; remembers prefix argument to pass along to commands invoked by second +;; click. +;; This is needed because in Emacs (not XEmacs), assigning to preix-arg +;; causes Emacs to count the second click as if it was a single click +(defvar vip-global-prefix-argument nil) + + + +;;; Code + +(defun vip-multiclick-p () + (not (vip-sit-for-short vip-multiclick-timeout t))) + +(defun vip-surrounding-word (count click-count) + "Returns word surrounding point according to a heuristic. +COUNT indicates how many regions to return. +If CLICK-COUNT is 1, `word' is a word in Vi sense. If it is > 1, +then `word' is a Word in Vi sense. +If the character clicked on is a non-separator and is non-alphanumeric but +is adjacent to an alphanumeric symbol, then it is considered alphanumeric +for the purpose of this command. If this character has a matching +character, such as `\(' is a match for `\)', then the matching character is +also considered alphanumeric. +For convenience, in Lisp modes, `-' is considered alphanumeric." + (let* ((basic-alpha "_a-zA-Z0-9") ;; it is important for `_' to come first + (basic-alpha-B "[_a-zA-Z0-9]") + (basic-nonalphasep-B vip-NONALPHASEP-B) + (end-modifiers "") + (start-modifiers "") + vip-ALPHA vip-ALPHA-B + vip-NONALPHA vip-NONALPHA-B + vip-ALPHASEP vip-ALPHASEP-B + vip-NONALPHASEP vip-NONALPHASEP-B + skip-flag + one-char-word-func word-function-forw word-function-back word-beg) + + (if (and (looking-at basic-nonalphasep-B) + (or (save-excursion (vip-backward-char-carefully) + (looking-at basic-alpha-B)) + (save-excursion (vip-forward-char-carefully) + (looking-at basic-alpha-B)))) + (setq start-modifiers + (cond ((looking-at "\\\\") "\\\\") + ((looking-at "-") "") + ((looking-at "[][]") "][") + ((looking-at "[()]") ")(") + ((looking-at "[{}]") "{}") + ((looking-at "[<>]") "<>") + ((looking-at "[`']") "`'") + ((looking-at "\\^") "") + ((looking-at vip-SEP-B) "") + (t (char-to-string (following-char)))) + end-modifiers + (cond ((looking-at "-") "C-C-") ;; note the C-C trick + ((looking-at "\\^") "^") + (t "")))) + + ;; Add `-' to alphanum, if it wasn't added and in we are in Lisp + (or (looking-at "-") + (not (string-match "lisp" (symbol-name major-mode))) + (setq end-modifiers (concat end-modifiers "C-C-"))) + + (setq vip-ALPHA + (format "%s%s%s" start-modifiers basic-alpha end-modifiers) + vip-ALPHA-B + (format "[%s%s%s]" start-modifiers basic-alpha end-modifiers) + vip-NONALPHA (concat "^" vip-ALPHA) + vip-NONALPHA-B (concat "[" vip-NONALPHA "]") + vip-ALPHASEP (concat vip-ALPHA vip-SEP) + vip-ALPHASEP-B + (format "[%s%s%s%s]" + start-modifiers basic-alpha vip-SEP end-modifiers) + vip-NONALPHASEP (format "^%s%s" vip-SEP vip-ALPHA) + vip-NONALPHASEP-B (format "[^%s%s]" vip-SEP vip-ALPHA) + ) + + (if (> click-count 1) + (setq one-char-word-func 'vip-one-char-Word-p + word-function-forw 'vip-end-of-Word + word-function-back 'vip-backward-Word) + (setq one-char-word-func 'vip-one-char-word-p + word-function-forw 'vip-end-of-word + word-function-back 'vip-backward-word)) + + (save-excursion + (cond ((> click-count 1) (skip-chars-backward vip-NONSEP)) + ((looking-at vip-ALPHA-B) (skip-chars-backward vip-ALPHA)) + ((looking-at vip-NONALPHASEP-B) + (skip-chars-backward vip-NONALPHASEP)) + (t (funcall word-function-back 1))) + + (setq word-beg (point)) + + (setq skip-flag t) + (while (> count 0) + ;; skip-flag and the test for 1-char word takes care of the + ;; special treatment that vip-end-of-word gives to 1-character + ;; words. Otherwise, clicking once on such a word will insert two + ;; words. + (if (and skip-flag (funcall one-char-word-func)) + (setq skip-flag (not skip-flag)) + (funcall word-function-forw 1)) + (setq count (1- count))) + + (vip-forward-char-carefully) + (buffer-substring word-beg (point))) + )) + + +(defun vip-mouse-click-get-word (click &optional count click-count) + "Returns word surrounding the position of a mouse click. +Click may be in another window. Current window and buffer isn't changed." + + (let ((click-word "") + (click-pos (vip-mouse-click-posn click)) + (click-buf (vip-mouse-click-window-buffer click))) + (or (numberp count) (setq count 1)) + (or (numberp click-count) (setq click-count 1)) + + (save-excursion + (save-window-excursion + (if click-pos + (progn + (set-buffer click-buf) + + (goto-char click-pos) + (setq click-word + (funcall vip-surrounding-word-function count click-count))) + (error "Click must be over a window.")) + click-word)))) + +(defun vip-mouse-click-frame (click) + "Returns window where click occurs." + (vip-window-frame (vip-mouse-click-window click))) + +(defun vip-mouse-click-window (click) + "Returns window where click occurs." + (if vip-xemacs-p + (event-window click) + (posn-window (event-start click)))) + +(defun vip-mouse-click-window-buffer (click) + "Returns the buffer of the window where click occurs." + (window-buffer (vip-mouse-click-window click))) + +(defun vip-mouse-click-window-buffer-name (click) + "Returns the name of the buffer in the window where click occurs." + (buffer-name (vip-mouse-click-window-buffer click))) + +(defun vip-mouse-click-posn (click) + "Returns position of a click." + (interactive "e") + (if vip-xemacs-p + (event-point click) + (posn-point (event-start click)))) + +(defun vip-mouse-click-insert-word (click arg) + "Insert word clicked or double-clicked on. +With prefix argument, N, insert that many words. +This command must be bound to a mouse click. +The double-click action of the same mouse button must not be bound +\(or it must be bound to the same function\). +See `vip-surrounding-word' for the definition of a word in this case." + (interactive "e\nP") + (if vip-frame-of-focus ;; to handle clicks in another frame + (vip-select-frame vip-frame-of-focus)) + + ;; turn arg into a number + (cond ((numberp arg) nil) + ;; prefix arg is a list when one hits C-u then command + ((and (listp arg) (numberp (car arg))) + (setq arg (car arg))) + (t (setq arg 1))) + + (let (click-count interrupting-event) + (if (and + (vip-multiclick-p) + ;; This trick checks if there is a pending mouse event + ;; if so, we use this latter event and discard the current mouse click + ;; If the next panding event is not a mouse event, we execute + ;; the current mouse event + (progn + (setq interrupting-event (vip-read-event)) + (vip-mouse-event-p last-input-event))) + (progn ;; interrupted wait + (setq vip-global-prefix-argument arg) + ;; count this click for XEmacs + (vip-event-click-count click)) + ;; uninterrupted wait or the interrupting event wasn't a mouse event + (setq click-count (vip-event-click-count click)) + (if (> click-count 1) + (setq arg vip-global-prefix-argument + vip-global-prefix-argument nil)) + (insert (vip-mouse-click-get-word click arg click-count)) + (if (and interrupting-event + (eventp interrupting-event) + (not (vip-mouse-event-p interrupting-event))) + (vip-set-unread-command-events interrupting-event)) + ))) + +;; arg is an event. accepts symbols and numbers, too +(defun vip-mouse-event-p (event) + (if (eventp event) + (string-match "\\(mouse-\\|frame\\|screen\\|track\\)" + (prin1-to-string (vip-event-key event))))) + +;; XEmacs has no double-click events. So, we must simulate. +;; So, we have to simulate event-click-count. +(defun vip-event-click-count (click) + (if vip-xemacs-p + (progn + ;; if more than 1 second + (if (> (- (event-timestamp click) vip-last-click-event-timestamp) + vip-multiclick-timeout) + (setq vip-current-click-count 0)) + (setq vip-last-click-event-timestamp (event-timestamp click) + vip-current-click-count (1+ vip-current-click-count))) + (event-click-count click))) + + + +(defun vip-mouse-click-search-word (click arg) + "Find the word clicked or double-clicked on. Word may be in another window. +With prefix argument, N, search for N-th occurrence. +This command must be bound to a mouse click. The double-click action of the +same button must not be bound \(or it must be bound to the same function\). +See `vip-surrounding-word' for the details on what constitutes a word for +this command." + (interactive "e\nP") + (if vip-frame-of-focus ;; to handle clicks in another frame + (vip-select-frame vip-frame-of-focus)) + (let (click-word click-count + (previous-search-string vip-s-string)) + + (if (and + (vip-multiclick-p) + ;; This trick checks if there is a pending mouse event + ;; if so, we use this latter event and discard the current mouse click + ;; If the next panding event is not a mouse event, we execute + ;; the current mouse event + (progn + (vip-read-event) + (vip-mouse-event-p last-input-event))) + (progn ;; interrupted wait + (setq vip-global-prefix-argument arg) + ;; remember command that was before the multiclick + (setq this-command last-command) + ;; make sure we counted this event---needed for XEmacs only + (vip-event-click-count click)) + ;; uninterrupted wait + (setq click-count (vip-event-click-count click)) + (setq click-word (vip-mouse-click-get-word click nil click-count)) + + (if (> click-count 1) + (setq arg vip-global-prefix-argument + vip-global-prefix-argument nil)) + (setq arg (or arg 1)) + + (vip-deactivate-mark) + (if (or (not (string= click-word vip-s-string)) + (not (markerp vip-search-start-marker)) + (not (equal (marker-buffer vip-search-start-marker) + (current-buffer))) + (not (eq last-command 'vip-mouse-click-search-word))) + (progn + (setq vip-search-start-marker (point-marker) + vip-local-search-start-marker vip-search-start-marker + vip-mouse-click-search-noerror t + vip-mouse-click-search-limit nil) + + ;; make search string known to Viper + (setq vip-s-string (if vip-re-search + (regexp-quote click-word) + click-word)) + (if (not (string= vip-s-string (car vip-search-history))) + (setq vip-search-history + (cons vip-s-string vip-search-history))) + )) + + (push-mark nil t) + (while (> arg 0) + (vip-forward-word 1) + (condition-case nil + (progn + (if (not (search-forward click-word vip-mouse-click-search-limit + vip-mouse-click-search-noerror)) + (progn + (setq vip-mouse-click-search-noerror nil) + (setq vip-mouse-click-search-limit + (save-excursion + (if (and + (markerp vip-local-search-start-marker) + (marker-buffer vip-local-search-start-marker)) + (goto-char vip-local-search-start-marker)) + (vip-line-pos 'end))) + + (goto-char (point-min)) + (search-forward click-word + vip-mouse-click-search-limit nil))) + (goto-char (match-beginning 0)) + (message "Searching for: %s" vip-s-string) + (if (<= arg 1) + (vip-flash-search-pattern)) + ) + (error (beep 1) + (if (or (not (string= click-word previous-search-string)) + (not (eq last-command 'vip-mouse-click-search-word))) + (message "`%s': String not found in %s" + vip-s-string (buffer-name (current-buffer))) + (message + "`%s': Last occurrence in %s. Back to beginning of search" + click-word (buffer-name (current-buffer))) + (setq arg 1) ;; to terminate the loop + (sit-for 2)) + (setq vip-mouse-click-search-noerror t) + (setq vip-mouse-click-search-limit nil) + (if (and (markerp vip-local-search-start-marker) + (marker-buffer vip-local-search-start-marker)) + (goto-char vip-local-search-start-marker)))) + (setq arg (1- arg))) + ))) + +(defun vip-mouse-catch-frame-switch (event arg) + "Catch the event of switching frame. +Usually is bound to a 'down-mouse' event to work properly. See sample +bindings in viper.el and in the Viper manual." + (interactive "e\nP") + (setq vip-frame-of-focus nil) + ;; pass prefix arg along to vip-mouse-click-search/insert-word + (setq prefix-arg arg) + (if (eq last-command 'handle-switch-frame) + (setq vip-frame-of-focus vip-pre-click-frame)) + ;; make Emacs forget that it executed vip-mouse-catch-frame-switch + (setq this-command last-command)) + +;; Called just before switching frames. Saves the old selected frame. +;; Sets last-command to handle-switch-frame (this is done automatically in +;; Emacs. +;; The semantics of switching frames is different in Emacs and XEmacs. +;; In Emacs, if you select-frame A while mouse is over frame B and then +;; start typing, input goes to frame B, which becomes selected. +;; In XEmacs, input will go to frame A. This may be a bug in one of the +;; Emacsen, but also may be a design decision. +;; Also, in Emacs sending input to frame B generates handle-switch-frame +;; event, while in XEmacs it doesn't. +;; All this accounts for the difference in the behavior of +;; vip-mouse-click-* commands when you click in a frame other than the one +;; that was the last to receive input. In Emacs, focus will be in frame A +;; until you do something other than vip-mouse-click-* command. +;; In XEmacs, you have to manually select frame B (with the mouse click) in +;; order to shift focus to frame B. +(defun vip-save-pre-click-frame (frame) + (setq last-command 'handle-switch-frame) + (setq vip-pre-click-frame (vip-selected-frame))) + + +(cond (window-system + (let* ((search-key (if vip-xemacs-p [(meta button1up)] [S-mouse-1])) + (search-key-catch (if vip-xemacs-p + [(meta button1)] [S-down-mouse-1])) + (insert-key (if vip-xemacs-p [(meta button2up)] [S-mouse-2])) + (insert-key-catch (if vip-xemacs-p + [(meta button2)] [S-down-mouse-2])) + (search-key-unbound (and (not (key-binding search-key)) + (not (key-binding search-key-catch)))) + (insert-key-unbound (and (not (key-binding insert-key)) + (not (key-binding insert-key-catch)))) + ) + + (if search-key-unbound + (global-set-key search-key 'vip-mouse-click-search-word)) + (if insert-key-unbound + (global-set-key insert-key 'vip-mouse-click-insert-word)) + + ;; The following would be needed if you want to use the above two + ;; while clicking in another frame. If you only want to use them + ;; by clicking in another window, not frame, the bindings below + ;; aren't necessary. + + ;; These must be bound to mouse-down event for the same mouse + ;; buttons as 'vip-mouse-click-search-word and + ;; 'vip-mouse-click-insert-word + (if search-key-unbound + (global-set-key search-key-catch 'vip-mouse-catch-frame-switch)) + (if insert-key-unbound + (global-set-key insert-key-catch 'vip-mouse-catch-frame-switch)) + + (if vip-xemacs-p + (add-hook 'mouse-leave-screen-hook + 'vip-save-pre-click-frame) + (defadvice handle-switch-frame (before vip-frame-advice activate) + "Remember the selected frame before the switch-frame event." + (vip-save-pre-click-frame (vip-selected-frame)))) + ))) + + + +(provide 'viper-mous) + +;;; viper-mous.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/emulation/viper-util.el Mon Feb 20 23:15:10 1995 +0000 @@ -0,0 +1,798 @@ +;;; viper-util.el --- Utilities used by viper.el + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 2, or (at your option) +;; any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + +(require 'ring) + +(defconst vip-xemacs-p (string-match "\\(Lucid\\|Xemacs\\)" emacs-version) + "Whether it is XEmacs or not.") +(defconst vip-emacs-p (not vip-xemacs-p) + "Whether it is Emacs or not.") + + +;;; Macros + +(defmacro vip-deflocalvar (var default-value &optional documentation) + (` (progn + (defvar (, var) (, default-value) + (, (format "%s\n\(buffer local\)" documentation))) + (make-variable-buffer-local '(, var)) + ))) + +(defmacro vip-loop (count body) + "(vip-loop COUNT BODY) Execute BODY COUNT times." + (list 'let (list (list 'count count)) + (list 'while '(> count 0) + body + '(setq count (1- count)) + ))) + +(defmacro vip-buffer-live-p (buf) + (` (and (, buf) (get-buffer (, buf)) (buffer-name (get-buffer (, buf)))))) + +;; return buffer-specific macro definition, given a full macro definition +(defmacro vip-kbd-buf-alist (macro-elt) + (` (nth 1 (, macro-elt)))) +;; get a pair: (curr-buffer . macro-definition) +(defmacro vip-kbd-buf-pair (macro-elt) + (` (assoc (buffer-name) (vip-kbd-buf-alist (, macro-elt))))) +;; get macro definition for current buffer +(defmacro vip-kbd-buf-definition (macro-elt) + (` (cdr (vip-kbd-buf-pair (, macro-elt))))) + +;; return mode-specific macro definitions, given a full macro definition +(defmacro vip-kbd-mode-alist (macro-elt) + (` (nth 2 (, macro-elt)))) +;; get a pair: (major-mode . macro-definition) +(defmacro vip-kbd-mode-pair (macro-elt) + (` (assoc major-mode (vip-kbd-mode-alist (, macro-elt))))) +;; get macro definition for the current major mode +(defmacro vip-kbd-mode-definition (macro-elt) + (` (cdr (vip-kbd-mode-pair (, macro-elt))))) + +;; return global macro definition, given a full macro definition +(defmacro vip-kbd-global-pair (macro-elt) + (` (nth 3 (, macro-elt)))) +;; get global macro definition from an elt of macro-alist +(defmacro vip-kbd-global-definition (macro-elt) + (` (cdr (vip-kbd-global-pair (, macro-elt))))) + +;; last elt of a sequence +(defsubst vip-seq-last-elt (seq) + (elt seq (1- (length seq)))) + +;; Check if arg is a valid character for register +;; TYPE is a list that can contain `letter', `Letter', and `digit'. +;; Letter means lowercase letters, Letter means uppercase letters, and +;; digit means digits from 1 to 9. +;; If TYPE is nil, then down/uppercase letters and digits are allowed. +(defun vip-valid-register (reg &optional type) + (or type (setq type '(letter Letter digit))) + (or (if (memq 'letter type) + (and (<= ?a reg) (<= reg ?z))) + (if (memq 'digit type) + (and (<= ?1 reg) (<= reg ?9))) + (if (memq 'Letter type) + (and (<= ?A reg) (<= reg ?Z))) + )) + +(defun vip-valid-marker (marker) + (if (markerp marker) + (let ((buf (marker-buffer marker)) + (pos (marker-position marker))) + (save-excursion + (set-buffer buf) + (and (<= pos (point-max)) (<= (point-min) pos)))))) + + +(defvar vip-minibuffer-overlay-priority 300) +(defvar vip-replace-overlay-priority 400) +(defvar vip-search-overlay-priority 500) + + +;;; XEmacs support + +(if vip-xemacs-p + (progn + (fset 'vip-read-event (symbol-function 'next-command-event)) + (fset 'vip-make-overlay (symbol-function 'make-extent)) + (fset 'vip-overlay-start (symbol-function 'extent-start-position)) + (fset 'vip-overlay-end (symbol-function 'extent-end-position)) + (fset 'vip-overlay-put (symbol-function 'set-extent-property)) + (fset 'vip-overlay-p (symbol-function 'extentp)) + (fset 'vip-overlay-get (symbol-function 'extent-property)) + (fset 'vip-move-overlay (symbol-function 'set-extent-endpoints)) + (if window-system + (fset 'vip-iconify (symbol-function 'iconify-screen))) + (fset 'vip-raise-frame (symbol-function 'raise-screen)) + (fset 'vip-window-frame (symbol-function 'window-screen)) + (fset 'vip-select-frame (symbol-function 'select-screen)) + (fset 'vip-selected-frame (symbol-function 'selected-screen)) + (fset 'vip-frame-selected-window + (symbol-function 'screen-selected-window)) + (fset 'vip-frame-parameters (symbol-function 'screen-parameters)) + (fset 'vip-modify-frame-parameters + (symbol-function 'modify-screen-parameters)) + (cond (window-system + (fset 'vip-get-face (symbol-function 'get-face)) + (fset 'vip-color-defined-p + (symbol-function 'x-valid-color-name-p)) + (fset 'vip-display-color-p + (symbol-function 'x-color-display-p))))) + (fset 'vip-read-event (symbol-function 'read-event)) + (fset 'vip-make-overlay (symbol-function 'make-overlay)) + (fset 'vip-overlay-start (symbol-function 'overlay-start)) + (fset 'vip-overlay-end (symbol-function 'overlay-end)) + (fset 'vip-overlay-put (symbol-function 'overlay-put)) + (fset 'vip-overlay-p (symbol-function 'overlayp)) + (fset 'vip-overlay-get (symbol-function 'overlay-get)) + (fset 'vip-move-overlay (symbol-function 'move-overlay)) + (if window-system + (fset 'vip-iconify (symbol-function 'iconify-or-deiconify-frame))) + (fset 'vip-raise-frame (symbol-function 'raise-frame)) + (fset 'vip-window-frame (symbol-function 'window-frame)) + (fset 'vip-select-frame (symbol-function 'select-frame)) + (fset 'vip-selected-frame (symbol-function 'selected-frame)) + (fset 'vip-frame-selected-window (symbol-function 'frame-selected-window)) + (fset 'vip-frame-parameters (symbol-function 'frame-parameters)) + (fset 'vip-modify-frame-parameters + (symbol-function 'modify-frame-parameters)) + (cond (window-system + (fset 'vip-get-face (symbol-function 'internal-get-face)) + (fset 'vip-color-defined-p (symbol-function 'x-color-defined-p)) + (fset 'vip-display-color-p (symbol-function 'x-display-color-p))))) + +;; OS/2 +(cond ((eq window-system 'pm) + (fset 'vip-color-defined-p + (function (lambda (color) (assoc color pm-color-alist)))))) + +;; needed to smooth out the difference between Emacs and XEmacs +(defsubst vip-italicize-face (face) + (if vip-xemacs-p + (make-face-italic face) + (make-face-italic face nil 'noerror))) + +;; test if display is color and the colors are defined +(defsubst vip-can-use-colors (&rest colors) + (if (vip-display-color-p) + (not (memq nil (mapcar 'vip-color-defined-p colors))) + )) + +;; currently doesn't work for XEmacs +(defun vip-change-cursor-color (new-color) + (if (and window-system (vip-display-color-p) + (stringp new-color) (vip-color-defined-p new-color)) + (vip-modify-frame-parameters + (vip-selected-frame) (list (cons 'cursor-color new-color))))) + +(defsubst vip-save-cursor-color () + (if (and window-system (vip-display-color-p)) + (let ((color (cdr (assoc 'cursor-color (vip-frame-parameters))))) + (if (and (stringp color) (vip-color-defined-p color) + (not (string= color vip-replace-overlay-cursor-color))) + (vip-overlay-put vip-replace-overlay 'vip-cursor-color color))))) + +(defsubst vip-restore-cursor-color () + (vip-change-cursor-color + (vip-overlay-get vip-replace-overlay 'vip-cursor-color))) + + +;; Check the current version against the major and minor version numbers +;; using op: cur-vers op major.minor If emacs-major-version or +;; emacs-minor-version are not defined, we assume that the current version +;; is hopelessly outdated. We assume that emacs-major-version and +;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the +;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value +;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be +;; incorrect. However, this gives correct result in our cases, since we are +;; testing for sufficiently high Emacs versions. +(defun vip-check-version (op major minor &optional type-of-emacs) + (if (and (boundp 'emacs-major-version) (boundp 'emacs-minor-version)) + (and (cond ((eq type-of-emacs 'xemacs) vip-xemacs-p) + ((eq type-of-emacs 'emacs) vip-emacs-p) + (t t)) + (cond ((eq op '=) (and (= emacs-minor-version minor) + (= emacs-major-version major))) + ((memq op '(> >= < <=)) + (and (or (funcall op emacs-major-version major) + (= emacs-major-version major)) + (if (= emacs-major-version major) + (funcall op emacs-minor-version minor) + t))) + (t + (error "%S: Invalid op in vip-check-version" op)))) + (cond ((memq op '(= > >=)) nil) + ((memq op '(< <=)) t)))) + + +;; Early versions of XEmacs didn't have window-live-p (or it didn't work right) +(if (vip-check-version '< 19 11 'xemacs) + (defun window-live-p (win) + (let ((visible nil)) + (walk-windows + '(lambda (walk-win) + (if(equal walk-win win) + (setq visible t))) + nil 'all-screens) + visible)) + ) + + +(defun vip-get-visible-buffer-window (wind) + (if vip-xemacs-p + (get-buffer-window wind t) + (get-buffer-window wind 'visible))) + + +(defun vip-line-pos (pos) + "Return line position. +If pos is 'start then returns position of line start. +If pos is 'end, returns line end. If pos is 'mid, returns line center. +Pos = 'indent returns beginning of indentation. +Otherwise, returns point. Current point is not moved in any case." + (let ((cur-pos (point)) + (result)) + (cond + ((equal pos 'start) + (beginning-of-line)) + ((equal pos 'end) + (end-of-line)) + ((equal pos 'mid) + (goto-char (+ (vip-line-pos 'start) (vip-line-pos 'end) 2))) + ((equal pos 'indent) + (back-to-indentation)) + (t nil)) + (setq result (point)) + (goto-char cur-pos) + result)) + + +(defun vip-move-marker-locally (var pos &optional buffer) + "Like move-marker but creates a virgin marker if arg isn't already a marker. +The first argument must eval to a variable name. +Arguments: (var-name position &optional buffer). + +This is useful for moving markers that are supposed to be local. +For this, VAR-NAME should be made buffer-local with nil as a default. +Then, each time this var is used in `vip-move-marker-locally' in a new +buffer, a new marker will be created." + (if (markerp (eval var)) + () + (set var (make-marker))) + (move-marker (eval var) pos buffer)) + + +(defun vip-message-conditions (conditions) + "Print CONDITIONS as a message." + (let ((case (car conditions)) (msg (cdr conditions))) + (if (null msg) + (message "%s" case) + (message "%s: %s" case (mapconcat 'prin1-to-string msg " "))) + (beep 1))) + + +;;; List/alist utilities + +(defun vip-list-to-alist (lst) + "Convert LIST to an alist." + (let ((alist)) + (while lst + (setq alist (cons (list (car lst)) alist)) + (setq lst (cdr lst))) + alist)) + +(defun vip-alist-to-list (alst) + "Convert ALIST to a list." + (let ((lst)) + (while alst + (setq lst (cons (car (car alst)) lst)) + (setq alst (cdr alst))) + lst)) + +(defun vip-filter-alist (regexp alst) + "Filter ALIST using REGEXP. Return alist whose elements match the regexp." + (interactive "s x") + (let ((outalst) (inalst alst)) + (while (car inalst) + (if (string-match regexp (car (car inalst))) + (setq outalst (cons (car inalst) outalst))) + (setq inalst (cdr inalst))) + outalst)) + +(defun vip-filter-list (regexp lst) + "Filter LIST using REGEXP. Return list whose elements match the regexp." + (interactive "s x") + (let ((outlst) (inlst lst)) + (while (car inlst) + (if (string-match regexp (car inlst)) + (setq outlst (cons (car inlst) outlst))) + (setq inlst (cdr inlst))) + outlst)) + + +;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1 +;; LIS2 is modified by filtering it: deleting its members of the form +;; \(car elt\) such that (car elt') is in LIS1. +(defun vip-append-filter-alist (lis1 lis2) + (let ((temp lis1) + elt) + + ;;filter-append the second list + (while temp + ;; delete all occurrences + (while (setq elt (assoc (car (car temp)) lis2)) + (setq lis2 (delq elt lis2))) + (setq temp (cdr temp))) + + (nconc lis1 lis2))) + + + + +;;; Insertion ring + +;; Rotate RING's index. DIRection can be positive or negative. +(defun vip-ring-rotate1 (ring dir) + (if (and (ring-p ring) (> (ring-length ring) 0)) + (progn + (setcar ring (cond ((> dir 0) + (ring-plus1 (car ring) (ring-length ring))) + ((< dir 0) + (ring-minus1 (car ring) (ring-length ring))) + ;; don't rotate if dir = 0 + (t (car ring)))) + (vip-current-ring-item ring) + ))) + +(defun vip-special-ring-rotate1 (ring dir) + (if (memq vip-intermediate-command + '(repeating-display-destructive-command + repeating-insertion-from-ring)) + (vip-ring-rotate1 ring dir) + ;; don't rotate otherwise + (vip-ring-rotate1 ring 0))) + +;; current ring item; if N is given, then so many items back from the +;; current +(defun vip-current-ring-item (ring &optional n) + (setq n (or n 0)) + (if (and (ring-p ring) (> (ring-length ring) 0)) + (aref (cdr (cdr ring)) (mod (- (car ring) 1 n) (ring-length ring))))) + +;; push item onto ring. the second argument is a ring-variable, not value. +(defun vip-push-onto-ring (item ring-var) + (or (ring-p (eval ring-var)) + (set ring-var (make-ring (eval (intern (format "%S-size" ring-var)))))) + (or (null item) ; don't push nil + (and (stringp item) (string= item "")) ; or empty strings + (equal item (vip-current-ring-item (eval ring-var))) ; or old stuff + ;; Since vip-set-destructive-command checks if we are inside vip-repeat, + ;; we don't check whether this-command-keys is a `.'. + ;; The cmd vip-repeat makes a call to the current function only if + ;; `.' is executing a command from the command history. It doesn't + ;; call the push-onto-ring function if `.' is simply repeating the + ;; last destructive command. + ;; We only check for ESC (which happens when we do insert with a + ;; prefix argument, or if this-command-keys doesn't give anything + ;; meaningful (in that case we don't know what to show to the user). + (and (eq ring-var 'vip-command-ring) + (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)" + (vip-array-to-string (this-command-keys)))) + (vip-ring-insert (eval ring-var) item)) + ) + + +;; removing elts from ring seems to break it +(defun vip-cleanup-ring (ring) + (or (< (ring-length ring) 2) + (null (vip-current-ring-item ring)) + ;; last and previous equal + (if (equal (vip-current-ring-item ring) (vip-current-ring-item ring 1)) + (vip-ring-pop ring)))) + +;; ring-remove seems to be buggy, so we concocted this for our purposes. +(defun vip-ring-pop (ring) + (let* ((ln (ring-length ring)) + (vec (cdr (cdr ring))) + (veclen (length vec)) + (hd (car ring)) + (idx (max 0 (ring-minus1 hd ln))) + (top-elt (aref vec idx))) + + ;; shift elements + (while (< (1+ idx) veclen) + (aset vec idx (aref vec (1+ idx))) + (setq idx (1+ idx))) + (aset vec idx nil) + + (setq hd (max 0 (ring-minus1 hd ln))) + (if (= hd (1- ln)) (setq hd 0)) + (setcar ring hd) ; move head + (setcar (cdr ring) (max 0 (1- ln))) ; adjust length + top-elt + )) + +(defun vip-ring-insert (ring item) + (let* ((ln (ring-length ring)) + (vec (cdr (cdr ring))) + (veclen (length vec)) + (hd (car ring)) + (vecpos-after-hd (if (= hd 0) ln hd)) + (idx ln)) + + (if (= ln veclen) + (progn + (aset vec hd item) ; hd is always 1+ the actual head index in vec + (setcar ring (ring-plus1 hd ln))) + (setcar (cdr ring) (1+ ln)) + (setcar ring (ring-plus1 vecpos-after-hd (1+ ln))) + (while (and (>= idx vecpos-after-hd) (> ln 0)) + (aset vec idx (aref vec (1- idx))) + (setq idx (1- idx))) + (aset vec vecpos-after-hd item)) + item)) + + +;;; String utilities + +;; If STRING is longer than MAX-LEN, truncate it and print ...... instead +;; PRE-STRING is a string to prepend to the abbrev string. +;; POST-STRING is a string to append to the abbrev string. +;; ABBREV_SIGN is a string to be inserted before POST-STRING +;; if the orig string was truncated. +(defun vip-abbreviate-string (string max-len + pre-string post-string abbrev-sign) + (let (truncated-str) + (setq truncated-str + (if (stringp string) + (substring string 0 (min max-len (length string))))) + (cond ((null truncated-str) "") + ((> (length string) max-len) + (format "%s%s%s%s" + pre-string truncated-str abbrev-sign post-string)) + (t (format "%s%s%s" pre-string truncated-str post-string))))) + + +;;; Saving settings in custom file + +(defun vip-save-setting (var message custom-file &optional erase-msg) + "Save the current setting of VAR in CUSTOM-FILE. +If given, MESSAGE is a message to be displayed after that. +This message is erased after 2 secs, if erase-msg is non-nil. +Arguments: (vip-save-setting var message custom-file &optional erase-message)" + (let* ((var-name (symbol-name var)) + (var-val (if (boundp var) (eval var))) + (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name)) + (buf (find-file-noselect (substitute-in-file-name custom-file))) + ) + (message message) + (save-excursion + (set-buffer buf) + (goto-char (point-min)) + (if (re-search-forward regexp nil t) + (let ((reg-end (1- (match-end 0)))) + (search-backward var-name) + (delete-region (match-beginning 0) reg-end) + (goto-char (match-beginning 0)) + (insert (format "%s '%S" var-name var-val))) + (goto-char (point-max)) + (if (not (bolp)) (insert "\n")) + (insert (format "(setq %s '%S)\n" var-name var-val))) + (save-buffer)) + (kill-buffer buf) + (if erase-msg + (progn + (sit-for 2) + (message ""))) + )) + +;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that +;; match this pattern. +(defun vip-save-string-in-file (string custom-file &optional pattern) + (let ((buf (find-file-noselect (substitute-in-file-name custom-file)))) + (save-excursion + (set-buffer buf) + (goto-char (point-min)) + (if pattern (delete-matching-lines pattern)) + (goto-char (point-max)) + (if string (insert string)) + (save-buffer)) + (kill-buffer buf) + )) + + +;;; Overlays + +;; Search + +(defun vip-flash-search-pattern () + (if (vip-overlay-p vip-search-overlay) + (vip-move-overlay vip-search-overlay (match-beginning 0) (match-end 0)) + (setq vip-search-overlay + (vip-make-overlay + (match-beginning 0) (match-end 0) (current-buffer)))) + + (vip-overlay-put vip-search-overlay 'priority vip-search-overlay-priority) + (if window-system + (progn + (vip-overlay-put vip-search-overlay 'face vip-search-face) + (sit-for 2) + (vip-overlay-put vip-search-overlay 'face nil)))) + +;; Replace state + +(defun vip-set-replace-overlay (beg end) + (if (vip-overlay-p vip-replace-overlay) + (vip-move-replace-overlay beg end) + (setq vip-replace-overlay (vip-make-overlay beg end (current-buffer))) + (vip-overlay-put vip-replace-overlay + 'vip-start + (move-marker (make-marker) + (vip-overlay-start vip-replace-overlay))) + (vip-overlay-put vip-replace-overlay + 'vip-end + (move-marker (make-marker) + (vip-overlay-end vip-replace-overlay))) + (vip-overlay-put + vip-replace-overlay 'priority vip-replace-overlay-priority)) + (if window-system + (vip-overlay-put vip-replace-overlay 'face vip-replace-overlay-face)) + (vip-save-cursor-color) + (vip-change-cursor-color vip-replace-overlay-cursor-color) + ) + + +(defsubst vip-hide-replace-overlay () + (vip-restore-cursor-color) + (if window-system + (vip-overlay-put vip-replace-overlay 'face nil))) + + + +(defsubst vip-replace-start () + (vip-overlay-get vip-replace-overlay 'vip-start)) +(defsubst vip-replace-end () + (vip-overlay-get vip-replace-overlay 'vip-end)) + +(defsubst vip-move-replace-overlay (beg end) + (vip-move-overlay vip-replace-overlay beg end) + (move-marker (vip-replace-start) (vip-overlay-start vip-replace-overlay)) + (move-marker (vip-replace-end) (vip-overlay-end vip-replace-overlay))) + + +;; Minibuffer + +(defun vip-set-minibuffer-overlay () + (vip-check-minibuffer-overlay) + ;; We always move the minibuffer overlay, since in XEmacs + ;; this overlay may get detached. Moving will reattach it. + ;; This overlay is also moved via the post-command-hook, + ;; to insure taht it covers the whole minibuffer. + (vip-move-minibuffer-overlay) + (if window-system + (progn + (vip-overlay-put + vip-minibuffer-overlay 'face vip-minibuffer-current-face) + (vip-overlay-put + vip-minibuffer-overlay 'priority vip-minibuffer-overlay-priority)) + )) + +(defun vip-check-minibuffer-overlay () + (if (vip-overlay-p vip-minibuffer-overlay) + () + (setq vip-minibuffer-overlay + (vip-make-overlay 1 (1+ (buffer-size)) (current-buffer))))) + +;; arguments to this function are dummies. they are needed just because +;; it is used as a insert-in-front-hook to vip-minibuffer-overlay, and such +;; hooks require 3 arguments. +(defun vip-move-minibuffer-overlay (&optional overl beg end) + (if (vip-is-in-minibuffer) + (progn + (vip-check-minibuffer-overlay) + (vip-move-overlay vip-minibuffer-overlay 1 (1+ (buffer-size)))))) + +(defsubst vip-is-in-minibuffer () + (string-match "\*Minibuf-" (buffer-name))) + + + +;;; XEmacs compatibility + +;; Sit for VAL miliseconds. XEmacs doesn't support the milisecond arg to +;; sit-for, so this is for compatibility. +(defsubst vip-sit-for-short (val &optional nodisp) + (if vip-xemacs-p + (sit-for (/ val 1000.0) nodisp) + (sit-for 0 val nodisp))) + +;; EVENT may be a single event of a sequence of events +(defsubst vip-ESC-event-p (event) + (let ((ESC-keys '(?\e (control \[) escape)) + (key (vip-event-key event))) + (member key ESC-keys))) + +;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring) +;; is the same as (mark t). +(defsubst vip-set-mark-if-necessary () + (setq mark-ring (delete (vip-mark-marker) mark-ring)) + (set-mark-command nil)) + +(defsubst vip-mark-marker () + (if vip-xemacs-p + (mark-marker t) + (mark-marker))) + +;; In transient mark mode (zmacs mode), it is annoying when regions become +;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless +;; the user explicitly wants highlighting, e.g., by hitting '' or `` +(defun vip-deactivate-mark () + (if vip-xemacs-p + (zmacs-deactivate-region) + (deactivate-mark))) + + +(defsubst vip-events-to-keys (events) + (cond (vip-xemacs-p (events-to-keys events)) + (t events))) + + +(defun vip-eval-after-load (file form) + (if vip-emacs-p + (eval-after-load file form) + (or (assoc file after-load-alist) + (setq after-load-alist (cons (list file) after-load-alist))) + (let ((elt (assoc file after-load-alist))) + (or (member form (cdr elt)) + (setq elt (nconc elt (list form))))) + form + )) + + +;; like read-event, but in XEmacs also try to convert to char, if possible +(defun vip-read-event-convert-to-char () + (let (event) + (if vip-emacs-p + (read-event) + (setq event (next-command-event)) + (or (event-to-character event) + event)) + )) + + +;; Enacs has a bug in eventp, which causes (eventp nil) to return (nil) +;; instead of nil, if '(nil) was previously inadvertantly assigned to +;; unread-command-events +(defun vip-event-key (event) + (or (and event (eventp event)) + (error "vip-event-key: Wrong type argument, eventp, %S" event)) + (let ((mod (event-modifiers event)) + basis) + (setq basis + (cond + (vip-xemacs-p + (cond ((key-press-event-p event) + (event-key event)) + ((button-event-p event) + (concat "mouse-" (event-button event))) + (t + (error "vip-event-key: Unknown event, %S" event)))) + (t + ;; Emacs doesn't handle capital letters correctly, since + ;; \S-a isn't considered the same as A (it behaves as + ;; plain `a' instead). So we take care of this here + (if (and (numberp event) (<= ?A event) (<= event ?Z)) + (setq mod nil + event event) + (event-basic-type event))))) + + (if (numberp basis) + (setq basis + (if (= basis ?\C-?) + (list 'control '\?) ; taking care of an emacs bug + (intern (char-to-string basis))))) + + (if mod + (append mod (list basis)) + basis) + )) + +(defun vip-key-to-emacs-key (key) + (let (key-name char-p modifiers mod-char-list base-key base-key-name) + (cond (vip-xemacs-p key) + ((symbolp key) + (setq key-name (symbol-name key)) + (if (= (length key-name) 1) ; character event + (string-to-char key-name) + key)) + ((listp key) + (setq modifiers (subseq key 0 (1- (length key))) + base-key (vip-seq-last-elt key) + base-key-name (symbol-name base-key) + char-p (= (length base-key-name) 1)) + (setq mod-char-list + (mapcar + '(lambda (elt) (upcase (substring (symbol-name elt) 0 1))) + modifiers)) + (if char-p + (setq key-name + (car (read-from-string + (concat + "?\\" + (mapconcat 'identity mod-char-list "-\\") + "-" + base-key-name)))) + (setq key-name + (intern + (concat + (mapconcat 'identity mod-char-list "-") + "-" + base-key-name)))))) + )) + + +;; Args can be a sequence of events, a string, or a Viper macro. Will try to +;; convert events to keys and, if all keys are regular printable +;; characters, will return a string. Otherwise, will return a string +;; representing a vector of converted events. If the input was a Viper macro, +;; will return a string that represents this macro as a vector. +(defun vip-array-to-string (event-seq &optional representation) + (let (temp) + (cond ((stringp event-seq) event-seq) + ((vip-event-vector-p event-seq) + (setq temp (mapcar 'vip-event-key event-seq)) + (if (vip-char-symbol-sequence-p temp) + (mapconcat 'symbol-name temp "") + (prin1-to-string (vconcat temp)))) + ((vip-char-symbol-sequence-p event-seq) + (mapconcat 'symbol-name event-seq "")) + (t (prin1-to-string event-seq))))) + + +(defsubst vip-fast-keysequence-p () + (not (vip-sit-for-short vip-fast-keyseq-timeout t))) + +(defun vip-read-char-exclusive () + (let (char + (echo-keystrokes 1)) + (while (null char) + (condition-case nil + (setq char (read-char)) + (error + ;; skip event if not char + (vip-read-event)))) + char)) + + + +(defun vip-setup-master-buffer (&rest other-files-or-buffers) + "Set up the current buffer as a master buffer. +Arguments become related buffers. This function should normally be used in +the `Local variables' section of a file." + (setq vip-related-files-and-buffers-ring + (make-ring (1+ (length other-files-or-buffers)))) + (mapcar '(lambda (elt) + (vip-ring-insert vip-related-files-and-buffers-ring elt)) + other-files-or-buffers) + (vip-ring-insert vip-related-files-and-buffers-ring (buffer-name)) + ) + + +(provide 'viper-util) + +;;; viper-util.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/emulation/viper.el Mon Feb 20 23:15:10 1995 +0000 @@ -0,0 +1,5486 @@ +;;; viper.el --- A full-featured Vi emulator for GNU Emacs 19 and XEmacs 19, +;; a VI Plan for Emacs Rescue, +;; and a venomous VI PERil. +;; Viper Is also a Package for Emacs Rebels. + +;; Version: 2.71 +;; Keywords: emulations +;; Author: Michael Kifer <kifer@cs.sunysb.edu> + +;; LCD Archive Entry: +;; viper|Michael Kifer|kifer@cs.sunysb.edu| +;; A full-featured Vi emulator for GNU Emacs 19 and XEmacs 19| +;; 17-February-95|2.71|~/modes/viper.tar.Z| + +(defconst viper-version "2.71 of February 17, 1995" + "The current version of Viper") + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 2, or (at your option) +;; any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + +;;; Commentary: + +;; Viper is a full-featured Vi emulator for Emacs 19. It emulates and +;; improves upon the standard features of Vi and, at the same time, allows +;; full access to all Emacs facilities. Viper supports multiple undo, +;; file name completion, command, file, and search history and it extends +;; Vi in many other ways. Viper is highly customizable through the various +;; hooks, user variables, and keymaps. It is implemented as a collection +;; of minor modes and it is designed to provide full access to all Emacs +;; major and minor modes. +;; +;;; History +;; +;; Viper is a new name for a package formerly known as VIP-19, +;; which was a successor of VIP version 3.5 by Masahiko Sato +;; <ms@sail.stanford.edu> and VIP version 4.2 by Aamod Sane +;; <sane@cs.uiuc.edu>. Some ideas from vip 4.4.2 by Aamod Sane +;; were also shamelessly plagiarized. +;; +;; Viper maintains some degree of compatibility with these older +;; packages. See the documentation for customization. +;; +;; The main difference between Viper and these older packages are: +;; +;; 1. Viper emulates Vi at several levels, from almost complete conformity +;; to a rather loose Vi-compliance. +;; +;; 2. Viper provides full access to all major and minor modes of Emacs +;; without the need to type extra keys. +;; The older versions of VIP (and other Vi emulators) do not work with +;; some major and minor modes. +;; +;; 3. Viper supports vi-style undo. +;; +;; 4. Viper fully emulates (and improves upon) vi's replacement mode. +;; +;; 5. Viper has a better interface to ex, including command, variable, and +;; file name completion. +;; +;; 6. Viper uses native Emacs history and completion features; it doesn't +;; rely on other packages (such as gmhist.el and completer.el) to provide +;; these features. +;; +;; 7. Viper supports Vi-style editing in the minibuffer, by allowing the +;; user to switch from Insert state to Vi state to Replace state, etc. +;; +;; 8. Viper keeps history of recently inserted pieces of text and recently +;; executed Vi-style destructive commands, such as `i', `d', etc. +;; These pieces of text can be inserted in later insertion commands; +;; the previous destructive commands can be re-executed. +;; +;; 9. Viper has Vi-style keyboard macros, which enhances the similar +;; facility in the original Vi. +;; First, one can execute any Emacs command while defining a +;; macro, not just the Vi commands. Second, macros are defined in a +;; WYSYWYG mode, using an interface to Emacs' WYSIWYG style of defining +;; macros. Third, in Viper, one can define macros that are specific to +;; a given buffer, a given major mode, or macros defined for all buffers. +;; The same macro name can have several different definitions: +;; one global, several definitions for various major modes, and +;; definitions for specific buffers. +;; Bffer-specific definitions override mode-specific +;; definitions, which, in turn, override global definitions. +;; +;; +;;; Installation: +;; ------------- +;; +;; (require 'viper) +;; + +;;; Acknowledgements: +;; ----------------- +;; Bug reports and ideas contributed by the following users +;; have helped improve Viper and the various versions of VIP: +;; +;; jjm@hplb.hpl.hp.com (Jean-Jacques Moreau), jl@cse.ogi.edu (John +;; Launchbury), rxga@ulysses.att.com, jamesm@bga.com (D.J. Miller II), +;; ascott@fws214.intel.com (Andy Scott), toma@convex.convex.com, +;; gvr@cs.brown.edu, dave@hellgate.utah.edu, cook@biostat.wisc.edu +;; (Tom Cook), lindstro@biostat.wisc.edu (Mary Lindstrom), +;; edmonds@edmonds.home.cs.ubc.ca (Brian Edmonds), mveiga@dit.upm.es +;; (Marcelino Veiga Tuimil), dwight@toolucky.llnl.gov (Dwight Shih), +;; phil_brooks@MENTORG.COM (Phil Brooks), kin@isi.com (Kin Cho), +;; ahg@panix.com (Al Gelders), dwallach@cs.princeton.edu (Dan Wallach), +;; hpz@ibmhpz.aug.ipp-garching.mpg.de (Hans-Peter Zehrfeld), +;; simonb@prl.philips.co.uk (Simon Blanchard), Mark.Bordas@East.Sun.COM +;; (Mark Bordas), gviswana@cs.wisc.edu (Guhan Viswanathan) +;; +;; Special thanks to Marcelino Veiga Tuimil <mveiga@dit.upm.es> for +;; suggesting a way of intercepting ESC sequences on dumb terminals. Due to +;; this, Viper can now handle arrow keys, F-keys, etc., in Xterm windows +;; and on dumb terminals. This also made it possible to implement Vi-style +;; timeout macros. +;; +;; +;;; Notes: +;; +;; 1. Major modes. +;; In most cases, Viper handles major modes correctly, i.e., they come up +;; in the right state (either vi-state or emacs-state). For instance, text +;; files come up in vi-state, while, say, Dired appears in emacs-state by +;; default. +;; However, some modes do not appear in the right mode in the beginning, +;; usually because they neglect to follow Emacs conventions (e.g., they don't +;; use (kill-all-local-variables) when they start. Some major modes +;; may fail to come up in emacs-state if they call hooks, such as +;; text-hook, for no good reason. +;; +;; As an immediate solution, you can hit C-z to bring about the right mode. +;; An interim solution is to add an appropriate hook to the mode like this: +;; +;; (add-hook 'your-favorite-mode 'viper-mode) +;; or +;; (add-hook 'your-favorite-mode 'vip-change-state-to-emacs) +;; +;; whichever applies. The right thing to do, however, is to complain to the +;; author of the respective package. (Sometimes they also neglect to equip +;; their modes with hooks, which is one more reason for complaining.) +;; +;; 2. Keymap handling +;; Because Emacs 19 has an elegant mechanism for turning minor mode keymaps +;; on and off, implementation of Viper has been greatly simplified. Viper +;; has several minor modes. +;; +;; Viper's Vi state consists of seven minor modes: +;; +;; vip-vi-intercept-minor-mode +;; vip-vi-local-user-minor-mode +;; vip-vi-global-user-minor-mode +;; vip-vi-kbd-minor-mode +;; vip-vi-state-modifier-minor-mode +;; vip-vi-diehard-minor-mode +;; vip-vi-basic-minor-mode +;; +;; Bindings done to the keymap of the first mode overshadow those done to +;; the second, which, in turn, overshadows those done to the third, etc. +;; +;; The last vip-vi-basic-minor-mode contains most of the usual Vi bindings +;; in its edit mode. This mode provides access to all Emacs facilities. +;; Novice users, however, may want to set their vip-expert-level to 1 +;; in their .vip file. This will enable vip-vi-diehard-minor-mode. This +;; minor mode's bindings make Viper simulate the usual Vi very closely. +;; For instance, C-c will not have its standard Emacs binding +;; and so many of the goodies of Emacs are not available. +;; +;; An skilled user, should set vip-expert-level to at least 3. This will +;; enable ;; C-c and many Emacs facilities will become available. +;; In this case, vip-vi-diehard-minor-mode is inactive. +;; +;; Viper gurus should have at least +;; (setq vip-expert-level 4) +;; in their ~/.vip files. This will unsuppress all Emacs keys that are not +;; essential for VI-style editing. +;; Pick-and-choose users may want to put +;; (setq vip-expert-level 5) +;; in ~/.vip. Viper will then leave it up to the user to set the variables +;; vip-want-* See vip-set-expert-level for details. +;; +;; The very first minor mode, vip-vi-intercept-minor-mode, is of no +;; concern for the user. It is needed to bind Viper's vital keys, such as +;; ESC and C-z. +;; +;; The second mode, vip-vi-local-user-minor-mode, usually has an +;; empty keymap. However, the user can set bindings in this keymap, which +;; will overshadow the corresponding bindings in the other two minor +;; modes. This is useful, for example, for setting up ZZ in gnus, +;; rmail, mh-e, etc., to send message instead of saving it in a file. +;; Likewise, in Dired mode, you may want to bind ZN and ZP to commands +;; that would visit the next or the previous file in the Dired buffer. +;; Setting local keys is tricky, so don't do it directly. Instead, use +;; vip-add-local-keys function (see its doc). +;; +;; The third minor mode, vip-vi-global-user-minor-mode, is also intended +;; for the users but, unlike vip-vi-local-user-minor-mode, its key +;; bindings are seen in all Viper buffers. This mode keys can be done +;; with define-key command. +;; +;; The fourth minor mode, vip-vi-kbd-minor-mode, is used by keyboard +;; macros. Users are NOT supposed to modify this keymap directly. +;; +;; The fifth mode, vip-vi-state-modifier-minor-mode, can be used to set +;; key bindings that are visible in some major modes but not in others. +;; +;; Users are allowed to modify keymaps that belong to +;; vip-vi-local-user-minor-mode, vip-vi-global-user-minor-mode, +;; and vip-vi-state-modifier-minor-mode only. +;; +;; Viper's Insert state also has seven minor modes: +;; +;; vip-insert-intercept-minor-mode +;; vip-insert-local-user-minor-mode +;; vip-insert-global-user-minor-mode +;; vip-insert-kbd-minor-mode +;; vip-insert-state-modifier-minor-mode +;; vip-insert-diehard-minor-mode +;; vip-insert-basic-minor-mode +;; +;; As with VI's editing modes, the first mode, vip-insert-intercept-minor-mode +;; is used to bind vital keys that are not to be changed by the user. +;; +;; The next mode, vip-insert-local-user-minor-mode, is used to customize +;; bindings in the insert state of Viper. The third mode, +;; vip-insert-global-user-minor-mode is like +;; vip-insert-local-user-minor-mode, except that its bindings are seen in +;; all Viper buffers. As with vip-vi-local-user-minor-mode, its bindings +;; should be done via the function vip-add-local-keys. Bindings for +;; vip-insert-global-user-minor-mode can be set with the define-key command. +;; +;; The next minor mode, vip-insert-kbd-minor-mode, +;; is used for keyboard VI-style macros defined with :map!. +;; +;; The fifth minor mode, vip-insert-state-modifier-minor-mode, is like +;; vip-vi-state-modifier-minor-mode, except that it is used in the Insert +;; state; it can be used to modify keys in a mode-specific fashion. +;; +;; The minor mode vip-insert-diehard-minor-mode is in effect when +;; the user wants a high degree of Vi compatibility (a bad idea, really!). +;; The last minor mode, vip-insert-basic-minor-mode, is always in effect +;; when Viper is in insert state. It binds a small number of keys needed for +;; Viper's operation. +;; +;; Finally, Viper provides minor modes for overriding bindings set by Emacs +;; modes when Viper is in Emacs state: +;; +;; vip-emacs-local-user-minor-mode +;; vip-emacs-global-user-minor-mode +;; vip-emacs-kbd-minor-mode +;; vip-emacs-state-modifier-minor-mode +;; +;; These minor modes are in effect when Viper is in Emacs state. The keymap +;; associated with vip-emacs-global-user-minor-mode, +;; vip-emacs-global-user-map, overrides the global and local keymaps as +;; well as the minor mode keymaps set by other modes. The keymap of +;; vip-emacs-local-user-minor-mode, vip-emacs-local-user-map, overrides +;; everything, but it is used on a per buffer basis. +;; The keymap associated with vip-emacs-state-modifier-minor-mode +;; overrides keys on a per-major-mode basis. The mode +;; vip-emacs-kbd-minor-mode is used to define Vi-style macros in Emacs +;; state. +;; +;; 3. There is also one minor mode that is used when Viper is in its +;; replace-state (used for commands like cw, C, etc.). This mode is +;; called +;; +;; vip-replace-minor-mode +;; +;; and its keymap is vip-replace-map. Replace minor mode is always +;; used in conjunction with the minor modes for insert-state, and its +;; keymap overshadows the keymaps for insert minor modes. +;; +;; 4. Defining buffer-local bindings in Vi and Insert modes. +;; As mentioned before, sometimes, it is convenient to have +;; buffer-specific of mode-specific key bindings in Vi and insert modes. +;; Viper provides a special function, vip-add-local-keys, to do precisely +;; this. For instance, is you need to add couple of mode-specific bindings +;; to Insert mode, you can put +;; +;; (vip-add-local-keys 'insert-state '((key1 . func1) (key2 .func2))) +;; +;; somewhere in a hook of this major mode. If you put something like this +;; in your own elisp function, this will define bindings specific to the +;; buffer that was current at the time of the call to vip-add-local-keys. +;; The only thing to make sure here is that the major mode of this buffer +;; is written according to Emacs conventions, which includes a call to +;; (kill-all-local-variables). See vip-add-local-keys for more details. +;; +;; +;; TO DO (volunteers?): +;; +;; 1. Some of the code that is inherited from VIP-3.5 is rather +;; convoluted. Instead of vip-command-argument, keymaps should bind the +;; actual commands. E.g., "dw" should be bound to a generic command +;; vip-delete that will delete things based on the value of +;; last-command-char. This would greatly simplify the logic and the code. +;; +;; 2. Somebody should venture to write a customization package a la +;; options.el that would allow the user to change values of variables +;; that meet certain specs (e.g., match a regexp) and whose doc string +;; starts with a '*'. Then, the user should be offered to save +;; variables that were changed. This will make user's customization job +;; much easier. +;; + + +(require 'advice) +(require 'cl) +(require 'ring) + +(require 'viper-util) + + +;;; Variables + +;; Is t until viper-mode executes for the very first time. +;; Prevents recursive descend into startup messages. +(defvar vip-first-time t) + +(defvar vip-expert-level 0 + "User's expert level. +The minor mode vip-vi-diehard-minor-mode is in effect when +vip-expert-level is 1 or 2 or when vip-want-emacs-keys-in-vi is t. +The minor mode vip-insert-diehard-minor-mode is in effect when +vip-expert-level is 1 or 2 or if vip-want-emacs-keys-in-insert is t. +Use `M-x vip-set-expert-level' to change this.") + +;; Max expert level supported by Viper. This is NOT a user option. +;; It is here to make it hard for the user from resetting it. +(defconst vip-max-expert-level 5) + +;; Contains user settings for vars affected by vip-set-expert-level function. +;; Not a user option. +(defvar vip-saved-user-settings nil) + + +;;; Viper minor modes + +;; for some reason, this is not local in Emacs, so I made it so. +(make-variable-buffer-local 'minor-mode-map-alist) + +;; Ideally, minor-mode-map-alist should be permanent-local. But Emacs has a +;; bug that precludes that. So, there is a workaround in +;; vip-harness-minor-mode. +;;(put 'minor-mode-map-alist 'permanent-local t) + +;; Mode for vital things like \e, C-z. +(vip-deflocalvar vip-vi-intercept-minor-mode nil) + +(vip-deflocalvar vip-vi-basic-minor-mode nil + "Viper's minor mode for Vi bindings.") + +(vip-deflocalvar vip-vi-local-user-minor-mode nil + "Auxiliary minor mode for user-defined local bindings in Vi state.") + +(vip-deflocalvar vip-vi-global-user-minor-mode nil + "Auxiliary minor mode for user-defined global bindings in Vi state.") + +(vip-deflocalvar vip-vi-state-modifier-minor-mode nil + "Minor mode used to make major-mode-specific modification to Vi state.") + +(vip-deflocalvar vip-vi-diehard-minor-mode nil + "This minor mode is in effect when the user wants Viper to be Vi.") + +(vip-deflocalvar vip-vi-kbd-minor-mode nil + "Minor mode for Ex command macros Vi state. +The corresponding keymap stores key bindings of Vi macros defined with +the Ex command :map.") + +;; Mode for vital things like \e, C-z. +(vip-deflocalvar vip-insert-intercept-minor-mode nil) + +(vip-deflocalvar vip-insert-basic-minor-mode nil + "Viper's minor mode for bindings in Insert mode.") + +(vip-deflocalvar vip-insert-local-user-minor-mode nil + "Auxiliary minor mode for buffer-local user-defined bindings in Insert state. +This is a way to overshadow normal Insert mode bindings locally to certain +designated buffers.") + +(vip-deflocalvar vip-insert-global-user-minor-mode nil + "Auxiliary minor mode for global user-defined bindings in Insert state.") + +(vip-deflocalvar vip-insert-state-modifier-minor-mode nil + "Minor mode used to make major-mode-specific modification to Insert state.") + +(vip-deflocalvar vip-insert-diehard-minor-mode nil + "Minor mode that simulates Vi very closely. +Not recommened, except for the novice user.") + +(vip-deflocalvar vip-insert-kbd-minor-mode nil +"Minor mode for Ex command macros Insert state. +The corresponding keymap stores key bindings of Vi macros defined with +the Ex command :map!.") + +(vip-deflocalvar vip-replace-minor-mode nil + "Minor mode in effect in replace state (cw, C, and the like commands).") + +;; Mode for vital things like \C-z and \C-x) +;; This is t, by default. So, any new buffer will have C-z defined as +;; switch to Vi, unless we switched states in this buffer +(vip-deflocalvar vip-emacs-intercept-minor-mode t) + +(vip-deflocalvar vip-emacs-local-user-minor-mode t + "Minor mode for local user bindings effective in Emacs state. +Users can use it to override Emacs bindings when Viper is in its Emacs +state.") + +(vip-deflocalvar vip-emacs-global-user-minor-mode t + "Minor mode for global user bindings in effect in Emacs state. +Users can use it to override Emacs bindings when Viper is in its Emacs +state.") + +(vip-deflocalvar vip-emacs-kbd-minor-mode t + "Minor mode for Vi style macros in Emacs state. +The corresponding keymap stores key bindings of Vi macros defined with +`vip-record-kbd-macro' command. There is no Ex-level command to do this +interactively.") + +(vip-deflocalvar vip-emacs-state-modifier-minor-mode t + "Minor mode used to make major-mode-specific modification to Emacs state. +For instance, a Vi purist may want to bind `dd' in Dired mode to a function +that deletes a file.") + + + +;;; ISO characters + +(defvar vip-automatic-iso-accents nil + "*If non-nil, ISO accents will be turned on in insert/replace emacs states and turned off in vi-state. +For some users, this behavior may be too primitive. In this case, use +insert/emacs/vi state hooks.") + + +;;; Emacs keys in other states. + +(defvar vip-want-emacs-keys-in-insert t + "*Set to nil if you want complete Vi compatibility in insert mode. +Complete compatibility with Vi is not recommended for power use of Viper.") + +(defvar vip-want-emacs-keys-in-vi t + "*Set to nil if you want complete Vi compatibility in Vi mode. +Full Vi compatibility is not recommended for power use of Viper.") + + + +;; VI-style Undo + +;; Used to 'undo' complex commands, such as replace and insert commands. +(vip-deflocalvar vip-undo-needs-adjustment nil) +(put 'vip-undo-needs-adjustment 'permanent-local t) + +;; A mark that Viper puts on buffer-undo-list. Marks the beginning of a +;; complex command that must be undone atomically. If inserted, it is +;; erased by vip-change-state-to-vi and vip-repeat. +(defconst vip-buffer-undo-list-mark 'viper) + +(defvar vip-keep-point-on-undo nil + "*Non-nil means not to move point while undoing commands. +This style is different from Emacs and Vi. Try it to see if +it better fits your working style.") + +;; Replace mode and changing text + +;; Viper's own after/before change functions, which get add-hook'ed to Emacs' +(vip-deflocalvar vip-after-change-functions nil "") +(vip-deflocalvar vip-before-change-functions nil "") +(vip-deflocalvar vip-post-command-hooks nil "") +(vip-deflocalvar vip-pre-command-hooks nil "") + +;; Can be used to pass global states around for short period of time +(vip-deflocalvar vip-intermediate-command nil "") + +;; Indicates that the current destructive command has started in replace mode. +(vip-deflocalvar vip-began-as-replace nil "") + +(defvar vip-replace-overlay-cursor-color "Red" + "*Color to use in Replace state") + + +(vip-deflocalvar vip-replace-overlay nil "") +(put 'vip-replace-overlay 'permanent-local t) + +(if window-system + (progn + (make-face 'vip-replace-overlay-face) + (or (face-differs-from-default-p 'vip-replace-overlay-face) + (progn + (if (vip-can-use-colors "darkseagreen2" "Black") + (progn + (set-face-background + 'vip-replace-overlay-face "darkseagreen2") + (set-face-foreground 'vip-replace-overlay-face "Black"))) + (set-face-underline-p 'vip-replace-overlay-face t)) + ))) + +(defvar vip-replace-overlay-face 'vip-replace-overlay-face + "*Face for highlighting replace regions on a window display.") + +(defvar vip-replace-region-end-symbol + (if (and window-system (vip-display-color-p)) "" "$") + "*Symbol to mark the end of a replacement region. A string. +At present, only the first character of a non-empty string is used to +actually mark the region.") +(defvar vip-replace-region-start-symbol "" + "*Symbol to mark the beginning of a replacement region. A string. +Not yet implemented.") + +;; These are local marker that must be initialized to nil and moved with +;; `vip-move-marker-locally' +;; +;; Remember the last position inside the replace region. +(vip-deflocalvar vip-last-posn-in-replace-region nil) +;; Remember the last position while inserting +(vip-deflocalvar vip-last-posn-while-in-insert-state nil) +(put 'vip-last-posn-in-replace-region 'permanent-local t) +(put 'vip-last-posn-while-in-insert-state 'permanent-local t) + +(vip-deflocalvar vip-sitting-in-replace nil "") +(put 'vip-sitting-in-replace 'permanent-local t) + +;; Remember the number of characters that have to be deleted in replace +;; mode to compensate for the inserted characters. +(vip-deflocalvar vip-replace-chars-to-delete 0 "") +(vip-deflocalvar vip-replace-chars-deleted 0 "") + +;; Insertion ring and command ring +(defvar vip-insertion-ring-size 14 + "The size of the insertion ring.") +;; The insertion ring. +(defvar vip-insertion-ring nil) +;; This is temp insertion ring. Used to do rotation for display purposes. +;; When rotation just started, it is initialized to vip-insertion-ring. +(defvar vip-temp-insertion-ring nil) +(defvar vip-last-inserted-string-from-insertion-ring "") + +(defvar vip-command-ring-size 14 + "The size of the command ring.") +;; The command ring. +(defvar vip-command-ring nil) +;; This is temp command ring. Used to do rotation for display purposes. +;; When rotation just started, it is initialized to vip-command-ring. +(defvar vip-temp-command-ring nil) + +;; Modes and related variables + +;; Current mode. One of: `emacs-state', `vi-state', `insert-state' +(vip-deflocalvar vip-current-state 'emacs-state) + + +(defvar vip-toggle-key "\C-z" + "The key used to change states from emacs to Vi and back. +In insert mode, this key also functions as Meta. +Must be set in .vip file or prior to loading Viper. +This setting cannot be changed interactively.") + +(defvar vip-ESC-key "\e" + "Key used to ESC. +Must be set in .vip file or prior to loading Viper. +This setting cannot be changed interactively.") + +(defvar vip-no-multiple-ESC t + "*If true, multiple ESC in Vi mode will cause bell to ring. +\_ is then mapped to Meta. +This is set to t on a windowing terminal and to 'twice on a dumb +terminal (unless the user level is 1, 2, or 5). On a dumb terminal, this +enables cursor keys and is generally more convenient, as terminals usually +don't have a convenient Meta key. +Setting vip-no-multiple-ESC to nil will allow as many multiple ESC, +as is allowed by the major mode in effect.") + + +(defvar vip-want-ctl-h-help nil + "*If t then C-h is bound to help-command in insert mode, if nil then it is +bound to delete-backward-char.") + +;; Autoindent in insert + +;; Variable that keeps track of whether C-t has been pressed. +(vip-deflocalvar vip-cted nil "") + +;; Preserve the indent value, used by C-d in insert mode. +(vip-deflocalvar vip-current-indent 0) + +;; Whether to preserve the indent, used by C-d in insert mode. +(vip-deflocalvar vip-preserve-indent nil) + +(defconst vip-auto-indent nil + "*Autoindent if t.") + +(defconst vip-shift-width 8 + "*The shiftwidth variable.") + +;; Variables for repeating destructive commands + +(defconst vip-keep-point-on-repeat t + "*If t, don't move point when repeating previous command. +This is useful for doing repeated changes with the '.' key. +The user can change this to nil, if she likes when the cursor moves +to a new place after repeating previous Vi command.") + +;; Remember insert point as a marker. This is a local marker that must be +;; initialized to nil and moved with `vip-move-marker-locally'. +(vip-deflocalvar vip-insert-point nil) +(put 'vip-insert-point 'permanent-local t) + +;; This remembers the point before dabbrev-expand was called. +;; If vip-insert-point turns out to be bigger than that, it is reset +;; back to vip-pre-command-point. +;; The reason this is needed is because dabbrev-expand (and possibly +;; others) may jump to before the insertion point, delete something and +;; then reinsert a bigger piece. For instance: bla^blo +;; If dabbrev-expand is called after `blo' and ^ undicates vip-insert-point, +;; then point jumps to the beginning of `blo'. If expansion is found, `blablo' +;; is deleted, and we have |^, where | denotes point. Next, dabbrev-expand +;; will insert the expansion, and we get: blablo^ +;; Whatever we insert next goes before the ^, i.e., before the +;; vip-insert-point marker. So, Viper will think that nothing was +;; inserted. Remembering the orig position of the marker circumvents the +;; problem. +;; We don't know of any command, except dabbrev-expand, that has the same +;; problem. However, the same trick can be used if such a command is +;; discovered later. +;; +(vip-deflocalvar vip-pre-command-point nil) +(put 'vip-pre-command-point 'permanent-local t) ; this is probably an overkill + +;; This is used for saving inserted text. +(defvar vip-last-insertion nil) + +;; Remembers the last replaced region. +(defvar vip-last-replace-region "") + +;; Remember com point as a marker. +;; This is a local marker. Should be moved with `vip-move-marker-locally' +(vip-deflocalvar vip-com-point nil) + +;; If non-nil, the value is a list (M-COM VAL COM REG inserted-text cmd-keys) +;; It is used to re-execute last destructive command. +;; M-COM is a Lisp symbol representing the function to be executed. +;; VAL is the prefix argument that was used with that command. +;; COM is an internal descriptor, such as ?r, ?c, ?C, which contains +;; additional information on how the function in M-COM is to be handled. +;; REG is the register used by command +;; INSERTED-TEXT is text inserted by that command (in case of o, c, C, i, r +;; commands). +;; COMMAND-KEYS are the keys that were typed to invoke the command. +(defvar vip-d-com nil) + +;; The character remembered by the Vi `r' command. +(defvar vip-d-char nil) + +;; Name of register to store deleted or yanked strings +(defvar vip-use-register nil) + + + +;; Variables for Moves and Searches + +;; For use by `;' command. +(defvar vip-f-char nil) + +;; For use by `.' command. +(defvar vip-F-char nil) + +;; For use by `;' command. +(defvar vip-f-forward nil) + +;; For use by `;' command. +(defvar vip-f-offset nil) + +;; Last search string +(defvar vip-s-string "") + +(defvar vip-quote-string "> " + "String inserted at the beginning of quoted region.") + +;; If t, search is forward. +(defvar vip-s-forward nil) + +(defconst vip-case-fold-search nil + "*If t, search ignores cases.") + +(defconst vip-re-search t + "*If t, search is reg-exp search, otherwise vanilla search.") + +(defconst vip-re-query-replace t + "*If t then do regexp replace, if nil then do string replace.") + +(defconst vip-re-replace t + "*If t, do regexp replace. nil means do string replace.") + +(vip-deflocalvar vip-ex-style-motion t + "*Ex-style: the commands l,h do not cross lines, etc.") + +(vip-deflocalvar vip-ex-style-editing-in-insert t + "*The keys ^H, ^? don't jump lines in insert, ESC moves cursor back, etc. +Note: this doesn't preclude ^H and ^? from deleting characters by moving +past the insertion point. This is a feature, not a bug. ") + +(vip-deflocalvar vip-delete-backwards-in-replace nil + "*If t, DEL key will delete characters while moving the cursor backwards. +If nil, the cursor will move backwards without deleting anything.") + +(defconst vip-buffer-search-char nil + "*Key bound for buffer-searching.") + +(defconst vip-search-wrap-around-t t + "*If t, search wraps around.") + +(vip-deflocalvar vip-related-files-and-buffers-ring nil + "*Ring of file and buffer names that are considered to be related to the +current buffer. +These buffers can be cycled through via :R and :P commands.") +(put 'vip-related-files-and-buffers-ring 'permanent-local t) + +;; Used to find out if we are done with searching the current buffer. +(vip-deflocalvar vip-local-search-start-marker nil) +;; As above, but global +(defvar vip-search-start-marker (make-marker)) + +;; the search overlay +(vip-deflocalvar vip-search-overlay nil) + + +(defvar vip-heading-start + (concat "^\\s-*(\\s-*defun\\s-\\|" ;; lisp + "^{\\s-*$\\|^[_a-zA-Z][^()]*[()].*{\\s-*$\\|" ;; C/C++ + "^\\s-*class.*{\\|^\\s-*struct.*{\\|^\\s-*enum.*{\\|" + "^\\\\[sb][a-z]*{.*}\\s-*$\\|" ;; latex + "^@node\\|@table\\|^@m?enu\\|^@itemize\\|^@if\\|" ;; texinfo + "^.+:-") ;; prolog + "*Regexps for Headings. Used by \[\[ and \]\].") + +(defvar vip-heading-end + (concat "^}\\|" ;; C/C++ + "^\\\\end{\\|" ;; latex + "^@end \\|" ;; texinfo + ")\n\n[ \t\n]*\\|" ;; lisp + "\\.\\s-*$") ;; prolog + "*Regexps to end Headings/Sections. Used by \[\].") + + +;; These two vars control the interaction of jumps performed by ' and `. +;; In this new version, '' doesn't erase the marks set by ``, so one can +;; use both kinds of jumps interchangeably and without loosing positions +;; inside the lines. + +;; Remembers position of the last jump done using ``'. +(vip-deflocalvar vip-last-jump nil) +;; Remembers position of the last jump done using `''. +(vip-deflocalvar vip-last-jump-ignore 0) + +;; Some common error messages + +(defconst vip-SpuriousText "Spurious text after command" "") +(defconst vip-BadExCommand "Not an editor command" "") +(defconst vip-InvalidCommandArgument "Invalid command argument" "") +(defconst vip-NoPrevSearch "No previous search string" "") +(defconst vip-EmptyRegister "`%c': Nothing in this register" "") +(defconst vip-InvalidRegister "`%c': Invalid register" "") +(defconst vip-EmptyTextmarker "`%c': Text marker doesn't point anywhere" "") +(defconst vip-InvalidTextmarker "`%c': Invalid text marker" "") +(defconst vip-InvalidViCommand "Invalid command" "") +(defconst vip-BadAddress "Ill-formed address" "") +(defconst vip-FirstAddrExceedsSecond "First address exceeds second" "") +(defconst vip-NoFileSpecified "No file specified" "") + + +;; History variables + +(defvar vip-history nil) +;; History of search strings. +(defvar vip-search-history (list "")) +;; History of query-replace strings used as a source. +(defvar vip-replace1-history nil) +;; History of query-replace strings used as replacement. +(defvar vip-replace2-history nil) +;; History of region quoting strings. +(defvar vip-quote-region-history (list vip-quote-string)) +;; History of Ex-style commands. +(defvar vip-ex-history nil) +;; History of shell commands. +(defvar vip-shell-history nil) + + +;; Last shell command. There are two of these, one for Ex (in viper-ex) +;; and one for Vi. + +;; Last shell command executed with ! command. +(defvar vip-last-shell-com nil) + + + +;;; Miscellaneous + +;; setup emacs-supported vi-style feel +(setq mark-even-if-inactive t + next-line-add-newlines nil + require-final-newline t) + +(defvar vip-inhibit-startup-message nil + "Whether Viper startup message should be inhibited.") + +(defvar vip-always t + "t means, arrange that vi-state will be a default.") + +(defvar vip-custom-file-name "~/.vip" + "Viper customisation file. +This variable must be set _before_ loading Viper.") + +(defvar vip-info-file-name "viper" + "The name prefix for Viper Info files.") + +(defvar vip-spell-function 'ispell-region + "Spell function used by #s<move> command to spell.") + +(defvar vip-tags-file-name "TAGS") + +;; Minibuffer + +(defvar vip-vi-style-in-minibuffer t + "If t, use vi-style editing in minibuffer. +Should be set in `~/.vip' file.") + +;; overlay used in the minibuffer to indicate which state it is in +(vip-deflocalvar vip-minibuffer-overlay nil) + +;; Hook, specific to Viper, which is run just *before* exiting the minibuffer. +;; Beginning with Emacs 19.26, the standard `minibuffer-exit-hook' is run +;; *after* exiting the minibuffer +(defvar vip-minibuffer-exit-hook nil) + +(vip-deflocalvar vip-vi-minibuffer-minor-mode nil + "Minor mode that forces Vi-style when the Minibuffer is in Vi state.") +(vip-deflocalvar vip-insert-minibuffer-minor-mode nil + "Minor mode that forces Vi-style when the Minibuffer is in Insert state.") + +(vip-deflocalvar vip-add-newline-at-eob t + "If t, always add a newline at the end of buffer. +Usually, Viper adds a newline character at the end of the last +line in a buffer, if it's missing. In some major modes, however, like +shell-mode, this is undesirable and must be set to nil. See vip-set-hooks.") + + +;; Mode line +(defconst vip-vi-state-id "<V> " + "Mode line tag identifying the Vi mode of Viper.") +(defconst vip-emacs-state-id "<E> " + "Mode line tag identifying the Emacs mode of Viper.") +(defconst vip-insert-state-id "<I> " + "Mode line tag identifying the Insert mode of Viper.") +(defconst vip-replace-state-id "<R> " + "Mode line tag identifying the Replace mode of Viper.") + +;; Viper changes the default mode-line-buffer-identification +(setq-default mode-line-buffer-identification '(" %b")) + +;; Variable displaying the current Viper state in the mode line. +(vip-deflocalvar vip-mode-string vip-emacs-state-id) +(or (memq 'vip-mode-string global-mode-string) + (setq global-mode-string + (append '("" vip-mode-string) (cdr global-mode-string)))) + + +(defvar vip-vi-state-hooks nil + "*Hooks run just before the switch to Vi mode is completed.") +(defvar vip-insert-state-hooks nil + "*Hooks run just before the switch to Insert mode is completed.") +(defvar vip-replace-state-hooks nil + "*Hooks run just before the switch to Replace mode is completed.") +(defvar vip-emacs-state-hooks nil + "*Hooks run just before the switch to Emacs mode is completed.") + +(defvar vip-load-hooks nil + "Hooks run just after loading Viper.") + + +;; Generic predicates + +;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane + +;; generate test functions +;; given symbol foo, foo-p is the test function, foos is the set of +;; Viper command keys +;; (macroexpand '(vip-test-com-defun foo)) +;; (defun foo-p (com) (consp (memq (if (< com 0) (- com) com) foos))) + +(defmacro vip-test-com-defun (name) + (let* (;;(snm (make-symbol "s1")) + (snm (symbol-name name)) + ;;(nm-p (make-symbol "s2")) + (nm-p (intern (concat snm "-p"))) + ;;(nms (make-symbol "s3")) + (nms (intern (concat snm "s")))) + (` (defun (, nm-p) (com) + (consp (memq (if (< com 0) (- com) com) (, nms))))))) + +;; Variables for defining VI commands + +(defconst vip-prefix-commands '(?c ?d ?y ?! ?= ?# ?< ?> ?\") + "Modifying commands that can be prefixes to movement commands") +(vip-test-com-defun vip-prefix-command) + +(defconst vip-charpair-commands '(?c ?d ?y ?! ?= ?< ?> ?r ?R) + "Commands that are pairs eg. dd. r and R here are a hack") +(vip-test-com-defun vip-charpair-command) + +(defconst vip-movement-commands '(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l + ?H ?M ?n ?t ?T ?w ?W ?$ ?% + ?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?` + ?; ?, ?0 ?? ?/ + ) + "Movement commands") +(vip-test-com-defun vip-movement-command) + +(defconst vip-dotable-commands '(?c ?d ?C ?D ?> ?<) + "Commands that can be repeated by .(dotted)") +(vip-test-com-defun vip-dotable-command) + +(defconst vip-hash-cmds '(?c ?C ?g ?q ?S) + "Commands that can follow a #") +(vip-test-com-defun vip-hash-cmd) + +(defconst vip-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X) + "Commands that may have registers as prefix") +(vip-test-com-defun vip-regsuffix-command) + + + +;;; Arrange the keymaps +(require 'viper-keym) + + +;;;; CODE + +;; changing mode + +;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state. +(defun vip-change-state (new-state) + ;; keep them always fresh + (add-hook 'post-command-hook 'vip-post-command-sentinel t) + (add-hook 'pre-command-hook 'vip-pre-command-sentinel t) + ;; These hooks will be added back if switching to insert/replace mode + (remove-hook 'vip-post-command-hooks + 'vip-insert-state-post-command-sentinel) + (remove-hook 'vip-pre-command-hooks + 'vip-insert-state-pre-command-sentinel) + (cond ((eq new-state 'vi-state) + (cond ((member vip-current-state '(insert-state replace-state)) + + ;; move vip-last-posn-while-in-insert-state + ;; This is a normal hook that is executed in insert/replace + ;; states after each command. In Vi/Emacs state, it does + ;; nothing. We need to execute it here to make sure that + ;; the last posn was recorded when we hit ESC. + ;; It may be left unrecorded if the last thing done in + ;; insert/repl state was dabbrev-expansion or abbrev + ;; expansion caused by hitting ESC + (vip-insert-state-post-command-sentinel) + + (condition-case conds + (progn + (vip-save-last-insertion + vip-insert-point + vip-last-posn-while-in-insert-state) + (if vip-began-as-replace + (setq vip-began-as-replace nil) + ;; repeat insert commands if numerical arg > 1 + (save-excursion + (vip-repeat-insert-command)))) + (error + (vip-message-conditions conds))) + + (if (> (length vip-last-insertion) 0) + (vip-push-onto-ring vip-last-insertion + 'vip-insertion-ring)) + + (if vip-ex-style-editing-in-insert + (or (bolp) (backward-char 1)))) + )) + + ;; insert or replace + ((memq new-state '(insert-state replace-state)) + (if (memq vip-current-state '(emacs-state vi-state)) + (vip-move-marker-locally 'vip-insert-point (point))) + (vip-move-marker-locally 'vip-last-posn-while-in-insert-state (point)) + (add-hook 'vip-post-command-hooks + 'vip-insert-state-post-command-sentinel t) + (add-hook 'vip-pre-command-hooks + 'vip-insert-state-pre-command-sentinel t) + ) + ) ; outermost cond + + ;; Nothing needs to be done to switch to emacs mode! Just set some + ;; variables, which is done in vip-change-state-to-emacs! + + (setq vip-current-state new-state) + (vip-normalize-minor-mode-map-alist) + (vip-adjust-keys-for new-state) + (vip-set-mode-vars-for new-state) + (vip-refresh-mode-line) + ) + + + +(defun vip-adjust-keys-for (state) + "Make necessary adjustments to keymaps before entering STATE." + (cond ((memq state '(insert-state replace-state)) + (if vip-auto-indent + (progn + (define-key vip-insert-basic-map "\C-m" 'vip-autoindent) + (if vip-want-emacs-keys-in-insert + ;; expert + (define-key vip-insert-basic-map "\C-j" nil) + ;; novice + (define-key vip-insert-basic-map "\C-j" 'vip-autoindent)))) + + (setq vip-insert-diehard-minor-mode + (not vip-want-emacs-keys-in-insert)) + + (if vip-want-ctl-h-help + (progn + (define-key vip-insert-basic-map "\C-h" 'help-command) + (define-key vip-replace-map "\C-h" 'help-command)) + (define-key vip-insert-basic-map + "\C-h" 'vip-del-backward-char-in-insert) + (define-key vip-replace-map + "\C-h" 'vip-del-backward-char-in-replace))) + + (t + (setq vip-vi-diehard-minor-mode (not vip-want-emacs-keys-in-vi)) + (if vip-want-ctl-h-help + (define-key vip-vi-basic-map "\C-h" 'help-command) + (define-key vip-vi-basic-map "\C-h" 'vip-backward-char))) + )) + + +(defun vip-normalize-minor-mode-map-alist () + "Normalizes minor-mode-map-alist by putting Viper keymaps first. +This ensures that Viper bindings are in effect, regardless of which minor +modes were turned on by the user or by other packages." + (setq minor-mode-map-alist + (vip-append-filter-alist + (list + (cons 'vip-vi-intercept-minor-mode vip-vi-intercept-map) + (cons 'vip-vi-minibuffer-minor-mode vip-minibuffer-map) + (cons 'vip-vi-local-user-minor-mode vip-vi-local-user-map) + (cons 'vip-vi-kbd-minor-mode vip-vi-kbd-map) + (cons 'vip-vi-global-user-minor-mode vip-vi-global-user-map) + (cons 'vip-vi-state-modifier-minor-mode + (if (keymapp + (cdr (assoc major-mode vip-vi-state-modifier-alist))) + (cdr (assoc major-mode vip-vi-state-modifier-alist)) + vip-empty-keymap)) + (cons 'vip-vi-diehard-minor-mode vip-vi-diehard-map) + (cons 'vip-vi-basic-minor-mode vip-vi-basic-map) + (cons 'vip-insert-intercept-minor-mode vip-insert-intercept-map) + (cons 'vip-replace-minor-mode vip-replace-map) + ;; vip-insert-minibuffer-minor-mode must come after + ;; vip-replace-minor-mode + (cons 'vip-insert-minibuffer-minor-mode + vip-minibuffer-map) + (cons 'vip-insert-local-user-minor-mode + vip-insert-local-user-map) + (cons 'vip-insert-kbd-minor-mode vip-insert-kbd-map) + (cons 'vip-insert-global-user-minor-mode + vip-insert-global-user-map) + (cons 'vip-insert-state-modifier-minor-mode + (if (keymapp + (cdr + (assoc major-mode vip-insert-state-modifier-alist))) + (cdr + (assoc major-mode vip-insert-state-modifier-alist)) + vip-empty-keymap)) + (cons 'vip-insert-diehard-minor-mode vip-insert-diehard-map) + (cons 'vip-insert-basic-minor-mode vip-insert-basic-map) + (cons 'vip-emacs-intercept-minor-mode + vip-emacs-intercept-map) + (cons 'vip-emacs-local-user-minor-mode + vip-emacs-local-user-map) + (cons 'vip-emacs-kbd-minor-mode vip-emacs-kbd-map) + (cons 'vip-emacs-global-user-minor-mode + vip-emacs-global-user-map) + (cons 'vip-emacs-state-modifier-minor-mode + (if (keymapp + (cdr + (assoc major-mode vip-emacs-state-modifier-alist))) + (cdr + (assoc major-mode vip-emacs-state-modifier-alist)) + vip-empty-keymap)) + ) + minor-mode-map-alist))) + + + + + +;; Viper mode-changing commands and utilities + +(defun vip-refresh-mode-line () + "Modifies mode-line-buffer-identification." + (setq vip-mode-string + (cond ((eq vip-current-state 'emacs-state) vip-emacs-state-id) + ((eq vip-current-state 'vi-state) vip-vi-state-id) + ((eq vip-current-state 'replace-state) vip-replace-state-id) + ((eq vip-current-state 'insert-state) vip-insert-state-id))) + + ;; Sets Viper mode string in global-mode-string + (force-mode-line-update)) + +;;;###autoload +(defun viper-mode () + "Turn on Viper emulation of Vi." + (interactive) + (if (not noninteractive) + (progn + (if vip-first-time ; This check is important. Without it, startup and + (progn ; expert-level msgs mix up when viper-mode recurses + (setq vip-first-time nil) + (if (not vip-inhibit-startup-message) + (save-window-excursion + (setq vip-inhibit-startup-message t) + (delete-other-windows) + (switch-to-buffer "Viper Startup Message") + (erase-buffer) + (insert + (substitute-command-keys + "Viper Is a Package for Emacs Rebels. +It is also a VI Plan for Emacs Rescue and a venomous VI PERil. + +Technically speaking, Viper is a Vi emulation package for GNU Emacs 19 and +XEmacs 19. It supports virtually all of Vi and Ex functionality, extending +and improving upon much of it. + + 1. Viper supports Vi at several levels. Level 1 is the closest to + Vi, level 5 provides the most flexibility to depart from many Vi + conventions. + + You will be asked to specify your user level in a following screen. + + If you select user level 1 then the keys ^X, ^C, ^Z, and ^G will + behave as in VI, to smooth transition to Viper for the beginners. + However, to use Emacs productively, you are advised to reach user + level 3 or higher. + + If your user level is 2 or higher, ^X and ^C will invoke Emacs + functions,as usual in Emacs; ^Z will toggle vi/emacs modes, and + ^G will be the usual Emacs's keyboard-quit (something like ^C in VI). + + 2. Vi exit functions (e.g., :wq, ZZ) work on INDIVIDUAL files -- they + do not cause Emacs to quit, except at user level 1 (a novice). + 3. ^X^C EXITS EMACS. + 4. Viper supports multiple undo: `u' will undo. Typing `.' will repeat + undo. Another `u' changes direction. + + 6. Emacs Meta functions are invoked by typing `_' or `\\ ESC'. + On a window system, the best way is to use the Meta-key. + 7. Try \\[keyboard-quit] and \\[abort-recursive-edit] repeatedly, + if something funny happens. This would abort the current editing + command. + +You can get more information on Viper by: + + a. Typing `:help' in Vi state + b. Printing Viper manual, found in ./etc/viper.dvi + c. Printing ViperCard, the Quick Reference, found in ./etc/viperCard.dvi + +This startup message appears whenever you load Viper, unless you type `y' now." + )) + (goto-char (point-min)) + (if (y-or-n-p "Inhibit Viper startup message? ") + (vip-save-setting + 'vip-inhibit-startup-message + "Viper startup message inhibited" + vip-custom-file-name t)) + (kill-buffer (current-buffer)))) + (message " ") + (vip-set-expert-level 'dont-change-unless))) + (vip-change-state-to-vi)))) + +;;;###autoload +(defalias 'vip-mode 'viper-mode) + + +(defun vip-exit-insert-state () + "Switch from Insert state to Vi state." + (interactive) + (vip-change-state-to-vi)) + +(defun vip-set-mode-vars-for (state) + "Sets Viper minor mode variables to put Viper's state STATE in effect." + + ;; Emacs state + (setq vip-vi-minibuffer-minor-mode nil + vip-insert-minibuffer-minor-mode nil + vip-vi-intercept-minor-mode nil + vip-insert-intercept-minor-mode nil + + vip-vi-local-user-minor-mode nil + vip-vi-kbd-minor-mode nil + vip-vi-global-user-minor-mode nil + vip-vi-state-modifier-minor-mode nil + vip-vi-diehard-minor-mode nil + vip-vi-basic-minor-mode nil + + vip-replace-minor-mode nil + + vip-insert-local-user-minor-mode nil + vip-insert-kbd-minor-mode nil + vip-insert-global-user-minor-mode nil + vip-insert-state-modifier-minor-mode nil + vip-insert-diehard-minor-mode nil + vip-insert-basic-minor-mode nil + vip-emacs-intercept-minor-mode t + vip-emacs-local-user-minor-mode t + vip-emacs-kbd-minor-mode (not (vip-is-in-minibuffer)) + vip-emacs-global-user-minor-mode t + vip-emacs-state-modifier-minor-mode t + ) + + ;; Vi state + (if (eq state 'vi-state) ; adjust for vi-state + (setq + vip-vi-intercept-minor-mode t + vip-vi-minibuffer-minor-mode (vip-is-in-minibuffer) + vip-vi-local-user-minor-mode t + vip-vi-kbd-minor-mode (not (vip-is-in-minibuffer)) + vip-vi-global-user-minor-mode t + vip-vi-state-modifier-minor-mode t + ;; don't let the diehard keymap block command completion + ;; and other things in the minibuffer + vip-vi-diehard-minor-mode (not + (or vip-want-emacs-keys-in-vi + (vip-is-in-minibuffer))) + vip-vi-basic-minor-mode t + vip-emacs-intercept-minor-mode nil + vip-emacs-local-user-minor-mode nil + vip-emacs-kbd-minor-mode nil + vip-emacs-global-user-minor-mode nil + vip-emacs-state-modifier-minor-mode nil + )) + + ;; Insert and Replace states + (if (member state '(insert-state replace-state)) + (setq + vip-insert-intercept-minor-mode t + vip-replace-minor-mode (eq state 'replace-state) + vip-insert-minibuffer-minor-mode (vip-is-in-minibuffer) + vip-insert-local-user-minor-mode t + vip-insert-kbd-minor-mode (not (vip-is-in-minibuffer)) + vip-insert-global-user-minor-mode t + vip-insert-state-modifier-minor-mode t + ;; don't let the diehard keymap block command completion + ;; and other things in the minibuffer + vip-insert-diehard-minor-mode (not + (or vip-want-emacs-keys-in-insert + (vip-is-in-minibuffer))) + vip-insert-basic-minor-mode t + vip-emacs-intercept-minor-mode nil + vip-emacs-local-user-minor-mode nil + vip-emacs-kbd-minor-mode nil + vip-emacs-global-user-minor-mode nil + vip-emacs-state-modifier-minor-mode nil + )) + + ;; minibuffer faces + (if window-system + (setq vip-minibuffer-current-face + (cond ((eq state 'emacs-state) vip-minibuffer-emacs-face) + ((eq state 'vi-state) vip-minibuffer-vi-face) + ((memq state '(insert-state replace-state)) + vip-minibuffer-insert-face)))) + + (if (vip-is-in-minibuffer) + (vip-set-minibuffer-overlay)) + ) + +;; This also takes care of the annoying incomplete lines in files. +;; Also, this fixed 'undo' to work vi-style for complex commands. +(defun vip-change-state-to-vi () + "Change Viper state to Vi." + (interactive) + (if (and vip-first-time (not (vip-is-in-minibuffer))) + (viper-mode) + (if overwrite-mode (overwrite-mode nil)) + (if abbrev-mode (expand-abbrev)) + (if (and auto-fill-function (> (current-column) fill-column)) + (funcall auto-fill-function)) + (vip-add-newline-at-eob-if-necessary) + (if vip-undo-needs-adjustment (vip-adjust-undo)) + (vip-change-state 'vi-state) + (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode)) + (iso-accents-mode -1)) ; turn off iso accents + + ;; Protection against user errors in hooks + (condition-case conds + (run-hooks 'vip-vi-state-hooks) + (error + (vip-message-conditions conds))))) + +(defun vip-change-state-to-insert () + "Change Viper state to Insert." + (interactive) + (vip-change-state 'insert-state) + (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode)) + (iso-accents-mode 1)) ; turn iso accents on + + ;; Protection against user errors in hooks + (condition-case conds + (run-hooks 'vip-insert-state-hooks) + (error + (vip-message-conditions conds)))) + +(defsubst vip-downgrade-to-insert () + (setq vip-current-state 'insert-state + vip-replace-minor-mode nil) + ) + + + +;; Change to replace state. When the end of replacement region is reached, +;; replace state changes to insert state. +(defun vip-change-state-to-replace (&optional non-R-cmd) + (vip-change-state 'replace-state) + (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode)) + (iso-accents-mode 1)) ; turn iso accents on + ;; Run insert-state-hook + (condition-case conds + (run-hooks 'vip-insert-state-hooks 'vip-replace-state-hooks) + (error + (vip-message-conditions conds))) + + (if non-R-cmd + (vip-start-replace) + ;; 'R' is implemented using Emacs's overwrite-mode + (vip-start-R-mode)) + ) + + +(defun vip-change-state-to-emacs () + "Change Viper state to Emacs." + (interactive) + (vip-change-state 'emacs-state) + (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode)) + (iso-accents-mode 1)) ; turn iso accents on + + ;; Protection agains user errors in hooks + (condition-case conds + (run-hooks 'vip-emacs-state-hooks) + (error + (vip-message-conditions conds)))) + +;; escape to emacs mode termporarily +(defun vip-escape-to-emacs (arg &optional events) + "Escape to Emacs state from Vi state for one Emacs command. +ARG is used as the prefix value for the executed command. If +EVENTS is a list of events, which become the beginning of the command." + (interactive "P") + (vip-escape-to-state arg events 'emacs-state)) + +;; escape to Vi mode termporarily +(defun vip-escape-to-vi () + "Escape from Emacs state to Vi state for one Vi 1-character command. +This doesn't work with prefix arguments or most complex commands like +cw, dw, etc. But it does work with some 2-character commands, +like dd or dr." + (interactive) + (vip-escape-to-state nil nil 'vi-state)) + +;; Escape to STATE mode for one Emacs command. +(defun vip-escape-to-state (arg events state) + (let (com key prefix-arg) + ;; this temporarily turns off Viper's minor mode keymaps + (vip-set-mode-vars-for state) + (vip-normalize-minor-mode-map-alist) + (if events (vip-set-unread-command-events events)) + + ;; protect against keyboard quit and other errors + (condition-case nil + (progn + (unwind-protect + (progn + (setq com (key-binding (setq key + (if vip-xemacs-p + (read-key-sequence nil) + (read-key-sequence nil t))))) + ;; In case of indirection--chase definitions. + ;; Have to do it here because we execute this command under + ;; different keymaps, so command-execute may not do the + ;; right thing there + (while (vectorp com) (setq com (key-binding com)))) + nil) + ;; exec command in the right Viper state + ;; otherwise, if we switch buffers in the escaped command, + ;; Viper's mode vars will remain those of `state'. When we return + ;; to the orig buffer, the bindings will be screwed up. + (vip-set-mode-vars-for vip-current-state) + + ;; this-command, last-command-char, last-command-event + (setq this-command com) + (if vip-xemacs-p ; XEmacs represents key sequences as vectors + (setq last-command-event (vip-seq-last-elt key) + last-command-char (event-to-character last-command-event)) + ;; Emacs represents them as sequences (str or vec) + (setq last-command-event (vip-seq-last-elt key) + last-command-char last-command-event)) + + (if (commandp com) + (progn + (setq prefix-arg arg) + (command-execute com))) + ) + (quit (ding)) + (error (beep 1)))) + (vip-set-mode-vars-for vip-current-state)) ; set state in new buffer + +(defun vip-exec-form-in-emacs (form) + "Execute FORM in Emacs, temporarily disabling Viper's minor modes. +Similar to vip-escape-to-emacs, but accepts forms rather than keystrokes." + (let ((buff (current-buffer)) + result) + (vip-set-mode-vars-for 'emacs-state) + (setq result (eval form)) + (if (not (equal buff (current-buffer))) ; cmd switched buffer + (save-excursion + (set-buffer buff) + (vip-set-mode-vars-for vip-current-state))) + (vip-set-mode-vars-for vip-current-state) + result)) + + + +;; This is needed because minor modes sometimes override essential Viper +;; bindings. By letting Viper know which files these modes are in, it will +;; arrange to reorganize minor-mode-map-alist so that things will work right. +(defun vip-harness-minor-mode (load-file) + "Familiarize Viper with a minor mode defined in LOAD_FILE. +Minor modes that have their own keymaps may overshadow Viper keymaps. +This function is designed to make Viper aware of the packages that define +such minor modes. +Usage: + (vip-harness-minor-mode load-file) + +LOAD-FILE is a name of the file where the specific minor mode is defined. +Suffixes such as .el or .elc should be stripped." + + (interactive "sEnter name of the load file: ") + + (vip-eval-after-load load-file '(vip-normalize-minor-mode-map-alist)) + + ;; This is a work-around the emacs bug that doesn't let us make + ;; minor-mode-map-alist permanent-local. + ;; This workaround changes the default for minor-mode-map-alist + ;; each time a harnessed minor mode adds its own keymap to the a-list. + (vip-eval-after-load load-file '(setq-default minor-mode-map-alist + minor-mode-map-alist)) + ) + +;; This doesn't work, i.e., doesn't replace vip-harness-minor-mode +;; function, since autoloaded files don't seem to be loaded with lisp's +;; `load' function. +;;(defadvice load (after vip-load-advice activate) +;; "Rearrange `minor-mode-map-alist' after loading a file or a library." +;; (vip-normalize-minor-mode-map-alist) +;; (setq-default minor-mode-map-alist minor-mode-map-alist)) + + + +(defun vip-ESC (arg) + "Emulate ESC key in Emacs. +Prevents multiple escape keystrokes if vip-no-multiple-ESC is true. In that +case \@ will be bound to ESC. If vip-no-multiple-ESC is 'twice double ESC +would dings in vi-state. Other ESC sequences are emulated via the current +Emacs's major mode keymap. This is more convenient on dumb terminals and in +Emacs -nw, since this won't block functional keys such as up,down, +etc. Meta key also will work. When vip-no-multiple-ESC is nil, ESC key +behaves as in Emacs, any number of multiple escapes is allowed." + (interactive "P") + (let (char) + (cond ((and (not vip-no-multiple-ESC) (eq vip-current-state 'vi-state)) + (setq char (vip-read-char-exclusive)) + (vip-escape-to-emacs arg (list ?\e char) )) + ((and (eq vip-no-multiple-ESC 'twice) + (eq vip-current-state 'vi-state)) + (setq char (vip-read-char-exclusive)) + (if (= char (string-to-char vip-ESC-key)) + (ding) + (vip-escape-to-emacs arg (list ?\e char) ))) + (t (ding))) + )) + +(defun vip-alternate-ESC (arg) + "ESC key without checking for multiple keystrokes." + (interactive "P") + (vip-escape-to-emacs arg '(?\e))) + + +;; Intercept ESC sequences on dumb terminals. +;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es> + +;; Check if last key was ESC and if so try to reread it as a function key. +;; But only if there are characters to read during a very short time. +;; Returns the last event, if any. +(defun vip-envelop-ESC-key () + (let ((event last-input-event) + (keyseq [nil]) + inhibit-quit) + (if (vip-ESC-event-p event) + (progn + (if (vip-fast-keysequence-p) + (progn + (let ((vip-vi-intercept-minor-mode nil) + (vip-insert-intercept-minor-mode nil) + (vip-emacs-intercept-minor-mode nil) + (vip-vi-state-modifier-minor-mode nil) + (vip-vi-global-user-minor-mode nil) + (vip-vi-local-user-minor-mode nil) + (vip-replace-minor-mode nil) ; actually unnecessary + (vip-insert-state-modifier-minor-mode nil) + (vip-insert-global-user-minor-mode nil) + (vip-insert-local-user-minor-mode nil) + (vip-emacs-state-modifier-minor-mode nil) + (vip-emacs-global-user-minor-mode nil) + (vip-emacs-local-user-minor-mode nil) + ) + ;; The treatment of XEmacs, below, is temporary, since we + ;; don't know how XEmacs will implement dumb terminals. + ;; Note: the treatment of fast keysequences here is + ;; needed only on dumb terminals in order to be able to + ;; handle function keys correctly. + (if vip-xemacs-p + (setq keyseq (vector event)) + (vip-set-unread-command-events event) + (setq keyseq + (funcall + (ad-get-orig-definition 'read-key-sequence) nil)) + )) + ;; If keyseq translates into something that still has ESC + ;; in the beginning, separate ESC from the rest of the seq. + ;; + ;; This is needed for the following reason: + ;; If ESC is the first symbol, we interpret it as if the + ;; user typed ESC and then quickly some other symbols. + ;; If ESC is not the first one, then the key sequence + ;; entered was apparently translated into a function key or + ;; something (e.g., one may have + ;; (define-key function-key-map "\e[192z" [f11]) + ;; which would translate the escape-sequence generated by + ;; f11 in an xterm window into the symbolic key f11. + (if (vip-ESC-event-p (elt keyseq 0)) + (progn + ;; put keys following ESC on the unread list + ;; and return ESC as the key-sequence + (vip-set-unread-command-events (subseq keyseq 1)) + (setq last-input-event event + keyseq "\e"))) + ) ; end progn + + ;; this is escape event with nothing after it + ;; put in unread-command-event and then re-read + (vip-set-unread-command-events event) + (setq keyseq + (funcall (ad-get-orig-definition 'read-key-sequence) nil)) + )) + ;; not an escape event + (setq keyseq (vector event))) + keyseq)) + + + +(defadvice read-key-sequence (around vip-read-key-sequence-ad activate) + (let (inhibit-quit event keyseq) + (setq keyseq ad-do-it) + (setq event (if vip-xemacs-p + (elt keyseq 0) ; XEmacs returns vector of events + (elt (listify-key-sequence keyseq) 0))) + (if (vip-ESC-event-p event) + (let (unread-command-events unread-command-event) + (vip-set-unread-command-events keyseq) + (if (vip-fast-keysequence-p) + (let ((vip-vi-global-user-minor-mode nil) + (vip-vi-local-user-minor-mode nil) + (vip-replace-minor-mode nil) ; actually unnecessary + (vip-insert-global-user-minor-mode nil) + (vip-insert-local-user-minor-mode nil)) + (setq keyseq ad-do-it)) + (setq keyseq ad-do-it)))) + keyseq)) + +(defadvice describe-key (before vip-read-key-sequence-ad protect activate) + "Force `describe-key' to read key via `read-key-sequence'." + (interactive (list (vip-events-to-keys + (read-key-sequence "Describe key: "))))) + +(defadvice describe-key-briefly + (before vip-read-key-sequence-ad protect activate) + "Force `describe-key-briefly' to read key via `read-key-sequence'." + (interactive (list (vip-events-to-keys + (read-key-sequence "Describe key briefly: "))))) + +(defun vip-intercept-ESC-key () + "Listen to ESC key. +If a sequence of keys starting with ESC is issued with very short delays, +interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key." + (interactive) + (let ((cmd (or (key-binding (vip-envelop-ESC-key)) + '(lambda () (interactive) (error ""))))) + + ;; call the actual function to execute ESC (if no other symbols followed) + ;; or the key bound to the ESC sequence (if the sequence was issued + ;; with very short delay between characters. + (if (eq cmd 'vip-intercept-ESC-key) + (setq cmd + (cond ((eq vip-current-state 'vi-state) + 'vip-ESC) + ((eq vip-current-state 'insert-state) + 'vip-exit-insert-state) + ((eq vip-current-state 'replace-state) + 'vip-replace-state-exit-cmd) + (t 'vip-change-state-to-vi) + ))) + (call-interactively cmd))) + + + +;; prefix argument for Vi mode + +;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM +;; represents the numeric value of the prefix argument and COM represents +;; command prefix such as "c", "d", "m" and "y". + +(defun vip-prefix-arg-value (event com) + "Compute numeric prefix arg value. +Invoked by CHAR. COM is the command part obtained so far." + (let (value) + ;; read while number + (while (and (numberp event) (>= event ?0) (<= event ?9)) + (setq value (+ (* (if (numberp value) value 0) 10) (- event ?0))) + (setq event (vip-read-event-convert-to-char))) + + (setq prefix-arg value) + (if com (setq prefix-arg (cons prefix-arg com))) + (while (eq event ?U) + (vip-describe-arg prefix-arg) + (setq event (vip-read-event-convert-to-char))) + (vip-set-unread-command-events event))) + +(defun vip-prefix-arg-com (char value com) + "Vi operator as prefix argument." + (let ((cont t)) + (while (and cont + (memq char + (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\" + vip-buffer-search-char))) + (if com + ;; this means that we already have a command character, so we + ;; construct a com list and exit while. however, if char is " + ;; it is an error. + (progn + ;; new com is (CHAR . OLDCOM) + (if (memq char '(?# ?\")) (error "")) + (setq com (cons char com)) + (setq cont nil)) + ;; If com is nil we set com as char, and read more. Again, if char + ;; is ", we read the name of register and store it in vip-use-register. + ;; if char is !, =, or #, a complete com is formed so we exit the + ;; while loop. + (cond ((memq char '(?! ?=)) + (setq com char) + (setq char (read-char)) + (setq cont nil)) + ((= char ?#) + ;; read a char and encode it as com + (setq com (+ 128 (read-char))) + (setq char (read-char))) + ((= char ?\") + (let ((reg (read-char))) + (if (vip-valid-register reg) + (setq vip-use-register reg) + (error "")) + (setq char (read-char)))) + (t + (setq com char) + (setq char (vip-read-char-exclusive))))))) + (if (atom com) + ;; com is a single char, so we construct prefix-arg + ;; and if char is ?, describe prefix arg, otherwise exit by + ;; pushing the char back + (progn + (setq prefix-arg (cons value com)) + (while (= char ?U) + (vip-describe-arg prefix-arg) + (setq char (read-char))) + (vip-set-unread-command-events char) + ) + ;; as com is non-nil, this means that we have a command to execute + (if (memq (car com) '(?r ?R)) + ;; execute apropriate region command. + (let ((char (car com)) (com (cdr com))) + (setq prefix-arg (cons value com)) + (if (= char ?r) (vip-region prefix-arg) + (vip-Region prefix-arg)) + ;; reset prefix-arg + (setq prefix-arg nil)) + ;; otherwise, reset prefix arg and call appropriate command + (setq value (if (null value) 1 value)) + (setq prefix-arg nil) + (cond ((equal com '(?c . ?c)) (vip-line (cons value ?C))) + ((equal com '(?d . ?d)) (vip-line (cons value ?D))) + ((equal com '(?d . ?y)) (vip-yank-defun)) + ((equal com '(?y . ?y)) (vip-line (cons value ?Y))) + ((equal com '(?< . ?<)) (vip-line (cons value ?<))) + ((equal com '(?> . ?>)) (vip-line (cons value ?>))) + ((equal com '(?! . ?!)) (vip-line (cons value ?!))) + ((equal com '(?= . ?=)) (vip-line (cons value ?=))) + (t (error "")))))) + +(defun vip-describe-arg (arg) + (let (val com) + (setq val (vip-P-val arg) + com (vip-getcom arg)) + (if (null val) + (if (null com) + (message "Value is nil, and command is nil") + (message "Value is nil, and command is `%c'" com)) + (if (null com) + (message "Value is `%d', and command is nil" val) + (message "Value is `%d', and command is `%c'" val com))))) + +(defun vip-digit-argument (arg) + "Begin numeric argument for the next command." + (interactive "P") + (vip-prefix-arg-value last-command-char + (if (consp arg) (cdr arg) nil))) + +(defun vip-command-argument (arg) + "Accept a motion command as an argument." + (interactive "P") + (condition-case nil + (vip-prefix-arg-com + last-command-char + (cond ((null arg) nil) + ((consp arg) (car arg)) + ((numberp arg) arg) + (t (error vip-InvalidCommandArgument))) + (cond ((null arg) nil) + ((consp arg) (cdr arg)) + ((numberp arg) nil) + (t (error vip-InvalidCommandArgument)))) + (quit (setq vip-use-register nil) + (signal 'quit nil))) + (vip-deactivate-mark)) + +;; Get value part of prefix-argument ARG. +(defsubst vip-p-val (arg) + (cond ((null arg) 1) + ((consp arg) (if (null (car arg)) 1 (car arg))) + (t arg))) + +;; Get raw value part of prefix-argument ARG. +(defsubst vip-P-val (arg) + (cond ((consp arg) (car arg)) + (t arg))) + +;; Get com part of prefix-argument ARG. +(defsubst vip-getcom (arg) + (cond ((null arg) nil) + ((consp arg) (cdr arg)) + (t nil))) + +;; Get com part of prefix-argument ARG and modify it. +(defun vip-getCom (arg) + (let ((com (vip-getcom arg))) + (cond ((equal com ?c) ?C) + ((equal com ?d) ?D) + ((equal com ?y) ?Y) + (t com)))) + + +;; repeat last destructive command + +;; Append region to text in register REG. +;; START and END are buffer positions indicating what to append. +(defsubst vip-append-to-register (reg start end) + (set-register reg (concat (or (get-register reg) "") + (buffer-substring start end)))) + +;; define functions to be executed + +;; invoked by C command +(defun vip-exec-change (m-com com) + ;; handle C cmd at the eol and at eob. + (if (or (and (eolp) (= vip-com-point (point))) + (= vip-com-point (point-max))) + (progn + (insert " ")(backward-char 1))) + (if (= vip-com-point (point)) + (vip-forward-char-carefully)) + (if (= com ?c) + (vip-change vip-com-point (point)) + (vip-change-subr vip-com-point (point)))) + +;; this is invoked by vip-substitute-line +(defun vip-exec-Change (m-com com) + (save-excursion + (set-mark vip-com-point) + (vip-enlarge-region (mark t) (point)) + (if vip-use-register + (progn + (cond ((vip-valid-register vip-use-register '(letter digit)) + ;;(vip-valid-register vip-use-register '(letter) + (copy-to-register + vip-use-register (mark t) (point) nil)) + ((vip-valid-register vip-use-register '(Letter)) + (vip-append-to-register + (downcase vip-use-register) (mark t) (point))) + (t (setq vip-use-register nil) + (error vip-InvalidRegister vip-use-register))) + (setq vip-use-register nil))) + (delete-region (mark t) (point))) + (open-line 1) + (if (= com ?C) (vip-change-mode-to-insert) (vip-yank-last-insertion))) + +(defun vip-exec-delete (m-com com) + (if vip-use-register + (progn + (cond ((vip-valid-register vip-use-register '(letter digit)) + ;;(vip-valid-register vip-use-register '(letter)) + (copy-to-register + vip-use-register vip-com-point (point) nil)) + ((vip-valid-register vip-use-register '(Letter)) + (vip-append-to-register + (downcase vip-use-register) vip-com-point (point))) + (t (setq vip-use-register nil) + (error vip-InvalidRegister vip-use-register))) + (setq vip-use-register nil))) + (setq last-command + (if (eq last-command 'd-command) 'kill-region nil)) + (kill-region vip-com-point (point)) + (setq this-command 'd-command) + (if vip-ex-style-motion + (if (and (eolp) (not (bolp))) (backward-char 1)))) + +(defun vip-exec-Delete (m-com com) + (save-excursion + (set-mark vip-com-point) + (vip-enlarge-region (mark t) (point)) + (if vip-use-register + (progn + (cond ((vip-valid-register vip-use-register '(letter digit)) + ;;(vip-valid-register vip-use-register '(letter)) + (copy-to-register + vip-use-register (mark t) (point) nil)) + ((vip-valid-register vip-use-register '(Letter)) + (vip-append-to-register + (downcase vip-use-register) (mark t) (point))) + (t (setq vip-use-register nil) + (error vip-InvalidRegister vip-use-register))) + (setq vip-use-register nil))) + (setq last-command + (if (eq last-command 'D-command) 'kill-region nil)) + (kill-region (mark t) (point)) + (if (eq m-com 'vip-line) (setq this-command 'D-command))) + (back-to-indentation)) + +(defun vip-exec-yank (m-com com) + (if vip-use-register + (progn + (cond ((vip-valid-register vip-use-register '(letter digit)) + ;; (vip-valid-register vip-use-register '(letter)) + (copy-to-register + vip-use-register vip-com-point (point) nil)) + ((vip-valid-register vip-use-register '(Letter)) + (vip-append-to-register + (downcase vip-use-register) vip-com-point (point))) + (t (setq vip-use-register nil) + (error vip-InvalidRegister vip-use-register))) + (setq vip-use-register nil))) + (setq last-command nil) + (copy-region-as-kill vip-com-point (point)) + (goto-char vip-com-point)) + +(defun vip-exec-Yank (m-com com) + (save-excursion + (set-mark vip-com-point) + (vip-enlarge-region (mark t) (point)) + (if vip-use-register + (progn + (cond ((vip-valid-register vip-use-register '(letter digit)) + ;;(vip-valid-register vip-use-register '(letter)) + (copy-to-register + vip-use-register (mark t) (point) nil)) + ((vip-valid-register vip-use-register '(Letter)) + (vip-append-to-register + (downcase vip-use-register) (mark t) (point))) + (t (setq vip-use-register nil) + (error vip-InvalidRegister vip-use-register))) + (setq vip-use-register nil))) + (setq last-command nil) + (copy-region-as-kill (mark t) (point))) + (goto-char vip-com-point)) + +(defun vip-exec-bang (m-com com) + (save-excursion + (set-mark vip-com-point) + (vip-enlarge-region (mark t) (point)) + (shell-command-on-region + (mark t) (point) + (if (= com ?!) + (setq vip-last-shell-com + (vip-read-string-with-history + "!" + nil + 'vip-shell-history + (car vip-shell-history) + )) + vip-last-shell-com) + t))) + +(defun vip-exec-equals (m-com com) + (save-excursion + (set-mark vip-com-point) + (vip-enlarge-region (mark t) (point)) + (if (> (mark t) (point)) (exchange-point-and-mark)) + (indent-region (mark t) (point) nil))) + +(defun vip-exec-shift (m-com com) + (save-excursion + (set-mark vip-com-point) + (vip-enlarge-region (mark t) (point)) + (if (> (mark t) (point)) (exchange-point-and-mark)) + (indent-rigidly (mark t) (point) + (if (= com ?>) + vip-shift-width + (- vip-shift-width))))) + +;; this is needed because some commands fake com by setting it to ?r, which +;; denotes repeated insert command. +(defsubst vip-exec-dummy (m-com com) + nil) + +(defun vip-exec-buffer-search (m-com com) + (setq vip-s-string (buffer-substring (point) vip-com-point)) + (setq vip-s-forward t) + (setq vip-search-history (cons vip-s-string vip-search-history)) + (vip-search vip-s-string vip-s-forward 1)) + +(defvar vip-exec-array (make-vector 128 nil)) + +;; Using a dispatch array allows adding functions like buffer search +;; without affecting other functions. Buffer search can now be bound +;; to any character. + +(aset vip-exec-array ?c 'vip-exec-change) +(aset vip-exec-array ?C 'vip-exec-Change) +(aset vip-exec-array ?d 'vip-exec-delete) +(aset vip-exec-array ?D 'vip-exec-Delete) +(aset vip-exec-array ?y 'vip-exec-yank) +(aset vip-exec-array ?Y 'vip-exec-Yank) +(aset vip-exec-array ?r 'vip-exec-dummy) +(aset vip-exec-array ?! 'vip-exec-bang) +(aset vip-exec-array ?< 'vip-exec-shift) +(aset vip-exec-array ?> 'vip-exec-shift) +(aset vip-exec-array ?= 'vip-exec-equals) + + + +;; This function is called by various movement commands to execute a +;; destructive command on the region specified by the movement command. For +;; instance, if the user types cw, then the command vip-forward-word will +;; call vip-execute-com to execute vip-exec-change, which eventually will +;; call vip-change to invoke the replace mode on the region. +;; +;; The list (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS) is set to +;; vip-d-com for later use by vip-repeat. +(defun vip-execute-com (m-com val com) + (let ((reg vip-use-register)) + ;; this is the special command `#' + (if (> com 128) + (vip-special-prefix-com (- com 128)) + (let ((fn (aref vip-exec-array (if (< com 0) (- com) com)))) + (if (null fn) + (error "%c: %s" com vip-InvalidViCommand) + (funcall fn m-com com)))) + (if (vip-dotable-command-p com) + (vip-set-destructive-command + (list m-com val + (if (memq com (list ?c ?C ?!)) (- com) com) + reg nil nil))) + )) + + +(defun vip-repeat (arg) + "Re-execute last destructive command. +Use the info in vip-d-com, which has the form +\(com val ch reg inserted-text command-keys\), +where `com' is the command to be re-executed, `val' is the +argument to `com', `ch' is a flag for repeat, and `reg' is optional; +if it exists, it is the name of the register for `com'. +If the prefix argument, ARG, is non-nil, it is used instead of `val'." + (interactive "P") + (let ((save-point (point)) ; save point before repeating prev cmd + ;; Pass along that we are repeating a destructive command + ;; This tells vip-set-destructive-command not to update + ;; vip-command-ring + (vip-intermediate-command 'vip-repeat)) + (if (eq last-command 'vip-undo) + ;; if the last command was vip-undo, then undo-more + (vip-undo-more) + ;; otherwise execute the command stored in vip-d-com. if arg is non-nil + ;; its prefix value is used as new prefix value for the command. + (let ((m-com (car vip-d-com)) + (val (vip-P-val arg)) + (com (nth 2 vip-d-com)) + (reg (nth 3 vip-d-com))) + (if (null val) (setq val (nth 1 vip-d-com))) + (if (null m-com) (error "No previous command to repeat.")) + (setq vip-use-register reg) + (if (nth 4 vip-d-com) ; text inserted by command + (setq vip-last-insertion (nth 4 vip-d-com) + vip-d-char (nth 4 vip-d-com))) + (funcall m-com (cons val com)) + (if (and vip-keep-point-on-repeat (< save-point (point))) + (goto-char save-point)) ; go back to before repeat. + (if (and (eolp) (not (bolp))) + (backward-char 1)) + )) + (if vip-undo-needs-adjustment (vip-adjust-undo)) ; take care of undo + ;; If the prev cmd was rotating the command ring, this means that `.' has + ;; just executed a command from that ring. So, push it on the ring again. + ;; If we are just executing previous command , then don't push vip-d-com + ;; because vip-d-com is not fully constructed in this case (its keys and + ;; the inserted text may be nil). Besides, in this case, the command + ;; executed by `.' is already on the ring. + (if (eq last-command 'vip-display-current-destructive-command) + (vip-push-onto-ring vip-d-com 'vip-command-ring)) + (vip-deactivate-mark) + )) + +(defun vip-repeat-from-history () + "Repeat a destructive command from history. +Doesn't change vip-command-ring in any way, so `.' will work as before +executing this command. +This command is supposed to be bound to a two-character Vi macro where +the second character is a digit 0 to 9. The digit indicates which +history command to execute. `<char>0' is equivalent to `.', `<char>1' +invokes the command before that, etc." + (interactive) + (let* ((vip-intermediate-command 'repeating-display-destructive-command) + (idx (cond (vip-this-kbd-macro + (string-to-number + (symbol-name (elt vip-this-kbd-macro 1)))) + (t 0))) + (num idx) + (vip-d-com vip-d-com)) + + (or (and (numberp num) (<= 0 num) (<= num 9)) + (setq idx 0 + num 0) + (message + "`vip-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'")) + (while (< 0 num) + (setq vip-d-com (vip-special-ring-rotate1 vip-command-ring -1)) + (setq num (1- num))) + (vip-repeat nil) + (while (> idx num) + (vip-special-ring-rotate1 vip-command-ring 1) + (setq num (1+ num))) + )) + + +(defun vip-special-prefix-com (char) + "This command is invoked interactively by the key sequence #<char>." + (cond ((= char ?c) + (downcase-region (min vip-com-point (point)) + (max vip-com-point (point)))) + ((= char ?C) + (upcase-region (min vip-com-point (point)) + (max vip-com-point (point)))) + ((= char ?g) + (push-mark vip-com-point t) + (vip-global-execute)) + ((= char ?q) + (push-mark vip-com-point t) + (vip-quote-region)) + ((= char ?s) (funcall vip-spell-function vip-com-point (point))) + (t (error "#%c: %s" char vip-InvalidViCommand)))) + + +;; undoing + +(defun vip-undo () + "Undo previous change." + (interactive) + (message "undo!") + (let ((modified (buffer-modified-p)) + (before-undo-pt (point-marker)) + (after-change-functions after-change-functions) + undo-beg-posn undo-end-posn) + + ;; no need to remove this hook, since this var has scope inside a let. + (add-hook 'after-change-functions + '(lambda (beg end len) + (setq undo-beg-posn beg + undo-end-posn (or end beg)))) + + (undo-start) + (undo-more 2) + (setq undo-beg-posn (or undo-beg-posn before-undo-pt) + undo-end-posn (or undo-end-posn undo-beg-posn)) + + (goto-char undo-beg-posn) + (sit-for 0) + (if (and vip-keep-point-on-undo + (pos-visible-in-window-p before-undo-pt)) + (progn + (push-mark (point-marker) t) + (vip-sit-for-short 300) + (goto-char undo-end-posn) + (vip-sit-for-short 300) + (if (and (> (abs (- undo-beg-posn before-undo-pt)) 1) + (> (abs (- undo-end-posn before-undo-pt)) 1)) + (goto-char before-undo-pt) + (goto-char undo-beg-posn))) + (push-mark before-undo-pt t)) + (if (and (eolp) (not (bolp))) (backward-char 1)) + (if (not modified) (set-buffer-modified-p t))) + (setq this-command 'vip-undo)) + +(defun vip-undo-more () + "Continue undoing previous changes." + (message "undo more!") + (condition-case nil + (undo-more 1) + (error (beep) + (message "No further undo information in this buffer"))) + (if (and (eolp) (not (bolp))) (backward-char 1)) + (setq this-command 'vip-undo)) + +;; The following two functions are used to set up undo properly. +;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines, +;; they are undone all at once. +(defun vip-adjust-undo () + (let ((inhibit-quit t) + tmp tmp2) + (setq vip-undo-needs-adjustment nil) + (if (listp buffer-undo-list) + (if (setq tmp (memq vip-buffer-undo-list-mark buffer-undo-list)) + (progn + (setq tmp2 (cdr tmp)) ; the part after mark + + ;; cut tail from buffer-undo-list temporarily by direct + ;; manipulation with pointers in buffer-undo-list + (setcdr tmp nil) + + (setq buffer-undo-list (delq nil buffer-undo-list)) + (setq buffer-undo-list + (delq vip-buffer-undo-list-mark buffer-undo-list)) + ;; restore tail of buffer-undo-list + (setq buffer-undo-list (nconc buffer-undo-list tmp2))) + (setq buffer-undo-list (delq nil buffer-undo-list)))))) + + +(defun vip-set-complex-command-for-undo () + (if (listp buffer-undo-list) + (if (not vip-undo-needs-adjustment) + (let ((inhibit-quit t)) + (setq buffer-undo-list + (cons vip-buffer-undo-list-mark buffer-undo-list)) + (setq vip-undo-needs-adjustment t))))) + + + + +(defun vip-display-current-destructive-command () + (let ((text (nth 4 vip-d-com)) + (keys (nth 5 vip-d-com)) + (max-text-len 30)) + + (setq this-command 'vip-display-current-destructive-command) + + (message " `.' runs %s%s" + (concat "`" (vip-array-to-string keys) "'") + (vip-abbreviate-string text max-text-len + " inserting `" "'" " .......")) + )) + + +;; don't change vip-d-com if it was vip-repeat command invoked with `.' +;; or in some other way (non-interactively). +(defun vip-set-destructive-command (list) + (or (eq vip-intermediate-command 'vip-repeat) + (progn + (setq vip-d-com list) + (setcar (nthcdr 5 vip-d-com) + (vip-array-to-string (this-command-keys))) + (vip-push-onto-ring vip-d-com 'vip-command-ring)))) + +(defun vip-prev-destructive-command (next) + "Find previous destructive command in the history of destructive commands. +With prefix argument, find next destructive command." + (interactive "P") + (let (cmd vip-intermediate-command) + (if (eq last-command 'vip-display-current-destructive-command) + ;; repeated search through command history + (setq vip-intermediate-command 'repeating-display-destructive-command) + ;; first search through command history--set temp ring + (setq vip-temp-command-ring (copy-list vip-command-ring))) + (setq cmd (if next + (vip-special-ring-rotate1 vip-temp-command-ring 1) + (vip-special-ring-rotate1 vip-temp-command-ring -1))) + (if (null cmd) + () + (setq vip-d-com cmd)) + (vip-display-current-destructive-command))) + +(defun vip-next-destructive-command () + "Find next destructive command in the history of destructive commands." + (interactive) + (vip-prev-destructive-command 'next)) + +(defun vip-insert-prev-from-insertion-ring (arg) + "Cycles through insertion ring in the direction of older insertions. +Undoes previous insertion and inserts new. +With prefix argument, cycles in the direction of newer elements. +In minibuffer, this command executes whatever the invocation key is bound +to in the global map, instead of cycling through the insertion ring." + (interactive "P") + (let (vip-intermediate-command) + (if (eq last-command 'vip-insert-from-insertion-ring) + (progn ; repeated search through insertion history + (setq vip-intermediate-command 'repeating-insertion-from-ring) + (if (eq vip-current-state 'replace-state) + (undo 1) + (if vip-last-inserted-string-from-insertion-ring + (backward-delete-char + (length vip-last-inserted-string-from-insertion-ring)))) + ) + ;;first search through insertion history + (setq vip-temp-insertion-ring (copy-list vip-insertion-ring))) + (setq this-command 'vip-insert-from-insertion-ring) + ;; so that things will be undone properly + (setq buffer-undo-list (cons nil buffer-undo-list)) + (setq vip-last-inserted-string-from-insertion-ring + (vip-special-ring-rotate1 vip-temp-insertion-ring (if arg 1 -1))) + + ;; this change of vip-intermediate-command must come after + ;; vip-special-ring-rotate1, so that the ring will rotate, but before the + ;; insertion. + (setq vip-intermediate-command nil) + (if vip-last-inserted-string-from-insertion-ring + (insert vip-last-inserted-string-from-insertion-ring)) + )) + +(defun vip-insert-next-from-insertion-ring () + "Cycles through insertion ring in the direction of older insertions. Undoes previous insertion and inserts new." + (interactive) + (vip-insert-prev-from-insertion-ring 'next)) + + +;; some region utilities + +(defun vip-add-newline-at-eob-if-necessary () + "If at the last line of buffer, add \\n before eob, if newline is missing." + (save-excursion + (end-of-line) + ;; make sure all lines end with newline, unless in the minibuffer or + ;; when requested otherwise (vip-add-newline-at-eob is nil) + (if (and + (eobp) + (not (bolp)) + vip-add-newline-at-eob + (not (vip-is-in-minibuffer))) + (insert "\n")))) + +(defun vip-yank-defun () + (mark-defun) + (copy-region-as-kill (point) (mark t))) + +(defun vip-enlarge-region (beg end) + "Enlarge region between BEG and END." + (or beg (setq beg end)) ; if beg is nil, set to end + (or end (setq end beg)) ; if end is nil, set to beg + + (if (< beg end) + (progn (goto-char beg) (set-mark end)) + (goto-char end) + (set-mark beg)) + (beginning-of-line) + (exchange-point-and-mark) + (if (or (not (eobp)) (not (bolp))) (forward-line 1)) + (if (not (eobp)) (beginning-of-line)) + (if (> beg end) (exchange-point-and-mark))) + + +(defun vip-quote-region () + "Quote region by each line with a user supplied string." + (setq vip-quote-string + (vip-read-string-with-history + "Quote string: " + nil + 'vip-quote-region-history + vip-quote-string)) + (vip-enlarge-region (point) (mark t)) + (if (> (point) (mark t)) (exchange-point-and-mark)) + (insert vip-quote-string) + (beginning-of-line) + (forward-line 1) + (while (and (< (point) (mark t)) (bolp)) + (insert vip-quote-string) + (beginning-of-line) + (forward-line 1))) + + +;; Tells whether BEG is on the same line as END. +;; If one of the args is nil, it'll return nil. +(defun vip-same-line (beg end) + (let ((selective-display nil)) + (cond ((and beg end) + ;; This 'if' is needed because Emacs treats the next empty line + ;; as part of the previous line. + (if (or (> beg (point-max)) (> end (point-max))) ; out of range + () + (if (and (> end beg) (= (vip-line-pos 'start) end)) + (setq end (min (1+ end) (point-max)))) + (if (and (> beg end) (= (vip-line-pos 'start) beg)) + (setq beg (min (1+ beg) (point-max)))) + (<= (count-lines beg end) 1) )) + + (t nil)) + )) + + +;; Check if the string ends with a newline. +(defun vip-end-with-a-newline-p (string) + (or (string= string "") + (= (vip-seq-last-elt string) ?\n))) + +(defun vip-tmp-insert-at-eob (msg) + (let ((savemax (point-max))) + (goto-char savemax) + (insert msg) + (sit-for 2) + (goto-char savemax) (delete-region (point) (point-max)) + )) + + + +;;; Minibuffer business + +(defsubst vip-set-minibuffer-style () + (add-hook 'minibuffer-setup-hook 'vip-minibuffer-setup-sentinel)) + + +(defun vip-minibuffer-setup-sentinel () + (let ((hook (if vip-vi-style-in-minibuffer + 'vip-change-state-to-insert + 'vip-change-state-to-emacs))) + (funcall hook) + + ;; Make sure the minibufer overlay is kept up-to-date. In XEmacs also + ;; guards against the possibility of detaching this overlay. + (add-hook 'vip-post-command-hooks 'vip-move-minibuffer-overlay) + )) + +;; Interpret last event in the local map +(defun vip-exit-minibuffer () + (interactive) + (let (command) + (setq command (local-key-binding (char-to-string last-command-char))) + (if command + (command-execute command) + (exit-minibuffer)))) + + +(defun vip-set-search-face () + (if (not window-system) + () + (defvar vip-search-face + (progn + (make-face 'vip-search-face) + (or (face-differs-from-default-p 'vip-search-face) + ;; face wasn't set in .vip or .Xdefaults + (if (vip-can-use-colors "Black" "khaki") + (progn + (set-face-background 'vip-search-face "khaki") + (set-face-foreground 'vip-search-face "Black")) + (copy-face 'italic 'vip-search-face) + (set-face-underline-p 'vip-search-face t))) + 'vip-search-face) + "*Face used to flash out the search pattern.") + )) + + +(defun vip-set-minibuffer-faces () + (if (not window-system) + () + (defvar vip-minibuffer-emacs-face + (progn + (make-face 'vip-minibuffer-emacs-face) + (or (face-differs-from-default-p 'vip-minibuffer-emacs-face) + ;; face wasn't set in .vip or .Xdefaults + (if vip-vi-style-in-minibuffer + ;; emacs state is an exception in the minibuffer + (if (vip-can-use-colors "darkseagreen2" "Black") + (progn + (set-face-background + 'vip-minibuffer-emacs-face "darkseagreen2") + (set-face-foreground + 'vip-minibuffer-emacs-face "Black")) + (copy-face 'highlight 'vip-minibuffer-emacs-face)) + ;; emacs state is the main state in the minibuffer + (if (vip-can-use-colors "Black" "pink") + (progn + (set-face-background 'vip-minibuffer-emacs-face "pink") + (set-face-foreground + 'vip-minibuffer-emacs-face "Black")) + (copy-face 'italic 'vip-minibuffer-emacs-face)) + )) + 'vip-minibuffer-emacs-face) + "Face used in the Minibuffer when it is in Emacs state.") + + (defvar vip-minibuffer-insert-face + (progn + (make-face 'vip-minibuffer-insert-face) + (or (face-differs-from-default-p 'vip-minibuffer-insert-face) + (if vip-vi-style-in-minibuffer + (if (vip-can-use-colors "Black" "pink") + (progn + (set-face-background 'vip-minibuffer-insert-face "pink") + (set-face-foreground + 'vip-minibuffer-insert-face "Black")) + (copy-face 'italic 'vip-minibuffer-insert-face)) + ;; If Insert state is an exception + (if (vip-can-use-colors "darkseagreen2" "Black") + (progn + (set-face-background + 'vip-minibuffer-insert-face "darkseagreen2") + (set-face-foreground + 'vip-minibuffer-insert-face "Black")) + (copy-face 'highlight 'vip-minibuffer-insert-face)) + (vip-italicize-face 'vip-minibuffer-insert-face))) + 'vip-minibuffer-insert-face) + "Face used in the Minibuffer when it is in Insert state.") + + (defvar vip-minibuffer-vi-face + (progn + (make-face 'vip-minibuffer-vi-face) + (or (face-differs-from-default-p 'vip-minibuffer-vi-face) + (if vip-vi-style-in-minibuffer + (if (vip-can-use-colors "Black" "grey") + (progn + (set-face-background 'vip-minibuffer-vi-face "grey") + (set-face-foreground 'vip-minibuffer-vi-face "Black")) + (copy-face 'bold 'vip-minibuffer-vi-face)) + (copy-face 'bold 'vip-minibuffer-vi-face) + (invert-face 'vip-minibuffer-vi-face))) + 'vip-minibuffer-vi-face) + "Face used in the Minibuffer when it is in Vi state.") + + ;; the current face used in the minibuffer + (vip-deflocalvar vip-minibuffer-current-face vip-minibuffer-emacs-face "") + )) + + + +;;; Reading string with history + +(defun vip-read-string-with-history (prompt &optional initial + history-var default keymap) + ;; Reads string, prompting with PROMPT and inserting the INITIAL + ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the + ;; input is an empty string. Uses KEYMAP, if given, or the + ;; minibuffer-local-map. + ;; Default value is displayed until the user types something in the + ;; minibuffer. + (let ((minibuffer-setup-hook + '(lambda () + (if (stringp initial) + (progn + (sit-for 840) + (erase-buffer) + (insert initial))) + (vip-minibuffer-setup-sentinel))) + (val "") + (padding "") + temp-msg) + + (setq keymap (or keymap minibuffer-local-map) + initial (or initial "") + temp-msg (if default + (format "(default: %s) " default) + "")) + + (setq vip-incomplete-ex-cmd nil) + (setq val (read-from-minibuffer prompt + (concat temp-msg initial val padding) + keymap nil history-var)) + (setq minibuffer-setup-hook nil + padding (vip-array-to-string (this-command-keys)) + temp-msg "") + ;; the following overcomes a glaring bug in history handling + ;; in XEmacs 19.11 + (if (not (string= val (car (eval history-var)))) + (set history-var (cons val (eval history-var)))) + (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var))) + (string= (nth 0 (eval history-var)) "")) + (set history-var (cdr (eval history-var)))) + (if (string= val "") + (or default "") + val))) + + + +;; insertion commands + +;; Called when state changes from Insert Vi command mode. +;; Repeats the insertion command if Insert state was entered with prefix +;; argument > 1. +(defun vip-repeat-insert-command () + (let ((i-com (car vip-d-com)) + (val (nth 1 vip-d-com)) + (char (nth 2 vip-d-com))) + (if (and val (> val 1)) ; first check that val is non-nil + (progn + (setq vip-d-com (list i-com (1- val) ?r nil nil nil)) + (vip-repeat nil) + (setq vip-d-com (list i-com val char nil nil nil)) + )))) + +(defun vip-insert (arg) + "Insert before point." + (interactive "P") + (vip-set-complex-command-for-undo) + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (vip-set-destructive-command (list 'vip-insert val ?r nil nil nil)) + (if com + (vip-loop val (vip-yank-last-insertion)) + (vip-change-state-to-insert)))) + +(defun vip-append (arg) + "Append after point." + (interactive "P") + (vip-set-complex-command-for-undo) + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (vip-set-destructive-command (list 'vip-append val ?r nil nil nil)) + (if (not (eolp)) (forward-char)) + (if (equal com ?r) + (vip-loop val (vip-yank-last-insertion)) + (vip-change-state-to-insert)))) + +(defun vip-Append (arg) + "Append at end of line." + (interactive "P") + (vip-set-complex-command-for-undo) + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (vip-set-destructive-command (list 'vip-Append val ?r nil nil nil)) + (end-of-line) + (if (equal com ?r) + (vip-loop val (vip-yank-last-insertion)) + (vip-change-state-to-insert)))) + +(defun vip-Insert (arg) + "Insert before first non-white." + (interactive "P") + (vip-set-complex-command-for-undo) + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (vip-set-destructive-command (list 'vip-Insert val ?r nil nil nil)) + (back-to-indentation) + (if (equal com ?r) + (vip-loop val (vip-yank-last-insertion)) + (vip-change-state-to-insert)))) + +(defun vip-open-line (arg) + "Open line below." + (interactive "P") + (vip-set-complex-command-for-undo) + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (vip-set-destructive-command (list 'vip-open-line val ?r nil nil nil)) + (let ((col (current-indentation))) + (if (equal com ?r) + (vip-loop val + (progn + (end-of-line) + (newline 1) + (if vip-auto-indent + (progn (setq vip-cted t) (indent-to col))) + (vip-yank-last-insertion))) + (end-of-line) + (newline 1) + (if vip-auto-indent (progn (setq vip-cted t) (indent-to col))) + (vip-change-state-to-insert) + )))) + +(defun vip-Open-line (arg) + "Open line above." + (interactive "P") + (vip-set-complex-command-for-undo) + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (vip-set-destructive-command (list 'vip-Open-line val ?r nil nil nil)) + (let ((col (current-indentation))) + (if (equal com ?r) + (vip-loop val + (progn + (beginning-of-line) + (open-line 1) + (if vip-auto-indent + (progn (setq vip-cted t) (indent-to col))) + (vip-yank-last-insertion))) + (beginning-of-line) + (open-line 1) + (if vip-auto-indent (progn (setq vip-cted t) (indent-to col))) + (vip-change-state-to-insert))))) + +(defun vip-open-line-at-point (arg) + "Open line at point." + (interactive "P") + (vip-set-complex-command-for-undo) + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (vip-set-destructive-command + (list 'vip-open-line-at-point val ?r nil nil nil)) + (if (equal com ?r) + (vip-loop val + (progn + (open-line 1) + (vip-yank-last-insertion))) + (open-line 1) + (vip-change-state-to-insert)))) + +(defun vip-substitute (arg) + "Substitute characters." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (push-mark nil t) + (forward-char val) + (if (equal com ?r) + (vip-change-subr (mark t) (point)) + (vip-change (mark t) (point))) + (vip-set-destructive-command (list 'vip-substitute val ?r nil nil nil)) + )) + +(defun vip-substitute-line (arg) + "Substitute lines." + (interactive "p") + (vip-set-complex-command-for-undo) + (vip-line (cons arg ?C))) + +;; Prepare for replace +(defun vip-start-replace () + (setq vip-began-as-replace t + vip-sitting-in-replace t + vip-replace-chars-to-delete 0 + vip-replace-chars-deleted 0) + (add-hook 'vip-after-change-functions 'vip-replace-mode-spy-after t) + (add-hook 'vip-before-change-functions 'vip-replace-mode-spy-before t) + ;; this will get added repeatedly, but no harm + (add-hook 'after-change-functions 'vip-after-change-sentinel t) + (add-hook 'before-change-functions 'vip-before-change-sentinel t) + (vip-move-marker-locally 'vip-last-posn-in-replace-region + (vip-replace-start)) + (add-hook 'vip-post-command-hooks 'vip-replace-state-post-command-sentinel t) + (add-hook 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t) + ) + +;; Runs vip-after-change-functions inside after-change-functions +(defun vip-after-change-sentinel (beg end len) + (let ((list vip-after-change-functions)) + (while list + (funcall (car list) beg end len) + (setq list (cdr list))))) + +;; Runs vip-before-change-functions inside before-change-functions +(defun vip-before-change-sentinel (beg end) + (let ((list vip-before-change-functions)) + (while list + (funcall (car list) beg end) + (setq list (cdr list))))) + +(defun vip-post-command-sentinel () + (run-hooks 'vip-post-command-hooks)) + +(defun vip-pre-command-sentinel () + (run-hooks 'vip-pre-command-hooks)) + +;; Needed so that Viper will be able to figure the last inserted +;; chunk of text with reasonable accuracy. +(defun vip-insert-state-post-command-sentinel () + (if (and (memq vip-current-state '(insert-state replace-state)) + vip-insert-point + (>= (point) vip-insert-point)) + (setq vip-last-posn-while-in-insert-state (point-marker))) + (if (and (eq this-command 'dabbrev-expand) + (integerp vip-pre-command-point) + (> vip-insert-point vip-pre-command-point)) + (move-marker vip-insert-point vip-pre-command-point)) + ) + +(defun vip-insert-state-pre-command-sentinel () + (if (and (eq this-command 'dabbrev-expand) + (markerp vip-insert-point) + (marker-position vip-insert-point)) + (setq vip-pre-command-point (marker-position vip-insert-point)))) + +(defun vip-R-state-post-command-sentinel () + ;; This is needed despite vip-replace-state-pre-command-sentinel + ;; When you jump to another buffer in another frame, the pre-command + ;; hook won't change cursor color to default in that other frame. + ;; So, if the second frame cursor was red and we set the point + ;; outside the replacement region, then the cursor color woll remain + ;; red. Restoring the default, below, prevents this. + (vip-restore-cursor-color) + (if (and (<= (vip-replace-start) (point)) + (<= (point) (vip-replace-end))) + (vip-change-cursor-color vip-replace-overlay-cursor-color))) + +(defun vip-replace-state-pre-command-sentinel () + (vip-restore-cursor-color)) + +(defun vip-replace-state-post-command-sentinel () + ;; This is needed despite vip-replace-state-pre-command-sentinel + ;; When you jump to another buffer in another frame, the pre-command + ;; hook won't change cursor color to default in that other frame. + ;; So, if the second frame cursor was red and we set the point + ;; outside the replacement region, then the cursor color woll remain + ;; red. Restoring the default, below, prevents this. + (vip-restore-cursor-color) + (cond + ((eq vip-current-state 'replace-state) + ;; delete characters to compensate for inserted chars. + (let ((replace-boundary + ;; distinguish empty repl-reg-end-symbol from non-empty + (- (vip-replace-end) + (if (eq (length vip-replace-region-end-symbol) 0) + 0 1))) + ) + + (save-excursion + (goto-char vip-last-posn-in-replace-region) + (delete-char vip-replace-chars-to-delete) + (setq vip-replace-chars-to-delete 0 + vip-replace-chars-deleted 0) + ;; terminate replace mode if reached replace limit + (if (= vip-last-posn-in-replace-region + (vip-replace-end)) + (vip-finish-change vip-last-posn-in-replace-region))) + + (if (and (<= (vip-replace-start) (point)) + (<= (point) replace-boundary)) + (progn + ;; the state may have changed in vip-finish-change above + (if (eq vip-current-state 'replace-state) + (vip-change-cursor-color vip-replace-overlay-cursor-color)) + (setq vip-last-posn-in-replace-region (point-marker)))) + )) + + (t ;; terminate replace mode if changed Viper states. + (vip-finish-change vip-last-posn-in-replace-region))) + ) + + +;; checks how many chars were deleted by the last change +(defun vip-replace-mode-spy-before (beg end) + (setq vip-replace-chars-deleted (- end beg + (max 0 (- end (vip-replace-end))) + (max 0 (- (vip-replace-start) beg)) + )) + ) + +;; Invoked as an after-change-function to set up parameters of the last change +(defun vip-replace-mode-spy-after (beg end length) + (if (memq vip-intermediate-command '(repeating-insertion-from-ring)) + (progn + (setq vip-replace-chars-to-delete 0) + (vip-move-marker-locally + 'vip-last-posn-in-replace-region (point))) + + (let (beg-col end-col real-end chars-to-delete) + (setq real-end (min end (vip-replace-end))) + (save-excursion + (goto-char beg) + (setq beg-col (current-column)) + (goto-char real-end) + (setq end-col (current-column))) + + ;; If beg of change is outside the replacement region, then don't + ;; delete anything in the repl region (set chars-to-delete to 0). + ;; + ;; This works fine except that we have to take special care of + ;; dabbrev-expand. The problem stems from new-dabbrev.el, which + ;; sometimes simply shifts the repl region rightwards, without + ;; deleting an equal amount of characters. + ;; + ;; The reason why new-dabbrev.el causes this are this: + ;; if one dinamically completes a partial word that starts before the + ;; replacement region (but ends inside)then new-dabbrev.el first + ;; moves cursor backwards, to the beginning of the word to be + ;; completed (say, pt A). Then it inserts the + ;; completed word and then deletes the old, incomplete part. + ;; Since the complete word is inserted at position before the repl + ;; region, the next If-statement would have set chars-to-delete to 0 + ;; unless we check for the current command, which must be + ;; dabbrev-expand. + ;; + ;; We should be able deal with these problems in a better way + ;; when emacs will have overlays with sticky back ends. + ;; In fact, it would be also useful to add overlays for insert + ;; regions as well, since this will let us capture the situation when + ;; dabbrev-expand goes back past the insertion point to find the + ;; beginning of the word to be expanded. + (if (or (and (<= (vip-replace-start) beg) + (<= beg (vip-replace-end))) + (and (= length 0) (eq this-command 'dabbrev-expand))) + (setq chars-to-delete + (max (- end-col beg-col) (- real-end beg) 0)) + (setq chars-to-delete 0)) + + ;; if beg = last change position, it means that we are within the + ;; same command that does multiple changes. Moreover, it means + ;; that we have two subsequent changes (insert/delete) that + ;; complement each other. + (if (= beg (marker-position vip-last-posn-in-replace-region)) + (setq vip-replace-chars-to-delete + (- (+ chars-to-delete vip-replace-chars-to-delete) + vip-replace-chars-deleted)) + (setq vip-replace-chars-to-delete chars-to-delete)) + + (vip-move-marker-locally + 'vip-last-posn-in-replace-region + (max (if (> end (vip-replace-end)) (vip-replace-start) end) + (or (marker-position vip-last-posn-in-replace-region) + (vip-replace-start)) + )) + + (setq vip-replace-chars-to-delete + (max 0 (min vip-replace-chars-to-delete + (- (vip-replace-end) + vip-last-posn-in-replace-region)))) + ))) + + +;; Delete stuff between posn and the end of vip-replace-overlay-marker, if +;; posn is within the overlay. +(defun vip-finish-change (posn) + (remove-hook 'vip-after-change-functions 'vip-replace-mode-spy-after) + (remove-hook 'vip-before-change-functions 'vip-replace-mode-spy-before) + (remove-hook 'vip-post-command-hooks + 'vip-replace-state-post-command-sentinel) + (remove-hook 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel) + (vip-restore-cursor-color) + (setq vip-sitting-in-replace nil) ; just in case we'll need to know it + (save-excursion + (if (and + vip-replace-overlay + (>= posn (vip-replace-start)) + (< posn (vip-replace-end))) + (delete-region posn (vip-replace-end))) + ) + + (if (eq vip-current-state 'replace-state) + (vip-downgrade-to-insert)) + ;; replace mode ended => nullify vip-last-posn-in-replace-region + (vip-move-marker-locally 'vip-last-posn-in-replace-region nil) + (vip-hide-replace-overlay) + (vip-refresh-mode-line) + (vip-put-string-on-kill-ring vip-last-replace-region) + ) + +(defun vip-put-string-on-kill-ring (string) + "Make STRING be the first element of the kill ring." + (setq kill-ring (cons string kill-ring)) + (if (> (length kill-ring) kill-ring-max) + (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)) + (setq kill-ring-yank-pointer kill-ring)) + +(defun vip-finish-R-mode () + (remove-hook 'vip-post-command-hooks 'vip-R-state-post-command-sentinel) + (remove-hook 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel) + (vip-downgrade-to-insert)) + +(defun vip-start-R-mode () + ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number + (overwrite-mode 1) + (add-hook 'vip-post-command-hooks 'vip-R-state-post-command-sentinel t) + (add-hook 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t) + ) + + + +(defun vip-replace-state-exit-cmd () + "Binding for keys that cause Replace state to switch to Vi or to Insert. +These keys are ESC, RET, and LineFeed" + (interactive) + (if overwrite-mode ;; If you are in replace mode invoked via 'R' + (vip-finish-R-mode) + (vip-finish-change vip-last-posn-in-replace-region)) + (let (com) + (if (eq this-command 'vip-intercept-ESC-key) + (setq com 'vip-exit-insert-state) + (vip-set-unread-command-events last-input-char) + (setq com (key-binding (read-key-sequence nil)))) + + (condition-case conds + (command-execute com) + (error + (vip-message-conditions conds))) + ) + (vip-hide-replace-overlay)) + + +(defun vip-overwrite (arg) +"This is the function bound to 'R'---unlimited replace. +Similar to Emacs's own overwrite-mode." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getcom arg)) (len)) + (vip-set-destructive-command (list 'vip-overwrite val ?r nil nil nil)) + (if com + (progn + ;; Viper saves inserted text in vip-last-insertion + (setq len (length vip-last-insertion)) + (delete-char len) + (vip-loop val (vip-yank-last-insertion))) + (setq last-command 'vip-overwrite) + (vip-set-complex-command-for-undo) + (vip-set-replace-overlay (point) (vip-line-pos 'end)) + (vip-change-state-to-replace) + ))) + + +;; line commands + +(defun vip-line (arg) + (let ((val (car arg)) + (com (cdr arg))) + (vip-move-marker-locally 'vip-com-point (point)) + (if (not (eobp)) + (next-line (1- val))) + ;; this ensures that dd, cc, D, yy will do the right thing on the last + ;; line of buffer when this line has no \n. + (vip-add-newline-at-eob-if-necessary) + (vip-execute-com 'vip-line val com)) + (if (and (eobp) (not (bobp))) (forward-line -1)) + ) + +(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 commands + +(defun vip-region (arg) + (interactive "P") + (let ((val (vip-P-val arg)) + (com (vip-getcom arg))) + (vip-move-marker-locally '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))) + (vip-move-marker-locally '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") + (if (and (eolp) (bolp)) (error "I see no character to replace here")) + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (vip-replace-char-subr (if (equal com ?r) vip-d-char (read-char)) val) + (if (and (eolp) (not (bolp))) (forward-char 1)) + (vip-set-destructive-command + (list 'vip-replace-char val ?r nil vip-d-char nil)) + )) + +(defun vip-replace-char-subr (char arg) + (delete-char arg t) + (setq vip-d-char char) + (vip-loop (if (> arg 0) arg (- arg)) + (if (eq char ?\C-m) (insert "\n") (insert char))) + (backward-char arg)) + +(defun vip-replace-string () + "The old replace string function. +If you supply null string as the string to be replaced, +the query replace mode will toggle between string replace and regexp replace. +This function comes from VIP 3.5 and is not used in Viper. A nostalgic user +can bind it to a key, if necessary." + (interactive) + (let (str) + (setq str (vip-read-string-with-history + (if vip-re-replace "Replace regexp: " "Replace string: ") + nil ; no initial + 'vip-replace1-history + (car vip-replace1-history) ; default + )) + (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-with-history + (format "Replace regexp `%s' with: " str) + nil ; no initial + 'vip-replace2-history + (car vip-replace2-history) ; default + )) + (replace-string + str + (vip-read-string-with-history + (format "Replace `%s' with: " str) + nil ; no initial + 'vip-replace2-history + (car vip-replace2-history) ; default + ))) + ))) + + +;; basic cursor movement. j, k, l, h commands. + +(defun vip-forward-char (arg) + "Move point right ARG characters (left if ARG negative). +On reaching end of line, stop and signal error." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (if vip-ex-style-motion + (progn + ;; the boundary condition check gets weird here because + ;; forward-char may be the parameter of a delete, and 'dl' works + ;; just like 'x' for the last char on a line, so we have to allow + ;; the forward motion before the 'vip-execute-com', but, of + ;; course, 'dl' doesn't work on an empty line, so we have to + ;; catch that condition before 'vip-execute-com' + (if (and (eolp) (bolp)) (error "") (forward-char val)) + (if com (vip-execute-com 'vip-forward-char val com)) + (if (eolp) (progn (backward-char 1) (error "")))) + (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 reaching beginning of line, stop and signal error." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (if vip-ex-style-motion + (progn + (if (bolp) (error "") (backward-char val)) + (if com (vip-execute-com 'vip-backward-char val com))) + (backward-char val) + (if com (vip-execute-com 'vip-backward-char val com))))) + +(defun vip-forward-char-carefully (&optional arg) + "Like forward-char, but doesn't move at end of buffer." + (setq arg (or arg 1)) + (if (>= (point-max) (+ (point) arg)) + (forward-char arg) + (goto-char (point-max)))) + +(defun vip-backward-char-carefully (&optional arg) + "Like backward-char, but doesn't move at end of buffer." + (setq arg (or arg 1)) + (if (<= (point-min) (- (point) arg)) + (backward-char arg) + (goto-char (point-min)))) + + + +;;; Word command + +;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators +;; for word movement. When executed with a destructive command, \n is +;; usually left untouched for the last word. + +;; skip only one \n +(defun vip-skip-separators (forward) + (if forward + (progn + (skip-chars-forward " \t") + (if (looking-at "\n") + (progn + (forward-char) + (skip-chars-forward " \t")))) + (skip-chars-backward " \t") + (backward-char) + (if (looking-at "\n") + (skip-chars-backward " \t") + (forward-char)))) + +(defconst vip-ALPHA "a-zA-Z0-9_") +(defconst vip-ALPHA-B (concat "[" vip-ALPHA "]")) +(defconst vip-NONALPHA (concat "^" vip-ALPHA)) +(defconst vip-NONALPHA-B (concat "[" vip-NONALPHA "]")) +(defconst vip-SEP " \t\n") +(defconst vip-SEP-B (concat "[" vip-SEP "]")) +(defconst vip-NONSEP (concat "^" vip-SEP)) +(defconst vip-NONSEP-B (concat "[" vip-NONSEP "]")) +(defconst vip-ALPHASEP (concat vip-ALPHA vip-SEP)) +(defconst vip-ALPHASEP-B (concat "[" vip-ALPHASEP "]")) +(defconst vip-NONALPHASEP (concat "^" vip-ALPHASEP )) +(defconst vip-NONALPHASEP-B (concat "[" vip-NONALPHASEP "]")) + + +(defun vip-forward-word-kernel (val) + (while (> val 0) + (cond ((looking-at vip-ALPHA-B) + (skip-chars-forward vip-ALPHA) + (vip-skip-separators t)) + ((looking-at vip-SEP-B) + (vip-skip-separators t)) + ((looking-at vip-NONALPHASEP-B) + (skip-chars-forward vip-NONALPHASEP) + (vip-skip-separators t))) + (setq val (1- val)))) + +(defun vip-fwd-skip (pat aux-pat lim) + (if (and (save-excursion + (re-search-backward pat lim t)) + (= (point) (match-end 0))) + (goto-char (match-beginning 0))) + (skip-chars-backward aux-pat lim) + (if (= (point) lim) + (vip-forward-char-carefully)) + ) + + +(defun vip-forward-word (arg) + "Forward word." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (vip-forward-word-kernel val) + (if com (progn + (cond ((memq com (list ?c (- ?c) ?y (- ?y))) + (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point)) + ((vip-dotable-command-p com) + (vip-fwd-skip "\n[ \t]*" "" vip-com-point))) + (vip-execute-com 'vip-forward-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 (vip-move-marker-locally 'vip-com-point (point))) + (vip-loop val + (progn + (skip-chars-forward vip-NONSEP) + (vip-skip-separators t))) + (if com (progn + (cond ((memq com (list ?c (- ?c) ?y (- ?y))) + (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point)) + ((vip-dotable-command-p com) + (vip-fwd-skip "\n[ \t]*" "" vip-com-point))) + (vip-execute-com 'vip-forward-Word val com))))) + + +;; this is a bit different from Vi, but Vi's end of word +;; makes no sense whatsoever +(defun vip-end-of-word-kernel () + (if (vip-end-of-word-p) (forward-char)) + (if (looking-at "[ \t\n]") + (skip-chars-forward vip-SEP)) + + (cond ((looking-at vip-ALPHA-B) (skip-chars-forward vip-ALPHA)) + ((looking-at vip-NONALPHASEP-B) + (skip-chars-forward vip-NONALPHASEP))) + (vip-backward-char-carefully)) + +(defun vip-end-of-word-p () + (if (eobp) t + (save-excursion + (cond ((looking-at vip-ALPHA-B) + (forward-char) + (looking-at vip-NONALPHA-B)) + ((looking-at vip-NONALPHASEP-B) + (forward-char) + (looking-at vip-ALPHASEP-B)))))) + +(defun vip-one-char-word-p () + (let ((step 2)) + (save-excursion + (cond ((looking-at vip-ALPHA-B) + (if (bobp) (setq step 1) (backward-char)) + (if (or (bobp) (looking-at vip-NONALPHA-B)) + (progn + (forward-char step) + (looking-at vip-NONALPHA-B)) + nil)) + ((looking-at vip-NONALPHASEP-B) + (if (bobp) (setq step 1) (backward-char)) + (if (or (bobp) (looking-at vip-ALPHASEP-B)) + (progn + (forward-char step) + (looking-at vip-ALPHASEP-B)) + nil)))))) + +(defun vip-one-char-Word-p () + (and (looking-at vip-NONSEP-B) + (save-excursion + (if (bobp) + t + (backward-char) + (looking-at vip-SEP-B))) + (save-excursion + (forward-char) + (or (eobp) + (looking-at vip-SEP-B))))) + +(defun vip-end-of-word (arg &optional careful) + "Move point to end of current word." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (vip-loop val (vip-end-of-word-kernel)) + (if com + (progn + (forward-char) + (vip-execute-com 'vip-end-of-word val com))))) + +(defun vip-end-of-Word (arg) + "Forward to end of word delimited by white character." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (vip-loop val + (progn + (vip-end-of-word-kernel) + (if (not (re-search-forward + vip-SEP-B nil t 1)) + (goto-char (point-max))) + (skip-chars-backward vip-SEP) + (backward-char))) + (if com + (progn + (forward-char) + (vip-execute-com 'vip-end-of-Word val com))))) + +(defun vip-backward-word-kernel (val) + (while (> val 0) + (backward-char) + (cond ((looking-at vip-ALPHA-B) + (skip-chars-backward vip-ALPHA)) + ((looking-at vip-SEP-B) + (forward-char) + (vip-skip-separators nil) + (backward-char) + (cond ((looking-at vip-ALPHA-B) + (skip-chars-backward vip-ALPHA)) + ((looking-at vip-NONALPHASEP-B) + (skip-chars-backward vip-NONALPHASEP)) + (t (forward-char)))) + ((looking-at vip-NONALPHASEP-B) + (skip-chars-backward vip-NONALPHASEP))) + (setq val (1- val)))) + +(defun vip-backward-word (arg) + "Backward word." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (if com + (let (i) + (if (setq i (save-excursion (backward-char) (looking-at "\n"))) + (backward-char)) + (vip-move-marker-locally 'vip-com-point (point)) + (if i (forward-char)))) + (vip-backward-word-kernel val) + (if com (vip-execute-com 'vip-backward-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 + (let (i) + (if (setq i (save-excursion (backward-char) (looking-at "\n"))) + (backward-char)) + (vip-move-marker-locally 'vip-com-point (point)) + (if i (forward-char)))) + (vip-loop val + (progn + (vip-skip-separators nil) + (skip-chars-backward vip-NONSEP))) + (if com (vip-execute-com 'vip-backward-Word val com)))) + + + +;; line commands + +(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 (vip-move-marker-locally '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 (vip-move-marker-locally 'vip-com-point (point))) + (forward-to-indentation (1- val)) + (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 (vip-move-marker-locally 'vip-com-point (point))) + (end-of-line val) + (if com (vip-execute-com 'vip-goto-eol val com)) + (if vip-ex-style-motion + (if (and (eolp) (not (bolp)) + ;; a fix for vip-change-to-eol + (not (equal vip-current-state 'insert-state))) + (backward-char 1) + )))) + + +(defun vip-goto-col (arg) + "Go to ARG's column." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (save-excursion + (end-of-line) + (if (> val (1+ (current-column))) (error ""))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (beginning-of-line) + (forward-char (1- val)) + (if com (vip-execute-com 'vip-goto-col 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 (vip-move-marker-locally 'vip-com-point (point))) + (next-line val) + (if vip-ex-style-motion + (if (and (eolp) (not (bolp))) (backward-char 1))) + (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") + (save-excursion + (end-of-line) + (if (eobp) (error "Last line in buffer"))) + (let ((val (vip-p-val arg)) + (com (vip-getCom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (forward-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 (vip-move-marker-locally 'vip-com-point (point))) + (previous-line val) + (if vip-ex-style-motion + (if (and (eolp) (not (bolp))) (backward-char 1))) + (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") + (save-excursion + (beginning-of-line) + (if (bobp) (error "First line in buffer"))) + (let ((val (vip-p-val arg)) + (com (vip-getCom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (forward-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))) + +(defun vip-erase-line (arg) + "Erase line." + (interactive "P") + (vip-beginning-of-line (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))) + (vip-move-marker-locally 'vip-com-point (point)) + (vip-deactivate-mark) + (push-mark nil t) + (if (null val) + (goto-char (point-max)) + (goto-char (point-min)) + (forward-line (1- val))) + (if (and (eobp) (bolp) (not (bobp))) (forward-line -1)) + (back-to-indentation) + (if com (vip-execute-com 'vip-goto-line val com)))) + +(defun vip-find-char (arg char forward offset) + "Find ARG's occurrence of CHAR on the current line. +If FORWARD then search is forward, otherwise backward. OFFSET is used to +adjust point after search." + (or (char-or-string-p char) (error "")) + (let ((arg (if forward arg (- arg))) + (cmd (if (eq vip-intermediate-command 'vip-repeat) + (nth 5 vip-d-com) + (vip-array-to-string (this-command-keys)))) + point) + (save-excursion + (save-restriction + (if (> arg 0) + (narrow-to-region + ;; forward search begins here + (if (eolp) (error "Command `%s': At end of line" cmd) (point)) + ;; forward search ends here + (progn (end-of-line) (point))) + (narrow-to-region + ;; backward search begins from here + (if (bolp) + (error "Command `%s': At beginning of line" cmd) (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))) + (if (let ((case-fold-search nil)) + (search-forward (char-to-string char) nil 0 arg)) + (setq point (point)) + (error "Command `%s': `%c' not found" cmd char)))) + (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 find from the terminal, and if +called from vip-repeat, the char last used is used. 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) + ;; vip-repeat --- set vip-F-char from command-keys + (setq vip-F-char (if (stringp (nth 5 vip-d-com)) + (vip-seq-last-elt (nth 5 vip-d-com)) + vip-F-char) + vip-f-char vip-F-char) + (setq val (- val))) + (if com (vip-move-marker-locally '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) + ;; vip-repeat --- set vip-F-char from command-keys + (setq vip-F-char (if (stringp (nth 5 vip-d-com)) + (vip-seq-last-elt (nth 5 vip-d-com)) + vip-F-char) + vip-f-char vip-F-char) + (setq val (- val))) + (if com (vip-move-marker-locally '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 + (setq vip-F-char vip-f-char) ; set new vip-F-char + (forward-char) + (vip-execute-com 'vip-goto-char-forward val com))))) + +(defun vip-find-char-backward (arg) + "Find char ARG on line backward." + (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 nil + vip-f-offset nil) + ;; vip-repeat --- set vip-F-char from command-keys + (setq vip-F-char (if (stringp (nth 5 vip-d-com)) + (vip-seq-last-elt (nth 5 vip-d-com)) + vip-F-char) + vip-f-char vip-F-char) + (setq val (- val))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (vip-find-char + val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil nil) + (setq val (- val)) + (if com + (progn + (setq vip-F-char vip-f-char) ; set new vip-F-char + (vip-execute-com 'vip-find-char-backward val com))))) + +(defun vip-goto-char-backward (arg) + "Go up to char ARG backward 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 nil + vip-f-offset t) + ;; vip-repeat --- set vip-F-char from command-keys + (setq vip-F-char (if (stringp (nth 5 vip-d-com)) + (vip-seq-last-elt (nth 5 vip-d-com)) + vip-F-char) + vip-f-char vip-F-char) + (setq val (- val))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil t) + (setq val (- val)) + (if com + (progn + (setq vip-F-char vip-f-char) ; set new vip-F-char + (vip-execute-com 'vip-goto-char-backward val com))))) + +(defun vip-repeat-find (arg) + "Repeat previous find command." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (vip-deactivate-mark) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (vip-find-char val vip-f-char vip-f-forward vip-f-offset) + (if com + (progn + (if vip-f-forward (forward-char)) + (vip-execute-com 'vip-repeat-find val com))))) + +(defun vip-repeat-find-opposite (arg) + "Repeat previous find command in the opposite direction." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (vip-deactivate-mark) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (vip-find-char val vip-f-char (not vip-f-forward) vip-f-offset) + (if com + (progn + (if vip-f-forward (forward-char)) + (vip-execute-com 'vip-repeat-find-opposite val com))))) + + +;; window scrolling etc. + +(defun vip-other-window (arg) + "Switch to other window." + (interactive "p") + (other-window arg) + (or (not (eq vip-current-state 'emacs-state)) + (string= (buffer-name (current-buffer)) " *Minibuf-1*") + (vip-change-state-to-vi))) + +(defun vip-window-top (arg) + "Go to home window line." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getCom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (push-mark nil t) + (move-to-window-line (1- val)) + (if (not com) (back-to-indentation)) + (if com (vip-execute-com 'vip-window-top val com)))) + +(defun vip-window-middle (arg) + "Go to middle window line." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getCom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (push-mark nil t) + (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val))) + (if (not com) (back-to-indentation)) + (if com (vip-execute-com 'vip-window-middle val com)))) + +(defun vip-window-bottom (arg) + "Go to last window line." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getCom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (push-mark nil t) + (move-to-window-line (- val)) + (if (not com) (back-to-indentation)) + (if com (vip-execute-com 'vip-window-bottom val com)))) + +(defun vip-line-to-top (arg) + "Put current line on the home line." + (interactive "p") + (recenter (1- arg))) + +(defun vip-line-to-middle (arg) + "Put current line on the middle line." + (interactive "p") + (recenter (+ (1- arg) (/ (1- (window-height)) 2)))) + +(defun vip-line-to-bottom (arg) + "Put current line on the last line." + (interactive "p") + (recenter (- (window-height) (1+ arg)))) + + +;; paren match +;; must correct this to only match ( to ) etc. On the other hand +;; it is good that paren match gets confused, because that way you +;; catch _all_ imbalances. + +(defun vip-paren-match (arg) + "Go to the matching parenthesis." + (interactive "P") + (let ((com (vip-getcom arg))) + (if (numberp arg) + (if (or (> arg 99) (< arg 1)) + (error "Prefix must be between 1 and 99") + (goto-char + (if (> (point-max) 80000) + (* (/ (point-max) 100) arg) + (/ (* (point-max) arg) 100))) + (back-to-indentation)) + (let (lim) + (if (and (eolp) (not (bolp))) (forward-char -1)) + (save-excursion + (end-of-line) + (setq lim (point))) + (if (re-search-forward "[][(){}]" lim t) + (backward-char) + (error "No matching character on line"))) + (cond ((looking-at "[\(\[{]") + (if com (vip-move-marker-locally 'vip-com-point (point))) + (forward-sexp 1) + (if com + (vip-execute-com 'vip-paren-match nil com) + (backward-char))) + ((looking-at "[])}]") + (forward-char) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (backward-sexp 1) + (if com (vip-execute-com 'vip-paren-match nil com))) + (t (error "")))))) + + +;; sentence ,paragraph and heading + +(defun vip-forward-sentence (arg) + "Forward sentence." + (interactive "P") + (push-mark nil t) + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (forward-sentence val) + (if com (vip-execute-com 'vip-forward-sentence nil com)))) + +(defun vip-backward-sentence (arg) + "Backward sentence." + (interactive "P") + (push-mark nil t) + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (backward-sentence val) + (if com (vip-execute-com 'vip-backward-sentence nil com)))) + +(defun vip-forward-paragraph (arg) + "Forward paragraph." + (interactive "P") + (push-mark nil t) + (let ((val (vip-p-val arg)) + (com (vip-getCom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (forward-paragraph val) + (if com (vip-execute-com 'vip-forward-paragraph nil com)))) + +(defun vip-backward-paragraph (arg) + "Backward paragraph." + (interactive "P") + (push-mark nil t) + (let ((val (vip-p-val arg)) + (com (vip-getCom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (backward-paragraph val) + (if com (vip-execute-com 'vip-backward-paragraph nil com)))) + +;; should be mode-specific etc. + +(defun vip-prev-heading (arg) + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getCom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (re-search-backward vip-heading-start nil t val) + (goto-char (match-beginning 0)) + (if com (vip-execute-com 'vip-prev-heading nil com)))) + +(defun vip-heading-end (arg) + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getCom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (re-search-forward vip-heading-end nil t val) + (goto-char (match-beginning 0)) + (if com (vip-execute-com 'vip-heading-end nil com)))) + +(defun vip-next-heading (arg) + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getCom arg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (end-of-line) + (re-search-forward vip-heading-start nil t val) + (goto-char (match-beginning 0)) + (if com (vip-execute-com 'vip-next-heading nil com)))) + + +;; scrolling + +(setq scroll-step 1) + +(defun vip-scroll (arg) + "Scroll to next screen." + (interactive "p") + (if (> arg 0) + (while (> arg 0) + (scroll-up) + (setq arg (1- arg))) + (while (> 0 arg) + (scroll-down) + (setq arg (1+ arg))))) + +(defun vip-scroll-back (arg) + "Scroll to previous screen." + (interactive "p") + (vip-scroll (- arg))) + +(defun vip-scroll-down (arg) + "Pull down half screen." + (interactive "P") + (condition-case nil + (if (null arg) + (scroll-down (/ (window-height) 2)) + (scroll-down arg)) + (error (beep 1) + (message "Beginning of buffer") + (goto-char (point-min))))) + +(defun vip-scroll-down-one (arg) + "Scroll up one line." + (interactive "p") + (scroll-down arg)) + +(defun vip-scroll-up (arg) + "Pull up half screen." + (interactive "P") + (condition-case nil + (if (null arg) + (scroll-up (/ (window-height) 2)) + (scroll-up arg)) + (error (beep 1) + (message "End of buffer") + (goto-char (point-max))))) + +(defun vip-scroll-up-one (arg) + "Scroll down one line." + (interactive "p") + (scroll-up arg)) + + +;; searching + +(defun vip-if-string (prompt) + (let ((s (vip-read-string-with-history + prompt + nil ; no initial + 'vip-search-history + (car vip-search-history)))) + (if (not (string= s "")) + (setq vip-s-string s)))) + + +(defun vip-toggle-search-style (arg) + "Toggle the value of vip-case-fold-search/vip-re-search. +Without prefix argument, will ask which search style to toggle. With prefix +arg 1,toggles vip-case-fold-search; with arg 2 toggles vip-re-search. + +Although this function is bound to \\[vip-toggle-search-style], the most +convenient way to use it is to bind `//' to the macro +`1 M-x vip-toggle-search-style' and `///' to +`2 M-x vip-toggle-search-style'. In this way, hitting `//' quickly will +toggle case-fold-search and hitting `/' three times witth toggle regexp +search. Macros are more convenient in this case because they don't affect +the Emacs binding of `/'." + (interactive "P") + (let (msg) + (cond ((or (eq arg 1) + (and (null arg) + (y-or-n-p (format "Search style: '%s'. Want '%s'? " + (if vip-case-fold-search + "case-insensitive" "case-sensitive") + (if vip-case-fold-search + "case-sensitive" + "case-insensitive"))))) + (setq vip-case-fold-search (null vip-case-fold-search)) + (if vip-case-fold-search + (setq msg "Search becomes case-insensitive") + (setq msg "Search becomes case-sensitive"))) + ((or (eq arg 2) + (and (null arg) + (y-or-n-p (format "Search style: '%s'. Want '%s'? " + (if vip-re-search + "regexp-search" "vanilla-search") + (if vip-re-search + "vanilla-search" + "regexp-search"))))) + (setq vip-re-search (null vip-re-search)) + (if vip-re-search + (setq msg "Search becomes regexp-style") + (setq msg "Search becomes vanilla-style"))) + (t + (setq msg "Search style remains unchanged"))) + (prin1 msg t))) + + +(defun vip-search-forward (arg) + "Search a string forward. +ARG is used to find the ARG's occurrence of the string. +Null string will repeat previous search." + (interactive "P") + (let ((val (vip-P-val arg)) + (com (vip-getcom arg)) + (old-str vip-s-string)) + (setq vip-s-forward t) + (vip-if-string "/") + ;; this is not used at present, but may be used later + (if (or (not (equal old-str vip-s-string)) + (not (markerp vip-local-search-start-marker)) + (not (marker-buffer vip-local-search-start-marker))) + (setq vip-local-search-start-marker (point-marker))) + (vip-search vip-s-string t val) + (if com + (progn + (vip-move-marker-locally 'vip-com-point (mark t)) + (vip-execute-com 'vip-search-next val com))))) + +(defun vip-search-backward (arg) + "Search a string backward. +ARG is used to find the ARG's occurrence of the string. +Null string will repeat previous search." + (interactive "P") + (let ((val (vip-P-val arg)) + (com (vip-getcom arg)) + (old-str vip-s-string)) + (setq vip-s-forward nil) + (vip-if-string "?") + ;; this is not used at present, but may be used later + (if (or (not (equal old-str vip-s-string)) + (not (markerp vip-local-search-start-marker)) + (not (marker-buffer vip-local-search-start-marker))) + (setq vip-local-search-start-marker (point-marker))) + (vip-search vip-s-string nil val) + (if com + (progn + (vip-move-marker-locally 'vip-com-point (mark t)) + (vip-execute-com 'vip-search-next val com))))) + + +(defun vip-search (string forward arg &optional no-offset init-point) + "Search for COUNT's occurrence of STRING. +Search is forward if FORWARD is non-nil, otherwise backward. +INIT-POINT is the position where search is to start. +Arguments: (STRING FORWARD COUNT &optional NO-OFFSET INIT-POINT LIMIT)." + (if (not (equal string "")) + (let ((val (vip-p-val arg)) + (com (vip-getcom arg)) + (null-arg (null (vip-P-val arg))) (offset (not no-offset)) + (case-fold-search vip-case-fold-search) + (start-point (or init-point (point)))) + (vip-deactivate-mark) + (if forward + (condition-case nil + (progn + (if offset (vip-forward-char-carefully)) + (if vip-re-search + (progn + (re-search-forward string nil nil val) + (re-search-backward string)) + (search-forward string nil nil val) + (search-backward string)) + (vip-flash-search-pattern) + (if (not (equal start-point (point))) + (push-mark start-point t))) + (search-failed + (if (and null-arg vip-search-wrap-around-t) + (progn + (message "Search wrapped around end of buffer") + (goto-char (point-min)) + (vip-search string forward (cons 1 com) t start-point) + ;; delete the wrapped around message + (sit-for 2)(message "") + ) + (goto-char start-point) + (error "`%s': %s not found" + string + (if vip-re-search "Pattern" "String")) + ))) + ;; backward + (condition-case nil + (progn + (if vip-re-search + (re-search-backward string nil nil val) + (search-backward string nil nil val)) + (vip-flash-search-pattern) + (if (not (equal start-point (point))) + (push-mark start-point t))) + (search-failed + (if (and null-arg vip-search-wrap-around-t) + (progn + (message "Search wrapped around beginning of buffer") + (goto-char (point-max)) + (vip-search string forward (cons 1 com) t start-point) + ;; delete the wrapped around message + (sit-for 2)(message "") + ) + (goto-char start-point) + (error "`%s': %s not found" + string + (if vip-re-search "Pattern" "String")) + ))))))) + +(defun vip-search-next (arg) + "Repeat previous search." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (if (null vip-s-string) (error vip-NoPrevSearch)) + (vip-search vip-s-string vip-s-forward arg) + (if com + (progn + (vip-move-marker-locally 'vip-com-point (mark t)) + (vip-execute-com 'vip-search-next val com))))) + +(defun vip-search-Next (arg) + "Repeat previous search in the reverse direction." + (interactive "P") + (let ((val (vip-p-val arg)) + (com (vip-getcom arg))) + (if (null vip-s-string) (error vip-NoPrevSearch)) + (vip-search vip-s-string (not vip-s-forward) arg) + (if com + (progn + (vip-move-marker-locally 'vip-com-point (mark t)) + (vip-execute-com 'vip-search-Next val com))))) + + +;; Search contents of buffer defined by one of Viper's motion commands. +;; Repeatable via `n' and `N'. +(defun vip-buffer-search-enable (&optional c) + (cond (c (setq vip-buffer-search-char c)) + ((null vip-buffer-search-char) + (setq vip-buffer-search-char ?g))) + (define-key vip-vi-basic-map + (char-to-string vip-buffer-search-char) 'vip-command-argument) + (aset vip-exec-array vip-buffer-search-char 'vip-exec-buffer-search) + (setq vip-prefix-commands (cons vip-buffer-search-char vip-prefix-commands))) + +(defun vip-isearch-forward (arg) + "This is a Viper wrap-around for isearch-forward." + (interactive "P") + ;; emacs bug workaround + (if (listp arg) (setq arg (car arg))) + (vip-exec-form-in-emacs (list 'isearch-forward arg))) + +(defun vip-isearch-backward (arg) + "This is a Viper wrap-around for isearch-backward." + (interactive "P") + ;; emacs bug workaround + (if (listp arg) (setq arg (car arg))) + (vip-exec-form-in-emacs (list 'isearch-backward arg))) + + +;; visiting and killing files, buffers + +(defun vip-switch-to-buffer () + "Switch to buffer in the current window." + (interactive) + (let (buffer) + (setq buffer + (read-buffer + (format "Switch to buffer in this window \(%s\): " + (buffer-name (other-buffer (current-buffer)))))) + (switch-to-buffer buffer) + )) + +(defun vip-switch-to-buffer-other-window () + "Switch to buffer in another window." + (interactive) + (let (buffer) + (setq buffer + (read-buffer + (format "Switch to buffer in another window \(%s\): " + (buffer-name (other-buffer (current-buffer)))))) + (switch-to-buffer-other-window buffer) + )) + +(defun vip-kill-buffer () + "Kill a buffer." + (interactive) + (let (buffer buffer-name) + (setq buffer-name + (read-buffer + (format "Kill buffer \(%s\): " + (buffer-name (current-buffer))))) + (setq buffer + (if (null buffer-name) + (current-buffer) + (get-buffer buffer-name))) + (if (null buffer) (error "`%s': No such buffer" buffer-name)) + (if (or (not (buffer-modified-p buffer)) + (y-or-n-p + (format + "Buffer `%s' is modified, are you sure you want to kill it? " + buffer-name))) + (kill-buffer buffer) + (error "Buffer not killed")))) + + +(defvar vip-smart-suffix-list '("" "tex" "c" "cc" "el" "p") + "*List of suffixes that Viper automatically tries to append to filenames ending with a `.'. +This is useful when you the current directory contains files with the same +prefix and many different suffixes. Usually, only one of the suffixes +represents an editable file. However, file completion will stop at the `.' +The smart suffix feature lets you hit RET in such a case, and Viper will +select the appropriate suffix. + +Suffixes are tried in the order given and the first suffix for which a +corresponding file exists is selected. If no file exists for any of the +suffixes, the user is asked to confirm. + +To turn this feature off, set this variable to nil.") + +;; Try to add suffix to files ending with a `.' +;; Useful when the user hits RET on a non-completed file name. +(defun vip-file-add-suffix () + (let ((count 0) + (len (length vip-smart-suffix-list)) + (file (buffer-string)) + found key cmd suff) + (goto-char (point-max)) + (if (and vip-smart-suffix-list (string-match "\\.$" file)) + (progn + (while (and (not found) (< count len)) + (setq suff (nth count vip-smart-suffix-list) + count (1+ count)) + (if (file-exists-p (format "%s%s" file suff)) + (progn + (setq found t) + (insert suff)))) + + (if found + () + (vip-tmp-insert-at-eob " [Please complete file name]") + (unwind-protect + (while (not (memq cmd '(exit-minibuffer vip-exit-minibuffer))) + (setq cmd + (key-binding (setq key (read-key-sequence nil)))) + (cond ((eq cmd 'self-insert-command) + (if vip-xemacs-p + (insert (events-to-keys key)) + (insert key))) + ((memq cmd '(exit-minibuffer vip-exit-minibuffer)) + nil) + (t (command-execute cmd))) + ))) + )) + )) + + +;; Advice for use in find-file and read-file-name commands. +(defadvice exit-minibuffer (before vip-exit-minibuffer-advice activate) + "Runs vip-minibuffer-exit-hook just before exiting the minibuffer. +Beginning with Emacs 19.26, the standard `minibuffer-exit-hook' is run +*after* exiting the minibuffer." + (run-hooks 'vip-minibuffer-exit-hook)) + +(defadvice find-file (before vip-add-suffix-advice activate) + "Uses read-file-name to read arguments." + (interactive (list (read-file-name "Find file: " + nil default-directory)))) + +(defadvice find-file-other-window (before vip-add-suffix-advice activate) + "Uses read-file-name to read arguments." + (interactive (list (read-file-name "Find file in other window: " + nil default-directory)))) + +;; find-file-other-screen doesn't need advice because it apparently uses +;; read-file-name to read its argument. +(defadvice find-file-other-frame (before vip-add-suffix-advice activate) + "Uses read-file-name to read arguments." + (interactive (list (read-file-name "Find file in other frame: " + nil default-directory)))) + +(defadvice read-file-name (around vip-suffix-advice activate) + "Makes exit-minibuffer run `vip-file-add-suffix' as a hook." + (let ((vip-minibuffer-exit-hook 'vip-file-add-suffix)) + ad-do-it)) + +;; must be after we did advice or else the advice won't take hold +(if vip-xemacs-p + (fset 'vip-find-file-other-frame + (symbol-function 'find-file-other-screen)) + (fset 'vip-find-file-other-frame + (symbol-function 'find-file-other-frame))) + + + +;; yank and pop + +(defsubst vip-yank (text) + "Yank TEXT silently. This works correctly with Emacs's yank-pop command." + (insert text) + (setq this-command 'yank)) + +(defun vip-put-back (arg) + "Put back after point/below line." + (interactive "P") + (let ((val (vip-p-val arg)) + (text (if vip-use-register + (cond ((vip-valid-register vip-use-register '(digit)) + (current-kill (- vip-use-register ?1) 'do-not-rotate)) + ((vip-valid-register vip-use-register) + (get-register (downcase vip-use-register))) + (t (error vip-InvalidRegister vip-use-register))) + (current-kill 0)))) + (if (null text) + (if vip-use-register + (let ((reg vip-use-register)) + (setq vip-use-register nil) + (error vip-EmptyRegister reg)) + (error ""))) + (setq vip-use-register nil) + (if (vip-end-with-a-newline-p text) + (progn + (if (eobp) + (insert "\n") + (forward-line 1)) + (beginning-of-line)) + (if (not (eolp)) (vip-forward-char-carefully))) + (set-marker (vip-mark-marker) (point) (current-buffer)) + (vip-set-destructive-command + (list 'vip-put-back val nil vip-use-register nil nil)) + (vip-loop val (vip-yank text))) + (exchange-point-and-mark) + (vip-deactivate-mark)) + +(defun vip-Put-back (arg) + "Put back at point/above line." + (interactive "P") + (let ((val (vip-p-val arg)) + (text (if vip-use-register + (cond ((vip-valid-register vip-use-register '(digit)) + (current-kill (- vip-use-register ?1) 'do-not-rotate)) + ((vip-valid-register vip-use-register) + (get-register (downcase vip-use-register))) + (t (error vip-InvalidRegister vip-use-register))) + (current-kill 0)))) + (if (null text) + (if vip-use-register + (let ((reg vip-use-register)) + (setq vip-use-register nil) + (error vip-EmptyRegister reg)) + (error ""))) + (setq vip-use-register nil) + (if (vip-end-with-a-newline-p text) (beginning-of-line)) + (vip-set-destructive-command + (list 'vip-Put-back val nil vip-use-register nil nil)) + (set-marker (vip-mark-marker) (point) (current-buffer)) + (vip-loop val (vip-yank text))) + (exchange-point-and-mark) + (vip-deactivate-mark)) + + +(defun vip-copy-region-as-kill (beg end) + "Copy region to kill-ring. +If BEG and END do not belong to the same buffer, copy empty region." + (condition-case nil + (copy-region-as-kill beg end) + (error (copy-region-as-kill beg beg)))) + +(defun vip-save-last-insertion (beg end) + "Saves last inserted text for possible use by vip-repeat command." + (setq vip-last-insertion (buffer-substring beg end)) + (or (< (length vip-d-com) 5) + (setcar (nthcdr 4 vip-d-com) vip-last-insertion)) + (or (null vip-command-ring) + (ring-empty-p vip-command-ring) + (progn + (setcar (nthcdr 4 (vip-current-ring-item vip-command-ring)) + vip-last-insertion) + ;; del most recent elt, if identical to the second most-recent + (vip-cleanup-ring vip-command-ring))) + ) + +(defsubst vip-yank-last-insertion () + "Inserts the text saved by the previous vip-save-last-insertion command." + (condition-case nil + (insert vip-last-insertion) + (error nil))) + + +(defun vip-delete-char (arg) + "Delete character." + (interactive "P") + (let ((val (vip-p-val arg))) + (vip-set-destructive-command (list 'vip-delete-char val nil nil nil nil)) + (if (> val 1) + (save-excursion + (let ((here (point))) + (end-of-line) + (if (> val (- (point) here)) + (setq val (- (point) here)))))) + (if (and (eq val 0) (not vip-ex-style-motion)) (setq val 1)) + (if (and vip-ex-style-motion (eolp)) + (if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch + (if vip-use-register + (progn + (cond ((vip-valid-register vip-use-register '((Letter))) + (vip-append-to-register + (downcase vip-use-register) (point) (- (point) val))) + ((vip-valid-register vip-use-register) + (copy-to-register + vip-use-register (point) (- (point) val) nil)) + (t (error vip-InvalidRegister vip-use-register))) + (setq vip-use-register nil))) + (if vip-ex-style-motion + (progn + (delete-char val t) + (if (and (eolp) (not (bolp))) (backward-char 1))) + (if (eolp) + (delete-backward-char val t) + (delete-char val t))))) + +(defun vip-delete-backward-char (arg) + "Delete previous character. On reaching beginning of line, stop and beep." + (interactive "P") + (let ((val (vip-p-val arg))) + (vip-set-destructive-command + (list 'vip-delete-backward-char val nil nil nil nil)) + (if (> val 1) + (save-excursion + (let ((here (point))) + (beginning-of-line) + (if (> val (- here (point))) + (setq val (- here (point))))))) + (if vip-use-register + (progn + (cond ((vip-valid-register vip-use-register '(Letter)) + (vip-append-to-register + (downcase vip-use-register) (point) (+ (point) val))) + ((vip-valid-register vip-use-register) + (copy-to-register + vip-use-register (point) (+ (point) val) nil)) + (t (error vip-InvalidRegister vip-use-register))) + (setq vip-use-register nil))) + (if (bolp) (ding) + (delete-backward-char val t)))) + +(defun vip-del-backward-char-in-insert () + "Delete 1 char backwards while in insert mode." + (interactive) + (if (and vip-ex-style-editing-in-insert (bolp)) + (beep 1) + (delete-backward-char 1 t))) + +(defun vip-del-backward-char-in-replace () + "Delete one character in replace mode. +If `vip-delete-backwards-in-replace' is t, then DEL key actually deletes +charecters. If it is nil, then the cursor just moves backwards, similarly +to Vi. The variable `vip-ex-style-editing-in-insert', if t, doesn't let the +cursor move past the beginning of the replacement region." + (interactive) + (cond (vip-delete-backwards-in-replace + (cond ((not (bolp)) + (delete-backward-char 1 t)) + (vip-ex-style-editing-in-insert + (beep 1)) + ((bobp) + (beep 1)) + (t + (delete-backward-char 1 t)))) + (vip-ex-style-editing-in-insert + (if (bolp) + (beep 1) + (backward-char 1))) + (t + (backward-char 1)))) + + + +;; join lines. + +(defun vip-join-lines (arg) + "Join this line to next, if ARG is nil. Otherwise, join ARG lines." + (interactive "*P") + (let ((val (vip-P-val arg))) + (vip-set-destructive-command (list 'vip-join-lines val nil nil nil nil)) + (vip-loop (if (null val) 1 (1- val)) + (progn + (end-of-line) + (if (not (eobp)) + (progn + (forward-line 1) + (delete-region (point) (1- (point))) + (fixup-whitespace))))))) + + +;; Replace state + +(defun vip-change (beg end) + (if (markerp beg) (setq beg (marker-position beg))) + (if (markerp end) (setq end (marker-position end))) + ;; beg is sometimes (mark t), which may be nil + (or beg (setq beg end)) + + (vip-set-complex-command-for-undo) + (if vip-use-register + (progn + (copy-to-register vip-use-register beg end nil) + (setq vip-use-register nil))) + (vip-set-replace-overlay beg end) + (setq last-command nil) ; separate repl text from prev kills + + (if (= (vip-replace-start) (point-max)) + (error "End of buffer")) + + (setq vip-last-replace-region + (buffer-substring (vip-replace-start) + (vip-replace-end))) + + ;; protect against error while inserting "@" and other disasters + ;; (e.g., read-only buff) + (condition-case conds + (if (vip-same-line (vip-replace-start) + (vip-replace-end)) + (let ((delim-end (if (= (length vip-replace-region-end-symbol) 0) + "" + (substring vip-replace-region-end-symbol 0 1)))) + + ;; tabs cause problems in replace, so untabify + (goto-char (vip-replace-end)) + (insert-before-markers "@") ; put placeholder after the TAB + + (untabify (vip-replace-start) (point)) + ;; del @ and the char under the '$'; don't put on kill ring + (delete-backward-char (1+ (length delim-end))) + (insert delim-end) + ;; this move takes care of the last posn in the overlay, which + ;; has to be shifted because of insert. We can't simply insert + ;; "$" before-markers because then overlay-start will shift the + ;; beginning of the overlay in case we are replacing a single + ;; character. This fixes the bug with `s' and `cl' commands. + (vip-move-replace-overlay (vip-replace-start) (point)) + (goto-char (vip-replace-start)) + (vip-change-state-to-replace t)) + (kill-region (vip-replace-start) + (vip-replace-end)) + (vip-change-state-to-insert)) + (error ;; make sure that the overlay doesn't stay. + ;; go back to the original point + (goto-char (vip-replace-start)) + (vip-hide-replace-overlay) + (vip-message-conditions conds)))) + + +(defun vip-change-subr (beg end) + ;; beg is sometimes (mark t), which may be nil + (or beg (setq beg end)) + + (if vip-use-register + (progn + (copy-to-register vip-use-register beg end nil) + (setq vip-use-register nil))) + (kill-region beg end) + (setq this-command 'vip-change) + (vip-yank-last-insertion)) + +(defun vip-toggle-case (arg) + "Toggle character case." + (interactive "P") + (let ((val (vip-p-val arg)) (c)) + (vip-set-destructive-command (list 'vip-toggle-case val nil nil nil nil)) + (while (> val 0) + (setq c (following-char)) + (delete-char 1 nil) + (if (eq c (upcase c)) + (insert-char (downcase c) 1) + (insert-char (upcase c) 1)) + (setq val (1- val))))) + + +;; query replace + +(defun vip-query-replace () + "Query replace. +If a null string is suplied 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-with-history + (if vip-re-query-replace "Query replace regexp: " + "Query replace: ") + nil ; no initial + 'vip-replace1-history + (car vip-replace1-history) ; default + )) + (if (string= str "") + (progn + (setq vip-re-query-replace (not vip-re-query-replace)) + (message "Query replace mode changed to %s" + (if vip-re-query-replace "regexp replace" + "string replace"))) + (if vip-re-query-replace + (query-replace-regexp + str + (vip-read-string-with-history + (format "Query replace regexp `%s' with: " str) + nil ; no initial + 'vip-replace1-history + (car vip-replace1-history) ; default + )) + (query-replace + str + (vip-read-string-with-history + (format "Query replace `%s' with: " str) + nil ; no initial + 'vip-replace1-history + (car vip-replace1-history) ; default + )))))) + + +;; marking + +(defun vip-mark-beginning-of-buffer () + (interactive) + (push-mark (point)) + (goto-char (point-min)) + (exchange-point-and-mark) + (message "Mark set at the beginning of buffer")) + +(defun vip-mark-end-of-buffer () + (interactive) + (push-mark (point)) + (goto-char (point-max)) + (exchange-point-and-mark) + (message "Mark set at the end of buffer")) + +(defun vip-mark-point () + (interactive) + (let ((char (vip-read-char-exclusive))) + (cond ((and (<= ?a char) (<= char ?z)) + (point-to-register (1+ (- char ?a)))) + ((= char ?<) (vip-mark-beginning-of-buffer)) + ((= char ?>) (vip-mark-end-of-buffer)) + ((= char ?.) (vip-set-mark-if-necessary)) + ((= char ?,) (vip-cycle-through-mark-ring)) + ((= char ?D) (mark-defun)) + (t (error "")) + ))) + +;; Algorithm: If first invocation of this command save mark on ring, goto +;; mark, M0, and pop the most recent elt from the mark ring into mark, +;; making it into the new mark, M1. +;; Push this mark back and set mark to the original point position, p1. +;; So, if you hit '' or `` then you can return to p1. +;; +;; If repeated command, pop top elt from the ring into mark and +;; jump there. This forgets the position, p1, and puts M1 back into mark. +;; Then we save the current pos, which is M0, jump to M1 and pop M2 from +;; the ring into mark. Push M2 back on the ring and set mark to M0. +;; etc. +(defun vip-cycle-through-mark-ring () + "Visit previous locations on the mark ring. +One can use `` and '' to temporarily jump 1 step back." + (let* ((sv-pt (point))) + ;; if repeated `m,' command, pop the previously saved mark. + ;; Prev saved mark is actually prev saved point. It is used if the + ;; user types `` or '' and is discarded + ;; from the mark ring by the next `m,' command. + ;; In any case, go to the previous or previously saved mark. + ;; Then push the current mark (popped off the ring) and set current + ;; point to be the mark. Current pt as mark is discarded by the next + ;; m, command. + (if (eq last-command 'vip-cycle-through-mark-ring) + () + ;; save current mark if the first iteration + (setq mark-ring (delete (vip-mark-marker) mark-ring)) + (if (mark t) + (push-mark (mark t) t)) ) + (pop-mark) + (set-mark-command 1) + ;; don't duplicate mark on the ring + (setq mark-ring (delete (vip-mark-marker) mark-ring)) + (push-mark sv-pt t) + (vip-deactivate-mark) + (setq this-command 'vip-cycle-through-mark-ring) + )) + + +(defun vip-goto-mark (arg) + "Go to mark." + (interactive "P") + (let ((char (read-char)) + (com (vip-getcom arg))) + (vip-goto-mark-subr char com nil))) + +(defun vip-goto-mark-and-skip-white (arg) + "Go to mark and skip to first non-white character on line." + (interactive "P") + (let ((char (read-char)) + (com (vip-getCom arg))) + (vip-goto-mark-subr char com t))) + +(defun vip-goto-mark-subr (char com skip-white) + (if (eobp) + (if (bobp) + (error "Empty buffer") + (backward-char 1))) + (cond ((vip-valid-register char '(letter)) + (let* ((buff (current-buffer)) + (reg (1+ (- char ?a))) + (text-marker (get-register reg))) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (if (not (vip-valid-marker text-marker)) + (error (format vip-EmptyTextmarker char))) + (if (and (vip-same-line (point) vip-last-jump) + (= (point) vip-last-jump-ignore)) + (push-mark vip-last-jump t) + (push-mark nil t)) ; no msg + (vip-register-to-point reg) + (setq vip-last-jump (point-marker)) + (cond (skip-white + (back-to-indentation) + (setq vip-last-jump-ignore (point)))) + (if com + (if (equal buff (current-buffer)) + (vip-execute-com (if skip-white + 'vip-goto-mark-and-skip-white + 'vip-goto-mark) + nil com) + (switch-to-buffer buff) + (goto-char vip-com-point) + (vip-change-state-to-vi) + (error ""))))) + ((and (not skip-white) (= char ?`)) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (if (and (vip-same-line (point) vip-last-jump) + (= (point) vip-last-jump-ignore)) + (goto-char vip-last-jump)) + (if (= (point) (mark t)) (pop-mark)) + (exchange-point-and-mark) + (setq vip-last-jump (point-marker) + vip-last-jump-ignore 0) + (if com (vip-execute-com 'vip-goto-mark nil com))) + ((and skip-white (= char ?')) + (if com (vip-move-marker-locally 'vip-com-point (point))) + (if (and (vip-same-line (point) vip-last-jump) + (= (point) vip-last-jump-ignore)) + (goto-char vip-last-jump)) + (if (= (point) (mark t)) (pop-mark)) + (exchange-point-and-mark) + (setq vip-last-jump (point)) + (back-to-indentation) + (setq vip-last-jump-ignore (point)) + (if com (vip-execute-com 'vip-goto-mark-and-skip-white nil com))) + (t (error vip-InvalidTextmarker char)))) + +(defun vip-insert-tab () + (interactive) + (insert-tab)) + +(defun vip-exchange-point-and-mark () + (interactive) + (exchange-point-and-mark) + (back-to-indentation)) + +;; Input Mode Indentation + +(defun vip-forward-indent () + "Indent forward -- `C-t' in Vi." + (interactive) + (setq vip-cted t) + (indent-to (+ (current-column) vip-shift-width))) + +(defun vip-backward-indent () + "Backtab, C-d in VI" + (interactive) + (if vip-cted + (let ((p (point)) (c (current-column)) bol (indent t)) + (if (vip-looking-back "[0^]") + (progn + (if (= ?^ (preceding-char)) (setq vip-preserve-indent t)) + (delete-backward-char 1) + (setq p (point)) + (setq indent nil))) + (save-excursion + (beginning-of-line) + (setq bol (point))) + (if (re-search-backward "[^ \t]" bol 1) (forward-char)) + (delete-region (point) p) + (if indent + (indent-to (- c vip-shift-width))) + (if (or (bolp) (vip-looking-back "[^ \t]")) + (setq vip-cted nil))))) + +(defun vip-autoindent () + "Auto Indentation, Vi-style." + (interactive) + (let ((col (current-indentation))) + (if (not vip-preserve-indent) + (setq vip-current-indent col) + (setq vip-preserve-indent nil)) + (newline 1) + (if vip-auto-indent + (progn + (setq vip-cted t) + (indent-to vip-current-indent))))) + + +;; Viewing registers + +(defun vip-ket-function (arg) + "Function called by \], the ket. View registers and call \]\]." + (interactive "P") + (let ((reg (read-char))) + (cond ((vip-valid-register reg '(letter Letter)) + (view-register (downcase reg))) + ((vip-valid-register reg '(digit)) + (let ((text (current-kill (- reg ?1) 'do-not-rotate))) + (save-excursion + (set-buffer (get-buffer-create "*Output*")) + (delete-region (point-min) (point-max)) + (insert (format "Register %c contains the string:\n" reg)) + (insert text) + (goto-char (point-min))) + (display-buffer "*Output*"))) + ((= ?\] reg) + (vip-next-heading arg)) + (t (error + vip-InvalidRegister reg))))) + +(defun vip-brac-function (arg) + "Function called by \[, the brac. View textmarkers and call \[\[" + (interactive "P") + (let ((reg (read-char))) + (cond ((= ?\[ reg) + (vip-prev-heading arg)) + ((= ?\] reg) + (vip-heading-end arg)) + ((vip-valid-register reg '(letter)) + (let* ((val (get-register (1+ (- reg ?a)))) + (buf (if (not val) + (error + (format vip-EmptyTextmarker reg)) + (marker-buffer val))) + (pos (marker-position val)) + line-no text (s pos) (e pos)) + (save-excursion + (set-buffer (get-buffer-create "*Output*")) + (delete-region (point-min) (point-max)) + (if (and buf pos) + (progn + (save-excursion + (set-buffer buf) + (setq line-no (1+ (count-lines (point-min) val))) + (goto-char pos) + (beginning-of-line) + (if (re-search-backward "[^ \t]" nil t) + (progn + (beginning-of-line) + (setq s (point)))) + (goto-char pos) + (forward-line 1) + (if (re-search-forward "[^ \t]" nil t) + (progn + (end-of-line) + (setq e (point)))) + (setq text (buffer-substring s e)) + (setq text (format "%s<%c>%s" + (substring text 0 (- pos s)) + reg (substring text (- pos s))))) + (insert + (format + "Textmarker `%c' is in buffer `%s' at line %d.\n" + reg (buffer-name buf) line-no)) + (insert (format "Here is some text around %c:\n\n %s" + reg text))) + (insert (format vip-EmptyTextmarker reg))) + (goto-char (point-min))) + (display-buffer "*Output*"))) + (t (error vip-InvalidTextmarker reg))))) + + + +;; commands in insertion mode + +(defun vip-delete-backward-word (arg) + "Delete previous word." + (interactive "p") + (save-excursion + (push-mark nil t) + (backward-word arg) + (delete-region (point) (mark t)) + (pop-mark))) + + +(defun vip-set-expert-level (&optional dont-change-unless) + "Sets the expert level for a Viper user. +Can be called interactively to change (temporarily or permanently) the +current expert level. + +The optional argument DONT-CHANGE-UNLESS if not nil, says that +the level should not be changed, unless its current value is +meaningless (i.e., not one of 1,2,3,4,5). + +User level determines the setting of Viper variables that are most +sensitive for VI-style look-and-feel." + + (interactive) + + (if (not (numberp vip-expert-level)) (setq vip-expert-level 0)) + + (save-window-excursion + (delete-other-windows) + ;; if 0 < vip-expert-level < vip-max-expert-level + ;; & dont-change-unless = t -- use it; else ask + (vip-ask-level dont-change-unless)) + + (setq vip-always t + vip-ex-style-motion t + vip-ex-style-editing-in-insert t + vip-want-ctl-h-help nil) + + (cond + ;; a novice or a beginner + ((eq vip-expert-level 1) + (global-set-key vip-toggle-key ;; in emacs-state + (if window-system + 'vip-iconify + 'suspend-emacs)) + (setq vip-no-multiple-ESC t + vip-re-search t + vip-vi-style-in-minibuffer t + vip-search-wrap-around-t t + vip-want-emacs-keys-in-vi nil + vip-want-emacs-keys-in-insert nil)) + + ;; an intermediate to guru + ((and (> vip-expert-level 1) (< vip-expert-level 5)) + (setq vip-no-multiple-ESC (if window-system t 'twice) + vip-want-emacs-keys-in-vi t + vip-want-emacs-keys-in-insert (> vip-expert-level 2)) + + (if (eq vip-expert-level 4) ; respect user's ex-style motions + ; and vip-no-multiple-ESC + (progn + (setq-default vip-ex-style-editing-in-insert + (cdr (assoc 'vip-ex-style-editing-in-insert + vip-saved-user-settings)) + vip-ex-style-motion + (cdr (assoc 'vip-ex-style-motion + vip-saved-user-settings))) + (setq vip-ex-style-motion + (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings)) + vip-ex-style-editing-in-insert + (cdr (assoc 'vip-ex-style-editing-in-insert + vip-saved-user-settings)) + vip-re-search + (cdr (assoc 'vip-re-search vip-saved-user-settings)) + vip-no-multiple-ESC + (cdr (assoc 'vip-no-multiple-ESC + vip-saved-user-settings)))))) + + ;; A wizard + ;; Ideally, if 5 is selected, a buffer should pop up to let the + ;; user toggle variable values. + (t (setq-default vip-ex-style-editing-in-insert + (cdr (assoc 'vip-ex-style-editing-in-insert + vip-saved-user-settings)) + vip-ex-style-motion + (cdr (assoc 'vip-ex-style-motion + vip-saved-user-settings))) + (setq vip-want-ctl-h-help + (cdr (assoc 'vip-want-ctl-h-help vip-saved-user-settings)) + vip-always + (cdr (assoc 'vip-always vip-saved-user-settings)) + vip-no-multiple-ESC + (cdr (assoc 'vip-no-multiple-ESC vip-saved-user-settings)) + vip-ex-style-motion + (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings)) + vip-ex-style-editing-in-insert + (cdr (assoc 'vip-ex-style-editing-in-insert + vip-saved-user-settings)) + vip-re-search + (cdr (assoc 'vip-re-search vip-saved-user-settings)) + vip-want-emacs-keys-in-vi + (cdr (assoc 'vip-want-emacs-keys-in-vi + vip-saved-user-settings)) + vip-want-emacs-keys-in-insert + (cdr (assoc 'vip-want-emacs-keys-in-insert + vip-saved-user-settings))))) + (vip-set-mode-vars-for vip-current-state) + (if (or vip-always + (and (> vip-expert-level 0) (> 5 vip-expert-level))) + (vip-set-hooks))) + +(defun vip-ask-level (dont-change-unless) + "Ask user expert level." + (let ((ask-buffer " *vip-ask-level*") + level-changed repeated) + (save-window-excursion + (switch-to-buffer ask-buffer) + + (or (eq this-command 'vip-set-expert-level) + (and + (<= vip-expert-level vip-max-expert-level) + (>= vip-expert-level 1)) + (progn + (insert " + + *** Important Notice for VIP users*** + + This is VIPER + +@joke +Viper Is a Package for Emacs Rebels, +a VI Plan for Emacs Rescue, +and a venomous VI PERil. +@end joke + +Technically speaking, Viper is a new Vi emulator that replaces +the old VIP package. + +Viper emulates Vi much better than VIP. It also significantly +extends and improves upon Vi in many useful ways. + +Although many VIP settings in your ~/.vip are compatible with Viper, +you may have to change some of them. Please refer to the documentation, +which can be obtained by executing + +:help + +when Viper is in Vi state. + +If you will be so lucky as to find a bug, report it via the command + +:submitReport + +Type any key to continue... ") + + (read-char) + (erase-buffer))) + + (while (or (> vip-expert-level vip-max-expert-level) + (< vip-expert-level 1) + (null dont-change-unless)) + (erase-buffer) + (if repeated + (progn + (message "Invalid user level") + (beep 1)) + (setq repeated t)) + (setq dont-change-unless t + level-changed t) + (insert " +Please specify your level of familiarity with the venomous VI PERil +(and the VI Plan for Emacs Rescue). +You can change it at any time by typing `M-x vip-set-expert-level RET' + + 1 -- BEGINNER: Almost all Emacs features are suppressed. + Feels almost like straight Vi. File name completion and + command history in the minibuffer are thrown in as a bonus. + To use Emacs productively, you must reach level 3 or higher. + 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state, + so most Emacs commands can be used when Viper is in Vi state. + Good progress---you are well on the way to level 3! + 3 -- GRAND MASTER: Like 3, but most Emacs commands are available also + in Viper's insert state. + 4 -- GURU: Like 3, but user settings are respected for vip-no-multiple-ESC, + vip-re-search, vip-ex-style-motion, & vip-ex-style-editing-in-insert + variables. Adjust these settings to your taste. + 5 -- WIZARD: Like 4, but user settings are also respected for vip-always, + vip-want-ctl-h-help, vip-want-emacs-keys-in-vi, and + vip-want-emacs-keys-in-insert. Adjust these to your taste. + +Please, specify your level now: ") + + (setq vip-expert-level (- (vip-read-char-exclusive) ?0)) + ) ; end while + + ;; tell the user if level was changed + (and level-changed + (progn + (insert + (format "\n\n\n\n\n\t\tYou have selected user level %d" + vip-expert-level)) + (if (y-or-n-p "Do you wish to make this change permanent? ") + ;; save the setting for vip-expert-level + (vip-save-setting + 'vip-expert-level + (format "Saving user level %d ..." vip-expert-level) + vip-custom-file-name)) + )) + (bury-buffer) ; remove ask-buffer from screen + (message "") + ))) + + +(defun viper-version () + (interactive) + (message "Viper version is %s" viper-version)) + +(defalias 'vip-version 'viper-version) + +(defun vip-nil () + (interactive) + (beep 1)) + + +;; Returns t, if the string before point matches the regexp STR. +(defsubst vip-looking-back (str) + (and (save-excursion (re-search-backward str nil t)) + (= (point) (match-end 0)))) + + + +;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer +(defun vip-register-to-point (char &optional enforce-buffer) + "Like jump-to-register, but switches to another buffer in another window." + (interactive "cViper register to point: ") + (let ((val (get-register char))) + (cond + ((and (fboundp 'frame-configuration-p) + (frame-configuration-p val)) + (set-frame-configuration val)) + ((window-configuration-p val) + (set-window-configuration val)) + ((vip-valid-marker val) + (if (and enforce-buffer + (not (equal (current-buffer) (marker-buffer val)))) + (error (concat vip-EmptyTextmarker " in this buffer") + (1- (+ char ?a)))) + (pop-to-buffer (marker-buffer val)) + (goto-char val)) + ((and (consp val) (eq (car val) 'file)) + (find-file (cdr val))) + (t + (error vip-EmptyTextmarker (1- (+ char ?a))))))) + + +(defun vip-save-kill-buffer () + "Save then kill current buffer. " + (interactive) + (if (< vip-expert-level 2) + (save-buffers-kill-emacs) + (save-buffer) + (kill-buffer (current-buffer)))) + + + +;;; Bug Report + +(defun vip-submit-report () + "Submit bug report on Viper." + (interactive) + (let ((reporter-prompt-for-summary-p t) + color-display-p frame-parameters + minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face + varlist salutation window-config) + + ;; If mode info is needed, add variable to `let' and then set it below, + ;; like we did with color-display-p. + (setq color-display-p (if window-system + (vip-display-color-p) + 'non-x) + minibuffer-vi-face (if window-system + (vip-get-face vip-minibuffer-vi-face) + 'non-x) + minibuffer-insert-face (if window-system + (vip-get-face vip-minibuffer-insert-face) + 'non-x) + minibuffer-emacs-face (if window-system + (vip-get-face vip-minibuffer-emacs-face) + 'non-x) + frame-parameters (if (fboundp 'vip-frame-parameters) + (vip-frame-parameters (vip-selected-frame)))) + + (setq varlist (list 'vip-vi-minibuffer-minor-mode + 'vip-insert-minibuffer-minor-mode + 'vip-vi-intercept-minor-mode + 'vip-vi-local-user-minor-mode + 'vip-vi-kbd-minor-mode + 'vip-vi-global-user-minor-mode + 'vip-vi-state-modifier-minor-mode + 'vip-vi-diehard-minor-mode + 'vip-vi-basic-minor-mode + 'vip-replace-minor-mode + 'vip-insert-intercept-minor-mode + 'vip-insert-local-user-minor-mode + 'vip-insert-kbd-minor-mode + 'vip-insert-global-user-minor-mode + 'vip-insert-state-modifier-minor-mode + 'vip-insert-diehard-minor-mode + 'vip-insert-basic-minor-mode + 'vip-emacs-intercept-minor-mode + 'vip-emacs-local-user-minor-mode + 'vip-emacs-kbd-minor-mode + 'vip-emacs-global-user-minor-mode + 'vip-emacs-state-modifier-minor-mode + 'vip-automatic-iso-accents + 'vip-want-emacs-keys-in-insert + 'vip-want-emacs-keys-in-vi + 'vip-keep-point-on-undo + 'vip-no-multiple-ESC + 'vip-ESC-key + 'vip-want-ctl-h-help + 'vip-ex-style-editing-in-insert + 'vip-delete-backwards-in-replace + 'vip-vi-style-in-minibuffer + 'vip-vi-state-hooks + 'vip-insert-state-hooks + 'vip-replace-state-hooks + 'vip-emacs-state-hooks + 'ex-cycle-other-window + 'ex-cycle-through-non-files + 'vip-expert-level + 'major-mode + 'window-system + 'color-display-p + 'frame-parameters + 'minibuffer-vi-face + 'minibuffer-insert-face + 'minibuffer-emacs-face + )) + (setq salutation " +Congratulations! You may have unearthed a bug in Viper! +Please mail a concise, accurate summary of the problem to the address above. + +-------------------------------------------------------------------") + (setq window-config (current-window-configuration)) + (with-output-to-temp-buffer " *vip-info*" + (switch-to-buffer " *vip-info*") + (delete-other-windows) + (princ " +PLEASE FOLLOW THESE PROCEDURES +------------------------------ + +Before reporting a bug, please verify that it is related to Viper, and is +not cause by other packages you are using. + +Don't report compilation warnings, unless you are certain that there is a +problem. These warnings are normal and unavoidable. + +Please note that users should not modify variables and keymaps other than +those advertised in the manual. Such `customization' is likely to crash +Viper, as it would any other improperly customized Emacs package. + +If you are reporting an error message received while executing one of the +Viper commands, type: + + M-x set-variable <Return> debug-on-error <Return> t <Return> + +Then reproduce the error. The above command will cause Emacs to produce a +back trace of the execution that leads to the error. Please include this +trace in your bug report. + +If you believe that one of Viper's commands goes into an infinite loop +\(e.g., Emacs freezes\), type: + + M-x set-variable <Return> debug-on-quit <Return> t <Return> + +Then reproduce the problem. Wait for a few seconds, then type C-g to abort +the current command. Include the resulting back trace in the bug report. + +Mail anyway (y or n)? ") + (if (y-or-n-p "Mail anyway? ") + () + (set-window-configuration window-config) + (error "Bug report aborted"))) + + (require 'reporter) + (set-window-configuration window-config) + + (reporter-submit-bug-report "kifer@cs.sunysb.edu" + (vip-version) + varlist + nil 'delete-other-windows + salutation) + )) + + + + +;; Needed to smooth out the difference between Emacs' unread-command-events +;; and XEmacs unread-command-event. Arg is a character, an event, a list of +;; events or a sequence of keys. +;; The semantics of placing an event on unread-command-event in XEmacs is +;; not the same as placing (setq unread-command-event event) +;; on the event queue using enqueue-eval-event. For instance, an event +;; sitting in unread-command-event will be available to (next-event). +;; In contrast, evals placed on event queue are not evaluated until all +;; previous commands have been executed. This makes a difference when one +;; of the events placed on the event queue is bound to a function that +;; pauses for input, because these evals won't make input immediately +;; available +;; +;; Due to a bug in unread-command-events, a non-event symbol in +;; unread-command-evets list may cause Emacs to label this symbol to be an +;; event. Below, we delete nil from event lists, since nil is the most +;; common problem here. Hopefully, unread-command-evets will be fixed in +;; the next release. +(defun vip-set-unread-command-events (arg) + (if vip-emacs-p + (setq unread-command-events + (let ((new-events + (cond ((eventp arg) (list arg)) + ((listp arg) arg) + ((sequencep arg) + (listify-key-sequence arg)) + (t (error + "vip-set-unread-command-events: Invalid arg, %S" + arg))))) + (if (not (eventp nil)) + (setq new-events (delq nil new-events))) + (append new-events unread-command-events))) + ;; XEmacs + (cond ((numberp arg) + (setq unread-command-event (character-to-event arg))) + ((eventp arg) + (setq unread-command-event arg)) + ((sequencep arg) + (let ((length (length arg)) + (count 0)) + (while (< count length) + (enqueue-eval-event + 'vip-fudge-event-list-in-xemacs + (if (stringp arg) + (character-to-event (elt arg count)) + (elt arg count))) + (setq count (1+ count)) + ) ; while + (if (> length 0) + (or arg unread-command-event)))) + (t (error "vip-set-unread-command-events: Invalid argument"))))) + +(defun vip-fudge-event-list-in-xemacs (arg) + (setq unread-command-event arg)) + + +;;; Bring in the rest of the files +(require 'viper-mous) +(require 'viper-macs) +(require 'viper-ex) + + + +;; The following is provided for compatibility with older VIP's + +(defalias 'vip-change-mode-to-vi 'vip-change-state-to-vi) +(defalias 'vip-change-mode-to-insert 'vip-change-state-to-insert) +(defalias 'vip-change-mode-to-emacs 'vip-change-state-to-emacs) + +;; This was the main Vi mode in old versions of VIP which may have been +;; extensively used by VIP users. We declare it as a global var +;; and, after .vip is loaded, we add this keymap to vip-vi-basic-map. +(defvar vip-mode-map (make-sparse-keymap) + "This was the main Vi-mode keymap in the old versions of VIP. +Viper provides this variable for compatibility. Whatever the user defines +for this map, is merged into Viper's vip-vi-basic-map after loading .vip") + + + +;; Load .vip and setup hooks +(defun vip-shell-mode-hook () + "Hook specifically designed to enable Vi-style editing in shell mode." + (setq vip-add-newline-at-eob nil) + ;; this is nicer in shell mode + (setq vip-ex-style-editing-in-insert nil + vip-ex-style-motion nil) + (vip-add-local-keys 'vi-state + '(("\C-m" . comint-send-input) ; return + ("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d + (vip-add-local-keys 'insert-state + '(("\C-m" . comint-send-input) ; return + ("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d + ) + + +;; This sets major mode hooks to make them come up in vip-state. +(defun vip-set-hooks () + + ;; It is of course a misnomer to call viper-mode a `major mode'. + ;; However, this has the effect that if the user didn't specify the + ;; default mode, new buffers that fall back on the default will come up + ;; in Fundamental Mode and Vi state. + (setq default-major-mode 'viper-mode) + + (defadvice fundamental-mode (after vip-fundamental-mode-ad activate) + (vip-change-state-to-vi)) + + ;; The following major modes should come up in vi-state + (defvar emacs-lisp-mode-hook nil) + (add-hook 'emacs-lisp-mode-hook 'viper-mode) + + (defvar lisp-mode-hook nil) + (add-hook 'lisp-mode-hook 'viper-mode) + + (defvar bibtex-mode-hook nil) + (add-hook 'bibtex-mode-hook 'viper-mode) + + (defvar cc-mode-hook nil) + (add-hook 'cc-mode-hook 'viper-mode) + + (defvar c-mode-hook nil) + (add-hook 'c-mode-hook 'viper-mode) + + (defvar c++-mode-hook nil) + (add-hook 'c++-mode-hook 'viper-mode) + + (defvar lisp-interaction-mode-hook nil) + (add-hook 'lisp-interaction-mode-hook 'viper-mode) + + (defvar text-mode-hook nil) + (add-hook 'text-mode-hook 'viper-mode) + + (add-hook 'completion-list-mode-hook 'viper-mode) + (add-hook 'compilation-mode-hook 'viper-mode) + + (defvar emerge-startup-hook nil) + (add-hook 'emerge-startup-hook 'vip-change-state-to-emacs) + ;; Run vip-change-state-to-vi after quitting emerge. + (vip-eval-after-load "emerge" + '(defadvice emerge-quit (after vip-emerge-advice activate) + "Run vip-change-state-to-vi after quitting emerge." + (vip-change-state-to-vi))) + ;; In case Emerge was loaded before Viper. + (defadvice emerge-quit (after vip-emerge-advice activate) + "Run vip-change-state-to-vi after quitting emerge." + (vip-change-state-to-vi)) + + (vip-eval-after-load "asm-mode" + '(defadvice asm-mode (after vip-asm-mode-ad activate) + "Run vip-change-state-to-vi on entry." + (vip-change-state-to-vi))) + + ;; passwd.el sets up its own buffer, which turns up in Vi mode, + ;; overriding the local map. Noone needs Vi mode here. + (vip-eval-after-load + "passwd" + '(defadvice read-passwd-1 (before vip-passwd-ad activate) + "Vi-ism is prohibited when reading passwords, so switch to Emacs." + (vip-change-state-to-emacs))) + + ;; Emacs shell + (defvar shell-mode-hook nil) + (add-hook 'shell-mode-hook 'vip-change-state-to-insert) + (add-hook 'shell-mode-hook 'vip-shell-mode-hook) + + ;; Shell scripts + (defvar sh-mode-hook nil) + (add-hook 'sh-mode-hook 'viper-mode) + + ;; Dired + ;; This is only necessary when the user uses vip-modify-major-mode + (add-hook 'dired-mode-hook 'vip-change-state-to-emacs) + + (defvar view-hook nil + "View hook. Run after view mode.") + (add-hook 'view-hook 'vip-change-state-to-emacs) + + ;; For VM users. + ;; Put summary and other VM buffers in Emacs state. + (defvar vm-mode-hooks nil + "This hook is run after vm is started.") + (defvar vm-summary-mode-hooks nil + "This hook is run after vm switches to summary mode.") + (add-hook 'vm-mode-hooks 'vip-change-state-to-emacs) + (add-hook 'vm-summary-mode-hooks 'vip-change-state-to-emacs) + + ;; For RMAIL users. + ;; Put buf in Emacs state after edit. + (vip-eval-after-load + "rmailedit" + '(defadvice rmail-cease-edit (after vip-rmail-advice activate) + "Switch buffer to emacs-state after finishing with editing a message." + (vip-change-state-to-emacs))) + ;; In case RMAIL was loaded before Viper. + (defadvice rmail-cease-edit (after vip-rmail-advice activate) + "Switch buffer to emacs-state after finishing with editing a message." + (vip-change-state-to-emacs)) + ) ; vip-set-hooks + + +;; ~/.vip is loaded if it exists +(if (and (file-exists-p vip-custom-file-name) + (not noninteractive)) + (load vip-custom-file-name)) + +;; VIP compatibility: merge whatever the user has in vip-mode-map into +;; Viper's basic map. +(vip-add-keymap vip-mode-map vip-vi-global-user-map) + + +;; Applying Viper customization -- runs after (load .vip) + +;; Save user settings or Viper defaults for vars controled by vip-expert-level +(setq vip-saved-user-settings + (list (cons 'vip-want-ctl-h-help vip-want-ctl-h-help) + (cons 'vip-always vip-always) + (cons 'vip-no-multiple-ESC vip-no-multiple-ESC) + (cons 'vip-ex-style-motion vip-ex-style-motion) + (cons 'vip-ex-style-editing-in-insert + vip-ex-style-editing-in-insert) + (cons 'vip-want-emacs-keys-in-vi vip-want-emacs-keys-in-vi) + (cons 'vip-want-emacs-keys-in-insert vip-want-emacs-keys-in-insert) + (cons 'vip-re-search vip-re-search))) + + +(vip-set-minibuffer-style) +(vip-set-minibuffer-faces) +(vip-set-search-face) + +;;; Familiarize Viper with some minor modes that have their own keymaps +(vip-harness-minor-mode "compile") +(vip-harness-minor-mode "outline") +(vip-harness-minor-mode "allout") +(vip-harness-minor-mode "xref") +(vip-harness-minor-mode "lmenu") +(vip-harness-minor-mode "vc") +(vip-harness-minor-mode "ltx-math") ; LaTeX-math-mode in AUC-TeX +(vip-harness-minor-mode "latex") ; latex they moved math mode here + + +;; Intercept maps could go in viper-keym.el +;; We keep them here in case someone redefines them in ~/.vip + +(define-key vip-vi-intercept-map vip-ESC-key 'vip-intercept-ESC-key) +(define-key vip-insert-intercept-map vip-ESC-key 'vip-intercept-ESC-key) + +;; This is taken care of by vip-insert-global-user-map. +;;(define-key vip-replace-map vip-ESC-key 'vip-intercept-ESC-key) + +(define-key vip-insert-intercept-map vip-toggle-key 'vip-alternate-ESC) +;; The default vip-toggle-key is \C-z; for the novice, it suspends or +;; iconifies Emacs +(define-key vip-vi-intercept-map vip-toggle-key + '(lambda () (interactive) + (if (and (< vip-expert-level 2) (equal vip-toggle-key "\C-z")) + (if window-system (vip-iconify) (suspend-emacs)) + (vip-change-state-to-emacs)))) + +(define-key vip-emacs-intercept-map vip-toggle-key 'vip-change-state-to-vi) + + +(if (or vip-always + (and (< vip-expert-level 5) (> vip-expert-level 0))) + (vip-set-hooks)) + +;; Let all minor modes take effect after loading +;; this may not be enough, so we also set default minor-mode-alist. +;; Without setting the default, new buffers that come up in emacs mode have +;; minor-mode-map-alist = nil, unless we call vip-change-state-* +(if (eq vip-current-state 'emacs-state) + (progn + (vip-change-state-to-emacs) + (setq-default minor-mode-map-alist minor-mode-map-alist) + )) + +;; set some useful macros + +;; repeat the 2nd previous command without rotating the command history +(vip-record-kbd-macro + (vector vip-repeat-from-history-key '\1) 'vi-state + [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't) +;; repeat the 3d previous command without rotating the command history +(vip-record-kbd-macro + (vector vip-repeat-from-history-key '\2) 'vi-state + [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't) + +;; toggle case sensitivity in search +(vip-record-kbd-macro + "//" 'vi-state + [1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return] 't) +;; toggle regexp/vanila search +(vip-record-kbd-macro + "///" 'vi-state + [2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return] 't) + + +(run-hooks 'vip-load-hooks) ; the last chance to change anything + +(provide 'viper) +(provide 'vip19) +(provide 'vip) + +;;; viper.el ends here +