summaryrefslogtreecommitdiffstats
path: root/fcomp.el
blob: 4e046d97656476259262c9c12ea1aea1ea7df021 (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
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
;;; fcomp.el --- fcomp front-end                     -*- lexical-binding: t; -*-

;; Copyright (C) 2019  Anastasis Grammenos

;; Author: Anastasis Grammenos <anastasis.gramm2@gmail.com>
;; Keywords: lisp
;; Version: 0.0.1

;; 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 <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Emacs front-end for fcomp. Get if from https://ubuntos.dynu.net/git/fcomp

;;; Install:

;; git clone https://ubuntos.dynu.net/git/fcomp && cd fcomp
;; make && sudo ln -s fcomp /usr/local/bin/fcomp # or mv

;; In the emacs init file:
;; (add-to-list 'load-path "/your/custom/path")
;; (require 'fcomp)
;; (setq fcomp-path "/your/custom/path/fcomp")
;; (global-set-key (kbd "M-i") 'fcomp-autocomplete)

;; or with use-package
;; (use-package fcomp
;;   :load-path "/your/custom/path"
;;   :bind (("M-i" . fcomp-autocomplete))
;;   :config
;;   (setq fcomp-path "/your/custom/path/fcomp"))


;;; Code:

(require 'ido)
(require 'subr-x)

(defvar fcomp-path "/usr/local/bin/fcomp")
(defvar fcomp-min-word-length 3)
(defvar fcomp-extra-valid-chars "")
(defvar fcomp-extra-invalid-chars "")

(defun pr ()
  (message "Status: %s" (process-status "fcomp")))

;; (thing-at-point 'word 'no-properties)
(defun fcomp-filter (proc str)
  (let* ((fcomp-pre
          (if (null (process-get proc 'input-word))
              nil
            (format "%s" (process-get proc 'input-word)))))
    ;;    (enable-recursive-minibuffers t))
    ;; (setq result (substring result (* -1 (- (length result) (length word))) nil))
    (progn
      (pr)
      (setq result
            (ido-completing-read
             "Complete:"
             (remove "" (split-string str "\n"))
             nil nil
             fcomp-pre))
      (if (null result)
          nil
        (insert
         (string-remove-prefix
          (if (null fcomp-pre)
              (format "%s" "")
            (format "%s" fcomp-pre))
          result))))))

(defun fcomp-autocomplete ()
  (interactive)
  (fcomp--autocomplete (thing-at-point 'word 'no-properties)))

(defun fcomp--autocomplete (word)
  "Do stuff and ac"
  (let ((term (if (null word)
                  (format "%s" "-au")
                (format "%s" word)))
        (input (buffer-substring-no-properties (point-min) (point-max))))
    (with-temp-buffer
      (set-process-filter
       (start-process
        "fcomp"
        nil
        fcomp-path
        "-w" (format "%s" fcomp-min-word-length)
        "-F" input
        "-v" (format "%s" fcomp-extra-valid-chars)
        "-i" (format "%s" fcomp-extra-invalid-chars)
        term)
       #'fcomp-filter)
      (process-put (get-process "fcomp") 'input-word word))))

(provide 'fcomp)
;;; fcomp.el ends here