unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Omar Polo <op@omarpolo.com>
To: martin-kemp@brusseler.com
Cc: help-gnu-emacs@gnu.org
Subject: Re: Using comment characters for specific major modes
Date: Sun, 06 Jun 2021 10:34:12 +0200	[thread overview]
Message-ID: <87im2rcry3.fsf@omarpolo.com> (raw)
In-Reply-To: <87lf7ncs1f.fsf@omarpolo.com>

I've seen only now the reply from Stefan Monnier, which is (of course
:P) better than mine, apologies.  His explanation is indeed better.

Sorry for the noise.

Omar Polo <op@omarpolo.com> writes:

> martin-kemp@brusseler.com writes:
>
>> Am using the following expression to make a line composed of ";" of length lena.
>>
>>
>>
>> The first two semicolons ";;" are for when I use elisp code.
>>
>>
>>
>>     (setq-local s (concat ";; " (make-string lena ?\;)))
>>
>>
>>
>> But I want to change the starting ";;" to be the comment character of the major mode I am working with.
>>
>>
>>
>> For texinfo I want "@c", and for fortran-mode I want "c", and "!!" for f90-mode.
>
> taking a look at how things like `comment-dwim' is quite educational.
> The comment handling is easy but there are a few gotchas, like not all
> major-modes have a single "comment string" (like ";" in Lisp), some have
> starting and ending comment string (like C with /* and */)
>
> I've come up with the following
>
> --------8<--------
> (defun insert-lena ()
>   (interactive)
>   (let* ((lena 8)
>          (s (make-string lena ?\;)))
>     ;; this bit is stolen from comment-dwim
>     (if comment-insert-comment-function
>         (funcall comment-insert-comment-function)
>       (let ((add (comment-add nil)))
>         (indent-according-to-mode)
>         (insert (comment-padright comment-start add))
>         (save-excursion
>           (unless (string= "" comment-end)
>             (insert (comment-padleft comment-end add)))
>           (indent-according-to-mode))))
>     ;; insert the string
>     (insert s)))
> -------->8--------
>
> that seems to works.
>
> It uses comment-insert-comment-function or comment-start/end.  I stolen
> a bit from comment-dwim.
>
> In lisps buffer it inserts
>
> ;; ;;;;;;;;
>
> while in a C buffer it adds
>
> /* ;;;;;;;; */
>
> Probably it doesn't do exactly what you want, and there are probably
> edge cases when there is a region active or things like that, but it's a
> good start (I think).
>
> HTH




  reply	other threads:[~2021-06-06  8:34 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-05 20:52 Using comment characters for specific major modes martin-kemp
2021-06-05 21:50 ` Jean Louis
2021-06-05 22:05   ` martin-kemp
2021-06-05 22:40     ` Jean Louis
2021-06-05 22:53       ` martin-kemp
2021-06-05 23:05         ` Jean Louis
2021-06-05 23:20           ` martin-kemp
2021-06-05 23:32             ` Jean Louis
2021-06-05 23:47               ` martin-kemp
2021-06-05 23:56                 ` Jean Louis
2021-06-06  0:17                   ` martin-kemp
2021-06-06  0:23                     ` Jean Louis
2021-06-06  0:39                       ` martin-kemp
2021-06-06  0:43                         ` Jean Louis
2021-06-06  9:14                           ` martin-kemp
2021-06-06  9:46                             ` Jean Louis
2021-06-06 16:02                             ` Stefan Möding
2021-06-06 16:23                               ` martin-kemp
2021-06-06 16:40                                 ` Jean Louis
2021-06-06 16:53                                   ` martin-kemp
2021-06-06 16:56                                 ` Jean Louis
2021-06-06 17:36                                   ` martin-kemp
2021-06-06 20:50                                     ` Jean Louis
2021-06-08  9:14                                       ` martin-kemp
2021-06-06  3:24             ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-09 15:42               ` martin-kemp
2021-06-06  8:32 ` Omar Polo
2021-06-06  8:34   ` Omar Polo [this message]
2021-06-06  9:05     ` martin-kemp
2021-06-06 11:43       ` Omar Polo

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=87im2rcry3.fsf@omarpolo.com \
    --to=op@omarpolo.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=martin-kemp@brusseler.com \
    /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.
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).