Display property of overlays - emacs

Is it possible to put both a string and an image into an overlay?
So far I know how to put a string into an overlay:
(overlay-put ov 'display "...")
Or a image:
(overlay-put ov 'display '(image :type xbm :file "~/foo.xbm"))
But how do I achieve both? The following does not work:
(overlay-put ov 'display
(concat
"..."
(propertize " "
'display '(image :type xbm :file "~/foo.xbm"))))

If I'm not mistaken an overlay property can have only one value, and of course you cannot have multiple properties with the same name.

Related

Pin/lock/fix text

With elisp it is possible to add overlays to parts of the buffer to hide them etc. I went through all the possible overlays and couldn't find a way to pin a selection. Is it possible to have a function that, given a selection in a buffer, pins this selection so that when you scroll up or down the selection is always shown? (a bit like what you have with Excel where you can lock some rows or columns so that they always appear on screen).
I wanted to do something like this but with (overlay-put new-overlay 'lock t) but there doesn't appear to be such overlay.
(defun hide-region-hide ()
"Hides a region by making an invisible overlay over it and save the
overlay on the hide-region-overlays \"ring\""
(interactive)
(let ((new-overlay (make-overlay (mark) (point))))
(push new-overlay hide-region-overlays)
(overlay-put new-overlay 'invisible t)
(overlay-put new-overlay 'intangible t)
(overlay-put new-overlay 'before-string
(if hide-region-propertize-markers
(propertize hide-region-before-string
'font-lock-face 'hide-region-before-string-face)
hide-region-before-string))
(overlay-put new-overlay 'after-string
(if hide-region-propertize-markers
(propertize hide-region-after-string
'font-lock-face 'hide-region-after-string-face)
hide-region-after-string))))
I came up with this solution that works pretty well:
(defvar-local pinned-buffer nil
"Variable to store the buffer that contains the pinned region.")
(defun region-unpin ()
"Unpin the current region"
(interactive)
(when pinned-buffer
(let ((window (get-buffer-window pinned-buffer 'visible)))
(setq-local pinned-buffer nil)
(quit-window t window))))
(defun region-pin ()
"Pin the current region to the top."
(interactive)
(when (use-region-p)
(let* ((regionp (buffer-substring (mark) (point)))
(buffer (get-buffer-create "tmp.ml"))
(mode major-mode))
(with-current-buffer buffer
(funcall mode)
(hide-mode-line-mode)
(goto-char (window-end))
(insert regionp)
(goto-char 0))
(setq-local window-min-height 1)
(setq-local pinned-buffer buffer)
(display-buffer-in-direction buffer '((direction . above)
(inhibit-same-window . t)
(window-height . fit-window-to-buffer)))
)))
This allows me to have a temporary window with the major mode of my current one, no modeline, the height of the window fits perfectly the selection and the cursor is set to the beginning of it to have the full text displayed in it but whenever I want to pin some more text it goes to the end of the buffer, insert the selected region and goes back up.

Update linum and linum-relative background on color theme change in emacs/spacemacs

Trying to get the line numbering right aligned with a separator space and the same background color as the line highlighting in Spacemacs was quite complicated. Specialy when it came to do both at the same time in linum and linum-relative.
I don't know if this code is ok, but it manages so far:
(defun dotspacemacs/user-config ()
"Configuration function for user code.
This function is called at the very end of Spacemacs initialization after
layers configuration. You are free to put any user code."
(global-linum-mode t)
(unless window-system
(add-hook 'linum-before-numbering-hook
(lambda ()
(setq-local my-linum-format-fmt
(let ((w (length (number-to-string
(count-lines (point-min) (point-max))))))
(concat "%" (number-to-string w) "d"))))
(set-face-attribute 'linum nil
:background (face-background 'hl-line nil t))))
(defface my-linum-hl
`((t :inherit linum :background ,(face-background 'hl-line nil t)))
"Face for the current line number."
:group 'linum)
(defun my-linum-format-func (line)
(concat
(propertize (format my-linum-format-fmt line) 'face 'my-linum-hl)
(propertize " " 'face 'my-linum-hl)))
(unless window-system
(setq linum-format 'my-linum-format-func))
;; linum-relative
(linum-relative-toggle)
(unless window-system
(setq-local my-linum-relative-format-fmt
(let ((w (length (number-to-string
(count-lines (point-min) (point-max))))))
(concat "%" (number-to-string w) "s "))))
(unless window-system
(setq linum-relative-format my-linum-relative-format-fmt))
)
Problem is: The numbers background color don't change to the correct one when I change the theme while inside Emacs. The color remains the same. How do I make emacs update the linum and linum-relative background color after a color theme change?

How to display text with different colors in mode-line

I want to display parts of my mode line with different colors but it isn't working as expected and I can't find a good web reference for this. I can change the text to bold or italic but not change the colors as required.
The simplest possible example is to display a simple mode line with the buffer-file-name in white rather than the default face color.
(custom-set-variables
'(mode-line-format
(quote
("%e" mode-line-front-space
"[" mode-name "] %l:%i"
"\t"
propertize buffer-file-name 'font-lock-face '(:foreground "white")))))
Thanks to legosica for pointing out that I should have included other examples of what I've tried ...
Replacing 'font-lock-face with 'face:
propertize buffer-file-name 'face '(:foreground "white")))))
Follow Up
Thanks to TacticalCoder I now have exactly what I want - multiple fonts and colours in my modeline. The reason why setting 'face '(:foreground "white") didn't work is that it needed to be wrapped in '(:eval ...).
I ended up with this ...
(setq-default mode-line-format
(list
mode-line-front-space ; +-- just like in the default mode-line-format
'(:eval (propertize (concat "\t[" mode-name "] %l:%i\t") 'face '(:foreground "black" :height 0.9 :weight normal)
'help-echo (buffer-file-name)))
'(:eval (propertize (file-name-directory buffer-file-name) 'face 'info-title-4
'help-echo (buffer-file-name)))
'(:eval (propertize (file-name-nondirectory buffer-file-name) 'face 'info-title-3
'help-echo (buffer-file-name)))
))
Combined with ...
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(info-title-3 ((t (:inherit info-title-4 :foreground "white" :height 1.2))))
'(info-title-4 ((t (:inherit info-title-4 :foreground "black"))))
'(mode-line ((t (:background "#6483af" :foreground "#001122" :box (:line-width 3 :color "#6483af") :weight ultra-bold :height 118 :family "Monospace")))))
... I get a nice simple mode-line that demonstrates most of what I want. More work to do but thanks to TacticalCoder, I'm back on track.
Here's a tiny part of the custom modeline I'm using (I don't remember where I found it), modified as you asked to show the buffer name in another color. In this example I'm using font-lock-warning-face (which is 'red' in my color scheme):
This is not a complete modeline by any mean:
(setq-default mode-line-format
(list
mode-line-front-space ; +-- just like in the default mode-line-format
mode-line-mule-info ; |
mode-line-client ; |
;; the buffer name; the file name as a tool tip if you hover the mouse on it
'(:eval (propertize "%b " 'face 'font-lock-warning-face
'help-echo (buffer-file-name)))
'(:eval (propertize (if overwrite-mode "OVERWRITE" "")
'face 'font-lock-warning-face
'help-echo (concat "Buffer is in "
(if overwrite-mode "overwrite" "insert") " mode")))
"%-" ; fill what's left with '-'
))
Does that work for you? I did also put the part where OVERWRITE appears in font-lock-warning-face in case you turn overwrite on (I kinda hate being in overwrite mode, so I want it to be very obvious).

Change the color of the mode-line depending on buffer state

Is there a way to dynamically change the color of the mode-line depending on specific conditions e.g. change color if I'm in narrowed view and to a different color if the buffer is read-only
Thank you very much!
You can use a post-command-hook and then just evaluate whatever you need and set the mode-line face color. I do this to change between 3 colors depending on which evil-mode state I'm in and if the buffer has any unsaved changes.
(lexical-let ((default-color (cons (face-background 'mode-line)
(face-foreground 'mode-line))))
(add-hook 'post-command-hook
(lambda ()
(let ((color (cond ((minibufferp) default-color)
((evil-insert-state-p) '("#e80000" . "#ffffff"))
((evil-emacs-state-p) '("#af00d7" . "#ffffff"))
((buffer-modified-p) '("#006fa0" . "#ffffff"))
(t default-color))))
(set-face-background 'mode-line (car color))
(set-face-foreground 'mode-line (cdr color))))))
I use this code. It colors the buffer-modified indicator on the left orange if it's read-only and red if it's modified. When you narrow a buffer it colors the line number indicator yellow. Obviously you might want to change the format yourself.
(defface my-narrow-face
'((t (:foreground "black" :background "yellow3")))
"todo/fixme highlighting."
:group 'faces)
(defface my-read-only-face
'((t (:foreground "black" :background "orange3")))
"Read-only buffer highlighting."
:group 'faces)
(defface my-modified-face
'((t (:foreground "gray80" :background "red4")))
"Modified buffer highlighting."
:group 'faces)
(setq-default
mode-line-format
'(" "
(:eval (let ((str (if buffer-read-only
(if (buffer-modified-p) "%%*" "%%%%")
(if (buffer-modified-p) "**" "--"))))
(if buffer-read-only
(propertize str 'face 'my-read-only-face)
(if (buffer-modified-p)
(propertize str 'face 'my-modified-face)
str))))
(list 'line-number-mode " ")
(:eval (when line-number-mode
(let ((str "L%l"))
(if (/= (buffer-size) (- (point-max) (point-min)))
(propertize str 'face 'my-narrow-face)
str))))
" %p"
(list 'column-number-mode " C%c")
" " mode-line-buffer-identification
" " mode-line-modes))

Highlight which of the file names in emacs buffer are missing

In GNU Emacs, I could use something like a hypothetical "flyexist.el" - I have a buffer with absolute (Unix) file names in it (plus some additional text around them). Most of those files exist, but some are missing. I would like to run a function that highlights me the missing files (maybe with a red overlay). This function would need to figure out which of the text in the buffer looks like a file name (some false positives are okay) and then work on it with file-exists-p.
For example, assume that my buffer contains
Some random text mentioning /file/that/does/exist.txt,
some more random text, and a /file/that/does/not-exist.txt
I want to have the second file highlighted.
Does something like this exist already?
I am new to emacs hacking... Here is my "minor-mode" version.
(defvar filehi-path-re "\\([/$][[:alnum:]$-_.]+\\)+"
"Regexp used for path matching.")
(defface filehi-file-existing
'((t (:foreground "White" :background "Green")))
"Face for existing files.")
(defface filehi-file-missing
'((t (:foreground "Yellow" :background "Red")))
"Face for missing files.")
(defun filehi-check-and-highlight (start end)
"Check if substring is existing file path and highlight it."
(remove-overlays start end 'name 'filehi-highlight)
(let ((overlay (make-overlay start end)))
(overlay-put overlay 'name 'filehi-highlight)
(overlay-put overlay 'face (if (file-exists-p (substitute-in-file-name
(buffer-substring start end)))
'filehi-file-existing
'filehi-file-missing))))
(defun filehi-highlight-file-paths (&optional start end _ignore)
"Run through the buffer and highliht file paths."
(save-excursion
(save-match-data ; fixes problem with dabbrev (and may be more...)
(remove-overlays (point-min) end 'name 'filehi-highlight)
(let ((prev-end (point-min)))
(goto-char (point-min)) ; FIXME use something like greedy
; search-backward
(while (and (<= (point) end)
(re-search-forward filehi-path-re nil t))
(filehi-check-and-highlight (match-beginning 0) (match-end 0)))))))
(define-minor-mode filehi-mode
"Minor mode for highlighting existing file paths.
May conflict with other modes..."
nil " Filehi" nil
(if filehi-mode
(progn ; enable mode
(make-local-hook 'after-change-functions)
(filehi-highlight-file-paths (point-min) (point-max))
(add-hook 'after-change-functions 'filehi-highlight-file-paths nil t))
; disable mode
(remove-hook 'after-change-functions 'filehi-highlight-file-paths t)
(remove-overlays (point-min) (point-max) 'name 'filehi-highlight)))
Try with this (you have to trigger it manually though, or incorporate it into some other periodical routine):
(defun x-mark-missing-files ()
(interactive)
(save-excursion
(while (search-forward-regexp "~?/[A-Za-z./-]+")
(when (not (file-exists-p (match-string 0)))
(overlay-put
(make-overlay (match-beginning 0) (match-end 0))
'face '(:background "red"))))))
Play a little with the filename regexp to get it right how you want it.