Highlighted text in Emacs minibuffer - emacs

I'm trying to display some highlighted text in the Emacs minibuffer. I know it's possible because SLIME does it when displaying argument hints. However, I can't see how it's being accomplished by looking at slime.el. Based on what I'm reading there, the displayed text shouldn't be highlighted (relevant section starts at line 3615).
I've tried
(message "%s" (propertize "test" 'face 'highlight))
(overlay-put (make-overlay (point-min) (point-min)) 'before-string (propertize "test" 'face 'highlight))
(with-current-buffer (window-buffer (minibuffer-window)) (insert (propertize "test" 'face 'highlight)))
That last one seems closest to what I want, but the displayed text appears and disappears sporadically as I move point, and it has to be manually removed later. I've also tested the solution given here, and it doesn't seem to work for me either. The non-working solutions all do the same thing; display the text
#("test" 0 4 (face highlight))
in the minibuffer.
In case it matters, I'm running Emacs 23.4.1 on Debian Wheezy.

You're victim of testing with M-: which additionally to running the code, displays the returned value in the minibuffer, thus immediately overwriting whatever your code has done.

Related

emacs highlight background changes

hi: Each time I insert some text in emacs , it will highlight the newly added text. I wonder how to change the background color of the highlight, because the highlight background color is very close to the font color , as a result, I can hardly recognize the code that I am writing.
thank you soooo much
For issues with fonts (which Emacs calls faces) inside of Emacs, it is often helpfull to know the function 'list-faces-display'. You can call this with M-x and it will list all faces defined in the current frame. This can be helpfull both identifying which face is problematic, it will also give you its name which can be used to modify the face. For instance to change the foreground colour of the face named "button" you can call something like this:
(set-face-foreground 'button "cyan")
The effect will be immediately visible. Many aspects of faces can be changed, including colour, font familiy and font size.
Obviously, this will not help you if the problematic behaviour stems from the terminal emulator you are using, as it would appear from some of the comments to your question, then the problem is outside of Emacs and cannot be fixed from inside of Emacs. Even so, knowing about 'list-faces-display' is usefull.
I had this exact question and managed to solve it using the following ways. But I also had another thing in mind: a marker to show which lines are modified.
For tracking changes between the saved file and the buffer, we should use the highlight-changes-mode. But before enabling that mode, we need to prepare some stuff as explained beautifully here for the line marks:
;; a part to add the fringe marks to the gutter. To change the shape, read the explanation of it in this code.
(eval-after-load "hilit-chg"
'(progn
(defvar highlight-fringe-mark 'filled-square
"The fringe bitmap name marked at changed line.
Should be selected from `fringe-bitmaps'.")
(defadvice hilit-chg-make-ov (after hilit-chg-add-fringe activate)
(mapc (lambda (ov)
(if (overlay-get ov 'hilit-chg)
(let ((fringe-anchor (make-string 1 ?x)))
(put-text-property 0 1 'display
(list 'left-fringe highlight-fringe-mark)
fringe-anchor)
(overlay-put ov 'before-string fringe-anchor))
))
(overlays-at (ad-get-arg 1))))))
;; make the highlight-changes-mode reset when the file is saved
(add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
(add-hook 'after-save-hook
(lambda ()
(when highlight-changes-mode
(save-restriction
(widen)
(highlight-changes-remove-highlight (point-min) (point-max))))))
make sure it is enabled globally except for buffers that start with ' and *
(setq highlight-changes-global-modes t)
(global-highlight-changes-mode)
make the mode to respect the syntax-highlighting
;; find the name of other faced using M-x ~list-faces-display~
(custom-set-faces
'(highlight-changes ((t (:background "dark green" :foreground nil)))))
(set-face-foreground 'highlight-changes nil)
(set-face-background 'highlight-changes "dark green")

elisp message-box: can I include newlines in the text, and if so, how?

Using the message-box fn, I can display a modal dialog.
I know this is annoying and not always a good user experience. Flymake's use of the message-box to warn when a flymake check has failed, seems a good example of that.
But put the user experience issue aside for the purposes of this question. Assume that I am sensible enough to use message-box responsibly.
How can I format the text displayed by the message box? The simplest case is, how can I tell message box to display multiple lines of text. If I have a longish message, it results in a very wide message box. (Another UI problem exhibited in the Flymake usage).
See here for an example. this code:
(message-box (concat "You need to get an \"api key\".<NL>"
"Then, set it in your .emacs with the appropriate statement."))
results in this UI:
I'd like a newline in place of the <NL>. I tried using \n and \r and \r\n, none of those worked. I also tried \x000D and \x000A.
Even better than simple line breaks, I'd like to be able to format the text. Italic, bold, or whatever. Are there options? Nothing is mentioned in the doc on this.
I looked in the source to try to figure this out but could not find message2, which is called by message-box, and I'm not sure I'd learn anything anyway by just looking at source.
Use \n. That does the trick:
(message-box (concat "You need to get an \"api key\".\n"
"Then, set it in your .emacs with the appropriate statement."))
hack workaround on Windows for bug #11138.
(defun multiline-message-box (msg)
"display a multiline message box on Windows.
According to bug #11138, when passing a message with newlines to
`message-box' on Windows, the rendered message-box appears all on
one line.
This function can work around that problem.
"
(flet ((ok (&optional p1 &rest args) t))
(let ((parts (split-string msg "\n"))
(menu-1 (make-sparse-keymap "Attention"))
c)
(define-key menu-1 [menu-1-ok-event]
`(menu-item ,(purecopy "OK")
ok
:keys ""))
(define-key menu-1 [separator-1] menu-bar-separator)
;; add lines in reverse order
(setq c (length parts))
(while (> c 0)
(setq c (1- c))
(define-key menu-1 (vector (intern (format "menu-1-fake-event-%d" c)))
`(menu-item ,(purecopy (nth c parts))
nil
:keys ""
:enable t)))
(x-popup-menu t menu-1))))
(multiline-message-box "Hello!\nI must be going!\nThis is line 3.")

Clickable text in emacs minibuffer

How can I put clickable text in emacs minibuffer?
For example, this code makes clickable text in a text buffer and it works fine for me.
(let ((map (make-sparse-keymap)))
(define-key map (kbd "<down-mouse-1>")
'(lambda() (interactive) (message-box "it works!")))
(add-text-properties 1 5 `(keymap, map
mouse-face highlight)))
However, similar code for minibuffer doesn't work correctly
[...]
(with-current-buffer (window-buffer (minibuffer-window))
(message "link")
(add-text-properties (point-at-bol) (point-at-eol)
`(keymap, map
mouse-face highlight))))
As a result a text is put in a minibuffer without these properties.
I also tried (propertize but result is same.
[...]
(message (propertize "link"
'mouse-face 'highlight
'keymap map)))
What's wrong with this?
Thanks
message writes to the echo area, not the minibuffer.
The minibuffer is not active. Even if you use insert instead of message you will not see the text there.
What is it that you are really trying to do? What is the context where you want to insert such propertized text in the minibuffer? The minibuffer is active when, e.g., input is being read. You can initiate reading and insert the text there when it is active.

Emacs: TODO indicator on left fringe has a strange side-effect - deleting characters

I just read Emacs :TODO indicator at left side, and tried it out. It seems intriguing. The little indicator triangles appear, but I'm getting a weird side effect: the text itself is being altered. Characters are being deleted.
Before:
After:
The mode-line does indicate that the buffer has been altered after running annotate-todo.
What explains this?
(I'm using emacs 22.2.1 on Windows)
Ahhh... I see the error of my ways earlier. Here's a new version.
(defun annotate-todo ()
"put fringe marker on TODO: lines in the curent buffer"
(interactive)
(save-excursion
(goto-char (point-min))
(while (re-search-forward "TODO:" nil t)
(let ((overlay (make-overlay (- (point) 5) (point))))
(overlay-put overlay 'before-string (propertize (format "A")
'display '(left-fringe right-triangle)))))))
The first solution used a the 'display text property, which changes how the specified text is displayed, in this case it was replaced by the triangle in the left fringe. What I needed to do was to use a 'before-string overlay instead. Which doesn't change the string being displayed.
Another advantage, the cut/paste of the code annotated by this does not carry the markup.
I've updated the code in the original question to reflect this change as well.

Emacs :TODO indicator at left side

I want to have sort of indiacator at left side of the line wherever I have in the source code
#TODO: some comment
//TODO: some comments
The indicator could be a just mark and I already enabled line numbers displayed at emacs.
This command will do something like you want.
(defun annotate-todo ()
"put fringe marker on TODO: lines in the curent buffer"
(interactive)
(save-excursion
(goto-char (point-min))
(while (re-search-forward "TODO:" nil t)
(let ((overlay (make-overlay (- (point) 5) (point))))
(overlay-put overlay 'before-string (propertize "A"
'display '(left-fringe right-triangle)))))))
You can customize the bitmap as desired.
To get this to apply to all files, you could add it to the 'find-file-hooks
(add-hook 'find-file-hooks 'annotate-todo)
Or, if you want it just for certain modes, you could add it to those mode hooks.
See Fringes, The 'display' Property, Overlays, and most importantly the before-string property.
Note: The code was updated 27/02/2010 to use overlays instead of directly adding text properties to the current text.
I like the approach described in this post on emacs-fu, which adds TODO/FIXME/... to the font-lock settings of the modes where you need it. In contrast to Trey's approach this should highlight the words as you type, whereas his approach should only highlight them when you open a file (or do I get this wrong).
Anyway its up to you. A good google search gives you probably even more ideas: http://www.google.com/search?q=emacs+highlight+todo
Update: Your question has already been answered: Emacs, highlight all occurences of a word