Subject: Re: bug#24837: 26.0.50; term.el: In char mode, displayed and executed commands can differ

From: Phil Sainty <[email-masked]>
Date: Mon, 9 Oct 2017 01:39:41 +1300

On 29/09/17 21:37, Eli Zaretskii wrote:
>> On 2017-09-25 13:48, Phil Sainty wrote:
>> Thinking on this further, it might be even better to use 
>> pre-command-hook
>> to establish whether the pre-command position of point is equal to the
>> process mark, and then act conditionally on that in post-command-hook,
>> so that if the pre-command point did not already match the process mark,
>> we do *not* forcibly move it to the process mark afterwards.
> 
> Do you have a patch along these lines?

I've found a little time to work on this some more.

New patch attached, with the aforementioned changes.

I think the remaining task would be to add user options to disable
the new behaviours, as some users might object to them?

-Phil
>From 01ebb45b5e6bc22a5db55343bb6084d9a1800f2e Mon Sep 17 00:00:00 2001
From: Phil Sainty <[email-masked]>
Date: Sun, 3 Sep 2017 14:30:18 +1200
Subject: [PATCH] Avoid creating inconsistent buffer states in term-char-mode
 (bug#24837)

* lisp/term.el (term-mode, term-char-mode, term-line-mode)
(term-emulate-terminal): Make buffer read-only in `term-char-mode',
except for the process filter's output.  Use `read-only-mode-hook' to
track and restore the user-set state of `buffer-read-only' for
`term-line-mode'.

term-line-mode-buffer-read-only: New variable.
(term-line-mode-buffer-read-only-update): New function.

(term-char-mode, term-line-mode): Use `term-set-goto-process-mark'
in pre-command-hook, and `term-goto-process-mark-maybe' in
post-command-hook to counter-act unexpected changes to point.

term-goto-process-mark: New buffer-local variable.
(term-set-goto-process-mark): New function.
(term-goto-process-mark-maybe): New function.
(term-process-mark): New function.
---
 lisp/term.el | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/lisp/term.el b/lisp/term.el
index c748c45..32dcb5a 100644
--- a/lisp/term.el
+++ b/lisp/term.el
@@ -427,6 +427,8 @@ term-old-mode-map
 (defvar term-old-mode-line-format) ; Saves old mode-line-format while paging.
 (defvar term-pager-old-local-map nil "Saves old keymap while paging.")
 (defvar term-pager-old-filter) ; Saved process-filter while paging.
+(defvar-local term-line-mode-buffer-read-only nil
+  "The `buffer-read-only' state to set in `term-line-mode'.")
 
 (defcustom explicit-shell-file-name nil
   "If non-nil, is file name to use for explicitly requested inferior shell."
@@ -1105,6 +1107,8 @@ term-mode
                     (term-reset-size (cdr size) (car size)))
                   size))
 
+  (add-hook 'read-only-mode-hook #'term-line-mode-buffer-read-only-update nil t)
+
   (easy-menu-add term-terminal-menu)
   (easy-menu-add term-signals-menu)
   (or term-input-ring
@@ -1246,6 +1250,12 @@ term-char-mode
     (easy-menu-add term-terminal-menu)
     (easy-menu-add term-signals-menu)
 
+    ;; Don't allow changes to the buffer or to point which are not
+    ;; caused by the process filter.
+    (setq buffer-read-only 1)
+    (add-hook 'pre-command-hook #'term-set-goto-process-mark nil t)
+    (add-hook 'post-command-hook #'term-goto-process-mark-maybe nil t)
+
     ;; Send existing partial line to inferior (without newline).
     (let ((pmark (process-mark (get-buffer-process (current-buffer))))
 	  (save-input-sender term-input-sender))
@@ -1265,9 +1275,17 @@ term-line-mode
 you type \\[term-send-input] which sends the current line to the inferior."
   (interactive)
   (when (term-in-char-mode)
+    (setq buffer-read-only term-line-mode-buffer-read-only)
+    (remove-hook 'pre-command-hook #'term-set-goto-process-mark t)
+    (remove-hook 'post-command-hook #'term-goto-process-mark-maybe t)
     (use-local-map term-old-mode-map)
     (term-update-mode-line)))
 
+(defun term-line-mode-buffer-read-only-update ()
+  "Update the user-set state of `buffer-read-only' in `term-line-mode'."
+  (when (term-in-line-mode)
+    (setq term-line-mode-buffer-read-only buffer-read-only)))
+
 (defun term-update-mode-line ()
   (let ((term-mode
          (if (term-in-char-mode)
@@ -2711,6 +2729,7 @@ term-emulate-terminal
 	   count-bytes ; number of bytes
 	   decoded-substring
 	   save-point save-marker old-point temp win
+	   (inhibit-read-only t)
 	   (buffer-undo-list t)
 	   (selected (selected-window))
 	   last-win
@@ -3109,6 +3128,39 @@ term-emulate-terminal
     (when (get-buffer-window (current-buffer))
       (redisplay))))
 
+(defvar-local term-goto-process-mark t
+  "Whether to reset point to the current process mark after each command.
+
+Set in `pre-command-hook' by `term-set-goto-process-mark'.")
+
+(defun term-set-goto-process-mark ()
+  "Sets `term-goto-process-mark'.  Called during `pre-command-hook'.
+
+Ensures that when point is equal to the process mark at the
+pre-command stage, it will be returned to the process mark at the
+post-command stage.  See also `term-goto-process-mark-maybe'."
+  (setq term-goto-process-mark
+        (eq (point) (marker-position (term-process-mark)))))
+
+(defun term-goto-process-mark-maybe ()
+  "Move point to the term buffer's process mark upon keyboard input.
+
+Used as a buffer-local `post-command-hook' function in
+`term-char-mode' to prevent commands from putting the buffer into
+an inconsistent state by unexpectedly moving point.
+
+Only acts when the pre-command position of point was equal to the
+current process mark.
+
+Mouse events are ignored so that mouse selection is unimpeded."
+  (unless (mouse-event-p last-command-event)
+    (when term-goto-process-mark
+      (goto-char (term-process-mark)))))
+
+(defun term-process-mark ()
+  "The current process-mark for the term buffer process."
+  (process-mark (get-buffer-process (current-buffer))))
+
 (defun term-handle-deferred-scroll ()
   (let ((count (- (term-current-row) term-height)))
     (when (>= count 0)
-- 
2.8.3