From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Omar Polo Newsgroups: gmane.emacs.help Subject: Re: Using comment characters for specific major modes Date: Sun, 06 Jun 2021 10:32:12 +0200 Message-ID: <87lf7ncs1f.fsf@omarpolo.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22340"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.4.15; emacs 28.0.50 Cc: help-gnu-emacs@gnu.org To: martin-kemp@brusseler.com Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sun Jun 06 10:33:07 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lpoDT-0005YW-EM for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 06 Jun 2021 10:33:07 +0200 Original-Received: from localhost ([::1]:33374 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lpoDR-0005jG-QF for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 06 Jun 2021 04:33:05 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59572) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lpoCn-0005hj-7D for help-gnu-emacs@gnu.org; Sun, 06 Jun 2021 04:32:25 -0400 Original-Received: from mail.omarpolo.com ([144.91.116.244]:51269) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lpoCj-0008L9-CT for help-gnu-emacs@gnu.org; Sun, 06 Jun 2021 04:32:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=omarpolo.com; s=20200327; t=1622968337; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=NdtjG6G4LkQOf9AtBLI56ooKw0yutLxILzBcnQqyyQU=; b=4TK1Mn6BWCpaXspWipDeOfBQUIb8NeubTURY5HsMs3h+ZRk9IWLWQseuRRmads5YpvZ+Qr FgyWriqzNL4PyhnIHrp2Admzw5yplSRLyYJlxxYTtW8a5ziKjchzqtx5O9HPUE70ViXils 4g4cvWb+G1tO1bCBAFEi0Cgo/8I2Cs0= Original-Received: from localhost (host-79-44-237-248.retail.telecomitalia.it [79.44.237.248]) by mail.omarpolo.com (OpenSMTPD) with ESMTPSA id c405bbb7 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sun, 6 Jun 2021 10:32:16 +0200 (CEST) Original-Received: from venera (localhost [127.0.0.1]) by localhost (OpenSMTPD) with ESMTP id ee5d077a; Sun, 6 Jun 2021 10:32:12 +0200 (CEST) In-reply-to: Received-SPF: pass client-ip=144.91.116.244; envelope-from=op@omarpolo.com; helo=mail.omarpolo.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:130573 Archived-At: 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