minibuffer input: word completion with custom dictionary - emacs

I am trying to accept user input for a command-line utility in emacs. I've got a handful of words that I can use in this command-line (something like the possible target list of a make invocation), and I want to be able to auto-complete words that I know about, allow user to input more than one entry in my dictionary, and also allow the user to write things not in my dictionary. Some library that allows word completion in minibuffer using a custom dictionary would be just the thing.
I do not require a full solution, but a few pointers on where to start looking would be much appreciated. Also, I'd rather avoid using intrusive libraries such as icicles or ido if at all possible - I don't want the users of this package to be limited in how they configure the rest of their setup.
My best solution so far is to use completing-read multiple times for each target until the user enters the empty string.
Solution
event_jr's answer below did the trick. The final code I've used looks like:
(require 'crm)
(let ((crm-separator " ")
(crm-local-completion-map (copy-keymap crm-local-completion-map)))
(define-key crm-local-completion-map " " 'self-insert-command)
(completing-read-multiple "prompt: " '("foo" "foobar" "baz"))))

How about this:
(completing-read-multiple ": " '("foo" "foo2" "foobar"))

Related

Configuring Org-Gcal to have subheadings within Spacemacs

this is a bit of a complicated case, so I will try to be as brief as possible.
I would like to use sub headings (level 2+) in org-gcal within Spacemacs. However, the sync currently only allows for level 1 syncing. However, I have manually tested replacing lvl 2 headers ** and replaced with -- (start at the beginning of a line or ^). This allows org-gcal to sync the sub-headers to the notes section of Google Calendar and allows me to take quick notes while out and about. My goal would be to automate a text replacement procedure of all *.org files w/i a specified directory by using a single custom command and ...
(a) convert all ** # beginning of lines to -- on all files w/i specified directory (b) run org-gcal-sync to sync the modified files (c) once files are synced, reverse process and convert all -- back to ** and allow for org-mode functionality of column view, etc.
Specifically, I am using the following packages... 1) https: //github.com/sineer/spacemacs-calendar-layer (implementation of org-gcal in Spacemacs) 2) http: //melpa.org/#/xah-find (for text replacement)
I have narrowed the code down to using either xah-find-replace-text or (preferably) xah-find-replace-text-regex. However, I am pretty sure I am having issues with regex, scope, and lisp commands in general. Details are below. To be clear, if the following is too complicated or there is a much easier solution, I am open to any suggestions. Thanks in advance!!!
(xah-find-replace-text "aaa" "bbb" "~/emacs/org/Test/." ".org\'" "y" "y" "y" "y")
Using the interactive mode (C-x C-e), I have tested the above and it will replace all instances of "aaa" with "bbb", but I would prefer to use something more along the lines of (xah-find-replace-text-regex "^**" "--" "~/pathToDir" "y" "y" "y") since the matches would be more specific to the start of a line. Please note that the regex version takes 7 parameters (compared to the xah-find-replace-text which takes 8).
Eventually, I would prefer to automate by placing something similar into my .init/.Spacemacs file...
(defun org-gcal-sync-subchildren ()
(interactive)
(xah-find-replace-text-regex "^\*\\*" "--"......etc.)
(org-gcal-sync)
(xah-find-replace-text-regex "^--" "**".......etc.))
For reference: Org-Gcal: 1) current issue: https: //github.com/myuhe/org-gcal.el/issues/43
xah-find: 1) regular text find: https: //github.com/xahlee/xah-find/blob/master/xah-find.el#L556 2) Regex text find and replace: https: //github.com/xahlee/xah-find/blob/master/xah-find.el#L642
Sorry for the broken links, but my reputation does not allow me to post more than two.
Looks like your issues is the regex.
This should work:
(xah-find-replace-text-regex "^\\*\\* " "-- " "~/emacs/org/Test/." "\\.org\\'" nil t t)
(xah-find-replace-text-regex "^-- " "** " "~/emacs/org/Test/." "\\.org\\'" nil t t)
Test them. If works, replace the nil to t (for write file)
You need double slash in elisp string for regex.

Emacs Evil mode function to Quickly Sort snippets

Basically what I'm trying to accomplish is rapid manual sorting of lots of notes and snippets of text.
What I would like to it mark a set of registers, (basically headlines in org-mode) for example
1-Tasks 2-Inbox, 3-Reference, 4-Someday
Then I want to highlight bits of a messy notes file, invoke a function, and have it kill the selection, yank it to one of the marks, and then return to the place i was before to process more snippets. So far I know I need to use:
(evil-paste-after COUNT &optional REGISTER YANK-HANDLER)
but not sure exactly how to structure the function.
It sounds like you want to use org-refile with some custom targets?

Emacs functions to read user input

I need functions to ask for user input both in: 1) Graphical and 2) non-Graphical environment in emacs.
found that I can do something like this for the non-graphical case:
(defun query-friends-phone (name)
"…"
(interactive "sEnter friend's name: ")
(message "Name: %s" name)
)
I need an input box for the Graphical case. Something like an input box to ask for something like other languages. Does Emacs have such a thing?
(to avoid confusion something like this image: http://eleganceit.com/blog/wp-content/uploads/input-form.png, this is just the idea and I know it won't be like this!)
Emacs stays away from that approach. All the text is made to be entered in the main frame. You can have dialog boxes for questions that can be answered by clicking with a mouse (see http://www.gnu.org/software/emacs/manual/html_node/elisp/Frames.html#Frames).
The GPG interface in emacs is using the approach you are suggesting for security purposes only. Having a separate window that captures all the keys no matter what makes sure that you do not accidentaly type your password in a file and save it to disk.

GNU Emacs: skeleton-mode, is it still used?

given all the possible solutions to have a template system with GNU Emacs, what do people use today ? I am still using skeleton-mode but as I read it here and there, we must be really few to do so.
What are you using and why ? (maybe I could switch to a more popular tool).
For example, given this snippet:
(define-skeleton mwe:cl-defpackage-skeleton
"Inserts a Common Lisp DEFPACKAGE skeleton."
(skeleton-read "Package: " (if v1
(file-name-sans-extension
(file-name-nondirectory
(buffer-file-name)))))
(if (setq v1 (bobp)) ";;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp;")
& (if buffer-file-coding-system
(concat " Coding:"
(symbol-name
(coding-system-get buffer-file-coding-system
'mime-charset))))
& " -*-"
& \n
& \n "(defpackage #:" str
\n "(:nicknames" ("Nickname: " " #:" str) & ")" | '(kill-whole-line -1)
\n "(:use #:CL" ((slime-read-package-name "USEd package: ") " #:" str) ")"
")" \n
\n
(if v1 "(in-package #:") & str & ")" & \n &
\n
_)
(credits: http://www.foldr.org/~michaelw/log/programming/lisp/defpackage-skeleton)
which (modern) template mode could do the same (and how ;)) ?
Cheers
For the record: even another 7 years later I am still very happy with skeletons. Lots of them available over there in my init files: https://github.com/ska2342/ska-init-files/blob/master/dot.emacs.d/init.el
I use yasnippet.
In my emacs I have this:
(require 'yasnippet-bundle)
In my hook for each mode where I want to use snippets (like my c-mode hook, etc), I have this:
(yas/minor-mode-on)
The "static" snippets I use are available, in the directory structure I use, here:
http://cheeso.members.winisp.net/srcview.aspx?dir=emacs&file=snippets.zip
You need to create the bundle .el file mentioned above, once, when any of the snippets change. do it this way:
(require 'yasnippet)
(yas/compile-bundle
; the starting point
"c:/your/path/yasnippet.el"
; the bundle file to generate
"c:/your/path/yasnippet-bundle.el"
; the snippet dir from which to generate the bundle
"c:/your/path/snippets")
That's it!
Then, when I'm in a C# file and type for<TAB>, I get a template with a for loop. And so on.
I also use yasnippet with dynamic snippet templates. A C# code-completion module I wrote calls yas/expand-snippet with a dynamically constructed string that defines the template to expand.
So, you can type
MyType.Method(<COMPLETE>
...where <COMPLETE> is the code-completion key, and the code-completion module does the lookup on the MyType.Method(, then builds a menu of choices, and pops it up. When the user selects a choice from the menu, the code-completion module builds the template, containing fields for each of the arguments for the selected method. Then it calls yas/expand-snippet and that template is injected into the buffer, just as if it had been a static template. In the dynamically-generated template, each argument to the method gets a "typeover" field, and I just fill it in, tabbing through the fields. Pretty nice.
This "dynamic snippet" idea would work with any code-completion engine. You just need a way to map from a method or function signature, like this:
function(int arg1, string arg2, char arg3)
to a yasnippet template definition string, which looks like this:
function(${1:int arg1}, ${2:string arg2}, ${3:char arg3})
And that's a pretty trivial piece of elisp.
I haven't used skeleton mode much, but I use YASnippet while coding in Ruby and C. Its pretty useful, but I suspect skeleton mode is far more powerful.
The emacs wiki lists Yasnippet as a possible replacement for skeleton. The snippets that come with yasnippet are pretty good, but you should really write your own, as the true power lies there.

Emacs how to auto-complete words of include files on C?

How can I make Emacs to complete words that are in C include files ?
#include <stdio.h>
int main(){
print//<-- this is where I want it to complete printf
What's the simplest way? (something simpler than Cedet)
First generate tags for the source and include files you'd like to be able to autocomplete for. See my blogpost for tips on using tags if you didn't use tag tables before.
Now if you have a TAGS table that includes the stdio.h, then you can autocomplete 'printf' using the command `complete-tag'.
Perhaps bind `complete-tag' to a key:
(global-set-key [f3] 'complete-tag)
Unlike complete-tag, dabbrev-expand, or hippie-expand (which does dabbrev-expand like things), the CEDET suite does exactly what the question describes. When asked to perform a completion, it looks and sees that you have included stdio.h, and then looks there for possible completions.
CEDET does a lot of other things related to completion as well which will provide very focused and correct suggestions, not just vaguely similar suggestions. A side affect is that CEDET takes more effort to setup. You need to teach it where you include files are, for example, and sometimes how to deal with macros, and what the project you are working on is like.
There is more detail on this here:
link text
You might want to try out M-/ (dabbrev-expand). This command attempts to complete the identifier immediately preceding the point (ie, where your cursor is) using the contents of the current buffer and then the contents of other buffers of the same mode. If the first completion offered isn't the one you want, just keep typing M-/. If you have the habit of keeping a single emacs session open continuously (which, if you don't have, you should really acquire), and have a handful of files from the current project open, you're quite likely to be able to find an the expansion you want for any particular prefix.
So, to answer you're original question, M-/ will find the printf completion you're looking for if (a) you've used printf anywhere else in the buffer you're editing, or (b) it appears in any other .c or .h file you have open in emacs.
You might also try hippie-expand, which has additional options regarding where it looks for completion information. I bind M-/ to hippie-expand, and then modified the order of the elements in hippie-expand-try-functions-list as follows:
(global-set-key (kbd "M-/") 'hippie-expand)
(setq hippie-expand-try-functions-list '(try-expand-dabbrev try-expand-dabbrev-all-buffers try-expand-dabbrev-from-kill try-complete-file-name-partially try-complete-file-name try-expand-all-abbrevs try-expand-list try-expand-line try-complete-lisp-symbol-partially try-complete-lisp-symbol))
This makes hippie-expand act like the normal M-/ at first, but repeated presses will yield more possible expansions.