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

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.

Related

How can I treat certain patterns as web-site links, that can be followed with org-open-at-point?

At work I frequently deal with support issues that my company keeps track of in a web-based bug tracker.
Each issue has an URL that looks like https://mycompany.com/support/SUPPORT-12345, but of course I don't want to spell this out every time I mention a support issue in my Org-mode file. I would like set up Org-mode in such a way that the pattern SUPPORT-(\d+) is treated as a hyperlink to https://mycompany.com/support/SUPPORT-\1.
I would like to be able to place my cursor over the SUPPORT-2345, type C-c C-o, and have Emacs point my browser to https://mycompany.com/support/SUPPORT-2345. Ideally, SUPPORT-2345 would behave no different than a hyperlink.
Can Org-mode be configured in this way? If not, what is the best alternative?
You should be able to do this with org-link-abbrev-alist. For example, see the below for some I use. You can then put [[Support:1234]] in your Org-mode file and have it treated as the expanded link.
(setq org-link-abbrev-alist
'(
("DOI" . "http://dx.doi.org/")
("FreshDesk" . "https://xyz.freshdesk.com/support/tickets/")
("JIRA" . "https://jira.apps.monash.edu/browse/")
("Support" . "https://support.xyz.com/helpdesk/tickets/")
("ISBN" . "http://isbn.nu/")))
I don't know whether Org-mode have this feature, but there is a bug-reference-mode for it.
Here is sample org file:
;; Local Variables:
;; eval: (bug-reference-mode)
;; bug-reference-bug-regexp: "\\(\\(?:\\(?:SUPPORT\\|support\\)-\\)\\([0-9]+\\)\\)"
;; bug-reference-url-format: "https://mycompany.com/support/SUPPORT-%s"
;; End:
* SUPPORT-123
* support-1234
And you can move point on support-* and press C-c RET (bug-reference-push-button) to open it.
For more detail about Bug Reference, please refer to C-h r g Bug Reference

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.

How can I define auto-indent and auto-pairing in .emacs file for emacs 24.3.1?

Just FYI, I am new to the .emacs file.
I would lik to set up my .emacs file to auto-indent and auto-pair a certain way to make writing code a little faster. I have found some info as to how to do these things independently but I'm not sure how to put it all together for the emacs version that I have. Ultimately, I would like to set up these definitions specific to which ever language I am coding in. Just to get me started I will use java as an example.
Obviously auto-pairing for ", (, ' are pretty straigforward. I would just like it to auto insert a closing ", ), ' and place the cursor in the middle.
For {, I would like it auto insert two newlines and the closing } whith the cursor in the middle.
Example
while (true) {
<--- cursor would be here with auto-indent of 2 spaces
}
I would also like this to work for nested curly braces which the appropriate indentation.
Example
while(true) {
if (...) {
}
}
Here is what I have so far in my .emacs file:
(defun java-autoindent ()
(when (and (eq major-mode 'java-mode) (looking-back "[{;]"))
(newline-and-indent)))
(add-hook 'post-self-insert-hook 'java-autoindent)
Obviously this just inserts a line and auto indents, but I also want the closed } to be included on the line below. I also tried using electric-pair but that didn't work.
My wish list may be a little unrealistic. I'm not even sure that this is possible, but I would be happy with the closest that I could get.
Any help to get me going in the right direction would be greatly appreciated.
Emacs defines modes for each type of language you code in. Some modes are derived from others and there is a mode called prog-mode which most programming modes are derived from.
The mode for a language is where things like indentation are defined because these tend to be language specific. The rules for indentation can be quite complicated, which is why people often use a mode with similar indentation style as the parent and derive a new mode from that.
Have a look at modes and derived mode in the emacs elisp manual.
With respect to adding matching/closing delimiters, have a look at electric-pair-mode (I think it was in emacs 24.4 - I'm running 25 and forget when it was introduced).
With respect to your requirement to enter some code, some newlines and position the cursor in a specific place, you probably want to look at one of emacs' template solutions. yasnippet is a popular choice and it is easy to define new templates in it. There are also many existing packaged yasnippet templates you can download/install. If you don't like yasnippet, google emacs template and have a look there are quitre a few frameworks.

Emacs lisp highlighting

I have wrote simple macro to define interactive function and bind it to key at same time
(That what my previous question related)
The only thing, that annoy me, that it looks ugly without highlighting.
It called such way:
(define-and-bind-command foo "C-x £" (message "Hello world"))
I want define-and-bind-command and foo be highlighted.
Well, to get define-and-bind-command highlighted, I can use wrapper around defmacro,
altho is not so pretty, and I have no idea about foo.
I know it is possible, because argument of require is highlighted with const face.
Or, probably, I am inventing wheel, and there is an another lisp mode with more
advanced highlighting?
highlighting the 'define-and-bind-command' can be achieved very easily using 'font-lock-add-keywords', usage would be
(defun my-elisp-mode-keywords()
(font-lock-add-keywords nil
'(
("\\<\\(define-and-bind-command\\)" . 'font-lock-keyword-face)
)
)
)
Of course you could change the 'font-lock-keyword-face' to any face of your liking or create and use your own face. You can find the name for a face already used very easy by moving the point (cursor) over the font-locked part of text and entering 'describe-face' followed by a return.
Edit2: Oh and of course you need to hook that defun to some hook, for elisp mode that would be:
(add-hook 'emacs-lisp-mode-hook 'my-elisp-mode-keywords)
Highlighting the foo part can be done using a regexp.
Unfortunately I can not yet help you with that part because I'm not sure how to match a regexp containing the 'define-and-bind-command' but highlighting only the word after.
Edit: unless you want to highlight both the 'define-and-bind-command' and the foo part in the same face, then it should be very easy. But I guess you want them to have different faces?
Edit3: Corrected my code, stackoverflow ate some braces before...
Edit4: Okay, I have a solution for matching the second part only, I have not tested it extensively but it seems to work. I have allowed for foo to contains any character but a space (and newline I think), I guess one could restrict that event futher to a-zA-z0-9 and "-", so feel free to change that to your liking. The Code responsible for matching only the foo part is
("\\bdefine-and-bind-command\s\\([^\s]*\\)" 1 'font-lock-function-name-face t)
Insert that in the line after the other font-lock keyword in the above function and you're good to go. The regexp matches 'define-and-bind-command' which must begin a word (that's the \b for) followed by a space and then it returns (font-locks) everything followed until a space exists.
Hope this helps!
font-lock-add-keywords can take a symbol, the mode to which to apply the new keywords. So you could do
(font-lock-add-keywords
'emacs-lisp-mode
'(("\\<\\(define-and-bind-command\\)" . 'font-lock-keyword-face)))
The disadvantage compared to using a function and a hook is that this doesn't work for derived modes; that is, if you have a mode which is derived from emacs-lisp-mode, it will not inherit these keywords.

minibuffer input: word completion with custom dictionary

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"))