Subject: Re: bug#24021: FEATURE ADDITION: 25.0.94: goto-marker, jumps to a marker potentially in a different buffer

From: Robert Weiner <[email-masked]>
Date: Tue, 19 Jul 2016 09:15:49 -0400

Slightly revised version of the function below.

On Mon, Jul 18, 2016 at 8:05 PM, <[email-masked]> wrote:
>
>
> > (defun goto-marker (marker)
> >   "Make MARKER's buffer and position current."
> >   (interactive)
>
> The interactive spec doesn't match the parameter list.  I'm not sure if
> it makes sense for this to be interactive (how would the user enter a
> marker?).

The interactive spec has been removed.

>
>
> >   (cond ((not (markerp marker))
> >          (error "Invalid marker: %s" marker))
> >         ((not (marker-buffer marker))
> >          (error "Invalid marker buffer: %s" marker))
>
> I think these checks are redundant, you'll get the same errors when you
> call marker-buffer and set-buffer, below.

I like these checks as they make the specific error very clear.  set-buffer
will trigger a type error when given a nil buffer but neither marker-buffer
nor marker-position signal an error when given a marker that doesn't point
anywhere, so the checks prior to calling those functions are relevant.

>
> >         (t (let* ((buffer (marker-buffer marker))
> >                   (position (marker-position marker)))
> >              (set-buffer buffer)
> >              (or (and (>= position (point-min))
> >                       (<= position (point-max)))
> >                  (if widen-automatically
> >                      (widen)
> >                    (error "Marker position is outside accessible part
of buffer: %s" marker)))
> >              (goto-char position)
> >              (switch-to-buffer buffer)))))
>
> If this is just a "simple function" (not an interactive command), it
> shouldn't widen, or call switch-to-buffer.
>
>
>
> save-excursion or save-current-buffer don't counteract
>
> switch-to-buffer, because it affects the UI selected buffer. You might
>
> be right about the widen part, not sure.

You are right that save-excursion and save-restriction won't counteract the
effects of this function, so its purpose must be to put the selected
window's point at the location of the marker.  Possibly, a macro called
with-marker-location could be useful when one needs to temporarily set
buffer and point from a marker and evaluate some forms

but not affect the display.

Here is the slightly revised function.  -- Bob

(defun hypb:goto-marker (marker)
  "Make MARKER's buffer and position current.
If MARKER is invalid signal an error."
  (cond ((not (markerp marker))
(error "Invalid marker: %s" marker))
((not (marker-buffer marker))
(error "Invalid marker buffer: %s" marker))
(t (let* ((buffer (marker-buffer marker))
 (position (marker-position marker)))
    (set-buffer buffer)
    (unless (and (>= position (point-min))
 (<= position (point-max)))
      (if widen-automatically
  (widen)
(error "Marker position is outside accessible part of buffer: %s" marker)))
    (goto-char position)
    (switch-to-buffer buffer)))))