Emacs Lisp Blog Archive - Dec 2015

Posts Below
12/1/2015 - Emacs Lisp code: Easily Search for Text You've Selected
12/2/2015 - Emacs Lisp code: Right-Click to Highlight and Select Name - Sort of Like Notepad++
12/4/2015 - Emacs Lisp code: Mouse-Clickable Search Results in Helm (with some glitches)
12/15/2015 - Emacs Add-On: multifiles-apmod.el (Add-On)


   ▲ Top  ▼ Bottom  △ TOC   ↓ Down
Emacs Lisp code: Easily Search for Text You've Selected
Tuesday, December 1st, 2015
13:42:54 GMT

(Updated at 9:27 AM EST.) I finally ran across a nice, slick way to be able to immediately search for the text I've selected in Emacs.

Most good text editors have something like that feature by default - but not Emacs, which was another maddening nuisance for me until today.

I found two solutions in this StackOverflow page. I prefer this solution, reproduced below.

I prefer the above because it even makes backward searching with C-r work right, not just forward searching with C-s, and acts normal when you search without selecting any text.

The original solution I tried only fixes forward searching, and erroneously selects blocks of text if you try to search without selecting any text. The second "search-selection" function from this StackOverflow post, reproduced below, along with the commands I added to my .emacs file to change the keychord C-s to make it use the "search-selection" function instead of "isearch-forward":

Prior to finding the above solutions in this StackOverflow post, the closest thing I had to a solution was the excellent highlight-symbol.el add-on.

Th highlight-symbol.el add-on is still very useful to me. The way I have it set up, I can press a single key - F4 - to search for whatever word (or function name, or whatever - even if it contains dashes and/or underscores) the point is resting inside. I can press Ctrl-F3 to easily replace that text. And I can press F3 to highlight that text throughout a buffer.

But, the highlight-symbol.el add-on doesn't provide a way to search for any arbitrary selected text, since that add-on doesn't require you to select text, nor pay any attention if you do select some text.

I'm still looking for a way to easily replace selected text, and will update this post if/when I find a way.

   ▲ Top  ▼ Bottom  △ TOC   ↓ Down

   ▲ Top  ▼ Bottom  △ TOC   ↓ Down   Up ↑
Emacs Lisp code: Right-Click to Highlight and Select Name - Sort of Like Notepad++
Wednesday, December 2nd, 2015
06:47:22 GMT

The text (and code) editor Notepad++ has a nice feature where all you have to do to temporarily highlight a name (such as a variable name, function name, command name, or other words) everywhere is double-click on it.

Emacs doesn't do that out of the box, and I didn't find a way to make Emacs behave exactly like Notepad++.

But, I like the below behavior more in some ways anyway.

I'm using GNU Emacs 24.5.1. The below requires three add-ons:

Here's all the code I had to add to my .emacs settings file:

Sorry, I don't know how to get rid of the "Wrong type argument: syntax-table-p, nil" error message. But it seems to work fine despite that error message.

And the above code does interfere with whatever right-clicking is supposed to do by default. But the original default behavior of right-click was rather dangerous and useless-seeming - unexpectedly selecting blocks of text and often deleting any text that was already selected - so I'm glad to be rid of that, whatever that was.

After adding the above code and add-ons to your Emacs, it should be possible to right-click on a name to highlight that name everywhere persistently.

The highlighting persists forever until you either right-click on the name again, or run the (highlight-symbol) function in some other way, such as clicking on the name and pressing F3 - the key I've bound to (highlight-symbol).

Right-clicking not only highlights the name, but also selects (or "marks", in Emacs jargon) some, most, or all of the name under your cursor (or "point", in Emacs jargon).

If you right-clicked on a dash or underscore in the name, the entire (or almost entire) name gets selected. Excluding any dollar signs at the beginning, to make it easier to select the names of PHP variables without their $ prefix. (But if you ever actually want to also select the dollar sign, a normal left double-click on a PHP variable name, or an underscore inside it, should accomplish that.)

If you right-clicked anything other than a dash or underscore, the only thing that gets selected is the word you clicked. So, you can easily select individual words in a function or variable name with dashes or underscores in it - something I never figured out how to do in Notepad++.

However, in Emacs, I don't know how to make it possible to so easily persistently highlight (as opposed to select) just individual words, because highlight-symbol.el pays no attention to what text you have or haven't selected - it just highlights the entire name it detects where your cursor (point) is.

But, doing a search while using the code from this previous blog post - Emacs Lisp code: Easily Search for Text You've Selected - will let you easily and case-insensitively put a temporary highlight on any arbitrary currently-selected text.

   ▲ Top  ▼ Bottom  △ TOC   ↓ Down   Up ↑

   ▲ Top  ▼ Bottom  △ TOC   ↓ Down   Up ↑
Emacs Lisp code: Mouse-Clickable Search Results in Helm (with some glitches)
Friday, December 4th, 2015
14:24:50 GMT

Helm is one of the most useful add-ons I've found for the GNU Emacs operating system - uh, I mean, text editor - so far.

Helm makes it tremendously easier to find all sorts of things such as files, buffers, function names, variable names, and text within files or buffers. In many Helm modes, you can just type a few letters, and Helm will narrow down your search and instantly display updated results.

Helm also gives you a way to browse and search your "imenu", which is where php-mode puts a list of functions defined in the PHP file (or multifile!) you're currently looking at.

And you can easily go to any of those functions by selecting its name in the Helm search results and pressing Return or Ctrl-j. Or, thanks to the below code, by double-clicking or right-clicking on any item in the Helm search results with your mouse.

By default, the Helm add-on has no mouse capabilities at all. So, I kept accidentally trying to click Helm results, resulting in nothing happening.

That seemed like the sort of thing someone probably already wrote an add-on to fix, so I searched the web, and soon found this post on Reddit:

Select helm candidate by using mouse (click)

To my delight, that code worked quite well out of the box. But, it globally overwrote my preferred mouse click settings, so, I had to figure out how to make some custom mouse click settings that would only be in effect while Helm was running.

Even though my final result wasn't very much code in the end, it still took me hours and lots of trial and error to figure out how to do it, and I wasn't sure I ever would figure it out. But somehow, I managed to cobble together the below code, which uses most of the code provided in the abovementioned Reddit post, plus some other code to create a custom minor mode which is supposed to only run when Helm is active.

Since I'm such an Emacs newbie, it would have taken quite a lot longer if I hadn't been able to stand on the shoulders of numerous giants, such as the author of the code in the abovementioned Reddit post, and all the authors of all the web pages and documentation I read that helped me figure out how to do this.

Thanks to everyone!

I still haven't figured out how to make the below code not get confused by mouse-wheel scrolling, and there could be other glitches too.

But, it still makes Emacs and Helm more cozy and intuitive to use.

I'm using GNU Emacs 24.5.1. And I'm not sure what version of the Helm add-on - all I know is I downloaded it on Nov. 13, 2015 at 6:22:26 AM EST from GitHub.

The below code only requires the Helm add-on, and can just be copied and pasted into your .emacs settings file, probably somewhere after the place in your settings code where you loaded Helm.

While trying to debug the above, I figured out that one glitch I had actually wasn't caused by the above.

I figured out that the Purpose add-on interferes with Helm by somehow making Helm open help text in the wrong window pane - usually Helm's own window pane - which accidentally kills Helm sessions. The only solution to that I found so far was to turn off the Purpose add-on.

Edit, 2:47/3:08 PM EST. Added some missing backslashes to the above code. And changed some wording.

By the way, I actually often make small, usually unmentioned edits to my blog posts after I publish them - but this edit seems important to point out because the lack of those backslashes probably could have stopped the code from working properly.

I guess the vanishing backslash problem is another thing I need to fix in my WordsPlatz blog software. WordsPlatz has served me well with scarcely any changes since 2009, but, I actually am still interested in updating it and improving it.

   ▲ Top  ▼ Bottom  △ TOC   ↓ Down   Up ↑

   ▲ Top  ▼ Bottom  △ TOC   Up ↑
Emacs Add-On: multifiles-apmod.el
Tuesday, December 15th, 2015
21:12:56 GMT


I finally managed to make most of the modifications and additions I wanted to make to Magnar Sveen's incredibly useful "multifiles.el" add-on for GNU Emacs.

Here's multifiles-apmod.el on GitHub:


(Addition, Jan. 10, 2016, 4:52 PM: Updated to Version 2.0, which partway fixes a really bad glitch I discovered today.)

Addition, 12/21/2015, 2:07 AM EST: Recently figured out and uploaded a slightly better solution to the slowness of my large PHP multifiles - perly-php-mode.el, which is simply perl-mode.el slightly modified to masquerade as php-mode. 12/24/2015, 5:24 AM EST: Released version 1.2.)

It's so nice finally being able to mindlessly scroll around my entire projects (at least the smaller projects) all in one page. I don't have to do searches nor even click tabs to get around.

Now, my difficulty remembering which files I put whatever things into won't get in my way as much anymore. If I space out and forget something, I can just daydreamily float around until something scrolls by which reminds me. Much more effortless and relaxing than having to click from tab to tab, or try to remember what I named things so I can do searches.

And searching is easier too, with everything apparently being in the same file.

Someday, I'd like to have an even more bizarre code editor with a concept map layout similar to VUE, or something like Code Bubbles (which I still haven't tried yet but which seems really cool, from what I've read/seen).

But, I think multifiles-apmod.el and GNU Emacs will already suffice to make it tremendously easier and more comfortable for me to get most any of my projects done.

Hopefully others will find it useful too. Perhaps even non-programmers might like it, such as anyone who likes Scrivener's Scrivenings mode.

So, I guess now I can finally return to working on my usual projects again.

I still haven't 100% perfected my Emacs setup, but, it's already mostly more comfortable for me to use than even my previous favorite editors, Notepad++ and Geany.

So, I think this might be the beginning of a golden age or renaissance for my programming hobby (or maybe almost career).

Tremendous thanks to everyone who made this possible!

   ▲ Top  ▼ Bottom  △ TOC   Up ↑