How to get face from under overlay? - emacs

Here's the situation: I need to retrieve the face's under point boundaries, but if I use highlight-current-line mode, that face overlays the face that I'm interested in.
face-at-point or (get-char-property (point) 'face) will only give me the first face in the list, and it will be the one from the current-line overlay. How to get the underlying faces?
EDIT:
This is more or less what I ended up doing:
(defun haxe-face-at-point ()
"This is like `face-at-point' except we will only look for faces
which are relevant to haxe-mode. This will also look under overlays
created by minor modes like ispel or highlight current line."
(interactive)
(let ((props (text-properties-at (point))))
(catch 't
(while props
(when (eql (car props) 'face)
(throw 't
(when (member (cadr props)
'(font-lock-string-face
font-lock-keyword-face
font-lock-variable-name-face
font-lock-comment-face
font-lock-preprocessor-face
font-lock-type-face
default))
(cadr props))))
(setq props (cdr props))))))
I only needed to find out if there's one of the list anyways.

There are sadly no good facilities provided to Elisp code for that. The best I can offer you is to use overlays-at and then loop through the result, using overlay-get to see which ones of the overlays specifies a face and finally use get-text-property to get the face specified by the text-properties (if any). The display engine combines all those.

Related

How to distinguish between different overlays at point

ORIGINAL QUESTION:  I'm looking for some assistance, please, to distinguish between overlays that may exist at point. If the overlays-at point is made with the lawlist-red-face then do X. If the overlays-at point is made with calendar-holiday-marker, then do Y. The overlays are made with these two functions.
(calendar-mark-visible-date (car holiday) lawlist-red-face)
(calendar-mark-visible-date (car holiday) calendar-holiday-marker)
EDIT (January 1, 2014):  #Drew wrote a nice test for this in calendar+.el ( http://www.emacswiki.org/emacs/calendar%2B.el ):
(when
(memq lawlist-red-face
(mapcar (function (lambda (ovr)
(overlay-get ovr 'face))) (overlays-at (point))))
... )
EDIT (February 13, 2014):  The above-listed snippet can also be used in conjunction with something like (while (re-search-backward "[0-9]" nil t) to create a combined list of dates with overlays -- month, day and year are extracted with calendar-extract-day, calendar-extract-month and calendar-extract-year -- the date is obtained with (calendar-cursor-to-nearest-date):
;; modified with the help of #phils based on comments down below.
(setq lawlist-mouse-marked
(append lawlist-mouse-marked
`((holiday-sexp '(list ,month ,day ,year) ""))))
That list can then be used with something like calendar-holiday-list (or the modified code below) to remark dates on a new calendar layout after it has been generated. This is useful if the user has manually marked dates (e.g., with the mouse) and wants those dates to reappear after scrolling the calendar forwards / backwards. The library holidays.el contains the function holiday-sexp which uses a filtering function holiday-filter-visible-calendar to edit the list of dates so that only those that are visible on the new calendar get marked:
(dolist (holiday
(let (res h err)
(sort
(dolist (p lawlist-mouse-marked res)
(if (setq h (eval p))
(setq res (append h res))))
'calendar-date-compare)))
(calendar-mark-visible-date (car holiday) lawlist-mouse-calendar-face))
Ok, let's try to figure this out (without really knowing anything about the calendar).
(overlays-at) returns a list of overlays. I will keep this simple and only handle the first, if you want to examine all of them, simply loop over them until you find one that is suitable.
Also, the face property can be more complex than simply the name of the face, if that is the case you would need to handle that as well.
But anyway, here is a simple piece of code that (hopefully) does what you want:
(let ((overlays (overlays-at (point))))
(if overlays
(let ((face (overlay-get (car overlays) 'face)))
(cond ((eq face 'lawlist-red-face)
;; Do something
)
((eq face 'holiday)
;; Do another thing
)
(t
;; Do something else)))))

Preserving buffer-face-mode when switching major-modes

Once in a while I manually set the font-family and size different from the default, and I use buffer-face-mode to do it. (To be exact I use the mouse & pick one from the dialog box.) Once I set it, I'd like it to stay set for that buffer, even if I change modes, so I tried a customization. The idea was to add a change-major-mode-hook (which runs just before buffer-locals get killed) that would save the buffer face, if it is set, in a function to be called later- that much seems to work. But then that function seems to be called too soon, and when the mode change is over, buffer-face-mode is not active.
Here's the customization I cam up with so far
(defun my-preserve-bufface-cmmh ()
"Keep the state of buffer-face-mode between major-mode changes"
(if (and (local-variable-p 'buffer-face-mode) buffer-face-mode)
(delay-mode-hooks
(message "face is %s" buffer-face-mode-face) ; Just to show me it has the right face
(let ((my-inner-face buffer-face-mode-face))
(run-mode-hooks
(message "inner %s" my-inner-face) ; it still has the right face here
(setq buffer-face-mode-face my-inner-face)
(buffer-face-mode))))))
(add-hook 'change-major-mode-hook
'my-preserve-bufface-cmmh)
The messages both run and show a custom face, as they should, when I'm changing major-mode in a buffer with the minor-mode buffer-face-mode set. I had thought the combination of delay-mode-hooks ... run-mode-hooks would make setq buffer-face-mode-face ... (buffer-face-mode) run after the new mode was set up, but apparently not.
Is this customization "close"/salvageable for my wants? Is there a cleaner way?
The first thing is that delayed-mode-hooks is itself a local variable.
That means, if you set it by (delay-mode-hooks (run-mode-hooks ...)) in change-major-mode-hook
this will have no effect since it is killed instantaneously.
The second thing is that the stuff within your run-mode-hooks is
evaluated within my-preserve-bufface-cmmh. It should be defined as a back-quoted hook function
`(lambda () ...) where you splice in the values you want to keep.
The alternative would be to use lexical binding (which google).
The 2nd thing is demonstrated in the following example (to be evaluated step-by-step):
(defun test (str)
(let ((mytest (concat "hello " str)))
(add-hook 'my-own-hook `(lambda () (message "mytest:%S" ,mytest)))))
(test "you")
(run-hooks 'my-own-hook)
(test "world")
(run-hooks 'my-own-hook)
(put :myface 'test)
If you want to keep the font buffer-local you have to use a local variable that survives kill-all-local-variables such as buffer-file-name. You can hook a property there:
Edit: Even if the symbol has a buffer local value its properties are not buffer local. Thus, the previous approach did not work. Better: Create your own permanent buffer-local variable:
(defvar-local my-preserve-bufface nil
"Keep the state of buffer-face-mode between major-mode changes")
(put 'my-preserve-bufface 'permanent-local t)
(defun my-preserve-bufface-put ()
"Keep the state of buffer-face-mode between major-mode changes"
(and (local-variable-p 'buffer-face-mode)
buffer-face-mode
(setq my-preserve-bufface buffer-face-mode-face)))
(defun my-preserve-bufface-get ()
"Keep the state of buffer-face-mode between major-mode changes"
(and my-preserve-bufface
(setq buffer-face-mode-face my-preserve-bufface)
(buffer-face-mode)))
(add-hook 'change-major-mode-hook 'my-preserve-bufface-put)
(add-hook 'after-change-major-mode-hook 'my-preserve-bufface-get)
Thanks for the educational comments and especially the answer/example from #user2708138, which I am going to accept because it does answer the question.
Yet I am also going to answer my own question, since I did come up with working code that is a more general solution. I went down this path after finding I also wanted my font-size changes maintained and that they were from text-scale-mode, one more minor-mode to keep. This code reads a list of minor-modes to preserve, without my having to figure out which variables they use. (It isn't too hard for a human to figure them out, but I wanted to try having emacs do it).
Alas there is no function I know of to retrieve the variables used by a minor-mode, but the modes I'm interested in use the convention minor-mode-var-name, so this code just filters buffer-local variables for that pattern.
; Save & restore minor modes I wish to be "permanent" if set
(setq my-preserve-minor-modes '(buffer-face-mode text-scale-mode))
(defun my-preserve-minor-modes-cmmh ()
"Keep the state of desired-permanent minor modes between major-mode changes. Assumes that associated buffer-local minor-mode variables to save begin with `minor-mode-'"
(setq my-restore-minor-modes-acmmh nil)
(dolist (mm my-preserve-minor-modes)
(when (and (local-variable-p mm) (symbol-value mm))
(push mm my-restore-minor-modes-acmmh)))
(when my-restore-minor-modes-acmmh
(add-hook 'after-change-major-mode-hook 'my-restore-minor-modes-acmmh)
; Predicate-list showing if symbol starts with a preserved mode
(let ((mm-p-l `(lambda (locvar-nm)
(or ,#(mapcar (lambda (mm)
`(and (< ,(length (symbol-name mm))
(length locvar-nm))
(string-prefix-p ,(symbol-name mm)
locvar-nm)))
my-restore-minor-modes-acmmh)))))
; For each found minor mode, create fn to restore its buf-local variables
(dolist (locvar (buffer-local-variables))
(if (and (listp locvar) (funcall mm-p-l (symbol-name (car locvar))))
(push `(lambda()(setq ,(car locvar) ',(cdr locvar)))
my-restore-minor-modes-acmmh))))))
(defun my-restore-minor-modes-acmmh ()
"After major-mode change, restore minor-mode state, and remove self from hook. It restores state by calling the function stored in the variable my-restore-minor-modes-acmmh."
(remove-hook 'after-change-major-mode-hook 'my-restore-minor-modes-acmmh)
(dolist (restore-f my-restore-minor-modes-acmmh) (funcall restore-f)))
(add-hook 'change-major-mode-hook 'my-preserve-minor-modes-cmmh)
I did know about the permanent-local property but I wasn't sure about any unwanted side-effects... probably unwarranted paranoia on my part!
My answer could be improved if there is ever a way to get a list of variables for a minor mode, or by having the user specify an alist of variables per minor mode- in either case we wouldn't have to loop over buffer-local-variables anymore, and maybe simply making those all permanent-local would be all we need, simplifying the code quite a bit. Anyway, figuring all this out (with your help & looking at the fine manual) was quite educational.

Emacs make ibuffer filter per frame

I use ibuffer as well as have opened several frames. The very frequent use-case of mine for ibuffer is to filter the list (C-x C-b) of buffers by name/filename. The catch is that filter works in the same way for all frame (it is predicted I believe). I'd want to have to apply filter for ibuffer listing per each frame I have.
E.g. in a first frame I'd like to see list of buffers filtered by a filename, in a second one filtered by mode.
Looking around ibuffer.el I have discovered an entity called ibuffer-filtering-qualifiers which holds an associative list of currently applied filters. I have tried played:
(let ((ibuffer-filtering-qualifiers '())) ;;; Imitate that there are no filters applied
(call-interactively 'ibuffer))
But this was not worked for me.
Could you please point me out how to achieve such desired behavior of ibuffer?
The function ibuffer-update uses the buffer-list, which includes all frames. Modifying ibuffer-update from using buffer-list to (frame-parameter (selected-frame) 'buffer-list) approaches the behavior you are seeking. An ibuffer filter based upon that same concept should also be possible.
In my opinion, the per frame buffer list (frame-parameter (selected-frame) 'buffer-list) is not the best method to associate buffers with frames because it gets modified too easily. The best method I have seen is frame-bufs by Al Parker:  https://github.com/alpaker/Frame-Bufs  This has been updated to work with a current version of Emacs, however, I am using a slightly modified version to suit my own needs.
So, the easiest method that I am aware to answer your question is to use a combination of frame-bufs by Al Parker, and use the frame-bufs-buffer-list within ibuffer-update. That gives you the best of both worlds. Here is an example that will give you a nice ibuffer with only the buffers associated with a given frame -- all other buffers will not be displayed. [FYI:  frame-bufs also sorts by mode or by file name.]
(require 'ibuffer)
(defalias 'ibuffer-update 'lawlist-ibuffer-update)
(defun lawlist-ibuffer-update (arg &optional silent)
"Regenerate the list of all buffers.
Prefix arg non-nil means to toggle whether buffers that match
`ibuffer-maybe-show-predicates' should be displayed.
If optional arg SILENT is non-nil, do not display progress messages."
(interactive "P")
(if arg
(setq ibuffer-display-maybe-show-predicates
(not ibuffer-display-maybe-show-predicates)))
(ibuffer-forward-line 0)
;; (let* ((bufs (buffer-list))
(let* ((bufs (frame-parameter (selected-frame) 'frame-bufs-buffer-list))
(blist (ibuffer-filter-buffers
(current-buffer)
(if (and
(cadr bufs)
(eq ibuffer-always-show-last-buffer
:nomini)
(minibufferp (cadr bufs)))
(cl-caddr bufs)
(cadr bufs))
(ibuffer-current-buffers-with-marks bufs)
ibuffer-display-maybe-show-predicates)))
(and (null blist)
(featurep 'ibuf-ext)
ibuffer-filtering-qualifiers
(message "No buffers! (note: filtering in effect)"))
(unless silent
(message "Updating buffer list..."))
(ibuffer-redisplay-engine blist arg)
(unless silent
(message "Updating buffer list...done")))
(if (eq ibuffer-shrink-to-minimum-size 'onewindow)
(ibuffer-shrink-to-fit t)
(when ibuffer-shrink-to-minimum-size
(ibuffer-shrink-to-fit)))
(ibuffer-forward-line 0)
;; I tried to update this automatically from the mode-line-process format,
;; but changing nil-ness of header-line-format while computing
;; mode-line-format is asking a bit too much it seems. --Stef
(setq header-line-format
(and ibuffer-use-header-line
ibuffer-filtering-qualifiers
ibuffer-header-line-format)))
The issue you have is not that the ibuffer works in the same way for all frames. The issue is that the ibuffer buffer is the same for all the frames (each frame has a separate window (with its cursor) showing your ibuffer unique instance).
A simple solution is to clone the ibuffer buffer (M-x clone-buffer). And that is: you have two different buffers and you can apply filters to them independently.

Dedicated misc buffer in Emacs (autocomplete, function info etc.)

I have been wondering for a very long time now: how to get a dedicated misc buffer in Emacs?
Auto-completion, function descriptions and perhaps documentation all can go there without ending up somewhere unexpected, but instead at a predefined location (a quarter of the screen perhaps?).
(I'm assuming you mean a dedicated window instead of a dedicated buffer.) If you keep a window open without doing any other window-splitting commands, help/repl buffers will automatically use it. You can change the size of the window as described in this question.
If you want to do be able to do normal window manipulation but have help windows be a certain size, I suggest you investigate temp-buffer-show-hook, a hook that is run when temporary buffers (such as help buffers) are shown. I haven't tried it, but it would probably be possible to set it to a function that arranges your window configuration in a particular way.
Here is what I do in One On One, to define a dedicated *Help* frame:
;; *Help* frame
(if 1on1-*Help*-frame-flag
(add-to-list
'special-display-buffer-names
(list "*Help*" '1on1-display-*Help*-frame
(list (cons 'background-color 1on1-help-frame-background)
(cons 'mouse-color 1on1-help-frame-mouse+cursor-color)
(cons 'cursor-color 1on1-help-frame-mouse+cursor-color)
'(height . 40))))
(setq special-display-buffer-names
(1on1-remove-if (lambda (elt) (equal "*Help*" (car elt)))
special-display-buffer-names)))
(defun 1on1-display-*Help*-frame (buf &optional args)
"Display *Help* buffer in its own frame.
`special-display-function' is used to do the actual displaying.
BUF and ARGS are the arguments to `special-display-function'."
(let ((old-ptr-shape (and (boundp 'x-pointer-shape) x-pointer-shape))
return-window)
(when (boundp 'x-pointer-xterm) (setq x-pointer-shape x-pointer-xterm))
(setq return-window (select-window (funcall special-display-function buf args)))
(raise-frame)
(setq x-pointer-shape old-ptr-shape)
return-window))
You don't need all of those details (pointer shapes etc.), but that gives you the idea. The main thing is to put *Help* on special-display-buffer-names. That's really all you need to do.
The 1on1-* variables used for the frame parameters here are pretty obvious. The *-remove-if function is a standard remove-if. The complete code is here: oneonone.el.

Gnus: How to save *all* attachments at once?

I would like to save all attachments to an email at once. I therefore set gnus-summary-save-parts-default-mime to ".* /.*". However, when using "X m", I not only get all attachments, but also a file named "nnimap+my.name#googlemail.com/INBOX.2393.1" (referring to the account I'm reading emails from) which contains the signature of the email I received. How can I exclude files of this "type" from being saved on "X m"? In other words: How can I specify the correct regexp for gnus-summary-save-parts-default-mime to prevent this file from being saved, too?
This defadvice will do what you want for the moment by excluding any parts that do not have filenames (in this case that is true of the article itself):
(defadvice gnus-summary-save-parts-1 (around gnus-summary-save-parts-exclude-self activate)
(let ((handle (ad-get-arg 2)))
(unless (and (not (stringp (car handle)))
(not (mm-handle-filename handle)))
ad-do-it)))
I am using Gnus v5.13; if you're also using the same or similar version, let me know if this modified version of gnus-summary-save-parts-1 works for you; you will want to set gnus-summary-save-parts-exclude-article to t. If it works for you, I will submit a patch for it to the Gnus projects.
Note, either use the above defadvice OR use the code below, but do not use both together. The defadvice is an easy quick fix that you can use for the moment. The code below I will submit as a patch to the Gnus project and I only included this here for you to test to see if it works on your system if you are also using Gnus v5.13. If they accept this patch and make it part of a future release then you will not need the defadvice above; instead you'll just be able to customize the gnus-summary-save-parts-exclude-article variable.
(require 'gnus)
(require 'gnus-sum)
(defcustom gnus-summary-save-parts-exclude-article nil
"If non-nil don't save article along with attachments."
:group 'gnus-article-mime
:type 'boolean)
(defun gnus-summary-save-parts-1 (type dir handle reverse)
(if (stringp (car handle))
(mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
(cdr handle))
(when (if reverse
(not (string-match type (mm-handle-media-type handle)))
(string-match type (mm-handle-media-type handle)))
(let* ((name (or
(mm-handle-filename handle)
(unless gnus-summary-save-parts-exclude-article
(format "%s.%d.%d" gnus-newsgroup-name
(cdr gnus-article-current)
gnus-summary-save-parts-counter))))
(file (when name
(expand-file-name
(gnus-map-function
mm-file-name-rewrite-functions
(file-name-nondirectory
name))
dir))))
(when file
(incf gnus-summary-save-parts-counter)
(unless (file-exists-p file)
(mm-save-part-to-file handle file)))))))