]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-base.el
Merge branch 'master' into new_ui
[factor.git] / misc / fuel / fuel-base.el
1 ;;; fuel-base.el --- Basic FUEL support code
2
3 ;; Copyright (C) 2008  Jose Antonio Ortega Ruiz
4 ;; See http://factorcode.org/license.txt for BSD license.
5
6 ;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
7 ;; Keywords: languages
8
9 ;;; Commentary:
10
11 ;; Basic definitions likely to be used by all FUEL modules.
12
13 ;;; Code:
14
15 (defconst fuel-version "1.0")
16
17 ;;;###autoload
18 (defsubst fuel-version ()
19   "Echoes FUEL's version."
20   (interactive)
21   (message "FUEL %s" fuel-version))
22
23 \f
24 ;;; Customization:
25
26 ;;;###autoload
27 (defgroup fuel nil
28   "Factor's Ultimate Emacs Library"
29   :group 'language)
30
31 \f
32 ;;; Emacs compatibility:
33
34 (eval-after-load "ring"
35   '(when (not (fboundp 'ring-member))
36      (defun ring-member (ring item)
37        (catch 'found
38          (dotimes (ind (ring-length ring) nil)
39            (when (equal item (ring-ref ring ind))
40              (throw 'found ind)))))))
41
42 (when (not (fboundp 'completion-table-dynamic))
43   (defun completion-table-dynamic (fun)
44     (lexical-let ((fun fun))
45       (lambda (string pred action)
46         (with-current-buffer (let ((win (minibuffer-selected-window)))
47                                (if (window-live-p win) (window-buffer win)
48                                  (current-buffer)))
49           (complete-with-action action (funcall fun string) string pred))))))
50
51 (when (not (fboundp 'looking-at-p))
52   (defsubst looking-at-p (regexp)
53     (let ((inhibit-changing-match-data t))
54       (looking-at regexp))))
55
56 \f
57 ;;; Utilities
58
59 (defun fuel--shorten-str (str len)
60   (let ((sl (length str)))
61     (if (<= sl len) str
62       (let* ((sep " ... ")
63              (sepl (length sep))
64              (segl (/ (- len sepl) 2)))
65         (format "%s%s%s"
66                 (substring str 0 segl)
67                 sep
68                 (substring str (- sl segl)))))))
69
70 (defun fuel--shorten-region (begin end len)
71   (fuel--shorten-str (mapconcat 'identity
72                                 (split-string (buffer-substring begin end) nil t)
73                                 " ")
74                      len))
75
76 (defsubst fuel--region-to-string (begin &optional end)
77   (mapconcat 'identity
78              (split-string (buffer-substring-no-properties begin
79                                                            (or end (point)))
80                            nil
81                            t)
82              " "))
83
84 (defsubst empty-string-p (str) (equal str ""))
85
86 (defun fuel--string-prefix-p (prefix str)
87   (and (>= (length str) (length prefix))
88        (string= (substring-no-properties 0 (length prefix) str)
89                 (substring-no-properties prefix))))
90
91 (defun fuel--respecting-message (format &rest format-args)
92   "Display TEXT as a message, without hiding any minibuffer contents."
93   (let ((text (format " [%s]" (apply #'format format format-args))))
94     (if (minibuffer-window-active-p (minibuffer-window))
95         (minibuffer-message text)
96       (message "%s" text))))
97
98 (provide 'fuel-base)
99 ;;; fuel-base.el ends here