* Adding String to Beginning of Lines
@ 2020-11-10 10:43 Christopher Dimech
2020-11-10 11:10 ` tomas
` (2 more replies)
0 siblings, 3 replies; 50+ messages in thread
From: Christopher Dimech @ 2020-11-10 10:43 UTC (permalink / raw)
To: Help Gnu Emacs
I have a paragraph and want to add a string to the beginning of each line after selecting
the paragraph region.
GNU is an operating system that is free software—that is, it respects users' freedom. The
GNU operating system consists of GNU packages (programs specifically released by the GNU
Project) as well as free software released by third parties. The development of GNU made
it possible to use a computer without software that would trample your freedom.
Example, passing ';; + ' as argument to a function to get
;; + GNU is an operating system that is free software—that is, it respects users' freedom. The
;; + GNU operating system consists of GNU packages (programs specifically released by the GNU
;; + Project) as well as free software released by third parties. The development of GNU made
;; + it possible to use a computer without software that would trample your freedom.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 10:43 Adding String to Beginning of Lines Christopher Dimech
@ 2020-11-10 11:10 ` tomas
2020-11-10 11:19 ` Christopher Dimech
2020-11-10 11:29 ` Christopher Dimech
2020-11-10 11:42 ` Michael Heerdegen
2020-11-12 19:01 ` Dante Catalfamo
2 siblings, 2 replies; 50+ messages in thread
From: tomas @ 2020-11-10 11:10 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 1183 bytes --]
On Tue, Nov 10, 2020 at 11:43:45AM +0100, Christopher Dimech wrote:
>
> I have a paragraph and want to add a string to the beginning of each line after selecting
> the paragraph region.
>
> GNU is an operating system that is free software—that is, it respects users' freedom. The
> GNU operating system consists of GNU packages (programs specifically released by the GNU
> Project) as well as free software released by third parties. The development of GNU made
> it possible to use a computer without software that would trample your freedom.
>
> Example, passing ';; + ' as argument to a function to get
>
> ;; + GNU is an operating system that is free software—that is, it respects users' freedom. The
> ;; + GNU operating system consists of GNU packages (programs specifically released by the GNU
> ;; + Project) as well as free software released by third parties. The development of GNU made
> ;; + it possible to use a computer without software that would trample your freedom.
- Select the region
- M-x query-replace-regexp "^" → ";; + "
- Profit!
Wrapping that up in a function is left as an exercise to the
reader :-)
Cheers
-- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 11:10 ` tomas
@ 2020-11-10 11:19 ` Christopher Dimech
2020-11-10 11:29 ` Christopher Dimech
1 sibling, 0 replies; 50+ messages in thread
From: Christopher Dimech @ 2020-11-10 11:19 UTC (permalink / raw)
To: tomas; +Cc: help-gnu-emacs
Hell, I started doing like this. Still a good exercise though.
(defun comblock (rg-beg rg-end)
"TODO"
(interactive "r")
(if (use-region-p)
(progn
(goto-char rg-beg)
(beginning-of-line)
(setq myLine
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position) ))
(message "region active"))
(message "region not active"))
)
> Sent: Tuesday, November 10, 2020 at 12:10 PM
> From: tomas@tuxteam.de
> To: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> On Tue, Nov 10, 2020 at 11:43:45AM +0100, Christopher Dimech wrote:
> >
> > I have a paragraph and want to add a string to the beginning of each line after selecting
> > the paragraph region.
> >
> > GNU is an operating system that is free software—that is, it respects users' freedom. The
> > GNU operating system consists of GNU packages (programs specifically released by the GNU
> > Project) as well as free software released by third parties. The development of GNU made
> > it possible to use a computer without software that would trample your freedom.
> >
> > Example, passing ';; + ' as argument to a function to get
> >
> > ;; + GNU is an operating system that is free software—that is, it respects users' freedom. The
> > ;; + GNU operating system consists of GNU packages (programs specifically released by the GNU
> > ;; + Project) as well as free software released by third parties. The development of GNU made
> > ;; + it possible to use a computer without software that would trample your freedom.
>
> - Select the region
> - M-x query-replace-regexp "^" → ";; + "
> - Profit!
>
> Wrapping that up in a function is left as an exercise to the
> reader :-)
>
> Cheers
> -- t
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 11:10 ` tomas
2020-11-10 11:19 ` Christopher Dimech
@ 2020-11-10 11:29 ` Christopher Dimech
2020-11-10 11:54 ` Michael Heerdegen
1 sibling, 1 reply; 50+ messages in thread
From: Christopher Dimech @ 2020-11-10 11:29 UTC (permalink / raw)
To: tomas; +Cc: help-gnu-emacs
Have selected a region, and called the command. However the region
remained unchanged.
> Sent: Tuesday, November 10, 2020 at 12:10 PM
> From: tomas@tuxteam.de
> To: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> On Tue, Nov 10, 2020 at 11:43:45AM +0100, Christopher Dimech wrote:
> >
> > I have a paragraph and want to add a string to the beginning of each line after selecting
> > the paragraph region.
> >
> > GNU is an operating system that is free software—that is, it respects users' freedom. The
> > GNU operating system consists of GNU packages (programs specifically released by the GNU
> > Project) as well as free software released by third parties. The development of GNU made
> > it possible to use a computer without software that would trample your freedom.
> >
> > Example, passing ';; + ' as argument to a function to get
> >
> > ;; + GNU is an operating system that is free software—that is, it respects users' freedom. The
> > ;; + GNU operating system consists of GNU packages (programs specifically released by the GNU
> > ;; + Project) as well as free software released by third parties. The development of GNU made
> > ;; + it possible to use a computer without software that would trample your freedom.
>
> - Select the region
> - M-x query-replace-regexp "^" → ";; + "
> - Profit!
>
> Wrapping that up in a function is left as an exercise to the
> reader :-)
>
> Cheers
> -- t
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 10:43 Adding String to Beginning of Lines Christopher Dimech
2020-11-10 11:10 ` tomas
@ 2020-11-10 11:42 ` Michael Heerdegen
2020-11-10 18:47 ` H. Dieter Wilhelm
2020-11-12 19:01 ` Dante Catalfamo
2 siblings, 1 reply; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-10 11:42 UTC (permalink / raw)
To: help-gnu-emacs
Christopher Dimech <dimech@gmx.com> writes:
> I have a paragraph and want to add a string to the beginning of each
> line after selecting the paragraph region.
Alternatively, you can use `string-rectangle' when you select an
appropriate rectangle (of width zero in this case) instead of the
paragraph.
See `rectangle-mark-mode' for how to get a visual feedback of the region
interpreted as a rectangle.
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 11:29 ` Christopher Dimech
@ 2020-11-10 11:54 ` Michael Heerdegen
2020-11-10 11:58 ` Christopher Dimech
2020-11-10 12:03 ` Christopher Dimech
0 siblings, 2 replies; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-10 11:54 UTC (permalink / raw)
To: help-gnu-emacs
Christopher Dimech <dimech@gmx.com> writes:
> Have selected a region, and called the command. However the region
> remained unchanged.
No, that should definitely work. What exactly have you typed? You have
left out the "" string markers? Don't type them in the prompts.
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 11:54 ` Michael Heerdegen
@ 2020-11-10 11:58 ` Christopher Dimech
2020-11-10 12:07 ` Michael Heerdegen
2020-11-10 12:03 ` Christopher Dimech
1 sibling, 1 reply; 50+ messages in thread
From: Christopher Dimech @ 2020-11-10 11:58 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
Had typed them in the prompts.
> Sent: Tuesday, November 10, 2020 at 12:54 PM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> Christopher Dimech <dimech@gmx.com> writes:
>
> > Have selected a region, and called the command. However the region
> > remained unchanged.
>
> No, that should definitely work. What exactly have you typed? You have
> left out the "" string markers? Don't type them in the prompts.
>
> Michael.
>
>
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 11:54 ` Michael Heerdegen
2020-11-10 11:58 ` Christopher Dimech
@ 2020-11-10 12:03 ` Christopher Dimech
2020-11-10 12:16 ` Michael Heerdegen
2020-11-10 14:03 ` Jean Louis
1 sibling, 2 replies; 50+ messages in thread
From: Christopher Dimech @ 2020-11-10 12:03 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
Have used the following
(defun compfx ()
(interactive "r")
(query-replace-regexp "^" ";; + "))
> Sent: Tuesday, November 10, 2020 at 12:54 PM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> Christopher Dimech <dimech@gmx.com> writes:
>
> > Have selected a region, and called the command. However the region
> > remained unchanged.
>
> No, that should definitely work. What exactly have you typed? You have
> left out the "" string markers? Don't type them in the prompts.
>
> Michael.
>
>
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 11:58 ` Christopher Dimech
@ 2020-11-10 12:07 ` Michael Heerdegen
0 siblings, 0 replies; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-10 12:07 UTC (permalink / raw)
To: help-gnu-emacs
Christopher Dimech <dimech@gmx.com> writes:
> Had typed them in the prompts.
Yes, you don't need them.
And when prompted for a regexp in `query-replace-regexp', you also don't
have to quote backslashes for regexp specials, contrarily to string
syntax, so one backslash is ok where you would have to type two in a
string describing a regexp.
The background is: the minibuffer input is used directly in this case
instead of being `read' as a lisp object (string).
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 12:03 ` Christopher Dimech
@ 2020-11-10 12:16 ` Michael Heerdegen
2020-11-10 13:40 ` Christopher Dimech
2020-11-10 14:03 ` Jean Louis
1 sibling, 1 reply; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-10 12:16 UTC (permalink / raw)
To: help-gnu-emacs
Christopher Dimech <dimech@gmx.com> writes:
> Have used the following
>
> (defun compfx ()
> (interactive "r")
> (query-replace-regexp "^" ";; + "))
(defun compfx (beg end)
(interactive "r")
(query-replace-regexp "^" ";; + " nil beg end))
Do you want to be asked interactively whether to replace each "match"?
If not, we want to use something non-interactive instead of
`query-replace-regexp'.
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 12:16 ` Michael Heerdegen
@ 2020-11-10 13:40 ` Christopher Dimech
2020-11-10 14:06 ` Jean Louis
` (2 more replies)
0 siblings, 3 replies; 50+ messages in thread
From: Christopher Dimech @ 2020-11-10 13:40 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
I now have got 'rec-cont', which lets me pass a string.
How can I add a dafault if I don't pass a regex in the
mini-buffer.
(defun rec-cont (beg end regex)
(interactive "r\ns rec-cont regex: ")
(replace-regexp "^" regex nil beg end))
;; (replace-regexp "^" ";; + " nil beg end))
> Sent: Tuesday, November 10, 2020 at 1:16 PM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> Christopher Dimech <dimech@gmx.com> writes:
>
> > Have used the following
> >
> > (defun compfx ()
> > (interactive "r")
> > (query-replace-regexp "^" ";; + "))
>
> (defun compfx (beg end)
> (interactive "r")
> (query-replace-regexp "^" ";; + " nil beg end))
>
> Do you want to be asked interactively whether to replace each "match"?
> If not, we want to use something non-interactive instead of
> `query-replace-regexp'.
>
>
> Michael.
>
>
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 12:03 ` Christopher Dimech
2020-11-10 12:16 ` Michael Heerdegen
@ 2020-11-10 14:03 ` Jean Louis
1 sibling, 0 replies; 50+ messages in thread
From: Jean Louis @ 2020-11-10 14:03 UTC (permalink / raw)
To: Christopher Dimech; +Cc: Michael Heerdegen, help-gnu-emacs
* Christopher Dimech <dimech@gmx.com> [2020-11-10 15:06]:
>
> Have used the following
>
> (defun compfx ()
> (interactive "r")
> (query-replace-regexp "^" ";; + "))
You can also record keyboard macros for repeated work.
See Emacs Lisp manual where it speaks about macros.
You would do:
F3 to start recording macro, then you do necessary actions like
commenting the page.
Press F4 to save macro.
Apply F4 in any buffer you wish.
Use `C-x C-k n' to name the last recorded macro.
Then you can save the macro into buffer and then into file:
(fset 'comment
(kmacro-lambda-form [?\M-< ?\M-x ?r ?e ?p ?l ?a ?c tab ?r ?e ?g ?e tab return ?^ return ?\; ?\; ? return] 0 "%d"))
That is what I did above for commenting first lines with ;; and named
the macro comment.
If you evaluate the above or load it from init.el or other file, you
can then invoke macro with:
M-x comment RET and it will do what you wanted.
Jean
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 13:40 ` Christopher Dimech
@ 2020-11-10 14:06 ` Jean Louis
2020-11-10 14:29 ` Michael Heerdegen
2020-11-10 14:16 ` Michael Heerdegen
2020-11-10 22:45 ` tomas
2 siblings, 1 reply; 50+ messages in thread
From: Jean Louis @ 2020-11-10 14:06 UTC (permalink / raw)
To: Christopher Dimech; +Cc: Michael Heerdegen, help-gnu-emacs
* Christopher Dimech <dimech@gmx.com> [2020-11-10 16:41]:
> I now have got 'rec-cont', which lets me pass a string.
> How can I add a dafault if I don't pass a regex in the
> mini-buffer.
>
> (defun rec-cont (beg end regex)
> (interactive "r\ns rec-cont regex: ")
> (replace-regexp "^" regex nil beg end))
> ;; (replace-regexp "^" ";; + " nil beg end))
Maybe (beg end &optional regex)
(let ((regex (or regex default))))
And if you need to read regexp: read-regexp
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 13:40 ` Christopher Dimech
2020-11-10 14:06 ` Jean Louis
@ 2020-11-10 14:16 ` Michael Heerdegen
2020-11-10 14:38 ` Christopher Dimech
2020-11-10 22:45 ` tomas
2 siblings, 1 reply; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-10 14:16 UTC (permalink / raw)
To: help-gnu-emacs
Christopher Dimech <dimech@gmx.com> writes:
> I now have got 'rec-cont', which lets me pass a string.
> How can I add a dafault if I don't pass a regex in the
> mini-buffer.
>
> (defun rec-cont (beg end regex)
> (interactive "r\ns rec-cont regex: ")
> (replace-regexp "^" regex nil beg end))
> ;; (replace-regexp "^" ";; + " nil beg end))
Eh - Are you speaking about a default for the replacement, the
TO-STRING, you want to insert? Because your regexp is always "^" as far
as I understand.
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 14:06 ` Jean Louis
@ 2020-11-10 14:29 ` Michael Heerdegen
2020-11-10 19:12 ` Jean Louis
0 siblings, 1 reply; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-10 14:29 UTC (permalink / raw)
To: help-gnu-emacs
Jean Louis <bugs@gnu.support> writes:
> (let ((regex (or regex default))))
^^^^^
That might possible need to be
(string-empty-p regex)
depending on how the contents of the variable had been read.
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 14:16 ` Michael Heerdegen
@ 2020-11-10 14:38 ` Christopher Dimech
2020-11-10 14:49 ` Michael Heerdegen
0 siblings, 1 reply; 50+ messages in thread
From: Christopher Dimech @ 2020-11-10 14:38 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
Correct, the replacement. Because in .el files, the replacement will be
";; + ", in other languages, it is dependent on the symbol for comments
(e.g. "!! +" for fortran code .f)
> Sent: Tuesday, November 10, 2020 at 3:16 PM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> Christopher Dimech <dimech@gmx.com> writes:
>
> > I now have got 'rec-cont', which lets me pass a string.
> > How can I add a dafault if I don't pass a regex in the
> > mini-buffer.
> >
> > (defun rec-cont (beg end regex)
> > (interactive "r\ns rec-cont regex: ")
> > (replace-regexp "^" regex nil beg end))
> > ;; (replace-regexp "^" ";; + " nil beg end))
>
> Eh - Are you speaking about a default for the replacement, the
> TO-STRING, you want to insert? Because your regexp is always "^" as far
> as I understand.
>
> Michael.
>
>
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 14:38 ` Christopher Dimech
@ 2020-11-10 14:49 ` Michael Heerdegen
2020-11-10 17:52 ` Christopher Dimech
0 siblings, 1 reply; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-10 14:49 UTC (permalink / raw)
To: Christopher Dimech; +Cc: help-gnu-emacs
Christopher Dimech <dimech@gmx.com> writes:
> Correct, the replacement. Because in .el files, the replacement will be
> ";; + ", in other languages, it is dependent on the symbol for comments
> (e.g. "!! +" for fortran code .f)
Ok.
If you prefer to have `interactive' return the default, then: a
not-stringish sexp as interactive spec is evaluated normally, and
expected to return the list of arguments. So you could use something
like:
(defun rec-cont (beg end s)
(interactive (list (region-beginning)
(region-end)
(read-string "Prefix: " nil nil "your-default")))
(replace-regexp "^" s nil beg end))
You can also use an expression to provide a default depending on the
current mode, e.g.
(if comment-start (concat comment-start " +") ";; +")
instead of a constant string "your-default".
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 14:49 ` Michael Heerdegen
@ 2020-11-10 17:52 ` Christopher Dimech
2020-11-10 18:03 ` Michael Heerdegen
0 siblings, 1 reply; 50+ messages in thread
From: Christopher Dimech @ 2020-11-10 17:52 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
Have tried to add a keybinding for the function, but after pressing
the keyseq, I am getting "H-z is undefined".
(define-key emacs-lisp-mode-hook (kbd "H-z") 'rec-field-nwltok)
> Sent: Tuesday, November 10, 2020 at 3:49 PM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: "Christopher Dimech" <dimech@gmx.com>
> Cc: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> Christopher Dimech <dimech@gmx.com> writes:
>
> > Correct, the replacement. Because in .el files, the replacement will be
> > ";; + ", in other languages, it is dependent on the symbol for comments
> > (e.g. "!! +" for fortran code .f)
>
> Ok.
>
> If you prefer to have `interactive' return the default, then: a
> not-stringish sexp as interactive spec is evaluated normally, and
> expected to return the list of arguments. So you could use something
> like:
>
> (defun rec-cont (beg end s)
> (interactive (list (region-beginning)
> (region-end)
> (read-string "Prefix: " nil nil "your-default")))
> (replace-regexp "^" s nil beg end))
>
> You can also use an expression to provide a default depending on the
> current mode, e.g.
>
> (if comment-start (concat comment-start " +") ";; +")
>
> instead of a constant string "your-default".
>
>
> Michael.
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 17:52 ` Christopher Dimech
@ 2020-11-10 18:03 ` Michael Heerdegen
2020-11-10 18:25 ` Christopher Dimech
0 siblings, 1 reply; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-10 18:03 UTC (permalink / raw)
To: help-gnu-emacs
Christopher Dimech <dimech@gmx.com> writes:
> Have tried to add a keybinding for the function, but after pressing
> the keyseq, I am getting "H-z is undefined".
>
> (define-key emacs-lisp-mode-hook (kbd "H-z") 'rec-field-nwltok)
Yeah, because it's, ehm, nonsense.
You want to bind your key in a keymap, and do that in the hook, e.g.
(add-hook 'emacs-lisp-mode-hook
(defun my-emacs-lisp-mode-hook-fun ()
(define-key emacs-lisp-mode-map (kbd "H-z") #'rec-field-nwltok)))
Hooks contain functions. I use to use named functions because lambdas
cannot be identified should you reload your piece of code and thus get
added multiple times. If you don't care (i.e. if you know for sure you
don't reload your init file) a lambda also works.
Regards,
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 18:03 ` Michael Heerdegen
@ 2020-11-10 18:25 ` Christopher Dimech
2020-11-10 18:49 ` Michael Heerdegen
0 siblings, 1 reply; 50+ messages in thread
From: Christopher Dimech @ 2020-11-10 18:25 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
The update is still giving me trouble though. Could be a problem with passing the arguments
to rec-field-nwltok
I have the following
(defun rec-field-nwltok (beg end nwltok)
(interactive
(list (region-beginning)
(region-end)
(read-string "rec-field-nwltok: " nil nil ";; + ")))
(replace-regexp "^" nwltok nil beg end) )
(add-hook 'emacs-lisp-mode-hook
(defun myrec-field-nwltok ()
(define-key emacs-lisp-mode-map (kbd "H-z") #'rec-field-nwltok)))
> Sent: Tuesday, November 10, 2020 at 7:03 PM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> Christopher Dimech <dimech@gmx.com> writes:
>
> > Have tried to add a keybinding for the function, but after pressing
> > the keyseq, I am getting "H-z is undefined".
> >
> > (define-key emacs-lisp-mode-hook (kbd "H-z") 'rec-field-nwltok)
>
> Yeah, because it's, ehm, nonsense.
>
> You want to bind your key in a keymap, and do that in the hook, e.g.
>
> (add-hook 'emacs-lisp-mode-hook
> (defun my-emacs-lisp-mode-hook-fun ()
> (define-key emacs-lisp-mode-map (kbd "H-z") #'rec-field-nwltok)))
>
> Hooks contain functions. I use to use named functions because lambdas
> cannot be identified should you reload your piece of code and thus get
> added multiple times. If you don't care (i.e. if you know for sure you
> don't reload your init file) a lambda also works.
>
>
> Regards,
>
> Michael.
>
>
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 11:42 ` Michael Heerdegen
@ 2020-11-10 18:47 ` H. Dieter Wilhelm
0 siblings, 0 replies; 50+ messages in thread
From: H. Dieter Wilhelm @ 2020-11-10 18:47 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
Michael Heerdegen <michael_heerdegen@web.de> writes:
> Christopher Dimech <dimech@gmx.com> writes:
>
>> I have a paragraph and want to add a string to the beginning of each
>> line after selecting the paragraph region.
>
> Alternatively, you can use `string-rectangle' when you select an
> appropriate rectangle (of width zero in this case) instead of the
> paragraph.
Excellent suggestion, in short: Select a region and C-x r t
and you're even getting visual feedback what is replaced ! :-)
Dieter
--
Best wishes
H. Dieter Wilhelm
Zwingenberg, Germany
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 18:25 ` Christopher Dimech
@ 2020-11-10 18:49 ` Michael Heerdegen
2020-11-10 18:56 ` Christopher Dimech
0 siblings, 1 reply; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-10 18:49 UTC (permalink / raw)
To: help-gnu-emacs
Christopher Dimech <dimech@gmx.com> writes:
> The update is still giving me trouble though. Could be a problem with passing the arguments
> to rec-field-nwltok
>
> I have the following
>
> (defun rec-field-nwltok (beg end nwltok)
> (interactive
> (list (region-beginning)
> (region-end)
> (read-string "rec-field-nwltok: " nil nil ";; + ")))
> (replace-regexp "^" nwltok nil beg end) )
>
> (add-hook 'emacs-lisp-mode-hook
> (defun myrec-field-nwltok ()
> (define-key emacs-lisp-mode-map (kbd "H-z") #'rec-field-nwltok)))
That works well for me. Of what kind is that trouble?
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 18:49 ` Michael Heerdegen
@ 2020-11-10 18:56 ` Christopher Dimech
2020-11-10 19:02 ` Michael Heerdegen
0 siblings, 1 reply; 50+ messages in thread
From: Christopher Dimech @ 2020-11-10 18:56 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
Still getting the keyseq as undefined.
> Sent: Tuesday, November 10, 2020 at 7:49 PM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> Christopher Dimech <dimech@gmx.com> writes:
>
> > The update is still giving me trouble though. Could be a problem with passing the arguments
> > to rec-field-nwltok
> >
> > I have the following
> >
> > (defun rec-field-nwltok (beg end nwltok)
> > (interactive
> > (list (region-beginning)
> > (region-end)
> > (read-string "rec-field-nwltok: " nil nil ";; + ")))
> > (replace-regexp "^" nwltok nil beg end) )
> >
> > (add-hook 'emacs-lisp-mode-hook
> > (defun myrec-field-nwltok ()
> > (define-key emacs-lisp-mode-map (kbd "H-z") #'rec-field-nwltok)))
>
> That works well for me. Of what kind is that trouble?
>
> Michael.
>
>
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 18:56 ` Christopher Dimech
@ 2020-11-10 19:02 ` Michael Heerdegen
2020-11-10 19:29 ` Christopher Dimech
0 siblings, 1 reply; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-10 19:02 UTC (permalink / raw)
To: help-gnu-emacs
Christopher Dimech <dimech@gmx.com> writes:
> Still getting the keyseq as undefined.
How did you evaluate that code, and where did you try?
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 14:29 ` Michael Heerdegen
@ 2020-11-10 19:12 ` Jean Louis
2020-11-10 19:42 ` Michael Heerdegen
0 siblings, 1 reply; 50+ messages in thread
From: Jean Louis @ 2020-11-10 19:12 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
* Michael Heerdegen <michael_heerdegen@web.de> [2020-11-10 17:30]:
> Jean Louis <bugs@gnu.support> writes:
>
> > (let ((regex (or regex default))))
> ^^^^^
>
> That might possible need to be
>
> (string-empty-p regex)
>
> depending on how the contents of the variable had been read.
Thank you.
User can choose to have "" regex as also plain function allows is.
(string-match "" "something")
If it is "" it is not nil, it would be chosen.
If it is nil it would not be chosen.
Is that right? Do you think it is necessary more than just `or'?
Afterthoughts:
I do not link that string-empty-p function
(string-empty-p nil) => nil
It is not logical to me. I was even surprised to find it just
yesterday because I had my own function.
Of course that NIL is not a string, but it is also not a string to
give NIL if it is empty (I know this sentence may be confusing) as
that means that NIL is not empty string.
If string is NIL I expect that string-empty-p on that string tells me
TRUE.
(defun rcd-string-empty-p (s)
"Returns T if string is empty or NIL"
(let ((s (if (null s) "" s)))
(if (stringp s)
(if (zerop (length s)) t nil))))
(rcd-string-empty-p nil) => T
That is my expectation in many programs where I am handling the string
which can be also NIL.
I was always loading files and then first time I installed it as
package that uses subr-x (I think function comes from there) so my
personal function string-empty-p collided with subr-x function and
suddenly my program did not behave well. Yesterday I have renamed my
function.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 19:02 ` Michael Heerdegen
@ 2020-11-10 19:29 ` Christopher Dimech
2020-11-10 20:07 ` Michael Heerdegen
0 siblings, 1 reply; 50+ messages in thread
From: Christopher Dimech @ 2020-11-10 19:29 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
I have now figured the problem.
Was testing it in the scratch buffer, and thought it is in emacs-lisp-mode, but
it is not.
> Sent: Tuesday, November 10, 2020 at 8:02 PM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> Christopher Dimech <dimech@gmx.com> writes:
>
> > Still getting the keyseq as undefined.
>
> How did you evaluate that code, and where did you try?
>
> Michael.
>
>
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 19:12 ` Jean Louis
@ 2020-11-10 19:42 ` Michael Heerdegen
2020-11-10 21:22 ` Jean Louis
2020-11-10 22:31 ` Stefan Monnier
0 siblings, 2 replies; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-10 19:42 UTC (permalink / raw)
To: Jean Louis; +Cc: help-gnu-emacs
Jean Louis <bugs@gnu.support> writes:
> User can choose to have "" regex as also plain function allows is.
>
> (string-match "" "something")
>
> If it is "" it is not nil, it would be chosen.
>
> If it is nil it would not be chosen.
>
> Is that right? Do you think it is necessary more than just `or'?
You are right with everything. But in the meantime I had found out that
the string is actually used as the prefix to prepend to the lines, not
as a regexp. So an empty string here likely indicates that the user
wants to use the default instead.
> Afterthoughts:
>
> I do not link that string-empty-p function
>
> (string-empty-p nil) => nil
>
> It is not logical to me. I was even surprised to find it just
> yesterday because I had my own function.
If the arguments don't fit the specification anything may happen. Just
that is the case here I would say. There is no implicit type check.
If you look at the definition:
(defsubst string-empty-p (string)
"Check whether STRING is empty."
(string= string ""))
and check the doc of `string=' you'll see that for symbols (like `nil')
their name is used as string. This is surely not useful for
`string-empty-p', but thus a symbol which has the empty string as name
(the interned one has a reader syntax "##") fulfills the predicate:
(string-empty-p '##) ==> t
No, that's not useful, just a side effect of the implementation.
| (defun rcd-string-empty-p (s)
| "Returns T if string is empty or NIL"
| (let ((s (if (null s) "" s)))
| (if (stringp s)
| (if (zerop (length s)) t nil))))
That's more or less the same as
(defun rcd-string-empty-p (s)
(or (null s) (and (stringp s) (zerop (length s)))))
But why handling an empty list specially, and not other empty sequences,
like the empty vector? That's what `seq-empty-p' in seq.el does, btw.
Regards,
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 19:29 ` Christopher Dimech
@ 2020-11-10 20:07 ` Michael Heerdegen
0 siblings, 0 replies; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-10 20:07 UTC (permalink / raw)
To: help-gnu-emacs
Christopher Dimech <dimech@gmx.com> writes:
> I have now figured the problem.
>
> Was testing it in the scratch buffer, and thought it is in
> emacs-lisp-mode, but it is not.
Yes - but that's not the problem. The key bindings should be inherited.
The problem is that the hook is not run if you evaluate it in the
existing buffer.
Better alternative: elisp-mode.el is already loaded when Emacs has
started. So you don't need a hook at all, the `define-key' call is
enough.
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 19:42 ` Michael Heerdegen
@ 2020-11-10 21:22 ` Jean Louis
2020-11-11 10:59 ` Michael Heerdegen
2020-11-10 22:31 ` Stefan Monnier
1 sibling, 1 reply; 50+ messages in thread
From: Jean Louis @ 2020-11-10 21:22 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
* Michael Heerdegen <michael_heerdegen@web.de> [2020-11-10 22:42]:
> If you look at the definition:
>
> (defsubst string-empty-p (string)
> "Check whether STRING is empty."
> (string= string ""))
I looked as my thing collided and I did not know what is happening
that suddenly everything is breaks. I cannot even see how that helps,
it just gives better meaning maybe:
(string-empty-p string)
(string= st "") -- shorter and does same
> and check the doc of `string=' you'll see that for symbols (like `nil')
> their name is used as string. This is surely not useful for
> `string-empty-p', but thus a symbol which has the empty string as name
> (the interned one has a reader syntax "##") fulfills the predicate:
>
> (string-empty-p '##) ==> t
>
> No, that's not useful, just a side effect of the implementation.
>
> | (defun rcd-string-empty-p (s)
> | "Returns T if string is empty or NIL"
> | (let ((s (if (null s) "" s)))
> | (if (stringp s)
> | (if (zerop (length s)) t nil))))
>
> That's more or less the same as
>
> (defun rcd-string-empty-p (s)
> (or (null s) (and (stringp s) (zerop (length s)))))
Alright.
> But why handling an empty list specially, and not other empty sequences,
> like the empty vector? That's what `seq-empty-p' in seq.el does,
> btw.
If I understand well I can simply remove my function and instead use
seq-empty-p? It looks that it does what we discussed here.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 19:42 ` Michael Heerdegen
2020-11-10 21:22 ` Jean Louis
@ 2020-11-10 22:31 ` Stefan Monnier
2020-11-10 23:00 ` Drew Adams
1 sibling, 1 reply; 50+ messages in thread
From: Stefan Monnier @ 2020-11-10 22:31 UTC (permalink / raw)
To: help-gnu-emacs
> If you look at the definition:
>
> (defsubst string-empty-p (string)
> "Check whether STRING is empty."
> (string= string ""))
FWIW, I agree with Louis on this one: (equal string "") is shorter and
just as clear as (or even more clear than) `(string-empty-p string)`,
so I'm no fan of such a definition.
> | (defun rcd-string-empty-p (s)
> | "Returns T if string is empty or NIL"
> | (let ((s (if (null s) "" s)))
> | (if (stringp s)
> | (if (zerop (length s)) t nil))))
>
> That's more or less the same as
>
> (defun rcd-string-empty-p (s)
> (or (null s) (and (stringp s) (zerop (length s)))))
BTW since (length nil) is zero, this can be reduced in most cases to
(zerop (length s)), the main difference being that the shorter
definition signals an error if passed an object that's neither an array
nor a proper list.
> But why handling an empty list specially, and not other empty sequences,
> like the empty vector? That's what `seq-empty-p' in seq.el does, btw.
Indeed,
Stefan
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 13:40 ` Christopher Dimech
2020-11-10 14:06 ` Jean Louis
2020-11-10 14:16 ` Michael Heerdegen
@ 2020-11-10 22:45 ` tomas
2020-11-11 11:16 ` Michael Heerdegen
2 siblings, 1 reply; 50+ messages in thread
From: tomas @ 2020-11-10 22:45 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 1078 bytes --]
On Tue, Nov 10, 2020 at 02:40:08PM +0100, Christopher Dimech wrote:
> I now have got 'rec-cont', which lets me pass a string.
> How can I add a dafault if I don't pass a regex in the
> mini-buffer.
>
> (defun rec-cont (beg end regex)
> (interactive "r\ns rec-cont regex: ")
> (replace-regexp "^" regex nil beg end))
> ;; (replace-regexp "^" ";; + " nil beg end))
But watch out: the replacement string (confusingly called `regex' here,
others have already pointed that out) might contain special sequences
(e.g. "\\&") which have a meaning in this context.
If doing that from a program, better not use `replace-regexp' but do
it yourself in a loop along this lines:
(while (search-forward-regexp "^" nil 'noerror)
(replace-match replacement-string))
(I've taken the liberty to rename the replacement string `replacement-string').
The doc explicitly warns against using `replace-regexp' in
programs:
"This function is for interactive use only;
in Lisp code use `re-search-forward' and `replace-match'
instead."
Cheers
- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 50+ messages in thread
* RE: Adding String to Beginning of Lines
2020-11-10 22:31 ` Stefan Monnier
@ 2020-11-10 23:00 ` Drew Adams
2020-11-11 21:37 ` Michael Heerdegen
0 siblings, 1 reply; 50+ messages in thread
From: Drew Adams @ 2020-11-10 23:00 UTC (permalink / raw)
To: Stefan Monnier, help-gnu-emacs
> FWIW, I agree with Louis on this one: (equal string "") is shorter and
> just as clear as (or even more clear than) `(string-empty-p string)`,
> so I'm no fan of such a definition.
+1.
Perhaps someone added it to have a unary predicate
to pass, instead of using an anonymous function or
`apply-partially'?
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 21:22 ` Jean Louis
@ 2020-11-11 10:59 ` Michael Heerdegen
2020-11-17 16:06 ` Jean Louis
0 siblings, 1 reply; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-11 10:59 UTC (permalink / raw)
To: help-gnu-emacs
Jean Louis <bugs@gnu.support> writes:
> If I understand well I can simply remove my function and instead use
> seq-empty-p? It looks that it does what we discussed here.
Yes. Please (require 'seq) before using it. Note that very old Emacs
versions don't have the "seq" library. For those (and only those) a
"seq" package is provided in Gnu Elpa.
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 22:45 ` tomas
@ 2020-11-11 11:16 ` Michael Heerdegen
2020-11-11 13:22 ` Christopher Dimech
2020-11-11 17:20 ` Christopher Dimech
0 siblings, 2 replies; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-11 11:16 UTC (permalink / raw)
To: help-gnu-emacs
<tomas@tuxteam.de> writes:
> But watch out: the replacement string (confusingly called `regex' here,
> others have already pointed that out) might contain special sequences
> (e.g. "\\&") which have a meaning in this context.
Indeed, an important objection.
I would suggest to avoid using any query-replace at all and rather use
something like
#+begin_src emacs-lisp
(save-excursion
(while (and (<= (point) end)
(not (eobp)))
(insert my-string)
(forward-line +1))
(deactivate-mark))
#+end_src
> Cheers
Ebenso!
Regards,
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-11 11:16 ` Michael Heerdegen
@ 2020-11-11 13:22 ` Christopher Dimech
2020-11-11 13:56 ` Michael Heerdegen
2020-11-11 14:07 ` tomas
2020-11-11 17:20 ` Christopher Dimech
1 sibling, 2 replies; 50+ messages in thread
From: Christopher Dimech @ 2020-11-11 13:22 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
> Sent: Wednesday, November 11, 2020 at 12:16 PM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> <tomas@tuxteam.de> writes:
>
> > But watch out: the replacement string (confusingly called `regex' here,
> > others have already pointed that out) might contain special sequences
> > (e.g. "\\&") which have a meaning in this context.
>
> Indeed, an important objection.
>
> I would suggest to avoid using any query-replace at all and rather use
> something like
>
> #+begin_src emacs-lisp
> (save-excursion
> (while (and (<= (point) end)
> (not (eobp)))
> (insert my-string)
> (forward-line +1))
> (deactivate-mark))
> #+end_src
That's what I started doing at the beginning. Should we revert to this
or continue with replace-regexp. Have now removed the confusingly called
`regex' and it is currently called nwltok.
> > Cheers
>
> Ebenso!
>
>
> Regards,
>
> Michael.
>
>
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-11 13:22 ` Christopher Dimech
@ 2020-11-11 13:56 ` Michael Heerdegen
2020-11-11 14:07 ` tomas
1 sibling, 0 replies; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-11 13:56 UTC (permalink / raw)
To: help-gnu-emacs
Christopher Dimech <dimech@gmx.com> writes:
> Should we revert to this or continue with replace-regexp.
That's totally up to you. If you don't want an interactive prompt, the
`insert' approach is the (in my opinion) simpler one. But each is ok.
It's a matter of (your) taste.
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-11 13:22 ` Christopher Dimech
2020-11-11 13:56 ` Michael Heerdegen
@ 2020-11-11 14:07 ` tomas
2020-11-11 14:49 ` Christopher Dimech
1 sibling, 1 reply; 50+ messages in thread
From: tomas @ 2020-11-11 14:07 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 1522 bytes --]
On Wed, Nov 11, 2020 at 02:22:44PM +0100, Christopher Dimech wrote:
>
> > Sent: Wednesday, November 11, 2020 at 12:16 PM
> > From: "Michael Heerdegen" <michael_heerdegen@web.de>
> > To: help-gnu-emacs@gnu.org
> > Subject: Re: Adding String to Beginning of Lines
> >
> > <tomas@tuxteam.de> writes:
> >
> > > But watch out: the replacement string (confusingly called `regex' here,
> > > others have already pointed that out) might contain special sequences
> > > (e.g. "\\&") which have a meaning in this context.
> >
> > Indeed, an important objection.
> >
> > I would suggest to avoid using any query-replace at all and rather use
> > something like
> >
> > #+begin_src emacs-lisp
> > (save-excursion
> > (while (and (<= (point) end)
> > (not (eobp)))
> > (insert my-string)
> > (forward-line +1))
> > (deactivate-mark))
> > #+end_src
>
> That's what I started doing at the beginning. Should we revert to this
> or continue with replace-regexp. Have now removed the confusingly called
> `regex' and it is currently called nwltok.
If it's always the beginning of line you're inserting at, your first
approach seems to make more sense.
Something like
(beginning-of-line) ; if you aren't already there, see below
(insert "foo")
should do what you're after.
The beginning-of-line isn't necessary if your advance function
puts you already there. Note that (beginning-of-line 2) will
put you at the beginning of the /next/ line.
Cheers
- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-11 14:07 ` tomas
@ 2020-11-11 14:49 ` Christopher Dimech
0 siblings, 0 replies; 50+ messages in thread
From: Christopher Dimech @ 2020-11-11 14:49 UTC (permalink / raw)
To: tomas; +Cc: help-gnu-emacs
> Sent: Wednesday, November 11, 2020 at 3:07 PM
> From: tomas@tuxteam.de
> To: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> On Wed, Nov 11, 2020 at 02:22:44PM +0100, Christopher Dimech wrote:
> >
> > > Sent: Wednesday, November 11, 2020 at 12:16 PM
> > > From: "Michael Heerdegen" <michael_heerdegen@web.de>
> > > To: help-gnu-emacs@gnu.org
> > > Subject: Re: Adding String to Beginning of Lines
> > >
> > > <tomas@tuxteam.de> writes:
> > >
> > > > But watch out: the replacement string (confusingly called `regex' here,
> > > > others have already pointed that out) might contain special sequences
> > > > (e.g. "\\&") which have a meaning in this context.
> > >
> > > Indeed, an important objection.
> > >
> > > I would suggest to avoid using any query-replace at all and rather use
> > > something like
> > >
> > > #+begin_src emacs-lisp
> > > (save-excursion
> > > (while (and (<= (point) end)
> > > (not (eobp)))
> > > (insert my-string)
> > > (forward-line +1))
> > > (deactivate-mark))
> > > #+end_src
> >
> > That's what I started doing at the beginning. Should we revert to this
> > or continue with replace-regexp. Have now removed the confusingly called
> > `regex' and it is currently called nwltok.
>
> If it's always the beginning of line you're inserting at, your first
> approach seems to make more sense.
>
> Something like
>
> (beginning-of-line) ; if you aren't already there, see below
> (insert "foo")
>
> should do what you're after.
Correct. Always at beginning of line
> The beginning-of-line isn't necessary if your advance function
> puts you already there. Note that (beginning-of-line 2) will
> put you at the beginning of the /next/ line.
>
> Cheers
> - t
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-11 11:16 ` Michael Heerdegen
2020-11-11 13:22 ` Christopher Dimech
@ 2020-11-11 17:20 ` Christopher Dimech
2020-11-11 18:12 ` Michael Heerdegen
1 sibling, 1 reply; 50+ messages in thread
From: Christopher Dimech @ 2020-11-11 17:20 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
Have put the following, bet the insert is not progressing to the
last line of the selected region. I started my selected region
on the first character of 'RFrec-field-nwltok' and ended my
selected region on the last character of 'insert'.
>
> (defun RFrec-field-nwltok (beg end nwltok)
> "rec-field-nwltok adds a newline token for a recutils field."
> (interactive
> (list (region-beginning)
> (region-end)
> (read-string "rec-field-nwltok: " nil nil ";; + ") ))
> (save-excursion
> (let (rz lpos)
> (setq rz (region-end))
> (goto-char (region-beginning))
> (beginning-of-line)
>
> (while (and (< (point) rz)
> (not (eobp)))
> (insert nwltok)
> (forward-line 1)) )
> (deactivate-mark) ))
>
> Sent: Wednesday, November 11, 2020 at 12:16 PM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> <tomas@tuxteam.de> writes:
>
> > But watch out: the replacement string (confusingly called `regex' here,
> > others have already pointed that out) might contain special sequences
> > (e.g. "\\&") which have a meaning in this context.
>
> Indeed, an important objection.
>
> I would suggest to avoid using any query-replace at all and rather use
> something like
>
> #+begin_src emacs-lisp
> (save-excursion
> (while (and (<= (point) end)
> (not (eobp)))
> (insert my-string)
> (forward-line +1))
> (deactivate-mark))
> #+end_src
>
> > Cheers
>
> Ebenso!
>
>
> Regards,
>
> Michael.
>
>
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-11 17:20 ` Christopher Dimech
@ 2020-11-11 18:12 ` Michael Heerdegen
2020-11-11 18:25 ` Stefan Monnier
2020-11-11 18:35 ` Christopher Dimech
0 siblings, 2 replies; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-11 18:12 UTC (permalink / raw)
To: help-gnu-emacs
Christopher Dimech <dimech@gmx.com> writes:
> Have put the following, bet the insert is not progressing to the
> last line of the selected region. I started my selected region
> on the first character of 'RFrec-field-nwltok' and ended my
> selected region on the last character of 'insert'.
Oh, of course, while inserting the stuff your specified end position
gets outdated. You can use a marker instead of a number:
(copy-marker (region-end))
it will automatically be adjusted. Arithmetic relations like `<' also
accept markers, so you don't need to rewrite your code.
BTW, your function calls `region-beginning' and `region-end' in the
interactive spec and a second time in the function body: The function
arguments BEG and END are ignored in the body. That makes no sense.
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-11 18:12 ` Michael Heerdegen
@ 2020-11-11 18:25 ` Stefan Monnier
2020-11-11 18:35 ` Christopher Dimech
1 sibling, 0 replies; 50+ messages in thread
From: Stefan Monnier @ 2020-11-11 18:25 UTC (permalink / raw)
To: help-gnu-emacs
> Oh, of course, while inserting the stuff your specified end position
> gets outdated. You can use a marker instead of a number:
>
> (copy-marker (region-end))
>
> it will automatically be adjusted.
The other classic solution is to do if starting from the end since the
region-start won't be affected by the buffer modifications.
Stefan
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-11 18:12 ` Michael Heerdegen
2020-11-11 18:25 ` Stefan Monnier
@ 2020-11-11 18:35 ` Christopher Dimech
1 sibling, 0 replies; 50+ messages in thread
From: Christopher Dimech @ 2020-11-11 18:35 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
> Sent: Wednesday, November 11, 2020 at 7:12 PM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Adding String to Beginning of Lines
>
> Christopher Dimech <dimech@gmx.com> writes:
>
> > Have put the following, bet the insert is not progressing to the
> > last line of the selected region. I started my selected region
> > on the first character of 'RFrec-field-nwltok' and ended my
> > selected region on the last character of 'insert'.
>
> Oh, of course, while inserting the stuff your specified end position
> gets outdated. You can use a marker instead of a number:
>
> (copy-marker (region-end))
I did not consider that detail.
> it will automatically be adjusted. Arithmetic relations like `<' also
> accept markers, so you don't need to rewrite your code.
>
> BTW, your function calls `region-beginning' and `region-end' in the
> interactive spec and a second time in the function body: The function
> arguments BEG and END are ignored in the body. That makes no sense.
You are correct, have now changed those parts.
> Michael.
>
>
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 23:00 ` Drew Adams
@ 2020-11-11 21:37 ` Michael Heerdegen
0 siblings, 0 replies; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-11 21:37 UTC (permalink / raw)
To: help-gnu-emacs
Drew Adams <drew.adams@oracle.com> writes:
> Perhaps someone added it to have a unary predicate
> to pass, instead of using an anonymous function or
> `apply-partially'?
BTW, we also have a `string-blank-p': "Check whether STRING is either
empty or only whitespace.".
Regards,
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-10 10:43 Adding String to Beginning of Lines Christopher Dimech
2020-11-10 11:10 ` tomas
2020-11-10 11:42 ` Michael Heerdegen
@ 2020-11-12 19:01 ` Dante Catalfamo
2020-11-14 22:09 ` Jamie Beardslee
2 siblings, 1 reply; 50+ messages in thread
From: Dante Catalfamo @ 2020-11-12 19:01 UTC (permalink / raw)
To: help-gnu-emacs
The way I would do this would be to make a zero character wide rectangle
selection that goes down the left side of the paragraph, and then use
string-rectangle to insert the characters.
(starting all the way at the left side)
C-x SPC
(move down to the bottom of the area you want to add the lines to)
C-t
;; +
RET
On 11/10/20 5:43 AM, Christopher Dimech wrote:
>
> I have a paragraph and want to add a string to the beginning of each line after selecting
> the paragraph region.
>
> GNU is an operating system that is free software—that is, it respects users' freedom. The
> GNU operating system consists of GNU packages (programs specifically released by the GNU
> Project) as well as free software released by third parties. The development of GNU made
> it possible to use a computer without software that would trample your freedom.
>
> Example, passing ';; + ' as argument to a function to get
>
> ;; + GNU is an operating system that is free software—that is, it respects users' freedom. The
> ;; + GNU operating system consists of GNU packages (programs specifically released by the GNU
> ;; + Project) as well as free software released by third parties. The development of GNU made
> ;; + it possible to use a computer without software that would trample your freedom.
>
>
>
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-12 19:01 ` Dante Catalfamo
@ 2020-11-14 22:09 ` Jamie Beardslee
2020-11-14 23:17 ` Stefan Monnier
0 siblings, 1 reply; 50+ messages in thread
From: Jamie Beardslee @ 2020-11-14 22:09 UTC (permalink / raw)
To: help-gnu-emacs
Dante Catalfamo <dante@lambda.cx> writes:
> C-x SPC
You don't actually need to use a rectangle selection, as all the
rectangle functions figure out what rectangle you mean with just a
normal region. So this can just be C-SPC.
> C-t
"C-x r t" is the default binding, I'm assuming this is just a typo?
--
Jamie
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-14 22:09 ` Jamie Beardslee
@ 2020-11-14 23:17 ` Stefan Monnier
2020-11-14 23:39 ` Jamie Beardslee
2020-11-15 9:12 ` tomas
0 siblings, 2 replies; 50+ messages in thread
From: Stefan Monnier @ 2020-11-14 23:17 UTC (permalink / raw)
To: help-gnu-emacs
>> C-x SPC
>
> You don't actually need to use a rectangle selection, as all the
> rectangle functions figure out what rectangle you mean with just a
> normal region. So this can just be C-SPC.
>
>> C-t
>
> "C-x r t" is the default binding, I'm assuming this is just a typo?
No, it's that after `C-x SPC` the user is placed in the
`rectangle-mark-mode` minor mode where `C-t` has a special binding
(the same as `C-x r t`).
Stefan
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-14 23:17 ` Stefan Monnier
@ 2020-11-14 23:39 ` Jamie Beardslee
2020-11-15 9:12 ` tomas
1 sibling, 0 replies; 50+ messages in thread
From: Jamie Beardslee @ 2020-11-14 23:39 UTC (permalink / raw)
To: help-gnu-emacs
Stefan Monnier <monnier@iro.umontreal.ca> writes:
> No, it's that after `C-x SPC` the user is placed in the
> `rectangle-mark-mode` minor mode where `C-t` has a special binding
> (the same as `C-x r t`).
Oh my bad, I'm surprised I hadn't figured that out yet. Can't find any
mention of this in the docstring or manual though.
--
Jamie
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-14 23:17 ` Stefan Monnier
2020-11-14 23:39 ` Jamie Beardslee
@ 2020-11-15 9:12 ` tomas
1 sibling, 0 replies; 50+ messages in thread
From: tomas @ 2020-11-15 9:12 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 756 bytes --]
On Sat, Nov 14, 2020 at 06:17:07PM -0500, Stefan Monnier wrote:
> >> C-x SPC
> >
> > You don't actually need to use a rectangle selection, as all the
> > rectangle functions figure out what rectangle you mean with just a
> > normal region. So this can just be C-SPC.
> >
> >> C-t
> >
> > "C-x r t" is the default binding, I'm assuming this is just a typo?
>
> No, it's that after `C-x SPC` the user is placed in the
> `rectangle-mark-mode` minor mode where `C-t` has a special binding
> (the same as `C-x r t`).
And... to top that off, that one really highlights a rectangle, which
looks wickedly cool ;-)
Since I've always been using C-SPC for that, I hadn't noticed. Thanks
to all involved in that little present!
Cheers
- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-11 10:59 ` Michael Heerdegen
@ 2020-11-17 16:06 ` Jean Louis
2020-11-17 16:28 ` Michael Heerdegen
0 siblings, 1 reply; 50+ messages in thread
From: Jean Louis @ 2020-11-17 16:06 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
* Michael Heerdegen <michael_heerdegen@web.de> [2020-11-11 14:01]:
> Jean Louis <bugs@gnu.support> writes:
>
> > If I understand well I can simply remove my function and instead use
> > seq-empty-p? It looks that it does what we discussed here.
>
> Yes. Please (require 'seq) before using it. Note that very old Emacs
> versions don't have the "seq" library. For those (and only those) a
> "seq" package is provided in Gnu Elpa.
I see when doing package-install that I do need to require all
packages and I do so.
Do I need also to put those built-in packages in the comment section
of Package-Requires: ?
^ permalink raw reply [flat|nested] 50+ messages in thread
* Re: Adding String to Beginning of Lines
2020-11-17 16:06 ` Jean Louis
@ 2020-11-17 16:28 ` Michael Heerdegen
0 siblings, 0 replies; 50+ messages in thread
From: Michael Heerdegen @ 2020-11-17 16:28 UTC (permalink / raw)
To: Jean Louis; +Cc: help-gnu-emacs
Jean Louis <bugs@gnu.support> writes:
> Do I need also to put those built-in packages in the comment section
> of Package-Requires: ?
AFAIK, yes, that makes sense. That way you can specify a minimum
version of a built-in package that you need for your stuff to work (M-x
list-packages can list packages as "incompatible"). And a package
built-in in your Emacs version might be not built-in in another (as it
is the case for seq and AFAIR cl-lib) so it might be necessary to
install there.
Michael.
^ permalink raw reply [flat|nested] 50+ messages in thread
end of thread, other threads:[~2020-11-17 16:28 UTC | newest]
Thread overview: 50+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-11-10 10:43 Adding String to Beginning of Lines Christopher Dimech
2020-11-10 11:10 ` tomas
2020-11-10 11:19 ` Christopher Dimech
2020-11-10 11:29 ` Christopher Dimech
2020-11-10 11:54 ` Michael Heerdegen
2020-11-10 11:58 ` Christopher Dimech
2020-11-10 12:07 ` Michael Heerdegen
2020-11-10 12:03 ` Christopher Dimech
2020-11-10 12:16 ` Michael Heerdegen
2020-11-10 13:40 ` Christopher Dimech
2020-11-10 14:06 ` Jean Louis
2020-11-10 14:29 ` Michael Heerdegen
2020-11-10 19:12 ` Jean Louis
2020-11-10 19:42 ` Michael Heerdegen
2020-11-10 21:22 ` Jean Louis
2020-11-11 10:59 ` Michael Heerdegen
2020-11-17 16:06 ` Jean Louis
2020-11-17 16:28 ` Michael Heerdegen
2020-11-10 22:31 ` Stefan Monnier
2020-11-10 23:00 ` Drew Adams
2020-11-11 21:37 ` Michael Heerdegen
2020-11-10 14:16 ` Michael Heerdegen
2020-11-10 14:38 ` Christopher Dimech
2020-11-10 14:49 ` Michael Heerdegen
2020-11-10 17:52 ` Christopher Dimech
2020-11-10 18:03 ` Michael Heerdegen
2020-11-10 18:25 ` Christopher Dimech
2020-11-10 18:49 ` Michael Heerdegen
2020-11-10 18:56 ` Christopher Dimech
2020-11-10 19:02 ` Michael Heerdegen
2020-11-10 19:29 ` Christopher Dimech
2020-11-10 20:07 ` Michael Heerdegen
2020-11-10 22:45 ` tomas
2020-11-11 11:16 ` Michael Heerdegen
2020-11-11 13:22 ` Christopher Dimech
2020-11-11 13:56 ` Michael Heerdegen
2020-11-11 14:07 ` tomas
2020-11-11 14:49 ` Christopher Dimech
2020-11-11 17:20 ` Christopher Dimech
2020-11-11 18:12 ` Michael Heerdegen
2020-11-11 18:25 ` Stefan Monnier
2020-11-11 18:35 ` Christopher Dimech
2020-11-10 14:03 ` Jean Louis
2020-11-10 11:42 ` Michael Heerdegen
2020-11-10 18:47 ` H. Dieter Wilhelm
2020-11-12 19:01 ` Dante Catalfamo
2020-11-14 22:09 ` Jamie Beardslee
2020-11-14 23:17 ` Stefan Monnier
2020-11-14 23:39 ` Jamie Beardslee
2020-11-15 9:12 ` tomas
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.