unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to check whether the current line is a comment-only one?
@ 2016-08-18  6:16 Marcin Borkowski
  2016-08-18  7:44 ` Stefan Monnier
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Marcin Borkowski @ 2016-08-18  6:16 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi folks,

is there a predicate function to determine whether the line the point is
in is entirely a comment line?  I'd prefer this function to work in
various modes (for Elisp mode, it's trivial; for C, less so).

TIA,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: How to check whether the current line is a comment-only one?
  2016-08-18  6:16 How to check whether the current line is a comment-only one? Marcin Borkowski
@ 2016-08-18  7:44 ` Stefan Monnier
  2016-08-18 14:11   ` Marcin Borkowski
  2016-08-18 11:39 ` Kaushal Modi
  2016-08-18 14:43 ` Thien-Thi Nguyen
  2 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2016-08-18  7:44 UTC (permalink / raw)
  To: help-gnu-emacs

> is there a predicate function to determine whether the line the point is
> in is entirely a comment line?

I don't think there is one yet.  Depending on what you mean by "entirely
a comment", it could be:

    (save-excursion
      (forward-line 0)
      (< (line-end-position)
         (let ((ppss (syntax-ppss)))
           (when (nth 4 ppss)
             (goto-char (nth 8 ppss)))
           (forward-comment (point-max))
           (point))))


-- Stefan




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

* Re: How to check whether the current line is a comment-only one?
  2016-08-18  6:16 How to check whether the current line is a comment-only one? Marcin Borkowski
  2016-08-18  7:44 ` Stefan Monnier
@ 2016-08-18 11:39 ` Kaushal Modi
  2016-08-18 14:09   ` Marcin Borkowski
  2016-08-18 14:43 ` Thien-Thi Nguyen
  2 siblings, 1 reply; 6+ messages in thread
From: Kaushal Modi @ 2016-08-18 11:39 UTC (permalink / raw)
  To: Marcin Borkowski, Help Gnu Emacs mailing list

Hi Marcin,

You can use the syntax-ppss function to check that syntax at the current
point. You can know if the current point is in a string or a comment, and
so on.

Check out the (elisp) Parser State Info node for more info.

If (nth 4 (syntax-ppss)) returns a non-nil value, the point is currently in
a comment.

Now, do you need to know if the *entire* line is a comment?  If so, the
solution gets a bit complicated, but you can do it with the help of (nth 4
(syntax-ppss)) by moving the point to various positions on the line.

Here's a simple example of one of my uses of syntax-ppss:

https://github.com/kaushalmodi/.emacs.d/blob/e8418e97abd36e5eeaf6376555b17c79e06a56fa/setup-files/setup-editing.el#L381

Here's a little more complicated example:

https://github.com/kaushalmodi/.emacs.d/blob/e8418e97abd36e5eeaf6376555b17c79e06a56fa/setup-files/setup-editing.el#L81

Kaushal

On Thu, Aug 18, 2016, 3:17 AM Marcin Borkowski <mbork@mbork.pl> wrote:

> Hi folks,
>
> is there a predicate function to determine whether the line the point is
> in is entirely a comment line?  I'd prefer this function to work in
> various modes (for Elisp mode, it's trivial; for C, less so).
>
-- 

Kaushal Modi


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

* Re: How to check whether the current line is a comment-only one?
  2016-08-18 11:39 ` Kaushal Modi
@ 2016-08-18 14:09   ` Marcin Borkowski
  0 siblings, 0 replies; 6+ messages in thread
From: Marcin Borkowski @ 2016-08-18 14:09 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: Help Gnu Emacs mailing list


On 2016-08-18, at 13:39, Kaushal Modi <kaushal.modi@gmail.com> wrote:

> Hi Marcin,
>
> You can use the syntax-ppss function to check that syntax at the current
> point. You can know if the current point is in a string or a comment, and
> so on.
>
> Check out the (elisp) Parser State Info node for more info.
>
> If (nth 4 (syntax-ppss)) returns a non-nil value, the point is currently in
> a comment.

Well, I was afraid someone would tell me that...  I hoped for some other
solution.  But thanks anyway.

> Now, do you need to know if the *entire* line is a comment?  If so, the
> solution gets a bit complicated, but you can do it with the help of (nth 4
> (syntax-ppss)) by moving the point to various positions on the line.

Yes, I want to know exactly that.  The rationale is that I'm working on
bug #21072 (still - slowly but steadily...), and I want mark-defun to
include comment-only lines preceding the defun.  (I'm going to report on
my work on #21072 soon - I implemented a nice (imho) macro to help test
functions/commands which move point and/or mark around.)

> Here's a simple example of one of my uses of syntax-ppss:
>
> https://github.com/kaushalmodi/.emacs.d/blob/e8418e97abd36e5eeaf6376555b17c79e06a56fa/setup-files/setup-editing.el#L381
>
> Here's a little more complicated example:
>
> https://github.com/kaushalmodi/.emacs.d/blob/e8418e97abd36e5eeaf6376555b17c79e06a56fa/setup-files/setup-editing.el#L81

Thanks, I'll look into these a bit later.

> Kaushal

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: How to check whether the current line is a comment-only one?
  2016-08-18  7:44 ` Stefan Monnier
@ 2016-08-18 14:11   ` Marcin Borkowski
  0 siblings, 0 replies; 6+ messages in thread
From: Marcin Borkowski @ 2016-08-18 14:11 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


On 2016-08-18, at 09:44, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

>> is there a predicate function to determine whether the line the point is
>> in is entirely a comment line?
>
> I don't think there is one yet.  Depending on what you mean by "entirely
> a comment", it could be:
>
>     (save-excursion
>       (forward-line 0)
>       (< (line-end-position)
>          (let ((ppss (syntax-ppss)))
>            (when (nth 4 ppss)
>              (goto-char (nth 8 ppss)))
>            (forward-comment (point-max))
>            (point))))

Hi Stefan,

from a cursory read of the above, this might be exactly what I need.
Thanks.

BTW, AUCTeX has a function called `TeX-in-commented-line'.  Here's its
docstring:

--8<---------------cut here---------------start------------->8---
Return non-nil if point is in a line consisting only of a comment.
The comment can be preceded by whitespace.  This means that
‘TeX-in-commented-line’ is more general than ‘TeX-in-line-comment’
which will not match commented lines with leading whitespace.  But
‘TeX-in-commented-line’ will match commented lines without leading
whitespace as well.
--8<---------------cut here---------------end--------------->8---

> -- Stefan

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: How to check whether the current line is a comment-only one?
  2016-08-18  6:16 How to check whether the current line is a comment-only one? Marcin Borkowski
  2016-08-18  7:44 ` Stefan Monnier
  2016-08-18 11:39 ` Kaushal Modi
@ 2016-08-18 14:43 ` Thien-Thi Nguyen
  2 siblings, 0 replies; 6+ messages in thread
From: Thien-Thi Nguyen @ 2016-08-18 14:43 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: Help Gnu Emacs mailing list

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

() Marcin Borkowski <mbork@mbork.pl>
() Thu, 18 Aug 2016 08:16:22 +0200

   predicate function to determine whether the line the point is
   in is entirely a comment line?  I'd prefer this function to
   work in various modes (for Elisp mode, it's trivial; for C,
   less so).

Aside from the other suggestions, see also ‘hs-inside-comment-p’
(from hideshow.el).

-- 
Thien-Thi Nguyen -----------------------------------------------
 (defun responsep (type via)
   (case type
     (technical (eq 'mailing-list via))
     ...))                              748E A0E8 1CB8 A748 9BFA
--------------------------------------- 6CE4 6703 2224 4C80 7502

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

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

end of thread, other threads:[~2016-08-18 14:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-18  6:16 How to check whether the current line is a comment-only one? Marcin Borkowski
2016-08-18  7:44 ` Stefan Monnier
2016-08-18 14:11   ` Marcin Borkowski
2016-08-18 11:39 ` Kaushal Modi
2016-08-18 14:09   ` Marcin Borkowski
2016-08-18 14:43 ` Thien-Thi Nguyen

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