I've a c file written like this:
int main(void){
}
I would like to change every bracket in the file according to the "linux" format.
int main(void)
{
}
How can I do this with Emacs?
EDIT 1:
This is my .emacs file:
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(ansi-color-faces-vector
[default default default italic underline success warning error])
'(custom-enabled-themes (quote (tango-dark)))
'(inhibit-startup-screen t)
'(package-archives
(quote
(("melpa" . "https://melpa.org/packages/")
("gnu" . "http://elpa.gnu.org/packages/")))))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
(setq make-backup-files nil) ; stop creating backup~ files
(setq auto-save-default nil) ; stop creating #autosave# files
(setq c-default-style "linux"
c-basic-offset 4)
EDIT 2:
Solved by creating an emacs macro that calls GNU indent.
Primary source of how to do this is the emacs wiki page. To use linux style in emacs:
(setq c-default-style "linux")
put the above line in your ~/.emacs or ~/.config/emacs/config if you have xdg paths defined for emacs.
CtrlM\ will reindent an arbitrary region, so after you set the c-default-style to linux simply select the content of the buffer and re-indent. More on this at CC-Mode manual
Related
I am a beginner in Emacs. I am trying to use trident-mode (for Parenscript). I have copied the commands from the trident-mode site trident-site-here into my init.el file. But the trident-mode keybindings C-c C-e don't work. I am copy-pasting my complete init.el file below:
;; Added by Package.el. This must come before configurations of
;; installed packages. Don't delete this line. If you don't want it,
;; just comment it out by adding a semicolon to the start of the line.
;; You may delete these explanatory comments.
;;;; Added from Melpa.org
(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
(not (gnutls-available-p))))
(proto (if no-ssl "http" "https")))
(when no-ssl (warn "\
Your version of Emacs does not support SSL connections,
which is unsafe because it allows man-in-the-middle attacks.
There are two things you can do about this warning:
1. Install an Emacs version that does support SSL and be safe.
2. Remove this warning from your init file so you won't see it again."))
(add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t)
;; Comment/uncomment this line to enable MELPA Stable if desired. See `package-archive-priorities`
;; and `package-pinned-packages`. Most users will not need or want to do this.
;;(add-to-list 'package-archives (cons "melpa-stable" (concat proto "://stable.melpa.org/packages/")) t)
)
; Commented coz it is there in the next line (package-initialize)
;;;; End Melpa.ord addition
(package-initialize)
(load (expand-file-name "~/quicklisp/slime-helper.el"))
;; Replace "sbcl" with the path to your implementation
(setq inferior-lisp-program "sbcl")
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(package-selected-packages (quote (trident-mode))))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(default ((t (:family "DejaVu Sans Mono" :foundry "PfEd" :slant normal :weight normal :height 158 :width normal)))))
;;; From Trident-model.el site on Github
(add-to-list 'auto-mode-alist (cons "\\.paren\\'" 'lisp-mode))
(add-hook 'lisp-mode-hook
#'(lambda ()
(when (and buffer-file-name
(string-match-p "\\.paren\\>" buffer-file-name))
(unless (slime-connected-p)
(save-excursion (slime)))
(trident-mode +1))))
;;;; From same site, key bindings
(trident-add-keys-with-prefix "C-c C-e")
;; The key sequence for trident-eval-region is "e r", so it's now bound to "C-c C-e er"
**This is What is Not getting Activated**
One thing that I observe if I open a .paren file is that, repl buffer gets to top, and file buffer (where .paren file is shown) gets below. I don't know how to fix that. But I did interchange them by clicking on the buffer names to change. I don't know if it is relevant, but I am writing it just in case that is the culprit. If possible I would prefer regular (program text on top and repl buffer below) setting.
What am I doing wrong? Thanks for the help.
Edit-1: I have added link to the trident-mode-site.
Thanks Phils Phils' answer worked.
I am posting the change I made:
Earlier I was using:
(trident-add-keys-with-prefix "C-c C-e")
This was not working. With Phil's suggestion, I replaced that line with
(with-eval-after-load "trident-mode" (trident-add-keys-with-prefix "C-c C-e"))
This Worked.
I am a newbie, so I have detailed a seemingly simple/obvious answer. This is to help other newbies like me. We can get confused about the smallest of things. :-)
Phils, thank you again.
When I edit a file with emacs, the configuration stored in the .emacs file is read (I can judge this by the font size). However, not a second passes and both the window and font sizes decrease. It seems that my custom configuration is being overridden. I don't know where this additional configuration is stored.
System: Light Ubuntu 18.04
My .emacs file:
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(setq tab-stop-list t))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(default ((t (:family "DejaVu Sans Mono" :foundry "PfEd" :slant normal :weight normal :height 143 :width normal)))))
; disable the wellcome screen
(setq inhibit-startup-screen t)
;; rebind C-x C-b to invoke buffer-menu rather than list-buffers
(global-set-key "\C-x\C-b" 'buffer-menu)
; open list of buffers in active window
(global-set-key "\C-x\C-b" 'buffer-menu)
; Directory to place the backup files
(setq backup-directory-alist `(("." . "~/.emacs.d/backup_files")))
; Disable Large file size warning
(setq large-file-warning-threshold nil)
;; show line numbers
(global-linum-mode 1)
Following #prosoitos advice from the comment's section in the original question:
created $HOME/.Xresouces file containing the following line
emacs*font: DejaVu Sans Mono 16
in the terminal, type xrdb $HOME/.Xresouces
I'm trying to delete trailing whitespaces in Verilog Mode. I have customized Verilog mode with the menu : "Verilog --> Customize Verilog Mode...".
Trailing whitespaces are not removing when editing or saving a Verilog file. I've tried somme commands 'add hook' but still no success.
What am I missing ?
That's my .emacs (Emacs 24.3.1) :
`(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(cua-mode t nil (cua-base))
'(global-whitespace-mode t)
'(indent-tabs-mode nil)
'(show-paren-mode t)
'(verilog-align-ifelse t)
'(verilog-auto-delete-trailing-whitespace t)
'(verilog-auto-endcomments nil)
'(verilog-auto-hook (quote (delete-trailing-whitespace)))
'(verilog-auto-indent-on-newline t)
'(verilog-auto-lineup (quote declarations))
'(verilog-auto-newline nil)
'(verilog-indent-level 2)
'(verilog-indent-level-behavioral 2)
'(verilog-indent-level-declaration 2)
'(verilog-indent-level-directive 2)
'(verilog-indent-level-module 2)
'(verilog-indent-lists nil)
'(verilog-tab-always-indent t))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)`
In general, not just for Verilog, I use the below clean up before save:
;; Before save
;; (add-hook 'before-save-hook 'delete-trailing-whitespace)
(add-hook 'before-save-hook 'whitespace-cleanup)
You can try enabling 'delete-trailing-whitespace only, if the 'whitespace-cleanup does too much.
I am working on C code.
I have a problem same as that in this question.
The solution there is to use spaces instead of tabs. But I'm trying/I'd prefer to use Smart Tabs Mode.
I've tried enabling and disabling both "c-tab-always-indent" and "indent-tabs-mode" (in cc-mode-hook).
The problem I see is that in some parts of the code a nested loop generates two Tabs, as expected(?). But in some cases, it generates only one tab and four spaces, while in other cases it generates only one tab, something like below:
function_name
open brace here
...code indented by 4 spaces (though I want a tab)>
open_brace
_tab_ code under condition
_4spc_ close_brace
some more 4 space aligned code
_tab_ open_brace
_tab+4spc_ code under some block
_tab_ close_brace
some more 4 space aligned code
_tab_ open_brace
_2 tabs_ code aligned as I prefer with two tabs
_tab_ close_brace
Can someone help me with getting the last style in the whole code?
My .emacs file is here (I use a few packages, like cscope and hide-show, but I don't think that should cause problems):
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(ansi-color-faces-vector [default default default italic underline success warning error])
'(ansi-color-names-vector ["black" "red3" "ForestGreen" "yellow3" "blue" "magenta3" "DeepSkyBlue" "gray50"])
'(c-basic-offset 4)
'(c-cleanup-list (quote (scope-operator space-before-funcall compact-empty-funcall)))
'(c-default-style (quote ((c-mode . "linux") (c++-mode . "linux") (java-mode . "java") (awk-mode . "awk") (other . "gnu"))))
'(c-hanging-braces-alist (quote set-from-style))
; '(c-tab-always-indent t)
'(custom-enabled-themes (quote (tango-dark)))
'(save-place t nil (saveplace))
'(show-paren-mode t))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
(setq savehist-additional-variables ;; also save...
'(search-ring regexp-search-ring) ;; ... my search entries
savehist-file "~/.emacs.d/savehist") ;; keep my home clean
(defalias 'yes-or-no-p 'y-or-n-p) ; to answer y or n instead of yes or no :-P ...I'm to lazy
(setq search-highlight t ;; highlight when searching...
query-replace-highlight 1) ;; ...and replacing
(add-to-list 'load-path "~/.emacs.d/packages/")
(add-to-list 'load-path "~/share/emacs/site-lisp")
;;;add packages
;;(require 'doxymacs)
;;; turn ons
(ido-mode)
(savehist-mode 1)
(setq show-paren-mode 1)
(global-linum-mode 1)
(setq column-number-mode 1)
;;cc-mode changes
(require 'smart-tabs-mode)
(require 'xcscope)
(cscope-setup)
(autoload 'smart-tabs-mode "smart-tabs-mode"
"Intelligently indent with tabs, align with spaces!")
(autoload 'smart-tabs-mode-enable "smart-tabs-mode")
(autoload 'smart-tabs-advice "smart-tabs-mode")
(autoload 'smart-tabs-insinuate "smart-tabs-mode")
(defun my-c-mode-common-hook ()
(setq require-trailing-newline 1) ;; Always add a final newline
(which-function-mode 1)
(subword-mode 1)
(hs-minor-mode 1)
(setq show-paren-style 'parenthesis)
; (setq indent-tabs-mode t)
(smart-tabs-mode 1)
)
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
I have a TODO file that I load emacs up to use 90% of the time. When I load emacs though it defaults to loading the scratch buffer. I would like it to load the TODO file initially. I'm very new to Emacs and have tried searching round for ways to do this using the .emacs file but nothing has worked so far.
Here are my attempts:
1: Use find-file to get the file and switch-to-buffer to load it to the screen
(switch-to-buffer (find-file "c:/Users/Seb/Documents/Emacs/TODO_List.org"))
2: Use pop-to-buffer to load the file instead
(pop-to-buffer (find-file "c:/Users/Seb/Documents/Emacs/TODO_List.org"))
3: Save the desktop so it loads the next time
(desktop-save-mode 1)
None of these are working.
Here is my full .emacs file, as you can see it's barely used!
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
; '(inhibit-startup-buffer-menu t)
'(inhibit-startup-screen t)
'(initial-buffer-choice t))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
;; Set the current directory to the Emacs Documents dir
(cd "C:/Users/Seb/Documents/Emacs")
;; Open TODO list on start up
(pop-to-buffer (find-file "c:/Users/Seb/Documents/Emacs/TODO_List.org"))
;; Turn off the annoying tool bar at startup - to turn back on
;; just type M-x tool-bar-mode
(tool-bar-mode -1)
;; Move the mouse when cursor is near
(mouse-avoidance-mode 'cat-and-mouse)
;; This enables saving the current desktop on shutdown.
(desktop-save-mode 1)
;; XML Pretty Print
(defun xml-pretty-print (begin end)
"Pretty format XML markup in region. You need to have nxml-mode
http://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do
this. The function inserts linebreaks to separate tags that have
nothing but whitespace between them. It then indents the markup
by using nxml's indentation rules."
(interactive "r")
(save-excursion
(nxml-mode)
(goto-char begin)
(while (search-forward-regexp "\>[ \\t]*\<" nil t)
(backward-char) (insert "\n"))
(indent-region begin end))
(message "Ah, much better!"))
In your startup file, you have this line:
'(initial-buffer-choice t))
as part of your "custom-set-variables" command. The documentation string for "initial-buffer-choice" is:
Buffer to show after starting Emacs. If the value is nil and
inhibit-startup-screen' is nil, show the startup screen. If the
value is string, visit the specified file or directory using
find-file'. If t, open the `scratch' buffer.
So, the value that you've specified ('t') is causing the *scratch* buffer to be displayed after startup. Change this line to the following and your issue should be resolved:
'(initial-buffer-choice "c:/Users/Seb/Documents/Emacs/TODO_List.org"))