How to highlight code parts that are longer than 80 chars? - emacs

In Emacs, I'd like to highlight the parts of long lines that exceed 80 characters.
The package highlight-80+ is great for that. But how can I automatically enable it when a C++ source file is loaded?
I tried to add highlight-80+ to the C++ mode, but it didn't work:
(require 'highlight-80+)
(defun my-c++-mode-common-hook ()
(highlight-80+-mode 1))
(add-hook 'c++-mode-common-hook 'my-c++-mode-common-hook)
When I load a .cc file it goes in C++ mode but highlight-80+ is not enabled, so the long lines are not marked.

Note that the Highlight80Plus wiki says that it is built-in to emacs starting with 23. I believe it's referring to whitespace-mode; it does this and is built in to emacs.
There is a function in emacs-starter-kit that does something like this already but you could easily duplicate it,
(defun esk-turn-on-whitespace ()
(whitespace-mode t))
(add-hook 'prog-mode-hook 'esk-turn-on-whitespace)

See the whitespace-mode, it does this kind of highlighting and more:
http://www.emacswiki.org/emacs/WhiteSpace

Can you try this:
(autoload 'highlight-80+)
(add-to-list 'auto-mode-alist '("\\.cpp$" . highlight-80+-mode))

Related

Running a .el file when opening emacs

I downloaded two .el files
One is to highlight current column where cursor is and another one to highlight some specific words.
I followed next steps in the file:
(add-to-list 'load-path "~/.xemacs/packages/") //path where I saved .el files
(load "column-marker") //name of file without .el extension
To this step it works fine, I find those when I press M-x column-marker and I can use it.
My problem begins when I want to use it everytime i open emacs and I found that I could use something similar to this:
(add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
Which in my case I am typing:
(add-to-list 'auto-mode-alist '("\\.\\'" . column-marker)) //I want to enable it for all .something files
But... when doing that, there is an error when opening emacs and it starts with default setup.
What am I doing wrong? I have tried many ways and none work.
Thanks in advance
Always post the error message you see. That helps others help you.
C-h f auto-mode-alist tells you that the functions you use in it must implement major modes. column-marker is not a major-mode function. It is not even a function; it is a file.
What you need to do, for each mode where you want some function in file column-marker.el to be invoked, is to put that function on the major-mode hook for that function. For example:
(add-hook 'emacs-lisp-mode (lambda () (interactive) (column-marker-1 80))
And lo and behold, what does the Commentary in file column-marker.el tell you?
;; Installation: Place this file on your load path, and put this in
;; your init file (`.emacs'):
;;
;; (require 'column-marker)
;;
;; Other init file suggestions (examples):
;;
;; ;; Highlight column 80 in foo mode.
;; (add-hook 'foo-mode-hook (lambda () (interactive) (column-marker-1 80)))
Couldn't be clearer. Provided you actually read it.
Seriously, a minimum of investigation is in order, before you post a question to StackOverflow. You should do a Google search, open README files and read them, and so on --- first. And in the case of Emacs questions, IMHO, you should ask Emacs first (e.g. C-h v auto-mode-alist).
According to the rules of S.O. posting, not doing preliminary simple research is even grounds for closing a question. So do not be surprised if this question gets closed or downvoted.

Emacs and vala-mode

I am using vala-mode to edit Vala code in Emacs. However, I want to change two things in vala-mode:
I want to indent with 4 spaces instead of 2 spaces (which is my Emacs default).
I want to enable auto-completion inside vala-mode.
Auto-completion works in all modes except for vala-mode, and I want the 4 spaces indentation only for vala-mode, not all modes. However, I don't know how to make these changes only for vala-mode.
Thank you.
Something like this should work:
(add-hook 'vala-mode-hook (lambda () (setq c-basic-offset 4)))
I have never used vala-mode, but it looks like it is based on cc-mode so that setting c-basic-offset might work. For info on how to set c-basic-offset in a style, see the documentation at
(info "(ccmode)Customizing Indentation")
I saw that the indentation issue was fixed but not the auto complete feature. If you are using the auto complete package, then in your init.el or .emacs you can simply put:
(require 'auto-complete-config)
(add-to-list 'ac-modes 'vala-mode)
You will then have auto complete locally. Thats one way of doing it. Also there is a Yasnippet package for vala that is available in MELPA (https://github.com/gopar/vala-snippets)

EMACS folding/unfolding R code

Is there an Emacs minor-mode (or piece of elisp code) that lets you selectively hide/show environments while in Sweave (Sweave = R + LaTeX)?
For instance, I would like to move to the beginning of a code block (<<>>), hit a keystroke, and have the contents of the environment hidden from view.
Is this possible? I just tried hs-minor-mode, allout-mode, and outline-minor-mode, but most of them don't recognize R environments.
I also tryed org-mode that works great for folding/unfolding but, do not support the LaTeX highlighting code for expression like: \cite{}; \ref{}; \ce{} ...
Best
Riccardo
--EDIT--
I tried for some day to use emacs-folding-mode but, because I work on a very long code (more than 2000 rows), folding mode "goes crazy" and for example misunderstand the mining of some special character (i.e. $), that has very different use both in R than LaTeX. I think the problem is intrinsic to Sweave, because in the same buffer I have R code and LaTeX code together.
So, now I'm testing emacs outline minor mode. But when I move through R from LaTeX (and vice versa) all the outlined part were unfolded despite I write in my .emacs:
(defun turn-on-outline-minor-mode ()
(outline-minor-mode 1))
(add-hook 'ess-mode-hook 'turn-on-outline-minor-mode)
(add-hook 'LaTeX-mode-hook 'turn-on-outline-minor-mode)
(add-hook 'latex-mode-hook 'turn-on-outline-minor-mode)
(setq outline-minor-mode-prefix "\C-c\C-o")
Do you have any suggestions??
Regards
--EDIT 2--
It seems to work:
(load "folding" 'nomessage 'noerror)
(folding-mode-add-find-file-hook)
(add-hook 'LaTeX-mode-hook 'folding-mode)
(add-hook 'ess-mode-hook 'folding-mode)
(folding-add-to-marks-list 'ess-mode "#{{{ " "#}}}" " ")
I don't know if is right that, when you leave the chunk, it is automatically unfolded.
There's a generic folding mode here: http://www.emacswiki.org/emacs/FoldingMode
I have had very good results with the hideshow hs-minor-mode, these are the lines I basically use in my ~/.emacs.d/init.el:
(add-hook 'ess-mode-hook 'hs-minor-mode)
(eval-after-load 'hideshow
'(progn
(global-set-key (kbd "C-+") 'hs-toggle-hiding)))

Auto-complete mode doesn't turn on automatically in ObjC buffers

I load auto-complete mode like this:
(let ((ac-path "path/to/auto-complete"))
(add-to-list 'load-path ac-path)
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories (concat ac-path "ac-dict"))
(ac-config-default))
It works fine with C major mode, but doesn't turn on automatically when I open ObjC files. I can still turn it on manually and it will work fine along with the ObjC major mode.
Here's a snippet from the docs regarding ObjC major mode:
The hook `c-mode-common-hook' is run with no args
at mode initialization, then `objc-mode-hook'.
If I understand correctly, auto-complete adds a hook to the c-mode-common-hook, but objc-mode-hook somehow overrides it. Is there a way to fix this?
Thanks.
While looking through the source code of auto-complete.el, I've stumbled upon this definition
(defcustom ac-modes
'(emacs-lisp-mode
lisp-interaction-mode
c-mode cc-mode c++-mode
java-mode clojure-mode scala-mode
scheme-mode
ocaml-mode tuareg-mode
perl-mode cperl-mode python-mode ruby-mode
ecmascript-mode javascript-mode js-mode js2-mode php-mode css-mode
makefile-mode sh-mode fortran-mode f90-mode ada-mode
xml-mode sgml-mode)
"Major modes `auto-complete-mode' can run on."
:type '(repeat symbol)
:group 'auto-complete)
It turns out that auto-complete doesn't have a true global mode. It is enabled only with those major modes that are included in the ac-modes variable.
So, adding the following line to the .emacs file has solved the issue for me.
; add this line after the auto-complete mode has been loaded
(add-to-list 'ac-modes 'objc-mode)
Use the following:
(defun my-objc-mode-hook ()
(auto-complete-mode 1))
(add-hook 'objc-mode-hook 'my-objc-mode-hook)
Note 1: The function auto-complete-mode is a toggle function, when called with no arguments.
Note 2: It's possible to add an anonymous function using lambda, but this have several drawbacks. The most important ones are: modifying the function and reevaluating the expression will add the modified function in addition to the earlier version and C-h v xxx will print the full unformatted lambda function, which typically is hopeless to read and understand.
(add-hook 'objc-mode-hook 'auto-complete-mode)
That should do it if you're using auto-complete-mode. You can add more complex things to mode hooks by doing:
(add-hook 'objc-mode-hook '(lambda ()
(something-with arguments)))
Note that both arguments to add-hook are quoted, this is necessary and if you add unquoted functions they will probably not work.

smart-operator.el not autoloading ... what gives?

I'm trying to use smart-operator.el in emacs. I've put the following into my init.el file:
(add-to-list 'load-path "~/.emacs.d/dotemacs_git/smart-operator/")
(require 'smart-operator)
(smart-operator-mode 1)
That doesn't seem to turn on smart-operator-mode automatically ... I still have to do
M-x smart-operator-mode
to get it working. What am I doing wrong? This setup here uses smart-operator-mode within a python-mode-hook; I don't see why making the function call conditional on python-mode would matter, though...
Thanks,
Mike
The link you provide shows folks using
(smart-operator-mode-on)
Also, they're adding that call in a hook, like so:
(add-hook 'python-mode-hook
(lambda ()
(smart-operator-mode-on)))
Which will turn it on for all buffers using python-mode. You'll need that since it appears that smart-operator-mode is not a global minor mode.