How do I make the entry in the mode-line popup a minor mode menu when clicked?
For reference see this discussion https://github.com/flycheck/flycheck/issues/365#issuecomment-38386558
It seems that the menu displayed on clicking the mode-line entry for a mode is the same menu that is displayed when one clicks on the mode's entry in the menu-bar, provided that the mode defines a top level menu entry. The simplest way would be define a top level menu like so
(easy-menu-define flycheck-menu flycheck-mode-map "Flycheck menu"
'("Flycheck"
["Check current buffer" flycheck-buffer t]
["Clear errors in buffer" flycheck-clear t]
["Compile current buffer" flycheck-compile t]
"---"
["Go to next error" flycheck-next-error t]
["Go to previous error" flycheck-previous-error t]
["Show all errors" flycheck-list-errors t]
["Google messages at point" flycheck-google-messages t]
"---"
["Select syntax checker" flycheck-select-checker t]
"---"
["Describe syntax checker" flycheck-describe-checker t]
["Read the Flycheck manual" flycheck-info t]))
This might not be a solution if you do not want to introduce another menu-bar item. Looking at the function minor-mode-menu-from-indicator in mouse.el it seems that it looks up for keybindings starting with [menu-bar] for getting the mode-line menu, that might also be interesting for you.
Note that you can supply any valid mode-line construct as the STRING / lighter value for a given mode's VARIABLE in minor-mode-alist.
So although this value is commonly just the name of the mode (or some appropriate abbreviation thereof), you can do more elaborate things such as including set text properties -- which can include a local-map property to specify a keymap for mouse clicks (see C-hig (elisp) Properties in Mode RET).
You might look at M-x find-variable RET mode-line-modes RET for an example (n.b. IIRC this variable is only in Emacs 24, where the complexity of the mode-line-format variable was broken out into independent sub-variables, to make the overall structure easier to understand/modify).
See C-hig (elisp) Mode Line Format RET for the full documentation.
Related
I tried Prelude - WikEmacs,
Every time I open emacs, there is a menu bar at the top:
File Edit Options Buffers Tools Emacs-Lisp Prelude Projectile Help
How can I remove it or prevent it from being shown?
You can disable the menu bar by turning off minor mode menu-bar-mode. C-h f menu-bar-mode tells you this:
menu-bar-mode is an interactive compiled Lisp function in
menu-bar.el.
(menu-bar-mode &optional ARG)
Toggle display of a menu bar on each frame (Menu Bar mode).
With a prefix argument ARG, enable Menu Bar mode if ARG is
positive, and disable it otherwise. If called from Lisp, also
enable Menu Bar mode if ARG is omitted or nil.
This command applies to all frames that exist and frames to be
created in the future.
So to turn it off using Lisp, for example in your init file (~/.emacs), you can do this:
(menu-bar-mode -1)
That description of turning the mode on/off interactively and from Lisp is general for minor modes.
Unfortunately, that doc string does not tell you that menu-bar-mode is a minor mode or that minor modes generally follow the same rules for turning them on/off. But if you click the link in that *Help* output to go to the definition of menu-bar-mode in menu-bar.el then you'll see that it's defined using macro define-minor-mode.
And C-h f define-minor-mode gives you general information about turning on/off a minor mode.
I am trying to get emacs to highlight trailing-spaces. I have tried using WhiteSpace, and also tried setting show-trailing-whitespace variable to true, but in each case it changes the representation of newline and space characters to $ and · characters, as shown in this screen capture.
Ideally I would like to just see the trailing whitespace highlighted in red without any such characters.
Disclaimer: I'm new to emacs, so this may well be obvious.
I don't use any library. I just set show-trailing-whitespace to t and any trailing white space is shown in red. The representation of newline and space characters is NOT changed.
Actually, my ".emacs" file contains this simple line:
(setq-default show-trailing-whitespace t)
In case you don't want to edit your ".emacs" file, you may try:
C-h v show-trailing-whitespace RET then click the customize link
(or just M-x customize-variable RET show-trailing-whitespace RET)
Click the toggle button to set it to on (non-nil)
Click the menu button State > Set for Current Session
Click the menu button State > Save for Future Sessions
[EDIT] (thanks to Francesco Frassinelli for his comment)
With setq-default, the value is changed for every mode.
If you want to disable it for some mode (like term-mode for example), you have to:
find the mode name of the current buffer. Usually you can get it from within the buffer with M-x describe-mode RET (shortcut C-h m or <f1> m).
find the entry "hook" for this mode. Usually, it's the mode name with the suffix -hook. You can find it by searching "hook" in the buffer describing the mode. For example, you may read:
Entry to this mode runs the hooks on ‘term-mode-hook’
add the following to your ".emacs" file:
(add-hook 'term-mode-hook (lambda () (setq show-trailing-whitespace nil)))
or you may try:
M-x customize-variable RET term-mode-hook RET
Click the INS button
Paste (lambda () (setq show-trailing-whitespace nil))
Click the menu button State > Set for Current Session
Click the menu button State > Save for Future Sessions
Note that show-trailing-whitespace automatically becomes buffer-local when set with setq.
Change the value of the whitespace-style variable to
(face trailing)
You might need to restart whitespace-mode for the changes to take effect.
To set a variable, use M-xset-variableEnter.
Another answer is to use library highlight-chars.el (description: Highlight library).
Command hc-toggle-highlight-trailing-whitespace does what you request.
You can also turn on such highlighting automatically, either everywhere or in a given buffer or for a given mode.
When I press ctrl+left-mouse-button in Emacs, I get the mouse buffer menu. This is my favourite way of switching buffers, but the list of buffers doesn't have to be too long before it re-organises the list into sub menus (fundamental, LISP, others etc...). I really hate this because I find it much harder to find the buffer I'm looking for.
My question is: How can I set the number of items in the mouse buffer menu that emacs will show before it breaks the menu into submenus? (I want to increase it, obviously!)
The following two variables give you some control over this:
mouse-buffer-menu-maxlen
mouse-buffer-menu-mode-mult
My interpretation is that the latter is the maximum number of buffers in a given major mode before that mode gets its own sub-menu, and the former is the maximum number of buffers allowed in any sub/menu before it is split into multiple menus.
setq as appropriate, or
M-x customize-group RET mouse RET
full code with details to add to .emacs file is below
also note that mouse-buffer-menu-mode-mult takes precedence
to evaluate the below and see effect immediately, highlight and type M-x eval-region or put cursor inside each () and type M-C-x
;; "ctrl - left click" buffer menu: increase number of items shown
;; set max length of this list. default 20. see next.
(setq mouse-buffer-menu-maxlen 30)
;; set # buffer in a mode before grouping begins. takes precedence over previous
;; set to 1 to always group by mode. default 4
(setq mouse-buffer-menu-mode-mult 8)
Question:
How do I create custom keybindings for minor modes? Something like this.
Here is what I have so far. I'm trying to get just one custom keybinding to work:
(define-minor-mode num-mode
"Toggle the Num pad keys.
With no argument, this command toggles the mode.
Non-null prefix argument turns on the mode.
Null prefix argument turns off the mode.
When Num mode is enabled, the num pad inserts the
keys you see on the pad. This may over ried existing
definitions, and is probably only usefule if you aref
running Emacs through a terminal."
;; The initial value.
nil
;; The indicator for the mode line.
" NumPad"
;; The minor mode bindings.
;; This doesn't work right now!!!!
'(((kbd "<kp-1>") . "a"))
:global 1
)
When I hit "1" on the num pad after calling my custom minor mode, "num-mode" and verifying it is on in the mini buffer, I get the error <kp-1> is undefined. What I want to happen is a is printed out where the pointer is when I hit <kp-1>. (just a test)
Context:
So, I usually use my num pad to move between buffers (the arrow keys move me the appropriate directions). This is set globally. I want to create a minor mode that I can call when I want to use my num-pad to simply enter numbers.
By default, the keys on my num-pad are undefined. I use <kp-0> to <kp-9> to define the keybindings for the numpad keys.
I can create a minor mode that I can call, but I can't attach any keybindings. This is true for all keybindings, including one not defined anywhere else.
Thanks for any help!
Short Answer
Problem line:
'(((kbd "<kp-1>") . "a"))
Solution (force the evaluation of the macro):
;; Single quote changed to back-quote and added a comma
`((,(kbd "<kp-1>") . "a"))
Long Answer
The define-minor-mode macro allows you to create minor modes relatively easily (as far as Emacs) goes.
First I'll show how it's done, then I'll explain how it works:
In general form:
(define-minor-mode NAME-mode
"DOCUMENTATION"
INIT-VALUE
"LIGHTER"
;; keymap
'(
(KEY-SEQUENCE . DEFINITION)
(KEY-SEQUENCE . DEFINITION)
... ETC ...
)
;; other options
:KEYWORD-ARG VALUE
:KEYWORD-ARG VALUE
... ETC ...
)
Specific example with forced evaluation of macros in the alist:
;; Custom Minor Mode
(define-minor-mode custom-mode
"Doc description, yada yada yada."
;; The initial value - Set to 1 to enable by default
nil
;; The indicator for the mode line.
" CustomMode"
;; The minor mode keymap
`(
(,(kbd "C-c C-a") . some-command)
(,(kbd "C-c C-b") . other-command)
("\C-c\C-c" . "This works too")
)
;; Make mode global rather than buffer local
:global 1
)
An alternative way, if you wish to use a variable for the keymap is to define the keymap variable and the keymap before the minor mode declaration something like this:
(defvar custom-mode-keymap (make-keymap) "num-mode keymap.")
(define-key custom-mode-keymap (kbd "C-c C-a") 'some-command)
And then, in your minor mode definition, simple list the variable name for your keymap, instead of the alist
(define-key custom-mode-keymap (kbd "C-c C-b") 'other-command)
;; Num pad enable
(define-minor-mode custom-mode
...
;; The minor mode bindings.
custom-mode-keymap
How it all works
From top to bottom, right after define-minor-mode we define a command name to toggle the minor mode. custom-mode in this case (M-x custom-mode to toggle the mode). This also defines a variable of the same name.
Right after the command name, we list the documentation string for the minor mode in quotes. This can pretty much be as long as you want.
Next we have several choices. The simplest choice is to simply list three things and then any additional options. The three things have to be listed in the order below. These three things are:
The initilization value for the minor mode variable. nil will have the mode off by default. Something other than nil will have it on by default.
The lighter. The lighter is simply what is display in the mode line at the bottom when your minor mode is on. This should be short, and it'll often help, in terms of formatting, to start it with a space.
The keymap. The keymap can either be defined as a variable or an alist (association list). Since using an alist is simpler and shorter, that's what I used in the example. The alist should be in the form (key-sequence . definition).
If you define the keymap as an alist, there's a few things to watch out for, especially if you're used to defining global keybindings. First, command names are not quoted. Second, if you want to use a macro, you have to force it to evaluate (and on SO). This is done with a combination of the back-quote (not single quote) and comma. You can see how this is done in the example with the kbd macro. I also included a keystroke definition if you don't use the kbd macro. If you simply quote a string in your keymap, that'll print out when the defined key combination is pressed (just like for defining global key bindings).
The keymap will not work with kbd macros if you do not force the evaluation of the kbd macros with a combination of the back quote and comma. Like this:
`((,(kbd "C-c C-a") . some-command))
Finally, you can add additional options using keyword-args of the form :blah. In the example I used :global. We could have defined the entire mode with keyword-args, but it's shorter to just list the init value, lighter, and keymap in the right order.
You need to have code like this in your minor mode definition:
(defvar your-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map parent-mode-shared-map)
(define-key map "\C-c\C-a" 'some-defun)
(define-key map "\C-c\C-b" 'some-other-sexp)
map)
(use-local-map your-mode-map)
You could have a look at the many modes available in EmacsWiki for reference.
Have a look at this (just for information about keybindings):
http://www.gnu.org/software/emacs/manual/html_node/emacs/Key-Bindings.html#Key-Bindings
http://www.gnu.org/software/emacs/manual/html_node/emacs/Local-Keymaps.html#Local-Keymaps
From Link:
Well written major modes will run hook at the end. So, you can use a hook to define your keybinding. Minor modes usually do not have hooks. In that case, you can call “(require ‹minor mode feature symbol›)” first, then define your keybinding.
Also you might have a look at:
http://www.cs.utah.edu/dept/old/texinfo/emacs19/emacs_35.html#SEC347
Maybe the accepted answer here also helps you.
Notepad++ has a convenient feature: if you select a word in your text (not necessarily a keyword), the word is highlighted throughout the text. Can this be done in Emacs as well? And if so, how?
It doesn't necessarily have to work exactly like Notepad++ (i.e., via selection); ideally, I would like to set up a key binding that causes all occurrences of the word under cursor to be highlighted.
It would be great if the highlights were permanent, i.e., moving point away from a highlighted word should not cause the highlight to be removed.
Also, it would be useful if there was a solution that made it possible to navigate between highlights (using custom key bindings).
The hi-lock suggestions are good. I think it's easier to use the M-x versions, though:
M-x highlight-regexp RET <REGEXP>
M-x highlight-phrase RET <REGEXP>
highlight-phrase is just a bit of sugar around highlight-regexp that ignores case and translates a space in the regex to match arbitrary whitespace. Handy.
Maybe highlight-symbol.el at http://nschum.de/src/emacs/highlight-symbol/ is what you are looking for:
Type C-s, then type the current word or type C-w. As a bonus, you can now hit C-s again to search for the word.
This is called incremental search.
What I use is idle-highlight
http://www.emacswiki.org/emacs/IdleHighlight
M-x idle-highlight sets an idle timer that highlights all occurences in the buffer of the word under the point.
To enable it for all programming modes, in ~/.emacs.d/init.el:
;; highlight words
(add-hook 'prog-mode-hook (lambda () (idle-highlight-mode t)))
Light-symbol will highlight whatever symbol point is over.
Alternately, you can use occur, which lists all lines matching a regexp. It's useful to quickly see all functions in a class.
Nobody mentioned symbol-overlay mode. It's basically a better rewrite of highlight-symbol-mode. "Better" as in, lacks bugs of original highlight-symbol (such as, temporary highlight getting stuck, or the temporary highlight disappearing for moving inside the highlighted word; or not being able to highlight symbols like *), better integrated, and maintained. See "Advantages" paragraph of its README.
You can install it as usual, with M-xpackage-install (make sure to update package list beforehand with package-list-packages). For reference, at the bottom I attached code I use to enable the mode and disable a few of the more advanced features which you may or may not want.
Notepad++ has a convenient feature: if you select a word in your text (not necessarily a keyword), the word is highlighted throughout the text. Can this be done in Emacs as well? And if so, how?
Once you enable overlay-symbol, occurrences on the screen will be shown for every word that you put cursor upon after a timeout (timeout by default is 0.5s, can be configured with symbol-overlay-idle-time variable). If a word don't get highlighted, this means there's just one match on the screen (the one you put cursor upon), hence there's no need to highlight it.
It would be great if the highlights were permanent, i.e., moving point away from a highlighted word should not cause the highlight to be removed.
To highlight the word under cursor permanently there's a function symbol-overlay-put. To unhighlight call it once again.
In my config example it's bound to Logo+` key.
(require 'symbol-overlay)
(defun enable-symbol-overlay-mode ()
(unless (or (minibufferp)
(derived-mode-p 'magit-mode)
(derived-mode-p 'xref--xref-buffer-mode))
(symbol-overlay-mode t)))
(define-global-minor-mode global-symbol-overlay-mode ;; name of the new global mode
symbol-overlay-mode ;; name of the minor mode
enable-symbol-overlay-mode)
(global-symbol-overlay-mode) ;; enable it
(global-set-key (kbd "s-`") 'symbol-overlay-put)
(setq symbol-overlay-ignore-functions nil) ;; don't ignore keywords in various languages
(setq symbol-overlay-map (make-sparse-keymap)) ;; disable special cmds on overlays
This may not be as nice as what you were hoping but if you put
(global-hi-lock-mode 1)
in your .emacs file then you can type C-x w h REGEX <RET> <RET> to highlight all occurances of REGEX, and C-x w r REGEX <RET> to unhighlight them again. Again, not as elegant as you'd probably like, but it'll work.
Try http://www.emacswiki.org/emacs/msearch.el
All occurences of the text selected with the cursor are highlighted.
You have to drag over the string which you want to highlight. That enables you to easily change the selection without changing the highlight.
If you want to preserve the highlighting of a string you can freeze it.
You can enslave a buffer to another buffer. Text selected in the master buffer will also be highlighted in the slave buffer. That is useful for comparing buffers.
It is also useful for taking notes in one buffer while you investigate the text in another one. You can have a collection of keywords in the notes buffer. Drag over such a keyword and its occurences in the investigated text will be highlighted.
I am using this stuff for years now. I added the freezing quite recently. So, maybe something is broken. If this is the case leave me a note on http://www.emacswiki.org/emacs/msearch or here.
Check Interactive Highlighting
Should be:
C-x w h word <RET> <RET>
Try iedit. It highlights the word at point and lets you edit all occurrences of it easily. With an additional keystroke (C-'), it hides all the lines without that word in it. Very handy!
Commands in library highlight.el
let you (un)highlight text matching a regexp (in this case a symbol), using overlays or text properties. You can cycle among the occurrences. Highlighting can be temporary or persistent. (more info).
This maybe won't highlight but will search for a word without you needing to type it...
when you've reached the word you wanted to search, C-S, then read the full word with C-W then you can C-S and it will search for it. In my Emacs it also highlights all instances in the document.
This package available in Melpa works, you can customize the highlight style as well.
https://github.com/ignacy/idle-highlight-in-visible-buffers-mode