10

Quite a few Emacs modes use (something like) forms: the customization interface is one of them, the Message mode (read: its header part) is another one; there are more of them, unfortunately not always very similar. By a "form" I mean here a buffer with read-only parts and places designated to enter text; it may or may not contain buttons (it probably should accept C-c C-c as the confirmation/committing key, irrespective of the presence of an OK button).

I am particularly interested in auto-completion in the fields (and in particular, enabling fields with lists of values from some designated set, say, comma-separated). My question is: is there any Emacs library enabling easy (by "easy", I mean not reinventing the wheel) creation of such forms, or should I code everything myself?

(I imagine that what I should do in the latter case would be (1) set some parts of the buffer read-only, (2) prepare a special major mode, so that e.g. TAB and S-TAB switch between "fields", and (3) maybe use something like Icicles or Helm for autocompletion.)

  • 1
    Changed your title to say input fields. Forms have a very special meaning in elisp, which could lead to confusion. – Malabarba Oct 27 '14 at 23:29
  • 2
    And yes, There's a built-in widget library. But I know nothing about it. – Malabarba Oct 27 '14 at 23:32
  • @Malabarba: thanks, that's probably a better wording. – mbork Oct 28 '14 at 0:33
  • (Even though Emacs itself uses the word "form" in this meaning – see the widgets manual mentioned in Drew's answer!) – mbork Oct 28 '14 at 0:41
8

See libraries cus-edit.el and wid-edit.el (and their top-level libraries custom.el and widget.el, and associated component libraries), which are included with GNU Emacs. The former makes use of the latter. The latter defines basic (and not-so-basic) form-entry thingies, called "widgets", in a hierarchy. You can use these predefined widgets and functions in the library to create your own widgets and widget types.

However, be warned that the code of these libraries, particularly that of wid-edit.el, is not very easy to follow. Your best bet is to start by reusing existing widgets or using existing widget-definition code as a guide.

(Two minor extension libraries for this are cus-edit+.el and wid-edit+.el. They also present some usage examples.)

There is an Info manual for widgets, distributed with GNU Emacs, entitled "The Emacs Widget Library".

There is also library forms.el, included with GNU Emacs, but I am not familiar with it. Perhaps someone else has something useful to say about it. There is a manual for forms, distributed with GNU Emacs, entitled "Forms Mode".

  • 2
    A little more on forms.el: stackoverflow.com/questions/10166600/… – phils Oct 28 '14 at 0:23
  • 1
    @Drew: thanks for your answer! Could you clarify the relation between \(custom\|widget\).el and \(cus\|wid\)-edit.el a bit? I find especially this part from the Widgets manual incomprehensible: (require 'widget) (eval-when-compile (require 'wid-edit)). – mbork Oct 28 '14 at 0:40
  • 1
    First, I'm no expert on this. The times when I needed to change something wrt Customize I dipped into cus-edit.el and wid-edit.el. The other files you mention are top-level files; the *-edit.el files have the code that I was interested in. Dunno much more than that; sorry. The real difficulty is the code itself, which uses a particular semi-OOP style that does not lend itself well to either Emacs self-documentation (to put it mildly) or to the Emacs debugger. You can investigate on your own, or perhaps try to contact the author (good luck with that!). Maybe others here will be some help. – Drew Oct 28 '14 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.