📄 gsdoc.el
字号:
;; gsdoc.el
;;
;; GNU emacs (19.34) functions to help working with the HTML form of
;; Ghostscript documentation.
;;
;; Pete Kaiser 8 September 1998 V1.2
;;============================================================
;; One global key setting, which runs the function to bind some keys
;; locally -- presumably in a buffer containing HTML code. Plus that
;; function itself.
(global-set-key [?\C-\S-k] 'gskeys)
(defun gskeys ()
"Set the keys in this buffer to use with Ghostscript HTML docs."
(interactive)
(local-set-key [?\C-\S-b] 'gs-bold)
(local-set-key [?\C-\S-c] 'gs-code)
(local-set-key [?\C-\S-e] 'gs-emphatic)
(local-set-key [?\C-\S-g] 'gs-get-anchor)
(local-set-key [?\C-\S-h] 'gs-href)
(local-set-key [?\C-\S-i] 'gs-italic)
(local-set-key [?\C-\S-m] 'gs-mailto)
(local-set-key [?\C-\S-n] 'gs-name)
(local-set-key [?\C-\S-p] 'gs-put-anchor)
(local-set-key [?\C-\S-q] 'gs-quote)
(local-set-key [?\C-\S-r] 'gs-row-of-table)
(local-set-key [?\C-\S-s] 'gs-selfref)
(local-set-key [?\C-\S-t] 'gs-table)
(local-set-key [?\C-\S-u] 'gs-tag)
(local-set-key [?\C-\S-x] 'gs-example)
)
;;============================================================
;; Each of these next few functions just wraps a region in a
;; <TAG>...</TAG>, or two nested tags. Where there are two, the first one
;; is inner. See the inner function ~gsregion.
(defun gs-bold () "Make text strong (bold)."
(interactive)
(~gsregion "b"))
(defun gs-code () "Make text strong code (bold TT)."
(interactive)
(~gsregion "tt")
(~gsregion "b"))
(defun gs-emphatic () "Make text emphatic (bold italic)."
(interactive)
(~gsregion "em")
(~gsregion "b"))
(defun gs-italic () "Make text italic."
(interactive)
(~gsregion "em"))
;;============================================================
(defun gs-quote ()
"Indent a region with BLOCKQUOTE and separate it with empty lines from
surrounding text."
(interactive)
(save-restriction (narrow-to-region (region-beginning) (region-end))
(goto-char (point-min)) (insert "\n\n")
(push-mark (1+ (point-min)) t)
(goto-char (point-max))
(~gsregion "blockquote")
(insert "\n\n")
)
)
;;============================================================
(defun gs-example ()
"Make an indented literatim example BLOCKQUOTE PRE and separate it with
empty lines from surrounding text."
(interactive)
(save-restriction (narrow-to-region (region-beginning) (region-end))
(goto-char (point-min)) (insert "\n")
(push-mark (point-min) t)
(goto-char (point-max))
(~gsregion "pre")
(~gsregion "blockquote")
(insert "\n")
)
)
;;============================================================
(defun gs-get-anchor ()
"Beginning at the head of this line, pick up the next anchor name for later
use along with its HTML file name. This is useful when picking up an
anchor name from a file in one buffer and using it in another buffer
containing a different file."
(interactive)
;; From the beginning of this line find and pick up the next non-empty
;; anchor, which might, of course not be right here -- though that's how it
;; ought to be used, to pick up an anchor for immediate use. The regular
;; expression picks up only the name itself.
(beginning-of-line)
(re-search-forward "<a name=\"?\\([^\">]+\\)\"?></a>" nil t)
(setq gs-anchor (buffer-substring (match-beginning 1) (match-end 1)))
;; Get the name of this buffer, treating it as the filename.
(setq gs-anchor-file (buffer-name))
)
;;============================================================
(defun gs-href ()
"Wrap a region in an empty link and leave point in the middle of the
emptiness to write the link. Maybe some day read the URL and put it
there."
(interactive)
(save-restriction
(narrow-to-region (region-beginning) (region-end))
(goto-char (point-min)) (insert "<a href=\"#\">")
(setq HREF (- (point) 2))
(goto-char (point-max)) (insert "</a>")
(goto-char HREF)
)
)
;;============================================================
(defun gs-mailto ()
"Turn an address into a proper \"mailto:\" visually bracketed with <>."
(interactive)
(save-restriction
(narrow-to-region (region-beginning) (region-end))
(setq gs-address (buffer-substring (point-min) (point-max)))
(goto-char (point-min)) (insert "<<a href=\"mailto:")
(goto-char (point-max)) (insert "\">" gs-address "</a>>")
)
)
;;============================================================
(defun gs-tag (Tag)
"Bracket a region with some arbitrary tag read from the minibuffer, leaving
point right after the opening word of the opening tag, and the end of the
region at the end of the closing tag. Leaving point there makes it
possible, for instance, to enter additional matter in a <FONT> tag. Get to
the end of a region with ^x-^x."
(interactive "*sTag: ")
(~gsregion Tag)
(exchange-point-and-mark) (forward-word 1)
)
;;============================================================
(defun gs-toc ()
"[Re]build the table of contents by picking up all the <Hn> lines and
converting them to properly indented <UL> entries, placing the TOC within
the standard TOC markers. Note that several of the original Ghostscript
HTML files have hand-modified TOCs, so it's wise to check before running
this function. It can be run from anywhere within the HTML file.
This function relies on the specific format of the structure comments for
the table of contents, which are set by the g~marker function used in
defvars run when this package is loaded."
(interactive)
(setq g~html-buffer (buffer-name))
(save-restriction (save-excursion
(widen)
;; Since we're building the TOC, delete any current TOC. Locate the place
;; for the TOC using the standard markers, deleting everything between the
;; TOC-beginning and TOC-end markers. The new TOC is built entirely in the
;; work buffer before being copied into the HTML buffer at that point.
(goto-char (point-min))
(search-forward g~toc-begin nil t)
(next-line 1) (beginning-of-line) (setq g~toc-insert (point))
(search-forward g~toc-end nil t)
(beginning-of-line) (delete-region g~toc-insert (point))
;; Empty the work buffer by copying nothing into it.
(copy-to-buffer gs-work-buffer 1 1)
;; Now collect all the following header lines into a buffer to work on
;; them. The later append-to-buffer needs point to be in the middle of the
;; empty list, so go there before entering the work buffer.
(save-excursion (while (re-search-forward "^<h[1-6][^>]*>" nil t)
(beginning-of-line) (setq BOH (point))
(re-search-forward "</h[1-6]>\n" nil t)
(append-to-buffer gs-work-buffer BOH (point))
))
(goto-char g~toc-insert)
;; All the useful header lines should be in the work buffer now.
(save-excursion
(set-buffer gs-work-buffer)
;; Formatting as list entries: insert <ul> when the level deepens and </ul>
;; when it rises.
(goto-char (point-min))
(while (search-forward "</a>" nil t) (replace-match ""))
(goto-char (point-min))
(while (re-search-forward "</h[1-6]>" nil t) (replace-match "</a>"))
(goto-char (point-min))
(while (re-search-forward "<a name=\"" nil t) (replace-match "<a href=\"#"))
;; Change <h[1-6]> to <li>, inserting <ul>...</ul> as needed. Pick up the
;; upmost level from the first header, usually <h1>, and save a copy to
;; use to insert any terminating </ul>.
(goto-char (point-min))
(re-search-forward "^<h\\([1-6]\\)[^>]*>" nil t)
(setq First (string-to-number
(buffer-substring (match-beginning 1) (match-end 1))))
(setq Previous First)
(replace-match "<li>" t t)
;; Got the first one, now handle the rest.
(while (re-search-forward "^<h\\([1-6]\\)[^>]*>" nil t)
(setq This (string-to-number
(buffer-substring (match-beginning 1) (match-end 1))))
(setq Hold This)
(replace-match "<li>" t t) (beginning-of-line)
;; No point being too fancy with conditionals: the "while" statements here
;; make at most one logically unnecessary test.
(while (> This Previous) (insert "<ul>\n") (setq This (1- This)))
(while (< This Previous) (insert "</ul>\n") (setq This (1+ This)))
(setq Previous Hold)
)
;; Done with the loop. Clean up by inserting at the end any </ul> needed
;; to get back to the top level.
(goto-char (point-max))
(while (> Previous First) (insert "</ul>\n") (setq Previous (1- Previous)))
;; Finally add the trailing whitespace and leading whitespace and header line.
(insert "</ul></blockquote>\n\n")
(goto-char (point-min))
(insert "\n<h2>Table of contents</h2>\n\n<blockquote><ul>\n")
;; The TOC is now entirely built in the work buffer. Move it to where it's
;; supposed to be in the original buffer.
(append-to-buffer g~html-buffer (point-min) (point-max))
)
))
)
;;============================================================
(defun gs-name ()
"Insert a name anchor at point and leave point ready to enter the anchor's
name. Anchors are always empty (that is, <a name=...></a>)."
(interactive)
(insert "<a name=\"\"></a>")
(backward-char 6)
)
;;============================================================
(defun gs-put-anchor ()
"Insert around the current region the last anchor picked up with
gs-get-anchor. This includes the HTML file name if the href is put in a
file other than the anchor."
(interactive)
(save-restriction
(narrow-to-region (region-beginning) (region-end))
(goto-char (point-min))
(insert (concat
"<a href=\""
;; Insert the filename (buffer name) picked up by gs-get-anchor only if
;; this is a different buffer.
(if (string-equal gs-anchor-file (buffer-name)) "" gs-anchor-file)
;; And finish unconditionally with the specific anchor name.
"#" gs-anchor "\">"))
(goto-char (point-max)) (insert "</a>"))
)
;;============================================================
(defun gs-row-of-table ()
"Set up a row of a table from the line containing point.
Insert the right things at beginning and end, and in between convert tab
and \"|\" into column breaks with a nonbreaking space in between -- which
means that no entry can contain a tab or \"|\". Format the HTML nicely
for readability.
Between each two substantive columns this function puts a column containing
a single nonbreaking space to provide a visual break. Generally in the
first row of a table those columns should be given enough NBSPs to make
the table look right on screen and when converted to text, but this has to
be done by hand."
(interactive)
(save-restriction
(end-of-line) (setq EOL (point))
(beginning-of-line) (narrow-to-region (point) EOL)
(insert "<tr valign=top>\t<td>")
(while (re-search-forward "[|\t]" nil t)
(replace-match "\n\t<td> \n\t<td>" t t))
(goto-char (point-max))
)
(next-line 1) (beginning-of-line)
)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -