unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Underline the current line (function inside)
@ 2021-06-11 14:32 Jean Louis
  2021-06-11 14:52 ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Jean Louis @ 2021-06-11 14:32 UTC (permalink / raw)
  To: Help GNU Emacs

This function is handy to underline the current line:

(defun underline-line ()
  "Underline the current line."
  (interactive)
  (let* ((start (line-beginning-position))
	 (end (line-end-position))
	 (length (- end start)))
    (end-of-line)
    (newline)
    (insert (make-string length ?=))
    (newline)))


When there is a line like this, a key binding or M-x underline-line
===================================================================

does that underlining as above.



Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/






^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Underline the current line (function inside)
  2021-06-11 14:32 Underline the current line (function inside) Jean Louis
@ 2021-06-11 14:52 ` Eli Zaretskii
  2021-06-11 15:33   ` Jean Louis
  0 siblings, 1 reply; 4+ messages in thread
From: Eli Zaretskii @ 2021-06-11 14:52 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Fri, 11 Jun 2021 17:32:26 +0300
> From: Jean Louis <bugs@gnu.support>
> 
> This function is handy to underline the current line:
> 
> (defun underline-line ()
>   "Underline the current line."
>   (interactive)
>   (let* ((start (line-beginning-position))
> 	 (end (line-end-position))
> 	 (length (- end start)))
>     (end-of-line)
>     (newline)
>     (insert (make-string length ?=))
>     (newline)))

This assumes that each character takes 1 column.  That is false in
general, because some characters have zero width on display, and some
others take closer to two columns.

You need to use string-width instead, or count columns instead of
buffer positions.

And, of course, all this assumes fixed-pitch font.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Underline the current line (function inside)
  2021-06-11 14:52 ` Eli Zaretskii
@ 2021-06-11 15:33   ` Jean Louis
  2021-06-11 17:55     ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Jean Louis @ 2021-06-11 15:33 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs

* Eli Zaretskii <eliz@gnu.org> [2021-06-11 17:53]:
> > Date: Fri, 11 Jun 2021 17:32:26 +0300
> > From: Jean Louis <bugs@gnu.support>
> > 
> > This function is handy to underline the current line:
> > 
> > (defun underline-line ()
> >   "Underline the current line."
> >   (interactive)
> >   (let* ((start (line-beginning-position))
> > 	 (end (line-end-position))
> > 	 (length (- end start)))
> >     (end-of-line)
> >     (newline)
> >     (insert (make-string length ?=))
> >     (newline)))
> 
> This assumes that each character takes 1 column.  That is false in
> general, because some characters have zero width on display, and some
> others take closer to two columns.
> 
> You need to use string-width instead, or count columns instead of
> buffer positions.
> 
> And, of course, all this assumes fixed-pitch font.

Thanks.

(defun underline-line ()
  "Underline the current line."
  (interactive)
  (let* ((start (line-beginning-position))
	 (end (line-end-position))
	 (length (string-width (buffer-substring-no-properties start end))))
    (end-of-line)
    (newline)
    (insert (make-string length ?=))
    (newline)))




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Underline the current line (function inside)
  2021-06-11 15:33   ` Jean Louis
@ 2021-06-11 17:55     ` Eli Zaretskii
  0 siblings, 0 replies; 4+ messages in thread
From: Eli Zaretskii @ 2021-06-11 17:55 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Fri, 11 Jun 2021 18:33:45 +0300
> From: Jean Louis <bugs@gnu.support>
> Cc: help-gnu-emacs@gnu.org
> 
> (defun underline-line ()
>   "Underline the current line."
>   (interactive)
>   (let* ((start (line-beginning-position))
> 	 (end (line-end-position))
> 	 (length (string-width (buffer-substring-no-properties start end))))
>     (end-of-line)
>     (newline)
>     (insert (make-string length ?=))
>     (newline)))

Right.  I suggest to try the alternative that uses current-column, it
will allow you to avoid consing a string, and I think will also be
faster.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-06-11 17:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-06-11 14:32 Underline the current line (function inside) Jean Louis
2021-06-11 14:52 ` Eli Zaretskii
2021-06-11 15:33   ` Jean Louis
2021-06-11 17:55     ` Eli Zaretskii

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).