Emacs automatically fills in header files

Source: Internet
Author: User

Target
1. When writing C/C ++ code, you can automatically complete the header file. Note that it is completed, that is, we need to enter at least a few characters to help complete.
2. If the header file is stored in the subdirectory of the search path, you can automatically list the files in the subdirectory and add these files to the candidate list for completion.
3. After completion, You can automatically determine whether to use # include or # include "FILE ".
Solution
In an article on emacser.org, this solution is mainly implemented using abbrev-mode and skeleton-mode. In this method, we enter inc and Press space, the system prompts you to enter the file name.
The Code is as follows:
View CodeLISP
; Install abbrev
(Mapc
(Lambda (mode)
(Define-abbrev-table mode '(
("Inc" skeleton-include 1)
)))
'(C-mode-abbrev-table c ++-mode-abbrev-table ))
 
; Input inc, You can automatically prompt to enter the file name, you can automatically complete.
(Define-skeleton-include
"Generate include <> """
> "# Include <"
(Completing-read "Include File :"
(Mapcar # '(lambda (f) (list f ))
(Apply 'append
(Mapcar
# '(Lambda (dir)
(Directory-files dir ))
(List "/usr/include"
"/Usr/local/include"
"/Usr/include/g ++-3 ")))))
"> ")
This method has several limitations:
• The search path of the header file is dead. If a directory does not exist, the above Code will report an error and cannot be completed.
• Files in subdirectories of the search path cannot be completed (that is, the previous AIM 2 ).
• When # include a file, it is not determined whether the symbol <> or "" should be used (that is, the previous AIM 3)
The solution is not complex. The corresponding solution is as follows:
• You can use some method to automatically obtain the include search path from the system. For example, the semantic-gcc-get-include-paths function provided by CEDET.
• Redefine the button "/" in minibuffer-mode. Bind it to a function used to search for and expand a directory and update minibuffer-completion-table (minibuffer-completion-table is a candidate list that is supplemented by minibuffer-mode ).
• Do not use <> or "in skeleton-include. We can use a special flag to determine the symbols to be used based on the header file path at the end of skeleton-include.
The code is implemented as follows:
View CodeLISP
; Input inc, You can automatically prompt to enter the file name, you can automatically complete.
; Provided by yangyingchao@gmail.com
(Mapc
(Lambda (mode)
(Define-abbrev-table mode '(
("Inc" skeleton-include 1)
)))
'(C-mode-abbrev-table c ++-mode-abbrev-table ))
 
(Defconst yc/inc-dir-list
(Append (semantic-gcc-get-include-paths "c ++") '("./") "nil ")
(Defvar inc-minibuffer-compl-list nil "nil ")
 
(Defun yc/update-minibuffer-complete-table ()
"Complete minibuffer"
(Interactive)
(Let (prompt (minibuffer-prompt ))
(Comp-part (minibuffer-contents-no-properties )))
(When (and (string = "Include File:" prompt)
(> (Length comp-part) 0 ))
(Setq minibuffer-completion-table
(Append minibuffer-completion-table
(Let (inc-files nil)
(Dirname nil)
(Tmp-name nil ))
(Mapc
(Lambda (d)
(Setq dirname (format "% s/% s" d comp-part ))
(When (file-exists-p dirname)
(Mapc
(Lambda (x)
(When (not (or (string = "." x)
(String = ".." x )))
(Setq tmp-name (format "% s/% s" comp-part x ))
(Add-to-list 'inc-files tmp-name )))
(Directory-files dirname ))))
Yc/inc-dir-list)
Inc-files )))))
(Insert "/"))
 
(Let (map minibuffer-local-completion-map ))
(Define-key map "/" 'yc/update-minibuffer-complete-table ))
 
(Defun yc/update-inc-marks ()
"Description"
(Let (statement (buffer-substring-no-properties
(Point-at-bol) (point-at-eol )))
(Inc-file nil)
(To-begin nil)
(To-end nil)
(Yc/re-include
(Rx "# include" (+ ascii) "| XXX |" (group (+ ascii) "| XXX | ")))
(When (string-match yc/re-include statement)
(Setq inc-file (match-string 1 statement ))
(If (file-exists-p (format "./% s" inc-file ))
(Setq to-begin "\" "to-end "\"")
(Setq to-begin "<" to-end "> ")
)
(Move-beginning-of-line 1)
(Kill-line)
(Insert (format "# include % s" to-begin inc-file to-end ))
(Move-end-of-line 1 ))))
 
(Define-skeleton-include
"Generate include <> """
> "# Include | XXX |"
(Completing-read
"Include File :"
(Mapcar
(Lambda (f) (list f ))
(Apply
'Append
(Mapcar
(Lambda (dir)
(Directory-files
Dir nil
"\ (\. H \\)? "))
Yc/inc-dir-list ))))
"| XXX |"
(Yc/update-inc-marks ))
Use and effect
The usage is simple:
1. Add the above Code to the Emacs configuration file and open a C/C ++ program,
2. Enter inc and press a space. Then, enter the names of some header files in minibuffer and use the TAB to complete the configuration.
3. If the header file is in a subdirectory, enter the directory name and enter "/". In this way, the content in the subdirectory will be added to the completed candidate list, and then you can continue to complete the TAB.
4. After confirming that the content entered in minibuffer is correct, press enter and skeleton-include will automatically update the tag.
:
 
Which of the following are:

Include system files

Include custom files

After all


Author bigclean

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.