Change emacs comment markers from ;; to // - emacs

When I am using Emacs to edit a ".s" file, I want to change the comment from ";;" to "//". I can't seem to find out how to change the comment identifiers?
For example, when I comment-region.
More information:
I appear to be in ASM-MODE which is the default mode for editing assembler files. I made sure that I was in asm-mode by
(setq auto-mode-alist
(append '(("\\.s$" . asm-mode)auto-mode-alist))
Because assembly programs typically use ; as the comment indicator, asm-mode uses this. However, for some reason I cannot figure the GNU assembler (GNU Binutils for Raspbian) 2.35.2 uses // or # or # for comments NOT a ;
Therefore, I would like to change the behaviour such that when I select a region and M-X comment-region it uses either // or # for comments. I cannot use the default comment character, I need to change it to either double-slashes // or an at symbol #
The question is really how do I go about changing the default comment character for in a mode?

Assuming that the major mode of the .s file is asm-mode, you can use the mode hook to tweak the comment start string:
(defun my/asm-comment-tweak ()
(setq-local comment-start "// "))
(eval-after-load "asm"
(add-hook 'asm-mode-hook #'my/asm-comment-tweak))
Adding the above to your init file should let you open a .s file, which will be in asm-mode. The last thing that is done by asm-mode is to run the mode hook which will call the function my/asm-comment-tweak: the function will set the buffer-local variable comment-start to the string you specified.
This pattern is very common: many problems in customizing emacs are solved in exactly the same way. You define a function that tweaks a variable and you arrange for the function to be called by the appropriate mode hook.

Related

How to use the same file extension with different modes?

I have files with a .new extension that contain LaTeX code (they are the output of a program that does things on the original .tex source).
In order to check them I need that they have the LaTeX syntax highlighted and that they can be opened in read-only mode to avoid introducing errors. So I put these lines in my .emacs:
(setq auto-mode-alist
(append '(("\\.new\\'" . read-only-mode)
("\\.new\\'" . latex-mode))
auto-mode-alist))
But it does not work as expected because only the first mode is applied. How can I force emacs to apply both read-only-mode and latex-mode to the same file extension?
An easy solution is to associate your file extension with a custom derived major mode:
(define-derived-mode read-only-latex-mode latex-mode "LaTeX"
"Major mode for viewing files of input for LaTeX."
(read-only-mode 1))
(add-to-list 'auto-mode-alist '("\\.new\\'" . read-only-latex-mode))
The new read-only-latex-mode does everything that latex-mode does (including running latex-mode-hook if you're already using that), but it additionally enables read-only-mode after all of the parent mode's actions have taken place.
Any other code which cares about the major mode being latex-mode should already be testing that using the standard approach of (derived-mode-p 'latex-mode) which is still true for this new mode; so in principle this approach should Just Work.
Not sure I can really provide an answer, but here's some things to consider:
read-only-mode is a minor mode, while
auto-mode-alist is meant to turn on major modes. For major modes, it makes sense that you can have only one.
You can put something like -*- buffer-read-only: t; -*- on your first line in the file (possibly behind some comments or shebangs, depending on the file type) which would turn on read-only-mode. Maybe automate this using auto-insert-alist and match the .new filename pattern.
There's some good suggestions over at https://www.reddit.com/r/emacs/comments/jqxgiz/how_can_i_make_some_files_automatically_readonly/ , namely try file local variables and use find-file-hook
Hope some of that helps.

Set emacs comment style based on file extension

There are multiple questions along these lines but I haven't been able to find what I need from start to finish. I have been using emacs for a few years but am not used to its customization.
I have a unique file type, identified by its extension, which emacs is not configured for. Its comment style is
<!-- text -->
and I would like to set the variables comment-start and comment-end to the relevant values (which I am assuming will allow me to use comment-region). I don't know the correct way to do this so that it will always be configured when I open this file type but won't affect the default behavior of emacs.
Do I need to make a new major mode for this file type, and then set the variables, or is there an easier way of doing it? An example of the complete requirements for my .emacs file would be much appreciated!
See here. I think this will work:
(add-to-list 'auto-mode-alist
'("\\.extension\\'" . (lambda ()
(setq-local comment-start "<!--")
(setq-local comment-end "-->"))))
Alternatively, if this file extension is well known (or if these files are close enough to a well known syntax), you may be able to just find a major-mode online that does what you want. For example, NXML Mode may just give you the comment syntax you want along with some other useful features.

Emacs org-mode tags not found

I'm learning to use emacs and org-mode. I create a few tags, in a .org file, as such :outline: lets say.
And then searched for them using:
C-c a m outline
C-c a t outline
C-c \ outline
And the output is always (basically, did't find anything):
Headlines with TAGS match: outline
Press `C-u r' to search again with new search string
What am I doing wrong. Can someone please tell me what I'm missing?
Thanks in advance.
Common problems when initially setting up org-mode include, but are not limited to, properly configuring the org-agenda-files variable. A user may choose to have one or more files, or a directory.
Here is an example of multiple files:
(setq org-agenda-files
(list "~/org/gtd.org" "~/org/work.org" "~/org/personal.org"))
Here is an example of a directory:
(setq org-agenda-files (list "~/"))
(setq org-agenda-file-regexp "\\`[^.].*\\.org\\|.todo\\'")
It is interesting to note that there is also non-interactive function with the same name, which looks up the configuration of the org-agenda-files variable -- the function is what org-mode generally relies upon when any other function looks up the value of the variable. To see an example of how that non-interactive function works, the user can do something like this:
M-x eval-expression RET (org-agenda-files) RET
The importance of setting the org-agenda-files variable can be seen by examining the function org-match-sparse-tree, which in turn calls org-scan-tags using org-make-tags-matcher, which uses org-global-tags-completion-table, which uses the function org-agenda-files, which uses the variable org-agenda-files. If the variable org-agenda-files is not set up correctly, a tags search and auto-completion of tags will not work correctly.
Another common issue arises when the variable org-tag-alist has not yet been properly set up -- here is a link to the manual page on that issue: http://www.orgmode.org/manual/Setting-tags.html

outline-minor-mode and org-mode

In emacs, org-mode has been enabled and have opened some *.org files. Now in the init.el file i am trying to enable the outline-minor-mode in order to fold the lines starting with ";" . When i try to evaluate the (outline-minor-mode) command i get the message "Symbol's function definition is void: th-outline-minor-mode-init"
Seems like the org-mode sets up the outline-minor-mode-hook
outline-minor-mode-hook's value is (th-outline-minor-mode-init)
This variable may be risky if used as a file-local variable.
How to setup the outline-minor-mode for init.el file so that the ";" is treated as heading.
Note:- When emacs is launched with --no-init-file option, outline-minor-mode works for init.el file
Doesn't org-mode use an org- prefix pretty consistently?
On that assumption, th-outline-minor-mode-init doesn't look like an org-mode function to me, so something else is probably at fault, and you'll need to find out what.
Obviously you can't find-function if it's void, so I'd just M-x rgrep your config for th-outline-minor-mode-init (or most likely searching for (add-hook 'outline-minor-mode-hook 'th-outline-minor-mode-init will find the culprit directly).
That issue aside, I happen to use outline-minor-mode in my init file (with headers being lines beginning with ;;;;, and auto-folding the ones beginning with ;;;; *), using the following Local Variables block at the end of the file:
;;; Local Variables:
;;; outline-regexp: ";;;; "
;;; eval:(progn (outline-minor-mode 1) (while (re-search-forward "^;;;; \\* " nil t) (outline-toggle-children)))
;;; End:
I keep a lot of documentation in the file, and with <backtab> bound to outline-toggle-children I find this a pretty convenient way to access it.

how to change variables for specific fundamental-mode buffers

Goal: I want to have show-trailing-whitespace enabled for all buffers save a few. Exceptions posing a problem are *Shell Command Output* and its cousin *Async Shell Command*.
I usually have show-trailing-whitespace customized to t. Therefore it is active in all new buffers.
I would also like to have it turned off for certain buffers, foremost amongst them *Shell Command Output*. This poses a problem for me:
The output buffer doesn't use a special mode; it is still in fundamental-mode. There is no fundamental-mode-hook that I could hook this setting into.
There is the after-major-mode-change-hook which is run when the major mode is changed to fundamental-mode, but the buffer starts out in that mode and therefore this hook is not run.
There doesn't seem to be a way to hook into get-buffer-create.
I know I can always advise the function get-buffer-create for this particular example, but I try to avoid that as much as possible.
Any hints?
You might be better off looking at the problem from the other side, and only set the var in those modes where you want to see trailing whitespace.
But I think you have a good point: these shell output buffers should not use fundamental-mode. It's probably time for M-x report-emacs-bug
In accordance with the accepted answer, here's a code snippet that enables trailing whitespaces highlighting for specific modes only:
(setq-default show-trailing-whitespace nil)
(defun namespace/show-trailing-whitespace ()
"Highlight trailing whitespaces in this buffer."
(setq-local show-trailing-whitespace t))
(dolist (hook '(prog-mode-hook text-mode-hook))
(add-hook hook 'namespace/show-trailing-whitespace))
This snippet is essentially taken from Steve Purcell's configuration.