How to define comment syntax for an entire line in Emacs?

I want the sequence // start the comment when it is at the beginning of the line. But inside the line, it should not start any comments.

 // this is a comment This is a URL: http://example.com 

Is it possible?

+7
comments emacs elisp
source share
2 answers

You can do this by writing syntax-propertize-function

I wrote an example of the main mode that shows this below. Emacs, built into the parsing, will call your syntax-propertize-function so that it can manually set the syntax-table text property in the lines starting with //.

 (define-derived-mode my-syntax-test-mode fundamental-mode "A major mode where // denotes a comment but only if it is at the beginning of a line." :syntax-table (make-syntax-table) (setq mode-name "my syntax test") ;; our mode will use `apply-my-custom-syntax-table-appropriately' to manually set ;; the syntax-table text property on lines starting with //" (setq syntax-propertize-function 'apply-my-custom-syntax-table-appropriately) ;; change `comment-dwim` to handle this type of comments correctly (local-set-key [remap comment-dwim] 'my-comment-dwim)) (defvar my-custom-syntax-table ;; syntax table where // starts a comment and \n ends it (let ((table (make-syntax-table))) (modify-syntax-entry ?/ "< 1" table) (modify-syntax-entry ?/ "< 2" table) (modify-syntax-entry ?\n "> " table) table)) (defun apply-my-custom-syntax-table-appropriately (beg end) (save-excursion (save-restriction (widen) (goto-char beg) ;; for every line between points BEG and END (while (and (not (eobp)) (< (point) end)) (beginning-of-line) ;; if it starts with a // (when (looking-at "^//") ;; remove current syntax-table property (remove-text-properties (1- (line-beginning-position)) (1+ (line-end-position)) '(syntax-table)) ;; set syntax-table property to our custom one ;; for the whole line including the beginning and ending newlines (add-text-properties (1- (line-beginning-position)) (1+ (line-end-position)) (list 'syntax-table my-custom-syntax-table))) (forward-line 1))))) (defun my-comment-dwim (arg) (interactive "*P") (require 'newcomment) (save-excursion (let ((comment-start "//") (comment-end "") (comment-column 0) ;; don't indent comments (comment-style 'plain)) ;; create the region containing current line if there is no active region (unless (use-region-p) (end-of-line) (push-mark (line-beginning-position)) (setq mark-active t)) (comment-dwim nil)))) 
+5
source share

I would do it like this:

 (defvar my-foo-mode-syntax-table (let ((st (make-syntax-table))) ;; Add other entries appropriate for my-foo-mode. (modify-syntax-entry ?/ ". 12" st) (modify-syntax-entry ?\n "> " st) st)) (defvar my-foo-font-lock-keywords ;; Add other rules appropriate for my-foo-mode. ()) (define-derived-mode my-foo-mode nil "My-Foo" (setq-local font-lock-keywords '(my-foo-font-lock-keywords)) ;; Add other settings appropriate for my-foo-mode. (setq-local syntax-propertize-function (syntax-propertize-rules ("./\\(/+\\)" (1 "."))))) 

Note. There is no need for any special font lock rule, since the font-lock function automatically selects comments for you based on syntax tables.

+6
source share

All Articles