Hot answers tagged

19

Check the fourth* value in the list returned by syntax-ppss: (nth 4 (syntax-ppss)) It's nil if point is outside any comment, t if inside a non-nestable comment, or an integer (the nesting depth) if inside a nestable comment. See the docstring for parse-partial-sexp for more details. * Zero-based. Note that this doesn't work with Org-mode, you should use ...


13

Actually, 3-and-more semi-colons stand for headings, where the more semi-colons you put the deeper the nesting of the heading. So it should look like ;;; Main heading ;;;; Sub heading ;;;;; Sub sub heading ;;;; Another sub heading ;;; Next main heading


12

comment-add: comment-add is a variable defined in newcomment.el. Its value is 1 Local in buffer .emacs; global value is 0 Documentation: How many more comment chars should be inserted by comment-region. This determines the default value of the numeric argument of comment-region. The plain comment style doubles this value. This should ...


12

You can check if the current character is a comment character by using the syntax table: (looking-at "\\s<"). The regexp \\s< will match any character with the "comment start" syntax; \\s> will match those with "comment end" syntax. Another option is the variable comment-start, which is the string inserted by comment-dwim and friends. It ...


11

In general, there's nothing wrong with using commands as part of elisp code. Those functions which are only meant for interactive use will (or should) warn you about that. See next-line for instance. In order to delete, instead of killing, just make sure the kill-ring isn't changed: (goto-char (point-min)) (let (kill-ring) (comment-kill (count-lines (...


11

The web-mode shortcut M-; (which calls web-mode-comment-or-uncomment) works for me in HTML, CSS, and PHP files. For the first, this command puts <!-- --> around the region, for the second, /* */, and so on. I also tried testing both comment-region and comment-dwim with web-mode enabled. Both functions had the same result: <!-- --> was placed ...


10

AFAIK the example you give are actually 2-char comment delimiters, except that depending on the 3rd character, these comments are treated in different ways (but always as comments in the sense that the semantics of the code is unaffected). So, the support offered by Emacs's syntax tables should be sufficient in most cirsumstances. If you want to highlight ...


10

Inside your hook put this: (setq comment-start "% "). Also, this will explain why: comment-region is an interactive compiled Lisp function. (comment-region BEG END &optional ARG) Comment or uncomment each line in the region. With just C-u prefix arg, uncomment each line in region BEG .. END. Numeric prefix ARG means use ARG comment ...


9

This is a bit disppointing, because ffap.el has some code that should do just that: ;; Immediate rejects (/ and // and /* are too common in C/C++): ((member name '("" "/" "//" "/*" ".")) nil) But unfortunately, it relies on there being a space after the comment separator. It's also very disappointing, because comment markers should never be part of ...


8

use the font-face, this is the trick I learned from flyspell. I tried syntax-ppss two years ago, it does not work for two reason: not work on edge of comment (comment limit), for example, for comment like // this is comment in c++-mode, if you place the cursor over the / character, the result of (nth 4 (syntax-ppss)) is nil. not work at all in major-mode ...


8

The package hide-comnt available from Melpa (now marmalade) can do it: M-x hide/show-comments-toggle


7

@Malabarba's answer looks like the simplest and most elegant solution. However, if you do this enough that it warrants its own function, you can also adapt comment-kill to delete without modifying the kill ring. Here's the source code of comment-kill with the single-line change to define comment-delete: (defun comment-delete (arg) "Delete the first ...


7

The below code works fine from my brief testing in a c-mode buffer: After typing /*, hit M-j, the default binding for indent-new-comment-line (and the default binding for c-indent-new-comment-line in c-mode). If it is the first comment line the closing closing characters */ will be auto-inserted. Hitting M-j more times with insert more comment lines with ...


7

You will have to change the wildcard match to a non-greedy version (+?,??,*?). Ref: Emacs Manual: Regexp In this case: (\\*[\0-\377[:nonascii:]]*?\\*) That tells it to find the shortest full match, rather than the longest.


7

I achieved what I wanted by overriding some definitions from the original major mode. In /lisp/progmodes/octave.el (the file was zipped, I had to unzip it to view the source), I redefined octave-indent-comment which originally goes like this: (defun octave-indent-comment () "A function for `smie-indent-functions' (which see)." (save-excursion (back-...


7

Based on a quick check of C-hig (emacs) Specifying File Variables, I'm reasonably sure that you can't. I think your options are: Move the comment outside of the local variables block. Change the variable (e.g. give it a prefix like DISABLED:) such that the value is simply assigned to a variable which nothing uses. Edit: If you don't want to have to ...


7

Customize option comment-empty-lines. comment-empty-lines is a variable defined in newcomment.el. Documentation: If nil, comment-region does not comment out empty lines. If t, it always comments out empty lines. If eol it only comments out empty lines if comments are terminated by the end of line (i.e. comment-end is empty).


6

Try using more than one % or # in a row: %% or %%% etc. By default, the number of consecutive comment chars determines the kind of comment and its behavior (e.g., with respect to indentation). See the Elisp manual, node Comment Tips.


6

I like orgstruct-mode. In the following file you can collapse headings with Tab or S-Tab, much like in org-mode. Note that the eval part below collapsed the file to out-most level when opening the file. ##* sec1 x <- rnorm(10) ##** sec11 y <- x<0 ##* sec 2 z <- x + y ## Local Variables: ## outline-regexp: "##*\\*+\\|\\`" ## orgstruct-heading-...


6

For the usual meaning of "comment", the answer is mostly no (see Dan answer for details), but if you just want to prevent code from being executed, then you can simply do nothing in your case, since the execution of "emacs" will not have any visible effect. If you want to use it on elements whose execution does have visible effects, you can use (defmacro ...


5

This is what I ended up going with: (defface special-comment '((t (:foreground "#2aa198"))) "Cyan") (font-lock-add-keywords 'clojure-mode '((";-;.*" 0 'special-comment t))) ";-;.*" is regex. 0 matches the entire expression. t overrides existing highlights. More info on font-lock-add-keywords can be found here: https://www.gnu.org/software/emacs/manual/...


5

Here's a quick command, lightly tested, that does what you're looking for. (defun eol-dwim () "Go to the end of the line, ignoring comments and trailing whitespace." (interactive) (let ((bol (line-beginning-position 1)) (eol (line-end-position 1))) (if (condition-case nil (comment-search-forward eol) (error nil)) ...


4

Thanks to the solution posted by @Sigma. I had that solution in my config for more than 2 years, and finally sent that as a patch to emacs upstream. Commit in emacs master: e472cfe8 Here is what the patch effectively does: (defun modi/ffap-string-at-point (&optional mode) "Return a string of characters from around point. MODE (defaults to value of `...


4

I came up with a minimal way of implementing this functionality: just bind space bar to also call (fill-paragraph)! (defun fill-then-insert-space () (interactive) (fill-paragraph) (insert " ")) (global-set-key (kbd "SPC") #'fill-then-insert-space) There are a couple of caveat's that I've stumbled across so far: elisp-mode (possibly others) does some ...


4

Yes. Use library Isearch+, specifically file isearch-prop.el. As it says here: You can search the text of THINGS of various kind (sexps, lists, defuns, lines, pages, sentences, filenames, strings, comments, xml/html elements, symbols,…), using command isearchp-thing. This is equivalent to using command isearchp-thing-define-contexts, which marks such ...


4

It looks like the answer is no, you cannot. The elisp manual node on Comments states that: In Lisp, a semicolon (;) starts a comment if it is not within a string or character constant. The comment continues to the end of line. It also, however, goes on to say that: The #@count construct, which skips the next count characters, is useful for ...


3

See Jonathan's answer for the easy solution. The hard solution would look something like: (\*\(?:[^*]\|\*+[^)*]\)*\*+)


3

I don't know which version of Emacs you're using, but there is (will be) C-x C-; in Emacs 25, it runs comment-line that seem to have the behavior you want.


3

You can use poporg for this. People mostly use it to edit comments in Org mode, but you can use whatever mode you want by setting poporg-edit-hook. (remove-hook 'poporg-edit-hook 'org-mode) (add-hook 'poporg-edit-hook 'markdown-mode)


3

The following is a partial answer that addresses the cosmetic issue (get the font-locking visuals correct), but it does not actually modify what org-mode considers to be a comment (e.g., for exporting purposes). Add a font lock keyword: (font-lock-add-keywords 'org-mode '(("^\\s-*#+\\s-?.*" . 'font-lock-comment-face)))


Only top voted, non community-wiki answers of a minimum length are eligible