;; sacrilege.el -- Edit buffers using nvi ;; Copyright (C) 2008 Christian Neukirchen ;; Licensed under the same terms as Emacs itself. ;; Dedicated to everyone claiming Emacs is a nice operating system ;; that doesn't have a good editor. ;; Version: 0.2 ;; 02jul2008 +chris+ (define-derived-mode nvi-mode term-mode "nvi" "Major mode for nvi sessions." (make-local-variable 'original-buffer) (term-char-mode) (setq header-line-format nil) (let ((proc (get-buffer-process (current-buffer)))) (set-process-sentinel proc 'nvi-sentinel) (term-check-size proc))) (defun nvi-sentinel (proc msg) "Sentinel for nvi buffers. The main purpose is to revert the original buffer with the updated file." (let ((buffer (process-buffer proc))) (when (memq (process-status proc) '(signal exit)) (if (null (buffer-name buffer)) ;; buffer killed (set-process-buffer proc nil) (unwind-protect (progn (term-handle-exit (process-name proc) msg) ;; Since the buffer and mode line will show that the ;; process is dead, we can delete it now. Otherwise it ;; will stay around until M-x list-processes. (delete-process proc) (set-buffer buffer) (switch-to-buffer original-buffer) (if (buffer-modified-p) ; don't ask if nothing changed anyway (revert-buffer) (revert-buffer t t t)) (kill-buffer buffer) (switch-to-buffer original-buffer) )))))) (defun nvi () "Edit the current buffer in a nvi session. It saves the buffer, opens it in an nvi session and afterwards restores the content from the file. Sometimes, there is just the right time for hjkl!" (interactive) (let ((buffer (current-buffer)) (file (buffer-file-name)) (line (line-number-at-pos)) (bufnam (buffer-name))) (if (not file) (error "Buffer does not contain file contents.") (save-buffer) (switch-to-buffer (make-term bufnam "nvi" nil (format "+%d" line) file)) (rename-buffer (concat bufnam "[nvi]") t) (setq original-buffer buffer) (nvi-mode)))) (defun sacrilege () "Sin by using nvi." (interactive) (nvi) (message "You better regret!")) (provide 'sacrilege) ;;; sacrilege.el ends here