New target example in regular buffers - short Wikipedia links
Say you want to teach Embark to treat text of the form ‘wikipedia:Garry_Kasparov’ in any regular buffer as a link to Wikipedia, with actions to open the Wikipedia page in eww or an external browser or to save the URL of the page in the kill-ring. We can take advantage of the actions that Embark has preconfigured for URLs, so all we need to do is teach Embark that ‘wikipedia:Garry_Kasparov’ stands for the URL ‘https://en.wikipedia.org/wiki/Garry_Kasparov’.
You can be as fancy as you want with the recognized syntax. Here, to keep the example simple, I’ll assume the link matches the regexp ‘wikipedia:[[:alnum:]_]+’. We will write a function that looks for a match surrounding point, and returns a dotted list of the form ‘'(url URL-OF-THE-PAGE START . END)’ where ‘START’ and ‘END’ are the buffer positions bounding the target, and are used by Embark to highlight it if you have ‘embark-highlight-indicator’ included in the list ‘embark-indicators’. (There are a couple of other options for the return value of a target finder: the bounding positions are optional and a single target finder is allowed to return multiple targets; see the documentation for ‘embark-target-finders’ for details.)
(defun my-short-wikipedia-link ()
"Target a link at point of the form wikipedia:Page_Name."
(save-excursion
(let* ((start (progn (skip-chars-backward "[:alnum:]_:") (point)))
(end (progn (skip-chars-forward "[:alnum:]_:") (point)))
(str (buffer-substring-no-properties start end)))
(save-match-data
(when (string-match "wikipedia:\\([[:alnum:]_]+\\)" str)
`(url
,(format "https://en.wikipedia.org/wiki/%s"
(match-string 1 str))
,start . ,end))))))
(add-to-list 'embark-target-finders 'my-short-wikipedia-link)