From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Emanuel Berg Newsgroups: gmane.emacs.help Subject: Re: Help improving an elisp function Date: Fri, 03 Apr 2015 01:45:17 +0200 Organization: Aioe.org NNTP Server Message-ID: <87mw2qumb6.fsf@debian.uxu> References: <7LcTw.28088$kA6.5489@fx46.am4> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1428018333 5716 80.91.229.3 (2 Apr 2015 23:45:33 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 2 Apr 2015 23:45:33 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Apr 03 01:45:20 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YdonW-0005pK-AV for geh-help-gnu-emacs@m.gmane.org; Fri, 03 Apr 2015 01:45:18 +0200 Original-Received: from localhost ([::1]:60468 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YdonV-0007LI-H8 for geh-help-gnu-emacs@m.gmane.org; Thu, 02 Apr 2015 19:45:17 -0400 Original-Path: usenet.stanford.edu!news.kjsl.com!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.mixmin.net!aioe.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 98 Original-NNTP-Posting-Host: feB02bRejf23rfBm51Mt7Q.user.speranza.aioe.org Original-X-Complaints-To: abuse@aioe.org User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-Notice: Filtered by postfilter v. 0.8.2 Cancel-Lock: sha1:vpum66htW1hboLm7/NVdnPD756Y= Mail-Copies-To: never Original-Xref: usenet.stanford.edu gnu.emacs.help:211235 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:103517 Archived-At: Glen Stark writes: > But it's pretty awful. What I'd really like to do is > have one function that looks up the buffer-name in > question, and inserts the include statement, without > jumping there. You are exactly right the problem is the user-level mucking around with the buffers. I could help you with that but I don't get the ggtags to work right away and as I never felt the need for that unless you provide me with step by step instructions I'll leave it at that. But: for all on-the-top user-level mucking around with buffers there are the equivalents to do that below where it isn't noticed, so you should find those equivalents. Also, the byte compiler (see the Emacs man page) can help you with some bad habits - often it tells you what you should use as well, and the buffer issue is one where the compiler is strict :) But... as for your problem, in principle it is absolutely correct to automatize the workflow, however in this case if you do any amount of C++ henceforth you will very rapidly not need this anyway, because you will know where stuff is and putting the includes in place manually won't bother you. So what you do is over-engineering, but we will of course help you nonetheless if you continue to post. > (require ggtags) (require 'ggtags) > (setq gas-cpp-include-path) Better than a global variable is a `let' in the function where it belongs (i.e., is used): (let ((gas-cpp-include-path ...) (other-var ...)) ; do stuff with the vars ) > (defun find-what-provides () > (interactive) > (ggtags-find-definition (thing-at-point `symbol)) > ) No need to backtick "symbol", (thing-at-point 'symbol) is fine. > (defun insert-missing-include () > (interactive) > (setq gas-cpp-include-path (buffer-file-name)) > (kill-buffer) > (beginning-of-buffer) > (while (re-search-forward "#include \".*\"" nil t)) > (insert (concat "\n#include \"" > (file-name-nondirectory gas-cpp-include-path) > "\"\n")) > ) Apart from what I mentioned - let, and the buffers - I've found that `format' is more manageable than `concat', especially when it gets complicated. With format, you can specify the structure first, which will communicate the purpose (to you), then you only set up the data once (all to the right) and never bother with it again: (format "purpose and structure" data_1 ... data_n) > (global-set-key (kbd "") 'find-what-provides) > (global-set-key (kbd "") 'insert-missing-include) You can type those as [f9] instead of (kbd "") - try evaluating it, if you are unsure! But: Those keys aren't good. You don't want to let go of your hands from the typing position, which is the "asdf" and "jkl;" keys for the left and right hand, respec... uhm, you get it. Tho seemingly as single key is faster and easier and less error-prone than a combination, that isn't so with time and practice if the combination is close and short, because then the "reach, find and reset" can be eliminated. Remember, speed kills! Good luck! Come back with more questions, of course. -- underground experts united http://user.it.uu.se/~embe8573