;;; viper-ex.el --- functions implementing the Ex commands for Viper
-;; Copyright (C) 1994, 95, 96, 97, 98, 2000, 01, 02 Free Software Foundation, Inc.
+;; Copyright (C) 1994-1998, 2000-2012 Free Software Foundation, Inc.
;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
+;; Package: viper
;; This file is part of GNU Emacs.
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; 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.
+;; the Free Software Foundation, either version 3 of the License, 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
;; 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, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; in order to spare non-viperized emacs from being viperized
(if noninteractive
(eval-when-compile
- (let ((load-path (cons (expand-file-name ".") load-path)))
- (or (featurep 'viper-util)
- (load "viper-util.el" nil nil 'nosuffix))
- (or (featurep 'viper-keym)
- (load "viper-keym.el" nil nil 'nosuffix))
- (or (featurep 'viper-cmd)
- (load "viper-cmd.el" nil nil 'nosuffix))
- )))
+ (if (not (featurep 'viper-cmd))
+ (require 'viper-cmd))
+ ))
;; end pacifier
(require 'viper-util)
(defgroup viper-ex nil
- "Viper support for Ex commands"
+ "Viper support for Ex commands."
:prefix "ex-"
:group 'viper)
;;; Variables
(defconst viper-ex-work-buf-name " *ex-working-space*")
-(defconst viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
+(defvar viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
(defconst viper-ex-tmp-buf-name " *ex-tmp*")
(defconst viper-ex-print-buf-name " *ex-print*")
-(defconst viper-ex-print-buf (get-buffer-create viper-ex-print-buf-name))
+(defvar viper-ex-print-buf (get-buffer-create viper-ex-print-buf-name))
;;; ex-commands...
;; If this is a one-letter magic command, splice in args.
(defun ex-splice-args-in-1-letr-cmd (key list)
- (let ((onelet (ex-cmd-is-one-letter (assoc (substring key 0 1) list))))
- (if onelet
+ (let ((oneletter (ex-cmd-is-one-letter (assoc (substring key 0 1) list))))
+ (if oneletter
(list key
- (append (cadr onelet)
+ (append (cadr oneletter)
(if (< 1 (length key)) (list (substring key 1))))
- (caddr onelet)))
+ (car (cdr (cdr oneletter))) ))
))
(defvar ex-addresses nil)
;; This flag is supposed to be set only by `#', `print', and `list',
-;; none of which is implemented. So, it and the pices of the code it
+;; none of which is implemented. So, it and the pieces of the code it
;; controls are dead weight. We keep it just in case this might be
;; needed in the future.
(defvar ex-flag nil)
(defcustom viper-glob-function
(cond (ex-unix-type-shell 'viper-glob-unix-files)
- ((eq system-type 'emx) 'viper-glob-mswindows-files) ; OS/2
(viper-ms-style-os-p 'viper-glob-mswindows-files) ; Microsoft OS
- (viper-vms-os-p 'viper-glob-unix-files) ; VMS
(t 'viper-glob-unix-files) ; presumably UNIX
)
"Expand the file spec containing wildcard symbols.
The default tries to set this variable to work with Unix, Windows,
-OS/2, and VMS.
+and OS/2.
However, if it doesn't work right for some types of Unix shells or some OS,
the user should supply the appropriate function and set this variable to the
(defvar viper-keep-reading-filename nil)
(defcustom ex-cycle-other-window t
- "*If t, :n and :b cycles through files and buffers in other window.
+ "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."
:type 'boolean
:group 'viper-ex)
(defcustom ex-cycle-through-non-files nil
- "*Cycle through *scratch* and other buffers that don't visit any file."
+ "Cycle through *scratch* and other buffers that don't visit any file."
:type 'boolean
:group 'viper-ex)
((eq ex-token-type 'minus) 'sub-number)
(t 'abs-number)))
(setq ex-token
- (string-to-int (buffer-substring (point) (mark t)))))
+ (string-to-number (buffer-substring (point) (mark t)))))
((looking-at "\\$")
(forward-char 1)
(setq ex-token-type 'end))
(setq initial-str (format "%d,%d" reg-beg-line reg-end-line)))
(setq com-str
- (or string (viper-read-string-with-history
- ":"
- initial-str
- 'viper-ex-history
- ;; no default when working on region
- (if initial-str
- nil
- (car viper-ex-history))
- map
- (if initial-str
- " [Type command to execute on current region]"))))
+ (if string
+ (concat initial-str string)
+ (viper-read-string-with-history
+ ":"
+ initial-str
+ 'viper-ex-history
+ ;; no default when working on region
+ (if initial-str
+ nil
+ (car viper-ex-history))
+ map
+ (if initial-str
+ " [Type command to execute on current region]"))))
(save-window-excursion
;; just a precaution
(setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
(format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c)))
(setq cont nil)
;; we are at an escaped delimiter: unescape it and continue
- (delete-backward-char 2)
+ (delete-char -2)
(insert c)
(if (eolp)
;; if at eol, exit loop and go to next line
(progn
(set-mark (point))
(re-search-forward "[0-9][0-9]*")
- (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
+ (setq ex-count (string-to-number (buffer-substring (point) (mark t))))
(skip-chars-forward " \t")))
(if (looking-at "[pl#]")
(progn
(progn
(set-mark (point))
(re-search-forward "[0-9][0-9]*")
- (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
+ (setq ex-count (string-to-number (buffer-substring (point) (mark t))))
(skip-chars-forward " \t")))
(if (looking-at "[pl#]")
(progn
;; Expand \% and \# in ex command
(defun ex-expand-filsyms (cmd buf)
(let (cf pf ret)
- (save-excursion
- (set-buffer buf)
+ (with-current-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 viper-ex-tmp-buf-name))
+ (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name)
(erase-buffer)
(insert cmd)
(goto-char (point-min))
ex-cmdfile nil
ex-cmdfile-args "")
(save-excursion
- (save-window-excursion
- (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
- (set-buffer viper-ex-work-buf)
+ (with-current-buffer (setq viper-ex-work-buf
+ (get-buffer-create viper-ex-work-buf-name))
(skip-chars-forward " \t")
(if (looking-at "!")
(if (and (not (viper-looking-back "[ \t]"))
beg end cont val)
(viper-add-keymap ex-read-filename-map
- (if viper-emacs-p
+ (if (featurep 'emacs)
minibuffer-local-completion-map
read-file-name-map))
(read-string "[Hit return to confirm] ")
(quit
(save-excursion (kill-buffer " *delete text*"))
- (error "")))
+ (error "Viper bell")))
(save-excursion (kill-buffer " *delete text*")))
(if ex-buffer
(cond ((viper-valid-register ex-buffer '(Letter))
(switch-to-buffer file))
(if ex-offset
(progn
- (save-window-excursion
- (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
- (set-buffer viper-ex-work-buf)
+ (with-current-buffer (setq viper-ex-work-buf
+ (get-buffer-create viper-ex-work-buf-name))
(delete-region (point-min) (point-max))
(insert ex-offset "\n")
(goto-char (point-min)))
(if (bobp) (setq cont nil)
(forward-line -1)
(end-of-line)))))
- (save-window-excursion
- (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
- (set-buffer viper-ex-work-buf)
+ (with-current-buffer (setq viper-ex-work-buf
+ (get-buffer-create viper-ex-work-buf-name))
;; com-str is the command string, i.e., g/pattern/ or v/pattern'
(setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
(while ex-g-marks
(if (eq 1 (length name))
(setq char (string-to-char name))
(error "`%s': Spurious text \"%s\" after mark name"
- name (substring name 1) viper-SpuriousText))
- (save-window-excursion
- (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
- (set-buffer viper-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 viper-SpuriousText)))
- (error "`%s' requires a following letter" ex-token))))
+ name (substring name 1)))
+ (with-current-buffer (setq viper-ex-work-buf
+ (get-buffer-create viper-ex-work-buf-name))
+ (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 viper-SpuriousText)))
+ (error "`%s' requires a following letter" ex-token))))
(save-excursion
(goto-char (car ex-addresses))
(point-to-register (viper-int-to-char (1+ (- char ?a)))))))
(progn
(ex-edit t)
(throw 'ex-edit nil))
- (setq count (string-to-int ex-file))
+ (setq count (string-to-number ex-file))
(if (= count 0) (setq count 1))
(if (< count 0) (error "Usage: `next <count>' (count >= 0)"))))
(setq count 1))
;; setup buffer
(if (setq wind (viper-get-visible-buffer-window buf))
()
- (setq wind (get-lru-window (if viper-xemacs-p nil 'visible)))
+ (setq wind (get-lru-window (if (featurep 'xemacs) nil 'visible)))
(set-window-buffer wind buf))
(if (viper-window-display-p)
(select-window wind)))
(save-window-excursion (select-window wind) (sit-for 1)))
- (save-excursion
- (set-buffer buf)
+ (with-current-buffer buf
(setq viper-related-files-and-buffers-ring old-ring))
(setq viper-local-search-start-marker (point-marker))
;; Ex print working directory
(defun ex-pwd ()
- (message default-directory))
+ (message "%s" default-directory))
;; Ex quit command
(defun ex-quit ()
;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
- (save-excursion
- (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
- (set-buffer viper-ex-work-buf)
+ (with-current-buffer (setq viper-ex-work-buf
+ (get-buffer-create viper-ex-work-buf-name))
(if (looking-at "!") (forward-char 1)))
(if (< viper-expert-level 3)
(save-buffers-kill-emacs)
(progn
(setq command
;; replace # and % with the previous/current file
- (ex-expand-filsyms (concat ex-file ex-cmdfile-args)
- (current-buffer)))
+ (ex-expand-filsyms
+ (concat (shell-quote-argument ex-file) ex-cmdfile-args)
+ (current-buffer)))
(shell-command command t))
(insert-file-contents ex-file)))
(ex-fixup-history viper-last-ex-prompt ex-file ex-cmdfile-args))
(setq var "blink-matching-paren"
val "nil"))
((member var '("ws" "wrapscan"))
- (setq var "viper-search-wrap-around-t"
+ (setq var "viper-search-wrap-around"
val "t"))
((member var '("nows" "nowrapscan"))
- (setq var "viper-search-wrap-around-t"
+ (setq var "viper-search-wrap-around"
val "nil")))
(if (and set-cmd (eq val 0)) ; value must be set by the user
(let ((cursor-in-echo-area t))
;; 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
- (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
- (set-buffer viper-ex-work-buf)
+ (with-current-buffer (setq viper-ex-work-buf
+ (get-buffer-create viper-ex-work-buf-name))
(goto-char (point-min))
(re-search-forward regex-forw nil t)
(let ((beg (point))
(condition-case nil
(progn
(pop-to-buffer (get-buffer-create "*info*"))
- (info (if viper-xemacs-p "viper.info" "viper"))
+ (info (if (featurep 'xemacs) "viper.info" "viper"))
(message "Type `i' to search for a specific topic"))
(error (beep 1)
(with-output-to-temp-buffer " *viper-info*"
This file is part of the standard distribution of %sEmacs.
Please contact your system administrator. "
- (if viper-xemacs-p "X" "")
+ (if (featurep 'xemacs) "X" "")
))))))
;; Ex source command. Loads the file specified as argument or `~/.viper'
;; Ex tag command
(defun ex-tag ()
(let (tag)
- (save-window-excursion
- (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
- (set-buffer viper-ex-work-buf)
+ (with-current-buffer (setq viper-ex-work-buf
+ (get-buffer-create viper-ex-work-buf-name))
(skip-chars-forward " \t")
(set-mark (point))
(skip-chars-forward "^ |\t\n")
;; create temp buffer for the region
(setq temp-buf (get-buffer-create " *ex-write*"))
(set-buffer temp-buf)
- (viper-cond-compile-for-xemacs-or-emacs
- (set-visited-file-name ex-file) ; xemacs
- (set-visited-file-name ex-file 'noquerry) ; emacs
- )
+ (if (featurep 'xemacs)
+ (set-visited-file-name ex-file)
+ (set-visited-file-name ex-file 'noquery))
(erase-buffer)
(if (and file-exists ex-append)
(insert-file-contents ex-file))
;; Execute shell command
(defun ex-command ()
(let (command)
- (save-window-excursion
- (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
- (set-buffer viper-ex-work-buf)
+ (with-current-buffer (setq viper-ex-work-buf
+ (get-buffer-create viper-ex-work-buf-name))
(skip-chars-forward " \t")
(setq command (buffer-substring (point) (point-max)))
(end-of-line))
If no args are given, then it runs the last compile command.
Type 'mak ' (including the space) to run make with no args."
(let (args)
- (save-window-excursion
- (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
- (set-buffer viper-ex-work-buf)
+ (with-current-buffer (setq viper-ex-work-buf
+ (get-buffer-create viper-ex-work-buf-name))
(setq args (buffer-substring (point) (point-max)))
(end-of-line))
;; Remove the newline that may (will?) be at the end of the args
(pos2 (viper-line-pos 'end))
lines file info)
(setq lines (count-lines (point-min) (viper-line-pos 'end))
- file (if (buffer-file-name)
- (concat (viper-abbreviate-file-name (buffer-file-name)) ":")
- (concat (buffer-name) " [Not visiting any file]:"))
+ file (cond ((buffer-file-name)
+ (concat (viper-abbreviate-file-name (buffer-file-name)) ":"))
+ ((buffer-file-name (buffer-base-buffer))
+ (concat (viper-abbreviate-file-name (buffer-file-name (buffer-base-buffer))) " (indirect buffer):"))
+ (t (concat (buffer-name) " [Not visiting any file]:")))
info (format "line=%d/%d pos=%d/%d col=%d %s"
(if (= pos1 pos2)
(1+ lines)
(if (buffer-modified-p) "[Modified]" "[Unchanged]")))
(if (< (+ 1 (length info) (length file))
(window-width (minibuffer-window)))
- (message (concat file " " info))
+ (message "%s" (concat file " " info))
(save-window-excursion
(with-output-to-temp-buffer " *viper-info*"
(princ (concat "\n" file "\n\n\t" info "\n\n")))
(princ (if viper-re-search "magic\n" "nomagic\n"))
(princ (if buffer-read-only "readonly\n" "noreadonly\n"))
(princ (if blink-matching-paren "showmatch\n" "noshowmatch\n"))
- (princ (if viper-search-wrap-around-t "wrapscan\n" "nowrapscan\n"))
+ (princ (if viper-search-wrap-around "wrapscan\n" "nowrapscan\n"))
(princ (format "shiftwidth \t\t= %S\n" viper-shift-width))
(princ (format "tabstop (local) \t= %S\n" tab-width))
(princ (format "tabstop (global) \t= %S\n" (default-value 'tab-width)))