1

I use Julia REPL, Singular or some other math software in my emacs. In every case, I have a buffer (*julia* or *Singular*,...) synchronise with some running process. The last line of this buffer is where I introduce the commands to execute. This last line is something like

julia > 

or

> 

So, I would like to add the property cursor-intangible to this text, since I will never edit it and it would be much more similar to an actual terminal. I know the commands add-text-property and so on, but I would like something that works automatically every time that the buffer changes. Does it exists? It would be something similar to font-lock but for properties.

MonLau
  • 81
  • 7
  • 1
    Whoever writes up the answer would need to examine the code that generates the prompt julia > and > to see if some text property support is built-in (e.g., a color or something like that), or whether it needs to be added contemporaneously when the prompt is generated, or perhaps a solution has already been written up somewhere on the internet. Rather than having everyone Google the terms Julia REPL Emacs, perhaps you can provide a link to the source code that you are using ... – lawlist Nov 24 '19 at 21:08
  • For julia I use ess (emacs speaks statistics) and julia-mode and for Singular https://github.com/Singular/Sources/tree/spielwiese/emacs – MonLau Nov 24 '19 at 21:29
  • But that would be an inconvenient solution, I am looking for a solution for all math software. Is not there some hook, function... something that adds/sets text properties every time a buffer is modified? Something like font-lock-add-keywords – MonLau Nov 24 '19 at 21:36
  • Here is a link to the manual describing the standard hooks: https://www.gnu.org/software/emacs/manual/html_node/elisp/Standard-Hooks.html One hook on the list that stands out is the after-change-functions hook ... "List of functions to call after each text change....". There may be other hooks on the list that could be of assistance. – lawlist Nov 24 '19 at 22:10
  • Does setting comint-prompt-read-only to t work? Otherwise, I believe that you can adapt line 2113 to 2124 in https://github.com/emacs-mirror/emacs/blob/master/lisp/comint.el to get what you want (assuming that the REPL's are powered by comint). – T. Verron Nov 24 '19 at 22:49
  • It partially works, now I cannot edit the prompt but the cursor still moves into it, for example with move-beginning-of-line. I will try to modify lines 2113--2124, that will work for every mode using comint – MonLau Nov 24 '19 at 23:02

1 Answers1

1

Solution proposed by @T.Verron works, nevertheless sadly singular-interactive-mode do not use comint. So, here I propose another solution following @lawist suggestion. This code produces a function brust-math-software-hookfun-to-intangify-prompt, which adds buffer-locally a function to after-change-functions which properly properties the prompt accordingly to the list brust-math-software-buffers-prompts. You only need to add such a function to the hook of your mode. And to add new languages you just need to update the list of buffers-prompts.

    (defvar brust-math-software-buffers-prompts
      '(("*julia*"    . "^julia>")
        ("*singular*" . "^>"))
      "List of cons with buffer names runing some math software and a regex for its promp string")

    (defun brust-math-software-intangify-buffer-text (-regexp beg end)
      "Set cursor-intangible property to all buffer text maching regular expresion `-regexp` between `beg` and  `end`"
      (save-excursion
        (goto-char beg)
        (save-match-data
          (while (re-search-forward -regexp end t)
            (add-text-properties (1- (match-beginning 0)) (match-end 0) '(cursor-intangible t rear-nonsticky nil))))))

    (defun brust-math-software-intangify-cursor-on-prompt (beg end length)
      "Set cursor-intangible in math software buffers prompts"
      (let ((-prompt (cdr (assoc (buffer-name) brust-math-software-buffers-prompts))))
        (when -prompt
          (brust-math-software-intangify-buffer-text -prompt beg end))))

    (defun brust-math-software-hookfun-to-intangify-prompt nil
      (cursor-intangible-mode 1)
      (add-hook 'after-change-functions #'brust-math-software-intangify-cursor-on-prompt nil t))

I have no idea why, but there is some incompatibility between properties cursor-intangible and rear-nonsticky. So, the lines 2113--2124 from @T.Verron answer would be:

      (add-text-properties prompt-start (point)
                           '(cursor-intangible t rear-nonsticky nil read-only t front-sticky (read-only)))))
MonLau
  • 81
  • 7