Disable help-menu on mouse button at a right side (with x-coord > 0x7F) - emacs

When I click, or scroll mouse wheel, and it's pointer is at the right part of the screen, where there no text, emacs shows help-menu like this:
Press PageUp key to reach this buffer from the minibuffer.
Alternatively, you can use Up/Down keys (or your History keys) to change
--/.../--
In this buffer, type RET to select the completion near point.
Possible completions are:
e==>Emacs Tutorial E==>Emacs Tutorial (choose language)...
--/.../--
a==>About Emacs A==>About GNU
=================================================================================
;; here my work buffer
=================================================================================
Help (up/down to change, PgUp to menu): e==>Emacs Tutorial
It is very disturbing, i have to press C-g to return to edit.
How to disable this feature?
P. S. I tried to press C-h k < LMB at right>, but it doesn't show the code of the key, it shows that menu again.
edit(in response to Stefan):
after pressing: aaa <LMB at right> C-g C-h l i see the following sequence:
... a a a ESC
[ M SPC \300\256 6 ESC [ M # \300\256 6 C-g C-h l
As I found, those symbols: \300\256 and 6 are dependent on the position of the cursor when i click. \300\256 is a smth like horisontal coordinate, 6 is a vertical coordinate.
I investigated it further: the menu appears when y coordinate from an ordinary symbol like a or } or DEL(actually it is the last ordinary symbol) becomes a code like \300\200( this is the first value, which cause the help-menu). As a unicode symbol table tells, DEL code is U+007F(0b01111111), and the next value will be U+00800(0b10000000) (Padding Character) which is in another table: C1 controls and Latin-1 supplement
I use GNU emacs 23.2.1, over PuTTy ssh client.

That does not sound like any of the features of Emacs in its default configuration. I suggest you try: a a a <LMB at right> and then C-h l which should show you the last few events that Emacs received. Try to figure out among these which events correspond to (they will appear right after the a a a) to see what the OS sends to Emacs when you do this .

This is actually a bug.
Here is a workaround for it:
;; Disable menubar
(menu-bar-mode -1)
;; Workaround for "mouse on menu-bar" bug
(defun transform-to-start-event (event)
(let ((y0 (second (window-inside-edges (selected-window))))
(x0 (first (window-inside-edges (selected-window)))))
(let ((x (- (car (third (second event))) #x3fff00 x0))
(y (- (cdr (third (second event))) y0))
(time (fourth (second event))))
`(down-mouse-1 ,(posn-at-x-y x y (window-at 0 0) t)))))
(defun tmm-menubar-mouse (event)
(interactive "e")
(let ((evt (transform-to-start-event event)))
(push (cons 'up-mouse-1 (cdr evt)) unread-command-events)
(mouse-drag-track evt t)))
It deactivates the menu bar, but i didn't need it anyway.
Also it doesn't handles selection, and scrolling properly, but at least, it positions point correctly.

Related

Make *Buffer List* always appear in horizontal split

I know Emacs tries to be intellectual and opens its helper buffers depending on which dimension of the window is bigger, so it may appear in vertical split window if current width is bigger than height, and in horizontal split otherwise.
But I’d prefer it to open that list always in horizontal split, because there are long paths I can’t see when the buffer is placed in vertical split. How can I do this?
I believe you've got the horizontal/vertical split terminology back to front (I can never remember which is which either), but as your goal was to retain the original window's width, I'm forcing a vertical split.
See C-hf split-window-sensibly RET. It tells you what to do:
You can enforce this function to not split WINDOW horizontally,
by setting (or binding) the variable `split-width-threshold' to
nil. If, in addition, you set `split-height-threshold' to zero,
chances increase that this function does split WINDOW vertically.
So as a permanent setting:
(setq split-width-threshold nil)
(setq split-height-threshold 0)
For just a specific function, you can advise that function (but see Edit 2 below!):
(defadvice list-buffers (around list-buffers-split-vertically)
"Always split vertically when displaying the buffer list.
See `split-window-sensibly'."
(let ((split-width-threshold nil)
(split-height-threshold 0))
ad-do-it))
(ad-activate 'list-buffers)
Edit: Actually, in this instance I suspect you're only concerned with the interactive case, in which case it's preferable to define a function and remap the bindings:
(defun my-list-buffers-vertical-split ()
"`list-buffers', but forcing a vertical split.
See `split-window-sensibly'."
(interactive)
(let ((split-width-threshold nil)
(split-height-threshold 0))
(call-interactively 'list-buffers)))
(global-set-key [remap list-buffers] 'my-list-buffers-vertical-split)
Edit 2: And Stefan points out that display-buffer-alist facilitates such things without advising functions (and of course avoiding unnecessary advice is always a good thing). I believe we still need a custom action, so:
(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))
If horizontally or vertically, presently neither split-height-threshold nor split-width-threshold seem reliable WRT to expected kind of split. Which looks like a bug, resp. design issue.
As a work-around call M-x split-window-horizontally resp. -vertically before, or advise the function with it.
You can remove (5) if you prefer not to select the window after it is displayed -- i.e., remove (select-window (get-buffer-window (buffer-name buffer))). I like the bottom window to be reserved for a 3-month calendar, so that's why I have a condition to use the window above (if it exists) -- you can remove that condition if you are so inclined. Actually, it's your function so you can modify everything as you see fit now that you see how it works. The alist would be used like this: '((window-width . 33)) if you wanted to control certain aspects of the target window, etc. I find myself always going back to this document page because it is the only scanty formal example I've found . . . and, of course, the source itself window.el: http://www.gnu.org/software/emacs/manual/html_node/elisp/Display-Action-Functions.html
(defun lawlist-list-buffers-left (&optional arg)
"Display a list of existing buffers.
The list is displayed in a buffer named \"*Buffer List*\".
See `buffer-menu' for a description of the Buffer Menu.
By default, all buffers are listed except those whose names start
with a space (which are for internal use). With prefix argument
ARG, show only buffers that are visiting files."
(interactive "P")
(lawlist-display-buffer-left (list-buffers-noselect arg) nil))
(defun lawlist-list-buffers-right (&optional arg)
"Display a list of existing buffers.
The list is displayed in a buffer named \"*Buffer List*\".
See `buffer-menu' for a description of the Buffer Menu.
By default, all buffers are listed except those whose names start
with a space (which are for internal use). With prefix argument
ARG, show only buffers that are visiting files."
(interactive "P")
(lawlist-display-buffer-right (list-buffers-noselect arg) nil))
(defun lawlist-display-buffer-left (buffer alist)
"(1) If `buffer` is already displayed, then display it again in the same window.
(2) If `buffer` is not already displayed, and if there is a window to the left,
then display that `buffer` in said window. (3) If `buffer` is not already
displayed, and if there is a window to the right, then use the selected window.
(4) If all else fails, then create a new window to the left and display `buffer` there.
(5) Select the target window which displays `buffer`."
(let (
(window
(cond
((get-buffer-window buffer (selected-frame)))
((window-in-direction 'above))
((window-in-direction 'left))
((window-in-direction 'right)
(selected-window))
(t
(split-window (selected-window) nil 'left)))))
(window--display-buffer buffer window 'window alist display-buffer-mark-dedicated)
;; OPTIONAL -- uncomment to select the target window.
;; (select-window (get-buffer-window (buffer-name buffer)))
))
(defun lawlist-display-buffer-right (buffer alist)
"(1) If `buffer` is already displayed, then display it again in the same window.
(2) If `buffer` is not already displayed, and if there is a window to the right,
then display that `buffer` in said window. (3) If `buffer` is not already
displayed, and if there is a window to the left, then use the selected window.
(4) If all else fails, then create a new window to the right and display `buffer` there.
(5) Select the target window which displays `buffer`."
(let (
(window
(cond
((get-buffer-window buffer (selected-frame)))
((window-in-direction 'above))
((window-in-direction 'right))
((window-in-direction 'left)
(selected-window))
(t
(split-window (selected-window) nil 'right)))))
(window--display-buffer buffer window 'window alist display-buffer-mark-dedicated)
;; OPTIONAL -- uncomment to select the target window.
;; (select-window (get-buffer-window (buffer-name buffer)))
))

How to show the whole line in the window?

I'm using Emacs 24.2 with a line-wrapping activated.
When I read log files of various simulations which contain messages like: "Error: ...some message...", I perform an incremental search: C-s error RET, C-s, C-s...
I find it very annoying that the highlighted result of the search (the word Error) is displayed at the bottom of the screen, and all the additional wrapped lines can't be seen:
I'd like to add modifications which ensure that the whole line of text will be displayed in the buffer, like this:
I found this question concerning re-centering of the search results. It seems that I could use the same defadvice statements for the search functions, but rather than re-centering the line I need just scroll the screen down by the number of wrapped parts.
How to do this?
You can use the solution on the question you reference, but changing recenter-top-bottom by this highly untested function:
(defun scroll-if-truncated()
(scroll-up
(/ (- (save-excursion
(end-of-line) (point))
(save-excursion
(beginning-of-line) (point)))
(window-body-width))))
After playing a bit with the code according to #juanleon's advice, I ended up with this:
;; Execute after each update in isearch-mode
(setq isearch-update-post-hook 'show-whole-line)
(defun show-whole-line ()
"Scroll such that the whole line (which contains the point) will be visible."
;; If it is the top part which is truncated
(if (not (pos-visible-in-window-p (line-beginning-position)))
(let
((amount
;; the required number of lines to scroll
(ceiling (/
(- (window-start)
(line-beginning-position))
(float (window-body-width))))))
;; don't scroll at all if the search result will be scrolled out
(if (< amount (/
(- (window-end)
(point) )
(float (window-body-width))))
(scroll-down amount)))
;; Else
(if (not (pos-visible-in-window-p (line-end-position)))
(let
((amount
(min
;; the required number of lines to scroll
(ceiling (/
(-
(line-end-position)
(window-end (selected-window) t))
(float (window-body-width))) )
;; however not to scroll out the first line
(/ (- (line-beginning-position) (window-start)) (window-body-width)))))
(scroll-up amount)))))
Few explanations:
Setting defadvice for isearch-forward is not enough - this function is not called again when you add chars to the search string. After a quick review of isearch.el.gz package I decided to advice to isearch-update function. This also eliminates the need of adding a separate advice for isearch-repeat-forward and etc. Later I noticed that there is a predefined hook in isearch-update, therefore no need for defadvice here.
show-whole-line function checks whether the beginning of the current line is visible. If not, it scrolls-down to show the beginning of the line, unless this scrolling will result in hiding the search match itself.
If the beginning of the line is visible, show-whole-line checks whether the end of the line is also visible. If not, it scrolls-up to show the end of the line, unless this scrolling will result in hiding the beginning of the line. I prefer to be able to see the beginning of the line.
This function and a hook work pretty well, but there is one annoying thing about it: the function is called when you initially press C-s (before you typed in any search string). This means that if the point is at a line which has its beginning or end out of the window, a simple invocation of C-s will result in scrolling in some manner.
While not critical at all, I'll be glad to hear suggestions how to remove the above side effect.
You should be able to get the behavior you want using variables scroll-conservatively and scroll-margin, in particular the latter.

Looking forward a way to make cursor blinks like a heartbeat in Emacs

How can I make the cursor in Emacs blink like a heartbeat. Like the LED on the front panel of laptop when the computer is suspended.
There is variable blink-cursor-alist that control the blink of cursor, but I do not know how to use it to satisfy my requirement.
Is it possible?
This simple minor mode implements a heartbeat-style blinking cursor. You can tweak heartbeat-cursor-colors to get different hue or variations thereof.
The code is tested in Emacs 24.2.1, but would be easy to port to older Emacsen.
(require 'cl)
(require 'color)
(defvar heartbeat-fps 16)
(defvar heartbeat-period 5)
(defun heartbeat-range (from to cnt)
(let ((step (/ (- to from) (float cnt))))
(loop for i below cnt collect (+ from (* step i)))))
(defun heartbeat-cursor-colors ()
(let ((cnt (* heartbeat-period heartbeat-fps)))
(mapcar (lambda (r)
(color-rgb-to-hex r 0 0))
(nconc (heartbeat-range .2 1 (/ cnt 2))
(heartbeat-range 1 .2 (/ cnt 2))))))
(defvar heartbeat-cursor-timer nil)
(defvar heartbeat-cursor-old-color)
(define-minor-mode heartbeat-cursor-mode
"Change cursor color with the heartbeat effect."
nil "" nil
:global t
(when heartbeat-cursor-timer
(cancel-timer heartbeat-cursor-timer)
(setq heartbeat-cursor-timer nil)
(set-face-background 'cursor heartbeat-cursor-old-color))
(when heartbeat-cursor-mode
(setq heartbeat-cursor-old-color (face-background 'cursor)
heartbeat-cursor-timer
(run-with-timer
0 (/ 1 (float heartbeat-fps))
(lexical-let ((colors (heartbeat-cursor-colors)) tail)
(lambda ()
(setq tail (or (cdr tail) colors))
(set-face-background 'cursor (car tail))))))))
You guess that the option has something to do with the word "blink".
So you press C-h a (for apropos) and type "blink".
On my emacs, I get two options: blink-cursor-mode and blink-matching-open.
The first one looks right. The description says: "Toggle blinking cursor mode".
The shortcut on my emacs says: <menu-bar> <options> <blink-cursor-mode>.
So I guess that the option is somewhere in the menu, maybe under "options".
I open the Options menu, and there it is: "Blinking Cursor" with a tick box.
This also sounds like an option that can be customised.
So I type M-x customize-option and then blink-cursor-mode. This allows me to toggle the value and also save it for future sessions.
EDIT: To set the interval between ON and OFF for the cursor, there is a variable called blink-cursor-interval. You can use M-x customize-variable and then blink-cursor-interval to set the interval. The variable blink-cursor-alist matches an OFF state cursor type to the ON state cursor type, and is not related to the speed of blinking.
EDIT2: As far as I know there is no way to make the cursor gradually turn off and on, because the shape of the cursor for the ON state could be different to the shape in the OFF state (so a gradual change in shape will be required).

Emacs caps lock minor mode?

Is there a command in Emacs to turn on what might be described as "caps lock minor mode"? I'm looking to do something like M-x toggle-caps-mode, then every letter I type in the buffer is a capital letter until I do M-x toggle-caps-mode again.
Note: I'm NOT looking for directions on how to swap caps and control. In reality this is because I have already done that. I am generally quite happy with it, but occasionally I'm editing code where there are a bunch of constants that are in all caps, and it gets to be a strain holding down the shift key. I'm aware of the various upcase conversion functions; I'd rather not have to type the word, select it, then run upcase-region.
If it matters, I'm using Aquamacs 2.2 w/ Emacs 23.3.1.
You don't need to type the word then select it. If you want to upcase the last word, press M-b M-u or ESC b u. Ok, you'll need to press b several times if it's a word_with_underscores.
If you really want a caps lock minor mode, try John Paul Wallington's lockcaps.el.
You can try something like this:
(define-minor-mode caps-lock-mode
"caps-lock mode"
;; The initial value.
nil
;; The indicator for the mode line.
" CAPS-LOCK"
;; The minor mode bindings.
'(("a" . (lambda () (interactive) (insert-char ?A 1)))
("b" . (lambda () (interactive) (insert-char ?B 1)))
;;etc
("A" . (lambda () (interactive) (insert-char ?a 1)))
("B" . (lambda () (interactive) (insert-char ?b 1)))
;;etc
))

Emacs: how to evaluate the smallest s-expression the cursor is in, or the following s-expression

What is a good way to evaluate the (+ 100 (+ 100 100)) part in
(+ (+ 1 2) (+ 100 (+ 100 100)))
?
For now, I do it by C-x C-e, which means I need to locate the ending parenthesis, which is difficult in most cases. Options > Paren Matching Highlighting helps, but still I need to move the cursor toward the ending parenthesis until the highlighted match is the starting parenthesis.
One way would be to have the reverse version of C-x C-e, so that I can place the cursor at the starting parenthesis like this:
(+ (+ 1 2) |(+ 100 (+ 100 100)))
and then press the appropriate keybinding.
Or I could place the cursor inside the expression, but not inside smaller expressions,:
(+ (+ 1 2) (+ | 100 (+ 100 100)))
and press a keybinding. Because aiming at a target is easier if the target is big.
How can I make such a command? Or is there one already provided?
Sidenote: bar cursor and box cursor
Emacsers who use box cursor (default) might wonder where I'm putting the cursor with the bar notation above. In emacs, you can choose box cursor or bar cursor, (bar-cursor-mode t). When the bar cursor is between the letters A and B, the box cursor is on B. So the bar is the left wall of the box.
BTW, the concept of bar cursor is useful in some unusual way:
The practice of iterating from index1 to index2-1 in programming surprises beginners. It helps to imagine index1 and index2 as indicating bars (left walls) rather than boxes.
Bind a key to one or both of these:
(defun eval-next-sexp ()
(interactive)
(save-excursion
(forward-sexp)
(eval-last-sexp nil)))
(defun eval-surrounding-sexp (levels)
(interactive "p")
(save-excursion
(up-list (abs levels))
(eval-last-sexp nil)))
Tangentially related, I highly recommend paredit for working with s-expressions. The structure editing commands and bindings make editing s-expressions a breeze. It binds C-down to up-list, so that eval-surrounding-sexp above is almost exactly the same as C-down C-x C-e (the only difference is that the function uses save-excursion to prevent movement).
You could write such a command like so:
(require 'thingatpt)
(defun eval-sexp-at-or-surrounding-pt ()
"evaluate the sexp following the point, or surrounding the point"
(interactive)
(save-excursion
(forward-char 1)
(if (search-backward "(" nil t)
(message "%s" (eval (read-from-whole-string (thing-at-point 'sexp)))))))
In Icicles there is a general way to do what you want.
By default, in the minibuffer M-. is bound to a command that inserts text at (or near) point into the minibuffer (doesn't enter it or doing else with it; just inserts it).
You can, for example, use M-: to evaluate a Lisp sexp, and then you use M-. to grab a sexp at/near point.
If you repeat M-. then it drops what it just grabbed and grabs some other kind of THING (text) at/near point and inserts that. By default, it runs through these kinds of THING, in order:
a. A Lisp symbol or file name.
b. The active region (selected text) or a word.
c. The most immediate list.
d. The next largest list.
e. The next largest list.
f. Whichever file or URL the function ffap-guesser guesses.
g. Whatever URL the function thing-at-point-url-at-point guesses.
What does this mean for your example of (+ (+ 1 2) (+ 100 (+ 100 100)))?
If point is before the 1 of the second-to-last 100, for example, these are the sexps that are consecutively inserted in the minibuffer when you hit M-. repeatedly, in order:
a. +
b. 100
c. (+ 100 100)
d. (+ 100 (+ 100 100))
e. (+ (+ 1 2) (+ 100 (+ 100 100)))?
So to insert the largest of the enclosing lists you would do M-: M-. M-. M-. M-. M-., that is, hit M-. five times.
For this behavior, in particular for the accurate grabbing of lists, you also need library Thing At Point+.
There is an inbuilt eval-defun. It's bound by default to C-M-x. It's similar to what you want but evals the top-level defun. Perhaps you can adapt that.