help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C Headers completion candidates


From: B.V. Raghav
Subject: Re: C Headers completion candidates
Date: Sun, 17 Jul 2016 14:10:57 +0530
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

Drew Adams <address@hidden> writes:

[snip]
>
>> Step 1. Identifying the context
>> --- Example
>> #include <vec|>
>> ---
>> `|' represents the cursor here
>> I think I can code the regexp here

I have no experience with using using the point so far inside of an
interactive function. But I think it is do-able. Albeit, not with a
regexp, but somehow.

I leave it for another rainy day.

[snip]
>
>> Step 2. Set of completion candidates
>> Search the $INCLUDE environment variable, with `visited' flags on the
>> folders that have been visited; and create the set of completion
>> candidates, one in a line

I realised the environment variables, that I had initialized in
~/.bashrc are not avaliable at the X server environment (probably, not
even with ~/.profile). So I hard coded the list, and copy-pasted from
the bashrc. 

(require 'cl-lib)
(defun c-include-path()
    (cl-reduce #'append
               (mapcar #'parse-colon-path
                       `("/usr/include/c++/5:/usr/include/c++/4.9"
                         ,(format "%s/usr/include" (getenv "HOME"))
                         
"/usr/include:/usr/local/include:/usr/include/x86_64-linux-gnu"))))

The order of directories is important here.

May be it looks dirty, but work for now.

Then, I retrieved all the files at first level depth of each of this
list. And mapped all the directory elements to their absolute paths.

;; (c-include-path)
(defun c-include-libs-raw(path)
  (cl-reduce #'append
             (mapcar (lambda (x)
                       (mapcar (lambda (y)
                                 (let ((long-y (format "%s%s" x y)))
                                   (if (file-accessible-directory-p long-y)
                                       long-y
                                     y)))
                               (when (file-accessible-directory-p x)
                                 (directory-files x))))
                     path)))

Then I removed the unnecessary files

(defun sanitize-dir-read (path-list)
  (cl-remove-if (lambda(x)
                  (or (string-suffix-p "." x)
                      (string-suffix-p "~" x)
                      (string-prefix-p "_" x)))
                path-list))

[snip]
>
>> Step 3. Tell Icicles to invoke these set of completion candidates,
>> when the context is active. How? I do not know.
[snip]

Here I used the completion in two steps. Though not as `smart', or
seamless; but works

(defun c-header()
  (let* ((include-path-list (c-include-path))
         (lib-name-list (sanitize-dir-read (c-include-libs-raw 
(c-include-path))))
         (selected-path (completing-read "Library: " lib-name-list))
         (final-path (if (file-accessible-directory-p selected-path)
                         (read-file-name "Header: " (format "%s/" 
selected-path))
                       selected-path))
         (inclusive-path (which-c-prefix final-path include-path-list))
         (include-file-name (file-relative-name final-path inclusive-path)))
    include-file-name))

First completing the name from the files (and folders) at first
level. Then using the response to aid the next level completion.

Most C-libraries, that I require, are first class citizens, and are
completed within the first level of completion. Like <vector>,
<utility>, <unordered_map>, <iterator> --- all completed using the same
set of functions above, work like charm with Icicles.

For other libraries, I have to use the name of the library and S-TAB,
like <boost/algorithm/string/case_conv.hpp> This is a little longer, but
way faster (and cleaner) than what I used to do earlier. Either, I copy
pasted from the earlier files, or used a snippet.

Way forward, I want to implement this:
1. Implement a list of completions, at the user level; quite like
   persistent completions; 

   a. Any completion, after being pruned for prefix (before being
      returned), goes as into the list of completions, without
      duplicacy.

   b. This list is read, and appened to the lib-name-list; before the
      first completion is invoked.

Thanks,
r
-- 
(B.V. Raghav)
Ph.D. Student,
Design Programme, IIT Kanpur




reply via email to

[Prev in Thread] Current Thread [Next in Thread]