;-*- coding: utf-8 -*-
; some elisp string replacement functions
; used for regex replace operations
; 2007-06
; Xah Lee
; ∑ http://xahlee.org/
(defun wikipedia-link-replacement ()
"Returns a canonical form of Wikipedia link from a regex match.
This function is used for query-replace-regexp, to turn the
following forms of links:
event
Middle_distance
Middle_distance_track_event
Sapir-Whorf_Hypothesis
into a cannonical form. Basically, the link text needs to have
“_” replaced by space. Also, it shouldn't match links that's
already in canonical form, nor matching non-wikipedia link texts.
The regex to be used for this function is:
\\(\\([-.A-Za-z0-9]+_\\)+[-.A-Za-z0-9]+ ?\\)
To use a function in query-replace-regexp, do “\\,(wikipedia-link-replacement)”.
."
(let (langCode articlePath linkText linkText2 returnText)
(setq langCode (buffer-substring (match-beginning 1) (match-end 1)))
(setq articlePath (buffer-substring (match-beginning 2) (match-end 2)))
(setq linkText (buffer-substring (match-beginning 3) (match-end 3)))
(setq linkText2
(replace-regexp-in-string "_" " " articlePath))
(setq returnText
(concat "" linkText2 "" ))
returnText
)
)
(defun get-html-h1 ()
"Returns the current buffer's first
tag content.
This function is used to get a set of HTML page's tag
content in sync with the tag content. This function is used
for dired-do-query-replace-regex.
The search regex should be:
\([^<]+\)
The replace string should be:
\,(get-html-h1)"
(interactive)
(let (pos1 pos2)
; move to the beginning
; search for , get position
; search for
, get position
; get the text between positions
(save-excursion
(goto-char (point-min))
(search-forward "" nil t)
(setq pos1 (point))
(search-forward "
" nil t)
(search-backward "
" nil t)
(setq pos2 (point))
)
(buffer-substring-no-properties pos1 pos2)
)
)
(defun remove-square-brackets ()
"Delete any text of the form “[…]”, including the brackets.
Work on text selection or current line.
Print out in *changed items* buffer of all removed text.
For example, if text is on the line:
the project was officially announced as Blu-ray Disc [11][12], and …
then, after the call the line becomes:
the project was officially announced as Blu-ray Disc, and …
."
(interactive)
(let (bds p1 p2 inputStr resultStr changedItems)
(setq bds (get-selection-or-unit 'line) )
(setq inputStr (elt bds 0) p1 (elt bds 1) p2 (elt bds 2) )
(setq changedItems '())
(setq resultStr
(with-temp-buffer
(insert inputStr)
(goto-char 1)
(while (search-forward-regexp "\\(\\[[0-9]+?\\]\\)" nil t)
(setq changedItems (cons (match-string 1) changedItems ) )
(replace-match "" t) )
(buffer-string)) )
(if (> (length changedItems) 0)
(progn
(delete-region p1 p2)
(insert resultStr)
(with-output-to-temp-buffer "*changed items*"
(mapcar
(lambda (ξx)
(princ ξx)
(princ "\n") ) changedItems) ) )
(message "No change.") ) ) )