Emacs modeline at the end of file in one line - emacs

I am working on a project where some people use vi, some use emacs and some others (including gedit). The most simple yet global way (although not perfect) to enforce (at least visual) style was to add the following lines to the end of each file:
...
return 0;
}
// Editor modelines - generated by http://www.wireshark.org/tools/modelines.html
// Local variables:
// c-basic-offset: 4
// tab-width: 4
// indent-tabs-mode: t
// truncate-lines: 1
// End:
// vim:set ft=cpp ts=4 sw=4 sts=4 nowrap: cindent:
the question is: how can I convert the emacs portion in a "one-line" code (as vim can)? and yet keep it at the end of the source file (not at the top).
(Probably this can be recasted as Lisp question but I am not familiar with it)

Directory Local Variables are probably a better approach.
http://www.emacswiki.org/emacs/DirectoryVariables
http://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html
The single .dir-locals.el file will be processed by everyone, and no need for file local variables at all.
vim may well have a similar mechanism?

You can use the eval: declaration, but Emacs will ask you to confirm that it is safe to evaluate. If you tell Emacs to accept it permanently, it won't ask about that expression again (it stores it in safe-local-variable-values in the custom-set-variables section of your init file).
;;; Local Variables:
;;; eval:(setq c-basic-offset 4 tab-width 4 indent-tabs-mode t truncate-lines 1)
;;; End:
You can wrap multiple expressions in progn:
;;; Local Variables:
;;; eval:(progn (setq c-basic-offset 4) (message "hello"))
;;; End:
Or use any other constructs (I don't think there are any restrictions).

You would need to use the // Local Variables: and // End:. The rest can be made into one line as in // eval: (setq c-basic-offset 4 tab-width 4 indent-tabs-mode t truncate-lines 1).

Looks like you might be SOL:
Specifying File Variables: http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html
Variables in Emacs:
http://www.gnu.org/software/emacs/manual/html_node/emacs/Variables.html#Variables
Unless you use Phils's eval style.

(setq c-basic-offset 4 tab-width 4 indent-tabs-mode t truncate-lines 1) add this to your .emacs. If you wish to do this for specific file types you can always use the add-hook call, such as (add-hook 'c-mode-common-hook 'your-func-here) where your-func-here could be a function that simply sets those variables.
Refer to phils method of using Local Variables and eval to accomplish this with the first line of code given above. Sorry I didn't quite understand that this was only for a single file or very few files.

I'm going to turn the question around? Do you really need any file-local variables? If you use the same style throughout all source files, it might be better to define a C indentation setup and distribute this among all developers.
(defconst my-c-style
'((c-basic-offset . 2)
(c-offsets-alist
. ((substatement-open . 0)
(statement-case-open . +)
(inline-open . 0)
(arglist-cont-nonempty . (c-indent-operator-lineup-arglist-operators
c-lineup-arglist)))))
"My indentation style")
(defun my-c-mode-common-hook ()
(interactive)
(c-add-style "my" my-c-style t))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

Related

Emacs. Spell check "on fly" for 2 languages

Windows 7, Emacs 25.1
I need to spell check "on fly" for my custom text (e.g. to emphasize the incorrect words). But I write text in two languages: English and Russian. And I want easy to switch between spell checking for 2 languages.
What is the best emacs package for this? Thanks.
You want this: guess_language.el
(use-package guess-language ; Automatically detect language for Flyspell
:ensure t
:defer t
:init (add-hook 'text-mode-hook #'guess-language-mode)
:config
(setq guess-language-langcodes '((en . ("en_GB" "English"))
(it . ("it_IT" "Italian")))
guess-language-languages '(en it)
guess-language-min-paragraph-length 45)
:diminish guess-language-mode)
Alternatively if you just want to cycle through them:
(defvar mu-languages-ring nil "Languages ring for Ispell")
(let ((languages '("en_GB" "it_IT")))
(validate-setq mu-languages-ring (make-ring (length languages)))
(dolist (elem languages) (ring-insert mu-languages-ring elem)))
(defun mu-cycle-ispell-languages ()
(interactive)
(let ((language (ring-ref mu-languages-ring -1)))
(ring-insert mu-languages-ring language)
(ispell-change-dictionary language)))
These should work with FlySpell
I had a similar problem, the solution I found manages two or more languages at the same time without using guess_language package. This solution is based on Hunspell spelling checker.
SYSTEM:
Windows 7 SP1, GNU Emacs 26.1
First, perform a Ispell/Hunspell configuration after Hunspell installation. Insert the next code in the .emacs file, usually located within C:/Users/Account.
;; START BLOCK1 <-- Ispell/Hunspell setting
;; e.g., "C:/Hunspell/bin/hunspell.exe"
(setq-default ispell-program-name "hunspell.exe FULL PATH")
(setq-default ispell-extra-args '("--sug-mode=ultra"))
;; Set "DICTDIR" variable, e.g., "C:/Hunspell/share/hunspell"
(setenv "DICTDIR" "hunspell DICTIONARY PATH")
;; Uncomment next line to set English or another dictionary
;; (setq ispell-dictionary "en_US")
;; Automatically enable flyspell-mode in text-mode
(setq text-mode-hook '(lambda() (flyspell-mode t) ))
(require 'ispell)
;; END BLOCK1 <-- Ispell/Hunspell setting
This code is based on the spelling configuration section of another discussion, see M Parashar - Load Theme. The block code 1 works for the default dictionary, which can be changed uncommenting the line with the ispell-dictionary variable. This code works perfectly for me.
The second code block enable us to use multiple dictionaries, is based on AM Lafon - Multi Spell Checking. For me, this code block works only next of code block 1.
;;START BLOCK2<-- Multiple dictionaries. Only works with BLOCK1
(with-eval-after-load "ispell"
;; Configure `LANG`, otherwise ispell.el cannot find a 'default
;; dictionary' even though multiple dictionaries will be configured
;; in next line.
(setenv "LANG" "es_ES")
;; English/spanish configuration.
(setq ispell-dictionary "en_US,es_ES")
;; ispell-set-spellchecker-params has to be called
;; before ispell-hunspell-add-multi-dic will work
(ispell-set-spellchecker-params)
(ispell-hunspell-add-multi-dic "en_US,es_ES")
;; For saving words to the personal dictionary, don't infer it from
;; the locale, otherwise it would save to ~/.hunspell_de_DE.
(setq ispell-personal-dictionary "~/.hunspell_personal"))
;; The personal dictionary file has to exist, otherwise hunspell will
;; silently not use it.
(unless (file-exists-p ispell-personal-dictionary)
(write-region "" nil ispell-personal-dictionary nil 0))
;;END BLOCK2<-- Multiple dictionaries. Only works with BLOCK1
These two code blocks enable the spell checking for the english/spanish languages at the same time. More languages can be added to the checking system expanding the "en_US,es_ES" strings with the appropriate dictionary names. I used that ones located in the Share/Hunspell directory. There is no keybinding to change between languages and no per file dictionary assignation at the beginning of files, like this:
# -\*- ispell-dictionary: "castellano8" -\*-.
A context menu is displayed when clicking the middle mouse button over the misspelled word, an option is the Save Word entry. These words will be saved using the variable ispell-personal-dictionary in the new file called .hunspell_personal, located on the usual path.
The inclusion of the two code blocks gave the expected results. Just including the second block of code threw the error 'wrong-type-argument stringp nil'.

How to set a column width in .h and .cpp files in emacs?

I want to have text-width to 80 columns for .h and .cpp files in emacs.
What I tried is following,
M-x auto-fill-mode
C-x f
But above solution is not working.
Also I tried the solution present at http://www.emacswiki.org/emacs/CPlusPlusMode. It is as below.
; style I want to use in c++ mode
(c-add-style "my-style"
'("stroustrup"
(indent-tabs-mode . nil) ; use spaces rather than tabs
(c-basic-offset . 4) ; indent by four spaces
(c-offsets-alist . ((inline-open . 0) ; custom indentation rules
(brace-list-open . 0)
(statement-case-open . +)))))
(defun my-c++-mode-hook ()
(c-set-style "my-style") ; use my-style defined above
(auto-fill-mode)
(c-toggle-auto-hungry-state 1))
(add-hook 'c++-mode-hook 'my-c++-mode-hook)**
Though, I did not completely understand the solution provided at emacswiki.
Appreciate any help in this regard.
Thanks.
You need to set fill-column only in those modes, which you can do by defining a function to do so and adding that hook to the relevant modes (you could also do it as a lambda, but it's usually good practice to put it in a named function so you can remove it from the hook later if you need to). *.h files come up in c-mode by default, while *.cpp files come up in c++-mode by default. First, define a function:
(defun my-c-hook ()
(setq fill-column 80))
Then, add that function to the relevant mode hooks:
(add-hook 'c-mode-hook 'my-c-hook)
(add-hook 'c++-mode-hook 'my-c-hook)
You can make your function as rich as you like, eg, by putting in the other items you had originally:
(defun my-c-hook ()
(c-set-style "my-style")
(auto-fill-mode)
(c-toggle-auto-hungry-state 1)
(setq fill-column 80))

How can I set Emacs tab settings by file type?

I need to be able to set the tab settings for the following file types:
.rb: 2 soft spaces
.css, .html.erb: 4 space tabs
I have tried the following, but none of it seems to alter my default tab settings for each of the file types.
;; js-mode-hook has also been tried
(add-hook 'javascript-mode-hook
'(lambda()
(setq tab-width 4)))
(add-hook 'css-mode-hook
'(lambda()
(setq tab-width 4)))
(add-hook 'html-mode-hook
'(lambda()
(setq tab-width 8)))
I am pretty new to emacs so my knowledge of configuration is pretty low.
In emacs each mode has it's own indentation style. The main command to indent (bound to TAB) is indent-for-tab-command.
This command calls mode specific indentation function found in the variable indent-line-function. So each mode has it's own way of doing it.
For Ruby (for my emacs 2 is a default):
(setq ruby-indent-level 2)
For CSS (again, default is 4 for me):
(setq css-indent-offset 4)
Unfortunately SGML mode (on which HTML mode is based) has a very simple indentation mechanism and apparently the level is not configurable.
See the source code of sgml-calculate-indent function.
I personally find it weird. I am not writing HTML, but you can try to modify the sgml-calculate-indent function yourself :). Learn some lisp.
I am using js2 mode, and it indents perfectly by default. For js you have to search for js-indent-level or something similar.
Cheers.
Theres a number of aspects to how Emacs does indentation. Setting the tab-width only specifics how big a tab is if a literal tab is inserted. If you don't wish to use literal tabs for indentation, then you should first disable their insertion (from the manual
):
Emacs normally uses both tabs and
spaces to indent lines. If you prefer,
all indentation can be made from
spaces only. To request this, set
indent-tabs-mode to nil. This is a
per-buffer variable, so altering the
variable affects only the current
buffer, but there is a default value
which you can change as well.which you can change as well.
However, to specify the indentation levels, you'll also need to set the c-basic-offset value variable as well:
(add-hook 'html-mode-hook
'(lambda()
(setq c-basic-offset 4)
(setq indent-tabs-mode nil))
In your case, you may only need the c-basic-offset but try a few combinations and see what works best.
js-mode uses js-indent-level so put (setq js-indent-level 4) into your ~/.emacs (shouldn't have to be in a hook, even, but if you're wondering, it's js-mode-hook, not javascript-mode-hook).
If setting tab-width doesn't change your indentation level for a certain mode, it's often simplest to just open the source for that mode. I found this variable by doing C-h f js-mode, clicking the link "js.el", then searching for "indent", second hit from the top.
However, if you collaborate a lot with other people, it's often better to put a cookie at the top of the file. I typically do // -*- tab-width: 8 -*- in the file, and then I have stuff like this in my ~/.emacs:
(defvaralias 'c-basic-offset 'tab-width)
(defvaralias 'cperl-indent-level 'tab-width)
(defvaralias 'perl-indent-level 'tab-width)
(defvaralias 'js-indent-level 'tab-width)
so that I have less variables to deal with (and don't have to get warnings about the file-local variable being unsafe or whatever if the mode-writer forgot to declare it as safe)
If you are using ELPA's css-mode.el with emacs 23.1.1, you can parametrize the global setting for tab width for CSS files for the tab width by doing the following:
1) Type M-x customize-variable ,
2) Then type css-indent-level,
3) Then after you change the variable to your liking, you do "Save for future sessions".
For HTML and erb: if you are using web-mode (the mode provided by Spacemacs) it can be as simple as:
(setq-default
web-mode-code-indent-offset 2
web-mode-markup-indent-offset 2)
where markup-indent-offset refers to the actual tags and code-indent-offset refers to embedded Ruby in ERB, etc.

Set python indent to 2 spaces in emacs 23?

I am using emacs 23.1.1 on Ubuntu 10.04. I wish to program in Python with a 2-space indent. emacs looks to have a default mode for python (python.el?).
I put the following in my .emacs:
;; Only spaces, no tabs
(setq indent-tabs-mode nil)
;; Always end a file with a newline
(setq require-final-newline nil)
;; Don't know which of these might work
(setq-default tab-width 2)
(setq-default python-indent 2)
(setq-default py-indent-offset 2)
When I edit a Python file, it uses a 4-space indent. When I try C-h v python-indent it says:
python-indent's value is 4
Local in buffer webpage_cache.py; global value is 2
This variable is safe as a file local variable if its value
satisfies the predicate `integerp'.
Documentation:
Number of columns for a unit of indentation in Python mode.
See also `M-x python-guess-indent'
You can customize this variable.
That is, it is 4, not 2. Grr. I tried customizing the variable and saving, still 4. I tried customize-group indent, still 4.
How do I get emacs to pay attention?
You can put this into your .emacs file:
(add-hook 'python-mode-hook '(lambda ()
(setq python-indent 2)))
The reason why
(setq-default python-indent 2)
does not work may because this variable does not exit when .emacs is loaded. (But I am an emacs newbie. I am not sure about my explanation.)
However, PEP 8 -- Style Guide for Python Code recommends "4 spaces per indentation level" and I find 4 spaces more readable. I actually use this piece of code to force a 4 spaces indentation.
Either in you .emacs file or in a file referenced by your .emacs add:
(setq-default indent-tabs-mode nil)
(setq-default tab-width 2)
You can put in a hook if you want to localize
;; Python Hook
(add-hook 'python-mode-hook
(function (lambda ()
(setq indent-tabs-mode nil
tab-width 2))))
EDIT: I have found the following the following issues can mess with my settings:
setting the variable before the library is loaded
other packages/configs resetting the global variables
For both those issues I have found creating hooks and localizing the variables can help.
I just ran into this problem myself, and I think the help for python-indent contains a big clue that no one else mentioned:
See also `M-x python-guess-indent'
If you don't customize python-guess-indent by setting it to nil, then python.el will automatically set python-indent for each Python buffer (that contains indented text), making your python-indent customization ineffective. (On the other hand, when in Rome...)
In the end, this is what went into my .emacs file (ignoring all other custom variables):
(custom-set-variables
'(python-guess-indent nil)
'(python-indent 2))

How to tell emacs to open .h file in C++ mode?

What lines should I add to my _emacs (on Windows) file to have it open .h files in C++ mode? The default is C mode.
Try this:
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
Whenever you open .h files, C++-mode will be used.
Another approach for using both c-mode and c++-mode as appropriate, is to use directory local variables to set the mode.
Directory variables are evaluated after the mode has been set1, so you can actually write a .dir-locals.el file for your C++ project containing this:
((c-mode . ((mode . c++))))
And Emacs will change the mode to c++-mode whenever it had initially set it to c-mode.
If you work with a mix of C and C++ projects, this makes for a pretty trivial solution on a per-project basis.
Of course, if the majority of your projects are C++, you might set c++-mode as the default2, and you could then use this approach in reverse to switch to c-mode where appropriate.
1 normal-mode calls (set-auto-mode) and (hack-local-variables) in that order. See also: How can I access directory-local variables in my major mode hooks?
2 To do so, add
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
to your .emacs file which open .h files in C++ mode by default.
If you don't want this to apply to every .h file, you can add the following to the bottom of your C++ header files.
// Local Variables:
// mode: c++
// End:
This will work for any Emacs variables that you want to set on a per file basis. Emacs ignores the leading characters, so use whatever comment characters are appropriate for the file type.
Apparently you can also put this at the top of the file:
// -*-c++-*-
to tell Emacs it's a C++ file.
I use this since I quite frequently end up on a vanilla Emacs and it works without configuring Emacs in any way.
Since I use both C and C++ regularly, I wrote this function to try and "guess" whether a .h file is meant to be C or C++
;; function decides whether .h file is C or C++ header, sets C++ by
;; default because there's more chance of there being a .h without a
;; .cc than a .h without a .c (ie. for C++ template files)
(defun c-c++-header ()
"sets either c-mode or c++-mode, whichever is appropriate for
header"
(interactive)
(let ((c-file (concat (substring (buffer-file-name) 0 -1) "c")))
(if (file-exists-p c-file)
(c-mode)
(c++-mode))))
(add-to-list 'auto-mode-alist '("\\.h\\'" . c-c++-header))
And if that doesn't work I set a key to toggle between C and C++ modes
;; and if that doesn't work, a function to toggle between c-mode and
;; c++-mode
(defun c-c++-toggle ()
"toggles between c-mode and c++-mode"
(interactive)
(cond ((string= major-mode "c-mode")
(c++-mode))
((string= major-mode "c++-mode")
(c-mode))))
It's not perfect, there might be a better heuristic for deciding whether a header is C or C++ but it works for me.
I could swear I saw this question answered appropriately already? Weird.
You want this:
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))