unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Replacing buffer contents --- without disturbing markers
@ 2010-09-10 23:11 Daniel Colascione
  2010-09-11  6:32 ` David Kastrup
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Daniel Colascione @ 2010-09-10 23:11 UTC (permalink / raw)
  To: emacs-devel

For a mode I'm writing, I have to temporarily replace certain parts of
the buffer with placeholder text. I'm using this function to do it:

(defun ntcmd-inplace-replace (replacement)
  "Replace the characters at point with REPLACEMENT without disturbing markers.

Leave point after replacement. The number of characters replaced
is the length of REPLACEMENT."

  (loop for c across replacement
        ;; Replace the character after point with the next character
        ;; from replacement. We must worry about two kinds
        ;; of marker: those pointing at point (including (point)), and
        ;; those pointing at (1+ (point)).
        ;;
        ;; Mentally run through the code below, and you'll see that
        ;; both kinds of marker are preserved.
        ;;
        do (progn
             (forward-char 1)
             (insert-before-markers c)
             (backward-char 1)
             (delete-char -1)
             (forward-char 1))))

Is it possible to do better? replace-match squashes markers.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Replacing buffer contents --- without disturbing markers
  2010-09-10 23:11 Replacing buffer contents --- without disturbing markers Daniel Colascione
@ 2010-09-11  6:32 ` David Kastrup
  2010-09-11 11:01 ` Stefan Monnier
  2010-09-11 15:49 ` Richard Stallman
  2 siblings, 0 replies; 4+ messages in thread
From: David Kastrup @ 2010-09-11  6:32 UTC (permalink / raw)
  To: emacs-devel

Daniel Colascione <dan.colascione@gmail.com> writes:

> For a mode I'm writing, I have to temporarily replace certain parts of
> the buffer with placeholder text. I'm using this function to do it:
>
> (defun ntcmd-inplace-replace (replacement)
>   "Replace the characters at point with REPLACEMENT without disturbing markers.
>
> Leave point after replacement. The number of characters replaced
> is the length of REPLACEMENT."
>
>   (loop for c across replacement
>         ;; Replace the character after point with the next character
>         ;; from replacement. We must worry about two kinds
>         ;; of marker: those pointing at point (including (point)), and
>         ;; those pointing at (1+ (point)).
>         ;;
>         ;; Mentally run through the code below, and you'll see that
>         ;; both kinds of marker are preserved.
>         ;;
>         do (progn
>              (forward-char 1)
>              (insert-before-markers c)
>              (backward-char 1)
>              (delete-char -1)
>              (forward-char 1))))
>
> Is it possible to do better? replace-match squashes markers.

Not sure whether make-indirect-buffer would keep the markers in piece.

-- 
David Kastrup




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Replacing buffer contents --- without disturbing markers
  2010-09-10 23:11 Replacing buffer contents --- without disturbing markers Daniel Colascione
  2010-09-11  6:32 ` David Kastrup
@ 2010-09-11 11:01 ` Stefan Monnier
  2010-09-11 15:49 ` Richard Stallman
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2010-09-11 11:01 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

>         ;; Replace the character after point with the next character
>         ;; from replacement. We must worry about two kinds
>         ;; of marker: those pointing at point (including (point)), and
>         ;; those pointing at (1+ (point)).

Actually there 4 kinds: at point, at point+1, multiplied by the two
insertion-types.  Your code works with the default insertion-type, but
not the other (don't worry: I don't think you can do much better).


        Stefan



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Replacing buffer contents --- without disturbing markers
  2010-09-10 23:11 Replacing buffer contents --- without disturbing markers Daniel Colascione
  2010-09-11  6:32 ` David Kastrup
  2010-09-11 11:01 ` Stefan Monnier
@ 2010-09-11 15:49 ` Richard Stallman
  2 siblings, 0 replies; 4+ messages in thread
From: Richard Stallman @ 2010-09-11 15:49 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

    For a mode I'm writing, I have to temporarily replace certain parts of
    the buffer with placeholder text. I'm using this function to do it:

In general, temporarily replacing text has some drawbacks.
If you can find a way to do the overall job without that method,'
it is likely to be better.




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-09-11 15:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-09-10 23:11 Replacing buffer contents --- without disturbing markers Daniel Colascione
2010-09-11  6:32 ` David Kastrup
2010-09-11 11:01 ` Stefan Monnier
2010-09-11 15:49 ` Richard Stallman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).