Function: ex-write
ex-write is a byte-compiled function defined in viper-ex.el.gz.
Signature
(ex-write Q-FLAG)
Source Code
;; Defined in /usr/src/emacs/lisp/emulation/viper-ex.el.gz
;; Ex write command
;; ex-write doesn't support wildcards, because file completion is a better
;; mechanism. We also don't support # and %
;; because file history is a better mechanism.
(defun ex-write (q-flag)
(viper-default-ex-addresses t)
(viper-get-ex-file)
(let ((end (car ex-addresses))
(beg (car (cdr ex-addresses)))
(orig-buf (current-buffer))
;;(orig-buf-file-name (buffer-file-name))
;;(orig-buf-name (buffer-name))
;;(buff-changed-p (buffer-modified-p))
temp-buf writing-same-file region
file-exists writing-whole-file)
(if (> beg end) (error viper-FirstAddrExceedsSecond))
(if ex-cmdfile
(progn
(viper-enlarge-region beg end)
(shell-command-on-region (point) (mark t)
(concat ex-file ex-cmdfile-args)))
(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)))
;; if ex-file is a directory use the file portion of the buffer file name
(if (and (file-directory-p ex-file)
buffer-file-name
(not (file-directory-p buffer-file-name)))
(setq ex-file
(concat (file-name-as-directory ex-file)
(file-name-nondirectory buffer-file-name))))
(setq file-exists (file-exists-p ex-file)
writing-same-file (string= ex-file (buffer-file-name)))
;; do actual writing
(if (and writing-whole-file writing-same-file)
;; saving whole buffer in visited file
(if (not (buffer-modified-p))
(message "(No changes need to be saved)")
(viper-maybe-checkout (current-buffer))
(save-buffer)
(save-restriction
(widen)
(ex-write-info file-exists ex-file (point-min) (point-max))
))
;; writing to non-visited file and it already exists
(if (and file-exists (not writing-same-file)
(not (yes-or-no-p
(format "File %s exists. Overwrite? " ex-file))))
(error "Quit"))
;; writing a region or whole buffer to non-visited file
(unwind-protect
(save-excursion
(viper-enlarge-region beg end)
(setq region (buffer-substring (point) (mark t)))
;; create temp buffer for the region
(setq temp-buf (get-buffer-create " *ex-write*"))
(set-buffer temp-buf)
(set-visited-file-name ex-file 'noquery)
(erase-buffer)
(if (and file-exists ex-append)
(insert-file-contents ex-file))
(goto-char (point-max))
(insert region)
;; ask user
(viper-maybe-checkout (current-buffer))
(setq selective-display nil)
(save-buffer)
(ex-write-info
file-exists ex-file (point-min) (point-max))
)
;; this must be under unwind-protect so that
;; temp-buf will be deleted in case of an error
(set-buffer temp-buf)
(set-buffer-modified-p nil)
(kill-buffer temp-buf)
;; buffer/region has been written, now take care of details
(set-buffer orig-buf)))
;; set the right file modification time
(if (and (buffer-file-name) writing-same-file)
(set-visited-file-modtime))
;; prevent loss of data if saving part of the buffer in visited file
(or writing-whole-file
(not writing-same-file)
(progn
(sit-for 2)
(message "Warning: you have saved only part of the buffer!")
(set-buffer-modified-p t)))
(if q-flag
(if (< viper-expert-level 2)
(save-buffers-kill-emacs)
(kill-buffer (current-buffer))))
)))