I have advised the narrow and widen functions so that the fringe color indicates if narrowing is active:

(advice-add 'narrow-to-region :after
            '(lambda (&rest args)
               (set-face-attribute 'fringe
                nil :background my-fringe-narrow-bg)))

(advice-add 'widen :after
            '(lambda (&rest args)
               (set-face-attribute 'fringe
                nil :background (face-attribute 'default :background))))

This works very well. However, sometimes the fringe gets colored by my advice while I did not call any narrowing function. One example is 'org-table-sort-lines: it calls 'narrow-to-region within a 'save-restriction block but does not call 'widen afterwards, of course.

So, how can I find out if 'narrow-to-region have been called from within 'save-restriction?

EDIT: Something like this would help as well:

(advice-add 'save-restriction :before
            '(lambda (&rest args)
               (ad-deactivate 'narrow-to-region)))    

(advice-add 'save-restriction :after
            '(lambda (&rest args)
               (ad-activate 'narrow-to-region)))

However this doesn't work, I get the error: advice--normalize: Advice impossible: save-restriction is a special form.

  • 1
    You might be able check the stack with backtrace or something, but I would suggest just checking the narrowing status in a post-command-hook instead. – npostavs Jun 4 '17 at 12:28
  • Well, but how do I find this out? And what if the buffer was already narrowed in the first place (since 'save-restriction restores narrowing's already in place)? I don't think this helps. – Tom Jun 4 '17 at 13:00
  • Just to make sure: you do know that whether the buffer is already indicated in the (default) mode-line, right? I guess you prefer showing it in the fringe, but just thought I'd mention this. – Drew Jun 4 '17 at 14:08
  • Please consider retitling the question. The accepted answer does not really say how to tell whether narrow-to-region was called within save-restriction. But it does answer what is apparently your real question. – Drew Jun 4 '17 at 14:59
  • Drew: yes I know about the indicator in the mode-line but I tend to overlook it. Also, I modified the title. – Tom Jun 4 '17 at 16:50

I agree with npostavs' suggestion. Something like:

(add-hook 'post-command-hook 'my-narrowed-fringe-status)

(defun my-narrowed-fringe-status ()
  "Make the fringe background reflect the buffer's narrowing status."
   'fringe nil :background (if (buffer-narrowed-p)
| improve this answer | |
  • Hm, thanks a lot, it works! I wonder, though, that 'describe-variable doesn't show buffer-narrowed-p. – Tom Jun 4 '17 at 13:29
  • 1
    That's because it's a function. Hence (buffer-narrowed-p) rather than buffer-narrowed-p. – phils Jun 4 '17 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.