summaryrefslogtreecommitdiffstats
path: root/.emacs.d/lisp/bandali-po.el
blob: a31fee2cd905ef65ca9b499da9c6da4eac8ed2ec (plain) (blame)
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
57
58
59
60
61
;;; bandali-po.el --- bandali's po-mode setup       -*- lexical-binding: t; -*-

;; Copyright (C) 2021  Amin Bandali

;; Author: Amin Bandali <bandali@gnu.org>
;; Keywords: i18n gettext

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; My po-mode setup for editing PO translation files in GNU Emacs.

;;; Code:

(with-eval-after-load 'po-mode
  ;; based on the `po-wrap' function from the GNUN manual:
  ;; https://www.gnu.org/s/trans-coord/manual/gnun/html_node/Wrapping-Long-Lines.html
  (defun b/po-wrap ()
    "Run the current `po-mode' buffer through `msgcat' to wrap all
lines."
    (interactive)
    (when (eq major-mode 'po-mode)
      (let ((tmp-file (make-temp-file "po-wrap."))
            (tmp-buffer (generate-new-buffer "*temp*")))
        (unwind-protect
            (progn
              (write-region (point-min) (point-max) tmp-file nil 1)
              (if (zerop
                   (call-process "msgcat" nil tmp-buffer t
                                 (shell-quote-argument tmp-file)))
                  (let ((saved (point))
                        (inhibit-read-only t))
                    (delete-region (point-min) (point-max))
                    (insert-buffer-substring tmp-buffer)
                    (goto-char (min saved (point-max))))
                (with-current-buffer tmp-buffer
                  (error (buffer-string)))))
          (kill-buffer tmp-buffer)
          (delete-file tmp-file)))))

  (add-hook 'po-mode-hook (lambda nil (run-with-timer 0.1 nil 'View-exit)))
  (define-key po-mode-map (kbd "M-q") #'b/po-wrap))

(autoload #'po-mode "po-mode"
  "Major mode for editing PO translation files" t)
(add-to-list 'auto-mode-alist '("\\.po\\'\\|\\.po\\." . po-mode))

(provide 'bandali-po)
;;; bandali-po.el ends here