summaryrefslogtreecommitdiff
path: root/lisp/init-text.el
blob: 32105f097d9d22b2c09ac924ec83efdf423b2886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

(add-hook 'text-mode-hook 'abbrev-mode)
(add-hook 'text-mode-hook 'turn-on-auto-fill)

(defun km/export-wrapped-text (arg)
  "Export the text in current buffer as wrapped text.

This is useful for preparing text in emacs and then exporting to
a wrapped buffer for pasting text (e.g., into a web form).

If region is active, restrict export to the region. If ARG is
non-nil, copy the region with `x-select-text'."
  (interactive "P")
  (let ((wrapped-buffer (get-buffer-create "*Wrapped export*"))
        beg end)
    (if (use-region-p)
        (progn (setq beg (region-beginning))
               (setq end (region-end)))
      (setq beg (point-min))
      (setq end (point-max)))
    (copy-to-buffer wrapped-buffer beg end)
    (switch-to-buffer-other-window wrapped-buffer)
    (while (not (eobp))
      (forward-paragraph)
      (forward-line -1)
      (km/unfill-paragraph)
      (forward-line 1))
    (when arg
      (x-select-text (buffer-substring-no-properties (point-min) (point-max))))))

(defun km/columnify-file (delim)
  "Separate current file on DELIM using column program.

By default, DELIM is set to \",\". With a single prefix argument,
use whitespace as the delimiter. With two prefix arguments,
prompt for a delimiter.

If a columnified buffer already exists, just switch to it."
  (interactive (list (cond ((not current-prefix-arg) ",")
                           ((> (prefix-numeric-value current-prefix-arg) 4)
                            (read-string "Delimiter: "))
                           (t nil))))
  (let* ((bufname (buffer-name))
         (output-buffer-name (concat "*cols: " bufname "*"))
         (output-buffer (get-buffer output-buffer-name))
         (fname (file-relative-name buffer-file-name))
         (col-args '("-t")))
    (unless output-buffer
      (setq output-buffer (get-buffer-create output-buffer-name))
      (when delim
        (add-to-list 'col-args (format "-s'%s'" delim)))
      (apply 'call-process "column" fname output-buffer nil
             col-args))
    (switch-to-buffer output-buffer)))

(provide 'init-text)