unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Pierre Neidhardt <mail@ambrevar.xyz>
To: 41784@debbugs.gnu.org
Subject: bug#41784: 26.3; Narrow to prompt (narrow-to-defun fro M-x shell / comint)
Date: Wed, 10 Jun 2020 10:51:06 +0200	[thread overview]
Message-ID: <87sgf3e22t.fsf@ambrevar.xyz> (raw)

[-- Attachment #1: Type: text/plain, Size: 2229 bytes --]

Hi!

I like the narrow-to-defun command and I miss it for comint-mode / M-x shell.
Besides, I found it quite hard to reliably parse prompts, especially
multiline prompt.

So here are a bunch of functions that I think would make a great
addition to upstream :)

--8<---------------cut here---------------start------------->8---
(defun ambrevar/shell-prompt-begin-position ()
  ;; We need this convoluted function because `looking-at-p' does not work on
  ;; multiline regexps _and_ `re-search-backward' skips the current line.
  (save-excursion
    (let ((old-point (point)))
      (max
       (save-excursion
         ;; Right result if not on prompt.
         (call-interactively #'comint-previous-prompt)
         (re-search-backward comint-prompt-regexp)
         (point))
       (save-excursion
         ;; Right result if on first char after prompt.
         (re-search-backward comint-prompt-regexp)
         (point))
       (save-excursion
         ;; Right result if on prompt.
         (call-interactively #'comint-next-prompt)
         (re-search-backward comint-prompt-regexp)
         (if (<= (point) old-point)
             (point)
           (point-min)))))))

(defun ambrevar/shell-prompt-end-position ()
  (save-excursion
    (goto-char (ambrevar/shell-prompt-begin-position))
    (call-interactively #'comint-next-prompt)
    (point)))

(defun ambrevar/shell-prompt ()
  (buffer-substring-no-properties
   (ambrevar/shell-prompt-begin-position)
   (ambrevar/shell-prompt-end-position)))

(defun ambrevar/shell-narrow-to-prompt ()
  "Narrow buffer to prompt at point."
  (interactive)
  (let ((begin (ambrevar/shell-prompt-begin-position)))
    (narrow-to-region
     begin
     (save-excursion
       (goto-char (ambrevar/shell-prompt-end-position))
       (call-interactively #'comint-next-prompt)
       (if (= begin
              (ambrevar/shell-prompt-begin-position))
           (point-max)
         (ambrevar/shell-prompt-begin-position))))))

(define-key shell-mode-map (kbd "C-x n d") 'ambrevar/shell-narrow-to-prompt)
--8<---------------cut here---------------end--------------->8---

Cheers!

-- 
Pierre Neidhardt
https://ambrevar.xyz/

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

             reply	other threads:[~2020-06-10  8:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-10  8:51 Pierre Neidhardt [this message]
2020-10-17 11:17 ` bug#41784: 26.3; Narrow to prompt (narrow-to-defun fro M-x shell / comint) Lars Ingebrigtsen
2020-10-17 12:09   ` Pierre Neidhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87sgf3e22t.fsf@ambrevar.xyz \
    --to=mail@ambrevar.xyz \
    --cc=41784@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).