Skip to content

Instantly share code, notes, and snippets.

@DerekV
Last active December 13, 2015 17:29
Show Gist options
  • Save DerekV/4948323 to your computer and use it in GitHub Desktop.
Save DerekV/4948323 to your computer and use it in GitHub Desktop.
emacs scripting... Wanting to get better at Emacs, so I'm going to save any interesting elisp snippets I get working here as I work
regex to insert a header above lines sharing a first letter
Query replace regexp (default \(^\([[:upper:]] \).*^J\(^\2.*^J\)*\) -> ^J^J\2^J^J\1):
;; quick start some formulaic file
(defun chart-template ()
(interactive)
(insert-file "../templates/css_header_1.html")
(goto-line 13)
(insert-file "../templates/menu_header.html")
(nxml-mode)
(html-autoview-mode)
(fixup-buffer)
(search-forward "</body>")
(move-to-beginning-of-line)
(previous-line)
)
;; cleanup, reformat current buffer
(defun fixup-buffer ()
"fixup whole buffer"
(interactive)
(delete-trailing-whitespace)
(indent-region (point-min) (point-max) nil)
(untabify (point-min) (point-max)))
;; memorize a common regex replace
(defun cert-build-list-items ()
(interactive)
(let ((case-fold-search nil))
(save-excursion
(beginning-of-buffer)
(while (re-search-forward "\\([CDSVQM]\\)\\s-+\\([[:xdigit:][:upper:]]\\{4\\}\\)\\s-+\\([^[:space:]].*\\)$" nil t)
(replace-match "<li><a href='/obidocs/obi/cert_pdf/ppo/\\2.pdf' target='_blank'>\\3</a></li>" t)))))
;; these two uniquify I found somewhere on the internet...
(defun uniquify-all-lines-region (start end)
"Find duplicate lines in region START to END keeping first occurrence."
(interactive "*r")
(save-excursion
(let ((end (copy-marker end)))
(while
(progn
(goto-char start)
(re-search-forward "^\\(.*\\)\n\\(\\(.*\n\\)*\\)\\1\n" end t))
(replace-match "\\1\n\\2")))))
(defun uniquify-all-lines-buffer ()
"Delete duplicate lines in buffer and keep first occurrence."
(interactive "*")
(uniquify-all-lines-region (point-min) (point-max)))
(fset 'cert-move-profserv-up
(lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([134217790 return 134217788 167772179 94 83 13 1 67108896 167772179 94 91 94 83 93 13 23 167772178 94 67 13 5 return 25 backspace backspace] 0 "%d")) arg)))
(defun sort-lines-whole-buffer ()
(interactive)
(save-excursion
(mark-whole-buffer)
(sort-lines nil (region-beginning) (region-end))))
(defun cert-do-grouping ()
(interactive)
(save-excursion
(beginning-of-buffer)
(while (re-search-forward "\\(^\\([[:upper:]] \\).*\n\\(^\\2.*\n\\)*\\)" nil t)
(replace-match "\n\n\\2\n\n\\1" t))))
(defun cert-html-wrap ()
(interactive)
(save-excursion
(beginning-of-buffer)
(insert "<!--#include file=\"include_certs_header.html\" -->")
(if (re-search-forward "^C " nil t)
(replace-match "<ul class= \"arrow\"><h2>Hospital Services</h2>" t))
(beginning-of-buffer)
(if (re-search-forward "^S " nil t)
(replace-match "</ul>\n<ul class= \"arrow\"><h2>Professional Services</h2>" t))
(beginning-of-buffer)
(if (re-search-forward "^D " nil t)
(replace-match "</ul>\n<ul class= \"arrow\"><h2>Drug Services</h2>" t))
(beginning-of-buffer)
(if (re-search-forward "^Q " nil t)
(replace-match "</ul>\n<ul class= \"arrow\"><h2>Dental Services</h2>" t))
(beginning-of-buffer)
(if (re-search-forward "^V " nil t)
(replace-match "</ul>\n<ul class= \"arrow\"><h2>Vision Services</h2>" t))
(end-of-buffer)
(insert "</ul>\n<div id=\"min-content-spacer\" > &nbsp;</div>\n")))
(defun cert-build-from-raw ()
(interactive)
(save-excursion
(sort-lines-whole-buffer)
(cert-move-profserv-up)
(cert-do-grouping)
(cert-build-list-items)
(cert-html-wrap)
(html-mode)
(fixup-buffer)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment