Emacs, C++ code completion for vectors - emacs

I am new to Emacs, and
I have the following code as a sample.
I have installed GNU Emacs 23.1.1 (i386-mingw-nt6.1.7600), installed cedet-1.0pre7.tar.gz. , installed ELPA, and company.
You can find my simple Emacs configuration at the bottom.
The problem is, when I type q[0] in main() and press . (dot), I see the 37 members of the vector, not Person although first_name and last_name are expected. The completion works as expected in the function greet() but it has nothing to do with vector.
My question is, how can I accomplish code completion for vector elements too?
#include <iostream>
#include <vector>
using namespace std;
class Person
{
public:
string first_name;
string last_name;
};
void greet(Person a_person)
{
// a_person.first_name is completed as expected!
cout << a_person.first_name << "|";
cout << a_person.last_name << endl;
};
int main()
{
vector<Person> q(2);
Person guy1;
guy1.first_name = "foo";
guy1.last_name = "bar";
Person guy2;
guy2.first_name = "stack";
guy2.last_name = "overflow";
q[0] = guy1;
q[1] = guy2;
greet(guy1);
greet(guy2);
// cout q[0]. I want to see first_name or last_name here!
}
My Emacs configuration:
;;; This was installed by package-install.el.
;;; This provides support for the package system and
;;; interfacing with ELPA, the package archive.
;;; Move this code earlier if you want to reference
;;; packages in your .emacs.
(when
(load
(expand-file-name "~/.emacs.d/elpa/package.el"))
(package-initialize))
(load-file "~/.emacs.d/cedet/common/cedet.el")
(semantic-load-enable-excessive-code-helpers)
(require 'semantic-ia)
(global-srecode-minor-mode 1)
(semantic-add-system-include "/gcc/include/c++/4.4.2" 'c++-mode)
(semantic-add-system-include "/gcc/i386-pc-mingw32/include" 'c++-mode)
(semantic-add-system-include "/gcc/include" 'c++-mode)
(defun my-semantic-hook ()
(imenu-add-to-menubar "TAGS"))
(add-hook 'semantic-init-hooks 'my-semantic-hook)

This is a known problem with the Semantic analyzer. I currently cannot deal with Template Specialization, which is used in the gcc STL (your problem stems from such a specialization in allocator.h). This has been discussed on the mailing list:
http://thread.gmane.org/gmane.emacs.semantic/2137/focus=2147

GCCSense
An example of completion of a C++ code in Emacs:

Related

Org mode agenda: Reuse code in block agenda

I have an org-agenda-custom-commands like so:
("u" "Unscheduled TODO"
todo ""
((org-agenda-overriding-header "\nUnscheduled TODO")
(org-agenda-skip-function '(org-agenda-skip-entry-if
'scheduled 'deadline 'timestamp
'todo '("BACKBURNER")))))
I would like to reuse the todo part of this agenda view in other block agenda views. So for instance defining an unscheduled-todo list for reuse (pseudocode, not working):
(setq unscheduled-todo '((org-agenda-overriding-header "\nUnscheduled TODO")
(org-agenda-skip-function '(org-agenda-skip-entry-if
'scheduled 'deadline 'timestamp
'todo '("BACKBURNER")))))
(setq org-agenda-custom-commands
'(("u" "Unscheduled TODO"
(todo "" unscheduled-todo))
("c" "Complete View"
((agenda "")
(todo "" unscheduled-todo))))
How do I get the above code to work? I think I have a fundamental misunderstanding of how and when the code and lists are getting evaluated. I've tried a number of configurations of ' and () in both setq and org-agenda-custom-commands, along with append to create lists, but I'd also like to understand what's going on here.
Here's an implementation along the lines of my comment (although I think you really don't need a macro here: a function does just as well or better):
(defun unscheduled-todo ()
'((org-agenda-overriding-header "\nUnscheduled TODO")
(org-agenda-skip-function '(org-agenda-skip-entry-if
'scheduled 'deadline 'timestamp
'todo '("BACKBURNER")))))
(setq org-agenda-custom-commands
; N.B. the character following this comment is a *backquote*
; (the key to the left of the 1 key on a standard US keyboard);
; it is *NOT* a quote (the key to the left of the ENTER key
; on a standard US keyboard).
`(("u" "Unscheduled TODO"
todo "" ,(unscheduled-todo))
("c" "Complete View"
((agenda "")
(todo "" ,(unscheduled-todo))))))
I hope it's correct now (I had to fix it a couple of bugs) but it's only lightly tested.
Note that this uses the backquote mechanism to quote most of the value in the
setq of org-agenda-custom-commands while allowing the evaluation of the call to the macro using the comma mechanism.
EDIT: As #Rorschach points out in a (now deleted) comment, I'm overcomplicating things. You still need backquote and comma, but you can use the variable you defined:
(setq unscheduled-todo
'((org-agenda-overriding-header "\nUnscheduled TODO")
(org-agenda-skip-function '(org-agenda-skip-entry-if
'scheduled 'deadline 'timestamp
'todo '("BACKBURNER")))))
(setq org-agenda-custom-commands
; N.B. the character following this comment is a *backquote*
; (the key to the left of the 1 key on a standard US keyboard);
; it is *NOT* a quote (the key to the left of the ENTER key
; on a standard US keyboard).
`(("u" "Unscheduled TODO"
todo "" ,unscheduled-todo)
("c" "Complete View"
((agenda "")
(todo "" ,unscheduled-todo)))))
The backquote/comma mechanism allows the evaluation of a spliced-in variable just as well as it allows the evaluation of a spliced-in function call.

The value of strings doesn't appear in eclipse mars CDT

Why does the value of strings in Eclipse Mars CDT not appear in the expression or variables windows?
It appears {...} but i want to see the value itself under the value tab.
How can i do this?
What is going on here is CDT is showing the information that GDB is providing to it.
For a trivial program, with the debugger stopped on the line with return 0;
#include <string>
using namespace std;
int main() {
string mystring = "my string here";
return 0;
}
this is what I see in the CDT variable view:
which matches what I see in GDB:
(gdb) p mystring
$1 = {static npos = <optimised out>,
_M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>},
_M_p = 0x602028 "my string here"}}
Pretty Printing C++
However, what I suspect you want is the pretty printers for libstdc++ which makes the variables view look like this:
Create a ~/.gdbinit file with the following contents (updating the path for your machine)
python
import sys
sys.path.insert(0, '/usr/share/gcc-4.8/python/')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
Then point your launch configuration at that gdbinit file and start debugging.
GDB wiki entry on the subject:
https://sourceware.org/gdb/wiki/STLSupport
GCC manual entry:
https://gcc.gnu.org/onlinedocs/libstdc++/manual/debug.html

When creating a customizable value with defcustom, how can I validate users' entries?

I'm writing an elisp file that's evolving into a package, so I'm translating some of its variables into defcustom statements and documenting them. A few of these defcustom variables are related, and I'd like to validate values entered through the Customize system to ensure that the relationships hold true.
Here's an example of what I have:
(defcustom widget-canonical-names '("my_widget" . "widget_assembly 8911_j4")
"Documentation"
:type '(alist :key-type (string :tag "Widget's short name")
:value-type (string :tag "Full widget name"))
:risky nil
:group 'widgets)
(defcustom widget-colors '("my_widget" . "brown")
"Documentation"
:type '(alist :key-type (string :tag "Widget's short name")
:value-type (color :tag "color of the widget"))
:risky nil
:group 'widgets)
(defcustom widget-paths '("my_widget" . "~/widgets")
"Documentation"
:type '(alist :key-type (string :tag "Widget's short name")
:value-type (directory :tag "support files for widget"))
:risky nil
:group 'widgets)
So there are widgets and they have various settings, and I need to be able to access an arbitrary setting for a widget by knowing just the widget's short name. I'd like to make a validation function of some kind (googling around for "emacs defcustom validate" hasn't helped, unfortunately) such that if the user enters a widget name in widget-paths or widget-colors that is not in the widget-canonical-names list, they get an "are you sure?" warning and are cautioned about entering mismatched names. Can I attach such a validation function to my defcustoms? If so, what's the syntax for that?
Of course, what would be ideal would be to just make the user enter the short name once, but I can't figure out how to do that from the 'Composite Types' elisp documentation. So an even better answer to my question would tell me how to arrange a defcustom that sets up a data structure similar to this Python dict:
customized_widgets = {
"my_widget": { "canonical_name": "widget_assembly 8911_j4",
"widget_color": "brown",
"widget_path": "~/widgets",
},
"another_widget": { "canonical_name" : "widget_obsolete 11.0",
"widget_color": "blue",
"widget_path": "~/blue_widgets",
},
}
So: how can I get the behavior I want, where settings are grouped according to the data that'll be used to access them, or where a validation function warns users when they might be entering inconsistent data?
This will define the closest Emacs equivalent of that Python structure, with dicts represented as alists, and fixed keys of the inner dict represented as symbols.
(defcustom my-customized-widgets ()
"My widget customization alist"
:type '(alist
:tag "Widgets"
:key-type (string :tag "Short name")
:value-type
(set
:format "%v"
:entry-format "%b %v"
(cons :format "%v"
(const :format "" widget-canonical-name)
(string :tag "CName"))
(cons :format "%v"
(const :format "" widget-color)
(color :tag "Color"))
(cons :format "%v"
(const :format "" widget-path)
(directory :tag " Path"))))
:group 'widgets)

discrepancy between build result and Problems view in Eclipse CDT

I'm using Eclipse 4.2, with CDT, and MinGW toolchain on a Windows machine (although I've a feeling the problem has nothing to do with this specific configuration). The G++ compiler is 4.7
I'm playing with c++11 features, with the following code:
#include <iostream>
#include <iomanip>
#include <memory>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
int main( int argc, char* argv[] )
{
vector<int> v { 1, 2, 3, 4, 5, 6, 7 };
int x {5};
auto mark = remove_if( v.begin(), v.end(), [x](int n) { return n<x; } );
v.erase( mark, v.end() );
for( int x : v ) { cout << x << ", "; }
cout << endl;
}
Everything is very straight forward and idiomatic c++11. The code compiles with no problems on the command line (g++ -std=c++11 hello.cpp).
In order to make this code compile In eclipse, I set the compiler to support C++11:
Properties -> C/C++ Build -> Settings -> Miscellaneous -> Ohter Flags:
I'm adding -std=c++11
Properties -> C/C++Build -> Discovery Options -> Compiler invocation arguments:
Adding -std=c++11
That's the only change I did to either the global preferences or to the project properties.
First Question: Why do I've to change the flags in two places? When each compiler flags is used?
If I hit Ctrl-B, the project will build successfully, as expected, and running it from within eclipse show the expected result (It prints: '5, 6, 7,').
However, the editor view shows red marks of error on both the 'remove_if' line, and the 'v.erase' line. Similarly, the Problems view shows I've these two problems. Looking at the details of the problem, I get:
For the remove_if line: 'Invalid arguments. Candidates are: #0 remove_if(#0, #0, #1)
For the erase line: 'Invalid arguments Candidates are: '? erase(?), ? erase(?,?)'
Second questions: It appears there are two different builds: one for continues status, and one for the actual build. Is that right? If so, do they have different rule (compilation flags, include paths, etc.)?
Third question: In the problem details I also see: 'Name resolution problem found by the indexer'. I guess this is why the error message are so cryptic. Are those messages coming from MinGW g++ compiler or from Eclipse? What is this Name resolution? How do I fix?
Appreciate your help.
EDIT (in reply to #Eugene): Thank you Eugene. I've opened a bug on Eclipse. I think that C++11 is only partially to blame. I've cleaned my code from C++11 stuff, and removed the -std=c++11 flag from both compilation switch. And yet, the CodAn barks on the remove_if line:
int pred( int n ) { return n < 5; }
int main( int argc, char* argv[] )
{
vector<int> v;
for( int i=0; i<=7; ++i ) {
v.push_back( i );
}
vector<int>::iterator mark = remove_if( v.begin(), v.end(), pred );
v.erase( mark, v.end() );
for( vector<int>::iterator i = v.begin(); i != v.end(); ++i ) {
cout << *i << ", ";
}
cout << endl;
}
The code compiles just fine (with Ctrl-B), but CodAn doesn't like the remove_if line, saying: Invalid Arguments, Candidates are '#0 remove_if(#0,#0,#1)'.
This is a very cryptic message - it appears it misses to substitute arguments in format string (#0 for 'iterator' and #1 for 'predicate'). I'm going to update the bug.
Interestingly, using 'list' instead of 'vector' clears up the error.
However, as for my question, I'm curious about how the CodAn work. Does it uses g++ (with a customized set of flags), or another external tool (lint?), or does it do it internally in Java? If there is a tool, how can I get its command line argument, and its output?
Build/Settings - these flags will be included into your makefile to do actual build. Build/Discovery - these flags will be passed to a compiler when "scanner settings" are discovered by IDE. IDE will run compiler in a special mode to discover values of the predefined macros, include paths, etc.
I believe, the problems you are seeing are detected by "Codan". Codan is a static analysis built into the CDT editor, you may find its settings on "C/C++ General"/"Code Analysis". You should report the problem to the bugs.eclipse.org if you feel the errors shown are bogus. Note that CDT does not yet support all C++11 features.

Libraries for making Eclipse plugin development easier?

I'm just learning Eclipse plugin development, currently for the purpose of adding a few simple custom commands that nobody else has bothered to implement. I've noticed that the Eclipse plugin API... leaves a lot to be desired. Are there any open source libraries which attempt to improve the plug-in development experience? (I've started to idly speculate about writing my own...).
I know Eclipse 4.0 is supposed to fix some of these issues long-term, but I'm unlikely to be able to move to it at work any time soon.
Edit
Here's an example of what I mean. This is an implementation of "hungry delete" functionality for emacs:
(defmacro hungry-delete-backward (&optional limit)
(if limit
`(let ((limit (or ,limit (point-min))))
(while (progn
;; skip-syntax-* doesn't count \n as whitespace..
(skip-chars-backward " \t\n\r\f\v" limit)
(and (eolp)
(eq (char-before) ?\\)
(> (point) limit)))
(backward-char)))
'(while (progn
(skip-chars-backward " \t\n\r\f\v")
(and (eolp)
(eq (char-before) ?\\)))
(backward-char))))
And here is part of an equivalent implementation for Eclipse, not including the manifest file, plugin.xml, or the Activator for the plugin:
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IEditorPart editor = HandlerUtil.getActiveEditorChecked(event);
if (!(editor instanceof ITextEditor)) return null;
ITextEditor ite = (ITextEditor) editor;
IDocumentProvider idp = ite.getDocumentProvider();
IDocument doc = idp.getDocument(ite.getEditorInput());
ISelection selection = ite.getSelectionProvider().getSelection();
if (!(editor instanceof ITextSelection)) return null;
ITextSelection its = (ITextSelection) selection;
int currentCursorPosition = its.getOffset();
int deletionStart, deletionEnd;
if (its.getLength() == 0) {
deletionStart = currentCursorPosition - 1;
deletionEnd = currentCursorPosition;
FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(doc);
while (Character.isWhitespace(frda.charAt(deletionStart)) && deletionStart > 0) {
deletionStart--;
}
if (deletionStart != 0 && deletionStart + 1 != deletionEnd) deletionStart++;
} else {
deletionStart = its.getOffset();
deletionEnd = its.getOffset() + its.getLength();
}
int deletionLength = deletionEnd - deletionStart;
try {
doc.replace(deletionStart, deletionLength, "");
} catch (BadLocationException ble) {
// Bad location, just ignore it.
}
return null;
}
The chunk of boilerplate at the top of the Java version, for example, could be easily replaced by a library.
I think you're looking for something like Groovy-Monkey or Eclipse-Monkey, which allow you to do scripting to plug into Eclipse. I don't know much about either project, nor do I know if they are actively maintained (actually I know that Eclipse-Monkey is no longer maintained). But, these allow you to do emacs-like scripting inside of Eclipse.