Change where semantic summary is displayed - emacs

In CEDET, the minor mode semantic-idle-summary-mode displays information about the symbol under point in the echo area. I really like this mode, as it helps me remember, for instance, which arguments the function I'm calling needs.
The problem is, it's a little buggy about displaying in the echo area. Since it automatically activates whenever there's a symbol under point, it sometimes hides useful information that is being displayed in the echo area (after all, that's the area that emacs uses to tell you stuff).
Is there a way to display the summary information somewhere else? A tooltip would be ideal, but one of the ecb frames is acceptable as well.

The first thing that comes to mind is the variable tooltip-use-echo-area which controls where / how tooltips are displayed. When set to t, all tooltips are displayed in the echo area. What is its value on your system? Maybe it would be possible to force cedet to use actual (pop-up) tooltips by setting that variable to nil.

semantic-idle-summary-mode uses the function eldoc-message and a few other eldoc queries to determine when to display messages. This means it should be pretty good at not covering up useful information.
Since eldoc is the preferred mode for providing similar summary information in Emacs Lisp buffers, the best thing would be to configure eldoc, but I didn't see a way to do it since eldoc-message appears configured to always call message.
Anyway, what that means is you can use defadvice to override eldoc-message to use a tooltip, and you will have your solution.
The below snippit is a guess at how to use defadvice, but I didn't give it a try.
(defadvice eldoc-message (around bruce-mode activate)
"Make eldoc display messages as a tooltip."
(if (some condition that means I want to use a tooltip)
(bruce-eldoc-message (ad-get-arg 0))
ad-do-it))
(require 'tooltip)
(defun bruce-eldoc-message (&rest args)
"My version of displaying a message for eldoc."
(if (null (cdr args))
;; One argument
(tooltip-show (car args))
;; Else, use format
(tooltip-show (apply 'format args)))
)

I had a similar need as you,
and I addressed it with this extension.
As you can see on this screenshot,
it shows the function arguments at the point of its invocation, without altering the echo area.
Some neat features are:
Shows you all overloaded functions, including constructors where appropriate.
Highlights in bold the current argument.
Jump to definition functionality for the current function variant.

Related

How to make emacs completing-read display *Completions* buffer as soon as I type

In emacs lisp, I expect the following to display a list of options in the completion buffer as I type. It prompts for information but doesn't display choices, either before or after I start typing. For example, I'd expect it to display 'First' after I'd typed 'F'.
(defun reader ()
(interactive)
(let ((choices '("First" "Second" "Third")))
(completing-read "Choose: " choices)))
Essentially I'm looking for something similar to the output described here. But what I get is this (there's nothing below the line, which is the edge of my window):
Can anyone explain where I'm going wrong with completing-read?
That's the way completing-read behaves. You're not doing anything wrong. It just doesn't support any kind of automatic, incremental completion.
If you want such automatic completion then you either need to write your own code to provide that or use a completion framework that provides it.
Icicles was the first to provide this incremental-completion behavior. Many other packages now provide the same or similar.

Paragraph filling for org-mode inside latex environment

I started using org-mode to write mathematical papers so I make a heavy use of latex environments such as proof, theorem, lemma, etc. For example I often write
\begin{proof}
a very long proof follows
\end{proof}
The problem is that in org-mode the fill-paragraph (or M-q) doesn't work inside latex environments. This complicates my life because some proofs can be very long, reaching several pages when compiled to pdf, and I am unable to efficiently format them in org-mode. I couldn't find any information in the manual on options controlling paragraph filling. Is it possible to enable fill-paragraph in this case?
The problem disappears if you use an org block instead of a latex environment:
#+BEGIN_proof
...
#+END_proof
This gets exported as \begin{proof}...\end{proof}. It also lets you use org syntax inside the block, and fill paragraph works.
if you don't want to do that, maybe try visual-line-mode as a workaround.
Edit: change fill-paragraph behaviour
If you want fill paragraph to work in latex environments, you have to dig a little deeper. Filling is done by org-fill-paragraph in org.el and this function ignores latex environments by default. To change this, go to the end of the function and replace
;; Ignore every other element.
(otherwise t)
with
(latex-environment nil) ;; use default fill-paragraph
;; Ignore every other element.
(otherwise t)
If you'd rather not change the org sources, you could use advice instead, e.g.
(defun org-fill-paragraph--latex-environment (&rest args)
"Use default fill-paragraph in latex environments."
(not (eql (org-element-type (org-element-context)) 'latex-environment)))
(advice-add 'org-fill-paragraph :before-while #'org-fill-paragraph--latex-environment)
I have a command that you could bind to M-q in Org:
(defun leuven-good-old-fill-paragraph ()
(interactive)
(let ((fill-paragraph-function nil)
(adaptive-fill-function nil))
(fill-paragraph)))
Though, I don't understand either why it's not enabled by default. Maybe a question for the Org ML?

How to control which window to be used for new contents in Emacs?

Often I have multiples windows carefully arranged within a frame.
However, certain command, say M-x man RET will grab one of the visible windows to display its own content. Sometimes this is annoying because the window that was taken away is one that I need to keep it visible.
e.g. I have 3 windows on screen, one useful source-code window and two useless windows. I want to keep the soure-code window visible while checking the man page. But very often Emacs just take away the code-window for the newly opened man page.
One way I can think of is to display the (chronological) open order of each window, so that I can focus point on n-th window and be confident that Emacs will grab (n+1)-th window for new content.
Is there a way to display such order, e.g. in the mode-line of each window?
Or is there another way for better control for displaying new window?
A little late to the party but as discussed in the comments, using dedicated windows is a good way to control where new content is displayed (+1 to #lawlist for bringing it up and to #phils for mentioning toggling!).
I'm pretty sure you'd be able to implement a command that toggles dedicatedness yourself at this point, but since I have the code for this handy I'll share it anyway:
(defun toggle-window-dedicated ()
"Control whether or not Emacs is allowed to display another
buffer in current window."
(interactive)
(message
(if (let (window (get-buffer-window (current-buffer)))
; set-window-dedicated-p returns FLAG that was passed as
; second argument, thus can be used as COND for if:
(set-window-dedicated-p window (not (window-dedicated-p window))))
"%s: Can't touch this!"
"%s is up for grabs.")
(current-buffer)))
(global-set-key (kbd "C-c d") 'toggle-window-dedicated)
Now, in a multi-window setup you can simply press C-c d in each window you would like to "protect".
I'll throw this example usage of display-buffer-alist into the mix, given that it was mentioned in the comments, and is indeed a general mechanism for controlling how and where buffers are displayed (even though, as Drew indicates, it is far from trivial).
Start with the help for the variable itself:
C-hv display-buffer-alist RET
From there you'll get to the help for display-buffer, and no doubt acquire some idea of the complexities involved :)
In any case, the following is an example of using a custom function to decide how to display buffers named *Buffer List*.
You can easily see that if you can write a function which can figure out how to display a buffer where you want it to, then you can use display-buffer-alist to make it happen.
(defun my-display-buffer-pop-up-same-width-window (buffer alist)
"A `display-buffer' ACTION forcing a vertical window split.
See `split-window-sensibly' and `display-buffer-pop-up-window'."
(let ((split-width-threshold nil)
(split-height-threshold 0))
(display-buffer-pop-up-window buffer alist)))
(add-to-list 'display-buffer-alist
'("\\*Buffer List\\*" my-display-buffer-pop-up-same-width-window))
A key quote regarding the ACTION functions is:
Each such FUNCTION should accept two arguments: the buffer to
display and an alist. Based on those arguments, it should
display the buffer and return the window.

How to keep dir-local variables when switching major modes?

I'm committing to a project where standard indentations and tabs are 3-chars wide, and it's using a mix of HTML, PHP, and JavaScript. Since I use Emacs for everything, and only want the 3-char indentation for this project, I set up a ".dir-locals.el" file at the root of the project to apply to all files/all modes under it:
; Match projets's default indent of 3 spaces per level- and don't add tabs
(
(nil .
(
(tab-width . 3)
(c-basic-offset . 3)
(indent-tabs-mode . nil)
))
)
Which works fine when I first open a file. The problem happens when switching major modes- for example to work on a chunk of literal HTML inside of a PHP file. Then I lose all the dir-local variables.
I've also tried explicitly stating all of the modes I use in ".dir-locals.el", and adding to my .emacs file "dir-locals-set-class-variables / dir-locals-set-directory-class". I'm glad to say they all behave consistently, initially setting the dir-local variables, and then losing them as I switch the major mode.
I'm using GNU Emacs 24.3.1.
What's an elegant way of reloading dir-local variables upon switching a buffer's major-mode?
-- edit -- Thanks for the excellent answers and commentary both Aaron and phils! After posting here, I thought it "smelled" like a bug, so entered a report to GNU- will send them a reference to these discussions.
As per comments to Aaron Miller's answer, here is an overview of what happens when a mode function is called (with an explanation of derived modes); how calling a mode manually differs from Emacs calling it automatically; and where after-change-major-mode-hook and hack-local-variables fit into this, in the context of the following suggested code:
(add-hook 'after-change-major-mode-hook 'hack-local-variables)
After visiting a file, Emacs calls normal-mode which "establishes the proper major mode and buffer-local variable bindings" for the buffer. It does this by first calling set-auto-mode, and immediately afterwards calling hack-local-variables, which determines all the directory-local and file-local variables for the buffer, and sets their values accordingly.
For details of how set-auto-mode chooses the mode to call, see C-hig (elisp) Auto Major Mode RET. It actually involves some early local-variable interaction (it needs to check for a mode variable, so there's a specific look-up for that which happens before the mode is set), but the 'proper' local variable processing happens afterwards.
When the selected mode function is actually called, there's a clever sequence of events which is worth detailing. This requires us to understand a little about "derived modes" and "delayed mode hooks"...
Derived modes, and mode hooks
The majority of major modes are defined with the macro define-derived-mode. (Of course there's nothing stopping you from simply writing (defun foo-mode ...) and doing whatever you want; but if you want to ensure that your major mode plays nicely with the rest of Emacs, you'll use the standard macros.)
When you define a derived mode, you must specify the parent mode which it derives from. If the mode has no logical parent, you still use this macro to define it (in order to get all the standard benefits), and you simply specify nil for the parent. Alternatively you could specify fundamental-mode as the parent, as the effect is much the same as for nil, as we shall see momentarily.
define-derived-mode then defines the mode function for you using a standard template, and the very first thing that happens when the mode function is called is:
(delay-mode-hooks
(PARENT-MODE)
,#body
...)
or if no parent is set:
(delay-mode-hooks
(kill-all-local-variables)
,#body
...)
As fundamental-mode itself calls (kill-all-local-variables) and then immediately returns when called in this situation, the effect of specifying it as the parent is equivalent to if the parent were nil.
Note that kill-all-local-variables runs change-major-mode-hook before doing anything else, so that will be the first hook which is run during this whole sequence (and it happens while the previous major mode is still active, before any of the code for the new mode has been evaluated).
So that's the first thing that happens. The very last thing that the mode function does is to call (run-mode-hooks MODE-HOOK) for its own MODE-HOOK variable (this variable name is literally the mode function's symbol name with a -hook suffix).
So if we consider a mode named child-mode which is derived from parent-mode which is derived from grandparent-mode, the whole chain of events when we call (child-mode) looks something like this:
(delay-mode-hooks
(delay-mode-hooks
(delay-mode-hooks
(kill-all-local-variables) ;; runs change-major-mode-hook
,#grandparent-body)
(run-mode-hooks 'grandparent-mode-hook)
,#parent-body)
(run-mode-hooks 'parent-mode-hook)
,#child-body)
(run-mode-hooks 'child-mode-hook)
What does delay-mode-hooks do? It simply binds the variable delay-mode-hooks, which is checked by run-mode-hooks. When this variable is non-nil, run-mode-hooks just pushes its argument onto a list of hooks to be run at some future time, and returns immediately.
Only when delay-mode-hooks is nil will run-mode-hooks actually run the hooks. In the above example, this is not until (run-mode-hooks 'child-mode-hook) is called.
For the general case of (run-mode-hooks HOOKS), the following hooks run in sequence:
change-major-mode-after-body-hook
delayed-mode-hooks (in the sequence in which they would otherwise have run)
HOOKS (being the argument to run-mode-hooks)
after-change-major-mode-hook
So when we call (child-mode), the full sequence is:
(run-hooks 'change-major-mode-hook) ;; actually the first thing done by
(kill-all-local-variables) ;; <-- this function
,#grandparent-body
,#parent-body
,#child-body
(run-hooks 'change-major-mode-after-body-hook)
(run-hooks 'grandparent-mode-hook)
(run-hooks 'parent-mode-hook)
(run-hooks 'child-mode-hook)
(run-hooks 'after-change-major-mode-hook)
Back to local variables...
Which brings us back to after-change-major-mode-hook and using it to call hack-local-variables:
(add-hook 'after-change-major-mode-hook 'hack-local-variables)
We can now see clearly that if we do this, there are two possible sequences of note:
We manually change to foo-mode:
(foo-mode)
=> (kill-all-local-variables)
=> [...]
=> (run-hooks 'after-change-major-mode-hook)
=> (hack-local-variables)
We visit a file for which foo-mode is the automatic choice:
(normal-mode)
=> (set-auto-mode)
=> (foo-mode)
=> (kill-all-local-variables)
=> [...]
=> (run-hooks 'after-change-major-mode-hook)
=> (hack-local-variables)
=> (hack-local-variables)
Is it a problem that hack-local-variables runs twice? Maybe, maybe not. At minimum it's slightly inefficient, but that's probably not a significant concern for most people. For me, the main thing is that I wouldn't want to rely upon this arrangement always being fine in all situations, as it's certainly not the expected behaviour.
(Personally I do actually cause this to happen in certain specific cases, and it works just fine; but of course those cases are easily tested -- whereas doing this as standard means that all cases are affected, and testing is impractical.)
So I would propose a small tweak to the technique, so that our additional call to the function does not happen if normal-mode is executing:
(defvar my-hack-local-variables-after-major-mode-change t
"Whether to process local variables after a major mode change.
Disabled by advice if the mode change is triggered by `normal-mode',
as local variables are processed automatically in that instance.")
(defadvice normal-mode (around my-do-not-hack-local-variables-twice)
"Prevents `after-change-major-mode-hook' from processing local variables.
See `my-after-change-major-mode-hack-local-variables'."
(let ((my-hack-local-variables-after-major-mode-change nil))
ad-do-it))
(ad-activate 'normal-mode)
(add-hook 'after-change-major-mode-hook
'my-after-change-major-mode-hack-local-variables)
(defun my-after-change-major-mode-hack-local-variables ()
"Callback function for `after-change-major-mode-hook'."
(when my-hack-local-variables-after-major-mode-change
(hack-local-variables)))
Disadvantages to this?
The major one is that you can no longer change the mode of a buffer which sets its major mode using a local variable. Or rather, it will be changed back immediately as a result of the local variable processing.
That's not impossible to overcome, but I'm going to call it out of scope for the moment :)
Be warned that I have not tried this, so it may produce undesired results ranging from your dir-local variables not being applied, to Emacs attempting to strangle your cat; by any sensible definition of how Emacs should behave, this is almost certainly cheating. On the other hand, it's all in the standard library, so it can't be that much of a sin. (I hope.)
Evaluate the following:
(add-hook 'after-change-major-mode-hook
'hack-dir-local-variables-non-file-buffer)
From then on, when you change major modes, dir-local variables should (I think) be reapplied immediately after the change.
If it doesn't work or you don't like it, you can undo it without restarting Emacs by replacing 'add-hook' with 'remove-hook' and evaluating the form again.
My take on this:
(add-hook 'after-change-major-mode-hook #'hack-local-variables)
and either
(defun my-normal-mode-advice
(function &rest ...)
(let ((after-change-major-mode-hook
(remq #'hack-local-variables after-change-major-mode-hook)))
(apply function ...)))
if you can live with the annoying
Making after-change-major-mode-hook buffer-local while locally let-bound!
message or
(defun my-normal-mode-advice
(function &rest ...)
(remove-hook 'after-change-major-mode-hook #'hack-local-variables)
(unwind-protect
(apply function ...)
(add-hook 'after-change-major-mode-hook #'hack-local-variables)))
otherwise and finally
(advice-add #'normal-mode :around #'my-normal-mode-advice)

How to make flyspell bypass some words by context?

I use Emacs for writing most of my writings. I write using reStructuredText, and then transform them to LaTeX after some preprocessing since I write my citations รก-la LaTeX. This is an excerpt of one of my texts (in Spanish):
En \cite[pp.~XXVIII--XXIX]{Crnkovic2002} se brindan algunos riesgos
que se pueden asumir con el desarrollo basado en componentes, los
This text is processed by some custom scripts that deals with the \cite part so rst2latex can do its job.
When I activate flyspell-mode it signals most of the citation keys as spelling errors.
How can I tell flyspell not to spellcheck things within \cite commands.
Furthermore, how can I combine rst-mode and flyspell, so that rst-mode would keep flyspell from spellchecking the following?
reST comments
reST code literal
reST directive parameters and arguments
reST raw directive contents
Any ideas?
You can set the variable ispell-parser to the value 'tex so that flyspell will ignore (la)tex sequences. To do so, you can either set it manually in each buffer like so:
M-: (setq 'ispell-parser 'tex)
or you write a little function that will do that for you. Put the following in your .emacs file:
(defun flyspell-ignore-tex ()
(interactive)
(set (make-variable-buffer-local 'ispell-parser) 'tex))
Then you can still invoke it manually, using
M-x flyspell-ignore-tex
or you could add a hook that calls that function automatically whenever you edit a file of a certain type. You would do the latter by adding the newly defined function to your auto-mode-alist. Say your filenames typically end with ".rst", then add this line to your .emacs file:
(add-to-list 'auto-mode-alist '("\\.rst$" . flyspell-ignore-tex))
As for the second part of your question: making flyspell-mode ignore larger regions, such as, e.g., reST comments, is not easily achievable. It becomes clear when you think about the way flyspell works: it checks text on a word-by-word basis. For that, flyspell-word only looks at one word at a time which it sends to an ispell process running in the background. The ispell process does the dictionary lookup and returns whether or not the current word is correct. If flyspell-word had to check every single time whether or not the current word is part of a comment or other region that should not be checked, it would be rather slow, because that would include quite a bit of searching through the buffer.
Now of course, one could approach this a little bit smarter and first find the non-comment regions etc. and then do the word-by-word checking only in those parts that are outside of those regions - but unfortunately, that's not the way flyspell is implemented.
If you can do without the "fly" part, however, ispell-mode has a mechanism to customize which regions of a buffer can be skipped. This is done via the variable ispell-skip-region-alist. But although flyspell-mode works off ispell-mode, for the reasons outlined above that variable is not used by flyspell-mode.
You can also use flyspell-generic-check-word-predicate as I explained in this question at Super User.
(aspell's tex filter may do exactly what you want - but if you want a more general solution)
Although I am using the code below to persuade flyspell to not flag certain words with numbers in them,
you can use this sort of hook to match certain context.
looking-at starts at the position you want - so you may want to search backwards for start/end of whatever context you care about.
(when "another attempt to accept certain words flyspell/ispell/aspell flags as incorrect"
(defun flyspell-ignore-WordNumber99-stuff/ag (beg end info)
(save-excursion
(goto-char beg)
(cond
((or
(looking-at "\\bWord1\\b")
(looking-at "\\bWord99Foo\\b")
)
t)
(t nil)
)
)
)
)
(add-hook 'flyspell-incorrect-hook 'flyspell-ignore-WordNumber99-stuff/ag)