This emacs thingie is really-really growing on me, its coolness and richeness are amazing!
Ok, about the added features.
Ctrl+Shift+V
This key combination pops a menu of last copied texts so you select one quickly and it's inserted. And this pasted chunk comes on top of copied things. When I started using Assist, this, along with Alt+o and Alt+m feature quickly became my top used features. I was so missing those.
After successfully implementing the Alt+o thing, I was constantly thinking about this one. I am very new in Emacs and I know those are easy things, but not for me.
Emacs has something similar to this paste menu, called "Paste from kill menu" in the "Edit" menu. But you know, when typing, it's not convenient to reach to mouse or press menu key combinations, it must be Ctrl+Shift+V, ok? How do I add new things for emacs? I do M-x ielm and the wanderful elisp interpreter appears. Her I can do whatever I want and at the end, when everything works, I just copy and paste to my .emacs.
First thing, after some trial and error, I got
(global-set-key [?\C-\S-v] 'do-paste-menu)
key combination to set up the keys to be calling the do-paste-menu function. Among some errors was that [?\C-V] was overriding the C+v too.
now my (defun do-paste-menu() ( print "hello" )) was printing my usual hello and it was already cool :-)
Next, I found from emcas include files that there was somthing called x-popup-menu. After reading its info it turned that this cool function was all I needed. Now
(defun do-paste-menu() ( let ((y (x-popup-menu t 'yank-menu))) (print y)))
was popping up the menu - my dream menu, but when I pressed it, nothing happened. Actually the content of the selected thing was prineted in the echo area. Not bad at all, but the quick replacement of 9print y) by (insert y) was inserting a lot of attributes along with the actual text. So I gave up at the moment and returned to it a couple of days later.
Long story short, my final function looks like this:
(defun do-paste-menu()
"Show and paste from menu"
(interactive)
(let* ((y (x-popup-menu t 'yank-menu)) (z (car y)))
(when (and z (> (length z) 0))
(set-text-properties 0 (length z) nil z)
(insert z))))
As it turned out, not y, but the car of y was holding the actual text and I stored it in z. And then if (when) there was anything there (the z part of and) and it it had a length of >0, then I removed its properties (set-text-properties 0 (length z) nil z) and inserted it! And voila! My ctrl+shift+v works!!!!!!!!!! Well, not exactly. In the ideal world it should also pop the pasted text and amke it my currently pasted guy and the list also contains duplicated texts, so there is still someting to work on. Will do that eventually.
The next thing, I know I did it all wrong, the menu item itself (the y guy) seems contains the actual pasting function too (emacs coolness), but I need to read up some stuff before being able to use it.
As for now, I already have the C+S+v's preliminary implementation and I am happy enough.
Alt+m
This again is one of my most favorite featrues of Visual assist. You press alt+m, the list of functions appears of an editable combo box, you then type in some letters and it selects the function from that combo box. It's so quick and easy, after some time you do not even remember the sequence of the functions. Not only you forget it, you don't even think about it, it becomes abstracted. Now one needs to be careful with this though. Indeed if you just type in functions regardless of their places, that would fuck up the structure of the object file and in some cases even the program's performance (this is quiet an advanced topic, so will not touch for now, go read some Drepper papers for that).
Back to the emacs. Apparently emacs' speedbar is a nice feature and it parses and dynamically updates the list of functions. But again... it's interface for this particular use case is too complcated. You need to
- switch to it (alt+zero for me)
- press +
- find your function
- press enter
As a result the list remains opened and a valuable vertical space is wasted. Though speedbar's added green overlay on top of the just visited function is sweet.
It turned out emacs already has a nice way of doing this and it's called imenu. Speedbar internally uses imenu's features to construct its items. Imenu has a feature of adding a menu item "Index" to the main menu, the contents of which is what I want. Imenu also offers a way of showing a menu for the M-x imenu command. It can have three values - Mouse event, Always, Never.
If it's bound to a mouse event, a menu is indeed appears. I want keyboard, so I selected "Always". It didn't work... what a pity. Again, I ended up with the following code -
;; go-to-definition-menu
(defun my-imenu-helper()
(let (index-alist
(result t)
alist)
;; Create a list for this buffer only when needed.
(while (eq result t)
(setq index-alist (imenu--make-index-alist))
(setq result (imenu--mouse-menu index-alist t))
(and (equal result imenu--rescan-item)
(imenu--cleanup)
(setq result t imenu--index-alist nil)))
result))
(defun my-imenu() (interactive) (imenu (my-imenu-helper)))
(global-set-key [?\M-\m] 'my-imenu)
The function my-imenu-handler was taken from imenu's codes and modified to meet my needs. The original function was bound to use mouse event which made it unusable as it was. So its modified coppy appears in my .emacs. The (imenu ....) call does the goto part to the place which the handler returns.
This works, I press Alt+M and the menu appears. This is good again, but it's again not what I really wanted. I want to have the filtering method - I want a combo box solution!
Alt+g
Or open the include file!
If you do Alt+g on line
#include "myfile.h"
and your cursor is somewhere between the "", visual assist opens the myfile.h. This again is a nice feature, which cuts a lot of speedbar usage.
After some ielming, I got the following code working for me:
(defun hoper-src(env) (let ((p (getenv env))) (if p (concat (file-name-as-directory p) "hoper/src") nil)))
(defun src-path () (list "." (hoper-src "LOCAL_BUILD") (hoper-src "GLOBAL_BUILD")))
(defun line-incl-fln(str) (when (string-match "^[[:space]*#[[:space]*include[[:space]+[\"\<]\\(.*\\)[\"\>][[:space]*$" str)
(match-string-no-properties 1 str)))
(defun incl-fln() (line-incl-fln (thing-at-point 'line)))
(defun open-incl-fln(str) (let ((fln (locate-file str (src-path)))) (find-file fln)))
(defun open-include-file()
"Open the C/C++ include file under cursor"
(interactive)
(let ((fln (incl-fln))) (when fln (open-incl-fln fln))))
(global-set-key [M-\kp-multiply] 'open-include-file)
(global-set-key [M-\kp-decimal] 'open-include-file)
Some things to mention.
(src-path) returns the list of directories to search for a file. It always includes the current directory and also I've added the directories for my company's project, which I mention as "hoper" (intentional change).
The funtion line-incl-fln parses the argument of form #include "file" or and returns the FILE part (well, it also accepts "file> and < file", but it's a feature, not a bug [while blogger.com's complaining about my <file incomplete key and eating it through the end of line is indeed a bug - wtf]).
The rest is just taking the current line (thing-at-point 'line), and searching it through find-file.
I could not bind it to Alt+g, so I went with both Alt+Keypad * and Alt+"Keypad .". The . one is similar to the default Alt+. aka goto definition and the * one is similar to goto current line of the visual studio debugger. Although totally different, but somehow it looked at least familiar for me.
Off now, will continue later...