summaryrefslogtreecommitdiffstats
path: root/fcomp.el
diff options
context:
space:
mode:
Diffstat (limited to 'fcomp.el')
-rw-r--r--fcomp.el91
1 files changed, 58 insertions, 33 deletions
diff --git a/fcomp.el b/fcomp.el
index 5b9524b..66eaf7c 100644
--- a/fcomp.el
+++ b/fcomp.el
@@ -47,10 +47,18 @@
(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 "")
+;; User variables
+
+(defvar fcomp-path "/usr/local/bin/fcomp"
+ "Path to fcomp executable.")
+(defvar fcomp-min-word-length 3
+ "Minimum word length to be counted as token.")
+(defvar fcomp-extra-valid-chars ""
+ "Extra valid chars for the tokenization.")
+(defvar fcomp-extra-invalid-chars ""
+ "Extra invalid chars for the tokenization.")
+(defvar fcomp-completing-read-func 'completing-read
+ "Function to call when showing results to user.")
(defun empty-string-p (string)
"Return true if the string is empty or nil. Expects string."
@@ -77,12 +85,13 @@
(result nil))
(if (empty-string-p fcomp--output)
nil
- (setq result (ido-completing-read
- "Complete:"
- (remove "" (split-string fcomp--output "\n"))
- nil nil
- initial-input)))
- (if (empty-string-p result)
+ (setq result
+ (funcall fcomp-completing-read-func
+ "Complete:"
+ (remove "" (split-string fcomp--output "\n"))
+ nil nil
+ initial-input)))
+ (if (empty-string-p result)
(message "%s" "No completion candidates")
(insert (string-remove-prefix
(if (empty-string-p initial-input)
@@ -92,6 +101,38 @@
(setq fcomp--query "")
(setq fcomp--output "")))
+(defun fcomp--autocomplete (word point)
+ "Start fcomp process and orchistrate it's output."
+ (let ((term (if (empty-string-p word)
+ (format "%s" "-au")
+ (format "%s" word)))
+ (input-len (buffer-size)))
+ (fcomp--start word)
+ (setq proc
+ (start-process
+ "fcomp"
+ nil
+ (format "%s" fcomp-path)
+ "-w" (format "%s" fcomp-min-word-length)
+ "-v" (format "\"%s\"" fcomp-extra-valid-chars)
+ "-i" (format "\"%s\"" fcomp-extra-invalid-chars)
+ term))
+ (set-process-filter proc #'fcomp--filter)
+ (set-process-coding-system proc 'raw-text-unix 'raw-text-unix)
+ (process-put proc 'input-word word)
+ (if (< input-len 50000)
+ (process-send-region proc (point-min) (point-max))
+ (process-send-region proc
+ (if (< (- point 25000) 0) (point-min) (- point 25000))
+ (if (> (+ point 25000) input-len) (point-max) (+ point 25000))))
+ (process-send-string proc "\n")
+ (process-send-eof proc)
+ (if (accept-process-output proc nil nil t)
+ (fcomp--handle-output)
+ (progn
+ (message "No completion for %s" word)
+ (delete-process proc)))))
+
(defun fcomp-autocomplete ()
"Autocompete using fcomp.c
@@ -99,36 +140,20 @@ When `thing-at-point' is a word, autocomplete it based on buffer contents.
When invoked with no `thing-at-point' show a list of all possible candidates.
+The syntax-table is temporarily modified and includes '_' '-' and
+`fcomp-extra-valid-chars' as part of a word.
+
Requires fcomp. Get it at URL `https://ubuntos.dynu.net/git/fcomp'"
;; Thanks Dan for the syntax-table trick
;; https://emacs.stackexchange.com/questions/9583/how-to-treat-underscore-as-part-of-the-word
(interactive)
(let ((table (copy-syntax-table (syntax-table))))
(modify-syntax-entry ?- "w" table)
+ (modify-syntax-entry ?_ "w" table)
+ (when (not (empty-string-p fcomp-extra-valid-chars))
+ (mapc (lambda (char) (modify-syntax-entry char "w" table)) fcomp-extra-valid-chars))
(with-syntax-table table
- (fcomp--autocomplete (thing-at-point 'word 'no-properties)))))
-
-(defun fcomp--autocomplete (word)
- "Start fcomp process and orchistrate it's output."
- (let ((term (if (empty-string-p word)
- (format "%s" "-a")
- (format "%s" word)))
- (input (buffer-substring-no-properties (point-min) (point-max))))
- (fcomp--start word)
- (set-process-filter
- (start-process
- "fcomp"
- nil
- (format "%s"fcomp-path)
- "-w" (format "%s" fcomp-min-word-length)
- "-F" (format "%s" 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)
- (accept-process-output (get-process "fcomp"))
- (fcomp--handle-output)))
+ (fcomp--autocomplete (thing-at-point 'word 'no-properties) (point)))))
(provide 'fcomp)
;;; fcomp.el ends here