emacs indentation inconsistent in nesed loops - emacs

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)

Related

Apply a Built-in Style to a file with Emacs

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

How to delete trailing whitespace in Verilog Mode Emacs

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.

lexical-binding not being enabled when evaluating .emacs

Here are some excerpts from my .emacs:
(setq lexical-binding t)
;; .emacs
(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.
'(diff-switches "-u")
'(tab-always-indent (quote complete)))
;...
(require 'dired)
;...
(dotimes (i 12) (define-key dired-mode-map (kbd (concat "<f" (number-to-string (1+ i)) ">"))
(lambda ()
(interactive)
(goto-char (point-min))
(forward-line (+ 4 i)))))
This should bind keys f1 to f12 in dired-mode to commands that jump to particular files in the list of files (ignoring . and ..). However, initially, after starting emacs, these keys don't work - I get an error message forward-line: Symbol's value as variable is void: i. However, when I go to the top line of my .emacs and press C-x C-e to evaluate that line, and then go to the last line quoted above and press C-x C-e to evaluate that dotimes expression, those function keys start working!
Why is that?
By the way, it also doesn't work if I evaluate the whole buffer.
It turns out that it is necessary to replace
(setq lexical-binding t)
with
;; -*- lexical-binding: t -*-
The manual hints suggestively at this, but does not actually say so outright.

Emacs: highlight 2 or more empty lines

I have entered the following code in my .emacs file to highlight unwanted white spaces.
(require 'whitespace)
(setq whitespace-style '(face empty tabs lines-tail trailing))
(global-whitespace-mode t)
This shows (1) empty lines at the beginning & end of buffer
(2) tabs
(3) lines which go over the 80 character limit
(4) trailing white spaces
I would like emacs to automatically highlight '2 or more empty lines'. Any ideas on how to implement this? I did find a blog post explaining a way to do this with the help of regexp, but I am not sure how to implement this in .emacs file.
Edit 1: Found a way to delete extra blank lines but this still doesn't help me with highlighting multiple blank lines automatically. delete extra blank lines in emacs
Edit 2: Adding the following to .emacs seems to work, but only after I save and reopen file in a buffer.
(add-hook 'change-major-mode-hook '(lambda () (highlight-regexp "\\(^\\s-*$\\)\n" 'hi-yellow)))
Edit 3: After adding (global-hi-lock-mode 1) to .emacs file just before the line in Edit 2, it seems to highlight 1 or more empty lines within the buffer. I am not sure how to modify the regexp so that it will only accept 2 or more empty lines.
Just use library Highlight (highlight.el). That's what it's for.
Use command hlt-highlight-regexp-region (C-x X h x) or hlt-highlight-regexp-to-end (C-x X h e). (To unhighlight a regexp, use C-x X u x or C-x X u e.)
Interactively, you input the regexp to use as usual in Emacs (with C-q C-j to match a newline character, and no need for double backslashes), so you type \(^\s-*$\) C-q C-j.
Your highlight-regexp-solution can be made into a minor-mode with the following elisp (e.g., in your .emacs file).
You can activate the minor mode by right-clicking onto one of the mode-names in the mode-line and then selecting nl2-mode. You can deactivate the minor mode by clicking on nl2 in the mode line and selecting Turn off minor mode.
To understand the code see the help for define-minor-mode and define-key (e.g., C-h f define-minor-mode RET). Note, that in emacs also mouse clicks in menus count as key strokes.
(define-minor-mode nl2-mode
"Highlight two successive newlines."
:global t
:lighter " nl2"
(if nl2-mode
(highlight-regexp "\\(^\\s-*$\\)\n" 'hi-yellow)
(unhighlight-regexp "\\(^\\s-*$\\)\n")))
(define-key mode-line-mode-menu [nl2-mode]
`(menu-item ,(purecopy "nl2-mode") nl2-mode
:help "Highlight two succesive newlines."
:button (:toggle . (bound-and-true-p nl2-mode))))
There are several facts that make highlighting two consecutive empty lines more complicated (font-lock tends to only highlight non-empty regions, linebreaks are limits for the region to re-fontify, re-fontification after buffer changes are required).
The following code shows one way. Maybe, there are easier ways.
(require 'font-lock)
(global-font-lock-mode)
(defface jit-lock-nl2-face '((default :background "yellow"))
"Face to indicate two or more successive newlines."
:group 'jit-lock)
(defun jit-nl2-extend (start end &optional old)
"Extend region to be re-fontified"
(save-excursion
(save-match-data
;; trailing:
(goto-char end)
(skip-chars-forward "[[:blank:]]\n")
(setq jit-lock-end (point))
;; leading:
(goto-char start)
(beginning-of-line)
(skip-chars-backward "[[:blank:]]\n")
(unless (bolp) (forward-line))
(setq jit-lock-start (point)))))
(defun jit-nl2 (jit-lock-start jit-lock-end)
"Highlight two or more successive newlines."
(save-excursion
(save-match-data
(jit-nl2-extend jit-lock-start jit-lock-end)
;; cleanup
(remove-text-properties jit-lock-start jit-lock-end '(font-lock-face jit-lock-nl2-face))
;; highlight
(while (< (point) jit-lock-end)
(if (looking-at "[[:blank:]]*\n\\([[:blank:]]*\n\\)+")
(progn (put-text-property (match-beginning 0) (match-end 0) 'font-lock-face 'jit-lock-nl2-face)
(goto-char (match-end 0)))
(forward-line))))))
(add-hook 'after-change-major-mode-hook (lambda ()
(add-hook 'jit-lock-after-change-extend-region-functions 'jit-nl2-extend)
(jit-lock-register 'jit-nl2)
(jit-lock-mode 1)
))

Emacs: Tab completion of file name appends an extra i:\cygwin

I am facing some strange behavior with file-name completion in emacs. C-x C-f to find file opens up the minibuffer with i:/cygwin/home/rrajagop/StockScreener/working_copy/master_repo/stock_screener/. Hitting a TAB makes it i:/cygwini:/cygwin/home/rrajagop/StockScreener/working_copy/master_repo/stock_screener/. A couple of interesting things I've noticed:
When the minibuffer opens up, i:/cygwin is greyed out and the path seems to start from /home. A C-a (go to begining of line) takes me to /home and not to i:/cygwin. So it looks like something in emacs is parsing the path to start from /home and not from i:/cygwin.
I checked that TAB runs minibuffer-complete from minibuffer.el (by doing a describe-key for TAB), so it looks like minibuffer-complete is doing some translation for cygwin and appending the extra i:/cygwin.
How would I go about figuring this out/fixing it?
EDIT: Extra Information
I tried opening up emacs with -Q and this problem doesn't happen. So this is something I'm loading in my .emacs. This is what I have in my .emacs
(require 'cl)
; Needed to see how fast Emacs loads. Loading time is printed at the
; and of the execution of .emacs file.
(defvar *emacs-load-start* (current-time))
; I really like this font. I also tried Monaco which you can
; see on lot of Railscasts but I couldn't find the one which
; supports Serbian Cyrillic and Latin letters.
(set-default-font "-outline-Courier New-normal-r-normal-normal-19-142-96-96-c-*-iso8859-1")
;; Don't show that splash screen
(setq inhibit-startup-message t)
; This should allegedly speed up Emacs starting by preventing
; some requests from the window manager back to the Emacs. Frankly
; speaking I didn't notice some speed up but I still keep it:(
(modify-frame-parameters nil '((wait-for-wm . nil)))
;Allows syntax highlighting to work, among other things
(global-font-lock-mode 1)
; Sets initial window position
(set-frame-position (selected-frame) 0 0)
; Sets initial window size to 85 columns and 47 rows
(set-frame-size (selected-frame) 88 32)
; Makes last line ends in carriage return
(setq requre-final-newline t)
; Sets Ctrl-x / key combination for easy commenting
; out of selected lines.
(global-set-key "\C-x/" 'comment-or-uncomment-region)
; Allow resizing of the mini-buffer when necessary
(setq resize-minibuffer-mode t)
; Auto magically read compressed files
(auto-compression-mode 1)
; Set standard indent to 2 rather then 4
(setq standard-indent 2)
; This tells Emacs to create backup files.
(setq make-backup-files t)
; And this will enable versioning with default values.
(setq version-control t)
; Remove annoying message about deleting excess backup of .recentf
; which is list of recent files used
(setq delete-old-versions t)
; Finally do not spread backups all over the disk.
; Just save all backup files in this directory.
(setq backup-directory-alist (quote ((".*" . "~/.emacs_backups/"))))
;; Directory to put various el files.
(add-to-list 'load-path "~/.emacs.d/includes")
(require 'ascii-table)
;; Loading collection of generic modes for different languages
(require 'generic-x)
;; Recent files
(require 'recentf)
(recentf-mode 1)
;; Loads ruby mode when a ruby file is opened.
(autoload 'ruby-mode "ruby-mode" "Major mode for editing ruby scripts." t)
(setq auto-mode-alist (cons '(".rb$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist (cons '(".rhtml$" . html-mode) auto-mode-alist))
(setq auto-mode-alist (cons '(".html.erb$" . html-mode) auto-mode-alist))
;; Turn on ruby electric (auto completion of parenthesis, etc.)
(add-hook 'ruby-mode-hook
(lambda()
(add-hook 'local-write-file-hooks
'(lambda()
(save-excursion
(untabify (point-min) (point-max))
(delete-trailing-whitespace) )))
(set (make-local-variable 'indent-tabs-mode) 'nil)
(set (make-local-variable 'tab-width) 2)
(imenu-add-to-menubar "IMENU")
(define-key ruby-mode-map "\C-m" 'newline-and-indent)
(require 'ruby-electric)
(ruby-electric-mode t) ))
;; Ruby debugging.
(add-to-list 'load-path "~/.emacs.d/plugins/rdebug")
(autoload 'rdebug "rdebug" "Ruby debugging support." t)
(global-set-key [f9] 'gud-step)
(global-set-key [f10] 'gud-next)
(global-set-key [f11] 'gud-cont)
(global-set-key "\C-c\C-d" 'rdebug)
;; set compile command based on current major mode
(autoload 'mode-compile "mode-compile"
"Command to compile current buffer file based on the major mode" t)
(global-set-key "\C-cc" 'mode-compile)
(autoload 'mode-compile-kill "mode-compile"
"Command to kill a compilation launched by `mode-compile'" t)
(global-set-key "\C-ck" 'mode-compile-kill)
;; yasnippet - adding code snippet insertion
(add-to-list 'load-path "~/.emacs.d/plugins/yasnippet")
(require 'yasnippet) ;; not yasnippet-bundle
(yas/initialize)
(yas/load-directory "~/.emacs.d/plugins/yasnippet/snippets")
;; Use CYGWIN bash
(require 'setup-cygwin)
;; Subversion integration via psvn - not gonna use svn anymore
;; (require 'psvn)
;; add some elisp tutorials to the info directory
(let ((info-root (concat usb-drive-letter "cygwin/usr/local/bin/emacs/info/")))
(setq Info-directory-list (list info-root
(concat info-root "elisp-tutorial-2.04/")
(concat info-root "emacs-lisp-intro-2.14")) )
)
;; Load time for .emacs - this should be the last line in .emacs for accurate load time
(message "ido and org-install took: %ds"
(destructuring-bind (hi lo ms) (current-time)
(- (+ hi lo) (+ (first *emacs-load-start*) (second *emacs-load-start*)) )))
I think my answer to your previous question on finding the package loading tramp will help you out here.
you can control tramp by changing the variable tramp-mode.
Side note, you would probably find it useful to use customize to customize emacs.
I did a customize-apropos with tramp and it found the tramp group. Clicking there showed all the ways to configure tramp, including turning it off.
File-name-shadow-mode greys out the c: in the file name..... so when cygwin-mount-substitute-longest-mount-name runs it does no see the c: and adds another
M-x find-file
c:/home/
> a
c:/home/a ; but the c: is greyed
> TAB
c:c:/home/anything