How to use emacs prettify-symbols-mode? - emacs

I have emacs version 25.0, I enable prettify-symbols-mode, and type (lambda () t) but it doesn't prettify. How do I use this mode? Also what symbols are available and how can I configure it? Any references are appreciated.
Edit: Nothing happened in scratch buffer and Markdown mode, but when I tried in a Emacs-lisp mode, It did prettify, but now I got a question mark instead of the lambda symbol, how do I fix that?
Edit: I asked the related question here.
Edit: This SO question solved the unicode problem.

prettify-symbols-mode is buffer-local. If you want to enable it globally, use global-prettify-symbols-mode.
The question mark you are seeing is probably because Emacs can't find a font that contains a lambda character. Try switching to a font with decent Unicode support like DejaVu Sans Mono.
I believe that only Lambda prettifies out of the box, and only in emacs-lisp-mode buffers. Check the value of prettify-symbols-alist from a buffer with prettify-symbols enabled to see the current replacements table.
If you wish to add prettification of other symbols you can do something like this, from C-h f prettify-symbols-mode RET:
(add-hook 'emacs-lisp-mode-hook
(lambda ()
(push '("<=" . ?≤) prettify-symbols-alist)))

Related

Disabling 'linum-mode' for speedbar when global 'linum-mode' is active

I have linum-mode enabled globally in my Emacs configuration. Being enabled globally means it is also applied to the speedbar which is undesirable.
The only suggestion I found for this issue, was in the archived Emacs help mailing list, which suggests the following speedbar-mode-hook:
(add-hook 'speedbar-mode-hook (lambda () (linum-mode -1)))
Unfortunately adding this to my configuration doesn't have the desired effect and the speedbar still has line numbers.
Edit: the above add-hook seems to work correctly after all, at least for Emacs >= 24.3. Leaving question for reference purposes since there isn't any other relating to this matter on StackOverflow.
You can use speedbar-before-popup-hook hook for achieving what you want:
(add-hook 'speedbar-before-popup-hook (lambda () (linum-mode -1)))
I am not sure why the generic mode hook is not working, though.

Line numbers only in certain modes

Is there a way to get line numbering only in specific modes, i.e Python, Java, C++, etc., rather than in every window? I thought I saw something about this the other day, but I can't find it.
Currently I'm set up that the default is just to have linum-mode on persistenly. It's kind of annoying in the shell.
Thanks.
Assuming you want to use it all major programming modes and you're using Emacs 24.x, you can use this snippet:
(add-hook 'prog-mode-hook 'linum-mode)
I didn't turn linum on globally.
Instead I turn linum on for the major mode in which I want it. For example:
(add-hook 'clojure-mode-hook '(lambda () (linum-on)))
or simply:
(add-hook 'clojure-mode-hook 'linum-mode)
Related here:
Turning on linum-mode when in python/c mode
and here:
Emacs Org-Mode: Turn off line numbers

What happened to the ido-imenu in ruby-mode function in Emacs24?

Emacs 23.2 in emacs-starter-kit v1 has C-x C-i (or ido-imenu) (similar to Sublime Text's Cmd+R). Emacs24 in emacs-starter-kit v2 lacks this function. I found this github issue and a fix, which try to recreate the functionality. While this ido-imenu works in elisp-mode, it stopped working in ruby-mode. I get:
imenu--make-index-alist: No items suitable for an index found in this buffer
Has anyone figured out how to get this to work?
Why was this taken out of Emacs24?
Is there a new replacement for this function?
Since the function is part of ESK (as opposed to something budled with Emacs) you'd probably do best to report the bug upstream. On a related note ESK main competitor Emacs Prelude offers the same functionality (bound to C-c i by default) and it seems to be working fine with ruby-mode in Emacs 24. Here you can find more on ido-imenu.
So I finally figured it out, after reading the Defining an Imenu Menu for a Mode section on emacs-wiki again.
Short answer: you need to add this bit to your customization. Feel free to add more types to the list (I am happy with just methods).
(add-hook 'ruby-mode-hook
(lambda ()
(set (make-local-variable imenu-generic-expression)
'(("Methods" "^\\( *\\(def\\) +.+\\)" 1)
))))
Longer answer: I first tried to define a ruby-imenu-generic-expression function and set that to imenu-generic-expression by using the ruby-mode-hook:
(defvar ruby-imenu-generic-expression
'(("Methods" "^\\( *\\(def\\) +.+\\)" 1))
"The imenu regex to parse an outline of the ruby file")
(defun ruby-set-imenu-generic-expression ()
(make-local-variable 'imenu-generic-expression)
(make-local-variable 'imenu-create-index-function)
(setq imenu-create-index-function 'imenu-default-create-index-function)
(setq imenu-generic-expression ruby-imenu-generic-expression))
(add-hook 'ruby-mode-hook 'ruby-set-imenu-generic-expression)
This however did not work (I would get the same error as before). More reading of the Defining an Imenu Menu for a Mode section showed me the way. Now, I'm not an elisp expert, so here's my hypothesis: basically, the above method works for modes where the
major mode supports a buffer local copy of the “real” variable, ‘imenu-generic-expression’. If your mode doesn’t do it, you will have to rely on a hook.
The example for foo-mode made it clear how to do it for ruby-mode. So it appears that ruby-mode does not have a buffer-local copy of the real imenu-generic-expression variable. I still can't explain why it worked in Emacs 23.2 (with ESK v1) but does not on Emacs24, but hey at least I found a working solution.

Syntax highlight a vimrc file in Emacs?

So, this might be a heretical question, but I'm looking for an Emacs mode that handles syntax highlighting of .vimrc files. This particular question has proved pretty hard to Google for the obvious reasons, but it seems extremely likely to me that someone would have written such a mode in the 20+ years of open warfare between the two editors. Any ideas?
Googling does turn up wenbinye's vimrc-mode, a very lightweight generic mode. Here's what I have in my .emacs:
(define-generic-mode 'vimrc-generic-mode
'()
'()
'(("^[\t ]*:?\\(!\\|ab\\|map\\|unmap\\)[^\r\n\"]*\"[^\r\n\"]*\\(\"[^\r\n\"]*\"[^\r\n\"]*\\)*$"
(0 font-lock-warning-face))
("\\(^\\|[\t ]\\)\\(\".*\\)$"
(2 font-lock-comment-face))
("\"\\([^\n\r\"\\]\\|\\.\\)*\""
(0 font-lock-string-face)))
'("/vimrc\\'" "\\.vim\\(rc\\)?\\'")
'((lambda ()
(modify-syntax-entry ?\" ".")))
"Generic mode for Vim configuration files.")
There is a great package for this: https://github.com/mcandre/vimrc-mode
Install by M-x package-install vimrc-mode.
It highlights vimrc files with amazing syntax highlighting automatically when they are opened, or you can invoke it manually via (vimrc-mode).

How to accomplish equivalent of Vim's Ctrl-n in GNU Emacs?

Vim's Ctrl+N generally works like this: I type few letters, hit Ctrl+N, and Vim provides me with completions based on words in my all opened buffers.
Solution for Emacs doesn't have to be identical. I mainly use it like this: declare variable, then use it in later code. But I like the lightweight approach of not parsing the source code.
You want dabbrev-expand, bound to M-/ by default. I haven't used Vim, but from your description, it does the exact same thing.
try hippie-expand, bound to your favorite key
(global-set-key (kbd "M-/") 'hippie-expand)
Instead of presenting a completion-list, repeatedly hitting the bound-key cycles through the completions in-place.
Why "hippie"-expand? I have no idea, and I actually avoided looking at the function because the name was uninformative and off-putting, until I read the write-up at 'Life Is Too Short For Bad Code'. (The EmacsWiki entry on hippie-expand also asks "why 'hippie?'" but can't answer it, either.)
I personally use AutoComplete It gives you a nice dropdown box. You can select how many letters you want to type before it activates and customise what you want to show up, including stuff in dabbrev-expand.
;; Allow tab to autocomplete
(defun indent-or-expand (arg)
"Either indent according to mode, or expand the word preceding point."
(interactive "*P")
(if (and
(or (bobp) (= ?w (char-syntax (char-before))))
(or (eobp) (not (= ?w (char-syntax (char-after))))))
(dabbrev-expand arg)
(tab-to-tab-stop)))
(defun my-tab-fix ()
(local-set-key [tab] 'indent-or-expand))
(add-hook 'as-mode-hook 'my-tab-fix)
(add-hook 'java-mode-hook 'my-tab-fix)
(add-hook 'c-mode-hook 'my-tab-fix)
(add-hook 'sh-mode-hook 'my-tab-fix)
(add-hook 'emacs-lisp-mode-hook 'my-tab-fix)
The matter, in my opinion is that emacs completion I tryed doesn't complete regarding the context.
For instance, if you write some OOP with a method foobar() and an argument foo, M-/ will suggest you both foo and foobar.
But it would be great if you are calling an object method, not to provide just "foo" completion.
Has anyone a solution?
Aif> This requires much more than what "hippie expand" has to offer. If you code C/C++ you COULD use ECB http://ecb.sourceforge.net/ but frankly, the project is quite dead and this addon is not very reliable. If you need really good intelligent completion you should try Eclipse (CDT). But if you code python then Emacs (rope + flymake) is just as fine as Eclipse (PyDev).