help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using comment characters for specific major modes


From: Omar Polo
Subject: Re: Using comment characters for specific major modes
Date: Sun, 06 Jun 2021 10:34:12 +0200
User-agent: mu4e 1.4.15; emacs 28.0.50

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 via email to

[Prev in Thread] Current Thread [Next in Thread]