[comp.emacs] Need mlisp for "prefix-region"

naim@nucsrl.UUCP (Naim Abdullah) (01/28/88)

I am a reformed vi user, trying to switch completely to GnuEmacs
and MicroGnu (mg). One thing I frequently find myself needing to
do is prefix a region with some character. Mg has a nice function
called "prefix-region" to do that but surprisingly I could not
find an equivalent in GnuEmacs 18.48. So when I am running GnuEmacs,
I have to pipe the region through a "sed s/./>&/p", yank it
out of the other window and paste it into my buffer and kill
the old region.

Can some mlisp guru post the function "prefix-region" (as
found in mg) in mlisp so that I can put it in my load path ?

As they used to say, thanks in advance for your help.

		      Naim Abdullah
		      Dept. of EECS,
		      Northwestern University

		      Internet: naim@eecs.nwu.edu
		      Uucp: {ihnp4, chinet, gargoyle}!nucsrl!naim

wolfgang@mgm.mit.edu (Wolfgang Rupprecht) (01/29/88)

In article <3910015@nucsrl.UUCP> naim@nucsrl.UUCP (Naim Abdullah) writes:
>Can some mlisp guru post the function "prefix-region" (as
>found in mg) in mlisp so that I can put it in my load path ?

How about:

(defun prefix-region (point mark string)
  "Prefix the region between POINT and MARK with STRING."
  (interactive "*r\nsPrefix: ")
  (save-excursion
    (save-restriction
      (narrow-to-region point mark)
      (goto-char point)
      (replace-regexp "^" string))))

--
Wolfgang Rupprecht	ARPA:  wolfgang@mgm.mit.edu (IP 18.82.0.114)
Freelance Consultant	UUCP:  mit-eddie!mgm.mit.edu!wolfgang
Boston, Ma.		VOICE: Hey_Wolfgang!_(617)_267-4365

Wolfgang Rupprecht	ARPA:  wolfgang@mgm.mit.edu (IP 18.82.0.114)
Freelance Consultant	UUCP:  mit-eddie!mgm.mit.edu!wolfgang
Boston, Ma.		VOICE: Hey_Wolfgang!_(617)_267-4365

Ram-Ashwin@cs.yale.edu (Ashwin Ram) (01/29/88)

In article <3910015@nucsrl.UUCP>, naim@nucsrl (Naim Abdullah) writes:
>		     One thing I frequently find myself needing to
> do is prefix a region with some character. Mg has a nice function
> called "prefix-region" to do that [...]

The following function should do what you want:

(defun prefix-region (begin end prefix)
   "Prefix every line in the region between BEGIN and END with the string PREFIX."
   (interactive "r\nsPrefix: ")
   (save-excursion
      (save-restriction
         (narrow-to-region begin end)
         (goto-char (point-min))
         (while (re-search-forward "^" nil t)
            (replace-match prefix t t)))))

One problem is that if the region starts from a point in the middle of a line
(rather than at the beginning of a line), the prefix will be inserted at that
point in  the  middle of the line too.   This is pretty easy to fix if it's a
problem (I don't know what MG's prefix-region does in this situation).

-- Ashwin Ram --

ARPA:    Ram-Ashwin@cs.yale.edu
UUCP:    {decvax,ucbvax,harvard,cmcl2,...}!yale!Ram-Ashwin
BITNET:  Ram@yalecs

gore@nucsrl.UUCP (Jacob Gore) (01/29/88)

/ nucsrl:comp.emacs / Ram-Ashwin@cs.yale.edu (Ashwin Ram) / Jan 28, 1988 /
>One problem is that if the region starts from a point in the middle of a line
>(rather than at the beginning of a line), the prefix will be inserted at that
>point in  the  middle of the line too.   This is pretty easy to fix if it's a
>problem (I don't know what MG's prefix-region does in this situation).

It (mg) treats the entire line as part of the region.

Jacob Gore				Gore@EECS.NWU.Edu
Northwestern Univ., EECS Dept.		{oddjob,gargoyle,ihnp4}!nucsrl!gore

jr@LF-SERVER-2.BBN.COM (John Robinson) (01/29/88)

Regular expression replacing in GNU emacs will handle this.  Here's a
sample function:

(defun prefix-region ()
  (save-excursion
    (save-restriction
      (narrow-to-region (point) (mark))
      (goto-char (point-min))
      (replace-regexp "^.*$" ">\\&"))))

The doubled backslash becomes one in the stored string; the & has to
be escaped.  Look at the dociumentation for replace-regexp, and in
Info under regular expressions.

/jr
jr@bbn.com or jr@bbn.uucp

wohler@spam.istc.sri.com (Bill Wohler) (02/02/88)

  while the prefix-region function that people have been posting is
  nice, this function that i wrote (with your help) last year is more
  general.  to perform a prefix-region, you would say, "M-x
  re-replace-region RET ^ RET whatever RET".

(defun re-replace-region (begin end old new)
  "Replace occurrences of REGEXP with TO-STRING in region."
  (interactive "*r\nsReplace string: \nswith: ")
  (save-excursion
    (save-restriction
      (narrow-to-region begin end)
      (goto-char (point-min))
      (while (re-search-forward old (point-max) t)
	(replace-match new t t)))))

  this has a feature of combining all the changes so that one undo
  will undo all of them.

							--bw

rbj@ICST-CMR.ARPA (Root Boy Jim) (02/24/88)

   From: Naim Abdullah <nucsrl!naim@ODDJOB.UCHICAGO.EDU>

   Mg has a nice function
   called "prefix-region" to do that but surprisingly I could not
   find an equivalent in GnuEmacs 18.48. So when I am running GnuEmacs,
   I have to pipe the region through a "sed s/./>&/p", yank it
   out of the other window and paste it into my buffer and kill
   the old region.

Not really. The function `shell-command-on-region', when given a prefix
arg, replaces the region with the command output. So you can type
^U ESC | sed 's/^/>/' RET. Note also the simpler regexp.

   Naim Abdullah		Dept. of EECS,	 Northwestern University
   Internet: naim@eecs.nwu.edu	 Uucp: {ihnp4, chinet, gargoyle}!nucsrl!naim

	(Root Boy) Jim Cottrell	<rbj@icst-cmr.arpa>
	National Bureau of Standards
	Flamer's Hotline: (301) 975-5688
We are now enjoying total mutual interaction in an imaginary hot tub...

wilkes@mips.COM (John Wilkes) (02/25/88)

In article <8802232111.AA14048@icst-cmr.arpa.ARPA> rbj@ICST-CMR.ARPA (Root Boy Jim) writes:
>
>   From: Naim Abdullah <nucsrl!naim@ODDJOB.UCHICAGO.EDU>
>
>   Mg has a nice function
>   called "prefix-region" to do that but surprisingly I could not
...
>^U ESC | sed 's/^/>/' RET. Note also the simpler regexp.

No need to pipe the region to sed...  Try this:

query-replace-regexp:
Replace some things after point matching REGEXP with TO-STRING.
As each match is found, the user must type a character saying
what to do with it.  For directions, type C-h at that time.

John
-- 
-- @work:
--		  {decwrl,ames,pyramid,prls}!mips!wilkes
--		       OR, for those of great faith:
--		           wilkes@mips.com

lrs@esl.UUCP (Lynn Slater) (02/25/88)

Posting-Front-End: GNU Emacs 18.44.12 of Mon Nov 16 1987 on esl (berkeley-unix)


>   Mg has a nice function
>   called "prefix-region" to do that but surprisingly I could not
>   find an equivalent in GnuEmacs 18.48.

I am not sure if this is what you are asking for, but here is my own
fix.

(defun insert-box (start end text)
  "Insert a text prefix at a column in all the lines in the region.
   Called from a program, takes three arguments, START, END, and TEXT.
   The column is taken from that of START."
  (interactive "r\nsText To Insert: ")
  (save-excursion
    (let (cc)
      ;; the point-marker stuff is needed to keep the edits from changing
      ;; where end is
      (goto-char end)
      (setq end (point-marker))
      (goto-char start)
      (setq cc  (current-column))
      (while (< (point) end) ;; modified 2/2/88
	;; I should here check for tab chars
	(insert text)
	(forward-line 1)
	(move-to-column cc))
      (move-marker end nil))))

(defun insert-end (start end text)
  "Insert a text prefix at the end in all the lines in the region.
   Called from a program, takes three arguments, START, END, and TEXT."
  (interactive "r\nsText To Insert: ")
  (save-excursion
    (let (cc)
      ;; the point-marker stuff is needed to keep the edits from changing
      ;; where end is
      (goto-char end)
      (setq end (point-marker))
      (goto-char start)
      (end-of-line)	
      (while (< (point) end);; modified 2/2/88
	;; I should here check for tab chars
	(insert text)
	(forward-line 1)
	(end-of-line)	
	)
      (move-marker end nil))))

===============================================================
Lynn Slater
ESL/TRW
(408) 738-2888 x 4482
lrs@esl.COM
===============================================================