PascalVKooten
PascalVKooten

Reputation: 21461

Lisp: creating a macro to cycle through a created list per document

Let's say I want to create a new document, and cycle quickly through a list. If it's only for one "word", I think there should be a general way to do this.

For instance:

"blue orange red yellow black white"

Does anyone know a way how to cycle through those items when I create:

\begin{orange}

... and I want to press a key to cycle through this list, replacing orange with the next item on the list (doing this procedure in the opposite direction wouldn't be hard then)?

I tried many different ideas with macro's (placing the list on the top of the document, and doing a whole bunch of i-searches), but that doesn't cut it.

I'd be willing to put the list in an elisp file, though I have no clue how to use that variable from elisp in, let's say, a LaTeX document (.tex).

Upvotes: 4

Views: 271

Answers (2)

PascalVKooten
PascalVKooten

Reputation: 21461

EDIT: I translated the macro into a function.

Here is a way I did it. I created a file called "list.list" where my "lists" are saved. I saved the LaTeX templates for Beamer in there. I inserted them like this:

Antibes Bergen Berkeley Berlin ..... Antibes

Note that you should always put the first entry in twice to allow it to loop.

Here is the code:

(defun cycle-list-word ()
  (interactive)
   (right-word)
   (backward-kill-word 1)
   (find-file "/emacs-24.1/list.list")
   (search-forward (substring-no-properties (car kill-ring)) nil t)
   (right-word)
   (backward-kill-word 1)
   (bury-buffer)
   (yank)
)

Upvotes: 1

user797257
user797257

Reputation:

Well, this might be possible, but depends on how much effort you are willing to put into writing eLisp code to make it work. It's not possible by just some configuration option. I would be looking into extending autocomplete by adding new sources to it, something like:

(defvar tex-tag-ac-sources
  '((init . tex-tag-ac-init)
    (requires . 0)
    (candidates . tex-tag-ac-candidates)
    (document . tex-tag-ac-documentation)
    (match . tex-tag-completion-filter)
    (prefix . tex-tag-ac-prefix-matcher)
    (symbol . "s"))
  "The source generator for autocompletion needed for interaction 
with auto-complete")

Where tex-tag-ac-candidates, tex-tag-ac-documentation, tex-tag-completion-filter and tex-tag-ac-prefix-matcher are function that do autocompletion. I.e. init function is called once when the autocompletion process starts for a specified prefix. It's called w/o arguments. The candidates is the function that is responsible for showing the filtered list of candidates, it's called w/o arguments, you would filter the candidates in the filter function, it is called with the prefix collected so far and the list of candidates so far. Lastly, the matcher function is invoked on the text of the file to see if the completion is needed at point. So, if it returns t, the init is called, and then loops through filter-candidates as you type.

While this is a bit involved... you'd definitely have a completion for anything you want. Obviously, if those functions in source are defined by you, then, if you wanted to, you could read completion arguments dynamically or have them generated dynamically in some way.

Ah, you would add the sources to autocomplete by something like:

(auto-complete (list tex-tag-ac-sources))

if doing it on per call basis, or

(setq ac-sources (list tex-tag-ac-sources <other sources>))

You can find more info here: http://cx4a.org/software/auto-complete/manual.html#Using_Source

Upvotes: 2

Related Questions