-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbc-mode.el
111 lines (91 loc) · 3.21 KB
/
bc-mode.el
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
;; -*- lexical-binding: t; -*-
;;; bc-mode.el --- Inferior mode for GNU bc
;; Copyright (C) 2015 Chris Zheng.
;; Author: Chris Zheng <chriszheng99@gmail.com>
;; Created: 2015-11-04
;; Version: 20151104
;; X-Original-Version: 0.1
;; Keywords: GNU bc, inferior mode, convenience
;; This file is not part of GNU Emacs.
;; 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, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Using GNU bc in Emacs.
;;; Installation:
;; Make sure to place `bc-mode.el' somewhere in the load-path and
;; add `(require 'bc-mode)' to init file.
;;; Code:
(require 'comint)
(defgroup inferior-bc nil
"Run bc in a buffer."
:group 'inferior-bc)
(defcustom inferior-bc-program "bc"
"Program invoked by `run-bc'."
:type 'string)
(defcustom inferior-bc-startup-args '("-i" "-l")
"List of command line arguments for the inferior bc process."
:type '(repeat string))
(defcustom inferior-bc-buffer "*Inferior BC*"
"Name of buffer for running an inferior bc process."
:type 'string)
(defun remove--truncate-slash (string)
(with-temp-buffer
(insert string)
(while (search-backward "\\
" nil t)
(replace-match ""))
(buffer-string)))
(defun run-bc (expr &optional replace)
"Calculate the `expr' using GNU bc.
If called interactively, the user is asked for input. If called on
region the selected expression is used as input. By default display
output in temp buffer `*BC Output*'. With prefix, insert the output."
(interactive
(list (if (use-region-p)
(buffer-substring-no-properties
(region-beginning)
(region-end))
(read-string "Expression: "))
current-prefix-arg))
(let ((buffer (get-buffer-create inferior-bc-buffer))
(output)
(try 30)
(pt))
(with-current-buffer buffer
(unless (comint-check-proc buffer)
(apply 'make-comint
(substring inferior-bc-buffer 1 -1)
inferior-bc-program nil inferior-bc-startup-args)
(set-process-filter (get-buffer-process buffer)
'comint-output-filter)
(add-hook 'comint-preoutput-filter-functions
'remove--truncate-slash nil t))
(setq pt (point))
(comint-send-string buffer expr)
(comint-send-string buffer "\n")
(while (and (= (point) pt)
(> try 0))
(sit-for 0.1)
(setq try (1- try)))
(unless (= (point) pt)
(save-excursion
(forward-line -1)
(setq output (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))))
(if (and replace output)
(when (use-region-p)
(delete-region (region-beginning) (region-end))
(insert output))
(message "Result: %s" output))))
(provide 'bc-mode)