Skip to content

Instantly share code, notes, and snippets.

@kobapan
Last active April 25, 2019 05:57
Show Gist options
  • Save kobapan/ee45b2b8798cea3df9dc0d68413caf45 to your computer and use it in GitHub Desktop.
Save kobapan/ee45b2b8798cea3df9dc0d68413caf45 to your computer and use it in GitHub Desktop.
;;; Depentancy:
;;
;; e2ps
;; ps2pdf
;; sudo apt install e2ps; sudo apt install ghostscript ;
;;; Usage:
;;
;; M-x e2ps-ps2pdf
;; 保存ディレクトリを選ぶ
;; ファイル名(拡張子抜き)を選ぶ
;; リージョン選択がされている場合は、選択範囲が、
;; 選択されていない場合は、カレントバッファ全体がPDF化される
(defun e2ps-ps2pdf (&optional b e)
(interactive "r")
(let ((command-format "nkf -e | e2ps -a4 -p -nh | ps2pdf - \"%s.pdf\"")
(begin (if (region-active-p) b (point-min)))
(end (if (region-active-p) e (point-max)))
(comment (if (region-active-p) "Region to PDF - " "Buffer to PDF - "))
(directory (file-name-directory buffer-file-name))
(e2ps-ps2pdf-keymap (copy-keymap minibuffer-local-completion-map))
)
(define-key e2ps-ps2pdf-keymap [tab] 'e2ps-ps2pdf-completion)
(shell-command-on-region begin end
(format "nkf -e | e2ps -a4 -p -nh | ps2pdf - \"%s%s.pdf\""
(let ((d
(read-from-minibuffer (concat comment "Save in (" directory "):" )
nil
e2ps-ps2pdf-keymap
nil nil directory)
))
(expand-file-name
(if (eq "/" (substring d -1)) d (concat d "/"))
))
(read-from-minibuffer (concat comment "File name (*.pdf):"))
))))
(defun e2ps-ps2pdf-completion ()
(interactive)
(goto-char (point-max))
(let* ((input (buffer-substring
(save-excursion (skip-chars-backward "^:") (point))
(point)))
(base-dir (file-name-directory input))
(candidate (file-name-nondirectory input))
(completion-table (mapcar '(lambda (d) (if (file-directory-p (concat base-dir d)) d nil))
(directory-files base-dir)))
(result (try-completion candidate completion-table))
(tmpmesg '(lambda (s)
(save-excursion
(goto-char (point-max))
(save-excursion (insert " " s))
(sit-for 1)
(delete-region (point) (point-max))))))
(cond
((eq result t) (funcall tmpmesg "[Solo Completion]"))
((eq result nil) (funcall tmpmesg "[No Match!]"))
((string= result candidate)
(with-output-to-temp-buffer "*Completins*"
(display-completion-list
(all-completions candidate completion-table))))
(t (skip-chars-backward "^/")
(delete-region (point) (point-max))
(insert result)
(if (eq t (try-completion result completion-table))
nil
(funcall tmpmesg "[Complete, but not unique]")
)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment